Skip to content

Commit d25856d

Browse files
authored
fix: collateral tvl not being accounted (#14458)
Signed-off-by: 0xhitgo <[email protected]>
1 parent 801d722 commit d25856d

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

projects/sentiment/v2.js

+46-42
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const ADDRESSES = require('../helper/coreAssets.json')
1+
const ADDRESSES = require("../helper/coreAssets.json");
22
const sdk = require("@defillama/sdk");
33

44
const PONDER_URL = "https://artistic-perfection-production.up.railway.app";
@@ -39,7 +39,7 @@ async function getPoolId(superPoolAddress) {
3939
return poolConnection.pool.id;
4040
}
4141

42-
async function getPositionAddresses(poolId) {
42+
async function getPositionAddresses() {
4343
let positions = [];
4444
let afterCursor = null;
4545
let hasNextPage = true;
@@ -50,14 +50,19 @@ async function getPositionAddresses(poolId) {
5050
headers: { "Content-Type": "application/json" },
5151
body: JSON.stringify({
5252
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+
}
5762
}
5863
}
5964
`,
60-
variables: { poolId, after: afterCursor },
65+
variables: { after: afterCursor },
6166
}),
6267
});
6368

@@ -77,35 +82,38 @@ async function tvl(api) {
7782

7883
// Lending TVL
7984
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+
});
8091
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)",
8395
});
84-
sdk.util.sumSingleBalance(balances, underlyingAsset, totalAssets);
96+
const availableLiquidity = BigInt(totalAssets) - BigInt(totalBorrows);
97+
sdk.util.sumSingleBalance(balances, underlyingAsset, availableLiquidity);
8598
}
8699

87100
// 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+
});
109117
}
110118

111119
return balances;
@@ -115,17 +123,13 @@ async function borrowed(api) {
115123
const balances = {};
116124

117125
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);
129133
}
130134

131135
return balances;

0 commit comments

Comments
 (0)