-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbalances.ts
More file actions
76 lines (69 loc) · 2.26 KB
/
Copy pathbalances.ts
File metadata and controls
76 lines (69 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { Command } from '@oclif/command'
import { ethers } from 'ethers'
import * as utils from '../lib/utils'
import { getWallet } from '../lib/wallet'
import { getTable } from 'console.table'
import { cancelled } from '../lib/prompt'
import { toDecimalString } from '@airswap/utils'
import { BatchCall } from '@airswap/libraries'
export default class Balances extends Command {
public static description = 'display token balances'
public async run() {
try {
const wallet = await getWallet(this)
const chainId = (await wallet.provider.getNetwork()).chainId
const metadata = await utils.getMetadata(this, chainId)
utils.displayDescription(this, Balances.description, chainId)
const startTime = Date.now()
const addresses = Object.keys(metadata.byAddress)
for (let i = addresses.length; i >= 0; i--) {
if (!ethers.utils.isAddress(addresses[i])) {
addresses.splice(i, 1)
}
}
this.log(`Checking balances for ${addresses.length} tokens...\n`)
const batchCallContract = BatchCall.getContract(wallet, chainId)
const chunk = 750
const count = addresses.length
let balances = []
let index = 0
while (index < count) {
balances = balances.concat(
await batchCallContract.walletBalances(
wallet.address,
addresses.slice(index, index + chunk)
)
)
index += chunk
}
const result = []
for (let i = 0; i < addresses.length; i++) {
const token = metadata.byAddress[addresses[i]]
if (!balances[i].eq(0)) {
const balanceDecimal = toDecimalString(
balances[i],
metadata.byAddress[token.address].decimals
)
result.push({
Token: token.symbol,
Balance: balanceDecimal,
})
}
}
if (result.length) {
this.log(getTable(result))
this.log(
`Balances displayed for ${result.length} of ${
addresses.length
} known tokens. (${Date.now() - startTime}ms)\n`
)
} else {
this.log(
`The current account holds no balances in any of ${addresses.length} known tokens.\n`
)
}
} catch (e) {
cancelled(e)
}
}
}