From 28f3efe4bd3a2ded5d69e8ef07ce38b610cf12df Mon Sep 17 00:00:00 2001 From: skords Date: Sun, 1 Dec 2024 18:58:30 +0530 Subject: [PATCH 1/7] revised apy calcs --- src/adaptors/avantis/abiVaultManager.js | 16 --- src/adaptors/avantis/helpers.js | 14 +++ src/adaptors/avantis/index.js | 155 +++++++++++++++++------- src/adaptors/beforeTests.js | 2 +- 4 files changed, 127 insertions(+), 60 deletions(-) delete mode 100644 src/adaptors/avantis/abiVaultManager.js create mode 100644 src/adaptors/avantis/helpers.js diff --git a/src/adaptors/avantis/abiVaultManager.js b/src/adaptors/avantis/abiVaultManager.js deleted file mode 100644 index 6a3135d19d..0000000000 --- a/src/adaptors/avantis/abiVaultManager.js +++ /dev/null @@ -1,16 +0,0 @@ -module.exports = [ - { - "type": "function", - "name": "getReserveRatio", - "inputs": [{ "name": "_reserveAmount", "type": "uint256", "internalType": "uint256" }], - "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], - "stateMutability": "view" - }, - { - "type": "function", - "name": "getProfitMultiplier", - "inputs": [], - "outputs": [{ "name": "", "type": "uint256", "internalType": "uint256" }], - "stateMutability": "view" - }, -] \ No newline at end of file diff --git a/src/adaptors/avantis/helpers.js b/src/adaptors/avantis/helpers.js new file mode 100644 index 0000000000..3f658f3a64 --- /dev/null +++ b/src/adaptors/avantis/helpers.js @@ -0,0 +1,14 @@ +module.exports = { + chunkArray: (array, size) => { + const result = []; + for (let i = 0; i < array.length; i += size) { + result.push(array.slice(i, i + size)); + } + return result; + }, + calculateTrancheTotal: (transfers, trancheAddress) => { + return transfers + .filter((t) => t.to.toLowerCase() === trancheAddress.toLowerCase()) + .reduce((acc, t) => acc + parseFloat((t.value / 1e6).toFixed(6)), 0); + }, +}; diff --git a/src/adaptors/avantis/index.js b/src/adaptors/avantis/index.js index 4425e16ed2..83dae29736 100644 --- a/src/adaptors/avantis/index.js +++ b/src/adaptors/avantis/index.js @@ -1,74 +1,143 @@ const utils = require('../utils'); const sdk = require('@defillama/sdk'); -const abiVaultManager = require('./abiVaultManager'); +const { request, gql } = require('graphql-request'); +const { chunkArray, calculateTrancheTotal } = require('./helpers'); +const { getBlocksByTime } = require('../utils'); const ADDRESSES = { base: { AvantisJuniorTranche: '0x944766f715b51967E56aFdE5f0Aa76cEaCc9E7f9', AvantisSeniorTranche: '0x83084cB182162473d6FEFfCd3Aa48BA55a7B66F7', - AvantisVaultManager: '0xe9fB8C70aF1b99F2Baaa07Aa926FCf3d237348DD', USDC: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', }, }; -const API_BASE = 'https://api.avantisfi.com/v1/history/vaults/apr/'; +const SUBGRAPH_URL = + 'https://subgraph.satsuma-prod.com/052b6e8d4af9/avantis/avantis-mainnet/version/v0.1.9/api'; -const main = async () => { - const [jrData, srData] = await Promise.all([ - utils.getData(`${API_BASE}${ADDRESSES.base.AvantisJuniorTranche}/7`), - utils.getData(`${API_BASE}${ADDRESSES.base.AvantisSeniorTranche}/7`), - ]); +const feeFetchCount = 10000; - if (jrData.success === false || srData.success === false) { - throw new Error('API response is not successful'); +const feeDistributedQuery = gql` + query FeesDistributeds($first: Int!, $skip: Int!, $start: Int!, $end: Int!) { + feesDistributeds( + first: $first + skip: $skip + where: { timestamp_gte: $start, timestamp_lte: $end } + orderBy: timestamp + orderDirection: asc + ) { + id + } } +`; - const { averageApr: jrAverageApr, averageFee: jrAverageFee } = jrData; - const { averageApr: srAverageApr, averageFee: srAverageFee } = srData; +const vmToTrancheQuery = gql` + query VmToTrancheTransfers($transactionHashes: [String!]!, $to: [String!]!) { + vmtoTrancheTransfers( + where: { transactionHash_in: $transactionHashes, to_in: $to } + ) { + value + to + } + } +`; - let [reserveRatio, profitMultiplier, jrTvl, srTvl] = await Promise.all([ - sdk.api.abi.call({ - target: ADDRESSES.base.AvantisVaultManager, - abi: abiVaultManager.find((m) => m.name === 'getReserveRatio'), - params: [0], - chain: 'base', - }), - sdk.api.abi.call({ - target: ADDRESSES.base.AvantisVaultManager, - abi: abiVaultManager.find((m) => m.name === 'getProfitMultiplier'), - chain: 'base', - }), +const fetchFeeDistributedIds = async (timestamp, skip = 0) => { + const { feesDistributeds } = await request( + SUBGRAPH_URL, + feeDistributedQuery, + { + first: feeFetchCount, + skip: skip, + start: timestamp - 7 * 86400, + end: timestamp, + } + ); + + let ids = feesDistributeds.map((f) => f.id); + + if (ids.length === feeFetchCount) { + ids = ids.concat( + await fetchFeeDistributedIds(timestamp, skip + feeFetchCount) + ); + } + + return ids; +}; + +const fetchTransfersForFeeDistributedIds = async (ids) => { + const chunkedIds = chunkArray(ids, 1000); + + let totalJunior = 0; + let totalSenior = 0; + + for (const chunk of chunkedIds) { + const { vmtoTrancheTransfers } = await request( + SUBGRAPH_URL, + vmToTrancheQuery, + { + transactionHashes: chunk, + to: [ + ADDRESSES.base.AvantisJuniorTranche, + ADDRESSES.base.AvantisSeniorTranche, + ], + } + ); + + totalJunior += calculateTrancheTotal( + vmtoTrancheTransfers, + ADDRESSES.base.AvantisJuniorTranche + ); + totalSenior += calculateTrancheTotal( + vmtoTrancheTransfers, + ADDRESSES.base.AvantisSeniorTranche + ); + } + + return { totalJunior: totalJunior / 7, totalSenior: totalSenior / 7 }; +}; + +const main = async (timestamp = null) => { + timestamp = timestamp ? timestamp : Math.floor(Date.now() / 1000); + + // Get total fees distributed for junior and senior tranches + const feesDistributedIds = await fetchFeeDistributedIds(timestamp); + const { totalJunior, totalSenior } = await fetchTransfersForFeeDistributedIds( + feesDistributedIds + ); + + console.log(totalJunior, totalSenior); + + const [block] = await getBlocksByTime([timestamp], 'base'); + + // Get TVL for junior and senior tranches + let [juniorTvl, seniorTvl] = await Promise.all([ sdk.api.abi.call({ abi: 'erc20:balanceOf', target: ADDRESSES.base.USDC, params: [ADDRESSES.base.AvantisJuniorTranche], chain: 'base', + block: block.height, }), sdk.api.abi.call({ abi: 'erc20:balanceOf', target: ADDRESSES.base.USDC, params: [ADDRESSES.base.AvantisSeniorTranche], chain: 'base', + block: block.height, }), ]); - jrTvl = jrTvl.output / 1e6; - srTvl = srTvl.output / 1e6; - - const jrFeeSplit = - (parseFloat(profitMultiplier.output) * parseFloat(reserveRatio.output)) / - 100; + juniorTvl = juniorTvl.output / 1e6; + seniorTvl = seniorTvl.output / 1e6; - let adjApyJr = 0, - adjApySr = 0; + // Calculate daily returns for junior and senior tranches + const juniorDailyReturns = totalJunior / juniorTvl; + const seniorDailyReturns = totalSenior / seniorTvl; - if (jrAverageApr > 0 && jrTvl > 0) { - adjApyJr = ((1 + jrAverageFee / jrTvl) ** 365 - 1) * jrFeeSplit; - } - - if (srAverageApr > 0 && srTvl > 0) { - adjApySr = ((1 + srAverageFee / srTvl) ** 365 - 1) * (100 - jrFeeSplit); - } + // Calculate APY for junior and senior tranches + const juniorApy = (1 + juniorDailyReturns) ** 365 - 1; + const seniorApy = (1 + seniorDailyReturns) ** 365 - 1; return [ { @@ -77,8 +146,8 @@ const main = async () => { project: 'avantis', symbol: 'USDC', poolMeta: 'junior', - tvlUsd: jrTvl, - apyBase: adjApyJr, + tvlUsd: juniorTvl, + apyBase: juniorApy * 100, url: 'https://www.avantisfi.com/earn/junior', }, { @@ -87,8 +156,8 @@ const main = async () => { project: 'avantis', symbol: 'USDC', poolMeta: 'senior', - tvlUsd: srTvl, - apyBase: adjApySr, + tvlUsd: seniorTvl, + apyBase: seniorApy * 100, url: 'https://www.avantisfi.com/earn/senior', }, ]; diff --git a/src/adaptors/beforeTests.js b/src/adaptors/beforeTests.js index 8a5904f893..5ab64eb7cd 100644 --- a/src/adaptors/beforeTests.js +++ b/src/adaptors/beforeTests.js @@ -15,7 +15,7 @@ module.exports = async function () { } const cwd = process.cwd(); - const passedFile = cwd.includes('src/adaptors') + const passedFile = cwd.includes('src\\adaptors') ? path.resolve(cwd, adapter) : path.resolve(cwd, `./src/adaptors/${adapter}`); const module = require(passedFile); From 7591a4e7b3a40d60399ab2ba6df4cac1808f5ada Mon Sep 17 00:00:00 2001 From: skords Date: Sun, 1 Dec 2024 18:58:53 +0530 Subject: [PATCH 2/7] Update index.js --- src/adaptors/avantis/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adaptors/avantis/index.js b/src/adaptors/avantis/index.js index 83dae29736..fd83252127 100644 --- a/src/adaptors/avantis/index.js +++ b/src/adaptors/avantis/index.js @@ -164,6 +164,6 @@ const main = async (timestamp = null) => { }; module.exports = { - timetravel: false, + timetravel: true, apy: main, }; From 7013078718e4c525fe5f539e3ad73c8bd7c6509d Mon Sep 17 00:00:00 2001 From: skords Date: Sun, 1 Dec 2024 19:00:17 +0530 Subject: [PATCH 3/7] Update beforeTests.js --- src/adaptors/beforeTests.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adaptors/beforeTests.js b/src/adaptors/beforeTests.js index 5ab64eb7cd..8a5904f893 100644 --- a/src/adaptors/beforeTests.js +++ b/src/adaptors/beforeTests.js @@ -15,7 +15,7 @@ module.exports = async function () { } const cwd = process.cwd(); - const passedFile = cwd.includes('src\\adaptors') + const passedFile = cwd.includes('src/adaptors') ? path.resolve(cwd, adapter) : path.resolve(cwd, `./src/adaptors/${adapter}`); const module = require(passedFile); From 51c06f859789286145a7a9ea9570eac956d5c36b Mon Sep 17 00:00:00 2001 From: skords Date: Sun, 1 Dec 2024 19:33:56 +0530 Subject: [PATCH 4/7] Update index.js --- src/adaptors/avantis/index.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/adaptors/avantis/index.js b/src/adaptors/avantis/index.js index fd83252127..51ec444ed3 100644 --- a/src/adaptors/avantis/index.js +++ b/src/adaptors/avantis/index.js @@ -98,7 +98,7 @@ const fetchTransfersForFeeDistributedIds = async (ids) => { }; const main = async (timestamp = null) => { - timestamp = timestamp ? timestamp : Math.floor(Date.now() / 1000); + timestamp = timestamp ? parseInt(timestamp) : Math.floor(Date.now() / 1000); // Get total fees distributed for junior and senior tranches const feesDistributedIds = await fetchFeeDistributedIds(timestamp); @@ -106,8 +106,6 @@ const main = async (timestamp = null) => { feesDistributedIds ); - console.log(totalJunior, totalSenior); - const [block] = await getBlocksByTime([timestamp], 'base'); // Get TVL for junior and senior tranches @@ -117,14 +115,14 @@ const main = async (timestamp = null) => { target: ADDRESSES.base.USDC, params: [ADDRESSES.base.AvantisJuniorTranche], chain: 'base', - block: block.height, + block: block, }), sdk.api.abi.call({ abi: 'erc20:balanceOf', target: ADDRESSES.base.USDC, params: [ADDRESSES.base.AvantisSeniorTranche], chain: 'base', - block: block.height, + block: block, }), ]); From 97dfbea9c0c572363c7950945beab80f77ee058c Mon Sep 17 00:00:00 2001 From: skords Date: Tue, 3 Dec 2024 15:42:52 +0530 Subject: [PATCH 5/7] Update index.js --- src/adaptors/avantis/index.js | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/adaptors/avantis/index.js b/src/adaptors/avantis/index.js index 51ec444ed3..2301ce1345 100644 --- a/src/adaptors/avantis/index.js +++ b/src/adaptors/avantis/index.js @@ -2,7 +2,7 @@ const utils = require('../utils'); const sdk = require('@defillama/sdk'); const { request, gql } = require('graphql-request'); const { chunkArray, calculateTrancheTotal } = require('./helpers'); -const { getBlocksByTime } = require('../utils'); +const { getBlocksByTime, getData } = require('../utils'); const ADDRESSES = { base: { @@ -97,16 +97,25 @@ const fetchTransfersForFeeDistributedIds = async (ids) => { return { totalJunior: totalJunior / 7, totalSenior: totalSenior / 7 }; }; +// NOTE: OUR SUBGRAPH IS NOT CAUGHT UP TO DATE, SO WE ARE USING THE API FOR NOW +// ----------------------------------------------------------------------------- +// We will reenable time travel once our subgraph is caught up const main = async (timestamp = null) => { - timestamp = timestamp ? parseInt(timestamp) : Math.floor(Date.now() / 1000); + // timestamp = timestamp ? parseInt(timestamp) : Math.floor(Date.now() / 1000); + // NOTE: OUR SUBGRAPH IS NOT CAUGHT UP TO DATE, SO WE ARE USING THE API FOR NOW + // ----------------------------------------------------------------------------- // Get total fees distributed for junior and senior tranches - const feesDistributedIds = await fetchFeeDistributedIds(timestamp); - const { totalJunior, totalSenior } = await fetchTransfersForFeeDistributedIds( - feesDistributedIds - ); + // const feesDistributedIds = await fetchFeeDistributedIds(timestamp); + // const { totalJunior, totalSenior } = await fetchTransfersForFeeDistributedIds( + // feesDistributedIds + // ); + + // const [block] = await getBlocksByTime([timestamp], 'base'); - const [block] = await getBlocksByTime([timestamp], 'base'); + const { meta } = await getData( + 'https://api.avantisfi.com/v1/vault/returns-7-days' + ); // Get TVL for junior and senior tranches let [juniorTvl, seniorTvl] = await Promise.all([ @@ -115,14 +124,14 @@ const main = async (timestamp = null) => { target: ADDRESSES.base.USDC, params: [ADDRESSES.base.AvantisJuniorTranche], chain: 'base', - block: block, + // block: block, }), sdk.api.abi.call({ abi: 'erc20:balanceOf', target: ADDRESSES.base.USDC, params: [ADDRESSES.base.AvantisSeniorTranche], chain: 'base', - block: block, + // block: block, }), ]); @@ -130,8 +139,10 @@ const main = async (timestamp = null) => { seniorTvl = seniorTvl.output / 1e6; // Calculate daily returns for junior and senior tranches - const juniorDailyReturns = totalJunior / juniorTvl; - const seniorDailyReturns = totalSenior / seniorTvl; + // const juniorDailyReturns = totalJunior / juniorTvl; + // const seniorDailyReturns = totalSenior / seniorTvl; + const juniorDailyReturns = meta.averageJrFees / juniorTvl; + const seniorDailyReturns = meta.averageSrFees / seniorTvl; // Calculate APY for junior and senior tranches const juniorApy = (1 + juniorDailyReturns) ** 365 - 1; @@ -162,6 +173,6 @@ const main = async (timestamp = null) => { }; module.exports = { - timetravel: true, + timetravel: false, apy: main, }; From 8420ff3a2a4c6f863823b82265811bd1f0b6302a Mon Sep 17 00:00:00 2001 From: Yug Kapoor Date: Fri, 31 Oct 2025 19:01:58 +0530 Subject: [PATCH 6/7] update avantis vault --- package-lock.json | 2 +- src/adaptors/avantis/helpers.js | 14 --- src/adaptors/avantis/index.js | 163 ++++---------------------------- 3 files changed, 17 insertions(+), 162 deletions(-) delete mode 100644 src/adaptors/avantis/helpers.js diff --git a/package-lock.json b/package-lock.json index b1d1f00914..543dc8be60 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8263,7 +8263,7 @@ "version": "1.0.3", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==", - "devOptional": true + "dev": true }, "node_modules/crc-32": { "version": "1.2.2", diff --git a/src/adaptors/avantis/helpers.js b/src/adaptors/avantis/helpers.js deleted file mode 100644 index 3f658f3a64..0000000000 --- a/src/adaptors/avantis/helpers.js +++ /dev/null @@ -1,14 +0,0 @@ -module.exports = { - chunkArray: (array, size) => { - const result = []; - for (let i = 0; i < array.length; i += size) { - result.push(array.slice(i, i + size)); - } - return result; - }, - calculateTrancheTotal: (transfers, trancheAddress) => { - return transfers - .filter((t) => t.to.toLowerCase() === trancheAddress.toLowerCase()) - .reduce((acc, t) => acc + parseFloat((t.value / 1e6).toFixed(6)), 0); - }, -}; diff --git a/src/adaptors/avantis/index.js b/src/adaptors/avantis/index.js index 2301ce1345..275770bb8f 100644 --- a/src/adaptors/avantis/index.js +++ b/src/adaptors/avantis/index.js @@ -1,173 +1,42 @@ const utils = require('../utils'); const sdk = require('@defillama/sdk'); -const { request, gql } = require('graphql-request'); -const { chunkArray, calculateTrancheTotal } = require('./helpers'); const { getBlocksByTime, getData } = require('../utils'); const ADDRESSES = { base: { - AvantisJuniorTranche: '0x944766f715b51967E56aFdE5f0Aa76cEaCc9E7f9', - AvantisSeniorTranche: '0x83084cB182162473d6FEFfCd3Aa48BA55a7B66F7', + AvantisVault: '0x944766f715b51967E56aFdE5f0Aa76cEaCc9E7f9', USDC: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', }, }; -const SUBGRAPH_URL = - 'https://subgraph.satsuma-prod.com/052b6e8d4af9/avantis/avantis-mainnet/version/v0.1.9/api'; - -const feeFetchCount = 10000; - -const feeDistributedQuery = gql` - query FeesDistributeds($first: Int!, $skip: Int!, $start: Int!, $end: Int!) { - feesDistributeds( - first: $first - skip: $skip - where: { timestamp_gte: $start, timestamp_lte: $end } - orderBy: timestamp - orderDirection: asc - ) { - id - } - } -`; - -const vmToTrancheQuery = gql` - query VmToTrancheTransfers($transactionHashes: [String!]!, $to: [String!]!) { - vmtoTrancheTransfers( - where: { transactionHash_in: $transactionHashes, to_in: $to } - ) { - value - to - } - } -`; - -const fetchFeeDistributedIds = async (timestamp, skip = 0) => { - const { feesDistributeds } = await request( - SUBGRAPH_URL, - feeDistributedQuery, - { - first: feeFetchCount, - skip: skip, - start: timestamp - 7 * 86400, - end: timestamp, - } - ); - - let ids = feesDistributeds.map((f) => f.id); - - if (ids.length === feeFetchCount) { - ids = ids.concat( - await fetchFeeDistributedIds(timestamp, skip + feeFetchCount) - ); - } - - return ids; -}; - -const fetchTransfersForFeeDistributedIds = async (ids) => { - const chunkedIds = chunkArray(ids, 1000); - - let totalJunior = 0; - let totalSenior = 0; - - for (const chunk of chunkedIds) { - const { vmtoTrancheTransfers } = await request( - SUBGRAPH_URL, - vmToTrancheQuery, - { - transactionHashes: chunk, - to: [ - ADDRESSES.base.AvantisJuniorTranche, - ADDRESSES.base.AvantisSeniorTranche, - ], - } - ); - - totalJunior += calculateTrancheTotal( - vmtoTrancheTransfers, - ADDRESSES.base.AvantisJuniorTranche - ); - totalSenior += calculateTrancheTotal( - vmtoTrancheTransfers, - ADDRESSES.base.AvantisSeniorTranche - ); - } - - return { totalJunior: totalJunior / 7, totalSenior: totalSenior / 7 }; -}; - -// NOTE: OUR SUBGRAPH IS NOT CAUGHT UP TO DATE, SO WE ARE USING THE API FOR NOW -// ----------------------------------------------------------------------------- -// We will reenable time travel once our subgraph is caught up const main = async (timestamp = null) => { - // timestamp = timestamp ? parseInt(timestamp) : Math.floor(Date.now() / 1000); - - // NOTE: OUR SUBGRAPH IS NOT CAUGHT UP TO DATE, SO WE ARE USING THE API FOR NOW - // ----------------------------------------------------------------------------- - // Get total fees distributed for junior and senior tranches - // const feesDistributedIds = await fetchFeeDistributedIds(timestamp); - // const { totalJunior, totalSenior } = await fetchTransfersForFeeDistributedIds( - // feesDistributedIds - // ); - - // const [block] = await getBlocksByTime([timestamp], 'base'); - const { meta } = await getData( 'https://api.avantisfi.com/v1/vault/returns-7-days' ); - // Get TVL for junior and senior tranches - let [juniorTvl, seniorTvl] = await Promise.all([ - sdk.api.abi.call({ - abi: 'erc20:balanceOf', - target: ADDRESSES.base.USDC, - params: [ADDRESSES.base.AvantisJuniorTranche], - chain: 'base', - // block: block, - }), - sdk.api.abi.call({ - abi: 'erc20:balanceOf', - target: ADDRESSES.base.USDC, - params: [ADDRESSES.base.AvantisSeniorTranche], - chain: 'base', - // block: block, - }), - ]); - - juniorTvl = juniorTvl.output / 1e6; - seniorTvl = seniorTvl.output / 1e6; + let vaultTvl = await sdk.api.abi.call({ + abi: 'erc20:balanceOf', + target: ADDRESSES.base.USDC, + params: [ADDRESSES.base.AvantisVault], + chain: 'base', + // block: block, + }); - // Calculate daily returns for junior and senior tranches - // const juniorDailyReturns = totalJunior / juniorTvl; - // const seniorDailyReturns = totalSenior / seniorTvl; - const juniorDailyReturns = meta.averageJrFees / juniorTvl; - const seniorDailyReturns = meta.averageSrFees / seniorTvl; + vaultTvl = vaultTvl.output / 1e6; - // Calculate APY for junior and senior tranches - const juniorApy = (1 + juniorDailyReturns) ** 365 - 1; - const seniorApy = (1 + seniorDailyReturns) ** 365 - 1; + const dailyReturns = meta.averageJrFees / vaultTvl; + const apy = (1 + dailyReturns) ** 365 - 1; return [ { - pool: `AVANTIS-${ADDRESSES.base.AvantisJuniorTranche}-base`.toLowerCase(), - chain: 'base', - project: 'avantis', - symbol: 'USDC', - poolMeta: 'junior', - tvlUsd: juniorTvl, - apyBase: juniorApy * 100, - url: 'https://www.avantisfi.com/earn/junior', - }, - { - pool: `AVANTIS-${ADDRESSES.base.AvantisSeniorTranche}-base`.toLowerCase(), + pool: `AVANTIS-${ADDRESSES.base.AvantisVault}-base`.toLowerCase(), chain: 'base', project: 'avantis', symbol: 'USDC', - poolMeta: 'senior', - tvlUsd: seniorTvl, - apyBase: seniorApy * 100, - url: 'https://www.avantisfi.com/earn/senior', + poolMeta: 'vault', + tvlUsd: vaultTvl, + apyBase: apy * 100, + url: 'https://www.avantisfi.com/earn/avantis-vault', }, ]; }; From 181e4d3265e8d292d8449e13c7d796b07ea2f8dd Mon Sep 17 00:00:00 2001 From: Yug Kapoor Date: Fri, 31 Oct 2025 19:01:58 +0530 Subject: [PATCH 7/7] update avantis vault --- package-lock.json | 2 +- src/adaptors/avantis/helpers.js | 14 --- src/adaptors/avantis/index.js | 163 ++++---------------------------- 3 files changed, 17 insertions(+), 162 deletions(-) delete mode 100644 src/adaptors/avantis/helpers.js diff --git a/package-lock.json b/package-lock.json index b1d1f00914..543dc8be60 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8263,7 +8263,7 @@ "version": "1.0.3", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==", - "devOptional": true + "dev": true }, "node_modules/crc-32": { "version": "1.2.2", diff --git a/src/adaptors/avantis/helpers.js b/src/adaptors/avantis/helpers.js deleted file mode 100644 index 3f658f3a64..0000000000 --- a/src/adaptors/avantis/helpers.js +++ /dev/null @@ -1,14 +0,0 @@ -module.exports = { - chunkArray: (array, size) => { - const result = []; - for (let i = 0; i < array.length; i += size) { - result.push(array.slice(i, i + size)); - } - return result; - }, - calculateTrancheTotal: (transfers, trancheAddress) => { - return transfers - .filter((t) => t.to.toLowerCase() === trancheAddress.toLowerCase()) - .reduce((acc, t) => acc + parseFloat((t.value / 1e6).toFixed(6)), 0); - }, -}; diff --git a/src/adaptors/avantis/index.js b/src/adaptors/avantis/index.js index 2301ce1345..275770bb8f 100644 --- a/src/adaptors/avantis/index.js +++ b/src/adaptors/avantis/index.js @@ -1,173 +1,42 @@ const utils = require('../utils'); const sdk = require('@defillama/sdk'); -const { request, gql } = require('graphql-request'); -const { chunkArray, calculateTrancheTotal } = require('./helpers'); const { getBlocksByTime, getData } = require('../utils'); const ADDRESSES = { base: { - AvantisJuniorTranche: '0x944766f715b51967E56aFdE5f0Aa76cEaCc9E7f9', - AvantisSeniorTranche: '0x83084cB182162473d6FEFfCd3Aa48BA55a7B66F7', + AvantisVault: '0x944766f715b51967E56aFdE5f0Aa76cEaCc9E7f9', USDC: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', }, }; -const SUBGRAPH_URL = - 'https://subgraph.satsuma-prod.com/052b6e8d4af9/avantis/avantis-mainnet/version/v0.1.9/api'; - -const feeFetchCount = 10000; - -const feeDistributedQuery = gql` - query FeesDistributeds($first: Int!, $skip: Int!, $start: Int!, $end: Int!) { - feesDistributeds( - first: $first - skip: $skip - where: { timestamp_gte: $start, timestamp_lte: $end } - orderBy: timestamp - orderDirection: asc - ) { - id - } - } -`; - -const vmToTrancheQuery = gql` - query VmToTrancheTransfers($transactionHashes: [String!]!, $to: [String!]!) { - vmtoTrancheTransfers( - where: { transactionHash_in: $transactionHashes, to_in: $to } - ) { - value - to - } - } -`; - -const fetchFeeDistributedIds = async (timestamp, skip = 0) => { - const { feesDistributeds } = await request( - SUBGRAPH_URL, - feeDistributedQuery, - { - first: feeFetchCount, - skip: skip, - start: timestamp - 7 * 86400, - end: timestamp, - } - ); - - let ids = feesDistributeds.map((f) => f.id); - - if (ids.length === feeFetchCount) { - ids = ids.concat( - await fetchFeeDistributedIds(timestamp, skip + feeFetchCount) - ); - } - - return ids; -}; - -const fetchTransfersForFeeDistributedIds = async (ids) => { - const chunkedIds = chunkArray(ids, 1000); - - let totalJunior = 0; - let totalSenior = 0; - - for (const chunk of chunkedIds) { - const { vmtoTrancheTransfers } = await request( - SUBGRAPH_URL, - vmToTrancheQuery, - { - transactionHashes: chunk, - to: [ - ADDRESSES.base.AvantisJuniorTranche, - ADDRESSES.base.AvantisSeniorTranche, - ], - } - ); - - totalJunior += calculateTrancheTotal( - vmtoTrancheTransfers, - ADDRESSES.base.AvantisJuniorTranche - ); - totalSenior += calculateTrancheTotal( - vmtoTrancheTransfers, - ADDRESSES.base.AvantisSeniorTranche - ); - } - - return { totalJunior: totalJunior / 7, totalSenior: totalSenior / 7 }; -}; - -// NOTE: OUR SUBGRAPH IS NOT CAUGHT UP TO DATE, SO WE ARE USING THE API FOR NOW -// ----------------------------------------------------------------------------- -// We will reenable time travel once our subgraph is caught up const main = async (timestamp = null) => { - // timestamp = timestamp ? parseInt(timestamp) : Math.floor(Date.now() / 1000); - - // NOTE: OUR SUBGRAPH IS NOT CAUGHT UP TO DATE, SO WE ARE USING THE API FOR NOW - // ----------------------------------------------------------------------------- - // Get total fees distributed for junior and senior tranches - // const feesDistributedIds = await fetchFeeDistributedIds(timestamp); - // const { totalJunior, totalSenior } = await fetchTransfersForFeeDistributedIds( - // feesDistributedIds - // ); - - // const [block] = await getBlocksByTime([timestamp], 'base'); - const { meta } = await getData( 'https://api.avantisfi.com/v1/vault/returns-7-days' ); - // Get TVL for junior and senior tranches - let [juniorTvl, seniorTvl] = await Promise.all([ - sdk.api.abi.call({ - abi: 'erc20:balanceOf', - target: ADDRESSES.base.USDC, - params: [ADDRESSES.base.AvantisJuniorTranche], - chain: 'base', - // block: block, - }), - sdk.api.abi.call({ - abi: 'erc20:balanceOf', - target: ADDRESSES.base.USDC, - params: [ADDRESSES.base.AvantisSeniorTranche], - chain: 'base', - // block: block, - }), - ]); - - juniorTvl = juniorTvl.output / 1e6; - seniorTvl = seniorTvl.output / 1e6; + let vaultTvl = await sdk.api.abi.call({ + abi: 'erc20:balanceOf', + target: ADDRESSES.base.USDC, + params: [ADDRESSES.base.AvantisVault], + chain: 'base', + // block: block, + }); - // Calculate daily returns for junior and senior tranches - // const juniorDailyReturns = totalJunior / juniorTvl; - // const seniorDailyReturns = totalSenior / seniorTvl; - const juniorDailyReturns = meta.averageJrFees / juniorTvl; - const seniorDailyReturns = meta.averageSrFees / seniorTvl; + vaultTvl = vaultTvl.output / 1e6; - // Calculate APY for junior and senior tranches - const juniorApy = (1 + juniorDailyReturns) ** 365 - 1; - const seniorApy = (1 + seniorDailyReturns) ** 365 - 1; + const dailyReturns = meta.averageJrFees / vaultTvl; + const apy = (1 + dailyReturns) ** 365 - 1; return [ { - pool: `AVANTIS-${ADDRESSES.base.AvantisJuniorTranche}-base`.toLowerCase(), - chain: 'base', - project: 'avantis', - symbol: 'USDC', - poolMeta: 'junior', - tvlUsd: juniorTvl, - apyBase: juniorApy * 100, - url: 'https://www.avantisfi.com/earn/junior', - }, - { - pool: `AVANTIS-${ADDRESSES.base.AvantisSeniorTranche}-base`.toLowerCase(), + pool: `AVANTIS-${ADDRESSES.base.AvantisVault}-base`.toLowerCase(), chain: 'base', project: 'avantis', symbol: 'USDC', - poolMeta: 'senior', - tvlUsd: seniorTvl, - apyBase: seniorApy * 100, - url: 'https://www.avantisfi.com/earn/senior', + poolMeta: 'vault', + tvlUsd: vaultTvl, + apyBase: apy * 100, + url: 'https://www.avantisfi.com/earn/avantis-vault', }, ]; };