NFT合约解析——SafeMath.sol——2021.5.17

Posted Blockchain_Key

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NFT合约解析——SafeMath.sol——2021.5.17相关的知识,希望对你有一定的参考价值。

一丶配置需求:

1.环境需求:WeBASE-Front

2.合约语言:Solidity >=0.6.0 <0.8.0

二丶SafeMath.sol合约

pragma solidity >=0.6.0 <0.8.0;

library SafeMath {  
    function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        uint256 c = a + b;
        if (c < a) return (false, 0);
        return (true, c);
    }

    function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        if (b > a) return (false, 0);
        return (true, a - b);
    }

    function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        if (a == 0) return (true, 0);
        uint256 c = a * b;
        if (c / a != b) return (false, 0);
        return (true, c);
    }
    
    function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        if (b == 0) return (false, 0);
        return (true, a / b);
    }

    function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        if (b == 0) return (false, 0);
        return (true, a % b);
    }
    
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a, "SafeMath: addition overflow");
        return c;
    }

    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b <= a, "SafeMath: subtraction overflow");
        return a - b;
    }

    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        if (a == 0) return 0;
        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");
        return c;
    }

    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b > 0, "SafeMath: division by zero");
        return a / b;
    }

    function mod(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b > 0, "SafeMath: modulo by zero");
        return a % b;
    }

    function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b <= a, errorMessage);
        return a - b;
    }

    function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b > 0, errorMessage);
        return a / b;
    }
    
    function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b > 0, errorMessage);
        return a % b;
    }
}

三丶解析合约

(1)创建库合约

pragma solidity >=0.6.0 <0.8.0;

library SafeMath {  

申明solidity版本在0.6-0.8之间编译
library关键字创建SafeMath库合约

(2)Try加减乘除取余

    function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        uint256 c = a + b;
        if (c < a) return (false, 0);
        return (true, c);
    }

    function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        if (b > a) return (false, 0);
        return (true, a - b);
    }

    function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        if (a == 0) return (true, 0);
        uint256 c = a * b;
        if (c / a != b) return (false, 0);
        return (true, c);
    }
    
    function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        if (b == 0) return (false, 0);
        return (true, a / b);
    }

    function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        if (b == 0) return (false, 0);
        return (true, a % b);
    }

参数:uint256 a b进行加减乘除取余操作
返回值:布尔,uint256的结果
正常四则运算判断,符合条件返回true和正确的结果,反之false和0

(3)加减乘除取余操作

  function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a, "SafeMath: addition overflow");
        return c;
    }

    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b <= a, "SafeMath: subtraction overflow");
        return a - b;
    }

    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        if (a == 0) return 0;
        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");
        return c;
    }

    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b > 0, "SafeMath: division by zero");
        return a / b;
    }

    function mod(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b > 0, "SafeMath: modulo by zero");
        return a % b;
    }

满足try之后,返回true可进行此方法调用

(3)重载减,除,取余方法(返回错误值)

   function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b <= a, errorMessage);
        return a - b;
    }

    function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b > 0, errorMessage);
        return a / b;
    }
    
    function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b > 0, errorMessage);
        return a % b;
    }

try为false之后,可调用此方法

四丶上一篇:NFT合约解析(2)——Ownable.sol

NFT合约解析(2)——Ownable.sol

五丶下一篇:NFT合约解析(4)——Counters.sol

NFT合约解析(4)——Counters.sol

六丶参考相关文章

solidity笔记(1)——第一篇
solidity笔记(2)——第二篇
solidity笔记(3)——abstract用法
solidity笔记(4)——冻结和交易属性
solidity笔记(5)——event用法
solidity笔记(6)——modifier用法
solidity笔记(7)——存储区域memory storage stack
solidity笔记(8)——pure用法
solidity笔记(9)——library用法

以上是关于NFT合约解析——SafeMath.sol——2021.5.17的主要内容,如果未能解决你的问题,请参考以下文章

NFT合约解析——Counters.sol——2021.5.19

Openzeppelin学习记录二:utils模块(SafeMath.sol+SafeCast.sol)

Openzeppelin学习记录二:utils模块(SafeMath.sol+SafeCast.sol)

Openzeppelin学习记录二:utils模块(SafeMath.sol+SafeCast.sol)

Openzeppelin学习记录二:utils模块(SafeMath.sol+SafeCast.sol)

solidity代理合约