Ethers.js 使用
單位 Ether 轉 Wei
return bigint (e.g. 12000000n)
ethers.utils.parseUnits("0.11", "ether")單位 Wei 轉 Ether
return string (ether string)
ethers.utils.formatEther(balance);讀取地址餘額:
const { ethers } = require("ethers");
let provider = new ethers.providers.InfuraProvider('rinkeby');
let address = "0x00....";
provider.getBalance(address).then((balance) => {
let etherString = ethers.utils.formatEther(balance);
console.log("Balance: " + etherString);
});執行合約
讀取合約:
前端使用 sendTransaction 與 signMessage
從後端呼叫 function
後端所以需要直接填入私鑰
Call static
通常會用在於獲取執行 function 的回傳值
JSON 內有 Bigint 結構轉換
例如出現:TypeError: Do not know how to serialize a BigInt
可以使用以下方式
Last updated
Was this helpful?