This commit is contained in:
Laurent Le Houerou 2021-11-08 22:35:39 +04:00
parent 831b88ae19
commit c3908948ec
2 changed files with 15 additions and 8 deletions

View File

@ -25,6 +25,7 @@ type Client interface {
PendingNonce(context.Context) (uint64, error) PendingNonce(context.Context) (uint64, error)
NewTransactor() (*bind.TransactOpts, error) NewTransactor() (*bind.TransactOpts, error)
Execute(context.Context, func(context.Context, *TransactOpts) (*types.Transaction, error)) (Transaction, error) Execute(context.Context, func(context.Context, *TransactOpts) (*types.Transaction, error)) (Transaction, error)
ExecuteAndWait(context.Context, func(context.Context) (Transaction, error)) error
NativeTokenBalance(context.Context) (decimal.Decimal, error) NativeTokenBalance(context.Context) (decimal.Decimal, error)
CurrentBlockNumber(context.Context) (uint64, error) CurrentBlockNumber(context.Context) (uint64, error)
CurrentBlock(context.Context) (*types.Block, error) CurrentBlock(context.Context) (*types.Block, error)
@ -130,7 +131,15 @@ func (c *client) Execute(ctx context.Context, action func(context.Context, *Tran
return nil, errors.Wrap(err, "executing waitable action") return nil, errors.Wrap(err, "executing waitable action")
} }
log.Debug().Msgf("//TX// tx started / hash: %s / gasprice: %s / gaslimit: %d", tx.Hash().Hex(), decimal.NewFromBigInt(tx.GasPrice(), -9), tx.Gas()) log.Debug().Msgf("//TX// tx started / hash: %s / gasprice: %s / gaslimit: %d", tx.Hash().Hex(), decimal.NewFromBigInt(tx.GasPrice(), -9), tx.Gas())
return NewTransaction(localctx, c, c.pendingTransactionCheckPeriod, tx), nil return NewTransaction(c, c.pendingTransactionCheckPeriod, tx), nil
}
func (c *client) ExecuteAndWait(ctx context.Context, action func(context.Context) (Transaction, error)) error {
tx, err := action(ctx)
if err != nil {
return errors.Wrap(err, "executing waitable action")
}
return tx.Wait(ctx)
} }
func (c *client) GetTransactionsForAddressInBlock(ctx context.Context, a string, b int64) { func (c *client) GetTransactionsForAddressInBlock(ctx context.Context, a string, b int64) {

View File

@ -11,36 +11,34 @@ import (
) )
type Transaction interface { type Transaction interface {
Wait() error Wait(ctx context.Context) error
} }
type transaction struct { type transaction struct {
*types.Transaction *types.Transaction
ctx context.Context
client Client client Client
pendingCheckPeriod time.Duration pendingCheckPeriod time.Duration
} }
func NewTransaction(ctx context.Context, client Client, pendingCheckPeriod time.Duration, tx *types.Transaction) Transaction { func NewTransaction(client Client, pendingCheckPeriod time.Duration, tx *types.Transaction) Transaction {
return &transaction{ return &transaction{
Transaction: tx, Transaction: tx,
ctx: ctx,
client: client, client: client,
pendingCheckPeriod: pendingCheckPeriod, pendingCheckPeriod: pendingCheckPeriod,
} }
} }
func (t *transaction) Wait() error { func (t *transaction) Wait(ctx context.Context) error {
hash := t.Hash() hash := t.Hash()
notfoundmax := 10 notfoundmax := 10
notfoundcount := 0 notfoundcount := 0
ticker := time.NewTicker(t.pendingCheckPeriod) ticker := time.NewTicker(t.pendingCheckPeriod)
for { for {
select { select {
case <-t.ctx.Done(): case <-ctx.Done():
return errors.New("context canceled") return errors.New("context canceled")
case <-ticker.C: case <-ticker.C:
_, pending, err := t.client.TransactionByHash(t.ctx, hash) _, pending, err := t.client.TransactionByHash(ctx, hash)
if err != nil { if err != nil {
if err == ethereum.NotFound && notfoundcount < notfoundmax { if err == ethereum.NotFound && notfoundcount < notfoundmax {
notfoundcount++ notfoundcount++