From d20626f49e246e393642de45ef763a852a84ab1b Mon Sep 17 00:00:00 2001 From: Daniel Krawisz Date: Thu, 8 Sep 2016 12:10:46 -0500 Subject: [PATCH] Add EstimateFee func to wallet. --- wallet.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/wallet.go b/wallet.go index 0eb5806..5525199 100644 --- a/wallet.go +++ b/wallet.go @@ -2273,6 +2273,43 @@ func (c *Client) GetInfo() (*btcjson.InfoWalletResult, error) { return c.GetInfoAsync().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() +} + // TODO(davec): Implement // backupwallet (NYI in btcwallet) // encryptwallet (Won't be supported by btcwallet since it's always encrypted)