Submitted by rbserver, also found by zhuXKET, Josiah, Aymen0909, immeas, immeas, teddav, joestakey, Ruhum, cducrest, markus_ether, markus_ether, Nyx, dec3ntraliz3d, Viktor_Cortess, __141345__, Toshii, 0xDING99YA, sashik_eth, Madalad, peanuts, AlexCzm, R2, 0xfusion, SpicyMeatball, sinarette, cccz, xmxanuel, said, T1MOH, RaymondFam, MalfurionWhitehat, ljmanini, volodya, 3agle, sces60107, anodaram, ckksec, 0xnev, dacian and volodya.
Calling the following Position._marketBuy function executes uint256 _fee = _maxFill.mul(rubiconMarket.getFeeBPS()).div(10000), and calling the Position._marketSell function below executes uint256 _fee = _minFill.mul(_feeBPS).div(10000) and _fee = _payAmount.mul(_feeBPS).div(10000); in these _fee calculations, 10000 is used as the denominator.
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 calling the Position._marketBuy and Position._marketSell functions, the following RubiconMarket.buy function will be eventually called. Calling the RubiconMarket.buy function executes uint256 fee = mul(spend, feeBPS) / 100_000, where 100_000, which is not 10000, is the denominator.
https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/RubiconMarket.sol#L314-L448
Because the Position._marketBuy and Position._marketSell functions calculate _fee through dividing by 10000 but the RubiconMarket.buy function calculates fee through dividing by 100_000, _fee and fee are different. This can cause incorrect calculations when calling the Position contracts functions that eventually call the Position._marketBuy or Position._marketSell function, and users can lose tokens that they are entitled to, as a result. For example, when calling the Position.buyAllAmountWithLeverage function, the user should receive more asset tokens when the Position._marketBuy function calculates _fee through dividing by 100_000 than when the Position._marketBuy function calculates _fee through dividing by 10000. Thus, since the Position._marketBuy function currently calculates _fee through dividing by 10000, the user loses some asset tokens that they are entitled to.
First, please add the following changes of quote and asset tokens when Position._marketBuy function calculates _fee through dividing by 10000 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 calculates _fee through dividing by 10000.
Second, please update the Position._marketBuy function, as follows, to divide by 100_000 for the POC purpose.
Third, please add the following changes of quote and asset tokens when Position._marketBuy function calculates _fee through dividing by 100_000 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 calculates _fee through dividing by 100_000. This test also demonstrates that the user should receive more asset tokens when the Position._marketBuy function calculates _fee through dividing by 100_000 than when the Position._marketBuy function calculates _fee through dividing by 10000.
VSCode
The Position._marketBuy and Position._marketSell functions can be updated to divide by 100_000, instead of 10000, when calculating _fee.
bghughes (Rubicon) confirmed via duplicate issue #1025
HickupHH3 (judge) decreased severity to Medium
