Skip to main content

Use Go

Send a regular transaction from one account to another with Go.

Prerequisites

info

Use MetaMask or similar to create an Ethereum account for testing.

Steps

1. Create a project directory

Create a new directory:

mkdir infura

cd into the directory:

cd infura

2. Initialize the directory and install the dependencies

go mod init infura
go get github.com/ethereum/go-ethereum/common
go get github.com/ethereum/go-ethereum/core/types
go get github.com/ethereum/go-ethereum/crypto
go get github.com/ethereum/go-ethereum/rpc@v1.10.17

3. Create eip1559_tx.go file

package main

import (
"context"
"crypto/ecdsa"
"fmt"
"log"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
)

func main() {
client, err := ethclient.Dial("https://sepolia.infura.io/v3/API_KEY")
if err != nil {
log.Fatal(err)
}

privateKey, err := crypto.HexToECDSA("PRIVATE_KEY")
if err != nil {
log.Fatal(err)
}

publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("error casting public key to ECDSA")
}

fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)

nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatal(err)
}

value := big.NewInt(10000000000000000) // in wei (0.01 eth)
gasLimit := uint64(21000) // in units
tip := big.NewInt(2000000000) // maxPriorityFeePerGas = 2 Gwei
feeCap := big.NewInt(20000000000) // maxFeePerGas = 20 Gwei
if err != nil {
log.Fatal(err)
}

toAddress := common.HexToAddress("ADDRESS_TO")
var data []byte

chainID, err := client.NetworkID(context.Background())
if err != nil {
log.Fatal(err)
}

tx := types.NewTx(&types.DynamicFeeTx{
ChainID: chainID,
Nonce: nonce,
GasFeeCap: feeCap,
GasTipCap: tip,
Gas: gasLimit,
To: &toAddress,
Value: value,
Data: data,
})

signedTx, err := types.SignTx(tx, types.LatestSignerForChainID(chainID), privateKey)
if err != nil {
log.Fatal(err)
}

err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
log.Fatal(err)
}

fmt.Printf("Transaction hash: %s", signedTx.Hash().Hex())
}

Replace the following values in the script:

If using a different Ethereum network, update the URL in the script.

danger

Never disclose your private key.

A malicious actor who has access to your private key can steal your assets.

4. Execute the transaction

Run the script:

go run eip1559_tx.go

Example output:

Transaction hash: 0x47ed277bdfee88902f971510a7cd7b4c55722ea06488e697fb05dc99454e51ab

You can search for the transaction on a block explorer like Sepolia Etherscan.