transaction

package
v0.0.0-...-fa704fd Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2022 License: BSD-3-Clause Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TxTypeTransfer           = 100
	TxTypeTransferToContract = 101
	TxTypeInvokeContract     = 102
)

Variables

View Source
var (
	// ErrNoCode is returned by call and transact operations for which the requested
	// recipient contract to operate on does not exist in the state db or does not
	// have any code associated with it (i.e. suicided).
	ErrNoCode = errors.New("no contract code at given address")

	// This error is raised when attempting to perform a pending state action
	// on a backend that doesn't implement PendingContractCaller.
	ErrNoPendingState = errors.New("backend does not support pending state")

	// This error is returned by WaitDeployed if contract creation leaves an
	// empty contract behind.
	ErrNoCodeAfterDeploy = errors.New("no contract code after deployment")
)
View Source
var (
	ErrEventNotFound = errors.New("event not found")
	ErrNoTopic       = errors.New("no topic")
)
View Source
var (
	ErrTransactionNotFound      = errors.New("transaction not found")
	ErrTransactionPending       = errors.New("transaction in pending status")
	ErrTransactionSenderInvalid = errors.New("invalid transaction sender")
)
View Source
var ErrMonitorClosed = errors.New("monitor closed")
View Source
var ErrTransactionCancelled = errors.New("transaction cancelled")
View Source
var (
	// ErrTransactionReverted denotes that the sent transaction has been
	// reverted.
	ErrTransactionReverted = errors.New("transaction reverted")
)

Functions

func FindSingleEvent

func FindSingleEvent(abi *abi.ABI, receipt *types.Receipt, contractAddress common.Address, event abi.Event, out interface{}) error

FindSingleEvent will find the first event of the given kind.

func FindSingleEventXwc

func FindSingleEventXwc(receipt *xwctypes.RpcTransactionReceipt, contractAddress common.Address, eventName string) (string, error)

func IsSynced

func IsSynced(ctx context.Context, backend Backend, maxDelay time.Duration) (bool, error)

IsSynced will check if we are synced with the given blockchain backend. This is true if the current wall clock is after the block time of last block with the given maxDelay as the maximum duration we can be behind the block time.

func ParseABIUnchecked

func ParseABIUnchecked(json string) abi.ABI

ParseABIUnchecked will parse a valid json abi. Only use this with string constants known to be correct.

func ParseEvent

func ParseEvent(a *abi.ABI, eventName string, c interface{}, e types.Log) error

ParseEvent will parse the specified abi event from the given log

func WaitSynced

func WaitSynced(ctx context.Context, backend Backend, maxDelay time.Duration) error

WaitSynced will wait until we are synced with the given blockchain backend, with the given maxDelay duration as the maximum time we can be behind the last block.

Types

type Backend

type Backend interface {
	ContractBackend
	TransactionReceipt(ctx context.Context, txHash common.Hash) (receipt *xwctypes.RpcTransactionReceipt, err error)
	TransactionByHash(ctx context.Context, hash common.Hash) (tx *xwctypes.RpcTransaction, isPending bool, err error)
	BlockNumber(ctx context.Context) (uint64, error)
	HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
	BlockByNumber(ctx context.Context, number *big.Int) (*xwctypes.RpcBlock, error)
	BalanceAt(ctx context.Context, address common.Address, block *big.Int) (*big.Int, error)
	NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error)

	// for xwc
	IsLocked(ctx context.Context) (bool, error)
	GetAccount(ctx context.Context, acctName string) (xwctypes.RpcAccountJson, error)
	CreateAccount(ctx context.Context, acctName string) (string, error)
	RefBlockInfo(ctx context.Context) (uint16, uint32, error)
	SendXwcTransaction(ctx context.Context, tx *xwcfmt.Transaction) (common.Hash, error)
}

Backend is the minimum of blockchain backend functions we need.

type ContractBackend

type ContractBackend interface {
	ContractCaller
	ContractTransactor
	ContractFilterer

	ContractXWC
}

ContractBackend defines the methods needed to work with contracts on a read-write basis.

type ContractCaller

