【試讀版】All In One Solidity
KrypltoCampChiHaoLu
  • Welcome!
  • | Chapter 0 | Intro
    • 【推薦序】
    • 【前言】
  • Part I Basic
    • | Chapter 1 | 基本介紹 Introduction
      • 【環境建置 Remix IDE】
      • 【版本控制】
      • 【Hello World & First Contract】
      • 【Practice】
      • 【Answer】
    • | Chapter 2 | 型別 Types & 變數 Variables
      • 【Integer】
      • 【Bool】
      • 【Address】
      • 【Contract & This】
      • 【String】
      • 【Scope】
      • 【Practice】
      • 【Answer】
    • | Chapter 3 | 單位 Unit & 運算子 Operators
      • 【單位】
      • 【Time】
      • 【運算子】
      • 【Practice】
      • 【Answer】
    • | Chapter 4 | 流程控制 Selection and Repetition
      • 【If-Else】
      • 【For】
      • 【While】
      • 【Practice】
      • 【Answer】
    • | Chapter 5 | 函式 Function
      • 【Returns】
      • 【Visibility】
      • 【stateMutability】
      • 【Constructor】
      • 【Function Overloading】
      • 【Fallback】
      • 【Useful Function】
      • 【Practice】
      • 【Answer】
    • | Chapter 6 | 資料結構 Data Structures
      • 【Array】
      • 【Mapping】
      • 【Structs】
      • 【Enum】
      • 【Practice】
      • 【Answer】
    • | Chapter 7 | 角色和全局訊息 Global Variables
      • 【Msg】
      • 【Block】
      • 【ABI】
      • 【TX】
      • 【Practice】
      • 【Answer】
  • Part II Medium
    • | Chapter 8 | 記憶體配置 Memory Allocation
    • | Chapter 9 | 異常處理 Handling Exception
      • 【Require】
      • 【Assert】
      • 【Revert】
      • 【Try/Catch】
      • 【Practice】
      • 【Answer】
    • | Chapter 10 | 事件 Event
      • 【emit】
      • 【Indexed】
      • 【web3.eth.subscribe()】
      • 【Logs with Data & Topics】
      • 【anonymous】
      • 【Practice】
      • 【Answer】
    • | Chapter 11 | 繼承 Inheritance
      • 【Inheritance】
      • 【Modifier】
      • 【合約互動】
      • 【Function Overriding】
      • 【Polymorphism】
      • 【多重繼承與 super】
      • 【Practice】
      • 【Answer】
    • | Chapter 12 | 介面 Interface
    • | Chapter 13 | 引用 Imports & 函式庫 Libraries
      • 【Library】
      • 【Import】
      • 【OpenZeppelin】
      • 【Practice】
      • 【Answer】
    • | Chapter 14 | ERC & Token
  • PART III Advanced
    • | Chapter 15 | 佈署 Deploy & 編譯 Compiler
    • | Chapter 16 | 開發工具 Dev. Tools
    • | Chapter 17 | 最佳化合約 Contract Optimization
  • | OTHERS | Information & Reference
    • 【結語】
    • 【參考資料】
Powered by GitBook
On this page

Was this helpful?

  1. Part II Medium
  2. | Chapter 13 | 引用 Imports & 函式庫 Libraries

【OpenZeppelin】

Previous【Import】Next【Practice】

Last updated 3 years ago

Was this helpful?

OpenZeppelin 是一個智能合約庫,其中包含了經過社區審查的 ERC 代幣標準、安全協議以及其他輔助工具,使用 OpenZeppelin 可以極大的提高 Solidity 合約的開發效率並保證合約的安全性。最常用的合約庫如下:

  • Ownable:

    • OpenZeppelin 的 Ownable 合約提供的 onlyOwner 模式是用來保護特定合約方法的訪問權限的基礎但非常有效的模式。

  • SafeMath:

    • 這個合約會進行所有必要的檢查,避免你的程式碼因算術運算而出現漏洞,或者因為溢位等因素而出現預期外的計算結果。

  • SafeCast:

    • 當我們在做型別轉換的時候有可能會出現精度遺漏或者溢位等問題,SafeCast 可以幫我們完成這些轉換而無需擔心溢出問題。

  • ERC20/ERC721 等代幣模式

Libraries & Using ...

pragma solidity ^0.5.13;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeMath.sol";

contract LibrariesExample {
    
    using SafeMath for uint;

    mapping(address => uint) public tokenBalance;

    constructor() public {
        tokenBalance[msg.sender] = tokenBalance[msg.sender].add(1);
        // tokenBalance[msg.sender] = 1;
    }

    function sendToken(address _to, uint _amount) public returns(bool) {
        tokenBalance[msg.sender] = tokenBalance[msg.sender].sub(_amount);
        tokenBalance[_to] =  tokenBalance[_to].add(_amount); 
        // tokenBalance[msg.sender] -= _amount;
        // tokenBalance[_to] += _amount;

        return true;
    }
}

< 0.8 or >= 0.8 has differnet overfolw and underflow problem

pragma solidity >=0.4.16 <0.7.0;

library Search {
    
    function indexOf(uint[] storage self, uint value)
    public
    view
    returns (uint){
        for (uint i = 0; i < self.length; i++)
            if (self[i] == value) return i;
            return uint(-1);
        }
}

contract NotUsingForExample {
    
    uint[] data;
    
    function append(uint value) public {
        data.push(value);    
    }
    
    function replace(uint _old, uint _new) public {
    // This performs the library function call
        uint index = Search.indexOf(data,_old);
        if (index == uint(-1))
            data.push(_new);
        else
            data[index] = _new;
    }
}

contract UsingForExample {
    
    using Search for uint[];
    uint[] data;
    
    function append(uint value) public {
        data.push(value);
    }
    
    function replace(uint _old, uint _new) public {
        // This performs the library function call
        uint index = data.indexOf(_old);
        if (index == uint(-1))
            data.push(_new);
        else
            data[index] = _new;
    }
}

使用OpenZeppelin需要特別注意GitHub上的分支,尤其是版本不同在引入到智能合約時會導致編譯錯誤或各種問題。舊版的OpenZeppelin使用上也需要格外小心,因為更新版本有可能是為了修補過往的漏洞或改善品質。

OpenZeppelin