Sending Tokens Using ethers.js

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 ethers
2

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

  1. contract_address: Token contract address (contract address is needed when the token you want to transfer is not ether)
  2. send_token_amount: The amount you want to send to the receiver
  3. to_address: The receiver's address
  4. send_account: The sender's address
  5. private_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(); // gasPrice
2

5. Define Transaction

These variables defined below are dependent on send_token()

Transaction parameters

  1. send_account: address of the token sender
  2. to_address: address of the token receiver
  3. send_token_amount: the amount of tokens to send
  4. gas_limit: gas limit
  5. gas_price: gas price

See below for how to use

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), // 100000
8 gasPrice : gas_price
9}
10
Show 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");
10
11send_token(contract_address, send_token_amount, to_address, send_address, private_key);
12
Show all

Success!

image of transaction done successfully

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);
5
6 window.ethersProvider.getGasPrice().then((currentGasPrice) =>
7 {
8 let gas_price = ethers.utils.hexlify(parseInt(currentGasPrice));
9 console.log(`gas_price: ${ gas_price }`);
10
11 if(contract_address)// general token send
12 {
13 let contract = new ethers.Contract(contract_address, send_abi, walletSigner)
14
15 // How many tokens?
16 let numberOfTokens = ethers.utils.parseUnits(send_token_amount, 18);
17 console.log(`numberOfTokens: ${ numberOfTokens }`);
18
19 // Send tokens
20 contract.transfer(to_address, numberOfTokens).then((transferResult) =>
21 {
22 console.dir(transferResult);
23 alert("sent token");
24 });
25 }
26 else // ether send
27 {
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), // 100000
35 gasPrice : gas_price
36 }
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 }
47
48 }
49 });
50}
51
Show all