1

I've been told SafeMath doesn't matter.

Does SafeMath slow things down, cost more ETH, or does it add an extra level of stability? Will SafeMath be unsupported one day?

contract SafeMath {
function safeAdd(uint a, uint b) public pure returns (uint c) {
    c = a + b;
    require(c >= a);
}
function safeSub(uint a, uint b) public pure returns (uint c) {
    require(b <= a); c = a - b; } function safeMul(uint a, uint b) public pure returns (uint c) { c = a * b; require(a == 0 || c / a == b); } function safeDiv(uint a, uint b) public pure returns (uint c) { require(b > 0);
    c = a / b;
}

Also is using compiler 0.5.0 still okay? Is it outdated?

Thank you.

2

Solidity had a historical issue with arithmetic overflows and underflows, that's why having SafeMath enabled was a necessity back in the day. SafeMath has been made obsolete starting from Solidity v0.8.0, as internal checking for arithmetic operations was added by default. I would suggest to always use the latest version of Solidity as possible.

1

SafeMath is used to protect your contract against math errors, such as overflow (adding uint above the max uint, for instance). SafeMath will revert the transaction if bad math happens in a transaction. It may very slightly increase the gas used for the transaction, but the benefit of safety is generally worth it.

SafeMath is no longer needed if you use Solidity version 0.8.0 and onward. It has been integrated by default in the compiler.

If you do want to use SafeMath with an older compiler, I suggest that you instead rely on a well-known library such as the one from openzeppelin and not build your own SafeMath module.

It is best practice to use the most recent version of the Solidity compiler, since security issues are fixed with compiler updates. It is not wrong to use an older version, but you should have reason to do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.