transfer

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2021 License: LGPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBlockchainQueueFull = errors.New("failed to send a transaction, blockchain queue is full")

ErrBlockchainQueueFull is returned if the current queue is full and incremeting gas price will be impossible. Transactions which receive this error should be retried later.

View Source
var ErrNoSigners = errors.New("failed to send a transaction, no signers for incrementing")

ErrNoSigners is returned if there are no signers to use for incrementing this transaction.

View Source
var ErrQueueClosed = errors.New("queue was closed")

ErrQueueClosed is returned when queue is closed and transaction was not processed.

View Source
var ErrQueueMissingResult = errors.New("transaction missing with no previous error, state unknown")

ErrQueueMissingResult is returned if neither an error or a transaction exists in the queue response.

Functions

func TransactionUniqueID

func TransactionUniqueID(orignalHash string, chainID int64) string

TransactionUniqueID returns a unique ID for a transaction.

Types

type BCTxStatus

type BCTxStatus string

BCTxStatus represents the status of tx on blockchain.

const (
	// StatusPending indicates that the tx is still pending.
	StatusPending BCTxStatus = "Pending"
	// StatusFailed indicates that the tx has failed.
	StatusFailed BCTxStatus = "Failed"
	// StatusSucceeded indicates that the tx has suceeded.
	StatusSucceeded BCTxStatus = "Succeeded"
)

type GasIncrementorConfig

type GasIncrementorConfig struct {
	PullInterval      time.Duration
	MaxQueuePerSigner int
}

GasIncrementorConfig is provided to the incrementor to configure it.

type GasPriceIncremenetor

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

GasPriceIncremenetor exposes a way automatically increment gas fees for transactions in a preconfigured manner.

func NewGasPriceIncremenetor

func NewGasPriceIncremenetor(cfg GasIncrementorConfig, storage Storage, cl MultichainClient, signers Signers) *GasPriceIncremenetor

NewGasPriceIncremenetor returns a new incrementer instance.

func (*GasPriceIncremenetor) AttachLogFunc

func (i *GasPriceIncremenetor) AttachLogFunc(logFn LogFunc)

AttachLogFunc attaches a new log func incremeter thread. The given log func is called every time the running incrementer thread created by the `Run` method encouters an error.

This method is not thread safe and should be called before Run.

func (*GasPriceIncremenetor) CanQueue

func (i *GasPriceIncremenetor) CanQueue(sender common.Address) (bool, error)

CanQueue returns if incrementor is able to queue a transaction

func (*GasPriceIncremenetor) CanSign

func (i *GasPriceIncremenetor) CanSign(sender common.Address) bool

CanSign returns if incrementor is able to sign transactions for the given sender.

func (*GasPriceIncremenetor) InsertInitial

func (i *GasPriceIncremenetor) InsertInitial(tx *types.Transaction, opts TransactionOpts, senderAddress common.Address) error

InsertInitial uses the given storage to insert an new transaction which will later be retreived using `GetTransactionsToCheck` in order to check it's state and retry with higher gas price if needed.

func (*GasPriceIncremenetor) Run

func (i *GasPriceIncremenetor) Run()

Run starts the gas price incrementer.

It will query the given storage for any entries that it needs to check for gas increase, trying to check their status.

func (*GasPriceIncremenetor) Stop

func (i *GasPriceIncremenetor) Stop()

Stop stops the execution of GasPriceIncrementer thread created by the Run method.

type GasPriceIncremenetorIface

type GasPriceIncremenetorIface interface {
	// InsertInitial inserts a new transaction to the queue.
	InsertInitial(tx *types.Transaction, opts TransactionOpts, senderAddress common.Address) error

	// CanQueue returns true if another transaction can be queue in to the incrementor.
	CanQueue(sender common.Address) (bool, error)

	// CanSign returns if incrementor can sign this transaction when incrementing gas price.
	CanSign(sender common.Address) bool
}

GasPriceIncremenetorIface abstracts gas price incrementor.

type HandlerOpts

type HandlerOpts struct {
	// SenderAddress is the address of the sender who is making the trasaction.
	SenderAddress common.Address
	// GasPriceIncOpts will be used to increase the gas price using an incrementor.
	GasPriceIncOpts TransactionOpts
	// Optional: ForceQueue allows to bypass the check if transaction is able to queue.
	ForceQueue bool
}

HandlerOpts are given when sending a new transaction.

type LogFunc

type LogFunc func(Transaction, error)

LogFunc can be attacheched to Incrementer to enable logging.

type MultichainClient

type MultichainClient interface {
	TransactionReceipt(chainID int64, hash common.Hash) (*types.Receipt, error)
	SendTransaction(chainID int64, tx *types.Transaction) error
	TransactionByHash(chainID int64, hash common.Hash) (*types.Transaction, bool, error)
}

