@@ -5,7 +5,7 @@ import BlockchainHelpers
55
66import " test_helpers.cdc"
77import " evm_state_helpers.cdc"
8- import " btc_daily_helpers .cdc"
8+ import " simulation_btc_daily_historical_helpers .cdc"
99
1010import " FlowYieldVaults"
1111import " FlowToken"
@@ -14,7 +14,6 @@ import "FlowYieldVaultsStrategiesV2"
1414import " FlowALPv0"
1515import " DeFiActions"
1616
17-
1817// ============================================================================
1918// CADENCE ACCOUNTS
2019// ============================================================================
@@ -372,6 +371,14 @@ fun runDailySimulation(
372371 log (" HF (Position): triggers when HF < \( agents [0 ].minHF ) or HF > \( agents [0 ].maxHF ) , rebalances to HF = \( agents [0 ].targetHF ) " )
373372 log (" VR (Vault): triggers when VR < \( vaultLowerThreshold ) or VR > \( vaultUpperThreshold ) , rebalances to VR ~ 1.0" )
374373
374+ // Liquidator account: pre-funded with MOET so it can attempt manual liquidations
375+ let liquidator = Test .createAccount ()
376+ transferFlow (signer : whaleFlowAccount , recipient : liquidator .address , amount : 10.0 )
377+ setupMoetVault (liquidator , beFailed : false )
378+ setupGenericVault (signer : liquidator , vaultIdentifier : WBTC_TOKEN_ID )
379+ let liquidatorMOETReserve = 100_000.0
380+ mintMoet (signer : flowALPAccount , to : liquidator .address , amount : liquidatorMOETReserve , beFailed : false )
381+
375382 var liquidationCount = 0
376383 var previousBTCPrice = initialPrice
377384 var lowestPrice = initialPrice
@@ -425,6 +432,30 @@ fun runDailySimulation(
425432 }
426433 preVaultRatios .append (preVaultRatio )
427434
435+ // Liquidation occurs when effectiveHF < 1.0 (check pre-rebalance)
436+ if preRebalanceHF < 1.0 && preRebalanceHF > 0.0 {
437+ liquidationCount = liquidationCount + 1
438+ log (" *** LIQUIDATION agent=\( a ) on day \( day ) (\( dates [day ]) )! HF=\( preRebalanceHF ) price=\( absolutePrice ) previousPrice=\( previousBTCPrice ) ***" )
439+ // Attempt a liquidation before rebalancing (worst case)
440+
441+ // TODO: Mainnet DEX does not support swapping these pairs, yet.
442+ let debt = getMOETDebtFromPosition (pid : pids [a ])
443+ let repayAmount = debt * 0.1
444+ let fairBTC = repayAmount / absolutePrice
445+ let seizeAmount = fairBTC * 0.95
446+
447+ let liqRes = _executeTransaction (
448+ " ../../lib/FlowALP/cadence/transactions/flow-alp/pool-management/manual_liquidation.cdc" ,
449+ [pids [a ], Type <@MOET.Vault >().identifier , WBTC_TOKEN_ID , seizeAmount , repayAmount ],
450+ liquidator
451+ )
452+ if liqRes .error == nil {
453+ log (" Liquidation succeeded: repaid \( repayAmount ) MOET, seized \( seizeAmount ) BTC" )
454+ } else {
455+ log (" Liquidation failed: \( liqRes .error ! .message ) " )
456+ }
457+ }
458+
428459 a = a + 1
429460 }
430461
@@ -468,12 +499,6 @@ fun runDailySimulation(
468499 if a == 0 && (day % 7 == 0 || absolutePrice == lowestPrice || absolutePrice == highestPrice ) {
469500 log (" [day \( day ) ] \( dates [day ]) price=$\( absolutePrice ) yt=\( ytPrice ) HF=\( preRebalanceHF ) ->\( postRebalanceHF ) VR=\( preVaultRatio ) ->\( postVaultRatio ) vaultRebalances=\( dayVaultRebalances ) positionRebalances=\( dayPositionRebalances ) " )
470501 }
471-
472- // Liquidation occurs when effectiveHF < 1.0 (check pre-rebalance)
473- if preRebalanceHF < 1.0 && preRebalanceHF > 0.0 {
474- liquidationCount = liquidationCount + 1
475- log (" *** LIQUIDATION agent=\( a ) on day \( day ) (\( dates [day ]) )! HF=\( preRebalanceHF ) price=\( absolutePrice ) previousPrice=\( previousBTCPrice ) ***" )
476- }
477502 a = a + 1
478503 }
479504
@@ -555,6 +580,8 @@ fun runDailySimulation(
555580
556581// ============================================================================
557582// TEST CASES
583+ // Edit the json files to change the simulation parameters. These are in the scripts/simulations folder.
584+ // Then run the generate_fixture.py script to generate the helpers.cdc file.
558585// ============================================================================
559586
560587access (all )
@@ -617,21 +644,6 @@ fun test_BtcDaily2024Bull_DailyRebalancing() {
617644 )
618645}
619646
620- access (all )
621- fun test_BtcDaily2024CapEff_DailyRebalancing () {
622- runDailySimulation (
623- scenarioName : " BTC Daily 2024 Capital Efficiency" ,
624- prices : btc_daily_2024_capeff_prices ,
625- dates : btc_daily_2024_capeff_dates ,
626- agents : btc_daily_2024_capeff_agents ,
627- pools : btc_daily_2024_capeff_pools ,
628- constants : btc_daily_2024_capeff_constants ,
629- expectedLiquidationCount : btc_daily_2024_capeff_expectedLiquidationCount ,
630- durationDays : btc_daily_2024_capeff_durationDays ,
631- notes : btc_daily_2024_capeff_notes
632- )
633- }
634-
635647access (all )
636648fun test_BtcDaily2025_DailyRebalancing () {
637649 runDailySimulation (
0 commit comments