Add the SafeMath Library safeguard Mathematical Operations¶
Directly from the SafeMath Library source code:
Arithmetic operations in Solidity wrap on overflow. This can easily result in bugs, because programmers usually assume that an overflow raises an error, which is the standard behavior in high level programming languages.
SafeMath
restores this intuition by reverting the transaction when an operation overflows.
Solidity changed
In a recent update of Solidity the Integer type variables cannot overflow anymore. Read more about the following Solidity 0.8 release notes!.
Add the following code only if you are using solidity < 0.8!!!
pragma solidity ^0.6.1;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/contracts/math/SafeMath.sol";
contract Allowance is Ownable {
using SafeMath for uint;
event AllowanceChanged(address indexed _forWho, address indexed _byWhom, uint _oldAmount, uint _newAmount);
mapping(address => uint) public allowance;
function isOwner() internal view returns(bool) {
return owner() == msg.sender;
}
function setAllowance(address _who, uint _amount) public onlyOwner {
//...
}
modifier ownerOrAllowed(uint _amount) {
//...
}
function reduceAllowance(address _who, uint _amount) internal ownerOrAllowed(_amount) {
emit AllowanceChanged(_who, msg.sender, allowance[_who], allowance[_who].sub(_amount));
allowance[_who] = allowance[_who].sub(_amount);
}
}
contract SharedWallet is Allowance {
//...
}