Nhảy đến nội dung chính

Gửi Token bằng ethers.js

ETHERS.JS
ERC-20
TOKEN
Người mới bắt đầu
Kim YongJun
6 tháng 4, 2021
3 số phút đọc

Gửi Token bằng ethers.js(5.0)

Trong Hướng dẫn này, bạn sẽ học cách

  • Nhập ethers.js
  • Chuyển token
  • Đặt giá gas theo tình hình lưu lượng truy cập của mạng

Để Bắt đầu

Để bắt đầu, trước tiên chúng ta phải nhập thư viện ethers.js vào javascript của mình Bao gồm ethers.js(5.0)

Cài đặt

1/home/ricmoo> npm install --save ethers

ES6 trong trình duyệt

1<script type="module">
2 import { ethers } from "https://cdn.ethers.io/lib/ethers-5.0.esm.min.js"
3 // Mã của bạn ở đây...
4</script>

ES3(UMD) trong trình duyệt

1<script
2 src="https://cdn.ethers.io/lib/ethers-5.0.umd.min.js"
3 type="application/javascript"
4></script>

Các tham số

  1. contract_address: Địa chỉ hợp đồng token (cần địa chỉ hợp đồng khi token bạn muốn chuyển không phải là ether)
  2. send_token_amount: Số tiền bạn muốn gửi cho người nhận
  3. to_address: Địa chỉ của người nhận
  4. send_account: Địa chỉ của người gửi
  5. private_key: Khóa riêng tư của người gửi để ký giao dịch và thực sự chuyển token

Lưu ý

signTransaction(tx) bị xóa vì sendTransaction() thực hiện điều đó trong nội bộ.

Thủ tục gửi

1. Kết nối với mạng (mạng thử nghiệm)

Thiết lập nhà cung cấp (Infura)

Kết nối với mạng thử nghiệm Ropsten

1window.ethersProvider = new ethers.providers.InfuraProvider("ropsten")

2. Tạo ví

1let wallet = new ethers.Wallet(private_key)

3. Kết nối Ví với mạng

1let walletSigner = wallet.connect(window.ethersProvider)

4. Lấy giá gas hiện tại

1window.ethersProvider.getGasPrice() // gasPrice

5. Xác định Giao dịch

Các biến được định nghĩa dưới đây phụ thuộc vào send_token()

Các tham số giao dịch

  1. send_account: địa chỉ của người gửi token
  2. to_address: địa chỉ của người nhận token
  3. send_token_amount: số lượng token cần gửi
  4. gas_limit: giới hạn gas
  5. gas_price: giá gas

Xem bên dưới để biết cách sử dụng

1const tx = {
2 from: send_account,
3 to: to_address,
4 value: ethers.utils.parseEther(send_token_amount),
5 nonce: window.ethersProvider.getTransactionCount(send_account, "latest"),
6 gasLimit: ethers.utils.hexlify(gas_limit), // 100000
7 gasPrice: gas_price,
8}

6. Chuyển

1walletSigner.sendTransaction(tx).then((transaction) => {
2 console.dir(transaction)
3 alert("Gửi xong!")
4})

Cách sử dụng

1let private_key =
2 "41559d28e936dc92104ff30691519693fc753ffbee6251a611b9aa1878f12a4d"
3let send_token_amount = "1"
4let to_address = "0x4c10D2734Fb76D3236E522509181CC3Ba8DE0e80"
5let send_address = "0xda27a282B5B6c5229699891CfA6b900A716539E6"
6let gas_limit = "0x100000"
7let wallet = new ethers.Wallet(private_key)
8let walletSigner = wallet.connect(window.ethersProvider)
9let contract_address = ""
10window.ethersProvider = new ethers.providers.InfuraProvider("ropsten")
11
12send_token(
13 contract_address,
14 send_token_amount,
15 to_address,
16 send_address,
17 private_key
18)
Hiện tất cả

Thành công!

hình ảnh giao dịch được thực hiện thành công

send_token()

1function send_token(
2 contract_address,
3 send_token_amount,
4 to_address,
5 send_account,
6 private_key
7) {
8 let wallet = new ethers.Wallet(private_key)
9 let walletSigner = wallet.connect(window.ethersProvider)
10
11 window.ethersProvider.getGasPrice().then((currentGasPrice) => {
12 let gas_price = ethers.utils.hexlify(parseInt(currentGasPrice))
13 console.log(`gas_price: ${gas_price}`)
14
15 if (contract_address) {
16 // gửi token chung
17 let contract = new ethers.Contract(
18 contract_address,
19 send_abi,
20 walletSigner
21 )
22
23 // Bao nhiêu token?
24 let numberOfTokens = ethers.utils.parseUnits(send_token_amount, 18)
25 console.log(`numberOfTokens: ${numberOfTokens}`)
26
27 // Gửi token
28 contract.transfer(to_address, numberOfTokens).then((transferResult) => {
29 console.dir(transferResult)
30 alert("đã gửi token")
31 })
32 } // gửi ether
33 else {
34 const tx = {
35 from: send_account,
36 to: to_address,
37 value: ethers.utils.parseEther(send_token_amount),
38 nonce: window.ethersProvider.getTransactionCount(
39 send_account,
40 "latest"
41 ),
42 gasLimit: ethers.utils.hexlify(gas_limit), // 100000
43 gasPrice: gas_price,
44 }
45 console.dir(tx)
46 try {
47 walletSigner.sendTransaction(tx).then((transaction) => {
48 console.dir(transaction)
49 alert("Gửi xong!")
50 })
51 } catch (error) {
52 alert("gửi thất bại!!")
53 }
54 }
55 })
56}
Hiện tất cả

Lần cập nhật trang lần cuối: 4 tháng 12, 2023

Hướng dẫn này có hữu ích không?