11import * as dotenv from 'dotenv' ;
22import * as getopts from 'getopts' ;
33import { table } from 'table' ;
4+ import { StacksCoreRpcClient } from '../../src/core-rpc/client' ;
45import { PgDataStore } from '../../src/datastore/postgres-store' ;
56
67type AddressBalanceResult = {
@@ -27,8 +28,8 @@ type TableCellValue = string | number | bigint | undefined;
2728async 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