mirror of
https://github.com/Instadapp/aave-protocol-v2.git
synced 2024-07-29 21:47:30 +00:00
feat: replaced SafeMath library
This commit is contained in:
parent
9d12200b8c
commit
5d003429c7
|
@ -1,163 +1,49 @@
|
||||||
// SPDX-License-Identifier: agpl-3.0
|
// SPDX-License-Identifier: agpl-3.0
|
||||||
pragma solidity 0.6.12;
|
pragma solidity 0.6.12;
|
||||||
|
|
||||||
/**
|
/// @title Optimized overflow and underflow safe math operations
|
||||||
* @dev Wrappers over Solidity's arithmetic operations with added overflow
|
/// @notice Contains methods for doing math operations that revert on overflow or underflow for minimal gas cost
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
library SafeMath {
|
library SafeMath {
|
||||||
/**
|
/// @notice Returns x + y, reverts if sum overflows uint256
|
||||||
* @dev Returns the addition of two unsigned integers, reverting on
|
/// @param x The augend
|
||||||
* overflow.
|
/// @param y The addend
|
||||||
*
|
/// @return z The sum of x and y
|
||||||
* Counterpart to Solidity's `+` operator.
|
function add(uint256 x, uint256 y) internal pure returns (uint256 z) {
|
||||||
*
|
require((z = x + y) >= x);
|
||||||
* Requirements:
|
|
||||||
* - Addition cannot overflow.
|
|
||||||
*/
|
|
||||||
function add(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
||||||
uint256 c = a + b;
|
|
||||||
require(c >= a, 'SafeMath: addition overflow');
|
|
||||||
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev 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) {
|
|
||||||
return sub(a, b, 'SafeMath: subtraction overflow');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev 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.
|
|
||||||
*/
|
|
||||||
function sub(
|
|
||||||
uint256 a,
|
|
||||||
uint256 b,
|
|
||||||
string memory errorMessage
|
|
||||||
) internal pure returns (uint256) {
|
|
||||||
require(b <= a, errorMessage);
|
|
||||||
uint256 c = a - b;
|
|
||||||
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev 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) {
|
|
||||||
// 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;
|
/// @notice Returns x - y, reverts if underflows
|
||||||
require(c / a == b, 'SafeMath: multiplication overflow');
|
/// @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);
|
||||||
|
}
|
||||||
|
|
||||||
return c;
|
/// @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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev 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) {
|
|
||||||
return div(a, b, 'SafeMath: division by zero');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/// @notice Returns x * y, reverts if overflows
|
||||||
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on
|
/// @param x The multiplicand
|
||||||
* division by zero. The result is rounded towards zero.
|
/// @param y The multiplier
|
||||||
*
|
/// @return z The product of x and y
|
||||||
* Counterpart to Solidity's `/` operator. Note: this function uses a
|
function mul(uint256 x, uint256 y) internal pure returns (uint256 z) {
|
||||||
* `revert` opcode (which leaves remaining gas untouched) while Solidity
|
require(x == 0 || (z = x * y) / x == y);
|
||||||
* uses an invalid opcode to revert (consuming all remaining gas).
|
}
|
||||||
*
|
|
||||||
* Requirements:
|
|
||||||
* - The divisor cannot be zero.
|
|
||||||
*/
|
|
||||||
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;
|
/// @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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
}
|
||||||
* @dev 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) {
|
|
||||||
return mod(a, b, 'SafeMath: modulo by zero');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev 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.
|
|
||||||
*/
|
|
||||||
function mod(
|
|
||||||
uint256 a,
|
|
||||||
uint256 b,
|
|
||||||
string memory errorMessage
|
|
||||||
) internal pure returns (uint256) {
|
|
||||||
require(b != 0, errorMessage);
|
|
||||||
return a % b;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user