Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

[Fix][Sherlock M-12] Correctly account for fee debits during liquidation #204

Open
wants to merge 1 commit into
base: dev2
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions packages/perennial/contracts/collateral/Collateral.sol
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ contract Collateral is ICollateral, UInitializable, UControllerProvider, UReentr

product.closeAll(account);

// Update after closing positions to account for position fees
totalCollateral = collateral(account, product);

// claim fee
UFixed18 liquidationFee = controller().liquidationFee();
// If maintenance is less than minCollateral, use minCollateral for fee amount
Expand Down
46 changes: 46 additions & 0 deletions packages/perennial/test/integration/core/liquidate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,52 @@ describe('Liquidate', () => {
.reverted
})

it('liquidates a user total collateral after fees', async () => {
const POSITION = utils.parseEther('0.0001')
const { user, userB, collateral, dsu, chainlink } = instanceVars

const product = await createProduct(instanceVars)
await depositTo(instanceVars, user, product, utils.parseEther('1000'))
await product.connect(user).openMake(POSITION)

expect(await collateral.liquidatable(user.address, product.address)).to.be.false

// Settle the product with a new oracle version
await chainlink.nextWithPriceModification(price => price.mul(10))
await product.settle()

await product.settleAccount(user.address)

expect(await collateral.liquidatableNext(user.address, product.address)).to.be.true
expect(await collateral.liquidatable(user.address, product.address)).to.be.true

await product.updateMakerFee(utils.parseEther('0.001'))
await expect(collateral.connect(userB).liquidate(user.address, product.address))
.to.emit(collateral, 'Liquidation')
.withArgs(user.address, product.address, userB.address, BigNumber.from('886203501804460347929'))

expect(await product.isLiquidating(user.address)).to.be.true
await expect(collateral.connect(userB).liquidate(user.address, product.address))
.to.be.revertedWithCustomError(collateral, 'CollateralAccountLiquidatingError')
.withArgs(user.address)
await expect(
collateral.connect(user).withdrawTo(user.address, product.address, constants.MaxUint256),
).to.be.revertedWithCustomError(collateral, 'CollateralInsufficientCollateralError')

expect(await collateral['collateral(address,address)'](user.address, product.address)).to.equal(0)
expect(await collateral['collateral(address)'](product.address)).to.equal(
utils.parseEther('1000').sub('886203501804460347929'),
)
expect(await dsu.balanceOf(userB.address)).to.equal(utils.parseEther('20000').add('886203501804460347929')) // Original 20000 + fee

await chainlink.next()
await product.settleAccount(user.address)

expect(await product.isLiquidating(user.address)).to.be.false
await expect(collateral.connect(user).withdrawTo(user.address, product.address, constants.MaxUint256)).to.not.be
.reverted
})

it('liquidates a user with minCollateral', async () => {
const POSITION = utils.parseEther('0.0001')
const { user, userB, collateral, controller, dsu, chainlink } = instanceVars
Expand Down