init
This commit is contained in:
@ -1,16 +1,41 @@
|
||||
pragma solidity =0.5.16;
|
||||
|
||||
// a library for performing overflow-safe math, courtesy of DappHub (https://github.com/dapphub/ds-math)
|
||||
|
||||
/**
|
||||
* @title SafeMath 安全数学运算库
|
||||
* @notice 提供防止溢出的数学运算函数
|
||||
* @dev 来自DappHub的ds-math库 (https://github.com/dapphub/ds-math)
|
||||
* @dev 在Solidity 0.8.0之前,需要手动检查算术溢出
|
||||
*/
|
||||
library SafeMath {
|
||||
/**
|
||||
* @notice 安全加法,防止溢出
|
||||
* @param x 第一个加数
|
||||
* @param y 第二个加数
|
||||
* @return z 两数之和
|
||||
* @dev 如果结果溢出则回滚交易
|
||||
*/
|
||||
function add(uint x, uint y) internal pure returns (uint z) {
|
||||
require((z = x + y) >= x, 'ds-math-add-overflow');
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice 安全减法,防止下溢
|
||||
* @param x 被减数
|
||||
* @param y 减数
|
||||
* @return z 两数之差
|
||||
* @dev 如果结果下溢则回滚交易
|
||||
*/
|
||||
function sub(uint x, uint y) internal pure returns (uint z) {
|
||||
require((z = x - y) <= x, 'ds-math-sub-underflow');
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice 安全乘法,防止溢出
|
||||
* @param x 第一个乘数
|
||||
* @param y 第二个乘数
|
||||
* @return z 两数之积
|
||||
* @dev 如果结果溢出则回滚交易,使用除法检查溢出
|
||||
*/
|
||||
function mul(uint x, uint y) internal pure returns (uint z) {
|
||||
require(y == 0 || (z = x * y) / y == x, 'ds-math-mul-overflow');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user