From ee8d08f985fc62453403a864d07302510c19fc99 Mon Sep 17 00:00:00 2001 From: Masanori Yoshida Date: Tue, 28 May 2024 11:15:02 +0900 Subject: [PATCH] set latest nonce to TransactOpts.Nonce to avoid piling up pending txs Signed-off-by: Masanori Yoshida --- pkg/relay/ethereum/client.go | 16 ++++++++++++++-- pkg/relay/ethereum/tx.go | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pkg/relay/ethereum/client.go b/pkg/relay/ethereum/client.go index 4ae89c2..0df5e89 100644 --- a/pkg/relay/ethereum/client.go +++ b/pkg/relay/ethereum/client.go @@ -18,13 +18,25 @@ func (chain *Chain) CallOpts(ctx context.Context, height int64) *bind.CallOpts { return opts } -func (chain *Chain) TxOpts(ctx context.Context) (*bind.TransactOpts, error) { +func (chain *Chain) TxOpts(ctx context.Context, useLatestNonce bool) (*bind.TransactOpts, error) { + addr := chain.signer.Address() + txOpts := &bind.TransactOpts{ - From: chain.signer.Address(), + From: addr, Signer: chain.signer.Sign, } + if err := NewGasFeeCalculator(chain.client, &chain.config).Apply(ctx, txOpts); err != nil { return nil, err } + + if useLatestNonce { + if nonce, err := chain.client.NonceAt(ctx, addr, nil); err != nil { + return nil, err + } else { + txOpts.Nonce = new(big.Int).SetUint64(nonce) + } + } + return txOpts, nil } diff --git a/pkg/relay/ethereum/tx.go b/pkg/relay/ethereum/tx.go index ffb58e2..9d96c5b 100644 --- a/pkg/relay/ethereum/tx.go +++ b/pkg/relay/ethereum/tx.go @@ -41,7 +41,7 @@ func (c *Chain) SendMsgs(msgs []sdk.Msg) ([]core.MsgID, error) { logAttrMsgType, fmt.Sprintf("%T", msg), )} - opts, err := c.TxOpts(ctx) + opts, err := c.TxOpts(ctx, true) if err != nil { return nil, err }