gweb3: a go module to interact with ethereum blockchain

Jan 31, 2024 by Arnaud Stoz | 215 views

Blockchain Ethereum

https://cylab.be/blog/322/gweb3-a-go-module-to-interact-with-ethereum-blockchain

Have you ever wonder why most of the web3 tools are written in go (geth, kubo,....) but it's actually difficult to find a go module that would let you interact with the web3 ecosystem like web3.js or web3.py does ? This blog post will introduce you to gweb3, a go module that aims at facilitate the interaction with an Ethereum blockchain from a go program.

gweb3

As of now, gweb3 contains 2 packages

  1. rpc
  2. contracts

rpc

The rpc package contains a lot of the Ethereum JSON-RPC Specification methods. Not all methods are already present, a few still need to be added.

The rpc package also contains all the logic to connect to an endpoint. Here is an example on how to start interacting with a node.

You have to own the node you connect to.

import "github.com/stolab/gweb3/rpc"
import "fmt"

const(
    node = "YOUR NODE IP"
    port = "YOUR NODE PORT"
)

func main() {
    endpoint := rpc.ConnectEndpoint(node, port) // you establish the connection
    response, err := endpoint.ClientVersion() //return the clientVersion of the endpoint
    if err != nil {
          exit(1)
    }
    defer r.Body.Close()

    bodyBytes, err := io.ReadAll(r.Body)
    fmt.println(string(bodyBytes)
}

if you need more information, have a look at the documentation of the rpc package

contracts

The rpc package only cover the beginning of all interactions possible with ethereum. And more precisely it does not facilitate at all the interaction with smart contract.

That's were the contracts package comes into play. This package really facilitate the interaction with a smart contract.

First you will have to initialize a new smart contract with the function InitializeContract. you will need:

  • Your endpoint object you are connected to
  • The address of the contract
  • The sender address (most of the time your wallet address)
  • The ABI of the contract.

Once you have initialized your contract, you can very easily call any function of this contract with the following syntax:

myContract.Function["Name of the function"].Call(args)

Here is a full example. The contract has two functions

  1. setMessage which takes one argument
  2. getMessage which takes no argument
import "github.com/stolab/gweb3/rpc"
import "github.com/stolab/gweb3/contracts"
import "fmt"

const (
    node = "YOUR NODE IP"
    port = "YOUR NODE PORT"
    ABI = "YOUR ABI"
   contractAddr = "CONTRACT ADDRESS"
    yourAddr = "SENDER ADDRESS"
)

func main() {
    endpoint := rpc.ConnectEndpoint(node, port)
    contract, err := InitalizeContract(endpoint, contractAddr, yourAddr, strings.NewReader(abiJson))
    if err != nil {
        t.Fatalf("Got and error from the function : %s", err)
    }

    result, err := contract.Function["setMessage"].Call("hello World")
  //chekc err and display result

   result, err = contract.Function["getMessage"].Call()
   //check err and display result
}

Congratulation you have interacted with a contract from a go program! As shown above this is very easy to interact with a contract thanks to gweb3

Remarks

This is still a work in progress. In case of bugs or if any features are missing, feel free to open a issue on the github (https://github.com/stolab/gweb3)

This blog post is licensed under CC BY-SA 4.0

This website uses cookies. More information about the use of cookies is available in the cookies policy.
Accept