Skip to content

Commit

Permalink
orai: calculate tvl dex v3 from on-chain data instead of from indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
trungbach committed Aug 16, 2024
1 parent 996dd8a commit 9f04e53
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 18 deletions.
24 changes: 23 additions & 1 deletion projects/helper/chain/cosmos.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -311,5 +332,6 @@ module.exports = {
getTokenBalance,
getToken,
sumCW20Tokens,
queryContractWithRetries
queryContractWithRetries,
queryBankWithRetries
};
74 changes: 57 additions & 17 deletions projects/oraidex-v3/index.js
Original file line number Diff line number Diff line change
@@ -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) {

Check failure on line 25 in projects/oraidex-v3/index.js

View workflow job for this annotation

GitHub Actions / test

Unexpected constant condition
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)
Expand Down

0 comments on commit 9f04e53

Please sign in to comment.