Posílání tokenů pomocí ethers.js
ETHERS.JS
ERC-20
TOKENY
Začátečník
Kim YongJun
6. dubna 2021
2 minuta čtení
Odeslání tokenu pomocí ethers.js(5.0)
V tomto tutoriálu se naučíte
- Importovat ethers.js
- Převést token
- Nastavit cenu transakčních poplatků podle situace v síti
Než začnete
Abyste mohli začít, musíme nejprve importovat knihovnu ethers.js do našeho javascriptu Zahrňte ethers.js(5.0)
Instalace
/home/ricmoo> npm install --save ethers
ES6 v prohlížeči
<script type="module">
import { ethers } from "https://cdn.ethers.io/lib/ethers-5.0.esm.min.js"
// Váš kód zde...
</script>
ES3(UMD) v prohlížeči
<script
src="https://cdn.ethers.io/lib/ethers-5.0.umd.min.js"
type="application/javascript"
></script>
Parametry
contract_address: Adresa kontraktu tokenu (adresa kontraktu je potřeba, když token, který chcete převést, není ether)send_token_amount: Částka, kterou chcete poslat příjemcito_address: Adresa příjemcesend_account: Adresa odesílateleprivate_key: Soukromý klíč odesílatele k podepsání transakce a skutečnému převodu tokenů
Upozornění
signTransaction(tx) je odstraněno, protože sendTransaction() to dělá interně.
Postupy odesílání
1. Připojit se k síti (testnet)
Nastavit poskytovatele (Infura)
Připojit se k Ropsten testnetu
window.ethersProvider = new ethers.providers.InfuraProvider("ropsten")
2. Vytvořit peněženku
let wallet = new ethers.Wallet(private_key)
3. Připojit peněženku k síti
let walletSigner = wallet.connect(window.ethersProvider)
4. Získat aktuální cenu transakčních poplatků
window.ethersProvider.getGasPrice() // cena transakčních poplatků
5. Definovat transakci
Níže definované proměnné jsou závislé na send_token()
Parametry transakce
send_account: adresa odesílatele tokenuto_address: adresa příjemce tokenusend_token_amount: počet tokenů k odeslánígas_limit: limit transakčních poplatkůgas_price: cena transakčních poplatků
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. Převod
walletSigner.sendTransaction(tx).then((transaction) => {
console.dir(transaction)
alert("Send finished!")
})
Jak to použít
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
)
Úspěch!
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) {
// obecné odeslání tokenu
let contract = new ethers.Contract(
contract_address,
send_abi,
walletSigner
)
// Kolik tokenů?
let numberOfTokens = ethers.utils.parseUnits(send_token_amount, 18)
console.log(`numberOfTokens: ${numberOfTokens}`)
// Odeslat tokeny
contract.transfer(to_address, numberOfTokens).then((transferResult) => {
console.dir(transferResult)
alert("sent token")
})
} // odeslání etheru
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("Send finished!")
})
} catch (error) {
alert("failed to send!!")
}
}
})
}
Poslední aktualizace stránky: 3. března 2026
