Added RPC method EstimateFee

This commit is contained in:
Jakub Matys 2018-03-27 10:30:30 +02:00
parent 71eec95c5b
commit 18ce3edac3
5 changed files with 85 additions and 2 deletions

View File

@ -116,6 +116,11 @@ func (c *blockChainWithMetrics) EstimateSmartFee(blocks int, conservative bool)
return c.b.EstimateSmartFee(blocks, conservative) return c.b.EstimateSmartFee(blocks, conservative)
} }
func (c *blockChainWithMetrics) EstimateFee(blocks int) (v float64, err error) {
defer func(s time.Time) { c.observeRPCLatency("EstimateFee", s, err) }(time.Now())
return c.b.EstimateFee(blocks)
}
func (c *blockChainWithMetrics) SendRawTransaction(tx string) (v string, err error) { func (c *blockChainWithMetrics) SendRawTransaction(tx string) (v string, err error) {
defer func(s time.Time) { c.observeRPCLatency("SendRawTransaction", s, err) }(time.Now()) defer func(s time.Time) { c.observeRPCLatency("SendRawTransaction", s, err) }(time.Now())
return c.b.SendRawTransaction(tx) return c.b.SendRawTransaction(tx)

View File

@ -255,6 +255,20 @@ type resEstimateSmartFee struct {
} `json:"result"` } `json:"result"`
} }
// estimatefee
type cmdEstimateFee struct {
Method string `json:"method"`
Params struct {
Blocks int `json:"nblocks"`
} `json:"params"`
}
type resEstimateFee struct {
Error *bchain.RPCError `json:"error"`
Result float64 `json:"result"`
}
// sendrawtransaction // sendrawtransaction
type cmdSendRawTransaction struct { type cmdSendRawTransaction struct {
@ -555,6 +569,24 @@ func (b *BitcoinRPC) EstimateSmartFee(blocks int, conservative bool) (float64, e
return res.Result.Feerate, nil return res.Result.Feerate, nil
} }
// EstimateFee returns fee estimation.
func (b *BitcoinRPC) EstimateFee(blocks int) (float64, error) {
glog.V(1).Info("rpc: estimatefee ", blocks)
res := resEstimateFee{}
req := cmdEstimateFee{Method: "estimatefee"}
req.Params.Blocks = blocks
err := b.Call(&req, &res)
if err != nil {
return 0, err
}
if res.Error != nil {
return 0, res.Error
}
return res.Result, nil
}
// SendRawTransaction sends raw transaction. // SendRawTransaction sends raw transaction.
func (b *BitcoinRPC) SendRawTransaction(tx string) (string, error) { func (b *BitcoinRPC) SendRawTransaction(tx string) (string, error) {
glog.V(1).Info("rpc: sendrawtransaction") glog.V(1).Info("rpc: sendrawtransaction")

View File

@ -70,7 +70,7 @@ type resGetBlockHeader struct {
// estimatefee // estimatefee
type resEstimateSmartFee struct { type resEstimateFee struct {
Error *bchain.RPCError `json:"error"` Error *bchain.RPCError `json:"error"`
Result float64 `json:"result"` Result float64 `json:"result"`
} }
@ -185,7 +185,18 @@ func (z *ZCashRPC) GetBlockHeader(hash string) (*bchain.BlockHeader, error) {
func (z *ZCashRPC) EstimateSmartFee(blocks int, conservative bool) (float64, error) { func (z *ZCashRPC) EstimateSmartFee(blocks int, conservative bool) (float64, error) {
glog.V(1).Info("rpc: estimatesmartfee") glog.V(1).Info("rpc: estimatesmartfee")
res := resEstimateSmartFee{} return z.estimateFee(blocks)
}
// EstimateFee returns fee estimation.
func (z *ZCashRPC) EstimateFee(blocks int) (float64, error) {
glog.V(1).Info("rpc: estimatefee ", blocks)
return z.estimateFee(blocks)
}
func (z *ZCashRPC) estimateFee(blocks int) (float64, error) {
res := resEstimateFee{}
req := untypedArrayParams{Method: "estimatefee"} req := untypedArrayParams{Method: "estimatefee"}
req.Params = append(req.Params, blocks) req.Params = append(req.Params, blocks)
err := z.Call(&req, &res) err := z.Call(&req, &res)

View File

@ -102,6 +102,7 @@ type BlockChain interface {
GetMempool() ([]string, error) GetMempool() ([]string, error)
GetTransaction(txid string) (*Tx, error) GetTransaction(txid string) (*Tx, error)
EstimateSmartFee(blocks int, conservative bool) (float64, error) EstimateSmartFee(blocks int, conservative bool) (float64, error)
EstimateFee(blocks int) (float64, error)
SendRawTransaction(tx string) (string, error) SendRawTransaction(tx string) (string, error)
// mempool // mempool
ResyncMempool(onNewTxAddr func(txid string, addr string)) error ResyncMempool(onNewTxAddr func(txid string, addr string)) error

View File

@ -162,6 +162,13 @@ var onMessageHandlers = map[string]func(*SocketIoServer, json.RawMessage) (inter
} }
return return
}, },
"estimateFee": func(s *SocketIoServer, params json.RawMessage) (rv interface{}, err error) {
blocks, err := unmarshalEstimateFee(params)
if err == nil {
rv, err = s.estimateFee(blocks)
}
return
},
"getInfo": func(s *SocketIoServer, params json.RawMessage) (rv interface{}, err error) { "getInfo": func(s *SocketIoServer, params json.RawMessage) (rv interface{}, err error) {
return s.getInfo() return s.getInfo()
}, },
@ -527,6 +534,33 @@ func (s *SocketIoServer) estimateSmartFee(blocks int, conservative bool) (res re
return return
} }
func unmarshalEstimateFee(params []byte) (blocks int, err error) {
p, err := unmarshalArray(params, 1)
if err != nil {
return
}
fblocks, ok := p[0].(float64)
if !ok {
err = errors.New("Invalid parameter nblocks")
return
}
blocks = int(fblocks)
return
}
type resultEstimateFee struct {
Result float64 `json:"result"`
}
func (s *SocketIoServer) estimateFee(blocks int) (res resultEstimateFee, err error) {
fee, err := s.chain.EstimateFee(blocks)
if err != nil {
return
}
res.Result = fee
return
}
type resultGetInfo struct { type resultGetInfo struct {
Result struct { Result struct {
Version int `json:"version,omitempty"` Version int `json:"version,omitempty"`