ethclient

package module
v0.1.39 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: GPL-3.0 Imports: 22 Imported by: 4

README

ethclient

Description

Extension ethclient.

Prerequisites

golang

Feautres

  • Schedule message
  • Sequence message
  • Protect message
  • Nonce management
  • Concurrent Transaction in Safe Multisig Wallets
  • Multiple rpc url supported

Quick Start

package main

import (
	"fmt"
	"time"

	"github.com/ivanzzeth/ethclient"
	"github.com/ivanzzeth/ethclient/message"
	"github.com/ivanzzeth/ethclient/tests/helper"
)

func main() {
	client, err := ethclient.Dial("http://localhost:8545")
	if err != nil {
		panic(err)
	}
	defer client.Close()

	go func() {
		client.ScheduleMsg((&message.Request{
			From:      helper.Addr,
			To:        &helper.Addr,
			StartTime: time.Now().Add(5 * time.Second).UnixNano(),
		}).SetRandomId())

		client.ScheduleMsg((&message.Request{
			From: helper.Addr,
			To:   &helper.Addr,
			// StartTime:      time.Now().Add(5 * time.Second).UnixNano(),
			ExpirationTime: time.Now().UnixNano() - int64(5*time.Second),
		}).SetRandomId())

		client.ScheduleMsg((&message.Request{
			From:           helper.Addr,
			To:             &helper.Addr,
			ExpirationTime: time.Now().Add(10 * time.Second).UnixNano(),
			Interval:       2 * time.Second,
		}).SetRandomId())

		time.Sleep(20 * time.Second)
		client.CloseSendMsg()
	}()

	for resp := range client.Response() {
		fmt.Println("execution resp: ", resp)
	}
}

Concurrent Transaction Management in Safe Multisig Wallets

The Safe multisig contract also uses a nonce. Our solution manages this nonce off-chain. Now you can submit multiple transactions at once - no need to wait for confirmations.

The current implementation uses Safe v1.3, but you can build your own for different Safe contract versions.

func BatchSendSafeTx(from common.Address, builder gnosissafe.SafeTxBuilder, deliverer gnosissafe.SafeTxDeliverer, params []gnosissafe.SafeTxParam) {

	safeContractAddress, _ := builder.GetContractAddress()

	for _, param := range params {
		callData, _, safeNonce, err := builder.Build(param)
		if err != nil {
			// Handle error
			return
		}

		req := message.Request{
			From:     from,
			To:       &safeContractAddress,
			Gas:      500000, // ensure gas meets the minimum requirement for successful Safe contract execution (avoiding reverts).
			Data:     callData,
		}
		err = deliverer.Deliver(&req, safeNonce)
		if err != nil {
			// Handle error
			return
		}
	}
}

Running tests

  • Unit + integration (simulated backend, no real RPC): go test ./... (subscriber tests run in parallel; use -timeout=60s if needed)
  • E2e (includes tests that talk to real RPC): go test -tags=e2e ./...

E2e tests live in *_e2e_test.go files behind the e2e build tag.

Setup local node for testing

you should install foundry before running the script below:

./cmd/run_test_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

	subscriber.Subscriber
	// contains filtered or unexported fields
}

func Dial

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

func NewClient

func NewClient(c *rpc.Client) *Client

func NewEthClient added in v0.1.0

func NewEthClient(
	c *rpc.Client,
	accRegistry account.Registry,
	msgStore message.Storage,
	nonceManager nonce.Manager,
	msgManager message.Manager,
	subscriber subscriber.Subscriber,
	sequencer message.Sequencer,
) (*Client, error)

func (*Client) AddABI

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

Trying to decode some data using the abi if specific

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) CallContractWithAccountOverride added in v0.1.0

func (c *Client) CallContractWithAccountOverride(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int, overrides *map[common.Address]gethclient.OverrideAccount) ([]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) GetNonce added in v0.1.0

func (c *Client) GetNonce(msgId common.Hash) (uint64, error)

func (*Client) GetSigner

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

func (*Client) HasMsg added in v0.1.0

func (c *Client) HasMsg(msgId common.Hash) bool

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) 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) Response added in v0.1.0

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

func (*Client) RpcClient added in v0.1.18

func (c *Client) RpcClient() *rpc.Client

func (*Client) ScheduleMsg

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

func (*Client) SetMaxQueriesPerMerge added in v0.1.39

func (c *Client) SetMaxQueriesPerMerge(n int)

SetMaxQueriesPerMerge sets the maximum number of realtime queries merged into one eth_getLogs. Use 2 on BSC to avoid nodes returning 0 for large merged filters. 0 = no limit. No-op if the underlying subscriber is not a *ChainSubscriber.

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) WaitMsgReceipt added in v0.1.0

func (c *Client) WaitMsgReceipt(msgId common.Hash, confirmations uint64, timeout time.Duration) (*message.Receipt, bool)

func (*Client) WaitMsgResponse added in v0.1.0

func (c *Client) WaitMsgResponse(msgId common.Hash, timeout time.Duration) (*message.Response, bool)

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)

Directories

Path Synopsis
common
ds
examples
client/debug_tx command
safetx command
Package subscriber: batch log filterer to merge multiple eth_getLogs into one JSON-RPC batch request.
Package subscriber: batch log filterer to merge multiple eth_getLogs into one JSON-RPC batch request.
tests

Jump to

Keyboard shortcuts

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