Making Ethereum Transactions in Go using Go-Ethereum

Step 1: Import the following required packages to do this.

import (
“context”
“log”

“github.com/ethereum/go-ethereum/ethclient”
)

Step 2: We will create a new transaction by specifying the recipient address, value, gas price, gas limit, and nonce in the go code. Also, replace YOUR_PRIVATE_KEY with the private key of the account making the transaction and 0xRECIPIENT_ADDRESS with the recipient’s Ethereum address.

Go




privateKey, err := crypto.HexToECDSA("YOUR_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)
toAddress := common.HexToAddress("0xRECIPIENT_ADDRESS")
  
value := big.NewInt(1000000000000000000) // 1 ETH in Wei
gasLimit := uint64(21000)
gasPrice := big.NewInt(30000000000) // 30 Gwei
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
    log.Fatal(err)
}
  
tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, nil)


Step 3: Sign the transaction using the private key by replacing chainID with the appropriate chain ID and send it to the Ethereum network.

Go




//Sing the transaction
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
if err != nil {
    log.Fatal(err)
}
  
//Send it to the ethereum network
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
    log.Fatal(err)
}


Code:

C++




package main
  
import (
    "context"
    "fmt"
    "io/ioutil"
    "log"
    "math/big"
  
    "github.com/ethereum/go-ethereum/accounts/keystore"
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/ethclient"
)
  
var (
    url  = "https://kovan.infura.io/v3/0c7b3f204f37416388610fb274b0452c"
    murl = "https://mainnet.infura.io/v3/0c7b3f204f37416388610fb274b0452c"
)
  
func main() {
    // ks := keystore.NewKeyStore("./wallet", keystore.StandardScryptN, keystore.StandardScryptP)
    // _, err := ks.NewAccount("password")
    // if err != nil {
    //     log.Fatal(err)
    // }
    // _, err = ks.NewAccount("password")
    // if err != nil {
    //     log.Fatal(err)
    // }
    // "1f7ecea2fa83cc4a7de969f11d16a40edf9023d7"
    // "1e41ca1ccfc06597525c966a986b35a09e22358d"
  
    client, err := ethclient.Dial(url)
    if err != nil {
        log.Fatal(err)
    }
    defer client.Close()
    a1 := common.HexToAddress("c393967d7b4b7fd02e697d13085d645c9412af11")
    a2 := common.HexToAddress("1e41ca1ccfc06597525c966a986b35a09e22358d")
  
    b1, err := client.BalanceAt(context.Background(), a1, nil)
    if err != nil {
        log.Fatal(err)
    }
  
    b2, err := client.BalanceAt(context.Background(), a2, nil)
    if err != nil {
        log.Fatal(err)
    }
  
    fmt.Println("Balance 1:", b1)
    fmt.Println("Balance 2:", b2)
    nonce, err := client.PendingNonceAt(context.Background(), a1)
    if err != nil {
        log.Fatal(err)
    }
    // 1 ether = 1000000000000000000 wei
    amount := big.NewInt(100000000000000000)
    gasPrice, err := client.SuggestGasPrice(context.Background())
    if err != nil {
        log.Fatal(err)
    }
    tx := types.NewTransaction(nonce, a2, amount, 21000, gasPrice, nil)
    chainID, err := client.NetworkID(context.Background())
    if err != nil {
        log.Fatal(err)
    }
  
    b, err := ioutil.ReadFile("wallet/UTC--2021-05-24T16-47-26.459903259Z--c393967d7b4b7fd02e697d13085d645c9412af11")
    if err != nil {
        log.Fatal(err)
    }
  
    key, err := keystore.DecryptKey(b, "password")
    if err != nil {
        log.Fatal(err)
    }
  
    tx, err = types.SignTx(tx, types.NewEIP155Signer(chainID), key.PrivateKey)
    if err != nil {
        log.Fatal(err)
    }
  
    err = client.SendTransaction(context.Background(), tx)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("tx sent: %s", tx.Hash().Hex())
}


Output: After you have added your Infura API key to the above code and the correct addresses of the sender and receiver.

 

Querying the Number of Transactions in a Block

Step 1: Import the following required packages to do this.

import (
“context”
“log”

“github.com/ethereum/go-ethereum/ethclient”
)

Step 2: Set up and create a client with the following GO code. 

Go




package main
  
import (
    "context"
    "log"
  
    "github.com/ethereum/go-ethereum/ethclient"
)
  
  
func main() {
    client, err := ethclient.DialContext(context.Background(), )
    }


Step 3: Proceed with the following code in the editor and use the link for a variable.

Go




package main
  
import (
    "context"
    "log"
  
    "github.com/ethereum/go-ethereum/ethclient"
)
  
var infuraURL = "https://mainnet.infura.io/v3/INFURA_API_KEY"
  
