withdrawMarginUsd()
uses round down
to compute requiredMarginInCollateralX18
May result in fillOrder()
part of fee
not being collected
in fillOrder()
We will deduct 3 fees settlementFee
, orderFee
, pnl
fillOrder()
-> deductAccountMargin()
The problem is this line: UD60x18 requiredMarginInCollateralX18 = amountUsdX18.div(marginCollateralPriceUsdX18);
This is using round down
, which may cause dust pnl
to change isMissingMargin
to false, causing break loop
.
Example.
The user has two collaterals
marginCollateralBalance = [Collateral_A = 1e18 usd , Collateral_B= 2000e18 usd ]
settlementFee =100e18
orderFee=100e18
pnl = 9
marginCollateralPrice= 10e18
loop use Collateral_A
1.1 charge settlementFee: withdrawMarginUsd(settlementFee=100e18)
marginCollateralBalanceX18 = 1e18
requiredMarginInCollateralX18 = 100e18 * 1e18 / 10e18 = 10e18
isMissingMargin = true , Remaining = 99e18 --------------------> correct
1.2 charge orderFee :withdrawMarginUsd(orderFee=100e18)
marginCollateralBalanceX18 = 0
requiredMarginInCollateralX18 = 100e18* 1e18 / 10e18 = 10e18
so isMissingMargin = true , Remaining = 100e18 --------------------> correct
1.3 charge pnl: withdrawMarginUsd(pnl=9)
marginCollateralBalanceX18 = 0
requiredMarginInCollateralX18 = 9* 1e18 / 10e18 = 0 ----------> ***** round down *****
so marginCollateralBalanceX18.gte(requiredMarginInCollateralX18) == true
so isMissingMargin = false --------------------------------------> **** wrong *****
will loop Collateral_B , but step 1.3 , isMissingMargin == false , so loop break
When charging a pnl, due to the use of round down
requiredMarginInCollateralX18 = amountUsdX18.div(marginCollateralPriceUsdX18) = 9 * 1e18 / 10e18 ==0
This caused the loop to jump out of the loop early by incorrectly overriding isMissingMargin=false
.
miss fees: settlementFee = 99e18 + orderFee = 100e18
dust pnl can cause orderFee/settlementFee to not be fully collected
use round up
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.