Skip to main content

Sending Tokens Using ethers.js

ETHERS.JS
ERC-20
TOKENS
Beginner
Kim YongJun
April 6, 2021
3 minute read

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

/home/ricmoo> npm install --save ethers

ES6 in the Browser

<script type="module">
  import { ethers } from "https://cdn.ethers.io/lib/ethers-5.0.esm.min.js"
  // Your code here...
</script>

ES3(UMD) in the Browser

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

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

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

2. Create wallet

let wallet = new ethers.Wallet(private_key)

3. Connect Wallet to net

let walletSigner = wallet.connect(window.ethersProvider)

4. Get current gas price

window.ethersProvider.getGasPrice() // gasPrice

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

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. Transfer

walletSigner.sendTransaction(tx).then((transaction) => {
  console.dir(transaction)
  alert("Send finished!")
})

How to use it

Success!

image of transaction done successfully

send_token()

Page last update: March 3, 2026

Was this tutorial helpful?