Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: margin zero #12971

Merged
merged 8 commits into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions projects/margin-zero/index.js
Original file line number Diff line number Diff line change
@@ -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,
}
};
Loading