Intro to Web3.go Part 1: EthClient

How to use go-ethereum as a library

Marius Van Der Wijden
Coinmonks
Published in
3 min readJul 6, 2020

--

Many developers have asked me how they can use their favorite programming language, Go, to access the Ethereum network(s). This article is part of a series that should explain the little known ability to use go-ethereum as a library to access Ethereum.

Back in 2015 Péter Szilágyi and the geth team started working on the ethclientpackage and the abigenTool. This old video gives a deep dive into the reasoning behind it and how to use them. Besides this video, there is not much available documentation about them. The intention of this series is to provide more documentation for developers on these tools.

Connecting: the ethclient package.

Package ethclient is the first package that you’ll stumble upon when you try to use geth as a library. It enables you to connect to a node via websocket, http or ipc. ethclient allows you to connect to any node that supports the rpc API as defined in the RPC-Spec.

Connect to an ethereum node

Dial returns an object that wraps a raw rpc.Client and can be used to call the following functions for example (there are a lot more):

Common functions

In the last snippet, you’ve seen some basic usages of the ethclient, but the careful reader has noticed that we’re creating an invalid transaction that will result in an error, so how can we create valid transactions and how does account management work?

Account Management: the accounts package.

In order to create a raw transaction, you can use the following snippet:

Create a new transaction

Now you need to sign the transaction. If you have your secret key in plaintext, you can use the following code to sign your transaction:

Sign your transaction

But what if you don’t want to handle raw secret keys or use your hardware key to send a transaction? We got you covered! You can use keystore.Keystore as an encrypted storage for your private keys as follows:

Use keystore.Keystore to create a TransactOpts

With a TransactOpts object you can sign transactions. That object is needed later-on to interact with smart contracts over the auto-generated bindings.

Sign a transaction with TransactOpts

This looks all pretty cool, but what if I want to interact with smart contracts? You could enter raw data in the data field of your transaction, but that would be pretty unsafe, so geth provides the bind package and the abigen binary to create contract bindings for your smart contracts. We’ll discuss this in part 2 of this series.

If you want to get in touch, just message me at @vdWijden on twitter or Linkedin.

Part1: Connecting and sending transactions
Part2: Calling functions on smart contracts
Part3: Listening on events from smart contracts
Part4: Creating a mock-blockchain for testing

Special thanks to Rene for proof-reading!

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

--

--

Marius Van Der Wijden
Coinmonks

Ethereum Developer, State Channel Researcher, Student at TU Darmstadt