How to Send ERC-20 Tokens using the QuickNode SDK | QuickNode (2024)

8 min read

Overview

In this guide, we delve into the practical aspects of interacting with the Ethereum blockchain, specifically focusing on the transfer of ERC-20 tokens. ERC-20 tokens are a standard type of Ethereum token, widely used in various applications. We'll explore how to accomplish this using the QuickNode SDK supported in JavaScript.

What You Will Do


  • Understand the basics of ERC-20 tokens and their transfer methods
  • Set up your development environment with the necessary libraries
  • Implement an ERC-20 token transfer to an Ethereum address
  • Implement an ERC-20 token approval and transfer on behalf of a smart contract

What You Will Need


  • Basic understanding of Ethereum and smart contracts.
  • Familiarity with JavaScript
  • A text editor or an IDE (e.g., VS Code)
  • Access to an Ethereum node or a service like QuickNode for connecting to the Ethereum network (or 24 other blockchains and counting! Sign up for free here
  • An EVM wallet (with access to your private key, like MetaMask or a burner wallet)
DependencyVersion
node.js18.13.0
@quicknode/sdk^1.1.4

ERC-20 Tokens

ERC-20 tokens are digital assets built on the Ethereum blockchain, following a specific set of standards that allow them to be shared, exchanged, or transferred to any Ethereum address. These tokens represent a wide range of assets, like voting rights, financial shares, rights to a resource, or simply a store of value.

Approval

Understanding the approve function is crucial when dealing with ERC-20 token transfers. This function grants permission to a smart contract to transfer a specific amount of tokens on your behalf. It's a security measure ensuring that tokens can only be moved with your explicit consent.

When to Use Approval


  • Interacting with Decentralized Exchanges (DEXs): For instance, before swapping tokens on platforms like Uniswap, you need to approve the DEX to access the amount of tokens you wish to trade.
  • Participating in DeFi Platforms: When depositing tokens into a DeFi protocol, approval is required to let the protocol's smart contract handle your tokens, whether for lending, staking, or yield farming.

When Approval is Not Necessary


  • Direct Transfers: If you're simply transferring tokens directly to another address, the approve function is not needed. This is akin to sending tokens from your wallet to another individual's wallet or a smart contract.
  • Previously Approved Contracts: If you have already approved a contract to use your tokens and the allowance covers your current transaction, no additional approval is required.

The approve function is a fundamental aspect of ERC-20 token interactions in the Ethereum ecosystem, providing control and security over how your tokens are utilized by smart contracts.

Now, before we delve into the technicalities of sending ERC-20 tokens, let's first set up a free QuickNode endpoint and fund our wallet.

Project Prerequisite: Create a QuickNode Endpoint

You're welcome to use public nodes or deploy and manage your own infrastructure; however, if you'd like 8x faster response times, you can leave the heavy lifting to us. Sign up for a free account here.

Once logged in, click the Create an endpoint button, then select the blockchain and network you want to deploy on. For the purpose of this guide, we'll choose the Ethereum Sepolia chain.

After creating your endpoint, keep the page handy, as we'll need it in the technical coding portion of this guide.

How to Send ERC-20 Tokens using the QuickNode SDK | QuickNode (1)

tip

Note that although we are using Ethereum Sepolia for the demonstration of this guide, you can also use other EVM-compatible chains like Base, Polygon, Arbitrum, and more to interact with smart contracts and ERC-20 tokens.

With our infrastructure created, let's now move on to the technical part.

Project Prerequisite: Fund Your Wallet

If you're in need of ETH on Sepolia testnet, the Multi-Chain QuickNode Faucet makes it easy to obtain test ETH!

Navigate to the Multi-Chain QuickNode Faucet and connect your wallet (e.g., MetaMask, Coinbase Wallet) or paste in your wallet address to retrieve test ETH. Note that there is a mainnet balance requirement of 0.001 ETH on Ethereum Mainnet to use the EVM faucets. You can also tweet or login with your QuickNode account to get a bonus!

How to Send ERC-20 Tokens using the QuickNode SDK | QuickNode (2)

For the remainder of this guide, we'll transition to the coding portion of the guide demonstrating how to send ERC-20 tokens to an Ethereum address and transfer it to a smart contract.

Send an ERC-20 Token using QuickNode SDK

The QuickNode SDK is an advanced web3 library for developers to easily connect with the QuickNode infrastructure and interact with the blockchain. It provides a versatile library compatible with both JavaScript and TypeScript, and works with any framework. The SDK supports both CommonJS and ES module formats as well as uses Viem to make RPC calls. All basic operations supported by Viem's Public Client, such as retrieving block numbers, transactions, and reading from smart contracts, are available on the client property of the Core class. Additionally, it is open-source, with its source code accessible in the qn-oss repository.

Note that if you need an ERC-20 token to finish the remainder of this guide, check out this QuickNode Guide: How to Create and Deploy an ERC20 Token

Step 1: Install the SDK and Set Up Project

To install the QuickNode SDK, we will use the npm package @quicknode/sdk. It's important to have Node.js version 16 or above for this package.

Create a project folder, navigate inside it, and within your terminal, run the following command to initialize a default npm project:

npm init --y

Then, to install the @quicknode/sdk via npm:

npm install @quicknode/sdk

OR, to initialize your project via yarn. Run the command:

Then, to install via yarn:

yarn add @quicknode/sdk

NOTE: After completing either step, insert "type": "module" into the package.json file just created. This enables the use of ES Module Syntax.

Check out the QuickNode documentation for more information.

Step 2: Create and Configure the File

Next, let's create a file and input the code. In your project's directory, create a file called index.js:

echo > index.js

Then, open the file in your code editor and input the following code:

import { Core } from '@quicknode/sdk';
import { sepolia } from 'viem/chains'; // Change the network as needed
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import abi from './abi.json' assert { type: 'json' };

// Define your private key
const privateKey = '0x' + 'YOUR_PRIVATE_KEY';

// Convert the private key to an account object
const account = privateKeyToAccount(privateKey);

// Initialize the clients
const core = new Core({
endpointUrl: "YOUR_QUICKNODE_ENDPOINT",
});

const walletClient = createWalletClient({ account, chain: sepolia, transport: http(core.endpointUrl) });

// Contract and transaction details
const contractAddress = 'ERC20_CONTRACT_ADDRESS';
const toAddress = 'TO_ADDRESS';
const decimals = 18; // This should change based on your ERC20 decimal places
const tokenAmount = BigInt(10 ** decimals); // 1 token, adjust as needed

async function send() {
try {
const { request } = await core.client.simulateContract({
address: contractAddress,
abi: abi,
functionName: 'transfer',
args: [toAddress, tokenAmount],
account
});

const response = await walletClient.writeContract(request);
console.log('Transaction sent. Transaction hash:', response);

} catch (error) {
console.error('Error sending transaction:', error);
}
}

send().catch(error => console.error('Error in send function:', error));

Take a few minutes to recap the code and comments. After, replace all the placeholder strings, YOUR_QUICKNODE_ENDPOINT, YOUR_PRIVATE_KEY, ERC20_CONTRACT_ADDRESS and TO_ADDRESS with their actual values.

NOTE: You'll notice that in the imports we create an abi variable that references our abi.json file. You will need to create a similar file in the same directory or place your ABI inline code (but this can take up space). For example, you can reference/copy an ABI from Etherscan by going to the Contract tab (example) and scrolling down to the Contract ABI section. However, note that this method only works for contracts verified on Etherscan.

Step 3: Execute and Send the ERC-20 Token

Execute your script by calling the command below. Remember to have updated all the placeholders in the code above with real values.

node index.js

If you're using yarn, then run:

yarn run index.js

The output will look similar to this:

Transaction sent. Transaction hash: 0x35f50018c2a83ca13685a7a0ad8e52598ef8ae48a0fa15dc537737f7fe6077a8

Confirm the transaction by looking it up on Etherscan or your preferred block explorer.

Grant ERC-20 Token Access to Smart Contracts

As mentioned in the Approve section earlier, you do not need to use the approve function to transfer ERC-20 tokens to a smart contract; however, if you would like the smart contract to access some or all of your ERC-20 tokens on your behalf, you will need to call the approve function first.

Let's demonstrate how to do this. We won't go over in-depth to set up your file (since it's the same process as above), so just follow along without coding.

QuickNode SDK

async function approve() {
try {
const { request } = await core.client.simulateContract({
address: contractAddress,
abi: abi,
functionName: 'approve',
args: [spender, tokenAmount],
account
});

const response = await walletClient.writeContract(request);
console.log('Transaction hash:', response);
} catch (error) {
console.error('Error sending transaction:', error);
}
}

Notice the only changes were to the functionName and argument fields. We updated the function to approve since that's the function we want to call and updated the parameter value to spender, which should reference the contract address you want to approve tokens to.

Final Thoughts

Congratulations on completing this guide! You've now equipped yourself with the knowledge to send ERC-20 tokens using the QuickNode SDK confidently. This skill is a fundamental part of interacting with the Ethereum blockchain and a valuable asset in your Web3 toolbox.

If you'd like to see the Python version of this guide using Web3.py, check out this QuickNode guide: How to Send ERC-20 Tokens using Web3.py

What's Next?


  • Experiment and Explore: Try sending tokens between different accounts or integrate these methods into your own projects.
  • Deepen Your Knowledge: Dive deeper into smart contract development and explore other token standards like ERC-721 or ERC-1155.
  • Join the Community: Engage with other developers, share your experiences, and collaborate on projects. The QuickNode community is a great place to start.

Remember, blockchain development is an ever-evolving field, and staying updated is 🔑. Subscribe to our newsletter for more articles and guides on Web3 and blockchain. If you have any questions or need further assistance, feel free to visit the QuickNode Forum. Stay informed and connected by following us on Twitter (@QuickNode) or joining our Discord community.

We ❤️ Feedback!

Let us know if you have any feedback or requests for new topics. We'd love to hear from you.

How to Send ERC-20 Tokens using the QuickNode SDK | QuickNode (2024)

FAQs

How to Send ERC-20 Tokens using the QuickNode SDK | QuickNode? ›

To send ERC20 tokens, you need to have ETH on a parent account of tokens. You need ETH to burn as gas (network fees). Make sure your parent Ethereum account holds some ETH to pay for the network fees of token transactions.

How do I send ERC20 token? ›

To send ERC20 tokens, you need to have ETH on a parent account of tokens. You need ETH to burn as gas (network fees). Make sure your parent Ethereum account holds some ETH to pay for the network fees of token transactions.

How to mint ERC20 tokens? ›

In the top right, click “select signing account” and pick the same account you used to create your ERC20 contract. You should now see options to transfer, mint, or burn your token. As an example, click the “Mint” option and input a value of 100. After clicking mint, the address balance should increment by 100.

How to bridge ERC20 tokens? ›

ERC-20 tokens can only be bridged via manual claiming, which requires you to cover the ETH fee on the destination layer. Make sure your wallet is properly funded before beginning this process!

How to send ERC20 tokens to MetaMask? ›

To deposit ETH or ERC-20 tokens to your MetaMask wallet from an exchange or another wallet, you will simply need to submit a transaction pointed to the address of one of your accounts in MetaMask. Please check the exchanges that operate in your country here.

Can you send any ERC-20 tokens to an ETH address? ›

ERC-20 Tokens​

ERC-20 tokens are digital assets built on the Ethereum blockchain, following a specific set of standards that allow them to be shared, exchanged, or transferred to any Ethereum address.

What wallet for erc-20 tokens? ›

MetaMask is one of the most popular crypto wallets for storing ERC20 tokens. There are no fees to use MetaMask, and it comes as a mobile app (iOS and Android) and a browser extension (Chrome, Firefox, Edge, Brave). MetaMask is decentralized, so users are provided with their private keys when setting the wallet up.

What does mint do on ERC20 token? ›

This calls the internal _mint function, which is provided by the ERC20 contract from OpenZeppelin. It creates new tokens and adds them to the balance of the specified address. With the mint function, the contract owner can create new tokens and distribute them as needed.

How do I manually add an ERC20? ›

Add your token manually by contract

First, go to the Wallet tab and click the "+" icon at the top-right corner. Now, you can either add your token manually by its contract or simply send it to your ETH address. We'll guide you through both options; Hit the Add by contract button.

How do I bridge my token? ›

Here's our step-by-step guide:
  1. Make sure your wallet is connected and that you've selected the account that has the tokens you want to bridge.
  2. Select the network from which you want to move tokens under the 'From this network' field.
  3. Input the details of the token you want to bridge under 'You send'.
3 days ago

Can you send ETH on base network? ›

Yep! The process is bidirectional, you can bridge from both Ethereum to Base, and Base to Ethereum. Make sure you use the Bridge UI to safely transfer funds to Base and back.

Can I convert ERC-20 to Ethereum? ›

You can convert ERC20 to Ethereum by selling ERC20 for ETH on a cryptocurrency exchange. ERC20 is currently trading on undefined exchanges. The best way to convert ERC20 for ETH is to use unknown. To see all exchanges where ERC20 is trading, click here.

Can I send any ERC20 token to Ledger? ›

Although your Ledger device can secure most Ethereum ERC20 tokens, not all ERC20 tokens are supported by the Ledger Live app. Non-supported ERC20 token deposits will not show in Ledger Live and will not create a transaction record in the Latest operations section in Ledger Live.

Can I send ERC-20 to Bitcoin wallet? ›

The Bitcoin.com Wallet app supports ERC-20 tokens not only on Ethereum, but also across the EVM-compatible networks supported in the wallet (at the time of writing these are Avalanche, BNB Smart Chain, and Polygon.

How to check if a token is ERC-20? ›

You can use a tool or library to call these functions on the target address. Evaluating Return Values: If the function calls succeed and return valid values (e.g., a non-zero balance or a total token supply), it's a strong indication that you're dealing with an ERC20 token contract.

How do I find my ERC20 token address? ›

Summary: How to Get the Address of an ERC-20 Token
  1. Step 1: Visit Moralis Money.
  2. Step 2: Go to the coin's token page.
  3. Step 3: Copy the address.
Oct 17, 2023

How do I create an ERC-20 wallet address? ›

To create a ERC20 wallet, simply download the Noone wallet app from App Store or Google Play, follow the setup instructions, and choose to create a new ERC20 wallet. The process is intuitive and user-friendly. What is a ERC20 wallet address?

How do you send Ethereum tokens? ›

To send ETH, you indicate the address you want to send to and enter the amount you'd like to send. Let's look at an example of sending ETH with the multichain Bitcoin.com Wallet app: From the app's home screen, tap the send button then select ETH. Enter the ETH address you wish to send to.

Top Articles
Latest Posts
Article information

Author: Prof. An Powlowski

Last Updated:

Views: 5575

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Prof. An Powlowski

Birthday: 1992-09-29

Address: Apt. 994 8891 Orval Hill, Brittnyburgh, AZ 41023-0398

Phone: +26417467956738

Job: District Marketing Strategist

Hobby: Embroidery, Bodybuilding, Motor sports, Amateur radio, Wood carving, Whittling, Air sports

Introduction: My name is Prof. An Powlowski, I am a charming, helpful, attractive, good, graceful, thoughtful, vast person who loves writing and wants to share my knowledge and understanding with you.