airswap-protocols icon indicating copy to clipboard operation
airswap-protocols copied to clipboard

SafeMath usage clarification

Open parseb opened this issue 3 years ago • 1 comments

https://github.com/airswap/airswap-protocols/blob/34e735993f13ae585b78a7ad47e13d54ff62c192/source/staking/contracts/Staking.sol#L5

I am actually curious about this. Is there a deliberate reason why safemath is being used here given that solidity 0.8.0 introduced overflow/underflow protections or is it the result of habit?

Not using SafeMath for uint256 would reduce contract size and likely gas.

Arithmetic operations revert on underflow and overflow. You can use unchecked { ... } to use the previous wrapping behaviour. Checks for overflow are very common, so we made them the default to increase readability of code, even if it comes at a slight increase of gas costs. https://docs.soliditylang.org/en/v0.8.10/080-breaking-changes.html

parseb avatar Dec 11 '21 02:12 parseb

The original committed contract was sub v0.8.0. We have since bumped up all contracts up to ^0.8.0. In this existing contract SafeMath was left in as the contract functions were not in scope for that bump up. I'm not sure how significant the gas optimizations are though sure we can bring this up in our dev meeting.

AboldUSER avatar Dec 18 '21 21:12 AboldUSER