From 192cbf6db649876389876c82f73336068715eb08 Mon Sep 17 00:00:00 2001 From: Daniel Krawisz Date: Thu, 8 Sep 2016 12:10:46 -0500 Subject: [PATCH] Add estimatefee functionality. --- chain.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/chain.go b/chain.go index 9d6144e..974641c 100644 --- a/chain.go +++ b/chain.go @@ -355,6 +355,43 @@ func (c *Client) GetRawMempoolVerbose() (map[string]btcjson.GetRawMempoolVerbose return c.GetRawMempoolVerboseAsync().Receive() } +// FutureEstimateFeeResult is a future promise to deliver the result of a +// EstimateFeeAsync RPC invocation (or an applicable error). +type FutureEstimateFeeResult chan *response + +// Receive waits for the response promised by the future and returns the info +// provided by the server. +func (r FutureEstimateFeeResult) Receive() (float64, error) { + res, err := receiveFuture(r) + if err != nil { + return -1, err + } + + // Unmarshal result as a getinfo result object. + var fee float64 + err = json.Unmarshal(res, &fee) + if err != nil { + return -1, err + } + + return fee, nil +} + +// EstimateFeeAsync returns an instance of a type that can be used to get the result +// of the RPC at some future time by invoking the Receive function on the +// returned instance. +// +// See EstimateFee for the blocking version and more details. +func (c *Client) EstimateFeeAsync(numBlocks int64) FutureEstimateFeeResult { + cmd := btcjson.NewEstimateFeeCmd(numBlocks) + return c.sendCmd(cmd) +} + +// EstimateFee provides an estimated fee in bitcoins per kilobyte. +func (c *Client) EstimateFee(numBlocks int64) (float64, error) { + return c.EstimateFeeAsync(numBlocks).Receive() +} + // FutureVerifyChainResult is a future promise to deliver the result of a // VerifyChainAsync, VerifyChainLevelAsyncRPC, or VerifyChainBlocksAsync // invocation (or an applicable error).