ethclient

package module
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2024 License: GPL-3.0 Imports: 20 Imported by: 0

README

ethclient

Description

Extension ethclient.

Prerequisites

golang

Quick Start

package main

import (
	"context"
	"fmt"
	"math/big"
	"time"

	"github.com/ivanzzeth/ethclient"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/crypto"
)

func main() {
	// The private key of your wallet address.
	privateKey, _ := crypto.HexToECDSA("9a01f5c57e377e0239e6036b7b2d700454b760b2dab51390f1eeb2f64fe98b68")

	// Dial Client.
	chainUrl := "ws://localhost:8546"
	client, err := ethclient.Dial(chainUrl)
	if err != nil {
		panic(err)
	}
	defer client.Close()

	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
	defer cancel()

	// The address your want to send to.
	to := common.HexToAddress("0x06514D014e997bcd4A9381bF0C4Dc21bD32718D4")

	// Send single transaction.
	tx, err := client.SendMsg(ctx, ethclient.Message{
		To:         &to,
		PrivateKey: privateKey,
		Value:      big.NewInt(0),
	})

	if err != nil {
		fmt.Printf("Send single message err: %v\n", err)
		return
	}

	// Waiting n confirmations.
	contains, err := client.ConfirmTx(tx.Hash(), 2, 5*time.Second)
	if err != nil {
		panic(err)
	}

	if !contains {
		fmt.Printf("The transaction %v is not contained at blockchain", tx.Hash().Hex())
	} else {
		receipt, err := client.RawClient().TransactionReceipt(ctx, tx.Hash())
		// do something.
		_, _ = receipt, err
	}

	fmt.Println("Send single message successful, txHash:", tx.Hash().Hex())

	// Send multiple transactions.
	mesgs := make(chan ethclient.Message)
	txs, errs := client.ScheduleMsg(ctx, mesgs)
	go func() {
		for i := 0; i < 5; i++ {
			mesgs <- ethclient.Message{
				PrivateKey: privateKey,
				To:         &to,
			}
		}

		close(mesgs)
	}()

	for tx := range txs {
        fmt.Printf("Send multiple message successful, txHash: %v, nonce: %v, err: %v\n",
            tx.Hash().Hex(), tx.Nonce(), <-errs)
	}
}

Setup local node for testing

you should install foundry before running the script below:

./run_local_node.sh

License

The ethclient library is licensed under the GNU General Public License v3.0, also included in our repository in the COPYING.LESSER file.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetDialer added in v0.0.2

func SetDialer(d Dialer)

Types

type Client

type Client struct {
	*ethclient.Client

	nonce.Manager

	subscriber.Subscriber
	// contains filtered or unexported fields
}

func Dial

func Dial(rawurl string) (*Client, error)

func NewClient

func NewClient(
	c *rpc.Client,
	msgStore message.Storage,
	nonceManager nonce.Manager,
	subscriber subscriber.Subscriber,
	sequencer message.Sequencer,
) (*Client, error)

func NewMemoryClient

func NewMemoryClient(c *rpc.Client) (*Client, error)

func (*Client) AddABI

func (c *Client) AddABI(intf abi.ABI)

Trying to decode some data using the abi if specific

func (*Client) CallAndSendMsg

func (c *Client) CallAndSendMsg(ctx context.Context, msg message.Request) (*types.Transaction, []byte, error)

func (*Client) CallContract

func (c *Client) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)

func (*Client) CallContractAtHash

func (c *Client) CallContractAtHash(ctx context.Context, msg ethereum.CallMsg, blockHash common.Hash) ([]byte, error)

func (*Client) CallMsg

func (c *Client) CallMsg(ctx context.Context, msg message.Request, blockNumber *big.Int) (returnData []byte, err error)

func (*Client) Close

func (c *Client) Close()

func (*Client) CloseSendMsg

func (c *Client) CloseSendMsg()

func (*Client) DebugTransactionOnChain

