1- import { chunk } from "lodash" ;
2- import MULTICALL_ABI from "./abi/Multicall.json" ;
31import TOKENS from "./default-tokens.json" ;
4- import {
5- SequencerProvider ,
6- Contract as SNContract ,
7- Abi ,
8- stark ,
9- hash ,
10- number ,
11- uint256 ,
12- } from "starknet" ;
2+ import { encode , SequencerProvider , uint256 } from "starknet" ;
133import { formatTokenBalance } from "./formatTokenBalance" ;
14-
15- const CHUNK_SIZE = 10 ;
16- const MULTICALL_ADDRESS = {
17- "mainnet-alpha" :
18- "0x0740a7a14618bb7e4688d10059bc42104d22c315bb647130630c77d3b6d3ee50" ,
19- "goerli-alpha" :
20- "0x042a12c5a641619a6c58e623d5735273cdfb0e13df72c4bacb4e188892034bd6" ,
21- } ;
4+ import { Multicall } from "@argent/x-multicall" ;
225
236export async function getBalances (
247 addresses : string [ ] ,
@@ -27,46 +10,21 @@ export async function getBalances(
2710 const tokens = TOKENS . filter ( ( token ) => token . network === network ) ;
2811 const tokenAddresses = tokens . map ( ( token ) => token . address ) ;
2912 const provider = new SequencerProvider ( { network } ) ;
30- const multicallContract = new SNContract (
31- MULTICALL_ABI as Abi ,
32- MULTICALL_ADDRESS [ network ] ,
33- provider
34- ) ;
13+ const multicallProvider = new Multicall ( provider ) ;
3514
3615 const addressesTokensCombinations = tokenAddresses . flatMap ( ( token ) =>
3716 addresses . map ( ( address ) => ( { address, token } ) )
3817 ) ;
3918
40- const calls = addressesTokensCombinations . flatMap ( ( { address, token } ) => {
41- const compiledCalldata = stark . compileCalldata ( {
42- address,
43- } ) ;
44- return [
45- token ,
46- hash . getSelectorFromName ( "balanceOf" ) ,
47- compiledCalldata . length ,
48- ...compiledCalldata ,
49- ] ;
50- } ) ;
51-
52- const chunks = chunk ( calls , CHUNK_SIZE * 4 ) ;
53- const results : string [ ] = [ ] ;
54- for ( const lChunk of chunks ) {
55- const response = await multicallContract . aggregate ( lChunk ) ;
56- const lResults : string [ ] = response . result . map ( ( res : any ) =>
57- number . toHex ( res )
58- ) ;
59- const resultChunks = chunk ( lResults , 2 ) ;
60- const balances = resultChunks . reduce ( ( acc , result ) => {
61- const balance = uint256
62- . uint256ToBN ( { low : result [ 0 ] , high : result [ 1 ] } )
63- . toString ( ) ;
64-
65- acc . push ( balance ) ;
66- return acc ;
67- } , [ ] as string [ ] ) ;
68- results . push ( ...balances ) ;
69- }
19+ const results = await Promise . all (
20+ addressesTokensCombinations . map ( ( { address, token } ) =>
21+ multicallProvider . call ( {
22+ contractAddress : token ,
23+ entrypoint : "balanceOf" ,
24+ calldata : [ address ] ,
25+ } )
26+ )
27+ ) ;
7028
7129 if ( addressesTokensCombinations . length !== results . length ) {
7230 throw new Error ( "Something went wrong" ) ;
@@ -76,7 +34,12 @@ export async function getBalances(
7634 address : addressToken . address ,
7735 token : addressToken . token ,
7836 balance : formatTokenBalance (
79- results [ index ] ,
37+ uint256
38+ . uint256ToBN ( {
39+ low : encode . addHexPrefix ( results [ index ] [ 0 ] ) ,
40+ high : encode . addHexPrefix ( results [ index ] [ 1 ] ) ,
41+ } )
42+ . toString ( ) ,
8043 tokens . find ( ( x ) => x . address === addressToken . token ) ! . decimals
8144 ) ,
8245 } ) ) ;
0 commit comments