-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathstate-check-balance.ts
45 lines (36 loc) · 1.25 KB
/
state-check-balance.ts
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
import { DFUSE_API_KEY, runMain, DFUSE_API_NETWORK } from "../../config"
import { createDfuseClient, DfuseClient } from "@dfuse/client"
const account = "eoscanadacom"
const blockNum = 42_500_250
async function main(): Promise<void> {
const client = createDfuseClient({
apiKey: DFUSE_API_KEY,
network: DFUSE_API_NETWORK,
})
try {
const { balance: atBalance, blockNum: atBlockNum } = await fetchBalance(client, blockNum)
const { balance: currentBalance, blockNum: currentBlockNum } = await fetchBalance(client)
console.log(`Your balance at block ${atBlockNum} was ${atBalance}`)
console.log(`Your current balance at block ${currentBlockNum} is ${currentBalance}`)
} catch (error) {
console.log("An error occurred", error)
}
client.release()
}
async function fetchBalance(
client: DfuseClient,
atBlock?: number
): Promise<{ balance?: string; blockNum: number }> {
const options = { blockNum: atBlock === undefined ? undefined : atBlock }
const response = await client.stateTable<AccountTableRow>(
"eosio.token",
account,
"accounts",
options
)
return { balance: response.rows[0].json?.balance, blockNum: response.up_to_block_num || blockNum }
}
type AccountTableRow = {
balance: string
}
runMain(main)