Wysyłanie tokenów przy użyciu ethers.js
ETHERS.JS
ERC-20
TOKENY
Początkujący
Kim YongJun
6 kwietnia 2021
3 minuta czytania
Wyślij token przy użyciu ethers.js(5.0)
W tym samouczku dowiesz się, jak
- Importować ethers.js
- Przesłać token
- Ustawić cenę gazu zgodnie z natężeniem ruchu w sieci
Na początek
Na początek musimy najpierw zaimportować bibliotekę ethers.js do naszego javascript Dołącz ethers.js(5.0)
Instalacja
/home/ricmoo> npm install --save ethers
ES6 w przeglądarce
<script type="module">
import { ethers } from "https://cdn.ethers.io/lib/ethers-5.0.esm.min.js"
// Twój kod tutaj...
</script>
ES3(UMD) w przeglądarce
<script
src="https://cdn.ethers.io/lib/ethers-5.0.umd.min.js"
type="application/javascript"
></script>
Parametry
contract_address: Adres kontraktu tokena (adres kontraktu jest potrzebny, gdy token, który chcesz przesłać, nie jest etherem)send_token_amount: Kwota, którą chcesz wysłać do odbiorcyto_address: Adres odbiorcysend_account: Adres nadawcyprivate_key: Klucz prywatny nadawcy do podpisania transakcji i faktycznego przesłania tokenów
Uwaga
Funkcja signTransaction(tx) została usunięta, ponieważ sendTransaction() wykonuje tę czynność wewnętrznie.
Procedury wysyłania
1. Połącz z siecią (sieć testowa)
Ustaw dostawcę (Infura)
Połącz z siecią testową Ropsten
window.ethersProvider = new ethers.providers.InfuraProvider("ropsten")
2. Utwórz portfel
let wallet = new ethers.Wallet(private_key)
3. Połącz portfel z siecią
let walletSigner = wallet.connect(window.ethersProvider)
4. Pobierz aktualną cenę gazu
window.ethersProvider.getGasPrice() // cena gazu
5. Zdefiniuj transakcję
Zmienne zdefiniowane poniżej są zależne od send_token()
Parametry transakcji
send_account: adres nadawcy tokenato_address: adres odbiorcy tokenasend_token_amount: ilość tokenów do wysłaniagas_limit: limit gazugas_price: cena gazu
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. Przesłanie
walletSigner.sendTransaction(tx).then((transaction) => {
console.dir(transaction)
alert("Wysyłanie zakończone!")
})
Jak tego używać
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
)
Sukces!
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) {
// ogólne wysyłanie tokenów
let contract = new ethers.Contract(
contract_address,
send_abi,
walletSigner
)
// Ile tokenów?
let numberOfTokens = ethers.utils.parseUnits(send_token_amount, 18)
console.log(`numberOfTokens: ${numberOfTokens}`)
// Wyślij tokeny
contract.transfer(to_address, numberOfTokens).then((transferResult) => {
console.dir(transferResult)
alert("wysłano token")
})
} // wysyłanie 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("Wysyłanie zakończone!")
})
} catch (error) {
alert("wysyłanie nie powiodło się!!")
}
}
})
}
Ostatnia aktualizacja strony: 3 marca 2026