MultichainClient handles calls to BC.

type Queue

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

Queue is channel queue which is able to queue transaction execution funcs.

func NewQueue

func NewQueue(size uint) *Queue

NewQueue returns a new queue. Size for the queue can be given so that more than 1 transaction could be queued at a time.

func (*Queue) Run

func (q *Queue) Run()

Run will start to read the queue.

func (*Queue) Stop

func (q *Queue) Stop()

Stop will stop the thread. No new transactions can be enqueued after.

func (*Queue) TransactionEnqueue

func (q *Queue) TransactionEnqueue(exec TransactionSendFn, resp chan<- QueueResponse) error

TransactionEnqueue will enqueue a given transaction and respond on the given resp channel.

Enqueue will fail and instantly return an error if the queue is closed. The given `resp` channel should be single use only. After receiving a message that channel will be closed.

type QueueResponse

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

func (*QueueResponse) Result

func (qr *QueueResponse) Result() (*types.Transaction, error)

Result extracts the innards of the queue response.

type SignatureFunc

type SignatureFunc func(tx *types.Transaction, chainID int64) (*types.Transaction, error)

SignatureFunc is used to sign transactions when resubmitting them.

type Signers

type Signers map[common.Address]SignatureFunc

Signers is a map that holds all possible signers to sign transactions when resending to the blockchain.

type Storage

type Storage interface {
	// UpsertIncrementorTransaction is called to upsert a transaction.
	// It either inserts a new entry or updates existing entries.
	UpsertIncrementorTransaction(tx Transaction) error

	// GetIncrementorTransactionsToCheck returns all transaction that need to rechecked.
	//
	// Entries should be filtered by possible signers. If incrementor cannot sign the transaction
	// it should not received it.
	GetIncrementorTransactionsToCheck(possibleSigners []string) (tx []Transaction, err error)

	// GasIncrementorSenderQueue returns the length of a queue for a single sender.
	GetIncrementorSenderQueue(sender string) (length int, err error)
}

Storage is given to the Incremeter to be used to insert, update or get transactions.

type Transaction

type Transaction struct {
	// UniqueID is a combination of the original tx hash and chainID.
	UniqueID string
	Opts     TransactionOpts
	State    TransactionState

	OrignalHashHex   string
	SenderAddressHex string
	ChainID          int64

	LatestTx []byte
}

Transaction objects is used when handling transactions.

type TransactionHandler

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

TransactionHandler wraps gas price increasers exposing a send method which handles gas prices when sending a transaction.

TransactionHandler expects the gas price increaser to be handleded and started by the caller himself.

func NewTransactionhandler

func NewTransactionhandler(inc GasPriceIncremenetorIface) *TransactionHandler

NewTransactionhandler returns a new transaction handler

func (*TransactionHandler) AttachLogger

func (t *TransactionHandler) AttachLogger(fn func(err error))

AttachLogger allows the caller to attach an optional logger. Logger logs non critical errors or warnings that happen during transaction handling.

This method is not thread safe and should be called before `SendWithGasPriceHandling`.

func (*TransactionHandler) SendWithGasPriceHandling

func (t *TransactionHandler) SendWithGasPriceHandling(txSend TransactionSendFn, opts HandlerOpts) (*types.Transaction, error)

SendWithGasPriceHandling given a new watchable transaction with options will send the transaction and increase the gas price accordingly.

type TransactionOpts

type TransactionOpts struct {
	PriceMultiplier  float64
	MaxPrice         *big.Int
	Timeout          time.Duration
	IncreaseInterval time.Duration
	CheckInterval    time.Duration

	// ValidUntil is an optional paremeter which
	// can be given to invalidate a transaction and mark it as failed
	// after the given time.Time.
	ValidUntil *time.Time
}

TransactionOpts are provided when creating a new transaction. They're used when handling a transaction.

type TransactionSendFn

type TransactionSendFn func() (*types.Transaction, error)

TransactionSendFn wraps a transaction execution which should result in a transaction being returned if it was successfull.

It allows to wrap different kinds of contract methods which all result in producing a transaction.

type TransactionState

type TransactionState string

TransactionState marks the current state of a given transaction

const (
	// TxStateCreated is given to transactions that have been initially inserted.
	TxStateCreated TransactionState = "created"
	// TxStatePriceIncreased is given to transactions which have received
	// a price increase.
	TxStatePriceIncreased TransactionState = "priceIncreased"
	// TxStateFailed is given to transactions which have
	// failed and should not be retried anymore.
	TxStateFailed TransactionState = "failed"
	// TxStateSucceed is given to transactions which have
	// succeeded and should not be retried.
	TxStateSucceed TransactionState = "succeed"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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