Ethers.js 备忘单
ethers.js是一个与以太坊区块链交互的库。
它非常有用,但官方文档对我来说有点难读,所以我想总结一下,方便参考。(重点介绍那些经常用到的内容。)
*按字母顺序排列。
账户
获取账户列表
const accounts = await provider.listAccounts();
例子:
// Connect web3
const provider = new ethers.providers.Web3Provider(window.ethereum);
const accounts = await provider.listAccounts();
console.log(accounts[0]);
平衡
获取地址平衡
const balance = await provider.getBalance(`address`);
例子:
// Connect web3
const provider = new ethers.providers.Web3Provider(window.ethereum);
const address = "0x28d3...";
const balance = await provider.getBalance(address);
console.log(`The ${address} balance: ${balance.toString()}`);
连接(MetaMask)
使用 MetaMask 连接到以太坊
const provider = new ethers.provider.Web3Provider(window.ethereum);
连接(RPC)
使用 RPC 连接到以太坊
const provider = new ethers.provider.JsonRpcProvider(`url`);
url
例如:
平台 | 网址 |
---|---|
炼金术 | https://<network>.alchemyapi.io/v2/YOUR-API-KEY |
因弗拉 | https://<network>.infura.io/v3/YOUR-PROJECT-ID |
合同
由签名者创建合约实例。
如果用户没有钱包或未连接,则无法使用。
const contract = new ethers.Contract(`address`, `abi`, `signer`);
例子:
import Artifact from './Contract.json';
// Connect web3
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contractAddress = "0x9fE4...";
const contract = new ethers.Contract(
contractAddress,
Artifact.abi,
signer
);
// Call a state-change method
const userAddress = "0x28d3...";
const dai = ethers.utils.parseUnits("1.0", 18);
await contract.transfer(userAddress, dai);
合同(只读)
通过提供者创建合约实例。
它只能调用只读方法。即使用户没有钱包或未连接,它也同样有效。
const contract = new ethers.Contract(`address`, `abi`, `provider`);
例子:
import Artifact from './Contract.json';
// For example here, interact with Alchemy JSON-RPC
const provider = new ethers.providers.JsonRpcProvider("https://eth-mainnet.alchemyapi.io/v2/<YOUR-API-KEY>");
const contractAddress = "0x9fE4...";
const contract = new ethers.Contract(
contractAddress,
Artifact.abi,
provider
);
// Call a getter method
const contractName = await contract.name();
console.log(`Contract name is ${contractName}`);
合约事件监听器
监听合同中发出的事件。
contract.on(`event`, `listener`);
例子:
contract.on("TransferedFrom", (from, to) => {
console.log(`Token transfered from ${from} to ${to}`);
});
contract.on("Minted", (tokenId) => {
console.log(`Token #${tokenId} minted`);
});
转换 (以太币 -> 维)
返回BigNumber
。
const wei = ethers.utils.parseEther(`ETH`);
例子:
const weiBigNumber = ethers.utils.parseEther("0.2");
const wei = weiBigNumber.toString();
console.log("wei: ", wei);
转换 (Wei -> Ether)
返回string
。
const ether = ethers.utils.formatEther(`wei`);
例子:
const address = "0x28d319067E209fa43Ef46bF54343Dae4CEDd3824";
const balanceBigNumber = await ethers.providers.getBalance(address);
const balance = ethers.utils.formatEther(balanceBigNumber.toString());
console.log(`user balance: ${balance} Ether`);
安装
npm install ethers
进口
对于CommonJS
const { ethers } = require('ethers');
ES模块
import { ethers } from 'ethers';
网络和链 ID
获取连接网络和链 ID。
const network = await provider.getNetwork();
const chainId = network.chainId;
例子:
// Connect web3
const provider = new ethers.providers.Web3Provider(window.ethereum);
const network = await provider.getNetwork();
const chainId = network.chainId;
链ID列表例如:
链ID | 网络 |
---|---|
1 | 主网 |
3 | 罗普斯滕 |
4 | 林克比 |
5 | 戈利 |
10 | 乐观 |
四十二 | 科万 |
56 | 平衡记分卡 |
137 | 多边形 |
42161 | 仲裁一号 |
43114 | 雪崩 |