Implement EstimateFee for eth
This commit is contained in:
parent
73486e851f
commit
2fe6118aca
@ -282,6 +282,7 @@ func ethTxToTx(tx *rpcTransaction, blocktime int64, confirmations uint32) (*bcha
|
|||||||
}, nil
|
}, 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) {
|
func (b *EthRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), b.timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), b.timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
@ -339,6 +340,7 @@ func (b *EthRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) {
|
|||||||
return &bbk, nil
|
return &bbk, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetTransaction returns a transaction by the transaction ID.
|
||||||
func (b *EthRPC) GetTransaction(txid string) (*bchain.Tx, error) {
|
func (b *EthRPC) GetTransaction(txid string) (*bchain.Tx, error) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), b.timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), b.timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
@ -384,10 +386,28 @@ func (b *EthRPC) GetMempool() ([]string, error) {
|
|||||||
panic("not implemented")
|
panic("not implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *EthRPC) EstimateSmartFee(blocks int, conservative bool) (float64, error) {
|
// EstimateFee returns fee estimation.
|
||||||
panic("not implemented")
|
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) {
|
func (b *EthRPC) SendRawTransaction(tx string) (string, error) {
|
||||||
panic("not implemented")
|
panic("not implemented")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,7 +13,7 @@ import (
|
|||||||
|
|
||||||
var rpcURL = "ws://10.34.3.4:18546"
|
var rpcURL = "ws://10.34.3.4:18546"
|
||||||
var ethClient *ethclient.Client
|
var ethClient *ethclient.Client
|
||||||
var ethRpcClient *rpc.Client
|
var ethRPCClient *rpc.Client
|
||||||
|
|
||||||
func setupEthRPC() *EthRPC {
|
func setupEthRPC() *EthRPC {
|
||||||
if ethClient == nil {
|
if ethClient == nil {
|
||||||
@ -22,12 +22,12 @@ func setupEthRPC() *EthRPC {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
ec := ethclient.NewClient(rc)
|
ec := ethclient.NewClient(rc)
|
||||||
ethRpcClient = rc
|
ethRPCClient = rc
|
||||||
ethClient = ec
|
ethClient = ec
|
||||||
}
|
}
|
||||||
return &EthRPC{
|
return &EthRPC{
|
||||||
client: ethClient,
|
client: ethClient,
|
||||||
rpc: ethRpcClient,
|
rpc: ethRPCClient,
|
||||||
timeout: time.Duration(25) * time.Second,
|
timeout: time.Duration(25) * time.Second,
|
||||||
rpcURL: "ws://10.34.3.4:18546",
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user