From d67b992c7811e6caf3ae15a7be4eb071d761e9e8 Mon Sep 17 00:00:00 2001 From: Tang Bo Hao Date: Sat, 28 Dec 2024 23:27:31 +0800 Subject: [PATCH] feat: use FixesTVL to simplify TVL calculation and to fix TVL query error --- projects/fixes-coins/index.js | 55 ++------------------------ projects/fixes-frc20-staking/index.js | 57 ++------------------------- projects/fixes-frc20/index.js | 43 ++------------------ 3 files changed, 10 insertions(+), 145 deletions(-) diff --git a/projects/fixes-coins/index.js b/projects/fixes-coins/index.js index 924a420ba0b3..7a6b5611b798 100644 --- a/projects/fixes-coins/index.js +++ b/projects/fixes-coins/index.js @@ -2,60 +2,11 @@ const { callCadence } = require("../helper/chain/flow"); let queryTVLCode = ` -import LiquidStaking from 0xd6f80565193ad727 -import stFlowToken from 0xd6f80565193ad727 -// Fixes Imports -import FRC20AccountsPool from 0xd2abb5dbf5e08666 -import FixesFungibleTokenInterface from 0xd2abb5dbf5e08666 -import FixesTokenLockDrops from 0xd2abb5dbf5e08666 -import FixesTradablePool from 0xd2abb5dbf5e08666 -import FRC20Indexer from 0xd2abb5dbf5e08666 +import FixesTVL from 0xd2abb5dbf5e08666 access(all) fun main(): UFix64 { - // singleton resource and constants - let acctsPool = FRC20AccountsPool.borrowAccountsPool() - let frc20Indexer = FRC20Indexer.getIndexer() - let stFlowTokenKey = "@".concat(Type<@stFlowToken.Vault>().identifier) - - // dictionary of addresses - let addrsDict = acctsPool.getAddresses(type: FRC20AccountsPool.ChildAccountType.FungibleToken) - // dictionary of tickers and total locked token balances - let tickerTotal: {String: UFix64} = {} - // This is the soft burned LP value which is fully locked in the BlackHole Vault - var flowLockedInBondingCurve = 0.0 - addrsDict.forEachKey(fun (key: String): Bool { - if let addr = addrsDict[key] { - // sum up all locked token balances in LockDrops Pool - if let dropsPool = FixesTokenLockDrops.borrowDropsPool(addr) { - let lockedTokenSymbol = dropsPool.getLockingTokenTicker() - tickerTotal[lockedTokenSymbol] = (tickerTotal[lockedTokenSymbol] ?? 0.0) + dropsPool.getTotalLockedTokenBalance() - } - // sum up all burned LP value in Tradable Pool - if let tradablePool = FixesTradablePool.borrowTradablePool(addr) { - flowLockedInBondingCurve = flowLockedInBondingCurve + tradablePool.getFlowBalanceInPool() - } - } - return true - }) - // sum up all locked token balances in LockDrops Pool - var totalLockingTokenTVL = 0.0 - tickerTotal.forEachKey(fun (key: String): Bool { - let lockedAmount = tickerTotal[key]! - if key == "" { - // this is locked FLOW - totalLockingTokenTVL = totalLockingTokenTVL + lockedAmount - } else if key == "fixes" { - // this is locked FIXES - let price = frc20Indexer.getBenchmarkValue(tick: "fixes") - totalLockingTokenTVL = totalLockingTokenTVL + lockedAmount * price - } else if key == stFlowTokenKey { - // this is locked stFlow - totalLockingTokenTVL = totalLockingTokenTVL + LiquidStaking.calcFlowFromStFlow(stFlowAmount: lockedAmount) - } - return true - }) - return totalLockingTokenTVL + flowLockedInBondingCurve + return FixesTVL.getAllLockedCoinFlowValue() } `; @@ -64,7 +15,7 @@ async function tvl() { const flowTokenTVL = await callCadence(queryTVLCode, true); return { flow: flowTokenTVL }; } catch (error) { - throw new Error("Couln't query scripts of Fixes coins", error); + throw new Error("Couln't query scripts of Fixes coins. Error: " + error.message); } } diff --git a/projects/fixes-frc20-staking/index.js b/projects/fixes-frc20-staking/index.js index 119d797cb1de..c3c371fb1d6b 100644 --- a/projects/fixes-frc20-staking/index.js +++ b/projects/fixes-frc20-staking/index.js @@ -2,60 +2,11 @@ const { callCadence } = require("../helper/chain/flow"); let queryTVLCode = ` -import FRC20Staking from 0xd2abb5dbf5e08666 -import FRC20AccountsPool from 0xd2abb5dbf5e08666 -import FRC20Marketplace from 0xd2abb5dbf5e08666 -import FRC20Storefront from 0xd2abb5dbf5e08666 -import FRC20Indexer from 0xd2abb5dbf5e08666 +import FixesTVL from 0xd2abb5dbf5e08666 access(all) fun main(): UFix64 { - let acctsPool = FRC20AccountsPool.borrowAccountsPool() - let stakingTokens = acctsPool.getAddresses(type: FRC20AccountsPool.ChildAccountType.Staking) - - var totalTVL = 0.0 - let ticks = stakingTokens.keys - - for tick in ticks { - let stakingAddr = stakingTokens[tick]! - let stakingPool = FRC20Staking.borrowPool(stakingAddr) - if stakingPool == nil { - continue - } - - let indexer = FRC20Indexer.getIndexer() - // calculate floor price - let benchmarkPrice = indexer.getBenchmarkValue(tick: tick) - var floorPrice = benchmarkPrice - - if let marketAddr = acctsPool.getFRC20MarketAddress(tick: tick) { - if let market = FRC20Marketplace.borrowMarket(marketAddr) { - let buyPriceRanks = market.getPriceRanks(type: FRC20Storefront.ListingType.FixedPriceBuyNow) - if buyPriceRanks.length > 0 { - var i = 0 - let floorPriceRank = buyPriceRanks[i] - let listIds = market.getListedIds(type: FRC20Storefront.ListingType.FixedPriceBuyNow, rank: floorPriceRank) - if listIds.length > 0 { - if let listing = market.getListedItem( - type: FRC20Storefront.ListingType.FixedPriceBuyNow, - rank: floorPriceRank, - id: listIds[0] - ) { - if let details = listing.getDetails() { - floorPrice = details.pricePerToken() - } - } - } - } - } - } // end if - - var details = stakingPool!.getDetails() - let validStaked = details.totalStaked - details.totalUnstakingLocked - - totalTVL = totalTVL + (validStaked * (floorPrice - benchmarkPrice)) - } - return totalTVL + return FixesTVL.getAllStakedFlowValue() } `; @@ -64,9 +15,9 @@ async function tvl() { const flowTokenTVL = await callCadence(queryTVLCode, true); return { flow: flowTokenTVL }; } catch (error) { + console.error(error.message); throw new Error( - "Couln't query scripts of fixes š¯”‰rc20 treasury pool", - error + "Couln't query scripts of fixes š¯”‰rc20 treasury pool. Error: " + error.message ); } } diff --git a/projects/fixes-frc20/index.js b/projects/fixes-frc20/index.js index 332ac4ca58a5..626d8109518d 100644 --- a/projects/fixes-frc20/index.js +++ b/projects/fixes-frc20/index.js @@ -2,47 +2,11 @@ const { callCadence } = require("../helper/chain/flow"); let queryTVLCode = ` -import FRC20Indexer from 0xd2abb5dbf5e08666 -import FGameLottery from 0xd2abb5dbf5e08666 -import FGameLotteryRegistry from 0xd2abb5dbf5e08666 -import FGameLotteryFactory from 0xd2abb5dbf5e08666 -import FRC20FTShared from 0xd2abb5dbf5e08666 -import FRC20Staking from 0xd2abb5dbf5e08666 -import FRC20AccountsPool from 0xd2abb5dbf5e08666 +import FixesTVL from 0xd2abb5dbf5e08666 access(all) fun main(): UFix64 { - let indexer = FRC20Indexer.getIndexer() - let tokens = indexer.getTokens() - var totalBalance = 0.0 - // all treasury pool balance - for tick in tokens { - let balance = indexer.getPoolBalance(tick: tick) - totalBalance = totalBalance + balance - } - - // FLOW lottery jackpot balance - let registry = FGameLotteryRegistry.borrowRegistry() - let flowLotteryPoolName = FGameLotteryFactory.getFIXESMintingLotteryPoolName() - if let poolAddr = registry.getLotteryPoolAddress(flowLotteryPoolName) { - if let poolRef = FGameLottery.borrowLotteryPool(poolAddr) { - let jackpotBalance = poolRef.getJackpotPoolBalance() - totalBalance = totalBalance + jackpotBalance - } - } - - // Unclaimed FLOW Reward in the staking reward pool - let acctsPool = FRC20AccountsPool.borrowAccountsPool() - let platformStakingTick = FRC20FTShared.getPlatformStakingTickerName() - if let stakingPoolAddr = acctsPool.getFRC20StakingAddress(tick: platformStakingTick) { - if let stakingPool = FRC20Staking.borrowPool(stakingPoolAddr) { - if let detail = stakingPool.getRewardDetails("") { - totalBalance = totalBalance + detail.totalReward - } - } - } - - return totalBalance + return FixesTVL.getAllTreasuryFlowValue() } `; @@ -52,8 +16,7 @@ async function tvl() { return { flow: flowTokenTVL }; } catch (error) { throw new Error( - "Couln't query scripts of fixes š¯”‰rc20 treasury pool", - error + "Couln't query scripts of fixes š¯”‰rc20 treasury pool. Error: " + error.message ); } }