Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
g1nt0ki committed Aug 2, 2024
1 parent 7409625 commit 9e83a15
Showing 1 changed file with 39 additions and 36 deletions.
75 changes: 39 additions & 36 deletions projects/meso-finance/index.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
const sdk = require("@defillama/sdk");
const { function_view } = require("../helper/chain/aptos");
const { transformBalances } = require("../helper/portedTokens");

async function getPoolAddresses() {
const pools = await function_view({ functionStr: "0x68476f9d437e3f32fd262ba898b5e3ee0a23a1d586a6cf29a28add35f253f6f7::meso::pools" })
return pools[0]['data'].map(obj => { return { coin: obj.key, poolAddress: obj.value.inner } });
}

async function getPoolInfo(pool) {
const poolInfo = await function_view({ functionStr: "0x68476f9d437e3f32fd262ba898b5e3ee0a23a1d586a6cf29a28add35f253f6f7::lending_pool::pool_info", type_arguments: [], args: [pool.poolAddress] })
return { coin: pool.coin, poolSupply: poolInfo[0].total_reserves, totalDebt: poolInfo[0].total_debt };
const { function_view } = require("../helper/chain/aptos")
const methodologies = require("../helper/methodologies")

let _poolData

function getPoolData() {
if (!_poolData) {
_poolData = new Promise(async (resolve, reject) => {

Check failure on line 8 in projects/meso-finance/index.js

View workflow job for this annotation

GitHub Actions / test

Promise executor functions should not be async
try {
const poolAddresses = await getPoolAddresses()
const poolInfos = await Promise.all(poolAddresses.map(getPoolInfo))
resolve(poolInfos)
} catch (e) { reject(e) }
})
}

return _poolData

async function getPoolAddresses() {
const pools = await function_view({ functionStr: "0x68476f9d437e3f32fd262ba898b5e3ee0a23a1d586a6cf29a28add35f253f6f7::meso::pools" })
return pools[0]['data'].map(obj => { return { coin: obj.key, poolAddress: obj.value.inner } })
}

async function getPoolInfo(pool) {
const poolInfo = await function_view({ functionStr: "0x68476f9d437e3f32fd262ba898b5e3ee0a23a1d586a6cf29a28add35f253f6f7::lending_pool::pool_info", type_arguments: [], args: [pool.poolAddress] })
return { coin: pool.coin, poolSupply: poolInfo[0].total_reserves, totalDebt: poolInfo[0].total_debt }
}
}

module.exports = {
timetravel: false,
methodology:
"Aggregates TVL for all markets in Meso.",
methodology: methodologies.lendingMarket,
aptos: {
tvl: async () => {
const balances = {};

const poolAddresses = await getPoolAddresses();
const poolInfos = await Promise.all(poolAddresses.map(poolAddress => getPoolInfo(poolAddress)));
poolInfos.forEach(({coin, poolSupply, totalDebt}) => {
sdk.util.sumSingleBalance(balances, coin, poolSupply);
});

return transformBalances("aptos", balances);
tvl: async (api) => {
const poolInfos = await getPoolData()
poolInfos.forEach(({ coin, poolSupply, }) => {
api.add(coin, poolSupply)
})
},

borrowed: async () => {
const balances = {};

const poolAddresses = await getPoolAddresses();
const poolInfos = await Promise.all(poolAddresses.map(poolAddress => getPoolInfo(poolAddress)));
poolInfos.forEach(({coin, poolSupply, totalDebt}) => {
sdk.util.sumSingleBalance(balances, coin, totalDebt);
});

return transformBalances("aptos", balances);
borrowed: async (api) => {
const poolInfos = await getPoolData()
poolInfos.forEach(({ coin, totalDebt }) => {
api.add(coin, totalDebt)
})
}
},
};

}

0 comments on commit 9e83a15

Please sign in to comment.