diff --git a/bchain/coins/eth/ethrpc.go b/bchain/coins/eth/ethrpc.go index 138d42a6..552c7618 100644 --- a/bchain/coins/eth/ethrpc.go +++ b/bchain/coins/eth/ethrpc.go @@ -282,6 +282,7 @@ func ethTxToTx(tx *rpcTransaction, blocktime int64, confirmations uint32) (*bcha }, nil } +// GetBlock returns block with given hash or height, hash has precedence if both passed func (b *EthRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) { ctx, cancel := context.WithTimeout(context.Background(), b.timeout) defer cancel() @@ -339,6 +340,7 @@ func (b *EthRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) { return &bbk, nil } +// GetTransaction returns a transaction by the transaction ID. func (b *EthRPC) GetTransaction(txid string) (*bchain.Tx, error) { ctx, cancel := context.WithTimeout(context.Background(), b.timeout) defer cancel() @@ -384,10 +386,28 @@ func (b *EthRPC) GetMempool() ([]string, error) { panic("not implemented") } -func (b *EthRPC) EstimateSmartFee(blocks int, conservative bool) (float64, error) { - panic("not implemented") +// EstimateFee returns fee estimation. +func (b *EthRPC) EstimateFee(blocks int) (float64, error) { + return b.EstimateSmartFee(blocks, true) } +// EstimateSmartFee returns fee estimation. +func (b *EthRPC) EstimateSmartFee(blocks int, conservative bool) (float64, error) { + ctx, cancel := context.WithTimeout(context.Background(), b.timeout) + defer cancel() + // TODO - what parameters of msg to use to get better estimate, maybe more data from the wallet are needed + a := ethcommon.HexToAddress("0x1234567890123456789012345678901234567890") + msg := ethereum.CallMsg{ + To: &a, + } + g, err := b.client.EstimateGas(ctx, msg) + if err != nil { + return 0, err + } + return float64(g), nil +} + +// SendRawTransaction sends raw transaction. func (b *EthRPC) SendRawTransaction(tx string) (string, error) { panic("not implemented") } diff --git a/bchain/coins/eth/ethrpc_test.go b/bchain/coins/eth/ethrpc_test.go index b93ff6f1..3251b716 100644 --- a/bchain/coins/eth/ethrpc_test.go +++ b/bchain/coins/eth/ethrpc_test.go @@ -13,7 +13,7 @@ import ( var rpcURL = "ws://10.34.3.4:18546" var ethClient *ethclient.Client -var ethRpcClient *rpc.Client +var ethRPCClient *rpc.Client func setupEthRPC() *EthRPC { if ethClient == nil { @@ -22,12 +22,12 @@ func setupEthRPC() *EthRPC { panic(err) } ec := ethclient.NewClient(rc) - ethRpcClient = rc + ethRPCClient = rc ethClient = ec } return &EthRPC{ client: ethClient, - rpc: ethRpcClient, + rpc: ethRPCClient, timeout: time.Duration(25) * time.Second, rpcURL: "ws://10.34.3.4:18546", } @@ -429,3 +429,42 @@ func TestEthRPC_GetTransaction(t *testing.T) { }) } } + +func TestEthRPC_EstimateFee(t *testing.T) { + type fields struct { + b *EthRPC + } + type args struct { + blocks int + } + tests := []struct { + name string + fields fields + args args + want float64 + wantErr bool + }{ + { + name: "1", + fields: fields{ + b: setupEthRPC(), + }, + args: args{ + blocks: 10, + }, + want: 1., // check that there is some estimate + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := tt.fields.b.EstimateFee(tt.args.blocks) + if (err != nil) != tt.wantErr { + t.Errorf("EthRPC.EstimateFee() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got < tt.want { + t.Errorf("EthRPC.EstimateFee() = %v, want %v", got, tt.want) + } + }) + } +}