Migration safeMath div to Solidity 8

Viewed 327

I am trying to migrate safeMath code to solidity 8, I have almost all arithmetic operations except div

TypeError: Member "div" not found or not visible after argument-dependent lookup in uint256.

currentShares = (_amount * (totalShares)).div(pool);

In the documentation they talk about a new property, but I am not sure how to implement it Panic(uint256)

EDIT: Could this be the simple solution that it could be?

   currentShares = (_amount * (totalShares)) / (pool);
1 Answers

SafeMath is not needed on Solidity 0.8+, because the logic is implemented on the language level.

Arithmetic operations revert on underflow and overflow. You can use unchecked { ... } to use the previous wrapping behaviour.

Source: https://docs.soliditylang.org/en/v0.8.11/080-breaking-changes.html


currentShares = (_amount * (totalShares)) / (pool);

This is a correct solution in v0.8+ that throws an exception in case you run into overflow, division by zero, as well as an underflow (not possible in this snippet, but generally underflow throws an exception as well). An uncaught exepction reverts the transaction/call.

Related