心智圖資源庫 ethers.js
web3 前端必用的函式庫,ethers中的provider是對eth網路唯讀連線的抽象。可以讀取區塊鏈狀態例如帳戶、區塊、交易資訊、event logs、透過call執行唯讀程式碼。
編輯於2024-11-25 18:55:18This template shows the structure and function of the reproductive system in the form of a mind map. It introduces the various components of the internal and external genitals, and sorts out the knowledge clearly to help you become familiar with the key points of knowledge.
This is a mind map about the interpretation and summary of the relationship field e-book, Main content: Overview of the essence interpretation and overview of the relationship field e-book. "Relationship field" refers to the complex interpersonal network in which an individual influences others through specific behaviors and attitudes.
This is a mind map about accounting books and accounting records. The main contents include: the focus of this chapter, reflecting the business results process of the enterprise, the loan and credit accounting method, and the original book of the person.
This template shows the structure and function of the reproductive system in the form of a mind map. It introduces the various components of the internal and external genitals, and sorts out the knowledge clearly to help you become familiar with the key points of knowledge.
This is a mind map about the interpretation and summary of the relationship field e-book, Main content: Overview of the essence interpretation and overview of the relationship field e-book. "Relationship field" refers to the complex interpersonal network in which an individual influences others through specific behaviors and attitudes.
This is a mind map about accounting books and accounting records. The main contents include: the focus of this chapter, reflecting the business results process of the enterprise, the loan and credit accounting method, and the original book of the person.
ethers.js
概念
Provider
ethers中的provider是對eth網路唯讀連線的抽象。可以讀取區塊鏈狀態例如帳戶、區塊、交易資訊、event logs、透過call執行唯讀程式碼
Signer
封裝了所有對帳戶的操作。
帳戶有一個私鑰存在某個地方
要么透過使用wallet存在內存中
要麼透過IPC層保護(例如metamask透過瀏覽器插件代理了網頁相關操作,保護私鑰不被網頁直接訪問,只有使用者允許的操作才會被執行
Contract
是部署到區塊鏈中的程序,包含程式碼和分配的可讀寫儲存區
當連接Provider後可以做讀取操作
當連接Signer後可以做修改操作
Transaction
對於任何對區塊鏈上狀態修改的都需要transaction
需要fee來做計算更新儲存資訊操作
transaction回滾也需要支付fee,驗證者需要嘗試執行交易來確認回滾以及儲存失敗回滾原因
分類
ether轉帳
部署合約
修改合約狀態
Receipt
當transaction提交後,會在memory pool 中排隊等待Validator確認是否處理該交易
transaction的修改只會在打包到區塊鏈中時執行一次,同時會產生receipt,receipt中包含詳細的transaction信息,包含所在區塊、實際使用的fee、gas、產生的events、是否成功/回滾
單位
wei
最小單位
以Wei Dai命名,比特幣前身b-money創作者
gwei
使用最多的單位,gas費一般使用gwei單位
ENS
Ethereum Name Service。有點像dns,用於將方便閱讀的名字例如"alice.eth"映射到eth地址/加密貨幣地址/內容hash/元資料。
應用
連接eth
連接metamask/其他注入方式provider
metamask外掛程式會在window下注入對象
對eth的唯讀權限(provider
透過私鑰進行寫入操作授權(signer
當進行需要授權的寫入操作或要求私鑰位址時,metamask會彈出視窗讓使用者確認
連線自訂rpc,例如開發節點或第三方服務
直接用JsonRpcProvider(遵循link-jsonrpc 協定)
JsonRpCProvider.getSigner連接帳戶
單位轉換
與區塊鏈交互
讀狀態
前提:需要Provider
目前帳戶狀態
歷史日誌
合約代碼
相關操作
發送transaction
前提:需要Signer
例如向MetaMask發送請求,metamask中可以選擇同意或拒絕操作
合約
是一個meta-class,運行時基於傳入的ABI動態生成
創建合約
abi
與以太坊網路互動需要二進位
多種表示形式
solidity compiler使用JSON轉儲
方便人類閱讀的 solidity 函數簽名
"function decimals() view returns (string)"
只讀方法。 pure 和 view
狀態修改方法
監聽事件
查詢歷史事件
如果查詢大範圍的區塊鏈可能會非常非常慢甚至報錯或未提示的截斷,由其後端決定
簽名訊息
問題
1. fee和gas有差別嗎?
2. Provider、metamask錢包、ethers的關係
Provider是eth的連線提供者,metamask有內建的provider,ethers可以透過使用metamask內建的provider對eth進行唯讀查詢
3. 單位之間的換算
學習資料
https://www.wtf.academy/docs/ethers-101/
https://docs.ethers.org/v6/getting-started/#starting-blockchain