1
- const { sumTokens } = require ( "../helper/chain/fuel" )
1
+ const { sumTokens } = require ( "../helper/chain/fuel" ) ;
2
2
3
3
async function tvl ( api ) {
4
- const contractId = '0xfe2c524ad8e088f33d232a45dbea43e792861640b71aa1814b30506bf8430ee5'
5
- return sumTokens ( { api, owner : contractId , } )
4
+ const contractIds = [
5
+ '0xfe2c524ad8e088f33d232a45dbea43e792861640b71aa1814b30506bf8430ee5' ,
6
+ '0xdafe498b31f24ea5577055e86bf77e96bcba2c39a7ae47abaa819c303a45a352' ,
7
+ '0x81e83f73530c262b0dbf5414649a875c48a48144de3c08ff68cb9d54b36f2eaa' ,
8
+ ] ;
9
+
10
+ try {
11
+ const balancesList = await Promise . all (
12
+ contractIds . map ( async ( contractId ) => {
13
+ try {
14
+ const balances = await sumTokens ( { api, owner : contractId } ) ;
15
+ console . log ( `Balances from contract ${ contractId } :` , balances ) ;
16
+ return balances ;
17
+ } catch ( err ) {
18
+ console . error ( `Error fetching balance for contract ${ contractId } :` , err . message ) ;
19
+ return { } ;
20
+ }
21
+ } )
22
+ ) ;
23
+
24
+ const combinedBalances = balancesList . reduce ( ( acc , balances ) => {
25
+ Object . entries ( balances ) . forEach ( ( [ key , value ] ) => {
26
+ const numericValue = BigInt ( value || 0 ) ;
27
+ acc [ key ] = acc [ key ] ? BigInt ( Math . max ( Number ( acc [ key ] ) , Number ( numericValue ) ) ) : numericValue ;
28
+ } ) ;
29
+ return acc ;
30
+ } , { } ) ;
31
+
32
+ console . log ( "Final combined balances:" , combinedBalances ) ;
33
+
34
+ const totalTvl = Object . values ( combinedBalances ) . reduce ( ( sum , value ) => {
35
+ return sum + Number ( value ) ;
36
+ } , 0 ) ;
37
+
38
+ const formattedBalances = Object . fromEntries (
39
+ Object . entries ( combinedBalances ) . map ( ( [ key , value ] ) => [ key , value . toString ( ) ] )
40
+ ) ;
41
+
42
+ console . log ( "Formatted Balances for TVL:" , formattedBalances ) ;
43
+
44
+ return formattedBalances ;
45
+ } catch ( err ) {
46
+ console . error ( "Unexpected error during TVL calculation:" , err . message ) ;
47
+ return { } ;
48
+ }
6
49
}
7
50
8
51
module . exports = {
9
- fuel : { tvl } ,
10
- timetravel : false ,
11
- }
52
+ fuel : { tvl } ,
53
+ timetravel : false ,
54
+ } ;
0 commit comments