Hello everyone! 🙌
In this guide, we'll walk through the process of using Go Ethereum (Go ETH) to send ERC-20 tokens via EIP-1559 transactions. Whether you're a developer looking for a quick script or a beginner learning blockchain interactions, this tutorial has you covered.
Prerequisites
Before diving in, ensure you have the following installed:
Go Ethereum (Go ETH)
Install via Homebrew:brew tap ethereum/ethereum brew install ethereum- Dotenv (Optional but recommended for security)
Project Setup
Importing Libraries and Configuring .env
Start by importing the necessary libraries and setting up a .env file to securely store sensitive details like your private key and Infura Project ID.
Example .env structure:
INFURA_PROJECT_ID=your_infura_project_id
PRIVATE_KEY=your_wallet_private_keyIf you need help creating a .env file, refer to the GoDotEnv repository.
Initializing the Ethereum Client
Connect to the Goerli testnet using Infura:
package main
import (
"context"
"fmt"
"log"
"math/big"
"os"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
// Connect to Infura
client, err := ethclient.Dial(fmt.Sprintf("https://goerli.infura.io/v3/%s", os.Getenv("INFURA_PROJECT_ID")))
if err != nil {
log.Fatal(err)
}
}Sending ERC-20 Tokens
Step 1: Load the Token Contract ABI
For this example, we’ll use UNI token on Goerli.
- Find the token’s contract ABI on Etherscan.
- Save the ABI as
abi.jsonin your project directory.
// Load the ERC-20 contract
tokenAddress := common.HexToAddress("0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984")
contract, err := bind.NewBoundContract(tokenAddress, abi, client, client, client)
if err != nil {
log.Fatal(err)
}Step 2: Configure the Transaction
Set gas parameters, nonce, and recipient address:
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatal(err)
}
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
log.Fatal(err)
}
auth := bind.NewKeyedTransactor(privateKey)
auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0) // 0 ETH (ERC-20 only)
auth.GasLimit = uint64(300000) // Adjust based on contract
auth.GasPrice = gasPrice // EIP-1559 dynamic pricing
toAddress := common.HexToAddress("0xRecipientAddress")
amount := big.NewInt(1000000000000000000) // 1 UNI (18 decimals)Step 3: Execute the Transfer
tx, err := contract.Transfer(auth, toAddress, amount)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Tx Hash: %s\n", tx.Hash().Hex())Complete Code Example
Here’s the full script for sending ERC-20 tokens:
package main
import (
"context"
"fmt"
"log"
"math/big"
"os"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
// Connect to Infura
client, err := ethclient.Dial(fmt.Sprintf("https://goerli.infura.io/v3/%s", os.Getenv("INFURA_PROJECT_ID")))
if err != nil {
log.Fatal(err)
}
// Load contract ABI (simplified)
tokenAddress := common.HexToAddress("0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984")
contract, err := bind.NewBoundContract(tokenAddress, abi, client, client, client)
if err != nil {
log.Fatal(err)
}
// Configure transaction
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatal(err)
}
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
log.Fatal(err)
}
auth := bind.NewKeyedTransactor(privateKey)
auth.Nonce = big.NewInt(int64(nonce))
auth.GasLimit = uint64(300000)
auth.GasPrice = gasPrice
// Send 1 UNI
toAddress := common.HexToAddress("0xRecipientAddress")
amount := big.NewInt(1000000000000000000)
tx, err := contract.Transfer(auth, toAddress, amount)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Tx Hash: %s\n", tx.Hash().Hex())
}👉 Explore more blockchain tutorials
FAQs
1. What is EIP-1559?
EIP-1559 introduces a base fee mechanism for Ethereum transactions, making gas fees more predictable.
2. Can I use this script for mainnet?
Yes! Replace the Goerli Infura endpoint with a mainnet RPC provider.
3. How do I find my token’s contract ABI?
Search the token’s address on Etherscan, navigate to the "Contract" tab, and copy the ABI.
👉 Learn advanced Go Ethereum techniques
Key Takeaways
- Use Go ETH for seamless ERC-20 transactions.
- Always secure sensitive data with
.env. - EIP-1559 improves gas fee efficiency.
Now you’re ready to send ERC-20 tokens programmatically! 🚀