-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
39 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { | ||
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) | ||
}) | ||
} | ||
}, | ||
}; | ||
|
||
} |