Eth中,Log和Event是一回事,我們往往將要寫入?yún)^(qū)塊鏈的方法九妈,前面加上 Event 來實現(xiàn)反砌,所以我們可以通過filter日志來查詢交易記錄
eip-155定義的eth 的chainID
CHAIN_ID | Chain(s) |
---|---|
1 | Ethereum mainnet |
2 | Morden (disused), Expanse mainnet |
3 | Ropsten |
4 | Rinkeby |
5 | Goerli |
42 | Kovan |
1337 | Geth private chains (default) |
ethers.utils.HDNode.isValidMnemonic(): 驗證助記詞是否有效
ethers.utils.getAddress():轉(zhuǎn)換為checkSum地址
1. send方法后面可寫入rpc調(diào)用方法名,后面跟參數(shù)
personal_importRawKey
let lockStatus = await rpcProvider.send("personal_lockAccount", address)
2.查詢token余額
const getBalance_USDT = async () => {
const contract = new ethers.Contract(USDTaddress, USDTabi, rpcProvider);
const balance = await contract.balanceOf(address);
return balance.toString();
};
//6為token有效小數(shù)位萌朱。查到的余額除以有效小數(shù)位才是實際余額
let actuBalUSDT = ethers.utils.formatUnits(queryBalUSDT, 6)
3.查詢eth余額
let pendingBal = await rpcProvider.getBalance(address, "pending")
查詢不同狀態(tài)的余額”latest”(已經(jīng)確認了的), “earliest”(創(chuàng)世區(qū)塊的) 宴树, “pending”(包含未確認的交易的余額)
4.utils
//去除地址前面的0位
let address = ethers.utils.hexStripZeros(addressQuery)
// 相當于從wei到ether
let numberOfDecimals = 18;
let BNBbal = ethers.utils.parseUnits(queryBalBNB, numberOfDecimals);
5.發(fā)送eth交易
let wallet = new ethers.Wallet(privateKey, provider);
let amount = ethers.utils.parseEther(amount);
//let nonce = await rpcProvider.getTransactionCount(address, "pending") //自己搭建節(jié)點時適用。
let nonce = await wallet.getTransactionCount();
let gasPrice=await provider.getGasPrice();
let tx = {
nonce: nonce,
gasLimit: 21000,
gasPrice: ethers.utils.bigNumberify(gasPrice),
to: toAddress,
chainId: chainId,
value: amount,
data: ""
};
// let signTx = await wallet.sign(tx)
// let resp = await rpcProvider.sendTransaction(signTx)
或
let resp = await wallet.sendTransaction(tx);
6.發(fā)送token交易
let numberOfTokens = ethers.utils.parseUnits(amount, decims);
// 先計算transfer 需要的gas 消耗量晶疼,這一步有默認值酒贬,非必須。
let gas = await contract.estimate.transfer(toAddress, numberOfTokens)
let gasP = await rpcProvider.getGasPrice()
// 必須關聯(lián)一個有過簽名錢包對象
let contractWithSigner = contract.connect(wallet);
// 發(fā)起交易翠霍,前面2個參數(shù)是函數(shù)的參數(shù)锭吨,第3個是交易參數(shù)
let tx = await contractWithSigner.transfer(toAddress, numberOfTokens, {
nonce: nonce,
gasLimit: gas,
gasPrice: ethers.utils.bigNumberify(gasP) ,
chainId: chainId
})
7.獲取節(jié)點所有賬戶的余額
async function checkAllBalances() {
let e = await rpcProvider.listAccounts()
for (let i = 0; i < e.length; i++) {
let bal = await rpcProvider.getBalance(e[i])
let balance = ethers.utils.formatEther(bal)
console.log(`${e[i]}: `+balance)
}
}
checkAllBalances();
8. 單位轉(zhuǎn)換
// 轉(zhuǎn)換cost為 wei單位的BigNumber類型
const costWei = ethers.utils.bigNumberify(21000).mul('0x3b9aca00')
console.log(costWei);
// 轉(zhuǎn)換cost為ether單位的一般表示(可讀的10進制)
const costEther = ethers.utils.formatEther(costWei)
console.log(costEther);
// 轉(zhuǎn)換cost為ether單位的BigNumber類型
const costBigNumber = ethers.utils.parseEther(costEther);
console.log(costBigNumber);
// 余額減去花費作為 新的轉(zhuǎn)入余額,
const amount = pendingBal.sub(costBigNumber)
console.log(ethers.utils.formatEther(amount));