Skip to content

Commit

Permalink
feat: use FixesTVL to simplify TVL calculation and to fix TVL query e…
Browse files Browse the repository at this point in the history
…rror
  • Loading branch information
btspoony committed Dec 28, 2024
1 parent 661c475 commit d67b992
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 145 deletions.
55 changes: 3 additions & 52 deletions projects/fixes-coins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
`;

Expand All @@ -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);
}
}

Expand Down
57 changes: 4 additions & 53 deletions projects/fixes-frc20-staking/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
`;

Expand All @@ -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
);
}
}
Expand Down
43 changes: 3 additions & 40 deletions projects/fixes-frc20/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
`;

Expand All @@ -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
);
}
}
Expand Down

0 comments on commit d67b992

Please sign in to comment.