|
| 1 | +const { PublicKey } = require('@solana/web3.js'); |
| 2 | +const { Program } = require("@project-serum/anchor"); |
| 3 | +const { getConnection, getProvider, getTokenSupplies } = require('../helper/solana') |
| 4 | + |
| 5 | +async function tvl(api) { |
| 6 | + |
| 7 | + const connection = getConnection() |
| 8 | + const programId = 'CarrotwivhMpDnm27EHmRLeQ683Z1PufuqEmBZvD282s' |
| 9 | + const testVaultExclusion = '2AV35oWyAuSN5wmuy26VD5JirjVpXkfkv5ZMCQ2LtpuV' // our test vault should not be included in TVL |
| 10 | + |
| 11 | + // Use this method to track TVL via the token... |
| 12 | + // const CRT_MINT = 'CRTx1JouZhzSU6XytsE42UQraoGqiHgxabocVfARTy2s'; |
| 13 | + // await getTokenSupplies(CRT_MINT, {api}) |
| 14 | + |
| 15 | + // Use this method to track TVL via onchain state of each Vault by adding the balance at each strategy. |
| 16 | + const programAccounts = await connection.getProgramAccounts(new PublicKey(programId), { |
| 17 | + filters: [{ |
| 18 | + memcmp: { |
| 19 | + offset: 8, |
| 20 | + bytes: 'CarrotLYPhQzYL4fEsTUvEzw5QDaMGSZUENHSkh7qzQa' // carrot keeper |
| 21 | + }, |
| 22 | + },] |
| 23 | + }); |
| 24 | + |
| 25 | + const provider = getProvider(); |
| 26 | + const idl = await Program.fetchIdl(programId, provider) |
| 27 | + const program = new Program(idl, programId, provider) |
| 28 | + |
| 29 | + programAccounts.forEach(({ account, pubkey }, i) => { |
| 30 | + if(pubkey.toBase58() !== testVaultExclusion) { |
| 31 | + const { assets, strategies } = program.coder.accounts.decode( |
| 32 | + "Vault", |
| 33 | + account.data |
| 34 | + ); |
| 35 | + const assetMap = {} |
| 36 | + assets.forEach(({assetId, mint }) => assetMap[assetId] = mint.toString()) |
| 37 | + |
| 38 | + strategies.forEach(i => { |
| 39 | + api.add(assetMap[i.assetId], i.balance.toString()) |
| 40 | + }) |
| 41 | + } |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +module.exports = { |
| 46 | + doublecounted: true, |
| 47 | + timetravel: false, |
| 48 | + methodology: 'TVL calculated by calling the onchain state of the CRT Vault accounts, and tallying the balance of each strategy.', |
| 49 | + solana: { tvl }, |
| 50 | +} |
0 commit comments