From d255b0da17ae2384156362e951c7194649ddd1cd Mon Sep 17 00:00:00 2001 From: g1nt0ki <99907941+g1nt0ki@users.noreply.github.com> Date: Fri, 16 Aug 2024 17:26:52 +0200 Subject: [PATCH] Oraidex v3 (#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 --- projects/helper/chain/cosmos.js | 30 +++++++++++++++++++-------- projects/oraidex-v3/index.js | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 projects/oraidex-v3/index.js diff --git a/projects/helper/chain/cosmos.js b/projects/helper/chain/cosmos.js index fda290201eb4..535ce0d9c7a4 100644 --- a/projects/helper/chain/cosmos.js +++ b/projects/helper/chain/cosmos.js @@ -118,7 +118,13 @@ async function getBalance({ token, owner, block, chain } = {}) { return Number(data.balance); } -async function sumCW20Tokens({ balances = {}, tokens, owner, block, chain } = {}) { +async function sumCW20Tokens({ balances, tokens, owner, block, chain, api, } = {}) { + if (api) { + if (!chain) chain = api.chain; + if (!balances) balances = api.getBalances(); + } else { + if (!balances) balances = {}; + } await Promise.all( tokens.map(async (token) => { const balance = await getBalance({ token, owner, block, chain, }); @@ -139,7 +145,7 @@ async function getDenomBalance({ denom, owner, block, chain } = {}) { return balance ? Number(balance.amount) : 0; } -async function getBalance2({ balances = {}, owner, block, chain, tokens, blacklistedTokens, } = {}) { +async function getBalance2({ balances = {}, owner, block, chain, tokens, blacklistedTokens, api, } = {}) { const subpath = "cosmos"; let endpoint = `${getEndpoint( chain @@ -153,7 +159,9 @@ async function getBalance2({ balances = {}, owner, block, chain, tokens, blackli for (const { denom, amount } of data) { if (blacklistedTokens?.includes(denom)) continue; if (tokens && !tokens.includes(denom)) continue; - sdk.util.sumSingleBalance(balances, denom.replaceAll('/', ':'), amount); + if (api) api.add(denom, amount); + else + sdk.util.sumSingleBalance(balances, denom.replaceAll('/', ':'), amount); } return balances; } @@ -195,7 +203,7 @@ const multipleEndpoints = { "https://sei-m.api.n0ok.net", "https://sei-api.lavenderfive.com", "https://api-sei.stingray.plus" - ] + ], } async function queryContractWithRetries({ contract, chain, data }) { @@ -220,7 +228,7 @@ async function queryContractWithRetries({ contract, chain, data }) { } } -async function queryManyContracts({ contracts = [], chain, data, permitFailure = false}) { +async function queryManyContracts({ contracts = [], chain, data, permitFailure = false }) { const parallelLimit = 25 const { results, errors } = await PromisePool .withConcurrency(parallelLimit) @@ -280,7 +288,13 @@ async function queryContractStore({ return query(url, block, chain); } -async function sumTokens({ balances = {}, owners = [], chain, owner, tokens, blacklistedTokens, }) { +async function sumTokens({ balances, owners = [], chain, owner, tokens, blacklistedTokens, api, }) { + if (api) { + if (!chain) chain = api.chain; + if (!balances) balances = api.getBalances(); + } else { + if (!balances) balances = {}; + } if (!tokens?.length || (tokens?.length === 1 && tokens[0] === ADDRESSES.null)) tokens = undefined; if (owner) owners = [owner] log(chain, "fetching balances for ", owners.length); @@ -288,7 +302,7 @@ async function sumTokens({ balances = {}, owners = [], chain, owner, tokens, bla const { errors } = await PromisePool.withConcurrency(parallelLimit) .for(owners) - .process(async (owner) => getBalance2({ balances, owner, chain, tokens, blacklistedTokens, })); + .process(async (owner) => getBalance2({ balances, owner, chain, tokens, blacklistedTokens, api, })); if (errors && errors.length) throw errors[0]; return transformBalances(chain, balances); @@ -311,5 +325,5 @@ module.exports = { getTokenBalance, getToken, sumCW20Tokens, - queryContractWithRetries + queryContractWithRetries, }; diff --git a/projects/oraidex-v3/index.js b/projects/oraidex-v3/index.js new file mode 100644 index 000000000000..664b36bb8b23 --- /dev/null +++ b/projects/oraidex-v3/index.js @@ -0,0 +1,36 @@ +const { sumTokens, sumCW20Tokens, queryContract } = require('../helper/chain/cosmos') +const { getUniqueAddresses } = require('../helper/utils') + +const AMM_V3_CONTRACT = "orai10s0c75gw5y5eftms5ncfknw6lzmx0dyhedn75uz793m8zwz4g8zq4d9x9a" + +const isNativeToken = denom => !denom.startsWith("orai1") + +async function tvl(api) { + const CHUNK_SIZE = 100 + const pools = [] + let hasMore = true + + while (hasMore) { + const startAfter = pools.length == 0 ? undefined : pools[pools.length - 1].pool_key + const res = await queryContract({ + chain: api.chain, + contract: AMM_V3_CONTRACT, + data: { pools: { limit: CHUNK_SIZE, startAfter } } + }) + + pools.push(...res) + hasMore = res.length === CHUNK_SIZE + } + + let cw20Tokens = pools.map(pool => [pool.pool_key.token_x, pool.pool_key.token_y]).flat().filter(token => !isNativeToken(token)) + cw20Tokens = getUniqueAddresses(cw20Tokens, true) + + await sumTokens({ owner: AMM_V3_CONTRACT, api, }) + return sumCW20Tokens({ api, tokens: cw20Tokens, owner: AMM_V3_CONTRACT }) +} + +module.exports = { + timetravel: false, + methodology: "Liquidity on pool V3", + orai: { tvl } +} \ No newline at end of file