Skip to content

Commit

Permalink
Oraidex v3 (#11331)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
g1nt0ki and trungbach authored Aug 16, 2024
1 parent 8303bb2 commit d255b0d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
30 changes: 22 additions & 8 deletions projects/helper/chain/cosmos.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, });
Expand All @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 }) {
Expand All @@ -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)
Expand Down Expand Up @@ -280,15 +288,21 @@ 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);
let parallelLimit = 25;

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);
Expand All @@ -311,5 +325,5 @@ module.exports = {
getTokenBalance,
getToken,
sumCW20Tokens,
queryContractWithRetries
queryContractWithRetries,
};
36 changes: 36 additions & 0 deletions projects/oraidex-v3/index.js
Original file line number Diff line number Diff line change
@@ -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 }
}

0 comments on commit d255b0d

Please sign in to comment.