diff --git a/bchain/coins/eth/ethrpc.go b/bchain/coins/eth/ethrpc.go index cf2eae50..c1c64a28 100644 --- a/bchain/coins/eth/ethrpc.go +++ b/bchain/coins/eth/ethrpc.go @@ -284,7 +284,13 @@ func (b *EthRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) { ctx, cancel := context.WithTimeout(context.Background(), b.timeout) defer cancel() var raw json.RawMessage - err := b.rpc.CallContext(ctx, &raw, "eth_getBlockByHash", ethcommon.HexToHash(hash), true) + var err error + if hash != "" { + err = b.rpc.CallContext(ctx, &raw, "eth_getBlockByHash", ethcommon.HexToHash(hash), true) + } else { + + err = b.rpc.CallContext(ctx, &raw, "eth_getBlockByNumber", fmt.Sprintf("%#x", height), true) + } if err != nil { return nil, err } else if len(raw) == 0 { diff --git a/bchain/coins/eth/ethrpc_test.go b/bchain/coins/eth/ethrpc_test.go index 2f8631fe..8b8e5d61 100644 --- a/bchain/coins/eth/ethrpc_test.go +++ b/bchain/coins/eth/ethrpc_test.go @@ -244,7 +244,7 @@ func TestEthRPC_GetBlock(t *testing.T) { wantErr bool }{ { - name: "2870000", + name: "2870000 by hash", fields: fields{ b: setupEthRPC(), }, @@ -260,6 +260,23 @@ func TestEthRPC_GetBlock(t *testing.T) { }, wantTxCount: 12, }, + { + name: "2870000 by height", + fields: fields{ + b: setupEthRPC(), + }, + args: args{ + height: 2870000, + }, + want: &bchain.Block{ + BlockHeader: bchain.BlockHeader{ + Hash: "eccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903895", + Height: 2870000, + Confirmations: int(uint32(bh.Number.Uint64()) - 2870000), + }, + }, + wantTxCount: 12, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {