Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add oraidex v3 to orai #11305

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
};
78 changes: 78 additions & 0 deletions projects/oraidex-v3/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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 CHUNK_SIZE = 100
const pools = []
// eslint-disable-next-line no-constant-condition
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
}
}

})
pools.push(...res);
if (res.length < CHUNK_SIZE) break;
}
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)
}
}

module.exports = {
timetravel: false,
misrepresentedTokens: true,
methodology: "Liquidity on pool V3",
orai: {tvl}
}
Loading