aave-protocol-v2/contracts/dependencies/openzeppelin/contracts/SafeMath.sol
2021-05-13 19:56:55 +02:00

49 lines
1.7 KiB
Solidity

// SPDX-License-Identifier: agpl-3.0
pragma solidity 0.6.12;
/// @title Optimized overflow and underflow safe math operations
/// @notice Contains methods for doing math operations that revert on overflow or underflow for minimal gas cost
library SafeMath {
/// @notice Returns x + y, reverts if sum overflows uint256
/// @param x The augend
/// @param y The addend
/// @return z The sum of x and y
function add(uint256 x, uint256 y) internal pure returns (uint256 z) {
require((z = x + y) >= x);
}
/// @notice Returns x - y, reverts if underflows
/// @param x The minuend
/// @param y The subtrahend
/// @return z The difference of x and y
function sub(uint256 x, uint256 y) internal pure returns (uint256 z) {
require((z = x - y) <= x);
}
/// @notice Returns x - y, reverts if underflows
/// @param x The minuend
/// @param y The subtrahend
/// @param message The error msg
/// @return z The difference of x and y
function sub(uint256 x, uint256 y, string memory message) internal pure returns (uint256 z) {
require((z = x - y) <= x, message);
}
/// @notice Returns x * y, reverts if overflows
/// @param x The multiplicand
/// @param y The multiplier
/// @return z The product of x and y
function mul(uint256 x, uint256 y) internal pure returns (uint256 z) {
require(x == 0 || (z = x * y) / x == y);
}
/// @notice Returns x / y, reverts if overflows - no specific check, solidity reverts on division by 0
/// @param x The numerator
/// @param y The denominator
/// @return z The product of x and y
function div(uint256 x, uint256 y) internal pure returns (uint256 z) {
return x / y;
}
}