Sending Tokens Using ethers.js
ETHERS.JS
ERC-20
TOKENS
beginner
Send Token Using ethers.js(5.0)
In This Tutorial You'll Learn How To
- Import ethers.js
- Transfer token
- Set gas price according to the network traffic situation
To-Get-Started
To get started, we must first import the ethers.js library into our javascript Include ethers.js(5.0)
Installing
1/home/ricmoo> npm install --save ethers2
ES6 in the Browser
1<script type="module">2 import { ethers } from "https://cdn.ethers.io/lib/ethers-5.0.esm.min.js";3 // Your code here...4</script>5
ES3(UMD) in the Browser
1<script src="https://cdn.ethers.io/lib/ethers-5.0.umd.min.js"2 type="application/javascript"></script>3
Parameters
contract_address
: Token contract address (contract address is needed when the token you want to transfer is not ether)send_token_amount
: The amount you want to send to the receiverto_address
: The receiver's addresssend_account
: The sender's addressprivate_key
: Private key of the sender to sign the transaction and actually transfer the tokens
Notice
signTransaction(tx)
is removed because sendTransaction()
does it internally.
Sending Procedures
1. Connect to network (testnet)
Set Provider (Infura)
Connect to Ropsten testnet
1window.provider = new InfuraProvider("ropsten");2
2. Create wallet
1let wallet = new ethers.Wallet(private_key);2
3. Connect Wallet to net
1let walletSigner = wallet.connect(window.ethersProvider);2
4. Get current gas price
1window.ethersProvider.getGasPrice(); // gasPrice2
5. Define Transaction
These variables defined below are dependent on send_token()
Transaction parameters
send_account
: address of the token senderto_address
: address of the token receiversend_token_amount
: the amount of tokens to sendgas_limit
: gas limitgas_price
: gas price
1const tx =2{3 from : send_account,4 to : to_address,5 value : ethers.utils.parseEther(send_token_amount),6 nonce : window.ethersProvider.getTransactionCount(send_account, 'latest'),7 gasLimit : ethers.utils.hexlify(gas_limit), // 1000008 gasPrice : gas_price9}10Show all
6. Transfer
1walletSigner.sendTransaction(tx).then((transaction) =>2 {3 console.dir(transaction);4 alert('Send finished!');5 });6
How to use it
1let private_key = "41559d28e936dc92104ff30691519693fc753ffbee6251a611b9aa1878f12a4d";2let send_token_amount = '1';3let to_address = '0x4c10D2734Fb76D3236E522509181CC3Ba8DE0e80';4let send_address = '0xda27a282B5B6c5229699891CfA6b900A716539E6';5let gas_limit = '0x100000';6let wallet = new ethers.Wallet(private_key);7let walletSigner = wallet.connect(window.ethersProvider);8let contract_address="";9window.ethersProvider = new ethers.providers.InfuraProvider("ropsten");1011send_token(contract_address, send_token_amount, to_address, send_address, private_key);12Show all
Success!
send_token()
1function send_token(contract_address, send_token_amount, to_address, send_account, private_key)2{3 let wallet = new ethers.Wallet(private_key);4 let walletSigner = wallet.connect(window.ethersProvider);56 window.ethersProvider.getGasPrice().then((currentGasPrice) =>7 {8 let gas_price = ethers.utils.hexlify(parseInt(currentGasPrice));9 console.log(`gas_price: ${ gas_price }`);1011 if(contract_address)// general token send12 {13 let contract = new ethers.Contract(contract_address, send_abi, walletSigner)1415 // How many tokens?16 let numberOfTokens = ethers.utils.parseUnits(send_token_amount, 18);17 console.log(`numberOfTokens: ${ numberOfTokens }`);1819 // Send tokens20 contract.transfer(to_address, numberOfTokens).then((transferResult) =>21 {22 console.dir(transferResult);23 alert("sent token");24 });25 }26 else // ether send27 {28 const tx =29 {30 from : send_account,31 to : to_address,32 value : ethers.utils.parseEther(send_token_amount),33 nonce : window.ethersProvider.getTransactionCount(send_account, 'latest'),34 gasLimit : ethers.utils.hexlify(gas_limit), // 10000035 gasPrice : gas_price36 }37 console.dir(tx);38 try{39 walletSigner.sendTransaction(tx).then((transaction) =>40 {41 console.dir(transaction);42 alert('Send finished!');43 });44 }catch(error){45 alert("failed to send!!");46 }4748 }49 });50}51Show all