View Source: contracts/openzeppelin/SafeMath.sol
Wrappers over Solidity's arithmetic operations with added overflow checks.
- 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. - Using this library instead of the unchecked operations eliminates an entire class of bugs, so it's recommended to use it always.
- add(uint256 a, uint256 b)
- sub(uint256 a, uint256 b)
- sub(uint256 a, uint256 b, string errorMessage)
- mul(uint256 a, uint256 b)
- div(uint256 a, uint256 b)
- div(uint256 a, uint256 b, string errorMessage)
- divCeil(uint256 a, uint256 b)
- divCeil(uint256 a, uint256 b, string errorMessage)
- mod(uint256 a, uint256 b)
- mod(uint256 a, uint256 b, string errorMessage)
- min256(uint256 _a, uint256 _b)
Returns the addition of two unsigned integers, reverting on
overflow.
* Counterpart to Solidity's +
operator.
* Requirements:
- Addition cannot overflow.
function add(uint256 a, uint256 b) internal pure
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
a | uint256 | |
b | uint256 |
Source Code
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
Returns the subtraction of two unsigned integers, reverting on
overflow (when the result is negative).
* Counterpart to Solidity's -
operator.
* Requirements:
- Subtraction cannot overflow.
function sub(uint256 a, uint256 b) internal pure
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
a | uint256 | |
b | uint256 |
Source Code
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}
Returns the subtraction of two unsigned integers, reverting with custom message on
overflow (when the result is negative).
* Counterpart to Solidity's -
operator.
* Requirements:
- Subtraction cannot overflow.
- Available since v2.4.0.
function sub(uint256 a, uint256 b, string errorMessage) internal pure
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
a | uint256 | |
b | uint256 | |
errorMessage | string |
Source Code
function sub(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
Returns the multiplication of two unsigned integers, reverting on
overflow.
* Counterpart to Solidity's *
operator.
* Requirements:
- Multiplication cannot overflow.
function mul(uint256 a, uint256 b) internal pure
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
a | uint256 | |
b | uint256 |
Source Code
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
Returns the integer division of two unsigned integers. Reverts on
division by zero. The result is rounded towards zero.
* Counterpart to Solidity's /
operator. Note: this function uses a
revert
opcode (which leaves remaining gas untouched) while Solidity
uses an invalid opcode to revert (consuming all remaining gas).
* Requirements:
- The divisor cannot be zero.
function div(uint256 a, uint256 b) internal pure
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
a | uint256 | |
b | uint256 |
Source Code
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
}
Returns the integer division of two unsigned integers. Reverts with custom message on
division by zero. The result is rounded towards zero.
* Counterpart to Solidity's /
operator. Note: this function uses a
revert
opcode (which leaves remaining gas untouched) while Solidity
uses an invalid opcode to revert (consuming all remaining gas).
* Requirements:
- The divisor cannot be zero.
- Available since v2.4.0.
function div(uint256 a, uint256 b, string errorMessage) internal pure
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
a | uint256 | |
b | uint256 | |
errorMessage | string |
Source Code
function div(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
// Solidity only automatically asserts when dividing by 0
require(b != 0, errorMessage);
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
Integer division of two numbers, rounding up and truncating the quotient
function divCeil(uint256 a, uint256 b) internal pure
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
a | uint256 | |
b | uint256 |
Source Code
function divCeil(uint256 a, uint256 b) internal pure returns (uint256) {
return divCeil(a, b, "SafeMath: division by zero");
}
Integer division of two numbers, rounding up and truncating the quotient
function divCeil(uint256 a, uint256 b, string errorMessage) internal pure
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
a | uint256 | |
b | uint256 | |
errorMessage | string |
Source Code
function divCeil(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
// Solidity only automatically asserts when dividing by 0
require(b != 0, errorMessage);
if (a == 0) {
return 0;
}
uint256 c = ((a - 1) / b) + 1;
return c;
}
Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
Reverts when dividing by zero.
* Counterpart to Solidity's %
operator. This function uses a revert
opcode (which leaves remaining gas untouched) while Solidity uses an
invalid opcode to revert (consuming all remaining gas).
* Requirements:
- The divisor cannot be zero.
function mod(uint256 a, uint256 b) internal pure
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
a | uint256 | |
b | uint256 |
Source Code
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}
Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
Reverts with custom message when dividing by zero.
* Counterpart to Solidity's %
operator. This function uses a revert
opcode (which leaves remaining gas untouched) while Solidity uses an
invalid opcode to revert (consuming all remaining gas).
* Requirements:
- The divisor cannot be zero.
- Available since v2.4.0.
function mod(uint256 a, uint256 b, string errorMessage) internal pure
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
a | uint256 | |
b | uint256 | |
errorMessage | string |
Source Code
function mod(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
function min256(uint256 _a, uint256 _b) internal pure
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
_a | uint256 | |
_b | uint256 |
Source Code
function min256(uint256 _a, uint256 _b) internal pure returns (uint256) {
return _a < _b ? _a : _b;
}
- Address
- Administered
- AdminRole
- AdvancedToken
- AdvancedTokenStorage
- Affiliates
- AffiliatesEvents
- ApprovalReceiver
- BProPriceFeed
- CheckpointsShared
- Constants
- Context
- DevelopmentFund
- DummyContract
- EnumerableAddressSet
- EnumerableBytes32Set
- EnumerableBytes4Set
- ERC20
- ERC20Detailed
- ErrorDecoder
- Escrow
- EscrowReward
- FeedsLike
- FeesEvents
- FeeSharingCollector
- FeeSharingCollectorProxy
- FeeSharingCollectorStorage
- FeesHelper
- FourYearVesting
- FourYearVestingFactory
- FourYearVestingLogic
- FourYearVestingStorage
- GenericTokenSender
- GovernorAlpha
- GovernorVault
- IApproveAndCall
- IChai
- IContractRegistry
- IConverterAMM
- IERC1820Registry
- IERC20_
- IERC20
- IERC777
- IERC777Recipient
- IERC777Sender
- IFeeSharingCollector
- IFourYearVesting
- IFourYearVestingFactory
- IFunctionsList
- ILiquidityMining
- ILiquidityPoolV1Converter
- ILoanPool
- ILoanToken
- ILoanTokenLogicBeacon
- ILoanTokenLogicModules
- ILoanTokenLogicProxy
- ILoanTokenModules
- ILoanTokenWRBTC
- ILockedSOV
- IMoCState
- IModulesProxyRegistry
- Initializable
- InterestUser
- IPot
- IPriceFeeds
- IPriceFeedsExt
- IProtocol
- IRSKOracle
- ISovryn
- ISovrynSwapNetwork
- IStaking
- ISwapsImpl
- ITeamVesting
- ITimelock
- IV1PoolOracle
- IVesting
- IVestingFactory
- IVestingRegistry
- IWrbtc
- IWrbtcERC20
- LenderInterestStruct
- LiquidationHelper
- LiquidityMining
- LiquidityMiningConfigToken
- LiquidityMiningProxy
- LiquidityMiningStorage
- LoanClosingsEvents
- LoanClosingsLiquidation
- LoanClosingsRollover
- LoanClosingsShared
- LoanClosingsWith
- LoanClosingsWithoutInvariantCheck
- LoanInterestStruct
- LoanMaintenance
- LoanMaintenanceEvents
- LoanOpenings
- LoanOpeningsEvents
- LoanParamsStruct
- LoanSettings
- LoanSettingsEvents
- LoanStruct
- LoanToken
- LoanTokenBase
- LoanTokenLogicBeacon
- LoanTokenLogicLM
- LoanTokenLogicProxy
- LoanTokenLogicStandard
- LoanTokenLogicStorage
- LoanTokenLogicWrbtc
- LoanTokenSettingsLowerAdmin
- LockedSOV
- MarginTradeStructHelpers
- Medianizer
- ModuleCommonFunctionalities
- ModulesCommonEvents
- ModulesProxy
- ModulesProxyRegistry
- MultiSigKeyHolders
- MultiSigWallet
- Mutex
- Objects
- OrderStruct
- OrigingVestingCreator
- OriginInvestorsClaim
- Ownable
- Pausable
- PausableOz
- PreviousLoanToken
- PreviousLoanTokenSettingsLowerAdmin
- PriceFeedRSKOracle
- PriceFeeds
- PriceFeedsLocal
- PriceFeedsMoC
- PriceFeedV1PoolOracle
- ProtocolAffiliatesInterface
- ProtocolLike
- ProtocolSettings
- ProtocolSettingsEvents
- ProtocolSettingsLike
- ProtocolSwapExternalInterface
- ProtocolTokenUser
- Proxy
- ProxyOwnable
- ReentrancyGuard
- RewardHelper
- RSKAddrValidator
- SafeERC20
- SafeMath
- SafeMath96
- setGet
- SharedReentrancyGuard
- SignedSafeMath
- SOV
- sovrynProtocol
- StakingAdminModule
- StakingGovernanceModule
- StakingInterface
- StakingProxy
- StakingRewards
- StakingRewardsProxy
- StakingRewardsStorage
- StakingShared
- StakingStakeModule
- StakingStorageModule
- StakingStorageShared
- StakingVestingModule
- StakingWithdrawModule
- State
- SwapsEvents
- SwapsExternal
- SwapsImplLocal
- SwapsImplSovrynSwap
- SwapsUser
- TeamVesting
- Timelock
- TimelockHarness
- TimelockInterface
- TokenSender
- UpgradableProxy
- USDTPriceFeed
- Utils
- VaultController
- Vesting
- VestingCreator
- VestingFactory
- VestingLogic
- VestingRegistry
- VestingRegistry2
- VestingRegistry3
- VestingRegistryLogic
- VestingRegistryProxy
- VestingRegistryStorage
- VestingStorage
- WeightedStakingModule
- WRBTC