type ContractCaller 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)
}

ContractCaller defines the methods needed to allow operating with a contract on a read only basis.

type ContractFilterer

type ContractFilterer interface {
	// 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)
}

ContractFilterer defines the methods needed to access log events using one-off queries or continuous event subscriptions.

type ContractTransactor

type ContractTransactor interface {
	// PendingCodeAt returns the code of the given account in the pending state.
	PendingCodeAt(ctx context.Context, account common.Address) ([]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)
	// 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
}

ContractTransactor defines the methods needed to allow operating with a contract on a write only basis. Besides the transacting method, the remainder are helpers used when the user does not provide some needed values, but rather leaves it up to the transactor to decide.

type ContractXWC

type ContractXWC interface {
	CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error)
	InvokeContractOffline(ctx context.Context, contract common.Address, api string, arg string) (string, error)
}

type DeployBackend

type DeployBackend interface {
	TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
	CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
}

DeployBackend wraps the operations needed by WaitMined and WaitDeployed.

type Matcher

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

func NewMatcher

func NewMatcher(backend Backend, signer types.Signer) *Matcher

func (Matcher) Matches

func (m Matcher) Matches(ctx context.Context, tx []byte, networkID uint64, senderOverlay penguin.Address) (bool, error)

type Monitor

type Monitor interface {
	io.Closer
	// WatchTransaction watches the transaction until either there is 1 confirmation or a competing transaction with cancellationDepth confirmations.
	WatchTransaction(txHash common.Hash, nonce uint64) (<-chan xwctypes.RpcTransactionReceipt, <-chan error, error)
}

Monitor is a nonce-based watcher for transaction confirmations. Instead of watching transactions individually, the senders nonce is monitored and transactions are checked based on this. The idea is that if the nonce is still lower than that of a pending transaction, there is no point in actually checking the transaction for a receipt. At the same time if the nonce was already used and this was a few blocks ago we can reasonably assume that it will never confirm.

func NewMonitor

func NewMonitor(logger logging.Logger, backend Backend, sender common.Address, pollingInterval time.Duration, cancellationDepth uint64) Monitor

type PendingContractCaller

type PendingContractCaller interface {
	// 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)
}

PendingContractCaller defines methods to perform contract calls on the pending state. Call will try to discover this interface when access to the pending state is requested. If the backend does not support the pending state, Call returns ErrNoPendingState.

type Service

type Service interface {
	// Send creates a transaction based on the request and sends it.
	Send(ctx context.Context, request *TxRequest) (txHash common.Hash, err error)
	// Call simulate a transaction based on the request.
	Call(ctx context.Context, request *TxRequest) (result []byte, err error)
	// WaitForReceipt waits until either the transaction with the given hash has been mined or the context is cancelled.
	// This is only valid for transaction sent by this service.
	WaitForReceipt(ctx context.Context, txHash common.Hash) (receipt *xwctypes.RpcTransactionReceipt, err error)
	// WatchSentTransaction start watching the given transaction.
	// This wraps the monitors watch function by loading the correct nonce from the store.
	// This is only valid for transaction sent by this service.
	WatchSentTransaction(txHash common.Hash) (<-chan xwctypes.RpcTransactionReceipt, <-chan error, error)
}

Service is the service to send transactions. It takes care of gas price, gas limit and nonce management.

func NewService

func NewService(logger logging.Logger, backend Backend, signer crypto.Signer, store storage.StateStorer, chainID *big.Int, monitor Monitor) (Service, error)

NewService creates a new transaction service.

type TxRequest

type TxRequest struct {
	To       *common.Address // recipient of the transaction
	Data     []byte          // transaction data
	GasPrice *big.Int        // gas price or nil if suggested gas price should be used
	GasLimit uint64          // gas limit or 0 if it should be estimated
	Value    *big.Int        // amount of wei to send

	TxType     int    // 100: transfer   101: transfer to contract   102: invoke contract
	Memo       string // transfer memo
	InvokeApi  string // used for invoking contract
	InvokeArgs string // used for invoking contract
}

TxRequest describes a request for a transaction that can be executed.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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