From 1e2cc1c65983e379d72518b3db29b0bbd604244c Mon Sep 17 00:00:00 2001 From: Nico Vergauwen Date: Tue, 5 Jan 2021 16:56:58 +0100 Subject: [PATCH] WIP --- eth/backend.go | 18 +++++++++++++++++- eth/backend_test.go | 2 +- eth/client.go | 2 +- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/eth/backend.go b/eth/backend.go index 5a6ab6b6ca..d88bebd97c 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -47,9 +47,11 @@ type backend struct { abiMap map[string]*abi.ABI nonceManager *NonceManager signer types.Signer + + maxGasPrice *big.Int } -func NewBackend(client *ethclient.Client, signer types.Signer) (Backend, error) { +func NewBackend(client *ethclient.Client, signer types.Signer, maxGasPrice *big.Int) (Backend, error) { abiMap, err := makeABIMap() if err != nil { return nil, err @@ -60,6 +62,7 @@ func NewBackend(client *ethclient.Client, signer types.Signer) (Backend, error) abiMap, NewNonceManager(client), signer, + maxGasPrice, }, nil } @@ -99,6 +102,19 @@ func (b *backend) SendTransaction(ctx context.Context, tx *types.Transaction) er return nil } +func (b *backend) SuggestGasPrice(ctx context.Context) (*big.Int, error) { + gp, err := b.Client.SuggestGasPrice(ctx) + if err != nil { + return nil, err + } + + if b.maxGasPrice != nil && gp.Cmp(b.maxGasPrice) >= 0 { + return nil, fmt.Errorf("current gas price exceeds maximum gas price") + } + + return gp, nil +} + type txLog struct { method string inputs string diff --git a/eth/backend_test.go b/eth/backend_test.go index 99adbaf82e..e947b6837f 100644 --- a/eth/backend_test.go +++ b/eth/backend_test.go @@ -80,7 +80,7 @@ func TestSendTransaction_SendErr_DontUpdateNonce(t *testing.T) { signedTx, err := types.SignTx(tx, signer, privateKey) require.Nil(t, err) - bi, err := NewBackend(client, signer) + bi, err := NewBackend(client, signer, nil) require.Nil(t, err) nonceLockBefore := bi.(*backend).nonceManager.getNonceLock(fromAddress) diff --git a/eth/client.go b/eth/client.go index e72c4b3538..b32e10dba5 100644 --- a/eth/client.go +++ b/eth/client.go @@ -158,7 +158,7 @@ func NewClient(accountAddr ethcommon.Address, keystoreDir string, eth *ethclient signer := types.NewEIP155Signer(chainID) - backend, err := NewBackend(eth, signer) + backend, err := NewBackend(eth, signer, nil) if err != nil { return nil, err }