Skip to content
Open
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
141 changes: 141 additions & 0 deletions src/adaptors/velkonix/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
const sdk = require('@defillama/sdk');

const utils = require('../utils');
const poolAbi = require('../aave-v3/poolAbi');

const PROTOCOL_DATA_PROVIDER = '0x6da56B769B42952CACA18D37Feda3015FDB2fE67';
const CHAIN = 'megaeth';
const PROJECT = 'velkonix';
const APP_URL = 'https://velkonix.com';

const apy = async () => {
const reserveTokens = (
await sdk.api.abi.call({
target: PROTOCOL_DATA_PROVIDER,
abi: poolAbi.find((m) => m.name === 'getAllReservesTokens'),
chain: CHAIN,
})
).output;

const aTokens = (
await sdk.api.abi.call({
target: PROTOCOL_DATA_PROVIDER,
abi: poolAbi.find((m) => m.name === 'getAllATokens'),
chain: CHAIN,
})
).output;

const poolsReserveData = (
await sdk.api.abi.multiCall({
calls: reserveTokens.map((p) => ({
target: PROTOCOL_DATA_PROVIDER,
params: p.tokenAddress,
})),
abi: poolAbi.find((m) => m.name === 'getReserveData'),
chain: CHAIN,
})
).output.map((o) => o.output);

const poolsReservesConfigurationData = (
await sdk.api.abi.multiCall({
calls: reserveTokens.map((p) => ({
target: PROTOCOL_DATA_PROVIDER,
params: p.tokenAddress,
})),
abi: poolAbi.find((m) => m.name === 'getReserveConfigurationData'),
chain: CHAIN,
})
).output.map((o) => o.output);

const poolsReserveCaps = (
await sdk.api.abi.multiCall({
calls: reserveTokens.map((p) => ({
target: PROTOCOL_DATA_PROVIDER,
params: p.tokenAddress,
})),
abi: poolAbi.find((m) => m.name === 'getReserveCaps'),
chain: CHAIN,
})
).output.map((o) => o.output);

const underlyingBalances = (
await sdk.api.abi.multiCall({
chain: CHAIN,
abi: 'erc20:balanceOf',
calls: aTokens.map((t, i) => ({
target: reserveTokens[i].tokenAddress,
params: [t.tokenAddress],
})),
})
).output.map((o) => o.output);

const underlyingDecimals = (
await sdk.api.abi.multiCall({
chain: CHAIN,
abi: 'erc20:decimals',
calls: aTokens.map((t) => ({
target: t.tokenAddress,
})),
})
).output.map((o) => o.output);

const priceKeys = reserveTokens
.map((t) => `${CHAIN}:${t.tokenAddress}`)
.join(',');
const prices = (
await utils.getPriceApiData(`/prices/current/${priceKeys}`)
).coins;

const pools = reserveTokens
.map((pool, i) => {
const cfg = poolsReservesConfigurationData[i];
if (cfg.isFrozen) return null;

const price = prices[`${CHAIN}:${pool.tokenAddress}`]?.price;
if (!price) return null;

const p = poolsReserveData[i];
const decimals = Number(underlyingDecimals[i]);
const toTokenAmount = (amount) => Number(amount) / 10 ** decimals;

const tvlUsd = toTokenAmount(underlyingBalances[i]) * price;
const totalBorrow =
BigInt(p.totalStableDebt) + BigInt(p.totalVariableDebt);
const totalBorrowUsd = toTokenAmount(totalBorrow) * price;
const totalSupplyUsd = tvlUsd + totalBorrowUsd;

const borrowCap = Number(poolsReserveCaps[i].borrowCap);
const borrowCapUsd = borrowCap * price;
const availableBorrowUsd = borrowCap
? Math.max(Math.min(tvlUsd, borrowCapUsd - totalBorrowUsd), 0)
: tvlUsd;
Comment on lines +101 to +111

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Report supplied liquidity as tvlUsd, not only idle liquidity.

Line 101 currently makes tvlUsd equal the underlying cash sitting in the aToken contract. Once users borrow, that underreports pool TVL by the borrowed amount. Keep available liquidity separate for availableBorrowUsd, and set tvlUsd from total supplied liquidity.

Proposed change
-      const tvlUsd = toTokenAmount(underlyingBalances[i]) * price;
+      const availableLiquidityUsd =
+        toTokenAmount(underlyingBalances[i]) * price;
       const totalBorrow =
         BigInt(p.totalStableDebt) + BigInt(p.totalVariableDebt);
       const totalBorrowUsd = toTokenAmount(totalBorrow) * price;
-      const totalSupplyUsd = tvlUsd + totalBorrowUsd;
+      const totalSupplyUsd = availableLiquidityUsd + totalBorrowUsd;
+      const tvlUsd = totalSupplyUsd;
 
       const borrowCap = Number(poolsReserveCaps[i].borrowCap);
       const borrowCapUsd = borrowCap * price;
       const availableBorrowUsd = borrowCap
-        ? Math.max(Math.min(tvlUsd, borrowCapUsd - totalBorrowUsd), 0)
-        : tvlUsd;
+        ? Math.max(
+            Math.min(availableLiquidityUsd, borrowCapUsd - totalBorrowUsd),
+            0
+          )
+        : availableLiquidityUsd;

Also applies to: 118-123

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/adaptors/velkonix/index.js` around lines 101 - 111, The liquidity
calculation in the Velkonix adaptor is using idle cash for tvlUsd, which
underreports TVL once borrowing exists. In the pricing loop in index.js, update
the tvlUsd assignment to use total supplied liquidity (the existing
totalSupplyUsd derived from underlying balances plus total borrow) and keep the
idle/capped liquidity logic only for availableBorrowUsd. Make the same
adjustment anywhere else the same pattern appears, including the later block
referenced in the review, so tvlUsd consistently represents supplied liquidity
rather than available cash.


return {
pool: `${aTokens[i].tokenAddress}-${CHAIN}`.toLowerCase(),
chain: utils.formatChain(CHAIN),
project: PROJECT,
symbol: pool.symbol,
tvlUsd,
apyBase: (Number(p.liquidityRate) / 10 ** 27) * 100,
underlyingTokens: [pool.tokenAddress],
totalSupplyUsd,
totalBorrowUsd,
availableBorrowUsd,
apyBaseBorrow: Number(p.variableBorrowRate) / 1e25,
borrowToken: pool.tokenAddress,
ltv: Number(cfg.ltv) / 10000,
borrowable: cfg.borrowingEnabled,
token: aTokens[i].tokenAddress,
};
})
.filter(Boolean);

return pools.filter(utils.keepFinite);
};

module.exports = {
timetravel: false,
apy,
url: APP_URL,
protocolId: '8056',
};
Loading