From ce15db5120c87b6b0e445fd82313096331c69492 Mon Sep 17 00:00:00 2001 From: NeutralTrade Date: Wed, 11 Dec 2024 16:36:23 +0800 Subject: [PATCH 1/8] Create constants.js --- projects/neutral-trade/constants.js | 122 ++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 projects/neutral-trade/constants.js diff --git a/projects/neutral-trade/constants.js b/projects/neutral-trade/constants.js new file mode 100644 index 000000000000..82625b364e9a --- /dev/null +++ b/projects/neutral-trade/constants.js @@ -0,0 +1,122 @@ +const DATA_URL = 'https://raw.githubusercontent.com/neutral-trade/vaults-data/main'; + +const START_TIMESTAMP = 1730419200; // 2024-11-01 + + +const TOKENS = { + USDC: { + name: 'USDC', + mint: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', + decimals: 6 + }, + SOL: { + name: 'SOL', + mint: 'So11111111111111111111111111111111111111112', + decimals: 9 + }, + WBTC: { + name: 'Wrapped BTC (Wormhole) (WBTC)', + mint: '3NZ9JMVBmGAqocybic2c7LQCJScmgsAZ6vQqTDzcqmJh', + decimals: 8 + }, + WETH: { + name: 'Wrapped ETH (Wormhole) (WETH)', + mint: '7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs', + decimals: 8 + } +}; + +const VAULTS = [ + { + name: "USDC Basis (Perps Basket)", + address: "4cvgasNfbJ36yeMVJSkscgL2Yco9dFGdj52Wrg91fmHv", + token: TOKENS.USDC, + dataUrl: `${DATA_URL}/btcethfunding.json` + }, + { + name: "BTC Super Staking", + address: "BVddkVtFJLCihbVrtLo8e3iEd9NftuLunaznAxFFW8vf", + token: TOKENS.WBTC, + dataUrl: `${DATA_URL}/btcjlpnl.json` + }, + { + name: "ETH Super Staking", + address: "ENr5e1BMN5vFUHf4iCCPzR4GjWCKgtHnQcdniRQqMdEL", + token: TOKENS.WETH, + dataUrl: `${DATA_URL}/ethjlpnl.json` + }, + { + name: "USDC Staking (JLP Delta Neutral)", + address: "9Fcn3Fd4d5ocrb12xCUtEvezxcjFEAyHBPfrZDiPt9Qj", + token: TOKENS.USDC, + dataUrl: `${DATA_URL}/jlpdn.json` + }, + { + name: "USDC Staking (JLP Delta Neutral) v2", + address: "41Y8C4oxk4zgJT1KXyQr35UhZcfsp5mP86Z2G7UUzojU", + token: TOKENS.USDC, + dataUrl: `${DATA_URL}/jlpdnv2.json` + }, + { + name: "USDC Basis (Sanctum-INF)", + address: "CxL8eQmGhN9LKSoHj7bU95JekFPtyZoUc57mbehb5A56", + token: TOKENS.USDC, + dataUrl: `${DATA_URL}/solbasis.json` + }, + { + name: "SOL Super Staking", + address: "EuSLjg23BrtwYAk1t4TFe5ArYSXCVXLBqrHRBfWQiTeJ", + token: TOKENS.SOL, + dataUrl: `${DATA_URL}/soljlpnl.json` + }, + { + name: "JLP Traders Print", + address: "85XuR4kE5yxp1hk91WHAawinXZsuJowxy59STYYpM9pK", + token: TOKENS.USDC, + dataUrl: `${DATA_URL}/tradersprint.json` + }, + { + name: "JLP Traders Rekt", + address: "2r81MPMDjGSrbmGRwzDg6aqhe3t3vbKcrYfpes5bXckS", + token: TOKENS.USDC, + dataUrl: `${DATA_URL}/tradersrekt.json` + }, + { + name: "vip01", + address: "CZU38L2NyL6tqFxzYAGYkmkf2JG98tZfZ2CnUapVgXQe", + token: TOKENS.USDC, + dataUrl: `${DATA_URL}/vip01.json` + }, + { + name: "vip02", + address: "DUW6uWcrsjYmsYDjp9iGDN4JdRa2MqznjuxjKVok5Fsj", + token: TOKENS.USDC, + dataUrl: `${DATA_URL}/vip02.json` + }, + { + name: "vip03", + address: "Fd3k4c6Dv7m9673ae87P6duQrftY9UVfwiCxngNbJrUQ", + token: TOKENS.USDC, + dataUrl: `${DATA_URL}/vip03.json` + }, + // 'vip04', // currently inactive + { + name: "vip05", + address: "9BMEyctGvajEubk5iCRBnM9fkeTXUhrxaweYq34jZdC8", + token: TOKENS.USDC, + dataUrl: `${DATA_URL}/vip05.json` + }, + { + name: "vip06", + address: "6DFDj66PbPoTC16Sh51MJijoTTMYCbMCVC85tnc5UfQ3", + token: TOKENS.USDC, + dataUrl: `${DATA_URL}/vip06.json` + }, +]; + +module.exports = { + DATA_URL, + START_TIMESTAMP, + TOKENS, + VAULTS, +}; From 027307af07b015b9cef39caa6e56df37ce227ca2 Mon Sep 17 00:00:00 2001 From: NeutralTrade Date: Wed, 11 Dec 2024 16:36:40 +0800 Subject: [PATCH 2/8] Create index.js --- projects/neutral-trade/index.js | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 projects/neutral-trade/index.js diff --git a/projects/neutral-trade/index.js b/projects/neutral-trade/index.js new file mode 100644 index 000000000000..c0435fc07abe --- /dev/null +++ b/projects/neutral-trade/index.js @@ -0,0 +1,36 @@ +const { VAULTS, START_TIMESTAMP } = require("./constants"); + +async function fetchHistoricalTvl(vault, date) { + let usd = 0; + let token = 0; + try { + const response = await fetch(vault.dataUrl); + const all = await response.json(); + const [, atDate] = Object.entries(all).find(([entryDate,]) => { + return entryDate.startsWith(date); + }); + usd = atDate?.TVL_USD || 0; + token = atDate?.TVL_TOKEN || 0; + } catch (_e) { } + return { usd, token }; +} + +async function tvl(api) { + const ts = new Date(api.timestamp * 1000); + const year = ts.getFullYear(); + const month = String(ts.getMonth() + 1).padStart(2, '0'); + const day = String(ts.getDate()).padStart(2, '0'); + const date = `${year}-${month}-${day}`; + + for (let i = 0; i < VAULTS.length; i++) { + const { token } = await fetchHistoricalTvl(VAULTS[i], date); + api.add(VAULTS[i].token.mint, token * Math.pow(10, VAULTS[i].token.decimals)); + } +} + +module.exports = { + start: START_TIMESTAMP, + timetravel: true, + methodology: "The combined TVL and PnL of all public and private vaults ; the historical data is pulled from the Neutral.trade vaults data repository.", + solana: { tvl }, +}; From 2e3b50a49b795efca504d66ae2386771e65dad1e Mon Sep 17 00:00:00 2001 From: NeutralTrade Date: Thu, 12 Dec 2024 11:59:00 +0800 Subject: [PATCH 3/8] fix lint --- projects/neutral-trade/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/projects/neutral-trade/index.js b/projects/neutral-trade/index.js index c0435fc07abe..d43dc028b41f 100644 --- a/projects/neutral-trade/index.js +++ b/projects/neutral-trade/index.js @@ -11,7 +11,9 @@ async function fetchHistoricalTvl(vault, date) { }); usd = atDate?.TVL_USD || 0; token = atDate?.TVL_TOKEN || 0; - } catch (_e) { } + } catch (_e) { + console.log(_e); + } return { usd, token }; } From a1f23f6c4ede17d285ba65f2fd8b0beb1e0682eb Mon Sep 17 00:00:00 2001 From: NeutralTrade Date: Thu, 12 Dec 2024 12:29:57 +0800 Subject: [PATCH 4/8] handle missing date use last recorded entry in case a specific date in missing --- projects/neutral-trade/index.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/projects/neutral-trade/index.js b/projects/neutral-trade/index.js index d43dc028b41f..6bcff16dbc66 100644 --- a/projects/neutral-trade/index.js +++ b/projects/neutral-trade/index.js @@ -1,20 +1,25 @@ const { VAULTS, START_TIMESTAMP } = require("./constants"); async function fetchHistoricalTvl(vault, date) { - let usd = 0; - let token = 0; + let usd_tvl = 0; + let token_tvl = 0; try { const response = await fetch(vault.dataUrl); - const all = await response.json(); - const [, atDate] = Object.entries(all).find(([entryDate,]) => { - return entryDate.startsWith(date); - }); - usd = atDate?.TVL_USD || 0; - token = atDate?.TVL_TOKEN || 0; + const all = Object.entries(await response.json()); + for (let i = 0; i < all.length; i++) { + const [entryTime, entryData] = all[i]; + usd_tvl = entryData?.TVL_USD; + token_tvl = entryData?.TVL_TOKEN; + if (entryTime?.startsWith(date)) { + break; + } + } + usd_tvl = usd_tvl || 0; + token_tvl = (token_tvl || 0) * Math.pow(10, vault.token.decimals); } catch (_e) { console.log(_e); } - return { usd, token }; + return { usd_tvl, token_tvl }; } async function tvl(api) { @@ -25,8 +30,8 @@ async function tvl(api) { const date = `${year}-${month}-${day}`; for (let i = 0; i < VAULTS.length; i++) { - const { token } = await fetchHistoricalTvl(VAULTS[i], date); - api.add(VAULTS[i].token.mint, token * Math.pow(10, VAULTS[i].token.decimals)); + const { token_tvl } = await fetchHistoricalTvl(VAULTS[i], date); + api.add(VAULTS[i].token.mint, token_tvl); } } From 31bda8ebc9d1e0b52e807e2afc080287a68db977 Mon Sep 17 00:00:00 2001 From: NeutralTrade Date: Sun, 15 Dec 2024 15:05:34 +0800 Subject: [PATCH 5/8] Create helpers.js --- projects/neutral-trade/helpers.js | 58 +++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 projects/neutral-trade/helpers.js diff --git a/projects/neutral-trade/helpers.js b/projects/neutral-trade/helpers.js new file mode 100644 index 000000000000..579e57b7b42c --- /dev/null +++ b/projects/neutral-trade/helpers.js @@ -0,0 +1,58 @@ +const { Connection, Keypair } = require("@solana/web3.js"); +const { AnchorProvider, Program } = require('@project-serum/anchor'); +const { DriftClient } = require('@drift-labs/sdk'); +const { IDL, VAULT_PROGRAM_ID, VaultClient } = require("@drift-labs/vaults-sdk"); +const { JLPDN_PROGAM_ID_V1 } = require("./constants"); + + +/// INIT DRIFT SDK +const initialize = async () => { + const connection = new Connection('https://cold-hanni-fast-mainnet.helius-rpc.com/'); + const wallet = Keypair.generate(); + const initializeDriftClient = async () => { + const driftClient = new DriftClient({ + connection, + wallet, + env: "mainnet-beta", + }); + await driftClient.subscribe(); + return driftClient; + }; + const initializeProgram = (version) => { + const provider = new AnchorProvider(connection, wallet, AnchorProvider.defaultOptions()); + return new Program( + IDL, + version == 1 ? JLPDN_PROGAM_ID_V1 : VAULT_PROGRAM_ID, + provider + ); + }; + const initializeVaultClient = async (version) => { + return new VaultClient({ + driftClient: await initializeDriftClient(), + program: initializeProgram(version), + cliMode: true, + }); + }; + const client = await initializeVaultClient(); + const client_jlpdnv1 = await initializeVaultClient(1); + return { client, client_jlpdnv1 }; +}; + + +/** + * Utility function to format a timestamp into YYYY-MM-DD format. + * @param {number} ts - The timestamp in milliseconds. + * @returns {string} - The formatted date string. + */ +function formatDate(date) { + const year = date.getFullYear(); + const month = String(date.getMonth() + 1).padStart(2, '0'); + const day = String(date.getDate()).padStart(2, '0'); + return `${year}-${month}-${day}`; +} + + +module.exports = { + initialize, + formatDate, +}; From 73d903a21ebf869ae889103ea7c500d765265020 Mon Sep 17 00:00:00 2001 From: NeutralTrade Date: Sun, 15 Dec 2024 15:06:11 +0800 Subject: [PATCH 6/8] Update index.js --- projects/neutral-trade/index.js | 98 +++++++++++++++++++++++++-------- 1 file changed, 76 insertions(+), 22 deletions(-) diff --git a/projects/neutral-trade/index.js b/projects/neutral-trade/index.js index 6bcff16dbc66..0ac0e671aad0 100644 --- a/projects/neutral-trade/index.js +++ b/projects/neutral-trade/index.js @@ -1,43 +1,97 @@ +const { PublicKey } = require("@solana/web3.js"); const { VAULTS, START_TIMESTAMP } = require("./constants"); +const { initialize, formatDate } = require("./helpers"); + +/** + * Fetch historical TVL data from the vault's data repository. + * @param {object} vault - Vault object containing data URL and token details. + * @param {string} date - Date string in YYYY-MM-DD format. + * @returns {Promise<{usd_tvl: number, token_tvl: number}>} - Historical TVL data. + */ async function fetchHistoricalTvl(vault, date) { let usd_tvl = 0; let token_tvl = 0; + try { const response = await fetch(vault.dataUrl); - const all = Object.entries(await response.json()); - for (let i = 0; i < all.length; i++) { - const [entryTime, entryData] = all[i]; - usd_tvl = entryData?.TVL_USD; - token_tvl = entryData?.TVL_TOKEN; - if (entryTime?.startsWith(date)) { - break; - } + const allEntries = Object.entries(await response.json()); + const entry = allEntries.find(([entryTime]) => entryTime?.startsWith(date)); + if (entry) { + const [, entryData] = entry; + usd_tvl = entryData?.TVL_USD || 0; + token_tvl = (entryData?.TVL_TOKEN || 0) * Math.pow(10, vault.token.decimals); } - usd_tvl = usd_tvl || 0; - token_tvl = (token_tvl || 0) * Math.pow(10, vault.token.decimals); - } catch (_e) { - console.log(_e); + } catch (error) { + console.error(`Error fetching historical TVL for vault ${vault.name} on ${date}:`, error); } + return { usd_tvl, token_tvl }; } + +/** + * Fetch the current TVL of a vault from the blockchain. + * @param {object} vault - Vault object containing address and token details. + * @param {object} client - Drift client for interacting with the blockchain. + * @returns {Promise} - Current token TVL. + */ +async function fetchCurrentTvl(vault, client) { + try { + const vaultInstance = await client.getVault(new PublicKey(vault.address)); + const token_tvl = await client.calculateVaultEquityInDepositAsset({ + address: vault.address, + vault: vaultInstance, + factorUnrealizedPNL: true, + }); + return Number(token_tvl); + } catch (error) { + console.error(`Error fetching current TVL for vault ${vault.name}:`, error); + return 0; + } +} + + +/** + * Main TVL calculation function, handling both historical and current data. + * @param {object} api - API object to aggregate TVL data. + */ async function tvl(api) { - const ts = new Date(api.timestamp * 1000); - const year = ts.getFullYear(); - const month = String(ts.getMonth() + 1).padStart(2, '0'); - const day = String(ts.getDate()).padStart(2, '0'); - const date = `${year}-${month}-${day}`; - - for (let i = 0; i < VAULTS.length; i++) { - const { token_tvl } = await fetchHistoricalTvl(VAULTS[i], date); - api.add(VAULTS[i].token.mint, token_tvl); + const queryDate = new Date(api.timestamp * 1000); + queryDate.setHours(0, 0, 0, 0); + const currentDate = new Date(); + currentDate.setHours(0, 0, 0, 0); + + const isHistorical = queryDate.valueOf() < currentDate.valueOf(); + + // Fetch historical TVL from Neutral.trade data repository. + if (isHistorical) { + console.log("fetching tvl from historical data repository"); + for (const vault of VAULTS) { + const { token_tvl } = await fetchHistoricalTvl(vault, formatDate(queryDate)); + api.add(vault.token.mint, token_tvl); + } + } + + // Fetch current TVL from the blockchain. + else { + console.log("fetching tvl from current onchain data"); + const { client, client_jlpdnv1 } = await initialize(); + await Promise.all( + VAULTS.map(async (vault) => { + const _client = + vault.name === "USDC Staking (JLP Delta Neutral)" ? client_jlpdnv1 : client; + const token_tvl = await fetchCurrentTvl(vault, _client); + api.add(vault.token.mint, token_tvl); + }) + ); } } + module.exports = { start: START_TIMESTAMP, timetravel: true, - methodology: "The combined TVL and PnL of all public and private vaults ; the historical data is pulled from the Neutral.trade vaults data repository.", + methodology: "The combined TVL and PnL of all public and private vaults.", solana: { tvl }, }; From c9eb0f1684596e69b0491fb611d887d1d8f752f6 Mon Sep 17 00:00:00 2001 From: NeutralTrade Date: Sun, 15 Dec 2024 15:07:45 +0800 Subject: [PATCH 7/8] add "@drift-labs/vaults-sdk": "^0.2.13" --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 16a0dbca56c7..b9e48e8e781a 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "dependencies": { "@coral-xyz/anchor": "^0.30.1", "@defillama/sdk": "latest", + "@drift-labs/vaults-sdk": "^0.2.13", "@project-serum/anchor": "^0.26.0", "@solana/web3.js": "^1.87.6", "@solendprotocol/solend-sdk": "^0.4.3", From adea909482e83717460875a703d581d80920f6e5 Mon Sep 17 00:00:00 2001 From: g1nt0ki <99907941+g1nt0ki@users.noreply.github.com> Date: Mon, 23 Dec 2024 11:46:04 +0100 Subject: [PATCH 8/8] add a hack --- package.json | 1 - projects/helper/chain/rpcProxy.js | 6 ++ projects/neutral-trade/constants.js | 13 ++-- projects/neutral-trade/helpers.js | 58 ------------------ projects/neutral-trade/index.js | 92 ++--------------------------- 5 files changed, 19 insertions(+), 151 deletions(-) delete mode 100644 projects/neutral-trade/helpers.js diff --git a/package.json b/package.json index b9e48e8e781a..16a0dbca56c7 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,6 @@ "dependencies": { "@coral-xyz/anchor": "^0.30.1", "@defillama/sdk": "latest", - "@drift-labs/vaults-sdk": "^0.2.13", "@project-serum/anchor": "^0.26.0", "@solana/web3.js": "^1.87.6", "@solendprotocol/solend-sdk": "^0.4.3", diff --git a/projects/helper/chain/rpcProxy.js b/projects/helper/chain/rpcProxy.js index a1dd3ce6e690..ee05f6104b8f 100644 --- a/projects/helper/chain/rpcProxy.js +++ b/projects/helper/chain/rpcProxy.js @@ -20,6 +20,12 @@ module.exports = { return data } }, + drift: { + vaultTvl: async (vault, version) => { + const { data } = await client.get('/drift/vault_tvl', { params: { vault, version } }) + return data + } + }, injective: { mitoVaultQuery: async ({ address }) => { const { data } = await client.get('/injective/mito-vault/' + address) diff --git a/projects/neutral-trade/constants.js b/projects/neutral-trade/constants.js index 82625b364e9a..17c9b3eaab13 100644 --- a/projects/neutral-trade/constants.js +++ b/projects/neutral-trade/constants.js @@ -45,12 +45,13 @@ const VAULTS = [ token: TOKENS.WETH, dataUrl: `${DATA_URL}/ethjlpnl.json` }, - { - name: "USDC Staking (JLP Delta Neutral)", - address: "9Fcn3Fd4d5ocrb12xCUtEvezxcjFEAyHBPfrZDiPt9Qj", - token: TOKENS.USDC, - dataUrl: `${DATA_URL}/jlpdn.json` - }, + // { + // name: "USDC Staking (JLP Delta Neutral)", + // address: "9Fcn3Fd4d5ocrb12xCUtEvezxcjFEAyHBPfrZDiPt9Qj", + // token: TOKENS.USDC, + // dataUrl: `${DATA_URL}/jlpdn.json`, + // version: 1, + // }, { name: "USDC Staking (JLP Delta Neutral) v2", address: "41Y8C4oxk4zgJT1KXyQr35UhZcfsp5mP86Z2G7UUzojU", diff --git a/projects/neutral-trade/helpers.js b/projects/neutral-trade/helpers.js deleted file mode 100644 index 579e57b7b42c..000000000000 --- a/projects/neutral-trade/helpers.js +++ /dev/null @@ -1,58 +0,0 @@ -const { Connection, Keypair } = require("@solana/web3.js"); -const { AnchorProvider, Program } = require('@project-serum/anchor'); -const { DriftClient } = require('@drift-labs/sdk'); -const { IDL, VAULT_PROGRAM_ID, VaultClient } = require("@drift-labs/vaults-sdk"); -const { JLPDN_PROGAM_ID_V1 } = require("./constants"); - - -/// INIT DRIFT SDK -const initialize = async () => { - const connection = new Connection('https://cold-hanni-fast-mainnet.helius-rpc.com/'); - const wallet = Keypair.generate(); - const initializeDriftClient = async () => { - const driftClient = new DriftClient({ - connection, - wallet, - env: "mainnet-beta", - }); - await driftClient.subscribe(); - return driftClient; - }; - const initializeProgram = (version) => { - const provider = new AnchorProvider(connection, wallet, AnchorProvider.defaultOptions()); - return new Program( - IDL, - version == 1 ? JLPDN_PROGAM_ID_V1 : VAULT_PROGRAM_ID, - provider - ); - }; - const initializeVaultClient = async (version) => { - return new VaultClient({ - driftClient: await initializeDriftClient(), - program: initializeProgram(version), - cliMode: true, - }); - }; - const client = await initializeVaultClient(); - const client_jlpdnv1 = await initializeVaultClient(1); - return { client, client_jlpdnv1 }; -}; - - -/** - * Utility function to format a timestamp into YYYY-MM-DD format. - * @param {number} ts - The timestamp in milliseconds. - * @returns {string} - The formatted date string. - */ -function formatDate(date) { - const year = date.getFullYear(); - const month = String(date.getMonth() + 1).padStart(2, '0'); - const day = String(date.getDate()).padStart(2, '0'); - return `${year}-${month}-${day}`; -} - - -module.exports = { - initialize, - formatDate, -}; diff --git a/projects/neutral-trade/index.js b/projects/neutral-trade/index.js index 0ac0e671aad0..6abdd85f6589 100644 --- a/projects/neutral-trade/index.js +++ b/projects/neutral-trade/index.js @@ -1,97 +1,17 @@ -const { PublicKey } = require("@solana/web3.js"); -const { VAULTS, START_TIMESTAMP } = require("./constants"); -const { initialize, formatDate } = require("./helpers"); +const { VAULTS, START_TIMESTAMP } = require("./constants") +const { drift: { vaultTvl } } = require("../helper/chain/rpcProxy") - -/** - * Fetch historical TVL data from the vault's data repository. - * @param {object} vault - Vault object containing data URL and token details. - * @param {string} date - Date string in YYYY-MM-DD format. - * @returns {Promise<{usd_tvl: number, token_tvl: number}>} - Historical TVL data. - */ -async function fetchHistoricalTvl(vault, date) { - let usd_tvl = 0; - let token_tvl = 0; - - try { - const response = await fetch(vault.dataUrl); - const allEntries = Object.entries(await response.json()); - const entry = allEntries.find(([entryTime]) => entryTime?.startsWith(date)); - if (entry) { - const [, entryData] = entry; - usd_tvl = entryData?.TVL_USD || 0; - token_tvl = (entryData?.TVL_TOKEN || 0) * Math.pow(10, vault.token.decimals); - } - } catch (error) { - console.error(`Error fetching historical TVL for vault ${vault.name} on ${date}:`, error); - } - - return { usd_tvl, token_tvl }; -} - - -/** - * Fetch the current TVL of a vault from the blockchain. - * @param {object} vault - Vault object containing address and token details. - * @param {object} client - Drift client for interacting with the blockchain. - * @returns {Promise} - Current token TVL. - */ -async function fetchCurrentTvl(vault, client) { - try { - const vaultInstance = await client.getVault(new PublicKey(vault.address)); - const token_tvl = await client.calculateVaultEquityInDepositAsset({ - address: vault.address, - vault: vaultInstance, - factorUnrealizedPNL: true, - }); - return Number(token_tvl); - } catch (error) { - console.error(`Error fetching current TVL for vault ${vault.name}:`, error); - return 0; - } -} - - -/** - * Main TVL calculation function, handling both historical and current data. - * @param {object} api - API object to aggregate TVL data. - */ async function tvl(api) { - const queryDate = new Date(api.timestamp * 1000); - queryDate.setHours(0, 0, 0, 0); - const currentDate = new Date(); - currentDate.setHours(0, 0, 0, 0); - - const isHistorical = queryDate.valueOf() < currentDate.valueOf(); - - // Fetch historical TVL from Neutral.trade data repository. - if (isHistorical) { - console.log("fetching tvl from historical data repository"); - for (const vault of VAULTS) { - const { token_tvl } = await fetchHistoricalTvl(vault, formatDate(queryDate)); - api.add(vault.token.mint, token_tvl); - } - } - - // Fetch current TVL from the blockchain. - else { - console.log("fetching tvl from current onchain data"); - const { client, client_jlpdnv1 } = await initialize(); - await Promise.all( - VAULTS.map(async (vault) => { - const _client = - vault.name === "USDC Staking (JLP Delta Neutral)" ? client_jlpdnv1 : client; - const token_tvl = await fetchCurrentTvl(vault, _client); - api.add(vault.token.mint, token_tvl); - }) - ); + for (const vault of VAULTS) { + const token_tvl = await vaultTvl(vault.address, vault.version); + api.add(vault.token.mint, token_tvl); } } module.exports = { start: START_TIMESTAMP, - timetravel: true, + timetravel: false, methodology: "The combined TVL and PnL of all public and private vaults.", solana: { tvl }, };