diff --git a/projects/drift.js b/projects/drift.js index 702364a68c16..da3f57d84a58 100644 --- a/projects/drift.js +++ b/projects/drift.js @@ -1,4 +1,8 @@ +const { PublicKey } = require("@solana/web3.js"); +const anchor = require("@project-serum/anchor"); const { sumTokens2, } = require("./helper/solana"); +const DRIFT_PROGRAM_ID = new PublicKey('dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH') +const { getConfig } = require('./helper/cache') module.exports = { timetravel: false, @@ -8,11 +12,38 @@ module.exports = { }, }; -async function tvl() { - return sumTokens2({ - owners: [ - 'CU4eFxpyCGNDEXN27Jonn7RfgwBt3cnp7TcTrJF6EW9Q', // legacy - 'JCNCMFXo5M5qwUPg2Utu1u6YWp3MbygxqBsBeXXJfrw', // Drift vault - ], - }) +async function tvl(api) { + + const legacyVaults = [ + '6W9yiHDCW9EpropkFV8R3rPiL8LVWUHSiys3YeW6AT6S', // legacy usdc vault + 'Bzjkrm1bFwVXUaV9HTnwxFrPtNso7dnwPQamhqSxtuhZ', // legacy usdc insurance fund + ]; + const getSpotMarketVaultPublicKey = marketIndex => getVaultPublicKey('spot_market_vault', marketIndex) + const getInsuranceFundVaultPublicKey = marketIndex => getVaultPublicKey('insurance_fund_vault', marketIndex) + let configFile = await getConfig('drift-config', 'https://raw.githubusercontent.com/drift-labs/protocol-v2/master/sdk/src/constants/spotMarkets.ts') + const marketIndices = []; + configFile = configFile.slice(configFile.indexOf('MainnetSpotMarkets:')) + + const regex = /marketIndex:\s*(\d+),/g + let match; + while ((match = regex.exec(configFile))) { + marketIndices.push(parseInt(match[1])); + } + + const vaults = [ + ...legacyVaults, + ...marketIndices.map(getSpotMarketVaultPublicKey), + ...marketIndices.map(getInsuranceFundVaultPublicKey), + ] + + return sumTokens2({ tokenAccounts: vaults }) } + +function getVaultPublicKey(seed, marketIndex) { + return PublicKey.findProgramAddressSync( + [ + Buffer.from(anchor.utils.bytes.utf8.encode(seed)), + new anchor.BN(marketIndex).toArrayLike(Buffer, 'le', 2), + ], DRIFT_PROGRAM_ID)[0].toBase58() +} +