使用 ethers.js 傳送代幣
ETHERS.JS
ERC-20
TOKENS
新手
Kim YongJun
2021年4月6日
3 分鐘閱讀
使用 ethers.js (5.0) 傳送代幣
在本教學中,您將學習如何
- 匯入 ethers.js
- 傳送代幣
- 根據網路流量情況設定 gas 價格
開始使用
開始之前,我們必須先將 ethers.js 函式庫匯入 javascript 程式碼中 包含 ethers.js (5.0)
安裝
/home/ricmoo> npm install --save ethers
瀏覽器中的 ES6
<script type="module">
import { ethers } from "https://cdn.ethers.io/lib/ethers-5.0.esm.min.js"
// 您的程式碼...
</script>
瀏覽器中的 ES3 (UMD)
<script
src="https://cdn.ethers.io/lib/ethers-5.0.umd.min.js"
type="application/javascript"
></script>
參數
contract_address:代幣合約地址(當您要傳送的代幣不是 ether 時,需要合約地址)send_token_amount:您想傳送給接收者的代幣數量to_address:接收者的地址send_account:傳送者的地址private_key:傳送者的私密金鑰,用以簽署交易並實際傳送代幣
注意事項
signTransaction(tx) 已被移除,因為 sendTransaction() 會在內部處理。
傳送程序
1. 連線至網路 (測試網)
設定提供者 (Infura)
連線至 Ropsten 測試網
window.ethersProvider = new ethers.providers.InfuraProvider("ropsten")
2. 建立錢包
let wallet = new ethers.Wallet(private_key)
3 將錢包連線至網路
let walletSigner = wallet.connect(window.ethersProvider)
4 取得目前的 gas 價格
window.ethersProvider.getGasPrice() // gas 價格
5 定義交易
下方定義的變數相依於 send_token()。
交易參數
send_account:代幣傳送者的地址to_address:代幣接收者的地址send_token_amount:要傳送的代幣數量gas_limit:gas 上限gas_price:gas 價格
const tx = {
from: send_account,
to: to_address,
value: ethers.utils.parseEther(send_token_amount),
nonce: window.ethersProvider.getTransactionCount(send_account, "latest"),
gasLimit: ethers.utils.hexlify(gas_limit), // 100000
gasPrice: gas_price,
}
6. 傳送
walletSigner.sendTransaction(tx).then((transaction) => {
console.dir(transaction)
alert("傳送完成!")
})
如何使用
let private_key =
"41559d28e936dc92104ff30691519693fc753ffbee6251a611b9aa1878f12a4d"
let send_token_amount = "1"
let to_address = "0x4c10D2734Fb76D3236E522509181CC3Ba8DE0e80"
let send_address = "0xda27a282B5B6c5229699891CfA6b900A716539E6"
let gas_limit = "0x100000"
let wallet = new ethers.Wallet(private_key)
let walletSigner = wallet.connect(window.ethersProvider)
let contract_address = ""
window.ethersProvider = new ethers.providers.InfuraProvider("ropsten")
send_token(
contract_address,
send_token_amount,
to_address,
send_address,
private_key
)
成功!
send_token()
function send_token(
contract_address,
send_token_amount,
to_address,
send_account,
private_key
) {
let wallet = new ethers.Wallet(private_key)
let walletSigner = wallet.connect(window.ethersProvider)
window.ethersProvider.getGasPrice().then((currentGasPrice) => {
let gas_price = ethers.utils.hexlify(parseInt(currentGasPrice))
console.log(`gas_price: ${gas_price}`)
if (contract_address) {
// 一般代幣傳送
let contract = new ethers.Contract(
contract_address,
send_abi,
walletSigner
)
// 多少代幣?
let numberOfTokens = ethers.utils.parseUnits(send_token_amount, 18)
console.log(`numberOfTokens: ${numberOfTokens}`)
// 傳送代幣
contract.transfer(to_address, numberOfTokens).then((transferResult) => {
console.dir(transferResult)
alert("已傳送代幣")
})
} // ether 傳送
else {
const tx = {
from: send_account,
to: to_address,
value: ethers.utils.parseEther(send_token_amount),
nonce: window.ethersProvider.getTransactionCount(
send_account,
"latest"
),
gasLimit: ethers.utils.hexlify(gas_limit), // 100000
gasPrice: gas_price,
}
console.dir(tx)
try {
walletSigner.sendTransaction(tx).then((transaction) => {
console.dir(transaction)
alert("傳送完成!")
})
} catch (error) {
alert("傳送失敗!!")
}
}
})
}
頁面最後更新: 2026年3月3日
