Skip to content

Commit d880952

Browse files
committed
feat: query nodes for balances via RPC
1 parent ac89cf8 commit d880952

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

utils/src/index.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as dotenv from 'dotenv';
22
import * as getopts from 'getopts';
33
import { table } from 'table';
4+
import { StacksCoreRpcClient } from '../../src/core-rpc/client';
45
import { PgDataStore } from '../../src/datastore/postgres-store';
56

67
type AddressBalanceResult = {
@@ -27,8 +28,8 @@ type TableCellValue = string | number | bigint | undefined;
2728
async function printTopAccountBalances(count: number, blockHeight: number) {
2829
const db = await PgDataStore.connect(true);
2930

31+
console.log(`Calculating balances for top ${count} accounts at block height ${blockHeight}...`);
3032
const blockInfo = await db.query(async client => {
31-
console.log(blockHeight);
3233
const result = await client.query<BlockInfo>(
3334
`
3435
SELECT block_height, block_hash, index_block_hash
@@ -80,12 +81,18 @@ async function printTopAccountBalances(count: number, blockHeight: number) {
8081
return result.rows;
8182
});
8283
// Next, fill them up with balances from DB and node.
83-
await Promise.all(
84-
addressBalances.map(async item => {
85-
const balance = await db.getStxBalanceAtBlock(item.address, blockInfo.block_height);
86-
item.apiBalance = balance.balance;
87-
})
88-
);
84+
const dbBalances = addressBalances.map(async item => {
85+
const balance = await db.getStxBalanceAtBlock(item.address, blockInfo.block_height);
86+
item.apiBalance = balance.balance;
87+
});
88+
const nodeBalances = addressBalances.map(async item => {
89+
const balance = await new StacksCoreRpcClient().getAccountBalance(
90+
item.address,
91+
blockInfo.index_block_hash.toString('hex')
92+
);
93+
item.nodeBalance = balance.valueOf();
94+
});
95+
await Promise.all(dbBalances.concat(nodeBalances));
8996

9097
const tabularData: TableCellValue[][] = [
9198
['event count', 'address', 'api balance', 'node balance'],

0 commit comments

Comments
 (0)