first commit
This commit is contained in:
63
contracts/lib/SafeMath.sol
Normal file
63
contracts/lib/SafeMath.sol
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 DODO ZOO.
|
||||
SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
*/
|
||||
|
||||
pragma solidity 0.6.9;
|
||||
pragma experimental ABIEncoderV2;
|
||||
|
||||
/**
|
||||
* @title SafeMath
|
||||
* @author DODO Breeder
|
||||
*
|
||||
* @notice Math operations with safety checks that revert on error
|
||||
*/
|
||||
library SafeMath {
|
||||
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
|
||||
if (a == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint256 c = a * b;
|
||||
require(c / a == b, "MUL_ERROR");
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
function div(uint256 a, uint256 b) internal pure returns (uint256) {
|
||||
require(b > 0, "DIVIDING_ERROR");
|
||||
return a / b;
|
||||
}
|
||||
|
||||
function divCeil(uint256 a, uint256 b) internal pure returns (uint256) {
|
||||
uint256 quotient = div(a, b);
|
||||
uint256 remainder = a - quotient * b;
|
||||
if (remainder > 0) {
|
||||
return quotient + 1;
|
||||
} else {
|
||||
return quotient;
|
||||
}
|
||||
}
|
||||
|
||||
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
|
||||
require(b <= a, "SUB_ERROR");
|
||||
return a - b;
|
||||
}
|
||||
|
||||
function add(uint256 a, uint256 b) internal pure returns (uint256) {
|
||||
uint256 c = a + b;
|
||||
require(c >= a, "ADD_ERROR");
|
||||
return c;
|
||||
}
|
||||
|
||||
function sqrt(uint256 x) internal pure returns (uint256 y) {
|
||||
uint256 z = (x + 1) / 2;
|
||||
y = x;
|
||||
while (z < y) {
|
||||
y = z;
|
||||
z = (x / z + z) / 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user