Skip to content

Commit d255b0d

Browse files
g1nt0kitrungbach
andauthored
Oraidex v3 (DefiLlama#11331)
* orai: add amm-v3 info * orai: calculate tvl dex v3 from on-chain data instead of from indexer * orai: disabled no-constant-condition for while true * code refactor --------- Co-authored-by: trungbach <[email protected]>
1 parent 8303bb2 commit d255b0d

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

projects/helper/chain/cosmos.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,13 @@ async function getBalance({ token, owner, block, chain } = {}) {
118118
return Number(data.balance);
119119
}
120120

121-
async function sumCW20Tokens({ balances = {}, tokens, owner, block, chain } = {}) {
121+
async function sumCW20Tokens({ balances, tokens, owner, block, chain, api, } = {}) {
122+
if (api) {
123+
if (!chain) chain = api.chain;
124+
if (!balances) balances = api.getBalances();
125+
} else {
126+
if (!balances) balances = {};
127+
}
122128
await Promise.all(
123129
tokens.map(async (token) => {
124130
const balance = await getBalance({ token, owner, block, chain, });
@@ -139,7 +145,7 @@ async function getDenomBalance({ denom, owner, block, chain } = {}) {
139145
return balance ? Number(balance.amount) : 0;
140146
}
141147

142-
async function getBalance2({ balances = {}, owner, block, chain, tokens, blacklistedTokens, } = {}) {
148+
async function getBalance2({ balances = {}, owner, block, chain, tokens, blacklistedTokens, api, } = {}) {
143149
const subpath = "cosmos";
144150
let endpoint = `${getEndpoint(
145151
chain
@@ -153,7 +159,9 @@ async function getBalance2({ balances = {}, owner, block, chain, tokens, blackli
153159
for (const { denom, amount } of data) {
154160
if (blacklistedTokens?.includes(denom)) continue;
155161
if (tokens && !tokens.includes(denom)) continue;
156-
sdk.util.sumSingleBalance(balances, denom.replaceAll('/', ':'), amount);
162+
if (api) api.add(denom, amount);
163+
else
164+
sdk.util.sumSingleBalance(balances, denom.replaceAll('/', ':'), amount);
157165
}
158166
return balances;
159167
}
@@ -195,7 +203,7 @@ const multipleEndpoints = {
195203
"https://sei-m.api.n0ok.net",
196204
"https://sei-api.lavenderfive.com",
197205
"https://api-sei.stingray.plus"
198-
]
206+
],
199207
}
200208

201209
async function queryContractWithRetries({ contract, chain, data }) {
@@ -220,7 +228,7 @@ async function queryContractWithRetries({ contract, chain, data }) {
220228
}
221229
}
222230

223-
async function queryManyContracts({ contracts = [], chain, data, permitFailure = false}) {
231+
async function queryManyContracts({ contracts = [], chain, data, permitFailure = false }) {
224232
const parallelLimit = 25
225233
const { results, errors } = await PromisePool
226234
.withConcurrency(parallelLimit)
@@ -280,15 +288,21 @@ async function queryContractStore({
280288
return query(url, block, chain);
281289
}
282290

283-
async function sumTokens({ balances = {}, owners = [], chain, owner, tokens, blacklistedTokens, }) {
291+
async function sumTokens({ balances, owners = [], chain, owner, tokens, blacklistedTokens, api, }) {
292+
if (api) {
293+
if (!chain) chain = api.chain;
294+
if (!balances) balances = api.getBalances();
295+
} else {
296+
if (!balances) balances = {};
297+
}
284298
if (!tokens?.length || (tokens?.length === 1 && tokens[0] === ADDRESSES.null)) tokens = undefined;
285299
if (owner) owners = [owner]
286300
log(chain, "fetching balances for ", owners.length);
287301
let parallelLimit = 25;
288302

289303
const { errors } = await PromisePool.withConcurrency(parallelLimit)
290304
.for(owners)
291-
.process(async (owner) => getBalance2({ balances, owner, chain, tokens, blacklistedTokens, }));
305+
.process(async (owner) => getBalance2({ balances, owner, chain, tokens, blacklistedTokens, api, }));
292306

293307
if (errors && errors.length) throw errors[0];
294308
return transformBalances(chain, balances);
@@ -311,5 +325,5 @@ module.exports = {
311325
getTokenBalance,
312326
getToken,
313327
sumCW20Tokens,
314-
queryContractWithRetries
328+
queryContractWithRetries,
315329
};

projects/oraidex-v3/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const { sumTokens, sumCW20Tokens, queryContract } = require('../helper/chain/cosmos')
2+
const { getUniqueAddresses } = require('../helper/utils')
3+
4+
const AMM_V3_CONTRACT = "orai10s0c75gw5y5eftms5ncfknw6lzmx0dyhedn75uz793m8zwz4g8zq4d9x9a"
5+
6+
const isNativeToken = denom => !denom.startsWith("orai1")
7+
8+
async function tvl(api) {
9+
const CHUNK_SIZE = 100
10+
const pools = []
11+
let hasMore = true
12+
13+
while (hasMore) {
14+
const startAfter = pools.length == 0 ? undefined : pools[pools.length - 1].pool_key
15+
const res = await queryContract({
16+
chain: api.chain,
17+
contract: AMM_V3_CONTRACT,
18+
data: { pools: { limit: CHUNK_SIZE, startAfter } }
19+
})
20+
21+
pools.push(...res)
22+
hasMore = res.length === CHUNK_SIZE
23+
}
24+
25+
let cw20Tokens = pools.map(pool => [pool.pool_key.token_x, pool.pool_key.token_y]).flat().filter(token => !isNativeToken(token))
26+
cw20Tokens = getUniqueAddresses(cw20Tokens, true)
27+
28+
await sumTokens({ owner: AMM_V3_CONTRACT, api, })
29+
return sumCW20Tokens({ api, tokens: cw20Tokens, owner: AMM_V3_CONTRACT })
30+
}
31+
32+
module.exports = {
33+
timetravel: false,
34+
methodology: "Liquidity on pool V3",
35+
orai: { tvl }
36+
}

0 commit comments

Comments
 (0)