From 9e83a15889a3f9c3864482148a05dffc9936a661 Mon Sep 17 00:00:00 2001 From: g1nt0ki <99907941+g1nt0ki@users.noreply.github.com> Date: Fri, 2 Aug 2024 17:53:58 +0300 Subject: [PATCH] code refactor --- projects/meso-finance/index.js | 75 ++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/projects/meso-finance/index.js b/projects/meso-finance/index.js index 258a742d17d5..51bb73931797 100644 --- a/projects/meso-finance/index.js +++ b/projects/meso-finance/index.js @@ -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) => { + 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) + }) } }, -}; - +} \ No newline at end of file