乐闻世界logo
搜索文章和话题

How to send erc20 tokens using Web3js

1个答案

1

Answer Overview

Sending ERC-20 tokens typically involves interacting with smart contracts. Web3.js is a widely used library that enables interaction with the Ethereum blockchain. To send ERC-20 tokens using Web3.js, you need to follow these steps:

  1. Set up the Web3.js environment: which includes connecting to the Ethereum network.
  2. Obtain the ABI and address of the smart contract: this is essential for interacting with the ERC-20 token contract.
  3. Create a contract instance: using the ABI and address.
  4. Use contract methods: call the transfer method to send tokens.

Detailed Steps

Step 1: Set up the Web3.js environment

First, import the Web3.js library into your project. If not installed, use npm or yarn:

bash
npm install web3

Then, in your JavaScript file, import and configure the Web3 instance to connect to the Ethereum network. This can be done via Infura or other node service providers:

javascript
const Web3 = require('web3'); const web3 = new Web3('https://mainnet.infura.io/v3/your_project_id'); // Connect to mainnet

Step 2: Obtain the ABI and address of the smart contract

Access the ABI (Application Binary Interface) and deployment address of the ERC-20 token smart contract. These are typically available in the project's official documentation or on Etherscan.

Step 3: Create a contract instance

With the ABI and address, create a contract instance for subsequent interactions:

javascript
const contractABI = [/* ABI array */]; const contractAddress = '0x...'; // Contract address const contract = new web3.eth.Contract(contractABI, contractAddress);

Step 4: Use contract methods to send tokens

The ERC-20 standard includes a transfer method for moving tokens between accounts. Specify the recipient's address and token amount (remember to account for the token's decimal places):

javascript
const fromAddress = '0xYourAddress'; const toAddress = '0xRecipientAddress'; const tokenAmount = web3.utils.toWei('100', 'ether'); // Assuming 18 decimal places // Send the transaction contract.methods.transfer(toAddress, tokenAmount).send({from: fromAddress}) .then(function(receipt){ console.log('Transaction receipt: ', receipt); });

Complete Example

Combining all steps, here is a complete example:

javascript
const Web3 = require('web3'); const web3 = new Web3('https://mainnet.infura.io/v3/your_project_id'); const contractABI = [/* ABI array */]; const contractAddress = '0x...'; const contract = new web3.eth.Contract(contractABI, contractAddress); const fromAddress = '0xYourAddress'; const toAddress = '0xRecipientAddress'; const tokenAmount = web3.utils.toWei('100', 'ether'); contract.methods.transfer(toAddress, tokenAmount).send({from: fromAddress}) .then(function(receipt){ console.log('Transaction receipt: ', receipt); });

Note that sending a transaction consumes Gas, so the sending account must have sufficient Ether to cover transaction fees.

2024年6月29日 12:07 回复

你的答案