It fails when engine.getUSDValue(collateralAddres, amountDeposited); is called. After debugging a bit with console logs I've seen that with a very high number it throws this exception. This is the function implementation
**
* @dev Returns the USD value of a given token amount.
* @param _priceFeedAddress The address of the Chainlink price feed.
* @param _amount The amount of tokens.
* @return The USD value of the token amount.
*/
function getUSDValue(address _priceFeedAddress, uint256 _amount) public view returns (uint256) {
AggregatorV3Interface priceFeed = AggregatorV3Interface(_priceFeedAddress);
(, int256 price,,,) = priceFeed.latestRoundData();
if (_amount == 0 || price == 0) {
return 0;
}
console2.log("We're about to revert with Arithmetic over/underflow");
uint256 priceWithPrecision = uint256(price) * ADDITIONAL_FEED_PRECISION;
console.log("### ~ priceWithPrecision:", priceWithPrecision);
console.log("### ~ _amount:", _amount);
uint256 usdValue = (priceWithPrecision * _amount) / PRECISION;
console.log("### ~ usdValue:", usdValue); // --> This is never printed when priceWithPrecision * _amount with high values happens
return usdValue;
}
Debugging
This is what the console prints: (the pricefeeds are mocked to return 2k weth abd 40k wbtc)
So I took those numbers and tested it using chisel
More traces
How should I handle this scenario? In any case if the price in USD is too high or the amount deposited is too high... this is going to break right? 🤔
I've tried lowering the _amount = bound(_amount, 1, type(uint).max); to _amount = bound(_amount, 1,type(uint96).max); And it does not revert
[PASS] invariant_protocolMustHaveMoreValueThanTotalSupply() (runs: 64, calls: 4096, reverts: 0) but the question still the same, in a "real" scenario this overflow error still could happen right?
Hi! I've already seen the closed issue https://github.com/Cyfrin/foundry-defi-stablecoin-f23/issues/30 but is not my case. Im running into this error when calling
depositCollateral
in the handler contract with a very high_amount
.This is the handler contract
This is the invariant contract
It fails when
engine.getUSDValue(collateralAddres, amountDeposited);
is called. After debugging a bit with console logs I've seen that with a very high number it throws this exception. This is the function implementationDebugging
This is what the console prints: (the pricefeeds are mocked to return 2k weth abd 40k wbtc)
So I took those numbers and tested it using
chisel
More traces
How should I handle this scenario? In any case if the price in USD is too high or the amount deposited is too high... this is going to break right? 🤔
Thanks
PD: This is my repository if you want to see more context https://github.com/0fprod/defi-protocol
EDIT:
I've tried lowering the
_amount = bound(_amount, 1, type(uint).max);
to_amount = bound(_amount, 1,type(uint96).max);
And it does not revert[PASS] invariant_protocolMustHaveMoreValueThanTotalSupply() (runs: 64, calls: 4096, reverts: 0)
but the question still the same, in a "real" scenario this overflow error still could happen right?