ethrpc

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2023 License: MIT Imports: 11 Imported by: 111

README

Ethrpc

Tests Coverage Status Go Report Card PkgGoDev

Golang client for ethereum JSON RPC API.

Beer for author by Ethereum: 0xf4144308d6D67A1F00a61A596c0eB7B08411344a

Usage:
package main

import (
    "log"

    "github.com/onrik/ethrpc"
)

func main() {
    client := ethrpc.New("http://127.0.0.1:8545")

    version, err := client.Web3ClientVersion()
    if err != nil {
        log.Println(err)
        return
    }
    log.Println(version)

    // Send 1 eth
    txid, err := client.EthSendTransaction(ethrpc.T{
        From:  "0x6247cf0412c6462da2a51d05139e2a3c6c630f0a",
        To:    "0xcfa202c4268749fbb5136f2b68f7402984ed444b",
        Value: ethrpc.Eth1(),
    })
    if err != nil {
        log.Println(err)
        return
    }
    log.Println(txid)
}
Methods:
  • web3_clientVersion
  • web3_sha3
  • net_version
  • net_peerCount
  • net_listening
  • eth_protocolVersion
  • eth_syncing
  • eth_coinbase
  • eth_mining
  • eth_hashrate
  • eth_gasPrice
  • eth_accounts
  • eth_blockNumber
  • eth_getBalance
  • eth_getStorageAt
  • eth_getTransactionCount
  • eth_getBlockTransactionCountByHash
  • eth_getBlockTransactionCountByNumber
  • eth_getUncleCountByBlockHash
  • eth_getUncleCountByBlockNumber
  • eth_getCode
  • eth_sign
  • eth_sendTransaction
  • eth_sendRawTransaction
  • eth_call
  • eth_estimateGas
  • eth_getBlockByHash
  • eth_getBlockByNumber
  • eth_getTransactionByHash
  • eth_getTransactionByBlockHashAndIndex
  • eth_getTransactionByBlockNumberAndIndex
  • eth_getTransactionReceipt
  • eth_pendingTransactions
  • eth_getUncleByBlockHashAndIndex
  • eth_getUncleByBlockNumberAndIndex
  • eth_getCompilers (DEPRECATED)
  • eth_compileLLL (DEPRECATED)
  • eth_compileSolidity (DEPRECATED)
  • eth_compileSerpent (DEPRECATED)
  • eth_newFilter
  • eth_newBlockFilter
  • eth_newPendingTransactionFilter
  • eth_uninstallFilter
  • eth_getFilterChanges
  • eth_getFilterLogs
  • eth_getLogs
  • eth_getWork
  • eth_submitWork
  • eth_submitHashrate
  • eth_getProof
  • db_putString
  • db_getString
  • db_putHex
  • db_getHex
  • shh_post
  • shh_version
  • shh_newIdentity
  • shh_hasIdentity
  • shh_newGroup
  • shh_addToGroup
  • shh_newFilter
  • shh_uninstallFilter
  • shh_getFilterChanges
  • shh_getMessages

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BigToHex

func BigToHex(bigInt big.Int) string

BigToHex covert big.Int to hexadecimal representation

func Eth1

func Eth1() *big.Int

Eth1 returns 1 ethereum value (10^18 wei)

func IntToHex

func IntToHex(i int) string

IntToHex convert int to hexadecimal representation

func ParseBigInt

func ParseBigInt(value string) (big.Int, error)

ParseBigInt parse hex string value to big.Int

func ParseInt

func ParseInt(value string) (int, error)

ParseInt parse hex string value to int

func WithDebug

func WithDebug(enabled bool) func(rpc *EthRPC)

WithDebug set debug flag

func WithHttpClient

func WithHttpClient(client httpClient) func(rpc *EthRPC)

WithHttpClient set custom http client

func WithLogger

func WithLogger(l logger) func(rpc *EthRPC)

WithLogger set custom logger

Types

type Block

type Block struct {
	Number           int
	Hash             string
	ParentHash       string
	Nonce            string
	Sha3Uncles       string
	LogsBloom        string
	TransactionsRoot string
	StateRoot        string
	Miner            string
	Difficulty       big.Int
	TotalDifficulty  big.Int
	ExtraData        string
	Size             int
	GasLimit         int
	GasUsed          int
	Timestamp        int
	Uncles           []string
	Transactions     []Transaction
}

Block - block object

type EthError

type EthError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

EthError - ethereum error

func (EthError) Error

func (err EthError) Error() string

type EthRPC

