【試讀版】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

【Library】

Library 非常類似於 contracts,但我們不能在裡面宣告任何的 state variables 而且我們也不可以傳送任何 ether。

使用範例:

pragma solidity ^0.8.11;

library Lib {  
  function add(uint a, uint b) pure internal returns(uint) {
    return a + b;
  }
}

contract A {
  using Lib for uint;

  function add(uint a, uint b) pure external returns(uint) {
    return a.add(b);
  }
}

在 Solidity 裡面有兩種 Libraries 類型:

  • deployed:有自己的合約地址,可以被智能合約在執行時調用

  • embedded:當所有的 library 函數皆為 internal;沒有自己的合約地址,會變成我們合約的一部分程式碼。

// Embedded (function is internal)
library Lib_Embedded {  
  function add(uint a, uint b) pure internal returns(uint) {
    return a + b;
  }
}

//Deployed (function is public)
library Lib_Deployed {  
  function add(uint a, uint b) pure public returns(uint) {
    return a + b;
  }
}

Libraries & Using ... for

  • Libraries使用上有點類似Contracts

  • 程式碼運用了DELEGATECALL 的特性可以被重複使用

  • Libraries運作上類似直接從「呼叫引入處」貼上來源程式碼至合約內

  • Libraries不具有自我摧毀的功能,因此從定義上不能被摧毀

  • 相關限制(在未來版本有可能會改變)

    • 不具有狀態變數(State variables)

    • 不可進行繼承和被繼承

    • 不可接收Ether

Previous| Chapter 13 | 引用 Imports & 函式庫 LibrariesNext【Import】

Last updated 3 years ago

Was this helpful?