-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Script to list accounts with lower free than frozen balance (#35)
- Loading branch information
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// This script is expected to run against a parachain network (using launch.ts script) | ||
import yargs from "yargs"; | ||
import { table } from "table"; | ||
|
||
import { getAccountIdentity } from "../utils/monitoring"; | ||
import { getApiFor, NETWORK_YARGS_OPTIONS } from ".."; | ||
import { bnMax } from "@polkadot/util"; | ||
import Web3 from "web3"; | ||
|
||
const argv = yargs(process.argv.slice(2)) | ||
.usage("Usage: $0") | ||
.version("1.0.0") | ||
.options({ | ||
...NETWORK_YARGS_OPTIONS, | ||
}).argv; | ||
|
||
const main = async () => { | ||
// Instantiate Api | ||
const api = await getApiFor(argv); | ||
const blockHash = await api.rpc.chain.getBlockHash(); | ||
const apiAt = await api.at(blockHash); | ||
|
||
const allLocks = await apiAt.query.balances.locks.entries(); | ||
|
||
const addressesToCheck: string[] = []; | ||
|
||
allLocks.map((lock) => { | ||
if (lock[1].length > 0) { | ||
addressesToCheck.push(`0x${lock[0].toHex().slice(-40)}`); | ||
} | ||
}); | ||
|
||
const systemAccounts = await apiAt.query.system.account.multi(addressesToCheck); | ||
|
||
const identities = await Promise.all(addressesToCheck.map((d) => getAccountIdentity(api, d))); | ||
|
||
const affectedAccounts = []; | ||
systemAccounts.map(async ({ data: { free, reserved, frozen } }, idx) => { | ||
if (free.lt(frozen)) { | ||
const transferableNew = free.add(reserved).sub(bnMax(reserved, frozen)); | ||
affectedAccounts.push([ | ||
addressesToCheck[idx], | ||
identities[idx], | ||
Web3.utils.fromWei(free), | ||
Web3.utils.fromWei(reserved), | ||
Web3.utils.fromWei(frozen), | ||
Web3.utils.fromWei(frozen.sub(free).toString()), | ||
]); | ||
} | ||
}); | ||
|
||
const tableData = ( | ||
[ | ||
["Account", "Identity", "Free", "Reserved", "Frozen", "Negative Transferable Balance"], | ||
] as any[] | ||
).concat(affectedAccounts); | ||
|
||
console.log(`preparing the table: ${tableData.length} entries`); | ||
console.log( | ||
table(tableData, { | ||
drawHorizontalLine: (lineIndex: number) => | ||
lineIndex == 0 || lineIndex == 1 || lineIndex == tableData.length, | ||
columns: [ | ||
{ alignment: "left" }, | ||
{ alignment: "left" }, | ||
{ alignment: "left" }, | ||
{ alignment: "right" }, | ||
{ alignment: "right" }, | ||
], | ||
}) | ||
); | ||
await api.disconnect(); | ||
}; | ||
|
||
main(); |