Go API
Overview
ETN-SC's reusable Go libraries focus on three main usage areas:
Simplified client side account management
Remote node interfacing via different transports
Contract interactions through auto-generated bindings
The libraries are updated synchronously with the ETN-SC GitHub repository. The Go libraries can be viewed in full at Go Packages.
Go packages
The electroneum-sc
library is distributed as a collection of standard Go packages straight from electroneum-sc's GitHub repository. The packages can be used directly via the official Go toolkit, without needing any third party tools.
The canonical import path for ETN-SC is github.com/electroneum/electroneum-sc
, with all packages residing underneath. Although there are lots of them most developers will only care about a limited subset.
All the ETN-SC packages can be downloaded using:
More Go API support for dapp developers can be found on the Go Contract Bindings and Go Account Management pages.
Tutorial
This section includes some basic usage examples for the ethclient
and gethclient
packages available as part of the Go API. The ethclient
package provides a client that implements the full Electroneum JSON-RPC API, whereas gethclient
offers the ETN-SC-specific API.
Instantiating a client
The client is an instance of the Client struct which has associated functions that wrap requests to the Electroneum or ETN-SC RPC API endpoints.
A client is instantiated by passing a raw url or path to an ipc file to the client's Dial function. In the following code snippet the path to the ipc file for a local ETN-SC node is provided to ethclient.Dial().
Interacting with the client
The client can now be used to handle requests to the ETN-SC node using the full JSON-RPC API. For example, the function BlockNumber()
wraps a call to the eth_blockNumber
endpoint. The function SendTransaction
wraps a call to eth_sendTransaction
. The full list of client methods can be found here.
Frequently, the functions take an instance of the Context type as their leading argument. This defines context about requests sent from the application such as deadlines, cancellation signals etc. More information on this can be found in the Go documentation. An empty context instance can be created using Context.Background()
.
Querying client for data
A simple starting point is to fetch the chain ID from the client. This e.g. is needed when signing a transaction as is to be seen in the next section.
Unlike ChainID
, many functions require arguments other than context. The Go API takes in and returns high-level types which are used in ETN-SC internals as well to simplify programming and remove the need for knowing how data needs to be formatted exactly as per the JSON-RPC API spec. For example to find out the nonce for an account at a given block the address needs to be provided as a common.Address
type and the block number as a *big.Int
:
Querying past events
Contracts emit events during execution which can be queried from the client. The parameters for the event one is interested in have to be filled out in the ethereum.FilterQuery
object. This includes which event topics are of interest, from which contracts and during which range of blocks. The example below queries Transfer events of all ERC-20 tokens for the last 10 blocks:
Sending a transaction
Sending a transaction is achieved using the SendTransaction()
function. SendTransaction
takes an instance of context.Context
as its leading argument and a signed transaction as its second argument. The signed transaction must be generated in advance. Building the signed transaction is a multi-stage process that requires first generating a key pair if none exists already, retrieving some chain data and defining sender and recipient addresses. Then these data can be collected into a transaction object and signed. The resulting signed transaction can then be passed to SendTransaction
.
The example below assumes the following key pair has already been generated:
The secret key and address can be used to send a transaction. In the example below 1 ETN
is sent from the address ADDR
to an arbitrary recipient.
gethclient
An instance of gethclient
can be used in exactly the same way as ethclient
. However, gethclient
includes ETN-SC-specific API methods. These additional methods are:
Note that both ethclient
and gethclient
have a CallContract()
function - the difference is that the gethclient
version includes an overrides argument.
Details relating to these endpoints can be found at pkg.go.dev or the ETN-SC GitHub.
Summary
There are a wide variety of Go APIs available for dapp developers that abstract away the complexity of interacting with Electroneum using a set of composable, reusable functions provided by ETN-SC.
Last updated