Documentation
¶
Overview ¶
Package ethclient provides an Ethereum blockchain client that implements the types.ChainClient interface. It supports balance queries, transaction monitoring, and coin/token transfers via JSON-RPC.
Index ¶
- Constants
- Variables
- func GetAddressCodec() address.AddressCodec
- func WeiToEtherFloat(wei *big.Int) *big.Float
- func WeiToEtherString(wei *big.Int) string
- type AddressCodec
- func (a *AddressCodec) DecodeAddressToBytes(addressStr string) ([]byte, error)
- func (a *AddressCodec) EncodeBytesToAddress(addressBytes []byte) (string, error)
- func (a *AddressCodec) IsValid(address string) bool
- func (a *AddressCodec) PrivateKeyToAddress(privateKey []byte) (address string, addressBytes []byte, err error)
- type Block
- func (b *Block) GetTransactions() (txs []*Transaction, err error)
- func (b *Block) GetTransactionsHashes() (txs []string)
- func (b *Block) UnmarshalJSON(data []byte) (err error)
- func (b *Block) WalkTransactions(view func(tx *Transaction) (stop bool)) error
- func (b *Block) WalkTransactionsHashes(view func(tx string) (stop bool))
- type Client
- func (c *Client) BalanceOf(address string) (balance *big.Int, err error)
- func (c *Client) BlockByHash(blockHash string, fullInfo bool) (block *types.BlockInfo, err error)
- func (c *Client) BlockByNum(blockNum int64, fullInfo bool) (block *types.BlockInfo, err error)
- func (c *Client) BlockNum() (blockNum int64, err error)
- func (c *Client) Call(contractAddress, data string) (callResult string, err error)
- func (c *Client) CallByBlockNumber(contractAddress, data string, blockNumber int64) (callResult string, err error)
- func (c *Client) ContractGetBalanceOf(contractAddress, address string) (balance *big.Int, err error)
- func (c *Client) Decimals() (decimals int)
- func (c *Client) GasPrice() (*big.Int, error)
- func (c *Client) GetAddressCodec() address.AddressCodec
- func (c *Client) GetBalance(address string) (*big.Int, error)
- func (c *Client) GetBlockByHash(hash string, fullTransactions bool) (*Block, error)
- func (c *Client) GetBlockByNumber(number int64, fullTransactions bool) (*Block, error)
- func (c *Client) GetBlockNumber() (int64, error)
- func (c *Client) GetChainId() (chainId string)
- func (c *Client) GetChainName() (chainName string)
- func (c *Client) GetChainSymbol() (chainSymbol string)
- func (c *Client) GetEstimatedFee(from, to, data string, amount *big.Int) (fee, gasPrice *big.Int, gas int64, err error)
- func (c *Client) GetEstimatedGas(from, to, data string, amount *big.Int) (gas int64, err error)
- func (c *Client) GetEstimatedGasPrice() (gasPrice *big.Int, err error)
- func (c *Client) GetNetId() (netId int64, err error)
- func (c *Client) GetTransactionByBlockHashAndIndex(hash string, index int) (*Transaction, error)
- func (c *Client) GetTransactionByBlockNumberAndIndex(blockNumber int64, index int) (*Transaction, error)
- func (c *Client) GetTransactionByHash(hash string) (*Transaction, error)
- func (c *Client) GetTxPoolContent() (pending, queued map[string]map[string]*Transaction, err error)
- func (c *Client) Init() error
- func (c *Client) MemPoolContent() (poolContent []*types.TransferInfo, err error)
- func (c *Client) MinConfirmations() (confirmations int)
- func (c *Client) PendingNonceAt(address string) (nonce int64, err error)
- func (c *Client) SendRawTransaction(data string) (txHash string, err error)
- func (c *Client) SetConfirmations(confirmations int)
- func (c *Client) TokenProtocols() []string
- func (c *Client) TokensBalanceOf(address string, token string) (balance *big.Int, err error)
- func (c *Client) TokensList() (tokensList []*types.TokenInfo)
- func (c *Client) TransactionSendRaw(rawTx []byte) (txHash string, err error)
- func (c *Client) TransferAllByPrivateKey(fromPrivateKey []byte, from, to string) (txHash string, err error)
- func (c *Client) TransferAllTokenByPrivateKey(fromPrivateKey []byte, from, to string, token string) (txHash string, err error)
- func (c *Client) TransferByPrivateKey(fromPrivateKey []byte, from, to string, amount *big.Int) (txHash string, err error)
- func (c *Client) TransferGetEstimatedFee(from, to string, amount *big.Int) (fee *big.Int, err error)
- func (c *Client) TransferInfoByHash(txHash string) (tx *types.TransferInfo, err error)
- func (c *Client) TransferInfoByNum(blockNum int64, txIndex int) (tx *types.TransferInfo, err error)
- func (c *Client) TransferTokenByPrivateKey(fromPrivateKey []byte, from, to string, amount *big.Int, token string) (txHash string, err error)
- func (c *Client) TransferTokenGetEstimatedFee(from, to string, amount *big.Int, token string) (fee *big.Int, err error)
- type Config
- type EthAddress
- type Option
- type Transaction
Constants ¶
const ( // DEFAULT_CONFIRMATIONS is the default number of block confirmations // required before a transaction is considered finalized. DEFAULT_CONFIRMATIONS = 12 )
Default confirmation count constants.
Variables ¶
var ( ErrHashesOnlyBlockHash = errors.New("only transactions hashes requested") ErrTransactionNotFound = errors.New("transaction not found") ErrInvalidAddressCheckSum = errors.New("invalid address checksum") ErrInvalidAddress = errors.New("invalid address") ErrTransactionNotTransfer = errors.New("transaction not transfer") ErrUnsupportedTransactionFormat = errors.New("unsupported transaction format") ErrUnsupportedTransactionType = errors.New("unsupported transaction type") ErrTransactionSignError = errors.New("transaction sign error") ErrInsufficientFunds = errors.New("insufficient funds") ErrNothingToTransfer = errors.New("nothing to transfer") ErrConfigStorageEmpty = errors.New("config storage is empty") ErrUnknownToken = errors.New("unknown token") )
Functions ¶
func GetAddressCodec ¶
func GetAddressCodec() address.AddressCodec
GetAddressCodec returns an Ethereum address codec instance.
func WeiToEtherString ¶
Types ¶
type AddressCodec ¶
type AddressCodec struct {
}
AddressCodec implements address.AddressCodec for Ethereum addresses. Handles checksummed address encoding/decoding per EIP-55.
func (*AddressCodec) DecodeAddressToBytes ¶
func (a *AddressCodec) DecodeAddressToBytes(addressStr string) ([]byte, error)
DecodeAddressToBytes converts an Ethereum address string to 20 bytes.
func (*AddressCodec) EncodeBytesToAddress ¶
func (a *AddressCodec) EncodeBytesToAddress(addressBytes []byte) (string, error)
EncodeBytesToAddress converts 20 bytes to a checksummed Ethereum address string.
func (*AddressCodec) IsValid ¶
func (a *AddressCodec) IsValid(address string) bool
IsValid checks if an address string is a valid Ethereum address.
func (*AddressCodec) PrivateKeyToAddress ¶
func (a *AddressCodec) PrivateKeyToAddress(privateKey []byte) (address string, addressBytes []byte, err error)
PrivateKeyToAddress derives an Ethereum address from a private key. Uses Keccak-256 hash of the public key, taking the last 20 bytes.
type Block ¶
type Block struct {
// FullTransactions set to true if "returns the full transaction"
// parameter set
FullTransactions bool `json:"hashesOnly"`
// Number The block number. null when its pending block.
Number int64 `json:"number"`
// Hash 32 Bytes - hash of the block. null when its pending block.
Hash string `json:"hash"`
// ParentHash 32 Bytes - hash of the parent block.
ParentHash string `json:"parentHash"`
// ParentBeaconBlockRoot 32 Bytes - hash of the parent beacon block.
ParentBeaconBlockRoot string `json:"parentBeaconBlockRoot"`
// Nonce 8 Bytes - hash of the generated proof-of-work. null when its pending block.
Nonce uint64 `json:"nonce"`
// Sha3Uncles 32 Bytes - SHA3 of the uncles data in the block.
Sha3Uncles string `json:"sha3Uncles"`
// LogsBloom 256 Bytes - the bloom filter for the logs of the block.
// null when its pending block.
LogsBloom string `json:"logsBloom"`
// TransactionsRoot 32 Bytes - the root of the transaction
// trie of the block.
TransactionsRoot string `json:"transactionsRoot"`
// StateRoot 32 Bytes - the root of the final state trie of the block.
StateRoot string `json:"stateRoot"`
// ReceiptsRoot 32 Bytes - the root of the receipts trie of the block.
ReceiptsRoot string `json:"receiptsRoot"`
// Miner DATA, 20 Bytes - the address of the beneficiary to whom the mining
Miner string `json:"miner"`
// BaseFeePerGas A string of the base fee encoded in hexadecimal format.
// Please note that this response field will not be included in a block
// requested before the EIP-1559 upgrade
BaseFeePerGas *big.Int `json:"baseFeePerGas"`
// Difficulty The integer of the difficulty for this block
// encoded as a hexadecimal
Difficulty int64 `json:"difficulty"`
// TotalDifficulty integer of the difficulty for this block
// encoded as a hexadecimal
TotalDifficulty string `json:"totalDifficulty"`
// ExtraData The "extra data" field of this block
ExtraData string `json:"extraData"`
// Size integer the size of this block in bytes.
Size int64 `json:"size"`
// GasLimit the maximum gas allowed in this block
GasLimit int64 `json:"gasLimit"`
// GasUsed the total used gas by all transactions in this block.
GasUsed int64 `json:"gasUsed"`
// Timestamp the unix timestamp for when the block was collated.
Timestamp int64 `json:"timestamp"`
// BlobGasUsed
BlobGasUsed string `json:"blobGasUsed"`
ExcessBlobGas string `json:"excessBlobGas"`
MixHash string `json:"mixHash"`
// Transactions Array of transaction objects, or 32 Bytes
// transaction hashes depending on the last given parameter.
Transactions json.RawMessage `json:"transactions"`
// Uncles Array of uncle hashes.
Uncles []string `json:"uncles"`
Withdrawals json.RawMessage `json:"withdrawals"`
WithdrawalsRoot string `json:"withdrawalsRoot"`
// contains filtered or unexported fields
}
func (*Block) GetTransactions ¶
func (b *Block) GetTransactions() (txs []*Transaction, err error)
GetTransactions returns the transactions from the block. If the block was requested without full transactions, it will return error
func (*Block) GetTransactionsHashes ¶
GetTransactionsHashes returns the transaction hashes from the block.
func (*Block) UnmarshalJSON ¶
UnmarshalJSON Since Ethereum uses non-standard encoding of integer data (0x prefixed hex) in its RPC responses, we need to implement full decoding of transactions and blocks through a special method.
func (*Block) WalkTransactions ¶
func (b *Block) WalkTransactions(view func(tx *Transaction) (stop bool)) error
WalkTransactions walks through the transactions of the block and calls the view function for each transaction. If the view function returns true, the walking is stopped. If the block was requested without full transactions, it will return error
func (*Block) WalkTransactionsHashes ¶
WalkTransactionsHashes walks through the transaction hashes of the block and calls the view function for each transaction. If the view function returns true, the walking is stopped.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the main Ethereum blockchain client. It implements the types.ChainClient interface.
func NewClient ¶
NewClient creates a new Ethereum client with the specified options. Default configuration: Ethereum mainnet, 18 decimals, 12 confirmations.
func (*Client) BlockByHash ¶
func (*Client) BlockByNum ¶
func (*Client) Call ¶
Call executes a new message call immediately without creating a transaction on the block chain. By default "latest" is used for the block tag. If you need call with specific block number, use CallByBlockNumber. Often used for executing read-only smart contract functions, for example the balanceOf for an ERC-20 contract.
func (*Client) CallByBlockNumber ¶
func (c *Client) CallByBlockNumber(contractAddress, data string, blockNumber int64) (callResult string, err error)
CallByBlockNumber executes a new message call immediately without creating a transaction on the block chain. Often used for executing read-only smart contract functions, for example the balanceOf for an ERC-20 contract.
func (*Client) ContractGetBalanceOf ¶
func (*Client) GasPrice ¶
GasPrice returns the current price per gas in wei. The gas price is determined by the last few blocks median gas price. You need know the gas price for calculate the fee of transaction send/execute.
func (*Client) GetAddressCodec ¶
func (c *Client) GetAddressCodec() address.AddressCodec
func (*Client) GetBalance ¶
GetBalance returns the balance of the account of given address. The balance is returned in wei. if you want to convert it to ether, use WeiToEther function.
func (*Client) GetBlockByHash ¶
GetBlockByHash returns information about a block by hash. If "fullTransactions" is true it returns the full transaction objects, if "fullTransactions" is false, only the hashes of the transactions.
func (*Client) GetBlockByNumber ¶
GetBlockByNumber returns information about a block by block number. If "fullTransactions" is true it returns the full transaction objects, if "fullTransactions" is false, only the hashes of the transactions.
func (*Client) GetBlockNumber ¶
GetBlockNumber returns the number of most recent block.
func (*Client) GetChainId ¶
func (*Client) GetChainName ¶
func (*Client) GetChainSymbol ¶
func (*Client) GetEstimatedFee ¶
func (*Client) GetEstimatedGas ¶
func (*Client) GetEstimatedGasPrice ¶
func (*Client) GetTransactionByBlockHashAndIndex ¶
func (c *Client) GetTransactionByBlockHashAndIndex(hash string, index int) (*Transaction, error)
GetTransactionByBlockHashAndIndex returns the information about a transaction requested by block hash and tx index. If the transaction not found (geth rpc call return null), it returns error.
func (*Client) GetTransactionByBlockNumberAndIndex ¶
func (c *Client) GetTransactionByBlockNumberAndIndex(blockNumber int64, index int) (*Transaction, error)
GetTransactionByBlockNumberAndIndex returns the information about a transaction requested by block number and tx index. If the transaction not found (geth rpc call return null), it returns error.
func (*Client) GetTransactionByHash ¶
func (c *Client) GetTransactionByHash(hash string) (*Transaction, error)
GetTransactionByHash returns the information about a transaction requested by transaction hash. If the transaction not found (geth rpc call return null), it returns error.
func (*Client) GetTxPoolContent ¶
func (c *Client) GetTxPoolContent() (pending, queued map[string]map[string]*Transaction, err error)
GetTxPoolContent returns the information about the transaction pool. It returns two maps: pending and queued transactions.
func (*Client) Init ¶
Init loads configuration and initializes the client. Must be called before using the client.
func (*Client) MemPoolContent ¶
func (c *Client) MemPoolContent() (poolContent []*types.TransferInfo, err error)
func (*Client) MinConfirmations ¶
func (*Client) PendingNonceAt ¶
func (*Client) SendRawTransaction ¶
SendRawTransaction sends the signed and RPL encoded transaction to the network. In fact, any transaction - transfer of funds, call of a smart contract function or deployment of a smart contract is carried out by calling this function
func (*Client) SetConfirmations ¶
func (*Client) TokenProtocols ¶
func (*Client) TokensBalanceOf ¶
TokensBalanceOf returns the token balance of an address. Accepts token symbol or contract address.
func (*Client) TokensList ¶
func (*Client) TransactionSendRaw ¶
func (*Client) TransferAllByPrivateKey ¶
func (*Client) TransferAllTokenByPrivateKey ¶
func (*Client) TransferByPrivateKey ¶
func (*Client) TransferGetEstimatedFee ¶
func (*Client) TransferInfoByHash ¶
func (c *Client) TransferInfoByHash(txHash string) (tx *types.TransferInfo, err error)
func (*Client) TransferInfoByNum ¶
func (*Client) TransferTokenByPrivateKey ¶
type Config ¶
type EthAddress ¶
type EthAddress string
func (EthAddress) IsValidate ¶
func (a EthAddress) IsValidate() bool
type Option ¶
type Option func(*Client)
Option is a function that configures a Client.
func WithAbiManager ¶
func WithAbiManager(abiManager *abi.SmartContractsManager) Option
func WithConfigStorage ¶
func WithConfigStorage(storage storage.BinStorage) Option
func WithIPCClient ¶
type Transaction ¶
type Transaction struct {
BlockHash string `json:"blockHash"`
BlockNumber int64 `json:"blockNumber"`
Hash string `json:"hash"`
From string `json:"from"`
To string `json:"to"`
Gas int64 `json:"gas"`
GasPrice *big.Int `json:"gasPrice"`
Value *big.Int `json:"value"`
Input string `json:"input"`
Nonce int64 `json:"nonce"`
TransactionIndex int64 `json:"transactionIndex"`
Type int64 `json:"type"`
ChainId int64 `json:"chainId"`
AccessList json.RawMessage `json:"accessList"`
V string `json:"v"`
R string `json:"r"`
S string `json:"s"`
}
func (*Transaction) UnmarshalJSON ¶
func (t *Transaction) UnmarshalJSON(data []byte) (err error)
UnmarshalJSON Since Ethereum uses non-standard encoding of integer data (0x prefixed hex) in its RPC responses, we need to implement full decoding of transactions and blocks through a special method.