SafeMath diff

Created Diff never expires
23 हटाए गए
लाइनें
कुल
हटाया गया
शब्द
कुल
हटाया गया
इस सुविधा का उपयोग जारी रखने के लिए, अपग्रेड करें
Diffchecker logo
Diffchecker Pro
227 लाइनें
8 जोड़े गए
लाइनें
कुल
जोड़ा गया
शब्द
कुल
जोड़ा गया
इस सुविधा का उपयोग जारी रखने के लिए, अपग्रेड करें
Diffchecker logo
Diffchecker Pro
215 लाइनें
// SPDX-License-Identifier: MIT
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/math/SafeMath.sol)
// OpenZeppelin Contracts (last updated v4.9.0) (utils/math/SafeMath.sol)


pragma solidity ^0.8.0;
pragma solidity ^0.8.0;


// CAUTION
// CAUTION
// This version of SafeMath should only be used with Solidity 0.8 or later,
// This version of SafeMath should only be used with Solidity 0.8 or later,
// because it relies on the compiler's built in overflow checks.
// because it relies on the compiler's built in overflow checks.


/**
/**
* @dev Wrappers over Solidity's arithmetic operations.
* @dev Wrappers over Solidity's arithmetic operations.
*
*
* NOTE: `SafeMath` is generally not needed starting with Solidity 0.8, since the compiler
* NOTE: `SafeMath` is generally not needed starting with Solidity 0.8, since the compiler
* now has built in overflow checking.
* now has built in overflow checking.
*/
*/
library SafeMath {
library SafeMath {
/**
/**
* @dev Returns the addition of two unsigned integers, with an overflow flag.
* @dev Returns the addition of two unsigned integers, with an overflow flag.
*
*
* _Available since v3.4._
* _Available since v3.4._
*/
*/
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
unchecked {
uint256 c = a + b;
uint256 c = a + b;
if (c < a) return (false, 0);
if (c < a) return (false, 0);
return (true, c);
return (true, c);
}
}
}
}


/**
/**
* @dev Returns the substraction of two unsigned integers, with an overflow flag.
* @dev Returns the subtraction of two unsigned integers, with an overflow flag.
*
*
* _Available since v3.4._
* _Available since v3.4._
*/
*/
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
unchecked {
if (b > a) return (false, 0);
if (b > a) return (false, 0);
return (true, a - b);
return (true, a - b);
}
}
}
}


/**
/**
* @dev Returns the multiplication of two unsigned integers, with an overflow flag.
* @dev Returns the multiplication of two unsigned integers, with an overflow flag.
*
*
* _Available since v3.4._
* _Available since v3.4._
*/
*/
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
unchecked {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) return (true, 0);
if (a == 0) return (true, 0);
uint256 c = a * b;
uint256 c = a * b;
if (c / a != b) return (false, 0);
if (c / a != b) return (false, 0);
return (true, c);
return (true, c);
}
}
}
}


/**
/**
* @dev Returns the division of two unsigned integers, with a division by zero flag.
* @dev Returns the division of two unsigned integers, with a division by zero flag.
*
*
* _Available since v3.4._
* _Available since v3.4._
*/
*/
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
unchecked {
if (b == 0) return (false, 0);
if (b == 0) return (false, 0);
return (true, a / b);
return (true, a / b);
}
}
}
}


/**
/**
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
*
*
* _Available since v3.4._
* _Available since v3.4._
*/
*/
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
unchecked {
if (b == 0) return (false, 0);
if (b == 0) return (false, 0);
return (true, a % b);
return (true, a % b);
}
}
}
}


/**
/**
* @dev Returns the addition of two unsigned integers, reverting on
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
* overflow.
*
*
* Counterpart to Solidity's `+` operator.
* Counterpart to Solidity's `+` operator.
*
*
* Requirements:
* Requirements:
*
*
* - Addition cannot overflow.
* - Addition cannot overflow.
*/
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
function add(uint256 a, uint256 b) internal pure returns (uint256) {
return a + b;
return a + b;
}
}


/**
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
* overflow (when the result is negative).
*
*
* Counterpart to Solidity's `-` operator.
* Counterpart to Solidity's `-` operator.
*
*
* Requirements:
* Requirements:
*
*
* - Subtraction cannot overflow.
* - Subtraction cannot overflow.
*/
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return a - b;
return a - b;
}
}


/**
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
* overflow.
*
*
* Counterpart to Solidity's `*` operator.
* Counterpart to Solidity's `*` operator.
*
*
* Requirements:
* Requirements:
*
*
* - Multiplication cannot overflow.
* - Multiplication cannot overflow.
*/
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
return a * b;
return a * b;
}
}


/**
/**
* @dev Returns the integer division of two unsigned integers, reverting on
* @dev Returns the integer division of two unsigned integers, reverting on
* division by zero. The result is rounded towards zero.
* division by zero. The result is rounded towards zero.
*
*
* Counterpart to Solidity's `/` operator.
* Counterpart to Solidity's `/` operator.
*
*
* Requirements:
* Requirements:
*
*
* - The divisor cannot be zero.
* - The divisor cannot be zero.
*/
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return a / b;
return a / b;
}
}


/**
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting when dividing by zero.
* reverting when dividing by zero.
*
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
* invalid opcode to revert (consuming all remaining gas).
*
*
* Requirements:
* Requirements:
*
*
* - The divisor cannot be zero.
* - The divisor cannot be zero.
*/
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return a % b;
return a % b;
}
}


/**
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
* overflow (when the result is negative).
*
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {trySub}.
* message unnecessarily. For custom revert reasons use {trySub}.
*
*
* Counterpart to Solidity's `-` operator.
* Counterpart to Solidity's `-` operator.
*
*
* Requirements:
* Requirements:
*
*
* - Subtraction cannot overflow.
* - Subtraction cannot overflow.
*/
*/
function sub(
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
unchecked {
unchecked {
require(b <= a, errorMessage);
require(b <= a, errorMessage);
return a - b;
return a - b;
}
}
}
}


/**
/**
* @dev Returns the integer division of two unsigned integers, reverting with custom message on
* @dev Returns the integer division of two unsigned integers, reverting with custom message on
* division by zero. The result is rounded towards zero.
* division by zero. The result is rounded towards zero.
*
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
* uses an invalid opcode to revert (consuming all remaining gas).
*
*
* Requirements:
* Requirements:
*
*
* - The divisor cannot be zero.
* - The divisor cannot be zero.
*/
*/
function div(
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
unchecked {
unchecked {
require(b > 0, errorMessage);
require(b > 0, errorMessage);
return a / b;
return a / b;
}
}
}
}


/**
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting with custom message when dividing by zero.
* reverting with custom message when dividing by zero.
*
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryMod}.
* message unnecessarily. For custom revert reasons use {tryMod}.
*
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
* invalid opcode to revert (consuming all remaining gas).
*
*
* Requirements:
* Requirements:
*
*
* - The divisor cannot be zero.
* - The divisor cannot be zero.
*/
*/
function mod(
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
unchecked {
unchecked {
require(b > 0, errorMessage);
require(b > 0, errorMessage);
return a % b;
return a % b;
}
}
}
}
}
}