|
| 1 | +const { sumTokens2 } = require('../helper/unwrapLPs'); |
| 2 | + |
| 3 | +// --- V3 (Dynamic NFPM Traversal Method) --- |
| 4 | + |
| 5 | +// Contract Addresses |
| 6 | +const v3Factory = "0x70355463122C113b0931146F6C9488D9b0a09037" |
| 7 | +const v3NFPM = "0x6d2E4e1eF48fa1457c4eaa6a603d7DDB3974C1B3" // NonfungiblePositionManager |
| 8 | + |
| 9 | +// ABIs required for traversal |
| 10 | +const abi = { |
| 11 | + NFPM: { |
| 12 | + totalSupply: "uint256:totalSupply", |
| 13 | + tokenByIndex: "function tokenByIndex(uint256 index) view returns (uint256)", |
| 14 | + // positions (tokenId) returns: (nonce, operator, token0, token1, fee, ...) |
| 15 | + positions: "function positions(uint256 tokenId) view returns (uint96, address, address, address, uint24, int24, int24, uint128, uint256, uint256, uint128, uint128)" |
| 16 | + }, |
| 17 | + Factory: { |
| 18 | + getPool: "function getPool(address tokenA, address tokenB, uint24 fee) view returns (address pool)" |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Dynamic V3 TVL function. |
| 24 | + */ |
| 25 | +async function getV3Tvl(api) { |
| 26 | + // 1. Get the total number of LP NFTs |
| 27 | + const nftTotalSupply = await api.call({ |
| 28 | + target: v3NFPM, |
| 29 | + abi: abi.NFPM.totalSupply, |
| 30 | + }); |
| 31 | + |
| 32 | + // 2. Build multicall to get all NFT token IDs |
| 33 | + const nftTokenIds = await api.multiCall({ |
| 34 | + target: v3NFPM, |
| 35 | + abi: abi.NFPM.tokenByIndex, |
| 36 | + calls: Array.from({ length: Number(nftTotalSupply) }, (_, i) => i), |
| 37 | + }); |
| 38 | + |
| 39 | + // 3. Build multicall to get position info (token0, token1, fee) for all NFTs |
| 40 | + const positionData = await api.multiCall({ |
| 41 | + target: v3NFPM, |
| 42 | + abi: abi.NFPM.positions, |
| 43 | + calls: nftTokenIds, |
| 44 | + }); |
| 45 | + |
| 46 | + // 4. De-duplicate to find all unique pool "recipes" (token0, token1, fee) |
| 47 | + const uniquePools = new Map(); |
| 48 | + positionData.forEach(pos => { |
| 49 | + // pos[2] is token0, pos[3] is token1, pos[4] is fee |
| 50 | + const key = `${pos[2]}-${pos[3]}-${pos[4]}`; |
| 51 | + if (!uniquePools.has(key)) { |
| 52 | + uniquePools.set(key, { |
| 53 | + token0: pos[2], |
| 54 | + token1: pos[3], |
| 55 | + fee: pos[4] |
| 56 | + }); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + // 5. Build multicall to get the actual pool addresses from the factory |
| 61 | + const poolAddresses = await api.multiCall({ |
| 62 | + target: v3Factory, |
| 63 | + abi: abi.Factory.getPool, |
| 64 | + calls: Array.from(uniquePools.values()).map(p => ({ |
| 65 | + params: [p.token0, p.token1, p.fee] |
| 66 | + })) |
| 67 | + }); |
| 68 | + |
| 69 | + // 6. Format the data for sumTokens2: [ [token0, token1], poolAddress ] |
| 70 | + const ownerTokens = []; |
| 71 | + const pools = Array.from(uniquePools.values()); |
| 72 | + poolAddresses.forEach((poolAddress, i) => { |
| 73 | + ownerTokens.push([ |
| 74 | + [pools[i].token0, pools[i].token1], poolAddress, |
| 75 | + ]); |
| 76 | + }); |
| 77 | + |
| 78 | + // 7. Calculate TVL and convert values to float |
| 79 | + const balances = await sumTokens2({ api, ownerTokens, balances: {} }); |
| 80 | + const floatBalances = Object.fromEntries( |
| 81 | + Object.entries(balances).map(([key, value]) => [key, parseFloat(value)]) |
| 82 | + ); |
| 83 | + return floatBalances; |
| 84 | +} |
| 85 | + |
| 86 | +module.exports = { |
| 87 | + v3Tvl: getV3Tvl, |
| 88 | +}; |
0 commit comments