-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: g1nt0ki <99907941+g1nt0ki@users.noreply.github.com>
- Loading branch information
1 parent
31465ad
commit 007159c
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
}; |