From 41e0b9475214cddc05e20b13f8702ab3fe8da48c Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Fri, 19 Aug 2022 14:05:04 +1200 Subject: [PATCH] update --- package.json | 1 + src/log.ts | 18 +++++++++++++----- src/scripts/top-holders.ts | 35 +++++++++++++++++++++++++++++++++++ yarn.lock | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 src/scripts/top-holders.ts diff --git a/package.json b/package.json index 422697a..37f032e 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "@acala-network/sdk-wallet": "^4.1.6-29", "@acala-network/types": "^4.1.6-29", "@fast-csv/format": "^4.3.5", + "@fast-csv/parse": "^4.3.6", "@open-web3/util": "^2.0.1", "@polkadot/api": "^9.1.1", "async": "^3.2.4", diff --git a/src/log.ts b/src/log.ts index 9b13dc1..4b13578 100644 --- a/src/log.ts +++ b/src/log.ts @@ -8,6 +8,10 @@ import BN from 'bn.js' import { config } from './runner' +let defaultPrecision = 3 + +export const setDefaultPrecision = (precision: number) => (defaultPrecision = precision) + export const logFormat = (x: any): any => { if (x == null) { return x @@ -45,7 +49,11 @@ export const formatDecimal = (x: number | BN | string | FixedPointNumber, length return Math.round(n * 10 ** length) / 10 ** length } -export const formatBalance = (x: number | BN | string | FixedPointNumber | bigint | undefined | null, decimal = 12) => { +export const formatBalance = ( + x: number | BN | string | FixedPointNumber | bigint | undefined | null, + decimal = 12, + precision = defaultPrecision +) => { let n if (x == null) { return '-' @@ -60,17 +68,17 @@ export const formatBalance = (x: number | BN | string | FixedPointNumber | bigin if (config.output === 'console') { if (n > 1e9) { - return `${formatDecimal(n / 1e9, 2)}B` + return `${formatDecimal(n / 1e9, precision)}B` } if (n > 1e6) { - return `${formatDecimal(n / 1e6, 2)}M` + return `${formatDecimal(n / 1e6, precision)}M` } if (n > 1e3) { - return `${formatDecimal(n / 1e3, 2)}K` + return `${formatDecimal(n / 1e3, precision)}K` } } - return formatDecimal(n).toString() + return formatDecimal(n, precision).toString() } export const table = (data: any) => { diff --git a/src/scripts/top-holders.ts b/src/scripts/top-holders.ts new file mode 100644 index 0000000..8f7366f --- /dev/null +++ b/src/scripts/top-holders.ts @@ -0,0 +1,35 @@ +import '@acala-network/types' +import '@acala-network/types/interfaces/types-lookup' + +import { fetchEntries } from '@open-web3/util' + +import { formatBalance, table } from '../log' +import runner from '../runner' + +runner() + .requiredNetwork(['acala']) + .atBlock() + .withApiPromise() + .run(async ({ apiAt }) => { + const tokens = [] as { account: string; amount: bigint }[] + await fetchEntries( + (startKey) => + apiAt.query.tokens.accounts.entriesPaged({ + args: [], + pageSize: 500, + startKey, + }), + (key, data) => { + const acc = key.args[0].toString() + const token = key.args[1].toString() + if (token === '{"token":"AUSD"}') { + const val = data.free.toBigInt() + tokens.push({ account: acc, amount: val }) + } + } + ) + + tokens.sort((a, b) => Number(b.amount - a.amount)) + + table(tokens.map((x) => ({ account: x.account, amount: formatBalance(x.amount) }))) + }) diff --git a/yarn.lock b/yarn.lock index 1c8a7df..9fd7b42 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1082,6 +1082,21 @@ __metadata: languageName: node linkType: hard +"@fast-csv/parse@npm:^4.3.6": + version: 4.3.6 + resolution: "@fast-csv/parse@npm:4.3.6" + dependencies: + "@types/node": ^14.0.1 + lodash.escaperegexp: ^4.1.2 + lodash.groupby: ^4.6.0 + lodash.isfunction: ^3.0.9 + lodash.isnil: ^4.0.0 + lodash.isundefined: ^3.0.1 + lodash.uniq: ^4.5.0 + checksum: 15434d2bc7a71af63a4cc45352a58f6edd69d464233fd6f9a808a8efb9ae326832b4f78d1ddcf9fc3bbf166e9c1dce19671c4de40371a926d60cb7c5a8bad08a + languageName: node + linkType: hard + "@gar/promisify@npm:^1.1.3": version: 1.1.3 resolution: "@gar/promisify@npm:1.1.3" @@ -5058,6 +5073,13 @@ __metadata: languageName: node linkType: hard +"lodash.groupby@npm:^4.6.0": + version: 4.6.0 + resolution: "lodash.groupby@npm:4.6.0" + checksum: e2d4d13d12790a1cacab3f5f120b7c072a792224e83b2f403218866d18efde76024b2579996dfebb230a61ce06469332e16639103669a35a605287e19ced6b9b + languageName: node + linkType: hard + "lodash.isboolean@npm:^3.0.3": version: 3.0.3 resolution: "lodash.isboolean@npm:3.0.3" @@ -5086,6 +5108,13 @@ __metadata: languageName: node linkType: hard +"lodash.isundefined@npm:^3.0.1": + version: 3.0.1 + resolution: "lodash.isundefined@npm:3.0.1" + checksum: 52b4d99a47bd41daa4e2860200258f56b1f2c99263c11a5f607fbbd91d6447fe674bdafc172735d099908a09136d4a0f98cf79715e38ca4b490fdda7162be289 + languageName: node + linkType: hard + "lodash.merge@npm:^4.6.2": version: 4.6.2 resolution: "lodash.merge@npm:4.6.2" @@ -5093,6 +5122,13 @@ __metadata: languageName: node linkType: hard +"lodash.uniq@npm:^4.5.0": + version: 4.5.0 + resolution: "lodash.uniq@npm:4.5.0" + checksum: a4779b57a8d0f3c441af13d9afe7ecff22dd1b8ce1129849f71d9bbc8e8ee4e46dfb4b7c28f7ad3d67481edd6e51126e4e2a6ee276e25906d10f7140187c392d + languageName: node + linkType: hard + "lodash@npm:^4.17.20, lodash@npm:^4.17.21": version: 4.17.21 resolution: "lodash@npm:4.17.21" @@ -6248,6 +6284,7 @@ __metadata: "@acala-network/sdk-wallet": ^4.1.6-29 "@acala-network/types": ^4.1.6-29 "@fast-csv/format": ^4.3.5 + "@fast-csv/parse": ^4.3.6 "@open-web3/util": ^2.0.1 "@polkadot/api": ^9.1.1 "@types/async": ^3.2.15