Intro to Web3.go Part 1: EthClient
How to use go-ethereum as a library
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 ethclient
package and the abigen
Tool. 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.
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):
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:
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:
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:
With a TransactOpts
object you can sign transactions. That object is needed later-on to interact with smart contracts over the auto-generated bindings.
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
- Copy Trading | Crypto Tax Software
- Grid Trading | Crypto Hardware Wallet
- Crypto Telegram Signals | Crypto Trading Bot
- Best Crypto Exchange | Best Crypto Exchange in India
- Best Crypto APIs for Developers
- Best Crypto Lending Platform
- Free Crypto Signals | Crypto Trading Bots
- An ultimate guide to Leveraged Token