Skip to content

Commit ebeef04

Browse files
zhangchiqingjribbink
authored andcommitted
speed up by combining scripts
1 parent 1666c94 commit ebeef04

5 files changed

Lines changed: 26 additions & 94 deletions

File tree

.claude/settings.json

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import "FlowYieldVaultsAutoBalancers"
2+
3+
/// Returns the value of deposits tracked by the AutoBalancer related to the provided YieldVault ID or `nil` if none exists
4+
/// This is the historical cumulative value used to compute the rebalance ratio: currentValue / valueOfDeposits
5+
///
6+
access(all)
7+
fun main(id: UInt64): UFix64? {
8+
return FlowYieldVaultsAutoBalancers.borrowAutoBalancer(id: id)?.valueOfDeposits() ?? nil
9+
}

cadence/tests/forked_btc_daily_2025_test.cdc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,14 +345,13 @@ fun test_BtcDaily2025_DailyRebalancing() {
345345
a = a + 1
346346
}
347347

348-
// Calculate vault ratio BEFORE rebalancing
348+
// Calculate vault ratio BEFORE rebalancing (single script call for efficiency)
349349
// vaultRatio = currentValue / valueOfDeposits
350350
// Triggers when ratio < 0.95 or ratio > 1.05
351351
var preVaultRatio: UFix64 = 1.0
352-
let preCurrentValue = getAutoBalancerCurrentValue(id: vaultIds[0]) ?? 0.0
353-
let preValueOfDeposits = getAutoBalancerValueOfDeposits(id: vaultIds[0]) ?? 0.0
354-
if preValueOfDeposits > 0.0 {
355-
preVaultRatio = preCurrentValue / preValueOfDeposits
352+
let preMetrics = getAutoBalancerMetrics(id: vaultIds[0]) ?? [0.0, 0.0]
353+
if preMetrics[1] > 0.0 {
354+
preVaultRatio = preMetrics[0] / preMetrics[1]
356355
}
357356

358357
// Potentially rebalance all agents (not forced)
@@ -371,12 +370,11 @@ fun test_BtcDaily2025_DailyRebalancing() {
371370
prevVaultRebalanceCount = currentVaultRebalanceCount
372371
prevPositionRebalanceCount = currentPositionRebalanceCount
373372

374-
// Calculate vault ratio AFTER rebalancing
373+
// Calculate vault ratio AFTER rebalancing (single script call for efficiency)
375374
var postVaultRatio: UFix64 = 1.0
376-
let postCurrentValue = getAutoBalancerCurrentValue(id: vaultIds[0]) ?? 0.0
377-
let postValueOfDeposits = getAutoBalancerValueOfDeposits(id: vaultIds[0]) ?? 0.0
378-
if postValueOfDeposits > 0.0 {
379-
postVaultRatio = postCurrentValue / postValueOfDeposits
375+
let postMetrics = getAutoBalancerMetrics(id: vaultIds[0]) ?? [0.0, 0.0]
376+
if postMetrics[1] > 0.0 {
377+
postVaultRatio = postMetrics[0] / postMetrics[1]
380378
}
381379

382380
// Calculate HF AFTER rebalancing

cadence/tests/test_helpers.cdc

Lines changed: 9 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,15 @@ fun getAutoBalancerValueOfDeposits(id: UInt64): UFix64? {
560560
return res.returnValue as! UFix64?
561561
}
562562

563+
/// Returns [currentValue, valueOfDeposits] for the AutoBalancer in a single script call.
564+
/// This is more efficient than calling getAutoBalancerCurrentValue and getAutoBalancerValueOfDeposits separately.
565+
access(all)
566+
fun getAutoBalancerMetrics(id: UInt64): [UFix64]? {
567+
let res = _executeScript("../scripts/flow-yield-vaults/get_auto_balancer_metrics_by_id.cdc", [id])
568+
Test.expect(res, Test.beSucceeded())
569+
return res.returnValue as! [UFix64]?
570+
}
571+
563572
access(all)
564573
fun getPositionDetails(pid: UInt64, beFailed: Bool): FlowALPv0.PositionDetails {
565574
let res = _executeScript("../../lib/FlowALP/cadence/scripts/flow-alp/position_details.cdc",
@@ -1222,64 +1231,3 @@ access(all) fun getMOETDebtFromPosition(pid: UInt64): UFix64 {
12221231
}
12231232
return 0.0
12241233
}
1225-
1226-
access(all)
1227-
fun setupGenericVault(signer: Test.TestAccount, vaultIdentifier: String) {
1228-
let setupResult = _executeTransaction(
1229-
"../../lib/flow-evm-bridge/cadence/transactions/example-assets/setup/setup_generic_vault.cdc",
1230-
[vaultIdentifier],
1231-
signer
1232-
)
1233-
Test.expect(setupResult, Test.beSucceeded())
1234-
}
1235-
1236-
access(all)
1237-
fun setERC20Balance(
1238-
signer: Test.TestAccount,
1239-
tokenAddress: String,
1240-
holderAddress: String,
1241-
balanceSlot: UInt256,
1242-
amount: UInt256
1243-
) {
1244-
let res = _executeTransaction(
1245-
"transactions/set_erc20_balance.cdc",
1246-
[tokenAddress, holderAddress, balanceSlot, amount],
1247-
signer
1248-
)
1249-
Test.expect(res, Test.beSucceeded())
1250-
}
1251-
1252-
access(all)
1253-
fun mintBTC(signer: Test.TestAccount, amount: UFix64) {
1254-
let wbtcAddress = "0x717dae2baf7656be9a9b01dee31d571a9d4c9579"
1255-
let wbtcTokenId = "A.1e4aa0b87d10b141.EVMVMBridgedToken_717dae2baf7656be9a9b01dee31d571a9d4c9579.Vault"
1256-
let wbtcBalanceSlot: UInt256 = 5
1257-
1258-
// Ensure signer has a COA (needs some FLOW for gas)
1259-
if getCOA(signer.address) == nil {
1260-
createCOA(signer, fundingAmount: 1.0)
1261-
}
1262-
let coaAddress = getCOA(signer.address)!
1263-
1264-
// Set wBTC ERC20 balance for the signer's COA on EVM
1265-
// wBTC has 8 decimals, so multiply amount by 1e8
1266-
// Split to avoid UFix64 overflow for large amounts
1267-
let whole = UInt256(amount)
1268-
let frac = amount - UFix64(UInt64(amount))
1269-
let amountSmallestUnit = whole * 100_000_000 + UInt256(frac * 100_000_000.0)
1270-
setERC20Balance(
1271-
signer: signer,
1272-
tokenAddress: wbtcAddress,
1273-
holderAddress: coaAddress,
1274-
balanceSlot: wbtcBalanceSlot,
1275-
amount: amountSmallestUnit
1276-
)
1277-
1278-
// Bridge wBTC from EVM to Cadence
1279-
let bridgeRes = _executeTransaction(
1280-
"../../lib/flow-evm-bridge/cadence/transactions/bridge/tokens/bridge_tokens_from_evm.cdc",
1281-
[wbtcTokenId, amountSmallestUnit],
1282-
signer
1283-
)
1284-
Test.expect(bridgeRes, Test.beSucceeded())
1285-
}

lib/FlowCreditMarket

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)