Skip to content

Commit 1886932

Browse files
committed
feat: add missing chains for stryke-clamm tvl data
Signed-off-by: 0xhitgo <[email protected]>
1 parent 5aedd17 commit 1886932

File tree

1 file changed

+85
-46
lines changed

1 file changed

+85
-46
lines changed

projects/dopex-clamm/index.js

Lines changed: 85 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
const { gql, request } = require('graphql-request');
1+
const { gql, request } = require("graphql-request");
2+
3+
const SUBGRAPH_ENDPOINTS = {
4+
arbitrum:
5+
"https://api.0xgraph.xyz/api/public/e2146f32-5728-4755-b1d1-84d17708c119/subgraphs/clamm-arbitrum/prod/gn",
6+
sonic:
7+
"https://api.0xgraph.xyz/api/public/e2146f32-5728-4755-b1d1-84d17708c119/subgraphs/clamm-sonic/prod/gn",
8+
base: "https://api.0xgraph.xyz/api/public/e2146f32-5728-4755-b1d1-84d17708c119/subgraphs/clamm-base/prod/gn",
9+
blast:
10+
"https://api.0xgraph.xyz/api/public/e2146f32-5728-4755-b1d1-84d17708c119/subgraphs/clamm-blast/prod/gn",
11+
mantle:
12+
"https://api.0xgraph.xyz/api/public/e2146f32-5728-4755-b1d1-84d17708c119/subgraphs/clamm-mantle/prod/gn",
13+
};
214

3-
const endpoint = 'https://api.0xgraph.xyz/subgraphs/name/dopex-v2-clamm-public';
4-
const abi = "function slot0() view returns (uint160 sqrtPriceX96, int24 tick, uint16 observationIndex, uint16 observationCardinality, uint16 observationCardinalityNext, uint8 feeProtocol, bool unlocked)"
15+
const abi =
16+
"function slot0() view returns (uint160 sqrtPriceX96, int24 tick, uint16 observationIndex, uint16 observationCardinality, uint16 observationCardinalityNext, uint8 feeProtocol, bool unlocked)";
517