func main() {
    client, err := ethclient.DialContext(context.Background(), infuraURL)
    if err != nil {
        log.Fatalf("Error in creating an ether client: %v", err)
    }
    defer client.Close()


Step 4: After creating the client let’s use it to get the Ethereum block number. Note that if you don’t mention the block number you will automatically be given the last mined block’s number.

Go




package main
  
import (
    "context"
    "log"
  
    "github.com/ethereum/go-ethereum/ethclient"
)
  
var infuraURL = "https://mainnet.infura.io/v3/81888237d633446c976203dcf922d861"
  
func main() {
    client, err := ethclient.DialContext(context.Background(), infuraURL)
    if err != nil {
        log.Fatalf("Error in creating an ether client: %v", err)
    }
    defer client.Close()
    block,err:= client.BlockByNumber(context.Background(),nil)
      
    if err != nil {
    log.Fatalf("Error to get a block: %v", err)
    }
      
    fmt.Println(block.Number())


Step 5: Run the code on the terminal

Output:

 

In my and your case, it could differ depending on the current block being used in the network. Running the code at different intervals will generate different block numbers.

Querying Details of Transactions in a Block

Step 1: Import the following required packages to do this.

import (
“context”
“log”

“github.com/ethereum/go-ethereum/ethclient”
)

Step 2: Connect to the Ethereum node and specify the block number for which you want to query the transaction details.

blockNumber := uint64(1234567) // Replace with the desired block number

Step 3: Retrieve the block containing the transactions and iterate over the transactions in the block and print their details. We will look at the details like the transaction hash, from to addresses, gas price, nonce, etc.

Go




block, err := client.BlockByNumber(context.Background(), big.NewInt(int64(blockNumber)))
if err != nil {
    log.Fatal(err)
}
  
for _, tx := range block.Transactions() {
    fmt.Println("Transaction Hash:", tx.Hash().Hex())
    fmt.Println("From:", tx.From().Hex())
    fmt.Println("To:", tx.To().Hex())
    fmt.Println("Value:", tx.Value().String())
    fmt.Println("Gas Limit:", tx.Gas())
    fmt.Println("Gas Price:", tx.GasPrice().String())
    fmt.Println("Nonce:", tx.Nonce())
    fmt.Println("Data:", tx.Data())
    fmt.Println("-----------------------------------")
}


Code:

Go




package main
  
import (
    "context"
    "fmt"
    "log"
    "math/big"
  
    "github.com/ethereum/go-ethereum"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/ethclient"
)
  
func main() {
    // Ethereum node endpoint
    nodeURL := "https://mainnet.infura.io/v3/your_infura_project_id"
  
    // Create an Ethereum client instance
    client, err := ethclient.Dial(nodeURL)
    if err != nil {
        log.Fatal(err)
    }
  
    // Specify the block number to retrieve
    blockNumber := uint64(1234567)
  
    // Retrieve the block using the block number
    block, err := client.BlockByNumber(context.Background(), big.NewInt(int64(blockNumber)))
    if err != nil {
        log.Fatal(err)
    }
  
    // Iterate over the transactions in the block
    for _, tx := range block.Transactions() {
        fmt.Println("Transaction Hash:", tx.Hash().Hex())
        fmt.Println("From:", tx.From().Hex())
        fmt.Println("To:", tx.To().Hex())
        fmt.Println("Value:", tx.Value().String())
        fmt.Println("Gas Limit:", tx.Gas())
        fmt.Println("Gas Price:", tx.GasPrice().String())
        fmt.Println("Nonce:", tx.Nonce())
        fmt.Println("Data:", tx.Data())
        fmt.Println("-----------------------------------")
    }
}


Output: After you have added the Infura API key in the code you will get a similar output.

 



Ethereum Development with Golang

Ethereum is a blockchain-based platform that enables developers to create decentralized applications (dApps) and smart contracts. Ethereum’s native cryptocurrency is Ether (ETH), which is used to pay transaction fees and incentivize miners to secure the network.

Go, also known as Golang, is a programming language developed by Google. It is a compiled language that is designed to be efficient, concise, and easy to use. Go is often used for building web applications, network servers, and other types of software that require high performance.

Similar Reads

Getting Started with Go-Ethereum

Ethereum Go, also known as Geth, is the official Go implementation of the Ethereum protocol. Geth provides a command-line interface (CLI) for interacting with the Ethereum network, as well as an API for building decentralized applications. The relationship between Ethereum and Go is that Geth is one of the software clients that can connect to the Ethereum network. Developers can use Geth to interact with the Ethereum network and build decentralized applications using the Go programming language....

Connecting to an Ethereum Node using Infura and Go

Step 1:  After we have installed go-ethereum, create a new Go module for your project by running the following command:...

Querying Ethereum Wallet Balances with Geth

...

Creating an Ethereum Wallet with Go-Ethereum

...

Making Ethereum Transactions in Go using Go-Ethereum

...