【試讀版】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 10 | 事件 Event

【web3.eth.subscribe()】

這邊為了更了解 event 的運作,首先我們要來到 web3.js 來看 web3.eth.subscribe() 的運作。web3.eth.subscribe() 讓我們可以聆聽區塊鏈上合約的特定事件。

web3.eth.subscribe(type [, options] [, callback]);
  • type: 輸入型態為 String

    • 我們想要聆聽的類型。

  • options: 輸入型態為 Mixed

    • (optional) 根據想要聆聽的類型(type)來決定這邊選填的內容是什麼。

  • callback: 輸入型態為 Function

    • (optional) 回傳的第一個參數為 error 物件,第二個參數為 result 物件,第三個參數為聆聽類型(subscription)自己

呼叫完 web3.eth.subscribe() 後回傳的物件為 EventEmitter,內容如下:

  • subscription.id: subscription 的 id,可以用來辨識或者取消聆聽此 subscription

  • subscription.subscribe([callback]): 可以使用同樣的參數來重新聆聽(re-subscribe)

  • subscription.unsubscribe([callback]): 取消聆聽這個(Unsubscribes)subscription 並且在成功時回傳 TRUE

  • subscription.arguments: subscription 的參數(arguments),可以被用來 re-subscribing.

  • on("data") returns Object: 以 log 物件當作參數執行每個即將發生的 log

  • on("changed") returns Object: 執行每個從區塊鏈被移除的 log。log 會有額外的屬性 "removed: true"

  • on("error") returns Object: 聆聽過程中如果出現錯誤就會執行

  • on("connected") returns String: 聆聽成功後就會回傳 subscription id

接下來我們講解 web3.eth.subscribe() 裡面 type 參數的重點聆聽類型 'logs',當然 type 參數還有很多其他的選擇,這些等到介紹 web3.js 的時候再專門講解。

web3.eth.subscribe('logs', options [, callback]);

使用給定的 options 聆聽 logs。

參數 Parameters:

  • "logs" - String,聆聽的類型

  • Object - 聆聽類型的 options

    • fromBlock - Number: 更早的區塊數量,預設為 null

    • address - String|Array: 一個儲存 address 的陣列或一個 address 來得到某個特定帳號的 logs

    • topics - Array: 一個在 log 裡面出現的數值陣列,其順序是有意義的,如果我們希望不要使用某個元素可以使用 null,e.g. [null, '0x00...']。

  • callback - Function: (optional) 回傳的第一個參數為 error 物件,第二個參數為 result 物件

回傳值 Returns:

  • EventEmitter:

    • "data" returns Object: 以 log 物件當作參數執行每個即將發生的 log

    • "changed" returns Object: 執行每個從區塊鏈被移除的 log。log 會有額外的屬性 "removed: true"

    • "error" returns Object: 聆聽過程中如果出現錯誤就會執行

    • "connected" returns Number: 聆聽成功後就會回傳 subscription id

範例:

var subscription = web3.eth.subscribe('logs', {
    address: '0x123456..',
    topics: ['0x12345...']
}, function(error, result){
    if (!error)
        console.log(result);
})
.on("connected", function(subscriptionId){
    console.log(subscriptionId);
})
.on("data", function(log){
    console.log(log);
})
.on("changed", function(log){
});

// unsubscribes the subscription
subscription.unsubscribe(function(error, success){
    if(success)
        console.log('Successfully unsubscribed!');
});
Previous【Indexed】Next【Logs with Data & Topics】

Last updated 3 years ago

Was this helpful?

官方文件