Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Aug 19, 2022
1 parent 65826ad commit 41e0b94
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 13 additions & 5 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 '-'
Expand All @@ -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) => {
Expand Down
35 changes: 35 additions & 0 deletions src/scripts/top-holders.ts
Original file line number Diff line number Diff line change
@@ -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) })))
})
37 changes: 37 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -5086,13 +5108,27 @@ __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"
checksum: ad580b4bdbb7ca1f7abf7e1bce63a9a0b98e370cf40194b03380a46b4ed799c9573029599caebc1b14e3f24b111aef72b96674a56cfa105e0f5ac70546cdc005
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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 41e0b94

Please sign in to comment.