buy and sell will revert when a low decimal ERC20 token is used as baseToken #208
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
duplicate-243
edited-by-warden
satisfactory
satisfies C4 submission criteria; eligible for awards
Lines of code
https://github.com/code-423n4/2022-12-caviar/blob/main/src/Pair.sol#L154
https://github.com/code-423n4/2022-12-caviar/blob/main/src/Pair.sol#L186
https://github.com/code-423n4/2022-12-caviar/blob/main/src/Pair.sol#L398-L400
https://github.com/code-423n4/2022-12-caviar/blob/main/src/Pair.sol#L406-L409
Vulnerability details
Impact
When buying or selling fractional Tokens from pair, buyQuote and sellQuote is used to calculate how much base Token is needed in exchange of the amount of fractional Tokens user is buying or selling. In the event that a low decimal ERC20 token is used (USDC with 6 decimals), buyQuote and sellQuote will underflow and revert unless fractional tokens amount is big enough.
Proof of Concept
In
buy
, buyQuote is used to calculate the amount of input base Tokens for the amount of fractional tokens user wants to buy.However, we can see that in
buyQuote
, fractionalTokenReserves (18 decimals) will be significantly bigger than baseTokenReserves if base Token has a small decimal count. i.e (USDC 6 decimals). buyQuote and sellQuote will underflow and revert due to baseTokenReserves divide by fractionalTokenReserves.Tools Used
Manual Review
Recommended Mitigation Steps
There should be a minimum for outputAmount depending on the number of decimal the ERC20 used for base Token has.
Or if possible, ONE can be based on base token decimals.
The text was updated successfully, but these errors were encountered: