Skip to content

Commit

Permalink
Refactor TVL calculation to filter pairs by locked value and improve …
Browse files Browse the repository at this point in the history
…error handling in validation
  • Loading branch information
g1nt0ki committed Jan 18, 2025
1 parent a3c3845 commit 24ebc07
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
29 changes: 18 additions & 11 deletions projects/maiar/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const { request } = require("graphql-request");
const { toUSDTBalances } = require('../helper/balances')

const LiquidityQuery = `
{
factory {
pairCount
totalValueLockedUSD
}
pairs(limit: 1000 minLockedValueUSD: 100) {
address
lockedValueUSD
liquidityPoolTokenPriceUSD
}
}
`

const StakingQuery2 = `{
Expand All @@ -16,15 +17,21 @@ const StakingQuery2 = `{
totalLockedMexStakedUSD
}`

async function tvl() {
const results = await request("http://graph.xexchange.com/graphql", LiquidityQuery)

return toUSDTBalances(results.factory.totalValueLockedUSD)
async function tvl(api) {
const { pairs } = await request("http://graph.xexchange.com/graphql", LiquidityQuery)
console.log(pairs.length)
pairs.forEach(i => {
if (i.lockedValueUSD > 1e8) {
api.log(`Pair ${i.address} has ${i.lockedValueUSD} USD locked, ignoring it`)
return;
}
api.addUSDValue(Math.round(+i.lockedValueUSD))
});
}

async function stakingAndLockedMEX() {
async function stakingAndLockedMEX(api) {
const results = await request("http://graph.xexchange.com/graphql", StakingQuery2)
return toUSDTBalances(results.totalValueStakedUSD)
api.addUSDValue(+results.totalValueStakedUSD)
}

module.exports = {
Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ function validateHallmarks(hallmark) {
storedKey = chain;
tvlFunctionIsFetch = true;
}
try {

await getTvl(
unixTimestamp,
ethBlock,
Expand All @@ -181,6 +183,10 @@ function validateHallmarks(hallmark) {
tvlFunctionIsFetch,
storedKey,
);
} catch (e) {
console.error(`Error in ${storedKey}:`, e)
process.exit(1)
}
let keyToAddChainBalances = tvlType;
if (tvlType === "tvl" || tvlType === "fetch") {
keyToAddChainBalances = "tvl";
Expand Down

0 comments on commit 24ebc07

Please sign in to comment.