Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cadence/contracts/FlowALPv0.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ access(all) contract FlowALPv0 {
/// - C means collateral: Ce is effective collateral, Ct is true collateral, measured in $
/// - D means debt: De is effective debt, Dt is true debt, measured in $
/// - Fc, Fd are collateral and debt factors
access(all) fun manualLiquidation(
access(FlowALPModels.EParticipant) fun manualLiquidation(
pid: UInt64,
debtType: Type,
seizeType: Type,
Expand Down
2 changes: 1 addition & 1 deletion cadence/tests/cap_test.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ fun testPublishClaimCap() {
// =============================================================================
//
// Actor: eParticipantUser — Capability<auth(EParticipant) &Pool>
// Matrix rows: createPosition, depositToPosition
// Matrix rows: createPosition, depositToPosition, manualLiquidation

/// EParticipant cap allows createPosition and depositToPosition.
access(all)
Expand Down
8 changes: 8 additions & 0 deletions cadence/tests/fork_liquidation_edge_cases.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ fun testPartialLiquidationSequences() {
// Post effectiveCollateral = 990 * 0.855 * 0.8 = 677.16
// Post health = 677.16 / 700 = 0.967371428571428571428571 ≤ 1.05
let liq1Res = manualLiquidation(
admin: MAINNET_PROTOCOL_ACCOUNT,
signer: liquidator1,
pid: pid1,
debtVaultIdentifier: MAINNET_MOET_TOKEN_ID,
Expand All @@ -224,6 +225,7 @@ fun testPartialLiquidationSequences() {
// Post effectiveCollateral = 980 * 0.855 * 0.8 = 670.32
// Post health = 670.32 / 680 = 0.985764705882352941176470 ≤ 1.05
let liq2Res = manualLiquidation(
admin: MAINNET_PROTOCOL_ACCOUNT,
signer: liquidator1,
pid: pid1,
debtVaultIdentifier: MAINNET_MOET_TOKEN_ID,
Expand All @@ -240,6 +242,7 @@ fun testPartialLiquidationSequences() {
// Post effectiveCollateral = 970 * 0.855 * 0.8 = 663.48
// Post health = 663.48 / 660 = 1.005272727272727272727272 ≤ 1.05
let liq3Res = manualLiquidation(
admin: MAINNET_PROTOCOL_ACCOUNT,
signer: liquidator1,
pid: pid1,
debtVaultIdentifier: MAINNET_MOET_TOKEN_ID,
Expand All @@ -259,6 +262,7 @@ fun testPartialLiquidationSequences() {

// Liquidation call 4: fails because health = 1.00527 > 1.0
let liq4Res = manualLiquidation(
admin: MAINNET_PROTOCOL_ACCOUNT,
signer: liquidator1,
pid: pid1,
debtVaultIdentifier: MAINNET_MOET_TOKEN_ID,
Expand All @@ -274,6 +278,7 @@ fun testPartialLiquidationSequences() {
mintMoet(signer: MAINNET_PROTOCOL_ACCOUNT, to: liquidator2.address, amount: 500.0, beFailed: false)

let liq5Res = manualLiquidation(
admin: MAINNET_PROTOCOL_ACCOUNT,
signer: liquidator2,
pid: pid1,
debtVaultIdentifier: MAINNET_MOET_TOKEN_ID,
Expand Down Expand Up @@ -410,6 +415,7 @@ fun testLiquidateMultiCollateralChooseUSDC() {
Test.expect(res, Test.beSucceeded())

let liqRes = manualLiquidation(
admin: MAINNET_PROTOCOL_ACCOUNT,
signer: liquidator,
pid: pid,
debtVaultIdentifier: MAINNET_USDF_TOKEN_ID,
Expand All @@ -435,6 +441,7 @@ fun testLiquidateMultiCollateralChooseUSDC() {

// A second liquidation attempt fails — position is now healthy
let liqRes2 = manualLiquidation(
admin: MAINNET_PROTOCOL_ACCOUNT,
signer: liquidator,
pid: pid,
debtVaultIdentifier: MAINNET_USDF_TOKEN_ID,
Expand Down Expand Up @@ -592,6 +599,7 @@ fun testStabilityAndInsuranceFees_notCollectedForLiquidatedFunds() {
let liquidator = Test.createAccount()
transferTokensWithSetup(tokenIdentifier: MAINNET_USDF_TOKEN_ID, from: MAINNET_USDF_HOLDER, to: liquidator, amount: 200.0)
let liqRes = manualLiquidation(
admin: MAINNET_PROTOCOL_ACCOUNT,
signer: liquidator,
pid: pid,
debtVaultIdentifier: MAINNET_USDF_TOKEN_ID,
Expand Down
1 change: 1 addition & 0 deletions cadence/tests/fork_multi_collateral_position_test.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ fun test_multi_asset_liquidation_collateral_selection() {
let seizeAmount: UFix64 = 140.0

let liqRes = manualLiquidation(
admin: MAINNET_PROTOCOL_ACCOUNT,
signer:liquidator,
pid: pid,
debtVaultIdentifier: MAINNET_MOET_TOKEN_ID,
Expand Down
2 changes: 2 additions & 0 deletions cadence/tests/fork_multiple_positions_per_user_test.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ access(all) fun testBatchLiquidations() {
let batchRepayAmounts = [usdfRepay, wethRepay, usdcRepay, wbtcRepay]

batchManualLiquidation(
admin: MAINNET_PROTOCOL_ACCOUNT,
pids: batchPids,
debtVaultIdentifier: MAINNET_MOET_TOKEN_ID,
seizeVaultIdentifiers: batchSeizeTypes,
Expand Down Expand Up @@ -945,6 +946,7 @@ access(all) fun testMassUnhealthyLiquidations() {
// DEX sources MOET from MAINNET_PROTOCOL_ACCOUNT's vault; liquidator receives seized collateral.
log("Liquidating all 100 positions via DEX in chunks of 10...\n")
batchLiquidateViaMockDex(
admin: MAINNET_PROTOCOL_ACCOUNT,
pids: batchPids,
debtVaultIdentifier: MAINNET_MOET_TOKEN_ID,
seizeVaultIdentifiers: batchSeize,
Expand Down
2 changes: 2 additions & 0 deletions cadence/tests/fork_oracle_failure_test.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ fun test_governance_tightens_dex_deviation_threshold() {
mintMoet(signer: MAINNET_PROTOCOL_ACCOUNT, to: liquidator.address, amount: 500.0, beFailed: false)

let liqRes = manualLiquidation(
admin: MAINNET_PROTOCOL_ACCOUNT,
signer: liquidator,
pid: pid,
debtVaultIdentifier: MAINNET_MOET_TOKEN_ID,
Expand Down Expand Up @@ -407,6 +408,7 @@ fun test_flash_crash_triggers_liquidation() {
// DEX quote: 100 / 0.50 = 200 FLOW
// Liquidator offers: 195 FLOW (better than DEX)
let liqRes = manualLiquidation(
admin: MAINNET_PROTOCOL_ACCOUNT,
signer: liquidator,
pid: pid,
debtVaultIdentifier: MAINNET_MOET_TOKEN_ID,
Expand Down
52 changes: 37 additions & 15 deletions cadence/tests/liquidation_phase1_test.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ fun testManualLiquidation_healthyPosition() {
let repayAmount = 2.0
let seizeAmount = 1.0
let liqRes = manualLiquidation(
signer:liquidator,
admin: PROTOCOL_ACCOUNT,
signer:liquidator,
pid: pid,
debtVaultIdentifier: Type<@MOET.Vault>().identifier,
seizeVaultIdentifier: FLOW_TOKEN_IDENTIFIER,
Expand Down Expand Up @@ -131,7 +132,8 @@ fun testManualLiquidation_liquidationExceedsTargetHealth() {
let repayAmount = 500.0
let seizeAmount = 500.0
let liqRes = manualLiquidation(
signer:liquidator,
admin: PROTOCOL_ACCOUNT,
signer:liquidator,
pid: pid,
debtVaultIdentifier: Type<@MOET.Vault>().identifier,
seizeVaultIdentifier: FLOW_TOKEN_IDENTIFIER,
Expand Down Expand Up @@ -194,7 +196,8 @@ fun testManualLiquidation_repayExceedsDebt() {
let repayAmount = debtBalance + 0.001
let seizeAmount = (repayAmount / newPrice) * 0.99
let liqRes = manualLiquidation(
signer:liquidator,
admin: PROTOCOL_ACCOUNT,
signer:liquidator,
pid: pid,
debtVaultIdentifier: Type<@MOET.Vault>().identifier,
seizeVaultIdentifier: FLOW_TOKEN_IDENTIFIER,
Expand Down Expand Up @@ -255,7 +258,8 @@ fun testManualLiquidation_seizeExceedsCollateral() {
let seizeAmount = collateralBalance + 0.001
let repayAmount = seizeAmount * newPrice * 1.01
let liqRes = manualLiquidation(
signer:liquidator,
admin: PROTOCOL_ACCOUNT,
signer:liquidator,
pid: pid,
debtVaultIdentifier: Type<@MOET.Vault>().identifier,
seizeVaultIdentifier: FLOW_TOKEN_IDENTIFIER,
Expand Down Expand Up @@ -317,7 +321,8 @@ fun testManualLiquidation_reduceHealth() {
let seizeAmount = collateralBalancePreLiq - 0.01
let repayAmount = seizeAmount * newPrice * 1.01
let liqRes = manualLiquidation(
signer:liquidator,
admin: PROTOCOL_ACCOUNT,
signer:liquidator,
pid: pid,
debtVaultIdentifier: Type<@MOET.Vault>().identifier,
seizeVaultIdentifier: FLOW_TOKEN_IDENTIFIER,
Expand Down Expand Up @@ -383,6 +388,7 @@ fun testBadDebt_seizeAllCollateral() {
// Post-health = 0 / remainingDebt = 0.0 ≤ 1.05 ✓
let minRepay: UFix64 = initialCollateral * crashedPrice + 0.00000001
let liqRes = manualLiquidation(
admin: PROTOCOL_ACCOUNT,
signer: liquidator,
pid: pid,
debtVaultIdentifier: Type<@MOET.Vault>().identifier,
Expand Down Expand Up @@ -433,6 +439,7 @@ fun testBadDebt_voluntaryRepayment_fullRepayReverts() {
// First liquidation: seize all collateral, enter bad-debt state
let minRepay: UFix64 = initialCollateral * crashedPrice + 0.00000001
Test.expect(manualLiquidation(
admin: PROTOCOL_ACCOUNT,
signer: liquidator,
pid: pid,
debtVaultIdentifier: Type<@MOET.Vault>().identifier,
Expand All @@ -448,6 +455,7 @@ fun testBadDebt_voluntaryRepayment_fullRepayReverts() {
// Post-health: 0 / 0.00000001 = 0.0 ≤ 1.05 ✓
let almostAll = remainingDebt - 0.00000001
Test.expect(manualLiquidation(
admin: PROTOCOL_ACCOUNT,
signer: liquidator,
pid: pid,
debtVaultIdentifier: Type<@MOET.Vault>().identifier,
Expand All @@ -459,6 +467,7 @@ fun testBadDebt_voluntaryRepayment_fullRepayReverts() {
// === Third liquidation: repaying the final tick reverts ===
// Repaying 0.00000001 MOET sets postDebt=0, so postHealth=UFix128.max > 1.05 → revert
let liqRes3 = manualLiquidation(
admin: PROTOCOL_ACCOUNT,
signer: liquidator,
pid: pid,
debtVaultIdentifier: Type<@MOET.Vault>().identifier,
Expand Down Expand Up @@ -512,7 +521,8 @@ fun testManualLiquidation_increaseHealthBelowTarget() {
let repayAmount = 100.0
let seizeAmount = 150.0
let liqRes = manualLiquidation(
signer:liquidator,
admin: PROTOCOL_ACCOUNT,
signer:liquidator,
pid: pid,
debtVaultIdentifier: Type<@MOET.Vault>().identifier,
seizeVaultIdentifier: FLOW_TOKEN_IDENTIFIER,
Expand Down Expand Up @@ -580,7 +590,8 @@ fun testManualLiquidation_liquidateToTarget() {
let repayAmount = 100.0
let seizeAmount = 33.66
let liqRes = manualLiquidation(
signer:liquidator,
admin: PROTOCOL_ACCOUNT,
signer:liquidator,
pid: pid,
debtVaultIdentifier: Type<@MOET.Vault>().identifier,
seizeVaultIdentifier: FLOW_TOKEN_IDENTIFIER,
Expand Down Expand Up @@ -639,6 +650,7 @@ fun testManualLiquidation_repaymentVaultCollateralType() {
// Purport to repay MOET to seize FLOW, but we will actually pass in a FLOW vault for repayment
let repayAmount = debtBalance + 0.001
let seizeAmount = (repayAmount / newPrice) * 0.99
grantBetaPoolParticipantAccess(PROTOCOL_ACCOUNT, liquidator)
let liqRes = _executeTransaction(
"../tests/transactions/flow-alp/helpers/manual_liquidation_chosen_vault.cdc",
[pid, Type<@MOET.Vault>().identifier, FLOW_TOKEN_IDENTIFIER, FLOW_TOKEN_IDENTIFIER, seizeAmount, repayAmount],
Expand Down Expand Up @@ -695,6 +707,7 @@ fun testManualLiquidation_repaymentVaultTypeMismatch() {
// Purport to repay MOET to seize FLOW, but we will actually pass in a MockYieldToken vault for repayment
let repayAmount = debtBalance + 0.001
let seizeAmount = (repayAmount / newPrice) * 0.99
grantBetaPoolParticipantAccess(PROTOCOL_ACCOUNT, liquidator)
let liqRes = _executeTransaction(
"../tests/transactions/flow-alp/helpers/manual_liquidation_chosen_vault.cdc",
[pid, Type<@MOET.Vault>().identifier, MOCK_YIELD_TOKEN_IDENTIFIER, FLOW_TOKEN_IDENTIFIER, seizeAmount, repayAmount],
Expand Down Expand Up @@ -750,6 +763,7 @@ fun testManualLiquidation_unsupportedDebtType() {
// Pass in MockYieldToken as repayment, an unsupported debt type
let repayAmount = debtBalance + 0.001
let seizeAmount = (repayAmount / newPrice) * 0.99
grantBetaPoolParticipantAccess(PROTOCOL_ACCOUNT, liquidator)
let liqRes = _executeTransaction(
"../tests/transactions/flow-alp/helpers/manual_liquidation_chosen_vault.cdc",
[pid, MOCK_YIELD_TOKEN_IDENTIFIER, MOCK_YIELD_TOKEN_IDENTIFIER, FLOW_TOKEN_IDENTIFIER, seizeAmount, repayAmount],
Expand Down Expand Up @@ -806,7 +820,8 @@ fun testManualLiquidation_unsupportedCollateralType() {
let seizeAmount = collateralBalancePreLiq - 0.01
let repayAmount = seizeAmount * newPrice * 1.01
let liqRes = manualLiquidation(
signer:liquidator,
admin: PROTOCOL_ACCOUNT,
signer:liquidator,
pid: pid,
debtVaultIdentifier: Type<@MOET.Vault>().identifier,
seizeVaultIdentifier: MOCK_YIELD_TOKEN_IDENTIFIER,
Expand Down Expand Up @@ -893,7 +908,8 @@ fun testManualLiquidation_supportedDebtTypeNotInPosition() {
let seizeAmount = 0.01
let repayAmount = 100.0
let liqRes = manualLiquidation(
signer:liquidator,
admin: PROTOCOL_ACCOUNT,
signer:liquidator,
pid: pid1,
debtVaultIdentifier: MOCK_YIELD_TOKEN_IDENTIFIER,
seizeVaultIdentifier: FLOW_TOKEN_IDENTIFIER,
Expand Down Expand Up @@ -980,7 +996,8 @@ fun testManualLiquidation_supportedCollateralTypeNotInPosition() {
let seizeAmount = 0.01
let repayAmount = 100.0
let liqRes = manualLiquidation(
signer:liquidator,
admin: PROTOCOL_ACCOUNT,
signer:liquidator,
pid: pid1,
debtVaultIdentifier: Type<@MOET.Vault>().identifier,
seizeVaultIdentifier: MOCK_YIELD_TOKEN_IDENTIFIER,
Expand Down Expand Up @@ -1042,7 +1059,8 @@ fun testManualLiquidation_dexOraclePriceDivergence_withinThreshold() {
let repayAmount = 50.0
let seizeAmount = 72.0
let liqRes = manualLiquidation(
signer:liquidator,
admin: PROTOCOL_ACCOUNT,
signer:liquidator,
pid: pid,
debtVaultIdentifier: Type<@MOET.Vault>().identifier,
seizeVaultIdentifier: FLOW_TOKEN_IDENTIFIER,
Expand Down Expand Up @@ -1080,7 +1098,8 @@ fun testManualLiquidation_dexOraclePriceDivergence_dexBelowOracle() {
setupMoetVault(liquidator, beFailed: false)
mintMoet(signer: Test.getAccount(0x0000000000000007), to: liquidator.address, amount: 1000.0, beFailed: false)
let liqRes = manualLiquidation(
signer:liquidator,
admin: PROTOCOL_ACCOUNT,
signer:liquidator,
pid: pid,
debtVaultIdentifier: Type<@MOET.Vault>().identifier,
seizeVaultIdentifier: FLOW_TOKEN_IDENTIFIER,
Expand Down Expand Up @@ -1119,7 +1138,8 @@ fun testManualLiquidation_dexOraclePriceDivergence_dexAboveOracle() {
setupMoetVault(liquidator, beFailed: false)
mintMoet(signer: Test.getAccount(0x0000000000000007), to: liquidator.address, amount: 1000.0, beFailed: false)
let liqRes = manualLiquidation(
signer:liquidator,
admin: PROTOCOL_ACCOUNT,
signer:liquidator,
pid: pid,
debtVaultIdentifier: Type<@MOET.Vault>().identifier,
seizeVaultIdentifier: FLOW_TOKEN_IDENTIFIER,
Expand Down Expand Up @@ -1173,7 +1193,8 @@ fun testManualLiquidation_liquidatorOfferWorseThanDex() {
let repayAmount = 50.0
let seizeAmount = 75.0
let liqRes = manualLiquidation(
signer:liquidator,
admin: PROTOCOL_ACCOUNT,
signer:liquidator,
pid: pid,
debtVaultIdentifier: Type<@MOET.Vault>().identifier,
seizeVaultIdentifier: FLOW_TOKEN_IDENTIFIER,
Expand Down Expand Up @@ -1229,7 +1250,8 @@ fun testManualLiquidation_combinedEdgeCase() {
let repayAmount = 50.0
let seizeAmount = 75.0
let liqRes = manualLiquidation(
signer:liquidator,
admin: PROTOCOL_ACCOUNT,
signer:liquidator,
pid: pid,
debtVaultIdentifier: Type<@MOET.Vault>().identifier,
seizeVaultIdentifier: FLOW_TOKEN_IDENTIFIER,
Expand Down
8 changes: 8 additions & 0 deletions cadence/tests/test_helpers.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -876,13 +876,15 @@ fun rebalancePosition(signer: Test.TestAccount, pid: UInt64, force: Bool, beFail

access(all)
fun manualLiquidation(
admin: Test.TestAccount,
signer: Test.TestAccount,
pid: UInt64,
debtVaultIdentifier: String,
seizeVaultIdentifier: String,
seizeAmount: UFix64,
repayAmount: UFix64,
): Test.TransactionResult {
grantBetaPoolParticipantAccess(admin, signer)
return _executeTransaction(
"../transactions/flow-alp/pool-management/manual_liquidation.cdc",
[pid, debtVaultIdentifier, seizeVaultIdentifier, seizeAmount, repayAmount],
Expand All @@ -892,13 +894,15 @@ fun manualLiquidation(

access(all)
fun liquidateViaMockDex(
admin: Test.TestAccount,
signer: Test.TestAccount,
pid: UInt64,
debtVaultIdentifier: String,
seizeVaultIdentifier: String,
seizeAmount: UFix64,
repayAmount: UFix64,
): Test.TransactionResult {
grantBetaPoolParticipantAccess(admin, signer)
return _executeTransaction(
"./transactions/flow-alp/pool-management/batch_liquidate_via_mock_dex.cdc",
[[pid], debtVaultIdentifier, [seizeVaultIdentifier], [seizeAmount], [repayAmount]],
Expand All @@ -909,13 +913,15 @@ fun liquidateViaMockDex(
/// Batch-liquidate positions using the liquidator's own tokens as repayment (no DEX).
/// The liquidator must hold sufficient debt tokens upfront.
access(all) fun batchManualLiquidation(
admin: Test.TestAccount,
pids: [UInt64],
debtVaultIdentifier: String,
seizeVaultIdentifiers: [String],
seizeAmounts: [UFix64],
repayAmounts: [UFix64],
signer: Test.TestAccount
) {
grantBetaPoolParticipantAccess(admin, signer)
let res = _executeTransaction(
"./transactions/flow-alp/pool-management/batch_manual_liquidation.cdc",
[pids, debtVaultIdentifier, seizeVaultIdentifiers, seizeAmounts, repayAmounts],
Expand All @@ -927,6 +933,7 @@ access(all) fun batchManualLiquidation(
/// Batch-liquidate positions using MockDexSwapper as the repayment source in chunks of
/// chunkSize to stay within the computation limit.
access(all) fun batchLiquidateViaMockDex(
admin: Test.TestAccount,
pids: [UInt64],
debtVaultIdentifier: String,
seizeVaultIdentifiers: [String],
Expand All @@ -935,6 +942,7 @@ access(all) fun batchLiquidateViaMockDex(
chunkSize: Int,
signer: Test.TestAccount
) {
grantBetaPoolParticipantAccess(admin, signer)
let total = pids.length
let numChunks = (total + chunkSize - 1) / chunkSize
for i in InclusiveRange(0, numChunks - 1) {
Expand Down
Loading
Loading