conclient

package module
v0.0.0-...-c69dff9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2024 License: MIT Imports: 13 Imported by: 1

README

go-conclient

Package conclient provides a basic smart-contract client for a smart-contract on a blockchain-network, for the Go programming language.

The blockchain-network could be Ethereum, some other EVM chain, or something else.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-conclient

GoDoc

Import

To import package conclient use import code like the follownig:

import "github.com/reiver/go-conclient"

Installation

To install package conclient do the following:

GOPROXY=direct go get https://github.com/reiver/go-conclient

Author

Package conclient was written by Charles Iliya Krempeaux

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a client for a smart-contract on some blockchain-network.

To create a new conclient.Client, use conclient.Make. For example:

client := conclient.MakeClient(contract, rpcurl)

func MakeClient

func MakeClient(contract Contract, rpcurl string) Client

MakeClient makes a conclient.Client for contract 'Contract' using RPC-URL 'rpcurl'.

Example:

client := conclient.MakeClient(contract, rpcurl)

func (Client) Call

func (receiver Client) Call(methodname string, parameters ...interface{}) ([]interface{}, error)

Call calls the 'methodname' method on the contract, and returns the results.

func (Client) Chain10Exponent

func (receiver Client) Chain10Exponent() (uint64, bool)

Chain10Exponent returns the exponent of the blockchain-network this client is attached to.

The 'exponent' comes from the following equation that shows up the default-currency-code relates to the smallest-currency-code:

1 [DEFAULT-CURRENCY-CODE] = 10ⁿ [SMALLEST-CURRENCY-CODE]

(Where "n" is the value that Chain10Exponent returns.)

For example, on Ethereum:

1 ETH = 1,000,000,000,000,000,000 wei = 10¹⁸ wei

Therefore Chain10Exponent would return: 18

And also, for example, on Avalanche:

1 AVAX = 1,000,000,000 nanoAVAX = 10⁹ nanoAVAX

Therefore Chain10Exponent would return: 9

The reason this number is useful is this —

Often you will be working with numbers that aer in the smallest-currency of the blockchain-network. (Ex: "wei" rather than "ETH".) But you need to display things in the default-currency. (Ex: "ETH" rather than "wei".) So you need to do a conversion. (Ex: convert "wei" to "ETH".) The number Chain10Exponent returns will enable you to do that conversion.

For example:

var chainid uint64 = // ...

// ...

var wei *big.Int = // ...

// ...

var exponent *big.Int = new(big.Int).SetUint64( chain10.Exponent(chainid) )

var conversionDenominator *big.Int = new(big.Int).Exp(10, exponent)

var ethrat *big.Rat = new(big.Rat).SetFrac(wei, conversionDenominator)
var eth *big.Float = new(big.Float).SetRat(ethrat)

func (Client) ChainCode

func (receiver Client) ChainCode() string

ChainCode returns the (curency) chain-code of the blockchain-network this client is attached to.

See https://chainlist.org/ for the list of chain-ids mapped to (currency) chain-codes.

func (Client) ChainID

func (receiver Client) ChainID() uint64

ChainID returns the chain-id of the blockchain-network this client is attached to.

See https://chainlist.org/ for the list of chain-ids.

func (Client) ChainName

func (receiver Client) ChainName() string

ChainName returns the (human-legible) chain-name of the blockchain-network this client is attached to.

See https://chainlist.org/ for the list of chain-ids mapped to chain-names.

func (Client) ContractAddress

func (receiver Client) ContractAddress() ethaddr.Address

func (Client) CurrentBlockNumber

func (receiver Client) CurrentBlockNumber() (*big.Int, error)

CurrentBlockNumber returns the current (newest) block-number for the blockchain-network which this client is conencted to.

Each blockchain-network will (likely) have a different current (newest) blockchain-number.

And each blockchain-network's current (newest) block-number will change over time.

Example:

currentBlockNumber, err := client.CurrentBlockNumber()

type Contract

type Contract struct {
	ABI             abi.ABI
	Address         ethaddr.Address
	ChainID         uint64
	FromBlockNumber uint64
}

Contract represents a smart-contract (specified by its 'Address') on a particular blockchain-network (specified by its `ChainID`) which was created at a particular block-number (specified by 'FromBlockNumber').

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL