Skip to content

Commit 1609df3

Browse files
committed
ethclient: use package hexutil for JSON handling
1 parent 24f2887 commit 1609df3

File tree

1 file changed

+35
-34
lines changed

1 file changed

+35
-34
lines changed

ethclient/ethclient.go

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/ethereum/go-ethereum"
2626
"github.com/ethereum/go-ethereum/common"
27+
"github.com/ethereum/go-ethereum/common/hexutil"
2728
"github.com/ethereum/go-ethereum/core/types"
2829
"github.com/ethereum/go-ethereum/core/vm"
2930
"github.com/ethereum/go-ethereum/rlp"
@@ -156,9 +157,9 @@ func (ec *Client) TransactionByHash(ctx context.Context, hash common.Hash) (*typ
156157

157158
// TransactionCount returns the total number of transactions in the given block.
158159
func (ec *Client) TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error) {
159-
var num rpc.HexNumber
160+
var num hexutil.Uint
160161
err := ec.c.CallContext(ctx, &num, "eth_getBlockTransactionCountByHash", blockHash)
161-
return num.Uint(), err
162+
return uint(num), err
162163
}
163164

164165
// TransactionInBlock returns a single transaction at index in the given block.
@@ -196,11 +197,11 @@ func toBlockNumArg(number *big.Int) string {
196197
}
197198

198199
type rpcProgress struct {
199-
StartingBlock rpc.HexNumber
200-
CurrentBlock rpc.HexNumber
201-
HighestBlock rpc.HexNumber
202-
PulledStates rpc.HexNumber
203-
KnownStates rpc.HexNumber
200+
StartingBlock hexutil.Uint64
201+
CurrentBlock hexutil.Uint64
202+
HighestBlock hexutil.Uint64
203+
PulledStates hexutil.Uint64
204+
KnownStates hexutil.Uint64
204205
}
205206

206207
// SyncProgress retrieves the current progress of the sync algorithm. If there's
@@ -220,11 +221,11 @@ func (ec *Client) SyncProgress(ctx context.Context) (*ethereum.SyncProgress, err
220221
return nil, err
221222
}
222223
return &ethereum.SyncProgress{
223-
StartingBlock: progress.StartingBlock.Uint64(),
224-
CurrentBlock: progress.CurrentBlock.Uint64(),
225-
HighestBlock: progress.HighestBlock.Uint64(),
226-
PulledStates: progress.PulledStates.Uint64(),
227-
KnownStates: progress.KnownStates.Uint64(),
224+
StartingBlock: uint64(progress.StartingBlock),
225+
CurrentBlock: uint64(progress.CurrentBlock),
226+
HighestBlock: uint64(progress.HighestBlock),
227+
PulledStates: uint64(progress.PulledStates),
228+
KnownStates: uint64(progress.KnownStates),
228229
}, nil
229230
}
230231

@@ -239,33 +240,33 @@ func (ec *Client) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header)
239240
// BalanceAt returns the wei balance of the given account.
240241
// The block number can be nil, in which case the balance is taken from the latest known block.
241242
func (ec *Client) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) {
242-
var result rpc.HexNumber
243+
var result hexutil.Big
243244
err := ec.c.CallContext(ctx, &result, "eth_getBalance", account, toBlockNumArg(blockNumber))
244245
return (*big.Int)(&result), err
245246
}
246247

247248
// StorageAt returns the value of key in the contract storage of the given account.
248249
// The block number can be nil, in which case the value is taken from the latest known block.
249250
func (ec *Client) StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error) {
250-
var result rpc.HexBytes
251+
var result hexutil.Bytes
251252
err := ec.c.CallContext(ctx, &result, "eth_getStorageAt", account, key, toBlockNumArg(blockNumber))
252253
return result, err
253254
}
254255

255256
// CodeAt returns the contract code of the given account.
256257
// The block number can be nil, in which case the code is taken from the latest known block.
257258
func (ec *Client) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) {
258-
var result rpc.HexBytes
259+
var result hexutil.Bytes
259260
err := ec.c.CallContext(ctx, &result, "eth_getCode", account, toBlockNumArg(blockNumber))
260261
return result, err
261262
}
262263

263264
// NonceAt returns the account nonce of the given account.
264265
// The block number can be nil, in which case the nonce is taken from the latest known block.
265266
func (ec *Client) NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) {
266-
var result rpc.HexNumber
267+
var result hexutil.Uint64
267268
err := ec.c.CallContext(ctx, &result, "eth_getTransactionCount", account, toBlockNumArg(blockNumber))
268-
return result.Uint64(), err
269+
return uint64(result), err
269270
}
270271

271272
// Filters
@@ -299,38 +300,38 @@ func toFilterArg(q ethereum.FilterQuery) interface{} {
299300

300301
// PendingBalanceAt returns the wei balance of the given account in the pending state.
301302
func (ec *Client) PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error) {
302-
var result rpc.HexNumber
303+
var result hexutil.Big
303304
err := ec.c.CallContext(ctx, &result, "eth_getBalance", account, "pending")
304305
return (*big.Int)(&result), err
305306
}
306307

307308
// PendingStorageAt returns the value of key in the contract storage of the given account in the pending state.
308309
func (ec *Client) PendingStorageAt(ctx context.Context, account common.Address, key common.Hash) ([]byte, error) {
309-
var result rpc.HexBytes
310+
var result hexutil.Bytes
310311
err := ec.c.CallContext(ctx, &result, "eth_getStorageAt", account, key, "pending")
311312
return result, err
312313
}
313314

314315
// PendingCodeAt returns the contract code of the given account in the pending state.
315316
func (ec *Client) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) {
316-
var result rpc.HexBytes
317+
var result hexutil.Bytes
317318
err := ec.c.CallContext(ctx, &result, "eth_getCode", account, "pending")
318319
return result, err
319320
}
320321

321322
// PendingNonceAt returns the account nonce of the given account in the pending state.
322323
// This is the nonce that should be used for the next transaction.
323324
func (ec *Client) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
324-
var result rpc.HexNumber
325+
var result hexutil.Uint64
325326
err := ec.c.CallContext(ctx, &result, "eth_getTransactionCount", account, "pending")
326-
return result.Uint64(), err
327+
return uint64(result), err
327328
}
328329

329330
// PendingTransactionCount returns the total number of transactions in the pending state.
330331
func (ec *Client) PendingTransactionCount(ctx context.Context) (uint, error) {
331-
var num rpc.HexNumber
332+
var num hexutil.Uint
332333
err := ec.c.CallContext(ctx, &num, "eth_getBlockTransactionCountByNumber", "pending")
333-
return num.Uint(), err
334+
return uint(num), err
334335
}
335336

336337
// TODO: SubscribePendingTransactions (needs server side)
@@ -344,29 +345,29 @@ func (ec *Client) PendingTransactionCount(ctx context.Context) (uint, error) {
344345
// case the code is taken from the latest known block. Note that state from very old
345346
// blocks might not be available.
346347
func (ec *Client) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
347-
var hex string
348+
var hex hexutil.Bytes
348349
err := ec.c.CallContext(ctx, &hex, "eth_call", toCallArg(msg), toBlockNumArg(blockNumber))
349350
if err != nil {
350351
return nil, err
351352
}
352-
return common.FromHex(hex), nil
353+
return hex, nil
353354
}
354355

355356
// PendingCallContract executes a message call transaction using the EVM.
356357
// The state seen by the contract call is the pending state.
357358
func (ec *Client) PendingCallContract(ctx context.Context, msg ethereum.CallMsg) ([]byte, error) {
358-
var hex string
359+
var hex hexutil.Bytes
359360
err := ec.c.CallContext(ctx, &hex, "eth_call", toCallArg(msg), "pending")
360361
if err != nil {
361362
return nil, err
362363
}
363-
return common.FromHex(hex), nil
364+
return hex, nil
364365
}
365366

366367
// SuggestGasPrice retrieves the currently suggested gas price to allow a timely
367368
// execution of a transaction.
368369
func (ec *Client) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
369-
var hex rpc.HexNumber
370+
var hex hexutil.Big
370371
if err := ec.c.CallContext(ctx, &hex, "eth_gasPrice"); err != nil {
371372
return nil, err
372373
}
@@ -378,7 +379,7 @@ func (ec *Client) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
378379
// the true gas limit requirement as other transactions may be added or removed by miners,
379380
// but it should provide a basis for setting a reasonable default.
380381
func (ec *Client) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (*big.Int, error) {
381-
var hex rpc.HexNumber
382+
var hex hexutil.Big
382383
err := ec.c.CallContext(ctx, &hex, "eth_estimateGas", toCallArg(msg))
383384
if err != nil {
384385
return nil, err
@@ -404,16 +405,16 @@ func toCallArg(msg ethereum.CallMsg) interface{} {
404405
"to": msg.To,
405406
}
406407
if len(msg.Data) > 0 {
407-
arg["data"] = fmt.Sprintf("%#x", msg.Data)
408+
arg["data"] = hexutil.Bytes(msg.Data)
408409
}
409410
if msg.Value != nil {
410-
arg["value"] = fmt.Sprintf("%#x", msg.Value)
411+
arg["value"] = (*hexutil.Big)(msg.Value)
411412
}
412413
if msg.Gas != nil {
413-
arg["gas"] = fmt.Sprintf("%#x", msg.Gas)
414+
arg["gas"] = (*hexutil.Big)(msg.Gas)
414415
}
415416
if msg.GasPrice != nil {
416-
arg["gasPrice"] = fmt.Sprintf("%#x", msg.GasPrice)
417+
arg["gasPrice"] = (*hexutil.Big)(msg.GasPrice)
417418
}
418419
return arg
419420
}

0 commit comments

Comments
 (0)