ethlike

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2021 License: MIT Imports: 11 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WaitForBlockConfirmations

func WaitForBlockConfirmations(
	blockHeightWaiter BlockHeightWaiter,
	startBlockNumber uint64,
	blockConfirmations uint64,
	stateCheck func() (bool, error),
) (bool, error)

WaitForBlockConfirmations ensures that after receiving specific number of block confirmations the state of the chain is actually as expected. It waits for predefined number of blocks since the start block number provided. After the required block number is reached it performs a check of the chain state with a provided function returning a boolean value.

Types

type Account

type Account struct {
	// Keyfile is a full path to a key file.  Normally this file is one of the
	// imported keys in your local chain node.  It can normally be found in
	// a directory <some-path>/data/keystore/ and starts with its creation date
	// "UTC--.*".
	KeyFile string

	// KeyFilePassword is the password used to unlock the account specified in
	// KeyFile.
	KeyFilePassword string
}

Account is a struct that contains the configuration for accessing an ETH-like network and a contract on the network.

type Address

type Address [20]byte

Address represents the 20 byte address of an ETH-like account.

func (Address) TerminalString

func (a Address) TerminalString() string

TerminalString returns the address as a console string.

type BalanceMonitor

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

BalanceMonitor provides the possibility to monitor balances for given accounts.

func NewBalanceMonitor

func NewBalanceMonitor(balanceSource BalanceSource) *BalanceMonitor

NewBalanceMonitor creates a new instance of the balance monitor.

func (*BalanceMonitor) Observe

func (bm *BalanceMonitor) Observe(
	ctx context.Context,
	address Address,
	alertThreshold *Token,
	tick time.Duration,
	retryTimeout time.Duration,
)

Observe starts a process which checks the address balance with the given tick and triggers an alert in case the balance falls below the alert threshold value. The balance check will be retried in case of an error up to the retry timeout.

type BalanceSource

type BalanceSource func(address Address) (*Token, error)

BalanceSource provides a balance info for the given address.

type Block

type Block struct {
	*Header
}

Block represents an entire block in the ETH-like blockchain.

type BlockCounter

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

BlockCounter represents a block counter.

func CreateBlockCounter

func CreateBlockCounter(chainReader ChainReader) (*BlockCounter, error)

CreateBlockCounter creates a block counter.

func (*BlockCounter) BlockHeightWaiter

func (bc *BlockCounter) BlockHeightWaiter(
	blockNumber uint64,
) (<-chan uint64, error)

BlockHeightWaiter returns a waiter for the given block.

func (*BlockCounter) CurrentBlock

func (bc *BlockCounter) CurrentBlock() (uint64, error)

CurrentBlock returns the current block.

func (*BlockCounter) WaitForBlockHeight

func (bc *BlockCounter) WaitForBlockHeight(blockNumber uint64) error

WaitForBlockHeight waits for a given block height.

func (*BlockCounter) WatchBlocks

func (bc *BlockCounter) WatchBlocks(ctx context.Context) <-chan uint64

WatchBlocks watches the blocks.

type BlockHeightWaiter

type BlockHeightWaiter interface {
	WaitForBlockHeight(blockNumber uint64) error
}

BlockHeightWaiter provides the ability to wait for a given block height.

type Chain

Chain represents an ETH-like chain handle.

type ChainReader

type ChainReader interface {
	// BlockByNumber gets the block by its number. The block number argument
	// can be nil to select the latest block.
	BlockByNumber(ctx context.Context, number *big.Int) (*Block, error)

	// SubscribeNewHead subscribes to notifications about changes of the
	// head block of the canonical chain.
	SubscribeNewHead(
		ctx context.Context,
		ch chan<- *Header,
	) (Subscription, error)
}

ChainReader provides access to the blockchain.

type Config

type Config struct {
	// Example: "ws://192.168.0.157:8546".
	URL string

	// Example: "http://192.168.0.157:8545".
	URLRPC string

	// A  map from contract names to contract addresses.
	ContractAddresses map[string]string

	Account Account

	// MiningCheckInterval is the interval in which transaction
	// mining status is checked. If the transaction is not mined within this
	// time, the gas price is increased and transaction is resubmitted.
	MiningCheckInterval int

	// RequestsPerSecondLimit sets the maximum average number of requests
	// per second which can be executed against the ETH-like node.
	// All types of chain requests are rate-limited,
	// including view function calls.
	RequestsPerSecondLimit int

	// ConcurrencyLimit sets the maximum number of concurrent requests which
	// can be executed against the ETH-like node at the same time.
	// This limit affects all types of chain requests,
	// including view function calls.
	ConcurrencyLimit int
}

Config is a struct that contains the configuration needed to connect to an ETH-like node. This information will give access to an ETH-like network.

type ContractTransactor

type ContractTransactor interface {
	// PendingNonceAt retrieves the current pending nonce associated
	// with an account.
	PendingNonceAt(ctx context.Context, account Address) (uint64, error)
}

ContractTransactor defines the methods needed to allow operating with contract on a write only basis.

type Hash

type Hash [32]byte

Hash represents the 32 byte Keccak256 hash of arbitrary data.

func (Hash) TerminalString

func (h Hash) TerminalString() string

TerminalString returns the hash as a console string.

type Header struct {
	Number *big.Int
}

