Skip to content

Commit

Permalink
Fix for L-21 issue (#2322)
Browse files Browse the repository at this point in the history
* Fix for L-21 issue

* Update test

* Update test
  • Loading branch information
0xMithrandir authored Oct 11, 2024
1 parent c37d64c commit 5255a1c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
6 changes: 5 additions & 1 deletion markets/bfp-market/contracts/storage/Margin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,11 @@ library Margin {
uint256 liqFeeWithRewardInUsd = liqFeeInUsd +
collateralValue.mulDecimal(marketConfig.liquidationRewardPercent);

return MathUtil.min(liqFeeWithRewardInUsd, globalConfig.maxKeeperFeeUsd);
uint256 boundedKeeperFeeUsd = MathUtil.min(
MathUtil.max(globalConfig.minKeeperFeeUsd, liqFeeWithRewardInUsd),
globalConfig.maxKeeperFeeUsd
);
return boundedKeeperFeeUsd;
}

/// @dev Returns whether an account in a specific market's margin can be liquidated.
Expand Down
12 changes: 10 additions & 2 deletions markets/bfp-market/contracts/storage/Position.sol
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,11 @@ library Position {
marketConfig.liquidationRewardPercent
);

return MathUtil.min(flagFeeWithRewardInUsd, globalConfig.maxKeeperFeeUsd);
uint256 boundedKeeperFeeUsd = MathUtil.min(
MathUtil.max(globalConfig.minKeeperFeeUsd, flagFeeWithRewardInUsd),
globalConfig.maxKeeperFeeUsd
);
return boundedKeeperFeeUsd;
}

/**
Expand Down Expand Up @@ -559,7 +563,11 @@ library Position {
);
uint256 iterations = getLiquidationIterations(size, maxLiqCapacity);

return MathUtil.min(liquidationFeeInUsd * iterations, globalConfig.maxKeeperFeeUsd);
uint256 boundedKeeperFeeUsd = MathUtil.min(
MathUtil.max(globalConfig.minKeeperFeeUsd, liquidationFeeInUsd * iterations),
globalConfig.maxKeeperFeeUsd
);
return boundedKeeperFeeUsd;
}

/// @dev Returns the health data given the `marketId`, `config`, and position{...} details.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ describe('LiquidationModule', () => {
keeperProfitMarginUsd: bn(0), // ensure keeperProfitMarginPercent would be used instead
keeperLiquidationGasUnits: 500_000,
keeperFlagGasUnits: 500_000,
minKeeperFeeUsd: bn(0),
});

// Set baseFeePerGas to 1gwei
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,17 @@ describe('PerpAccountModule splitAccount', () => {
const trader1 = traders()[1];

await setMarketConfiguration(bs, {
minKeeperFeeUsd: bn(100),
minKeeperFeeUsd: bn(10),
maxKeeperFeeUsd: bn(100),
});

// We want to split such that the from account is left with:
// We want to split such that from account is left with:
// - 0 size
// - nonzero collateral
// - nonzer0 debt
// Where the debt is nearly as much as the the collateral
// - nonzero debt
// Where the debt is nearly as much as the collateral

// If we allow this, the account would left in a margin liquidatable state
// If we allow this, the account would be left in a margin liquidatable state
// and the user can profit from the liquidation reward.

// Even if this action were not profitable for the user, it
Expand All @@ -198,13 +198,13 @@ describe('PerpAccountModule splitAccount', () => {
desiredMarginUsdDepositAmount: depositUsdAmount,
})
);
const iniitialOrder = await genOrder(bs, market, collateral, collateralDepositAmount, {
const initialOrder = await genOrder(bs, market, collateral, collateralDepositAmount, {
desiredSize: wei(0.99).toBN(),
desiredKeeperFeeBufferUsd: 10,
desiredPriceImpactPercentage: 0.5,
});

await commitAndSettle(bs, marketId, trader0, iniitialOrder);
await commitAndSettle(bs, marketId, trader0, initialOrder);
// Pay debt from the order fee so we can withdraw a bit
await payDebt(bs, marketId, trader0);

Expand Down

0 comments on commit 5255a1c

Please sign in to comment.