From 007159c0d5cd2c5efef5a77c53a18833b9a364cb Mon Sep 17 00:00:00 2001 From: 0xradishmz <165091631+0xradishmz@users.noreply.github.com> Date: Fri, 10 Jan 2025 06:33:25 +0700 Subject: [PATCH] feat: margin zero (#12971) Co-authored-by: g1nt0ki <99907941+g1nt0ki@users.noreply.github.com> --- projects/margin-zero/index.js | 47 +++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 projects/margin-zero/index.js diff --git a/projects/margin-zero/index.js b/projects/margin-zero/index.js new file mode 100644 index 000000000000..49de756f0849 --- /dev/null +++ b/projects/margin-zero/index.js @@ -0,0 +1,47 @@ +const { cachedGraphQuery } = require("../helper/cache"); +const { addUniV3LikePosition } = require('../helper/unwrapLPs'); + +const chainConfigs = +{ + sonic: { + subgraphUrl: "https://api.goldsky.com/api/public/project_cm58q8wq01kbk01ts09lc52kp/subgraphs/mz-subgraph/main/gn", + }, +} + +const LiquidityRangesQuery = `{ liquidityRanges(where: { liquidity_gt: "100" }) { pool hook liquidity tickLower tickUpper }}` + +const slot0Abi = + "function slot0() view returns (uint160 sqrtPriceX96, int24 tick, uint16 observationIndex, uint16 observationCardinality, uint16 observationCardinalityNext, uint8 feeProtocol, bool unlocked)"; + +async function tvl(api) { + const config = chainConfigs[api.chain] + + const liquidityRanges = await cachedGraphQuery('marign-zero/tvl', config.subgraphUrl, LiquidityRangesQuery, { + api, + fetchById: true, + useBlock: true, + }) + + let poolsDataMap = {} + const pools = Array.from(new Set(liquidityRanges.map(({ pool }) => pool.toLowerCase()))) + const poolIndexMap = {} + pools.forEach((pool, index) => poolIndexMap[pool] = index) + const token0s = await api.multiCall({ abi: 'address:token0', calls: pools }) + const token1s = await api.multiCall({ abi: 'address:token1', calls: pools }) + const slots = await api.multiCall({ abi: slot0Abi, calls: pools }) + + for (const { liquidity, tickLower, tickUpper, pool } of liquidityRanges) { + const idx = poolIndexMap[pool.toLowerCase()] + + addUniV3LikePosition({ api, token0: token0s[idx], token1: token1s[idx], tick: slots[idx].tick, liquidity, tickUpper, tickLower, }) + } + +} + +module.exports = { + methodology: "TVL is calculated by summing the value of all tokens in Margin Zero liquidity positions across supported chains", + doublecounted: true, + sonic: { + tvl, + } +};