-
Notifications
You must be signed in to change notification settings - Fork 0
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
Fee inclusivity calculations are inaccurate in RubiconMarket #1312
Labels
2 (Med Risk)
Assets not at direct risk, but function/availability of the protocol could be impacted or leak value
bug
Something isn't working
high quality report
This report is of especially high quality
M-01
primary issue
Highest quality submission among a set of duplicates
selected for report
This submission will be included/highlighted in the audit report
sponsor confirmed
Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Comments
0xSorryNotSorry marked the issue as high quality report |
0xSorryNotSorry marked the issue as primary issue |
This was referenced May 2, 2023
daoio marked the issue as sponsor confirmed |
This was referenced May 4, 2023
This was referenced May 26, 2023
Closed
Closed
Closed
HickupHH3 marked issue #899 as primary and marked this issue as a duplicate of 899 |
HickupHH3 marked the issue as selected for report |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
2 (Med Risk)
Assets not at direct risk, but function/availability of the protocol could be impacted or leak value
bug
Something isn't working
high quality report
This report is of especially high quality
M-01
primary issue
Highest quality submission among a set of duplicates
selected for report
This submission will be included/highlighted in the audit report
sponsor confirmed
Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Lines of code
https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/RubiconMarket.sol#L578-L589
Vulnerability details
Impact
Trading in the RubiconMarket has associated fee costs that are paid by the taker of the offer. These fees include the protocol fee and a new "maker fee" introduced in v2. Fees are pulled from the taker (caller of the function) independently of the trade amount, which means fees are not included in the trade amount. These are implemented in the
buy
function of the base contractSimpleMarket
:https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/RubiconMarket.sol#L337-L373
One of the new additions in the RubiconMarket v2 is fee inclusivity, a feature that would allow users to operate on the market by including the fee directly in the specified amount. This is present in different places of the contract, but the core implementation can be founded in the
calcAmountAfterFee
function:https://github.com/code-423n4/2023-04-rubicon/blob/main/contracts/RubiconMarket.sol#L578-L589
As we can see in the previous snippet, the function calculates the protocol fee and the marker fee based on the given amount, and substacts those values from the amount. This is an inaccurate calculation, as these fees later on will be calculated using this new value, which won't end up totalling the requested original amount. As an example, let's consider the case of 100 tokens, a 10% protocol fee and a 5% maker fee:
1000 * 10% = 100
tokens.1000 * 5% = 50
tokens.calcAmountAfterFee
will be1000 - 100 - 50 = 850
tokens.buy
function, the trade amount will be 850 tokens, and the function will then calculate fees based on this amount.850 * 10% = 85
and maker fee will be calculated as850 * 5% = 42
tokens.850 + 85 + 42 = 977
which is a bit less than the original 1000 tokens.Proof of Concept
In the following test, Alice makes an offer to sell 30k USDC for 1 BTC. Bob will execute the trade to buy the complete 30k USDC with BTC. However, as the fee calculation is inaccurate, the trade will be executed for an amount less than expected and Bob will be left with some unspent BTC (0.000225 BTC).
Note: the snippet shows only the relevant code for the test. Full test file can be found here.
Recommendation
The correct calculation for the fee inclusivity amount should be as follows:
For the example given in the previous section, this would result in an amount of
1000 / (100% + 10% + 5%) = 869
.The text was updated successfully, but these errors were encountered: