ethereum

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const ERC20ABI = `` /* 1309-byte string literal not displayed */

ERC20ABI is the standard ERC20 token interface ABI. This is the canonical definition used across the codebase.

Variables

This section is empty.

Functions

func ComputeBlockHash

func ComputeBlockHash(chainID, blockNumber uint64) []byte

ComputeBlockHash generates a deterministic block hash from chain ID and block number. This is used for synthetic blocks in the Canton-EVM bridge.

Types

type Client

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

Client represents an Ethereum client

func NewClient

func NewClient(cfg *Config, metrics *Metrics, logger *zap.Logger) (*Client, error)

NewClient creates a new Ethereum client.

metrics receives Prometheus observations for every outbound RPC call and for the deposit-event poll loop. Pass NewNopMetrics() in tests where metric values aren't asserted.

func (*Client) Close

func (c *Client) Close()

Close closes the Ethereum clients

func (*Client) DepositToCanton

func (c *Client) DepositToCanton(
	ctx context.Context,
	token common.Address,
	amount *big.Int,
	cantonRecipient [32]byte,
) (common.Hash, error)

DepositToCanton submits a deposit transaction (for testing)

func (*Client) GetLastScannedBlock

func (c *Client) GetLastScannedBlock() uint64

GetLastScannedBlock returns the highest block number the poller has scanned.

func (*Client) GetLatestBlockNumber

func (c *Client) GetLatestBlockNumber(ctx context.Context) (uint64, error)

GetLatestBlockNumber gets the latest block number

func (*Client) GetTransactor

func (c *Client) GetTransactor(ctx context.Context) (*bind.TransactOpts, error)

GetTransactor returns a transaction signer

func (*Client) IsWithdrawalProcessed

func (c *Client) IsWithdrawalProcessed(ctx context.Context, cantonTxHash [32]byte) (bool, error)

IsWithdrawalProcessed checks if a Canton withdrawal has already been processed on EVM

func (*Client) WatchDepositEvents

func (c *Client) WatchDepositEvents(ctx context.Context, fromBlock uint64, handler func(*DepositEvent) error) error

WatchDepositEvents polls for deposit events (uses polling for HTTP RPC compatibility).

Each tick's [currentBlock+1, latestBlock] range is walked in slices of at most config.MaxBlockRange blocks so requests stay under the provider's per-call cap. On a slice failure, currentBlock advances only through the last successful slice and the failing range is retried on the next tick.

func (*Client) WithdrawFromCanton

func (c *Client) WithdrawFromCanton(
	ctx context.Context,
	token common.Address,
	recipient common.Address,
	amount *big.Int,
	nonce *big.Int,
	cantonTxHash [32]byte,
) (common.Hash, error)

WithdrawFromCanton submits a withdrawal transaction

type Config

type Config struct {
	RPCURL             string        `yaml:"rpc_url" validate:"required"`
	WSUrl              string        `yaml:"ws_url" default:""`
	ChainID            int64         `yaml:"chain_id" validate:"required,gt=0"`
	BridgeContract     string        `yaml:"bridge_contract" validate:"required"`
	TokenContract      string        `yaml:"token_contract" default:""`
	RelayerPrivateKey  string        `yaml:"relayer_private_key" validate:"required"`
	ConfirmationBlocks int           `yaml:"confirmation_blocks" default:"12"`
	GasLimit           uint64        `yaml:"gas_limit" validate:"required,gt=0"`
	MaxGasPrice        string        `yaml:"max_gas_price" default:""`
	PollingInterval    time.Duration `yaml:"polling_interval" validate:"required,gt=0"`
	StartBlock         int64         `yaml:"start_block" default:"0"`
	LookbackBlocks     int64         `yaml:"lookback_blocks" default:"1000"`
	MaxBlockRange      uint64        `yaml:"max_block_range" default:"100" validate:"required,gt=0"`
}

Config contains Ethereum client settings

type DepositEvent

type DepositEvent struct {
	Token           common.Address
	Sender          common.Address
	CantonRecipient [32]byte
	Amount          *big.Int
	Nonce           *big.Int
	BlockNumber     uint64
	TxHash          common.Hash
	LogIndex        uint
}

DepositEvent represents a deposit to Canton event from Ethereum

type Metrics

type Metrics struct {
	// RPCCallsTotal counts outbound Ethereum RPC operations by method name and
	// terminal status. method is a stable lowercase identifier (see the
	// method=... call sites in client.go); status is "ok" or "error".
	RPCCallsTotal *prometheus.CounterVec

	// RPCDuration is the per-method latency distribution for RPC calls. Paired
	// with RPCCallsTotal so dashboards can show p95 latency next to error rate
	// for the same operation.
	RPCDuration *prometheus.HistogramVec

	// EventPollDuration is the wall-clock duration of one deposit-event poll
	// cycle (one tick of WatchDepositEvents). Empty cycles still contribute.
	EventPollDuration prometheus.Histogram

	// EventPollFailuresTotal counts poll cycles that hit an error at a
	// specific phase. reason ∈
	//   get_latest_block – HeaderByNumber failed at the start of the cycle
	//   filter_events    – FilterDepositToCanton failed
	//   iterator         – iter.Error() reported a problem while ranging
	// A single cycle can contribute to more than one reason if multiple phases
	// fail (rare but possible).
	EventPollFailuresTotal *prometheus.CounterVec

	// EventsFetchedTotal is the running count of deposit events the poll loop
	// has consumed (one increment per iter.Next() returning true). Pair with
	// rate() to get throughput.
	EventsFetchedTotal prometheus.Counter

	// LatestBlockSeen is the most recent block number the client has observed
	// from HeaderByNumber. Stale = the Ethereum node is unreachable or behind.
	LatestBlockSeen prometheus.Gauge

	// LastScannedBlock is the highest block number the poll loop has scanned
	// for deposit events. Distance from LatestBlockSeen = backlog the poller
	// is working through.
	LastScannedBlock prometheus.Gauge
}

Metrics holds Prometheus collectors for the Ethereum RPC client.

The collectors here cover two distinct surfaces that benefit from separate observation:

  1. Outbound RPC calls — every method this client invokes against the Ethereum node (eth_blockNumber, eth_gasPrice, contract calls/sends, log filtering). Latency and error rate live here.
  2. The deposit-event poll loop — a separate, dominant code path with its own cycle-level metrics: cycle duration, where in the cycle failures cluster, and how many events each cycle pulls in.

Both surfaces share a namespaced registerer so a deployment can scrape one /metrics endpoint and discriminate by metric name.

func NewMetrics

NewMetrics registers ethereum client metrics against the given registerer.

func NewNopMetrics

func NewNopMetrics() *Metrics

NewNopMetrics returns a Metrics instance backed by a throwaway registry. Use in tests or contexts where metric values are not asserted.

type WithdrawalEvent

type WithdrawalEvent struct {
	Token        common.Address
	Recipient    common.Address
	Amount       *big.Int
	Nonce        *big.Int
	CantonTxHash [32]byte
	BlockNumber  uint64
	TxHash       common.Hash
}

WithdrawalEvent represents a withdrawal from Canton event on Ethereum

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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