utils

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2022 License: Apache-2.0 Imports: 20 Imported by: 14

Documentation

Overview

Package utils provides a set of interfaces, helper functions, and authentication management helpers

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalcGasCost

func CalcGasCost(gasLimit uint64, gasPrice *big.Int) *big.Int

CalcGasCost calculate gas cost given gas limit (units) and gas price (wei)

func ContextDone added in v0.0.4

func ContextDone(ctx context.Context) bool

ContextDone returns true if we can exit due to a cancelled context.

func IsValidAddress

func IsValidAddress(iaddress interface{}) bool

IsValidAddress validate hex address

func IsZeroAddress

func IsZeroAddress(iaddress interface{}) bool

IsZeroAddress validate if it's a 0 address

func LogContextDone added in v0.0.4

func LogContextDone(logger *zap.Logger, ctx context.Context) bool

LogContextDone is like ContextDone but logs when the context is done

func NewAccount

func NewAccount() (*bind.TransactOpts, *ecdsa.PrivateKey, error)

NewAccount creates a new ethereum private key, and associated transactor

func NewKeyFile

func NewKeyFile(keyFileDir, keyPass string) (accounts.Account, error)

NewKeyFile returns a new ethereum account as generated by `geth account new`

func PublicKeyBytesToAddress

func PublicKeyBytesToAddress(publicKey []byte) common.Address

PublicKeyBytesToAddress ...

func SigRSV

func SigRSV(isig interface{}) ([32]byte, [32]byte, uint8)

SigRSV signatures R S V returned as arrays

func ToDecimal

func ToDecimal(ivalue interface{}, decimals int) decimal.Decimal

ToDecimal wei to decimals

func ToWei

func ToWei(iamount interface{}, decimals int) *big.Int

ToWei decimals to wei

Types

type Authorizer

type Authorizer struct {
	*bind.TransactOpts
	// contains filtered or unexported fields
}

Authorizer wraps an embedded bind.TransactOpts type with a mutex lock allowing for easier usage in concurrent programs. bind.TransactOpts is not thread-safe and as such must be used with mutex locks to prevent encountering any issues. Whenever using the embed bind.TransactOpts you must call Authorizer::Lock and Authorizer::Unlock to avoid any possible race conditions

func NewAuthorizer

func NewAuthorizer(keyFile, keyPass string) (*Authorizer, error)

NewAuthorizer returns an Authorizer object using a keyfile as the account source

func NewAuthorizerFromPK

func NewAuthorizerFromPK(pk *ecdsa.PrivateKey) *Authorizer

NewAuthorizerFromPK returns an authorizer from a private key

func (*Authorizer) Lock added in v0.0.4

func (a *Authorizer) Lock()

Lock is used to claim a lock on the authorizer type and must be called before using it for transaction signing

func (*Authorizer) Unlock added in v0.0.4

func (a *Authorizer) Unlock()

Unlock is used to release a lock on the authorizer type and must be called after using it for transaction signing

type Blockchain added in v0.0.2

type Blockchain interface {
	// CodeAt returns the code of the given account. This is needed to differentiate
	// between contract internal errors and the local chain being out of sync.
	CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error)
	// ContractCall executes an Ethereum contract call with the specified data as the
	// input.
	CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
	// PendingCodeAt returns the code of the given account in the pending state.
	PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error)
	// PendingCallContract executes an Ethereum contract call against the pending state.
	PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error)
	// PendingNonceAt retrieves the current pending nonce associated with an account.
	PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
	// SuggestGasPrice retrieves the currently suggested gas price to allow a timely
	// execution of a transaction.
	SuggestGasPrice(ctx context.Context) (*big.Int, error)
	// SuggestGasTipCap retrieves the currently suggested 1559 priority fee to allow
	// a timely execution of a transaction.
	SuggestGasTipCap(ctx context.Context) (*big.Int, 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.
	EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error)
	// SendTransaction injects the transaction into the pending pool for execution.
	SendTransaction(ctx context.Context, tx *types.Transaction) error
	// FilterLogs executes a log filter operation, blocking during execution and
	// returning all the results in one batch.
	//
	// TODO(karalabe): Deprecate when the subscription one can return past data too.
	FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error)
	// SubscribeFilterLogs creates a background log filtering operation, returning
	// a subscription immediately, which can be used to stream the found events.
	SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error)
	TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
	// SubscribeNewHead returns an event subscription for a new header.
	SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error)
	// TransactionByHash checks the pool of pending transactions in addition to the
	// blockchain. The isPending return value indicates whether the transaction has been
	// mined yet. Note that the transaction may not be part of the canonical chain even if
	// it's not pending.
	TransactionByHash(ctx context.Context, txHash common.Hash) (*types.Transaction, bool, error)
	// BlockNumber returns the most recent block number
	BlockNumber(ctx context.Context) (uint64, error)
	// HeaderByNumber returns a block header from the current canonical chain. If
	// number is nil, the latest known header is returned.
	HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
}

Blockchain is a generalized interface for interacting with the ethereum blockchain it satisfies all functions required by the ethclient, and simulated backend types. This allows you to use ethclient and the simulated backend interchangeably which is particularly useful for testing

Jump to

Keyboard shortcuts

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