Documentation
¶
Overview ¶
Package ethutil provides utilities used for dealing with Ethereum concerns in the context of implementing cross-chain interfaces defined in pkg/chain.
Index ¶
- func AddressFromHex(hex string) (common.Address, error)
- func CallAtBlock(fromAddress common.Address, blockNumber *big.Int, value *big.Int, ...) error
- func ConnectClients(url string, urlRPC string) (*ethclient.Client, *rpc.Client, *rpc.Client, error)
- func DecryptKeyFile(keyFile, password string) (*keystore.Key, error)
- func EstimateGas(from common.Address, to common.Address, method string, contractABI *abi.ABI, ...) (uint64, error)
- func WrapCallLogging(logger log.EventLogger, backend bind.ContractBackend) bind.ContractBackend
- func WrapRateLimiting(backend bind.ContractBackend, config *RateLimiterConfig) bind.ContractBackend
- type ErrorResolver
- type MiningWaiter
- type NonceManager
- type RateLimiterConfig
- type ResubmitTransactionFn
- type TransactionOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddressFromHex ¶
AddressFromHex converts the passed string to a common.Address and returns it, unless it is not a valid address, in which case it returns an error. Compare to common.HexToAddress, which assumes the address is valid and does not provide for an error return.
func CallAtBlock ¶
func CallAtBlock( fromAddress common.Address, blockNumber *big.Int, value *big.Int, contractABI *abi.ABI, caller bind.ContractCaller, errorResolver *ErrorResolver, contractAddress common.Address, method string, result interface{}, parameters ...interface{}, ) error
CallAtBlock allows the invocation of a particular contract method at a particular block. It papers over the fact that abigen bindings don't directly support calling at a particular block, and is mostly meant for use from generated contract code.
func ConnectClients ¶
ConnectClients takes HTTP and RPC URLs and returns initialized versions of standard, WebSocket, and RPC clients for the Ethereum node at that address.
func DecryptKeyFile ¶
DecryptKeyFile reads in a key file and uses the password to decrypt it.
func EstimateGas ¶
func EstimateGas( from common.Address, to common.Address, method string, contractABI *abi.ABI, transactor bind.ContractTransactor, parameters ...interface{}, ) (uint64, error)
EstimateGas tries to estimate the gas needed to execute a specific transaction based on the current pending state of the backend blockchain. There is no guarantee that this is the true gas limit requirement as other transactions may be added or removed by miners, but it should provide a basis for setting a reasonable default.
func WrapCallLogging ¶
func WrapCallLogging(logger log.EventLogger, backend bind.ContractBackend) bind.ContractBackend
WrapCallLogging wraps certain call-related methods on the given `backend` with debug logging sent to the given `logger`. Actual functionality is delegated to the passed backend.
func WrapRateLimiting ¶ added in v1.2.0
func WrapRateLimiting( backend bind.ContractBackend, config *RateLimiterConfig, ) bind.ContractBackend
WrapRateLimiting wraps the given contract backend with rate limiting capabilities with respect to the provided configuration. All types of requests to the contract are rate-limited, including view function calls.
Types ¶
type ErrorResolver ¶
type ErrorResolver struct {
// contains filtered or unexported fields
}
ErrorResolver bundles up the bits needed to turn errors like "failed to estimate gas needed" that are triggered by contract reverts but don't include revert causes into proper revert error messages from a contract by calling the contract method without trying to commit it.
It has one method, ResolveError, that does the heavy lifting.
func NewErrorResolver ¶
func NewErrorResolver( contractCaller ethereum.ContractCaller, abi *abi.ABI, address *common.Address, ) *ErrorResolver
NewErrorResolver returns an ErroResolver for the given Ethereum client, contract ABI, and contract address combination.
func (*ErrorResolver) ResolveError ¶
func (er *ErrorResolver) ResolveError( originalErr error, from common.Address, value *big.Int, methodName string, parameters ...interface{}, ) error
ResolveError resolves the given transaction error to a standard error that, if available, contains the error message the transaction produced when reverting.
ResolveError achieves this by re-calling the transaction (not submitting it for block inclusion, just calling it for its results). `value` is the value in gwei to send along with the simulated call.
type MiningWaiter ¶ added in v1.1.0
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 ¶ added in v1.1.0
func NewMiningWaiter( backend bind.DeployBackend, 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 ¶ added in v1.1.0
func (mw MiningWaiter) ForceMining( originalTransaction *types.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.
func (*MiningWaiter) WaitMined ¶ added in v1.1.0
func (mw *MiningWaiter) WaitMined( timeout time.Duration, tx *types.Transaction, ) (*types.Receipt, error)
WaitMined blocks the current execution until the transaction with the given hash is mined. Execution is blocked until the transaction is mined or until the given timeout passes.
type NonceManager ¶ added in v1.0.0
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 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 ¶ added in v1.0.0
func NewNonceManager( account common.Address, transactor bind.ContractTransactor, ) *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 client.
func (*NonceManager) CurrentNonce ¶ added in v1.0.0
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 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 ¶ added in v1.0.0
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 RateLimiterConfig ¶ added in v1.2.0
type RateLimiterConfig struct {
// RequestsPerSecondLimit sets the maximum average number of requests
// per second. It's important to note that in short periods of time
// the actual average may exceed this limit slightly.
RequestsPerSecondLimit int
// ConcurrencyLimit sets the maximum number of concurrent requests which
// can be executed against the underlying contract backend at the same time.
ConcurrencyLimit int
// AcquirePermitTimeout determines how long a request can wait trying
// to acquire a permit from the rate limiter.
AcquirePermitTimeout time.Duration
}
RateLimiterConfig represents the configuration of the rate limiter.
type ResubmitTransactionFn ¶ added in v1.1.0
type ResubmitTransactionFn func(gasPrice *big.Int) (*types.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 TransactionOptions ¶
type TransactionOptions struct {
// GasLimit specifies a gas limit to set on a transaction call; should be
// ignored if set to 0.
GasLimit uint64
// GasPrice specifies a gas price to set on a transaction call; should be
// ignored if set to nil.
GasPrice *big.Int
}
TransactionOptions represents custom transaction options which can be used while invoking contracts methods.
func (TransactionOptions) Apply ¶
func (to TransactionOptions) Apply(transactorOptions *bind.TransactOpts)
Apply takes a bind.TransactOpts pointer and applies the options described in TransactionOptions to it. Note that a GasLimit of 0 or a GasPrice of nil are not applied to the passed options; these values indicate that the options should remain unchanged.