type EthRPC struct {
	Debug bool
	// contains filtered or unexported fields
}

EthRPC - Ethereum rpc client

func New

func New(url string, options ...func(rpc *EthRPC)) *EthRPC

New create new rpc client with given url

func NewEthRPC

func NewEthRPC(url string, options ...func(rpc *EthRPC)) *EthRPC

NewEthRPC create new rpc client with given url

func (*EthRPC) Call

func (rpc *EthRPC) Call(method string, params ...interface{}) (json.RawMessage, error)

Call returns raw response of method call

func (*EthRPC) Eth1

func (rpc *EthRPC) Eth1() *big.Int

Eth1 returns 1 ethereum value (10^18 wei)

func (*EthRPC) EthAccounts

func (rpc *EthRPC) EthAccounts() ([]string, error)

EthAccounts returns a list of addresses owned by client.

func (*EthRPC) EthBlockNumber

func (rpc *EthRPC) EthBlockNumber() (int, error)

EthBlockNumber returns the number of most recent block.

func (*EthRPC) EthCall

func (rpc *EthRPC) EthCall(transaction T, tag string) (string, error)

EthCall executes a new message call immediately without creating a transaction on the block chain.

func (*EthRPC) EthCoinbase

func (rpc *EthRPC) EthCoinbase() (string, error)

EthCoinbase returns the client coinbase address

func (*EthRPC) EthEstimateGas

func (rpc *EthRPC) EthEstimateGas(transaction T) (int, error)

EthEstimateGas makes a call or transaction, which won't be added to the blockchain and returns the used gas, which can be used for estimating the used gas.

func (*EthRPC) EthGasPrice

func (rpc *EthRPC) EthGasPrice() (big.Int, error)

EthGasPrice returns the current price per gas in wei.

func (*EthRPC) EthGetBalance

func (rpc *EthRPC) EthGetBalance(address, block string) (big.Int, error)

EthGetBalance returns the balance of the account of given address in wei.

func (*EthRPC) EthGetBlockByHash

func (rpc *EthRPC) EthGetBlockByHash(hash string, withTransactions bool) (*Block, error)

EthGetBlockByHash returns information about a block by hash.

func (*EthRPC) EthGetBlockByNumber

func (rpc *EthRPC) EthGetBlockByNumber(number int, withTransactions bool) (*Block, error)

EthGetBlockByNumber returns information about a block by block number.

func (*EthRPC) EthGetBlockTransactionCountByHash

func (rpc *EthRPC) EthGetBlockTransactionCountByHash(hash string) (int, error)

EthGetBlockTransactionCountByHash returns the number of transactions in a block from a block matching the given block hash.

func (*EthRPC) EthGetBlockTransactionCountByNumber

func (rpc *EthRPC) EthGetBlockTransactionCountByNumber(number int) (int, error)

EthGetBlockTransactionCountByNumber returns the number of transactions in a block from a block matching the given block

func (*EthRPC) EthGetCode

func (rpc *EthRPC) EthGetCode(address, block string) (string, error)

EthGetCode returns code at a given address.

func (*EthRPC) EthGetCompilers

func (rpc *EthRPC) EthGetCompilers() ([]string, error)

EthGetCompilers returns a list of available compilers in the client.

func (*EthRPC) EthGetFilterChanges

func (rpc *EthRPC) EthGetFilterChanges(filterID string) ([]Log, error)

EthGetFilterChanges polling method for a filter, which returns an array of logs which occurred since last poll.

func (*EthRPC) EthGetFilterLogs

func (rpc *EthRPC) EthGetFilterLogs(filterID string) ([]Log, error)

EthGetFilterLogs returns an array of all logs matching filter with given id.

func (*EthRPC) EthGetLogs

func (rpc *EthRPC) EthGetLogs(params FilterParams) ([]Log, error)

EthGetLogs returns an array of all logs matching a given filter object.

func (*EthRPC) EthGetStorageAt

func (rpc *EthRPC) EthGetStorageAt(data string, position int, tag string) (string, error)

EthGetStorageAt returns the value from a storage position at a given address.

func (*EthRPC) EthGetTransactionByBlockHashAndIndex

func (rpc *EthRPC) EthGetTransactionByBlockHashAndIndex(blockHash string, transactionIndex int) (*Transaction, error)

EthGetTransactionByBlockHashAndIndex returns information about a transaction by block hash and transaction index position.

func (*EthRPC) EthGetTransactionByBlockNumberAndIndex

func (rpc *EthRPC) EthGetTransactionByBlockNumberAndIndex(blockNumber, transactionIndex int) (*Transaction, error)

