|
| 1 | +const { cachedGraphQuery } = require("../helper/cache"); |
| 2 | +const { addUniV3LikePosition } = require('../helper/unwrapLPs'); |
| 3 | + |
| 4 | +const chainConfigs = |
| 5 | +{ |
| 6 | + sonic: { |
| 7 | + subgraphUrl: "https://api.goldsky.com/api/public/project_cm58q8wq01kbk01ts09lc52kp/subgraphs/mz-subgraph/main/gn", |
| 8 | + }, |
| 9 | +} |
| 10 | + |
| 11 | +const LiquidityRangesQuery = `{ liquidityRanges(where: { liquidity_gt: "100" }) { pool hook liquidity tickLower tickUpper }}` |
| 12 | + |
| 13 | +const slot0Abi = |
| 14 | + "function slot0() view returns (uint160 sqrtPriceX96, int24 tick, uint16 observationIndex, uint16 observationCardinality, uint16 observationCardinalityNext, uint8 feeProtocol, bool unlocked)"; |
| 15 | + |
| 16 | +async function tvl(api) { |
| 17 | + const config = chainConfigs[api.chain] |
| 18 | + |
| 19 | + const liquidityRanges = await cachedGraphQuery('marign-zero/tvl', config.subgraphUrl, LiquidityRangesQuery, { |
| 20 | + api, |
| 21 | + fetchById: true, |
| 22 | + useBlock: true, |
| 23 | + }) |
| 24 | + |
| 25 | + let poolsDataMap = {} |
| 26 | + const pools = Array.from(new Set(liquidityRanges.map(({ pool }) => pool.toLowerCase()))) |
| 27 | + const poolIndexMap = {} |
| 28 | + pools.forEach((pool, index) => poolIndexMap[pool] = index) |
| 29 | + const token0s = await api.multiCall({ abi: 'address:token0', calls: pools }) |
| 30 | + const token1s = await api.multiCall({ abi: 'address:token1', calls: pools }) |
| 31 | + const slots = await api.multiCall({ abi: slot0Abi, calls: pools }) |
| 32 | + |
| 33 | + for (const { liquidity, tickLower, tickUpper, pool } of liquidityRanges) { |
| 34 | + const idx = poolIndexMap[pool.toLowerCase()] |
| 35 | + |
| 36 | + addUniV3LikePosition({ api, token0: token0s[idx], token1: token1s[idx], tick: slots[idx].tick, liquidity, tickUpper, tickLower, }) |
| 37 | + } |
| 38 | + |
| 39 | +} |
| 40 | + |
| 41 | +module.exports = { |
| 42 | + methodology: "TVL is calculated by summing the value of all tokens in Margin Zero liquidity positions across supported chains", |
| 43 | + doublecounted: true, |
| 44 | + sonic: { |
| 45 | + tvl, |
| 46 | + } |
| 47 | +}; |
0 commit comments