Skip to content

Commit

Permalink
set latest nonce to TransactOpts.Nonce to avoid piling up pending txs
Browse files Browse the repository at this point in the history
Signed-off-by: Masanori Yoshida <masanori.yoshida@datachain.jp>
  • Loading branch information
siburu committed May 28, 2024
1 parent 1db9ee9 commit ee8d08f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 14 additions & 2 deletions pkg/relay/ethereum/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion pkg/relay/ethereum/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit ee8d08f

Please sign in to comment.