Published: 06 Jul 2018 › Updated: 06 Jul 2018

使用 Golang 語言取得以太坊上的交易訊息:以著名的事件為例
0、要在 Windows 10 上使用 Golang 存取以太坊網路,請先完成以下設置:
https://steemit.com/go/@oneleo/windows-10-golang-rinkeby
https://steemkr.com/go/@oneleo/windows-10-golang-rinkeby
1、編譯並執行以下 Golang 程式碼:
// 設定套件名稱,若設置為 main 則為程式執行入口。
package main
// 匯入需要用的套件。
import (
"context"
"fmt"
"log"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
// main 函數為程式執行入口第一個會執行的函數
func main() {
// 連線至指定的以太坊位址:https://mainnet.infura.io
client, err := ethclient.Dial("https://mainnet.infura.io")
// 確認連線是否有誤
if err != nil {
// 若連線錯誤則將錯誤訊息紀錄後,強制結束程式執行。
log.Fatal(err)
}
// 設置要查詢的 Transaction 交易 Hash 碼。
// 這次要查詢的交易可至此查看:https://etherscan.io/tx/0x2d6a7b0f6adeff38423d4c62cd8b6ccb708ddad85da5d3d06756ad4d8a04a6a2
txHashTku := common.HexToHash("0x2d6a7b0f6adeff38423d4c62cd8b6ccb708ddad85da5d3d06756ad4d8a04a6a2")
// 從上述指定的以太坊地址找到指定的交易資訊。
txTku, _, err := client.TransactionByHash(context.Background(), txHashTku)
// 確認是否有找到指定的交易。
if err != nil {
// 若沒有找到指定的交易則將錯誤訊息紀錄後,強制結束程式執行。
log.Fatal(err)
}
// 將交易資訊中的 Input Data 取出後,轉換成文字形式並印出。
fmt.Println("【Transaction Hash】",txHashTku.Hex())
fmt.Println("【The transaction tx data string】\n", string(txTku.Data()))
fmt.Println("--------------------------------------------------")
// 設置要查詢的 Transaction 交易 Hash 碼。
// 這次要查詢的交易可至此查看:https://etherscan.io/tx/0x3ff976bb56ffd5b56284964596f07157f6b2009dec34373b70091d3eb82ab596
txHashAbs := common.HexToHash("0x3ff976bb56ffd5b56284964596f07157f6b2009dec34373b70091d3eb82ab596")
// 從上述指定的以太坊地址找到指定的交易資訊。
txAbs, _, err := client.TransactionByHash(context.Background(), txHashAbs)
// 確認是否有找到指定的交易。
if err != nil {
// 若沒有找到指定的交易則將錯誤訊息紀錄後,強制結束程式執行。
log.Fatal(err)
}
// 將交易資訊中的 Input Data 取出後,轉換成文字形式並印出。
fmt.Println("【Transaction Hash】",txHashAbs.Hex())
fmt.Println("【The transaction tx data string】\n", string(txAbs.Data()))
}
輸出:
【Transaction Hash】 0x2d6a7b0f6adeff38423d4c62cd8b6ccb708ddad85da5d3d06756ad4d8a04a6a2
【The transaction tx data string】
(…略)
--------------------------------------------------
【Transaction Hash】 0x3ff976bb56ffd5b56284964596f07157f6b2009dec34373b70091d3eb82ab596
【The transaction tx data string】
(…略)
Donate ADA:
DdzFFzCqrhsup2Q4nnhKJJZ5BRuPkYUSPqDJn72t2dtHtVqsz5kQQmopMQR16Sv9qS5NC4w8Kv5P8XrDH2n2FD2akxtrntjc8hbgAmTz
Leave 使用 Golang 語言取得以太坊上的交易訊息:以著名的事件為例 to:
Read more #go posts
Best Posts From oneleo
We have not curated any of oneleo's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From oneleo
- Solidity 智能合約 Constructor 的執行順序
- 在 Windows 內安裝單機版 Ethereum 節點 Ganache
- 【筆記】在 Windows 作業系統中建立 Node.js for ethereumjs-wallet 開發環境
- 橢圓曲線加密演算法 ECDSA 與 RFC6979 改進提案
- 詳解 HD Wallet、BIP-0032、BIP-0039、BIP-0043 及 BIP-0044
- ★【筆記】在 VHD 內安裝 Windows 10
- 【筆記】在 VHD 內安裝 Windows 10【Part 08】接下來要演示如何設置預設的開機選項,以及如何從開機選單中刪除開機選項。在這邊我們選擇進入「Windows 10 Test 02」(由 Vhd_Test_2.vhd 製作)作業系統中
- 【筆記】在 VHD 內安裝 Windows 10【Part 07】在 Vhd_Test.vhd 虛擬磁碟內完成安裝 Windows 10 後,我們可以複製 Vhd_Test.vhd 來製作第三個開機選項,並且演示如何設置開機選項的顯示名稱
- 【筆記】在 VHD 內安裝 Windows 10【Part 06】接下來電腦會自動在 MBR 分割表模式下使用 Windows 10 安裝隨身碟來開機,開始在 VHD 虛擬磁碟內安裝 Windows 10
- 【筆記】在 VHD 內安裝 Windows 10【Part 05】建立完成 VHD 檔後,因 Windows 10 預設開啟「快速啟動」功能導致無法再進入 BIOS/UEFI 畫面重新設置使用安裝隨身碟開機,故要使用特殊的方法