Header represents a block header in the ETH-like blockchain.

type MiningWaiter

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

MiningWaiter allows to block the execution until the given transaction is mined as well as monitor the transaction and bump up the gas price in case it is not mined in the given timeout.

func NewMiningWaiter

func NewMiningWaiter(
	txReader TransactionReader,
	checkInterval time.Duration,
	maxGasPrice *big.Int,
) *MiningWaiter

NewMiningWaiter creates a new MiningWaiter instance for the provided client backend. It accepts two parameters setting up monitoring rules of the transaction mining status.

Check interval is the time given for the transaction to be mined. If the transaction is not mined within that time, the gas price is increased by 20% and transaction is replaced with the one with a higher gas price.

Max gas price specifies the maximum gas price the client is willing to pay for the transaction to be mined. The offered transaction gas price can not be higher than this value. If the maximum allowed gas price is reached, no further resubmission attempts are performed.

func (MiningWaiter) ForceMining

func (mw MiningWaiter) ForceMining(
	originalTransaction *Transaction,
	resubmitFn ResubmitTransactionFn,
)

ForceMining blocks until the transaction is mined and bumps up the gas price by 20% in the intervals defined by MiningWaiter in case the transaction has not been mined yet. It accepts the original transaction reference and the function responsible for executing transaction resubmission.

type NonceManager

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

NonceManager tracks the nonce for the account and allows to update it after each successfully submitted transaction. Tracking the nonce locally is required when transactions are submitted from multiple goroutines or when multiple Ethereum-like clients are deployed behind a load balancer, there are no sticky sessions and mempool synchronization between them takes some time.

NonceManager provides no synchronization and is NOT safe for concurrent use. It is up to the client code to implement the required synchronization.

An example execution might work as follows: 1. Obtain transaction lock, 2. Calculate CurrentNonce(), 3. Submit transaction with the calculated nonce, 4. Call IncrementNonce(), 5. Release transaction lock.

func NewNonceManager

func NewNonceManager(
	transactor ContractTransactor,
	account Address,
) *NonceManager

NewNonceManager creates NonceManager instance for the provided account using the provided contract transactor. Contract transactor is used for every CurrentNonce execution to check the pending nonce value as seen by the Ethereum-like client.

func (*NonceManager) CurrentNonce

func (nm *NonceManager) CurrentNonce() (uint64, error)

CurrentNonce returns the nonce value that should be used for the next transaction. The nonce is evaluated as the higher value from the local nonce and pending nonce fetched from the Ethereum-like client. The local nonce is cached for the specific duration. If the local nonce expired, the pending nonce returned from the chain is used.

CurrentNonce is NOT safe for concurrent use. It is up to the code using this function to provide the required synchronization, optionally including IncrementNonce call as well.

func (*NonceManager) IncrementNonce

func (nm *NonceManager) IncrementNonce() uint64

IncrementNonce increments the value of the nonce kept locally by one. This function is NOT safe for concurrent use. It is up to the client code using this function to provide the required synchronization.

type Receipt

type Receipt struct {
	Status      uint64
	BlockNumber *big.Int
}

Receipt represents the results of a transaction.

type ResubmitTransactionFn

type ResubmitTransactionFn func(gasPrice *big.Int) (*Transaction, error)

ResubmitTransactionFn implements the code for resubmitting the transaction with the higher gas price. It should guarantee the same nonce is used for transaction resubmission.

type SubscribeOpts

type SubscribeOpts struct {

	// Tick is the duration with which subscription monitoring mechanism
	// pulls events from the chain. This mechanism is an additional process
	// next to a regular watchLogs subscription making sure no events are lost
	// even in case the regular subscription missed them because of, for
	// example, connectivity problems.
	Tick time.Duration

	// PastBlocks is the number of past blocks subscription monitoring mechanism
	// takes into consideration when pulling past events from the chain.
	// This event pull mechanism is an additional process next to a regular
	// watchLogs subscription making sure no events are lost even in case the
	// regular subscription missed them because of, for example, connectivity
	// problems.
	PastBlocks uint64
}

SubscribeOpts specifies optional configuration options that can be passed when creating ETH-like event subscription.

type Subscription

type Subscription interface {
	// Unsubscribe cancels the sending of events to the data channel and closes
	// the error channel.
	Unsubscribe()

	// Err returns the subscription error channel. The error channel receives
	// a value if there is an issue with the subscription. Only one value will
	// ever be sent. The error channel is closed by Unsubscribe.
	Err() <-chan error
}

Subscription represents an event subscription where events are delivered on a data channel.

type Token

type Token struct {
	*big.Int
}

Token represents a token.

func (*Token) UnmarshalToken

func (t *Token) UnmarshalToken(text []byte, units map[string]float64) error

UnmarshalToken is a function used to parse an ETH-like token.

type Transaction

type Transaction struct {
	Hash     Hash
	GasPrice *big.Int
}

Transaction represents an ETH-like chain transaction.

type TransactionReader

type TransactionReader interface {
	// TransactionReceipt returns the receipt of a mined transaction.
	// Note that the transaction may not be included in the current canonical
	// chain even if a receipt exists.
	TransactionReceipt(ctx context.Context, txHash Hash) (*Receipt, error)
}

TransactionReader provides access to past transactions and their receipts.

Jump to

Keyboard shortcuts

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