getBeanTokenPriceFromTwaReserves
will return wrong price with tokens that does not have 18 decimals
https://github.com/Cyfrin/2024-05-beanstalk-the-finale/blob/main/protocol/contracts/libraries/Well/LibWell.sol#L249-L257
The getBeanTokenPriceFromTwaReserves
is meant to compute the token price in terms of beans of a token paired with bean in a well. The implementation works as follows:
As we can see, the computation that is done is reserveOfBeans / reservesOfToken. Because solidity does not support floating point computations if reservesOfToken is greater than reserveOfBeans would return 0. To avoid this scenario, the reserveOfBeans is multiplied by 1e18. This extra decimals are intended to be cancelled with the next division.
For example:
reserveOfBeans = 1000e6
reservesOfToken = 10e18
computedPrice = 1000e6 * 1e18 / 10e18 = 100e6
In this example, the price of the token are 100 beans.
This 1e18 is used because almost all ERC20 have 18 decimals of precision. However, not all of them use this amount of decimals and this can lead to wrong results. Let's take the example of the WBTC on Ethereum chain. This token has 8 decimals of precision. Let's consider the following scenario:
reserveOfBeans = 60000e6
reservesOfWBTC = 1e8
In this scenario 1 WBTC equals 60000 beans, hence the result of the function would need to be 60000e6. However the result of the computation is much higher:
computedPrice = 60000e6 * 1e18 / 1e8 = 600000000000000e6
This extra 10 decimals multiplied make the price much expensive and will return a wrong amount to the protocol.
This function is used when evaluating the price during a sunrise
function. Where the well with the largest liquidity is fetch and computed the price.
This makes that if at some point the well with the largest liquidity has a token with more or less than 18 decimals it will return a wrong price and the caseId
for the evaluation of the price will be wrong.
High
Manual review
Multiply the reserves of beans by the amount of decimals of the token:
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.