Note: At the judges request here, this downgraded issue from the same warden has been included in this report for completeness.
https://github.com/code-423n4/2024-01-init-capital-invitational/blob/a01c4de620be98f9e57a60cf6a82d4feaec54f58/contracts/hook/MarginTradingHook.sol#L179
https://github.com/code-423n4/2024-01-init-capital-invitational/blob/a01c4de620be98f9e57a60cf6a82d4feaec54f58/contracts/hook/MarginTradingHook.sol#L484
https://github.com/code-423n4/2024-01-init-capital-invitational/blob/a01c4de620be98f9e57a60cf6a82d4feaec54f58/contracts/hook/MarginTradingHook.sol#L208
MarginTradeHook.sol#fillOrder does not validate order creators minHealth when filling the order
In MarginTradingHook.sol, user can create stopLoss or take profits order, this is the logic for fulfilling order:
As we can see, first, the borr token is transfered in to prepare for repayment then the msg.sender pays order.recipient with token amount out:
Then, the we repay for order creator:
Then the msg.sender that fulfill the order can remove order creators collateral:
The problem is that when removing order creators collateral amount, unlike create Margin Position where the code give users option to validate the account min_health, the fillOrder logic cannot let order creator specify minaccounthealth after order fulfillment.
Let us consider the case below:
This can be avoided if user can specify the account min health.
If user say, after order fulfilled, the account health cannot be below a certain threshold.
Fulfilling order that remove too much collateral can revert and user will not subject to liquidation.
The root cause is also this order.collAmount can become stale.
After user removeCollateral, the order.collAmount can become stale.
Allow the user to specify min account health when creating the order:
https://github.com/code-423n4/2024-01-init-capital-invitational/blob/a01c4de620be98f9e57a60cf6a82d4feaec54f58/contracts/hook/MarginTradingHook.sol#L346
Validate the min_health when they fulfill the order:
https://github.com/code-423n4/2024-01-init-capital-invitational/blob/a01c4de620be98f9e57a60cf6a82d4feaec54f58/contracts/hook/MarginTradingHook.sol#L396
fez-init (INIT) acknowledged
For this audit, 2 reports were submitted by wardens detailing gas optimizations. The report highlighted below by rvierdiiev received the top score from the judge.
The following wardens also submitted reports: ladboy233.
