Skip to content

Commit

Permalink
revert drift.js
Browse files Browse the repository at this point in the history
  • Loading branch information
g1nt0ki committed Aug 14, 2024
1 parent ae1995b commit f835448
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions projects/drift.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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()
}

0 comments on commit f835448

Please sign in to comment.