func (c *Client) DebugTransactionOnChain(ctx context.Context, txHash common.Hash) ([]byte, error)

func (*Client) DecodeJsonRpcError

func (c *Client) DecodeJsonRpcError(err error) error

func (*Client) EstimateGas

func (c *Client) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error)

func (*Client) FilterLogs

func (c *Client) FilterLogs(ctx context.Context, q ethereum.FilterQuery) (logs []types.Log, err error)

func (*Client) GetMsg

func (c *Client) GetMsg(msgId common.Hash) (message.Message, error)

func (*Client) GetSigner

func (c *Client) GetSigner() bind.SignerFn

func (*Client) MessageToTransactOpts

func (c *Client) MessageToTransactOpts(ctx context.Context, msg message.Request) (*bind.TransactOpts, error)

MessageToTransactOpts . NOTE: You must provide private key for signature.

func (*Client) NewMethodData

func (c *Client) NewMethodData(a abi.ABI, methodName string, args ...interface{}) ([]byte, error)

func (*Client) NewTransaction

func (c *Client) NewTransaction(ctx context.Context, msg message.Request) (*types.Transaction, error)

func (*Client) PendingCallContract

func (c *Client) PendingCallContract(ctx context.Context, msg ethereum.CallMsg) ([]byte, error)

func (*Client) PendingNonceAt

func (c *Client) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)

func (*Client) RawClient

func (c *Client) RawClient() *ethclient.Client

RawClient returns underlying ethclient

func (*Client) RegisterPrivateKey

func (c *Client) RegisterPrivateKey(ctx context.Context, key *ecdsa.PrivateKey) error

Registers the private key used for signing txs.

func (*Client) RegisterSigner

func (c *Client) RegisterSigner(signerFn bind.SignerFn)

func (*Client) ReplayMsg

func (c *Client) ReplayMsg(msgId common.Hash) (newMsgId common.Hash, err error)

func (*Client) ScheduleMsg

func (c *Client) ScheduleMsg(req message.Request)

func (*Client) ScheduleMsgResponse

func (c *Client) ScheduleMsgResponse() <-chan message.Response

func (*Client) SendMsg

func (c *Client) SendMsg(ctx context.Context, msg message.Request) (signedTx *types.Transaction, err error)

func (*Client) SetMsgBuffer

func (c *Client) SetMsgBuffer(buffer int)

func (*Client) SetNonceManager

func (c *Client) SetNonceManager(nm nonce.Manager)

func (*Client) SetSubscriber

func (c *Client) SetSubscriber(s subscriber.Subscriber)

func (*Client) SubscribeFilterLogs

func (c *Client) SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error)

func (*Client) SubscribeNewHead

func (c *Client) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error)

func (*Client) SuggestGasPrice

func (c *Client) SuggestGasPrice(ctx context.Context) (gasPrice *big.Int, err error)

func (*Client) WaitTxReceipt

func (c *Client) WaitTxReceipt(txHash common.Hash, confirmations uint64, timeout time.Duration) (*types.Receipt, bool)

type Dialer added in v0.0.2

type Dialer = func(rawurl string) (EthClientInterface, error)

type EthClientInterface added in v0.0.2

type EthClientInterface interface {
	bind.ContractBackend
	bind.DeployBackend
	bind.PendingContractCaller
	ethereum.ChainReader
	ethereum.TransactionReader
	ethereum.ChainStateReader
	ethereum.ContractCaller
	ethereum.LogFilterer
	ethereum.TransactionSender
	ethereum.GasPricer
	ethereum.GasPricer1559
	ethereum.FeeHistoryReader
	ethereum.PendingStateReader
	ethereum.PendingContractCaller
	ethereum.GasEstimator
	ethereum.BlockNumberReader
	ethereum.ChainIDReader
}

func DialOnce added in v0.0.2

func DialOnce(url string) EthClientInterface

Jump to

Keyboard shortcuts

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