Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mole: Remove the dependency for decimal.js and remove Aptos #11098

Closed
wants to merge 9 commits into from
6 changes: 4 additions & 2 deletions projects/mole/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ async function aptosTvl() {
return {...lyfTvl};
}

async function suiTvl(api) {
return calLyfTvlSui(api)
async function suiTvl() {
const { api } = arguments[3]

const lyfTvl = await calLyfTvlSui(api)
}

// run command: node test.js projects/mole/index.js
Expand Down
124 changes: 57 additions & 67 deletions projects/mole/lyf.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const sdk = require("@defillama/sdk");
const abi = require("./abi.json");
const BN = require("bn.js");
const BigNumber = require("bignumber.js");
const { coreTokens } = require("../helper/chain/aptos");
const { getResources } = require("../helper/chain/aptos");
const { getConfig } = require('../helper/cache')
const { unwrapUniswapLPs } = require("../helper/unwrapLPs");
const sui = require('../helper/chain/sui')
const { transformBalances } = require("../helper/portedTokens");
const { i32BitsToNumber } = require("./utils")
const { getObject } = require("../helper/chain/sui");
const { i32BitsToNumber, getCoinAmountFromLiquidity, tickIndexToSqrtPriceX64 } = require("./utils")

async function getProcolAddresses(chain) {
// if (chain === 'avax') {
Expand All @@ -17,16 +19,17 @@ async function getProcolAddresses(chain) {
// )
// );
// }

if (chain === 'aptos') {
if(chain === 'aptos') {
return (
await getConfig('mole/' + chain,
await getConfig('mole/'+chain,
"https://raw.githubusercontent.com/Mole-Fi/mole-protocol/main/.aptos_mainnet.json"
)
);
} else if (chain === 'sui') {
}else if(chain === 'sui') {
return (
await getConfig('mole/' + chain,
// modify the hosts for raw.githubusercontent.com ip if it cannot be retrieved.
await getConfig('mole/'+chain,
"https://raw.githubusercontent.com/Mole-Fi/mole-protocol/main/.sui_mainnet.json"
)
);
Expand All @@ -37,7 +40,7 @@ async function getProcolAddresses(chain) {
async function calLyfTvl(chain, block) {
/// @dev Initialized variables
const balances = {};
const transform = addr => 'avax:' + addr
const transform = addr => 'avax:'+addr

/// @dev Getting all addresses from Github
const addresses = await getProcolAddresses(chain);
Expand Down Expand Up @@ -125,18 +128,18 @@ async function calLyfTvlAptos() {

/// @dev unwrap LP to get underlaying token balances for workers that are working with LPs
await unwrapPancakeSwapLps({
balances,
lps,
account: '0xc7efb4076dbe143cbcd98cfaaa929ecfc8f299203dfff63b95ccb6bfe19850fa',
poolStr: 'swap::TokenPairReserve',
token0Reserve: i => i.data.reserve_x,
token1Reserve: i => i.data.reserve_y
balances,
lps,
account: '0xc7efb4076dbe143cbcd98cfaaa929ecfc8f299203dfff63b95ccb6bfe19850fa',
poolStr: 'swap::TokenPairReserve',
token0Reserve: i => i.data.reserve_x,
token1Reserve: i => i.data.reserve_y
})

/// @dev getting all unused liquidity on each vault
resources.filter(i => i.type.includes("vault::VaultInfo"))
.map(i => {
const token = i.type.split('<')[1].replace('>', '');
const token = i.type.split('<')[1].replace('>','');
sdk.util.sumSingleBalance(balances, token, new BigNumber(i.data.coin.value).minus(i.data.reserve_pool).toFixed(0))
})

Expand All @@ -149,7 +152,7 @@ function sumPancakeWorkerStakingLps(resources, lps, workers) {
i.type.includes("pancake_worker::WorkerInfo") ||
i.type.includes("delta_neutral_pancake_asset_worker::WorkerInfo") ||
i.type.includes("delta_neutral_pancake_stable_worker::WorkerInfo")
) && workers[i.type.split('::', 1)[0]]
) && workers[i.type.split('::',1)[0]]
)

workerInfos.forEach(i => {
Expand All @@ -158,7 +161,7 @@ function sumPancakeWorkerStakingLps(resources, lps, workers) {
const lp = lpWithSuffix.substr(0, lpWithSuffix.length - 1);
const amount = new BigNumber(i.data.total_balance ?? i.data.total_lp_balance)

if (lps[lp] === undefined) {
if(lps[lp] === undefined) {
lps[lp] = { amount: amount }
} else {
lps[lp].amount = lps[lp].amount.plus(amount);
Expand All @@ -174,21 +177,21 @@ async function unwrapPancakeSwapLps({
token0Reserve = i => i.data.coin_x_reserve.value,
token1Reserve = i => i.data.coin_y_reserve.value,
getTokens = i => i.type.split('<')[1].replace('>', '').split(', ')
}) {
}){
const coinInfos = {}
const lpReserves = {}
for (const lpType in lps) {
if (lps.hasOwnProperty(lpType)) {
if(lps.hasOwnProperty(lpType)){
coinInfos[`0x1::coin::CoinInfo<${lpType}>`] = lpType;
const tokens = getTokens({ type: lpType })
const tokens = getTokens({type: lpType})
lpReserves[`${account}::${poolStr}<${tokens[0]}, ${tokens[1]}>`] = lpType;
}
}

let pools = await getResources(account);
let lpInfos = pools;
pools = pools.filter((i) => {
if (!i.type.includes(poolStr)) {
if(!i.type.includes(poolStr)){
return false
}

Expand All @@ -197,7 +200,7 @@ async function unwrapPancakeSwapLps({
});
lpInfos.forEach(i => {
const lpType = coinInfos[i.type];
if (lpType) {
if(lpType){
lps[lpType].totalSupply = new BigNumber(i.data.supply.vec[0].integer.vec[0].value)
}
});
Expand Down Expand Up @@ -238,71 +241,57 @@ async function calLyfTvlSui(api) {
const workerInfos = await sui.getObjects(workerInfoIds)

let poolIds = []
workerInfos.forEach(workerInfo => {
let poolId = workerInfo.fields.position_nft.fields.pool
// poolId = poolId.replace('0x0', '0x')
if (!poolIds.includes(poolId)) {
poolIds.push(poolId)
workerInfos.forEach(workerInfo =>
{
let poolId = workerInfo.fields.position_nft.fields.pool
// poolId = poolId.replace('0x0', '0x')
if (!poolIds.includes(poolId)) {
poolIds.push(poolId)
}
}
}
)

const poolInfos = await sui.getObjects(poolIds)
const poolInfos = await sui.getObjects(poolIds)
let poolMap = new Map()
poolInfos.forEach(poolInfo => {
// const poolId = poolInfo.fields.id.id.replace('0x0', '0x')
poolMap.set(poolInfo.fields.id.id, poolInfo)
}
poolInfos.forEach(poolInfo =>
{
// const poolId = poolInfo.fields.id.id.replace('0x0', '0x')
poolMap.set(poolInfo.fields.id.id, poolInfo)
}
)

workerInfos.forEach(workerInfo => {
let poolId = workerInfo.fields.position_nft.fields.pool

let coinAamount = 0
let coinBamount = 0
computeCLMMPositionBalances()

const [coinA, coinB] = poolMap.get(poolId).type.replace('>', '').split('<')[1].split(', ')
api.add(coinA, coinAamount)
api.add(coinB, coinBamount)

// code copied from uni v3 NFT resolver
function computeCLMMPositionBalances() {
const tickToPrice = (tick) => 1.0001 ** tick

workerInfos.forEach(workerInfo =>
{
const liquidity = workerInfo.fields.position_nft.fields.liquidity
const tickLowerIndex = i32BitsToNumber(workerInfo.fields.position_nft.fields.tick_lower_index.fields.bits)
const tickUpperIndex = i32BitsToNumber(workerInfo.fields.position_nft.fields.tick_upper_index.fields.bits)
const bottomTick = tickLowerIndex
const topTick = tickUpperIndex
const tick = i32BitsToNumber(poolMap.get(poolId).fields.current_tick_index.fields.bits)
const sa = tickToPrice(bottomTick / 2)
const sb = tickToPrice(topTick / 2)

if (tick < bottomTick) {
coinAamount = liquidity * (sb - sa) / (sa * sb)
} else if (tick < topTick) {
const price = tickToPrice(tick)
const sp = price ** 0.5

coinAamount = liquidity * (sb - sp) / (sp * sb)
coinBamount = liquidity * (sp - sa)
} else {
coinBamount = liquidity * (sb - sa)
}
let poolId = workerInfo.fields.position_nft.fields.pool
// poolId = poolId.replace('0x0', '0x')
const currentSqrtPrice = poolMap.get(poolId).fields.current_sqrt_price

const coinAmounts = getCoinAmountFromLiquidity(new BN(liquidity), new BN(currentSqrtPrice), tickIndexToSqrtPriceX64(tickLowerIndex),
tickIndexToSqrtPriceX64(tickUpperIndex))

let coinAamount = coinAmounts.coinA
let coinBamount = coinAmounts.coinB

const [coinA, coinB] = poolMap.get(poolId).type.replace('>', '').split('<')[1].split(', ')
api.add(coinA, coinAamount.toString())
api.add(coinB, coinBamount.toString())
}
}
)

// calculate the Vault TVL.

const vaultInfoIds = addresses.Vaults.map(valut => valut.vaultInfo)
const vaultInfos = await sui.getObjects(vaultInfoIds)

for (let i = 0; i < vaultInfos.length; i++) {
const baseToken = addresses.Vaults[i].baseToken
const tokenAmount = vaultInfos[i].fields.value.fields.coin
api.add(baseToken, tokenAmount)
const vaultDebtVal = vaultInfos[i].fields.value.fields.vault_debt_val
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this value represent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @g1nt0ki , thanks for the review. This value represent the borrowed part in Mole savings vaults. It is a mistake we didn't add it up before. It is a fixing for vaults value. Thank you.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but the borrowed tokens dont leave the platform but are used as LP and you are already adding LP value right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @g1nt0ki , thank you for your review.

  1. For the vault_debt_val, sorry that it may cause some misunderstanding. It is mToken value staked in Mole fairlauch. It was missing before. It is actually should be taken into calculated in the Mole savings vaults vaule.

  2. For the Aptos chain TVL, if it should be kept for history tracking purpose, I'll submit a code fix later that keep Mole in Aptos TVL.

const vaultAmount = parseInt(tokenAmount) + parseInt(vaultDebtVal)
api.add(baseToken, vaultAmount.toString())
}
}

Expand All @@ -312,3 +301,4 @@ module.exports = {
calLyfTvlAptos,
calLyfTvlSui,
}

Loading
Loading