EthGetTransactionByBlockNumberAndIndex returns information about a transaction by block number and transaction index position.

func (*EthRPC) EthGetTransactionByHash

func (rpc *EthRPC) EthGetTransactionByHash(hash string) (*Transaction, error)

EthGetTransactionByHash returns the information about a transaction requested by transaction hash.

func (*EthRPC) EthGetTransactionCount

func (rpc *EthRPC) EthGetTransactionCount(address, block string) (int, error)

EthGetTransactionCount returns the number of transactions sent from an address.

func (*EthRPC) EthGetTransactionReceipt

func (rpc *EthRPC) EthGetTransactionReceipt(hash string) (*TransactionReceipt, error)

EthGetTransactionReceipt returns the receipt of a transaction by transaction hash. Note That the receipt is not available for pending transactions.

func (*EthRPC) EthGetUncleCountByBlockHash

func (rpc *EthRPC) EthGetUncleCountByBlockHash(hash string) (int, error)

EthGetUncleCountByBlockHash returns the number of uncles in a block from a block matching the given block hash.

func (*EthRPC) EthGetUncleCountByBlockNumber

func (rpc *EthRPC) EthGetUncleCountByBlockNumber(number int) (int, error)

EthGetUncleCountByBlockNumber returns the number of uncles in a block from a block matching the given block number.

func (*EthRPC) EthHashrate

func (rpc *EthRPC) EthHashrate() (int, error)

EthHashrate returns the number of hashes per second that the node is mining with.

func (*EthRPC) EthMining

func (rpc *EthRPC) EthMining() (bool, error)

EthMining returns true if client is actively mining new blocks.

func (*EthRPC) EthNewBlockFilter

func (rpc *EthRPC) EthNewBlockFilter() (string, error)

EthNewBlockFilter creates a filter in the node, to notify when a new block arrives. To check if the state has changed, call EthGetFilterChanges.

func (*EthRPC) EthNewFilter

func (rpc *EthRPC) EthNewFilter(params FilterParams) (string, error)

EthNewFilter creates a new filter object.

func (*EthRPC) EthNewPendingTransactionFilter

func (rpc *EthRPC) EthNewPendingTransactionFilter() (string, error)

EthNewPendingTransactionFilter creates a filter in the node, to notify when new pending transactions arrive. To check if the state has changed, call EthGetFilterChanges.

func (*EthRPC) EthProtocolVersion

func (rpc *EthRPC) EthProtocolVersion() (string, error)

EthProtocolVersion returns the current ethereum protocol version.

func (*EthRPC) EthSendRawTransaction

func (rpc *EthRPC) EthSendRawTransaction(data string) (string, error)

EthSendRawTransaction creates new message call transaction or a contract creation for signed transactions.

func (*EthRPC) EthSendTransaction

func (rpc *EthRPC) EthSendTransaction(transaction T) (string, error)

EthSendTransaction creates new message call transaction or a contract creation, if the data field contains code.

func (*EthRPC) EthSign

func (rpc *EthRPC) EthSign(address, data string) (string, error)

EthSign signs data with a given address. Calculates an Ethereum specific signature with: sign(keccak256("\x19Ethereum Signed Message:\n" + len(message) + message)))

func (*EthRPC) EthSyncing

func (rpc *EthRPC) EthSyncing() (*Syncing, error)

EthSyncing returns an object with data about the sync status or false.

func (*EthRPC) EthUninstallFilter

func (rpc *EthRPC) EthUninstallFilter(filterID string) (bool, error)

EthUninstallFilter uninstalls a filter with given id.

func (*EthRPC) NetListening

func (rpc *EthRPC) NetListening() (bool, error)

NetListening returns true if client is actively listening for network connections.

func (*EthRPC) NetPeerCount

func (rpc *EthRPC) NetPeerCount() (int, error)

NetPeerCount returns number of peers currently connected to the client.

func (*EthRPC) NetVersion

func (rpc *EthRPC) NetVersion() (string, error)

NetVersion returns the current network protocol version.

func (*EthRPC) RawCall

func (rpc *EthRPC) RawCall(method string, params ...interface{}) (json.RawMessage, error)

RawCall returns raw response of method call (Deprecated)

func (*EthRPC) URL

func (rpc *EthRPC) URL() string

URL returns client url

func (*EthRPC) Web3ClientVersion

func (rpc *EthRPC) Web3ClientVersion() (string, error)

Web3ClientVersion returns the current client version.

