Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Collateral with fee check in invariant #88

Merged
merged 1 commit into from
Aug 31, 2023
Merged
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/Market.sol
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,9 @@ contract Market is IMarket, Instance, ReentrancyGuard {
.sub(Fixed6Lib.from(pendingLocalPositions[i].keeper));
}
pendingLocalPositions[pendingLocalPositions.length - 1] = context.currentPosition.local; // current local position hasn't been stored yet
collateralAfterFees = collateralAfterFees
.sub(Fixed6Lib.from(context.currentPosition.local.fee))
.sub(Fixed6Lib.from(context.currentPosition.local.keeper));
}

/// @notice Computes the liquidation fee for the current latest local position
Expand Down
98 changes: 98 additions & 0 deletions packages/perennial/test/unit/market/Market.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11001,6 +11001,104 @@ describe('Market', () => {
).to.be.revertedWithCustomError(market, `MarketInsufficientLiquidityError`)
})

it('reverts when opening and closing immediately with settlementFee', async () => {
const marketParameter = { ...(await market.parameter()) }
marketParameter.settlementFee = parse6decimal('0.50')
await market.updateParameter(marketParameter)

const minMaintenanceAmount = parse6decimal('100')
const dustPosition = parse6decimal('0.000001')
dsu.transferFrom
.whenCalledWith(
user.address,
market.address,
minMaintenanceAmount.add(marketParameter.settlementFee).mul(1e12),
)
.returns(true)

await expect(
market.connect(user).update(user.address, dustPosition, 0, 0, minMaintenanceAmount, false),
).to.be.revertedWithCustomError(market, 'MarketInsufficientCollateralizationError')

await expect(
market
.connect(user)
.update(user.address, dustPosition, 0, 0, minMaintenanceAmount.add(marketParameter.settlementFee), false),
)
.to.emit(market, 'Updated')
.withArgs(
user.address,
ORACLE_VERSION_2.timestamp,
dustPosition,
0,
0,
minMaintenanceAmount.add(marketParameter.settlementFee),
false,
)

expectLocalEq(await market.locals(user.address), {
currentId: 1,
latestId: 0,
collateral: minMaintenanceAmount.add(marketParameter.settlementFee),
reward: 0,
protection: 0,
})
expectPositionEq(await market.positions(user.address), {
...DEFAULT_POSITION,
timestamp: ORACLE_VERSION_1.timestamp,
})
expectPositionEq(await market.pendingPositions(user.address, 1), {
...DEFAULT_POSITION,
timestamp: ORACLE_VERSION_2.timestamp,
maker: dustPosition,
delta: minMaintenanceAmount.add(marketParameter.settlementFee),
})
expectGlobalEq(await market.global(), {
currentId: 1,
latestId: 0,
protocolFee: 0,
oracleFee: 0,
riskFee: 0,
donation: 0,
})
expectPositionEq(await market.position(), {
...DEFAULT_POSITION,
timestamp: ORACLE_VERSION_1.timestamp,
})
expectPositionEq(await market.pendingPosition(1), {
...DEFAULT_POSITION,
timestamp: ORACLE_VERSION_2.timestamp,
maker: dustPosition,
})
expectVersionEq(await market.versions(ORACLE_VERSION_1.timestamp), {
makerValue: { _value: 0 },
longValue: { _value: 0 },
shortValue: { _value: 0 },
makerReward: { _value: 0 },
longReward: { _value: 0 },
shortReward: { _value: 0 },
})

await expect(
market.connect(user).update(user.address, 0, 0, 0, minMaintenanceAmount.mul(-1), false),
).to.be.revertedWithCustomError(market, 'MarketInsufficientCollateralError')

dsu.transfer
.whenCalledWith(user.address, minMaintenanceAmount.sub(marketParameter.settlementFee).mul(1e12))
.returns(true)
await market
.connect(user)
.update(user.address, 0, 0, 0, minMaintenanceAmount.sub(marketParameter.settlementFee).mul(-1), false)

expectLocalEq(await market.locals(user.address), {
currentId: 1,
latestId: 0,
collateral: parse6decimal('1'),
reward: 0,
protection: 0,
})
})

context('in liquidation', async () => {
const EXPECTED_LIQUIDATION_FEE = parse6decimal('225')

Expand Down