From 36108ac103a0fa755d918782cd7d3badaedf377e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=BF=97=E5=BC=BA?= <652732310@qq.com> Date: Tue, 8 Feb 2022 16:13:30 +0800 Subject: [PATCH 1/4] add CallContractAtHash to ethclient --- ethclient/ethclient.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index e6a93c96f6a0..755b5d0c6a42 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -456,6 +456,15 @@ func (ec *Client) CallContract(ctx context.Context, msg ethereum.CallMsg, blockN return hex, nil } +func (ec *Client) CallContractAtHash(ctx context.Context, msg ethereum.CallMsg, blockHash common.Hash) ([]byte, error) { + var hex hexutil.Bytes + err := ec.c.CallContext(ctx, &hex, "eth_call", toCallArg(msg), rpc.BlockNumberOrHashWithHash(blockHash, false)) + if err != nil { + return nil, err + } + return hex, nil +} + // PendingCallContract executes a message call transaction using the EVM. // The state seen by the contract call is the pending state. func (ec *Client) PendingCallContract(ctx context.Context, msg ethereum.CallMsg) ([]byte, error) { From 13111136cb43fddc8d762c6428386a2dbbf786b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=BF=97=E5=BC=BA?= <652732310@qq.com> Date: Tue, 8 Feb 2022 19:36:02 +0800 Subject: [PATCH 2/4] add docstring and test --- ethclient/ethclient.go | 1 + ethclient/ethclient_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index 755b5d0c6a42..b951c121ab06 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -456,6 +456,7 @@ func (ec *Client) CallContract(ctx context.Context, msg ethereum.CallMsg, blockN return hex, nil } +// CallContractAtHash is almost the same as CallContract except that it selects the block by block hash instead of block height. func (ec *Client) CallContractAtHash(ctx context.Context, msg ethereum.CallMsg, blockHash common.Hash) ([]byte, error) { var hex hexutil.Bytes err := ec.c.CallContext(ctx, &hex, "eth_call", toCallArg(msg), rpc.BlockNumberOrHashWithHash(blockHash, false)) diff --git a/ethclient/ethclient_test.go b/ethclient/ethclient_test.go index d56febc91d74..a7ce3ddedd8b 100644 --- a/ethclient/ethclient_test.go +++ b/ethclient/ethclient_test.go @@ -285,6 +285,9 @@ func TestEthClient(t *testing.T) { "CallContract": { func(t *testing.T) { testCallContract(t, client) }, }, + "CallContractAtHash": { + func(t *testing.T) { testCallContractAtHash(t, client) }, + }, "AtFunctions": { func(t *testing.T) { testAtFunctions(t, client) }, }, @@ -507,6 +510,33 @@ func testStatusFunctions(t *testing.T, client *rpc.Client) { } } +func testCallContractAtHash(t *testing.T, client *rpc.Client) { + ec := NewClient(client) + + // EstimateGas + msg := ethereum.CallMsg{ + From: testAddr, + To: &common.Address{}, + Gas: 21000, + Value: big.NewInt(1), + } + gas, err := ec.EstimateGas(context.Background(), msg) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if gas != 21000 { + t.Fatalf("unexpected gas price: %v", gas) + } + block, err := ec.BlockByNumber(context.Background(), big.NewInt(1)) + if err != nil { + t.Fatalf("BlockByNumber error: %v", err) + } + // CallContract + if _, err := ec.CallContractAtHash(context.Background(), msg, block.Hash()); err != nil { + t.Fatalf("unexpected error: %v", err) + } +} + func testCallContract(t *testing.T, client *rpc.Client) { ec := NewClient(client) From ca4aff9446d4be36f51f19b7ebf6cb2575e50cf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E5=BF=97=E5=BC=BA?= <652732310@qq.com> Date: Wed, 9 Feb 2022 08:02:17 +0800 Subject: [PATCH 3/4] optimize test --- ethclient/ethclient_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethclient/ethclient_test.go b/ethclient/ethclient_test.go index a7ce3ddedd8b..4a8727b37478 100644 --- a/ethclient/ethclient_test.go +++ b/ethclient/ethclient_test.go @@ -527,7 +527,7 @@ func testCallContractAtHash(t *testing.T, client *rpc.Client) { if gas != 21000 { t.Fatalf("unexpected gas price: %v", gas) } - block, err := ec.BlockByNumber(context.Background(), big.NewInt(1)) + block, err := ec.HeaderByNumber(context.Background(), big.NewInt(1)) if err != nil { t.Fatalf("BlockByNumber error: %v", err) } From ad9266b9a157f4eb233fc8a14332468eee985990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Tue, 15 Feb 2022 11:54:55 +0200 Subject: [PATCH 4/4] ethclient: nits --- ethclient/ethclient.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index b951c121ab06..68389efbf437 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -456,7 +456,8 @@ func (ec *Client) CallContract(ctx context.Context, msg ethereum.CallMsg, blockN return hex, nil } -// CallContractAtHash is almost the same as CallContract except that it selects the block by block hash instead of block height. +// CallContractAtHash is almost the same as CallContract except that it selects +// the block by block hash instead of block height. func (ec *Client) CallContractAtHash(ctx context.Context, msg ethereum.CallMsg, blockHash common.Hash) ([]byte, error) { var hex hexutil.Bytes err := ec.c.CallContext(ctx, &hex, "eth_call", toCallArg(msg), rpc.BlockNumberOrHashWithHash(blockHash, false))