func (*EthRPC) Web3Sha3

func (rpc *EthRPC) Web3Sha3(data []byte) (string, error)

Web3Sha3 returns Keccak-256 (not the standardized SHA3-256) of the given data.

type EthereumAPI

type EthereumAPI interface {
	Web3ClientVersion() (string, error)
	Web3Sha3(data []byte) (string, error)
	NetVersion() (string, error)
	NetListening() (bool, error)
	NetPeerCount() (int, error)
	EthProtocolVersion() (string, error)
	EthSyncing() (*Syncing, error)
	EthCoinbase() (string, error)
	EthMining() (bool, error)
	EthHashrate() (int, error)
	EthGasPrice() (big.Int, error)
	EthAccounts() ([]string, error)
	EthBlockNumber() (int, error)
	EthGetBalance(address, block string) (big.Int, error)
	EthGetStorageAt(data string, position int, tag string) (string, error)
	EthGetTransactionCount(address, block string) (int, error)
	EthGetBlockTransactionCountByHash(hash string) (int, error)
	EthGetBlockTransactionCountByNumber(number int) (int, error)
	EthGetUncleCountByBlockHash(hash string) (int, error)
	EthGetUncleCountByBlockNumber(number int) (int, error)
	EthGetCode(address, block string) (string, error)
	EthSign(address, data string) (string, error)
	EthSendTransaction(transaction T) (string, error)
	EthSendRawTransaction(data string) (string, error)
	EthCall(transaction T, tag string) (string, error)
	EthEstimateGas(transaction T) (int, error)
	EthGetBlockByHash(hash string, withTransactions bool) (*Block, error)
	EthGetBlockByNumber(number int, withTransactions bool) (*Block, error)
	EthGetTransactionByHash(hash string) (*Transaction, error)
	EthGetTransactionByBlockHashAndIndex(blockHash string, transactionIndex int) (*Transaction, error)
	EthGetTransactionByBlockNumberAndIndex(blockNumber, transactionIndex int) (*Transaction, error)
	EthGetTransactionReceipt(hash string) (*TransactionReceipt, error)
	EthGetCompilers() ([]string, error)
	EthNewFilter(params FilterParams) (string, error)
	EthNewBlockFilter() (string, error)
	EthNewPendingTransactionFilter() (string, error)
	EthUninstallFilter(filterID string) (bool, error)
	EthGetFilterChanges(filterID string) ([]Log, error)
	EthGetFilterLogs(filterID string) ([]Log, error)
	EthGetLogs(params FilterParams) ([]Log, error)
}

type FilterParams

type FilterParams struct {
	FromBlock string     `json:"fromBlock,omitempty"`
	ToBlock   string     `json:"toBlock,omitempty"`
	Address   []string   `json:"address,omitempty"`
	Topics    [][]string `json:"topics,omitempty"`
}

FilterParams - Filter parameters object

type Log

type Log struct {
	Removed          bool
	LogIndex         int
	TransactionIndex int
	TransactionHash  string
	BlockNumber      int
	BlockHash        string
	Address          string
	Data             string
	Topics           []string
}

Log - log object

func (*Log) UnmarshalJSON

func (log *Log) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type Syncing

type Syncing struct {
	IsSyncing     bool
	StartingBlock int
	CurrentBlock  int
	HighestBlock  int
}

Syncing - object with syncing data info

func (*Syncing) UnmarshalJSON

func (s *Syncing) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type T

type T struct {
	From     string
	To       string
	Gas      int
	GasPrice *big.Int
	Value    *big.Int
	Data     string
	Nonce    int
}

T - input transaction object

func (T) MarshalJSON

func (t T) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Unmarshaler interface.

type Transaction

type Transaction struct {
	Hash             string
	Nonce            int
	BlockHash        string
	BlockNumber      *int
	TransactionIndex *int
	From             string
	To               string
	Value            big.Int
	Gas              int
	GasPrice         big.Int
	Input            string
}

Transaction - transaction object

func (*Transaction) UnmarshalJSON

func (t *Transaction) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type TransactionReceipt

type TransactionReceipt struct {
	TransactionHash   string
	TransactionIndex  int
	BlockHash         string
	BlockNumber       int
	CumulativeGasUsed int
	GasUsed           int
	ContractAddress   string
	Logs              []Log
	LogsBloom         string
	Root              string
	Status            string
}

TransactionReceipt - transaction receipt object

func (*TransactionReceipt) UnmarshalJSON

func (t *TransactionReceipt) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

Jump to

Keyboard shortcuts

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