From 9f04e538c17a1e61bfd8071699d67f2f5afab2da Mon Sep 17 00:00:00 2001 From: trungbach Date: Fri, 16 Aug 2024 14:04:50 +0700 Subject: [PATCH] orai: calculate tvl dex v3 from on-chain data instead of from indexer --- projects/helper/chain/cosmos.js | 24 ++++++++++- projects/oraidex-v3/index.js | 74 +++++++++++++++++++++++++-------- 2 files changed, 80 insertions(+), 18 deletions(-) diff --git a/projects/helper/chain/cosmos.js b/projects/helper/chain/cosmos.js index fda290201eb4..fa6e917c0565 100644 --- a/projects/helper/chain/cosmos.js +++ b/projects/helper/chain/cosmos.js @@ -195,9 +195,30 @@ const multipleEndpoints = { "https://sei-m.api.n0ok.net", "https://sei-api.lavenderfive.com", "https://api-sei.stingray.plus" + ], + oraichain: [ + "https://lcd.orai.io" ] } +async function queryBankWithRetries({address, chain, denom}) { + const rpcs = multipleEndpoints[chain] + for (let i = 0; i < rpcs.length; i++) { + try { + const url = `${rpcs[i]}/cosmos/bank/v1beta1/balances/${address}/by_denom?denom=${denom}` + return ( + await get( + `${rpcs[i]}/cosmos/bank/v1beta1/balances/${address}/by_denom?denom=${denom}` + ) + ).balance; + } catch (e) { + if (i >= rpcs.length - 1) { + throw e + } + } + } +} + async function queryContractWithRetries({ contract, chain, data }) { const rpcs = multipleEndpoints[chain] if (rpcs === undefined) { @@ -311,5 +332,6 @@ module.exports = { getTokenBalance, getToken, sumCW20Tokens, - queryContractWithRetries + queryContractWithRetries, + queryBankWithRetries }; diff --git a/projects/oraidex-v3/index.js b/projects/oraidex-v3/index.js index 5ec75dcce15e..e5840f567614 100644 --- a/projects/oraidex-v3/index.js +++ b/projects/oraidex-v3/index.js @@ -1,28 +1,68 @@ -const graphql = require('../helper/utils/graphql'); -const ADDRESSES = require('../helper/coreAssets.json') +const {queryContractWithRetries, queryBankWithRetries} = require('../helper/chain/cosmos'); + +const AMM_V3_CONTRACT = "orai10s0c75gw5y5eftms5ncfknw6lzmx0dyhedn75uz793m8zwz4g8zq4d9x9a"; +const isNativeToken = (denom) => { + if (denom.startsWith("orai1")) { + return false; + } + return true; +}; + +async function oraichainQueryData({contract, data}) { + return await queryContractWithRetries({contract, chain: 'oraichain', data}); +} + +function getTokenFormat(tokenAddr) { + if (tokenAddr.includes("ibc")) return tokenAddr.split("/").join(":") + else if (tokenAddr.includes("factory")) return "orai:" + tokenAddr.split("/").join(":") + return "orai:" + tokenAddr +} async function tvl() { try { - const endpoint = 'https://staging-ammv3-indexer.oraidex.io' - const query = `query poolQuery { - pools { - nodes { - id - totalValueLockedInUSD + const CHUNK_SIZE = 100 + const pools = [] + while (true) { + const res = await oraichainQueryData({ + contract: AMM_V3_CONTRACT, + data: { + pools: { + limit: CHUNK_SIZE, + startAfter: pools.length == 0 ? undefined : pools[pools.length - 1].pool_key } } - }` - const res = await graphql.request(endpoint, query) - const token = 'orai:' + ADDRESSES.orai.USDT - const sum = { - [token]: 0 + }) + pools.push(...res); + if (res.length < CHUNK_SIZE) break; } - const COSMOS_DECIMALS = 6; - const decimals = 10 ** COSMOS_DECIMALS; - res.pools.nodes.forEach(pool => { - sum[token] += pool.totalValueLockedInUSD * decimals + const poolTokens = pools.map(pool => [pool.pool_key.token_x, pool.pool_key.token_y]).flat() + const uniqueTokens = new Set(poolTokens); + + const sum = {} + const balancePromises = Array.from(uniqueTokens).map(async (token) => { + const key = getTokenFormat(token) + let tokenBalances = {} + if (isNativeToken(token)) { + tokenBalances = await queryBankWithRetries({ + address: AMM_V3_CONTRACT, + chain: 'oraichain', + denom: token + }) + sum[key] = tokenBalances.amount + } else { + tokenBalances = await oraichainQueryData({ + contract: token, + data: { + balance: { + address: AMM_V3_CONTRACT, + } + } + }) + sum[key] = tokenBalances.balance + } }) + await Promise.all(balancePromises) return sum } catch (error) { console.error("Error when get tvl oraidex v3: ", error)