1- const ADDRESSES = require ( '../helper/coreAssets.json' )
2- const sdk = require ( '@defillama/sdk' ) ;
3-
1+ const ADDRESSES = require ( '../helper/coreAssets.json' ) ;
2+ const { sliceIntoChunks } = require ( '../helper/utils' ) ;
43
54const EtherFiCashFactory = '0xF4e147Db314947fC1275a8CbB6Cde48c510cd8CF' ;
65const CashBorrowerHelperContract = '0xF0df37503714f08d0fCA5B434F1FFA2b8b1AF34B' ;
76
7+ const abi = {
8+ getTotalCollateralForSafesWithIndex : 'function getTotalCollateralForSafesWithIndex(uint256 startIndex, uint256 n) view returns (tuple(address token, uint256 amount)[])' ,
9+ }
810
9- //Get all the collateral held by cash borrow mode vaults which can be used to borrow against
10- async function getCollateralInCashBorrowMode ( api , config ) {
11-
12- const collateralTokensToAmount = { } ;
13-
14- const { timestamp } = api ;
15- const scroll_api = new sdk . ChainApi ( { timestamp, chain : 'scroll' } ) ;
16-
11+ async function tvl ( api ) {
1712 //get last collateral mode vault
18- const lastCollateralModeVault = ( await scroll_api . call ( {
13+ const lastCollateralModeVault = ( await api . call ( {
1914 target : EtherFiCashFactory ,
2015 abi : 'function numContractsDeployed() view returns (uint256)' ,
2116 } ) ) - 1 ;
22-
23- const batch_size = 100 ;
24-
17+
18+ const batch_size = 50 ;
19+
20+
2521 const calls = [ ] ;
2622 for ( let i = 0 ; i < Number ( lastCollateralModeVault ) ; i += batch_size ) {
2723 const startIndex = i ;
2824 const endIndex = Math . min ( i + batch_size , lastCollateralModeVault ) ;
2925 const n = endIndex - startIndex ;
30-
26+
3127 calls . push ( {
3228 target : CashBorrowerHelperContract ,
33- abi : 'function getTotalCollateralForSafesWithIndex(uint256 startIndex, uint256 n) view returns (tuple(address token, uint256 amount)[])' ,
3429 params : [ startIndex , n ] ,
3530 } ) ;
3631 }
3732
38- const parallelBatchSize = 30 ;
39- const batches = [ ] ;
40- for ( let i = 0 ; i < calls . length ; i += parallelBatchSize ) {
41- batches . push ( calls . slice ( i , Math . min ( i + parallelBatchSize , calls . length ) ) ) ;
42- }
43-
44- // Execute batches in parallel
45- for ( const [ batchIndex , batch ] of batches . entries ( ) ) {
46- const batchPromises = batch . map ( call => scroll_api . call ( call ) ) ;
47- const batchResults = await Promise . all ( batchPromises ) ;
48-
49- // Process results
50- batchResults . forEach ( ( result , index ) => {
51- if ( Array . isArray ( result ) ) {
52- for ( const [ token , amount ] of result ) {
53- // Validate amount before adding
54- if ( amount && amount !== 'Infinity' && ! isNaN ( Number ( amount ) ) ) {
55- collateralTokensToAmount [ token ] = ( collateralTokensToAmount [ token ] || 0n ) + BigInt ( amount ) ;
56- } else {
57- console . warn ( 'Invalid amount for token:' , token , amount )
58- }
59- }
60- }
61- } ) ;
62- }
63-
64- const result = { } ;
65- for ( const [ token , amount ] of Object . entries ( collateralTokensToAmount ) ) {
66- result [ token ] = amount . toString ( ) ;
33+ const chunks = sliceIntoChunks ( calls , 100 ) ;
34+ let i = 0
35+ for ( const chunk of chunks ) {
36+ const res = await api . multiCall ( { abi : abi . getTotalCollateralForSafesWithIndex , calls : chunk } )
37+ res . forEach ( batchResult => {
38+ batchResult . forEach ( ( { token, amount } ) => api . add ( token , amount ) )
39+ } )
40+ api . log ( `Processed chunk ${ ++ i } /${ chunks . length } ` )
6741 }
68-
69- return result ;
70- }
71-
72- async function tvl ( api ) {
73- const collateralTokensToAmount = await getCollateralInCashBorrowMode ( api ) ;
74- for ( const [ token , amount ] of Object . entries ( collateralTokensToAmount ) ) {
75- api . add ( token , amount ) ;
76- }
7742}
7843
7944async function borrow ( api ) {
8045 const cashDebitCore = '0x0078C5a459132e279056B2371fE8A8eC973A9553'
8146 const usdcScroll = ADDRESSES . scroll . USDC
82- const { timestamp } = api ;
83- const scroll_api = new sdk . ChainApi ( { timestamp, chain : 'scroll' } ) ;
84- const borrowingAmount = await scroll_api . call ( {
47+ const borrowingAmount = await api . call ( {
8548 target : cashDebitCore ,
8649 abi : 'function totalBorrowingAmount(address borrowToken) view returns (uint256)' ,
8750 params : [ usdcScroll ] ,
@@ -90,9 +53,9 @@ async function borrow(api) {
9053}
9154
9255module . exports = {
93- misrepresentedTokens : true ,
56+ isHeavyProtocol : true ,
9457 scroll : {
95- tvl,
58+ tvl,
9659 borrowed : borrow ,
9760 } ,
9861} ;
0 commit comments