618
const query = gql`
719
query strikes($limit: Int!, $skip: Int!) {
@@ -13,77 +25,104 @@ const query = gql`
1325
orderDirection: desc
1426
) {
1527
pool
16-
token0 {id}
17-
token1 {id}
28+
token0 {
29+
id
30+
}
31+
token1 {
32+
id
33+
}
1834
tickLower
1935
tickUpper
2036
totalLiquidity
2137
}
2238
}
2339
`;
2440

25-
async function fetchStrikes(limit, skip, allData = []) {
41+
async function fetchStrikes(endpoint, limit, skip, allData = []) {
2642
const variables = { limit, skip };
2743
const { strikes } = await request(endpoint, query, variables);
2844
allData.push(...strikes);
2945

3046
if (strikes.length === limit) {
31-
return fetchStrikes(limit, skip + limit, allData);
47+
return fetchStrikes(endpoint, limit, skip + limit, allData);
3248
}
3349

3450
return allData;
3551
}
3652

3753
function addV3PositionBalances(strike, sqrtPricesMap) {
38-
const tickToPrice = (tick) => 1.0001 ** tick
39-
const token0 = strike.token0.id
40-
const token1 = strike.token1.id
41-
const liquidity = strike.totalLiquidity
42-
const bottomTick = +strike.tickLower
43-
const topTick = +strike.tickUpper
44-
const tick = +sqrtPricesMap[strike.pool.toLowerCase()].tick
45-
const sa = tickToPrice(bottomTick / 2)
46-
const sb = tickToPrice(topTick / 2)
47-
48-
let amount0 = 0
49-
let amount1 = 0
54+
const tickToPrice = (tick) => 1.0001 ** tick;
55+
const token0 = strike.token0.id;
56+
const token1 = strike.token1.id;
57+
const liquidity = strike.totalLiquidity;
58+
const bottomTick = +strike.tickLower;
59+
const topTick = +strike.tickUpper;
60+
const tick = +sqrtPricesMap[strike.pool.toLowerCase()].tick;
61+
const sa = tickToPrice(bottomTick / 2);
62+
const sb = tickToPrice(topTick / 2);
63+
64+
let amount0 = 0;
65+
let amount1 = 0;
5066

5167
if (tick < bottomTick) {
52-
amount0 = liquidity * (sb - sa) / (sa * sb)
68+
amount0 = (liquidity * (sb - sa)) / (sa * sb);
5369
} else if (tick < topTick) {
54-
const price = tickToPrice(tick)
55-
const sp = price ** 0.5
70+
const price = tickToPrice(tick);
71+
const sp = price ** 0.5;
5672

57-
amount0 = liquidity * (sb - sp) / (sp * sb)
58-
amount1 = liquidity * (sp - sa)
73+
amount0 = (liquidity * (sb - sp)) / (sp * sb);
74+
amount1 = liquidity * (sp - sa);
5975
} else {
60-
amount1 = liquidity * (sb - sa)
76+
amount1 = liquidity * (sb - sa);
6177
}
6278

63-
return { token0, amount0, token1, amount1 }
79+
return { token0, amount0, token1, amount1 };
6480
}
6581

66-
async function tvl(api) {
67-
const limit = 1000;
68-
const allData = await fetchStrikes(limit, 0);
69-
70-
let pools = allData.map(strike => strike.pool.toLowerCase())
71-
pools = [...new Set(pools)]
72-
const sqrtPrices = await api.multiCall({ calls: pools, abi })
73-
const sqrtPricesMap = sqrtPrices.reduce((acc, item, i) => {
74-
return { ...acc, [pools[i]]: item, }
75-
}, {});
76-
77-
allData.forEach((strike) => {
78-
const { token0, amount0, token1, amount1 } = addV3PositionBalances(strike, sqrtPricesMap);
79-
api.add(token0, amount0);
80-
api.add(token1, amount1);
81-
});
82+
function tvlByChain(chain) {
83+
return async function (api) {
84+
const endpoint = SUBGRAPH_ENDPOINTS[chain];
85+
if (!endpoint)
86+
throw new Error(`No subgraph endpoint configured for chain: ${chain}`);
87+
88+
const limit = 1000;
89+
const allData = await fetchStrikes(endpoint, limit, 0);
90+
91+
let pools = allData.map((strike) => strike.pool.toLowerCase());
92+
pools = [...new Set(pools)];
93+
const sqrtPrices = await api.multiCall({ calls: pools, abi });
94+
const sqrtPricesMap = sqrtPrices.reduce((acc, item, i) => {
95+
return { ...acc, [pools[i]]: item };
96+
}, {});
97+
98+
allData.forEach((strike) => {
99+
const { token0, amount0, token1, amount1 } = addV3PositionBalances(
100+
strike,
101+
sqrtPricesMap
102+
);
103+
api.add(token0, amount0);
104+
api.add(token1, amount1);
105+
});
106+
};
82107
}
83108

84109
module.exports = {
85-
doublecounted: true, // tokens are stored in UNI-V3 pools
86-
arbitrum: { tvl, },
110+
doublecounted: true, // tokens are stored in UNI-V3 pools
111+
arbitrum: {
112+
tvl: tvlByChain("arbitrum"),
113+
},
114+
sonic: {
115+
tvl: tvlByChain("sonic"),
116+
},
117+
base: {
118+
tvl: tvlByChain("base"),
119+
},
120+
blast: {
121+
tvl: tvlByChain("blast"),
122+
},
123+
mantle: {
124+
tvl: tvlByChain("mantle"),
125+
},
126+
methodology:
127+
"TVL is calculated by summing the value of all tokens in Stryke liquidity positions across supported chains",
87128
};
88-
89-

0 commit comments

Comments
 (0)