Submitted by rbserver, also found by adriro, zhuXKET, immeas, bin2chen, cccz, rvierdiiev, said, anodaram, T1MOH, RaymondFam, MalfurionWhitehat, ladboy233 and J4de.
When the following Position._marketBuy and Position._marketSell functions calculate _fee, the maker fee is not included.
https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/utilities/poolsUtility/Position.sol#L454-L473
https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/utilities/poolsUtility/Position.sol#L475-L511
When the Position._marketBuy and Position._marketSell functions eventually call the following RubiconMarket.buy function, the maker fee can be considered and paid when makerFee() > 0 is true.
https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/RubiconMarket.sol#L314-L448
Since the Position._marketBuy and Position._marketSell functions do not consider the maker fee but the RubiconMarket.buy function does, _fee calculated by the Position._marketBuy and Position._marketSell functions and the fees, including the maker fee, that are paid by the user when calling the RubiconMarket.buy function are different. As a result, calling the Position contracts functions that eventually call the Position._marketBuy or Position._marketSell function can cause incorrect calculations and user funds to be lost. For instance, when calling the Position.buyAllAmountWithLeverage function, the user should receive less asset tokens when the Position._marketBuy function includes the maker fee in _fee than when the Position._marketBuy function does not include the maker fee in _fee. Since the Position._marketBuy function currently does not include the maker fee in _fee, the user receives more asset tokens than she or he is entitled to, and other users asset tokens in the protocol are lost.
First, please update the Position._marketBuy function as follows to divide by 100_000 for the POC purpose. This is for fixing the bug mentioned in the report titled Calling Position._marketBuy and Position._marketSell functions that calculate _fee by dividing by 10000 can cause incorrect calculations.
Second, please add the following changes of quote and asset tokens when Position._marketBuy function calculates _fee through dividing by 100_000 but does not consider maker fee test in the Long positions describe block in test\hardhat-tests\leverage-wrapper.ts. This test will pass to show the number of asset tokens received by the user who calls the Position.buyAllAmountWithLeverage function when the Position._marketBuy function does not include the maker fee in _fee.
Third, please update the Position._marketBuy function again as follows to include the maker fee in _fee for the POC purpose.
Fourth, please add the following changes of quote and asset tokens when Position._marketBuy function calculates _fee through dividing by 100_000 and also considers maker fee test in the Long positions describe block in test\hardhat-tests\leverage-wrapper.ts. This test will pass to show the number of asset tokens received by the user who calls the Position.buyAllAmountWithLeverage function when the Position._marketBuy function includes the maker fee in _fee. This test also demonstrates that the user should receive less asset tokens when the Position._marketBuy function includes the maker fee in _fee than when the Position._marketBuy function does not include the maker fee in _fee.
VSCode
The Position._marketBuy and Position._marketSell functions can be updated to include the maker fee in _fee when rubiconMarket.makerFee() > 0 is true.
HickupHH3 (judge) decreased severity to Medium and commented:
