Skip to content

Commit 972c574

Browse files
committed
allow use the old "data" field
1 parent bba3fa9 commit 972c574

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

ethclient/ethclient.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ import (
3333

3434
// Client defines typed wrappers for the Ethereum RPC API.
3535
type Client struct {
36-
c *rpc.Client
36+
c *rpc.Client
37+
useData bool
3738
}
3839

3940
// Dial connects a client to the given URL.
@@ -52,14 +53,19 @@ func DialContext(ctx context.Context, rawurl string) (*Client, error) {
5253

5354
// NewClient creates a client that uses the given RPC client.
5455
func NewClient(c *rpc.Client) *Client {
55-
return &Client{c}
56+
return &Client{c: c}
5657
}
5758

5859
// Close closes the underlying RPC connection.
5960
func (ec *Client) Close() {
6061
ec.c.Close()
6162
}
6263

64+
// UseData makes toCallArg use the old "data" field instead of "input" field
65+
func (ec *Client) UseData() {
66+
ec.useData = true
67+
}
68+
6369
// Client gets the underlying RPC client.
6470
func (ec *Client) Client() *rpc.Client {
6571
return ec.c
@@ -518,7 +524,7 @@ func (ec *Client) PendingTransactionCount(ctx context.Context) (uint, error) {
518524
// blocks might not be available.
519525
func (ec *Client) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
520526
var hex hexutil.Bytes
521-
err := ec.c.CallContext(ctx, &hex, "eth_call", toCallArg(msg), toBlockNumArg(blockNumber))
527+
err := ec.c.CallContext(ctx, &hex, "eth_call", toCallArg(msg, ec.useData), toBlockNumArg(blockNumber))
522528
if err != nil {
523529
return nil, err
524530
}
@@ -529,7 +535,7 @@ func (ec *Client) CallContract(ctx context.Context, msg ethereum.CallMsg, blockN
529535
// the block by block hash instead of block height.
530536
func (ec *Client) CallContractAtHash(ctx context.Context, msg ethereum.CallMsg, blockHash common.Hash) ([]byte, error) {
531537
var hex hexutil.Bytes
532-
err := ec.c.CallContext(ctx, &hex, "eth_call", toCallArg(msg), rpc.BlockNumberOrHashWithHash(blockHash, false))
538+
err := ec.c.CallContext(ctx, &hex, "eth_call", toCallArg(msg, ec.useData), rpc.BlockNumberOrHashWithHash(blockHash, false))
533539
if err != nil {
534540
return nil, err
535541
}
@@ -540,7 +546,7 @@ func (ec *Client) CallContractAtHash(ctx context.Context, msg ethereum.CallMsg,
540546
// The state seen by the contract call is the pending state.
541547
func (ec *Client) PendingCallContract(ctx context.Context, msg ethereum.CallMsg) ([]byte, error) {
542548
var hex hexutil.Bytes
543-
err := ec.c.CallContext(ctx, &hex, "eth_call", toCallArg(msg), "pending")
549+
err := ec.c.CallContext(ctx, &hex, "eth_call", toCallArg(msg, ec.useData), "pending")
544550
if err != nil {
545551
return nil, err
546552
}
@@ -605,7 +611,7 @@ func (ec *Client) FeeHistory(ctx context.Context, blockCount uint64, lastBlock *
605611
// but it should provide a basis for setting a reasonable default.
606612
func (ec *Client) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error) {
607613
var hex hexutil.Uint64
608-
err := ec.c.CallContext(ctx, &hex, "eth_estimateGas", toCallArg(msg))
614+
err := ec.c.CallContext(ctx, &hex, "eth_estimateGas", toCallArg(msg, ec.useData))
609615
if err != nil {
610616
return 0, err
611617
}
@@ -639,13 +645,17 @@ func toBlockNumArg(number *big.Int) string {
639645
return fmt.Sprintf("<invalid %d>", number)
640646
}
641647

642-
func toCallArg(msg ethereum.CallMsg) interface{} {
648+
func toCallArg(msg ethereum.CallMsg, useData bool) interface{} {
643649
arg := map[string]interface{}{
644650
"from": msg.From,
645651
"to": msg.To,
646652
}
647653
if len(msg.Data) > 0 {
648-
arg["input"] = hexutil.Bytes(msg.Data)
654+
if useData {
655+
arg["data"] = hexutil.Bytes(msg.Data)
656+
} else {
657+
arg["input"] = hexutil.Bytes(msg.Data)
658+
}
649659
}
650660
if msg.Value != nil {
651661
arg["value"] = (*hexutil.Big)(msg.Value)

0 commit comments

Comments
 (0)