Jan 31, 2024 by Arnaud Stoz | 603 views
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 to facilitate the interaction with an Ethereum blockchain from a go program.
As of now, gweb3 contains 2 packages
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
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 where the contracts
package comes into play. This package really facilitates the interaction with a smart contract.
First, you will have to initialize a new smart contract with the function InitializeContract
. You will need:
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
setMessage
which takes one argument getMessage
which takes no argumentimport "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
This is still a work in progress. In case of bugs or if any features are missing, feel free to open an issue on the GitHub (https://github.com/stolab/gweb3)
This blog post is licensed under CC BY-SA 4.0