Skip to content

fix: collateral tvl not being accounted #14458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 46 additions & 42 deletions projects/sentiment/v2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const ADDRESSES = require('../helper/coreAssets.json')
const ADDRESSES = require("../helper/coreAssets.json");
const sdk = require("@defillama/sdk");

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

async function getPositionAddresses(poolId) {
async function getPositionAddresses() {
let positions = [];
let afterCursor = null;
let hasNextPage = true;
Expand All @@ -50,14 +50,19 @@ async function getPositionAddresses(poolId) {
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query: `
query GetPositions($poolId: BigInt!, $after: String) {
positions(limit: 100, after: $after, where: { poolId: $poolId }) {
items { id }
pageInfo { hasNextPage, endCursor }
query GetPositions($after: String) {
positions(limit: 999, after: $after) {
items {
id
}
pageInfo {
hasNextPage
endCursor
}
}
}
`,
variables: { poolId, after: afterCursor },
variables: { after: afterCursor },
}),
});

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

// Lending TVL
for (const { superPool, underlyingAsset } of SUPERPOOLS) {
const poolId = await getPoolId(superPool);
const totalBorrows = await api.call({
target: POOL_ADDRESS,
params: [poolId],
abi: "function getTotalBorrows(uint256) view returns (uint256)",
});
const totalAssets = await api.call({
target: superPool,
abi: "uint256:totalAssets",
target: POOL_ADDRESS,
params: [poolId],
abi: "function getTotalAssets(uint256) view returns (uint256)",
});
sdk.util.sumSingleBalance(balances, underlyingAsset, totalAssets);
const availableLiquidity = BigInt(totalAssets) - BigInt(totalBorrows);
sdk.util.sumSingleBalance(balances, underlyingAsset, availableLiquidity);
}

// Collateral TVL
for (const { superPool } of SUPERPOOLS) {
try {
const poolId = await getPoolId(superPool);
const positions = await getPositionAddresses(poolId);

const assetData = await api.multiCall({
abi: "function getAssetData(address) view returns ((address asset, uint256 amount, uint256 valueInEth)[])",
calls: positions,
target: PORTFOLIO_LENS_ADDRESS,
});

assetData.flat().forEach(({ asset, amount }) => {
sdk.util.sumSingleBalance(balances, `hyperliquid:${asset}`, amount);
});
} catch (error) {
console.error(
"Error fetching collateral for superPool:",
superPool,
error
);
}
const positions = await getPositionAddresses();

// Batch positions into chunks of 30
const BATCH_SIZE = 30;
for (let i = 0; i < positions.length; i += BATCH_SIZE) {
const positionBatch = positions.slice(i, i + BATCH_SIZE);

const assetDataBatch = await api.multiCall({
abi: "function getAssetData(address) view returns ((address asset, uint256 amount, uint256 valueInEth)[])",
calls: positionBatch,
target: PORTFOLIO_LENS_ADDRESS,
});

assetDataBatch.flat().forEach(({ asset, amount }) => {
sdk.util.sumSingleBalance(balances, `hyperliquid:${asset}`, amount);
});
}

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

for (const { superPool, underlyingAsset } of SUPERPOOLS) {
try {
const poolId = await getPoolId(superPool);
const totalBorrows = await api.call({
target: POOL_ADDRESS,
params: [poolId],
abi: "function getTotalBorrows(uint256) view returns (uint256)",
});
sdk.util.sumSingleBalance(balances, underlyingAsset, totalBorrows);
} catch (error) {
console.error("Error fetching borrowed amount:", superPool, error);
}
const poolId = await getPoolId(superPool);
const totalBorrows = await api.call({
target: POOL_ADDRESS,
params: [poolId],
abi: "function getTotalBorrows(uint256) view returns (uint256)",
});
sdk.util.sumSingleBalance(balances, underlyingAsset, totalBorrows);
}

return balances;
Expand Down
Loading