1
- const ADDRESSES = require ( ' ../helper/coreAssets.json' )
1
+ const ADDRESSES = require ( " ../helper/coreAssets.json" ) ;
2
2
const sdk = require ( "@defillama/sdk" ) ;
3
3
4
4
const PONDER_URL = "https://artistic-perfection-production.up.railway.app" ;
@@ -39,7 +39,7 @@ async function getPoolId(superPoolAddress) {
39
39
return poolConnection . pool . id ;
40
40
}
41
41
42
- async function getPositionAddresses ( poolId ) {
42
+ async function getPositionAddresses ( ) {
43
43
let positions = [ ] ;
44
44
let afterCursor = null ;
45
45
let hasNextPage = true ;
@@ -50,14 +50,19 @@ async function getPositionAddresses(poolId) {
50
50
headers : { "Content-Type" : "application/json" } ,
51
51
body : JSON . stringify ( {
52
52
query : `
53
- query GetPositions($poolId: BigInt!, $after: String) {
54
- positions(limit: 100, after: $after, where: { poolId: $poolId }) {
55
- items { id }
56
- pageInfo { hasNextPage, endCursor }
53
+ query GetPositions($after: String) {
54
+ positions(limit: 999, after: $after) {
55
+ items {
56
+ id
57
+ }
58
+ pageInfo {
59
+ hasNextPage
60
+ endCursor
61
+ }
57
62
}
58
63
}
59
64
` ,
60
- variables : { poolId , after : afterCursor } ,
65
+ variables : { after : afterCursor } ,
61
66
} ) ,
62
67
} ) ;
63
68
@@ -77,35 +82,38 @@ async function tvl(api) {
77
82
78
83
// Lending TVL
79
84
for ( const { superPool, underlyingAsset } of SUPERPOOLS ) {
85
+ const poolId = await getPoolId ( superPool ) ;
86
+ const totalBorrows = await api . call ( {
87
+ target : POOL_ADDRESS ,
88
+ params : [ poolId ] ,
89
+ abi : "function getTotalBorrows(uint256) view returns (uint256)" ,
90
+ } ) ;
80
91
const totalAssets = await api . call ( {
81
- target : superPool ,
82
- abi : "uint256:totalAssets" ,
92
+ target : POOL_ADDRESS ,
93
+ params : [ poolId ] ,
94
+ abi : "function getTotalAssets(uint256) view returns (uint256)" ,
83
95
} ) ;
84
- sdk . util . sumSingleBalance ( balances , underlyingAsset , totalAssets ) ;
96
+ const availableLiquidity = BigInt ( totalAssets ) - BigInt ( totalBorrows ) ;
97
+ sdk . util . sumSingleBalance ( balances , underlyingAsset , availableLiquidity ) ;
85
98
}
86
99
87
100
// Collateral TVL
88
- for ( const { superPool } of SUPERPOOLS ) {
89
- try {
90
- const poolId = await getPoolId ( superPool ) ;
91
- const positions = await getPositionAddresses ( poolId ) ;
92
-
93
- const assetData = await api . multiCall ( {
94
- abi : "function getAssetData(address) view returns ((address asset, uint256 amount, uint256 valueInEth)[])" ,
95
- calls : positions ,
96
- target : PORTFOLIO_LENS_ADDRESS ,
97
- } ) ;
98
-
99
- assetData . flat ( ) . forEach ( ( { asset, amount } ) => {
100
- sdk . util . sumSingleBalance ( balances , `hyperliquid:${ asset } ` , amount ) ;
101
- } ) ;
102
- } catch ( error ) {
103
- console . error (
104
- "Error fetching collateral for superPool:" ,
105
- superPool ,
106
- error
107
- ) ;
108
- }
101
+ const positions = await getPositionAddresses ( ) ;
102
+
103
+ // Batch positions into chunks of 30
104
+ const BATCH_SIZE = 30 ;
105
+ for ( let i = 0 ; i < positions . length ; i += BATCH_SIZE ) {
106
+ const positionBatch = positions . slice ( i , i + BATCH_SIZE ) ;
107
+
108
+ const assetDataBatch = await api . multiCall ( {
109
+ abi : "function getAssetData(address) view returns ((address asset, uint256 amount, uint256 valueInEth)[])" ,
110
+ calls : positionBatch ,
111
+ target : PORTFOLIO_LENS_ADDRESS ,
112
+ } ) ;
113
+
114
+ assetDataBatch . flat ( ) . forEach ( ( { asset, amount } ) => {
115
+ sdk . util . sumSingleBalance ( balances , `hyperliquid:${ asset } ` , amount ) ;
116
+ } ) ;
109
117
}
110
118
111
119
return balances ;
@@ -115,17 +123,13 @@ async function borrowed(api) {
115
123
const balances = { } ;
116
124
117
125
for ( const { superPool, underlyingAsset } of SUPERPOOLS ) {
118
- try {
119
- const poolId = await getPoolId ( superPool ) ;
120
- const totalBorrows = await api . call ( {
121
- target : POOL_ADDRESS ,
122
- params : [ poolId ] ,
123
- abi : "function getTotalBorrows(uint256) view returns (uint256)" ,
124
- } ) ;
125
- sdk . util . sumSingleBalance ( balances , underlyingAsset , totalBorrows ) ;
126
- } catch ( error ) {
127
- console . error ( "Error fetching borrowed amount:" , superPool , error ) ;
128
- }
126
+ const poolId = await getPoolId ( superPool ) ;
127
+ const totalBorrows = await api . call ( {
128
+ target : POOL_ADDRESS ,
129
+ params : [ poolId ] ,
130
+ abi : "function getTotalBorrows(uint256) view returns (uint256)" ,
131
+ } ) ;
132
+ sdk . util . sumSingleBalance ( balances , underlyingAsset , totalBorrows ) ;
129
133
}
130
134
131
135
return balances ;
0 commit comments