diff --git a/btcjson/walletsvrcmds.go b/btcjson/walletsvrcmds.go index 53bb93da14..be1a67fbf0 100644 --- a/btcjson/walletsvrcmds.go +++ b/btcjson/walletsvrcmds.go @@ -188,6 +188,15 @@ func NewGetBalanceCmd(account *string, minConf *int) *GetBalanceCmd { } } +// GetBalancesCmd defines the getbalances JSON-RPC command. +type GetBalancesCmd struct{} + +// NewGetBalancesCmd returns a new instance which can be used to issue a +// getbalances JSON-RPC command. +func NewGetBalancesCmd() *GetBalancesCmd { + return &GetBalancesCmd{} +} + // GetNewAddressCmd defines the getnewaddress JSON-RPC command. type GetNewAddressCmd struct { Account *string @@ -693,6 +702,7 @@ func init() { MustRegisterCmd("getaccountaddress", (*GetAccountAddressCmd)(nil), flags) MustRegisterCmd("getaddressesbyaccount", (*GetAddressesByAccountCmd)(nil), flags) MustRegisterCmd("getbalance", (*GetBalanceCmd)(nil), flags) + MustRegisterCmd("getbalances", (*GetBalancesCmd)(nil), flags) MustRegisterCmd("getnewaddress", (*GetNewAddressCmd)(nil), flags) MustRegisterCmd("getrawchangeaddress", (*GetRawChangeAddressCmd)(nil), flags) MustRegisterCmd("getreceivedbyaccount", (*GetReceivedByAccountCmd)(nil), flags) diff --git a/btcjson/walletsvrcmds_test.go b/btcjson/walletsvrcmds_test.go index 2e0f1d0f8e..554a87413a 100644 --- a/btcjson/walletsvrcmds_test.go +++ b/btcjson/walletsvrcmds_test.go @@ -250,6 +250,17 @@ func TestWalletSvrCmds(t *testing.T) { MinConf: btcjson.Int(6), }, }, + { + name: "getbalances", + newCmd: func() (interface{}, error) { + return btcjson.NewCmd("getbalances") + }, + staticCmd: func() interface{} { + return btcjson.NewGetBalancesCmd() + }, + marshalled: `{"jsonrpc":"1.0","method":"getbalances","params":[],"id":1}`, + unmarshalled: &btcjson.GetBalancesCmd{}, + }, { name: "getnewaddress", newCmd: func() (interface{}, error) { diff --git a/btcjson/walletsvrresults.go b/btcjson/walletsvrresults.go index 9246d131ae..6e69ed9066 100644 --- a/btcjson/walletsvrresults.go +++ b/btcjson/walletsvrresults.go @@ -159,3 +159,17 @@ type GetBestBlockResult struct { Hash string `json:"hash"` Height int32 `json:"height"` } + +// BalanceDetailsResult models the details data from the `getbalances` command. +type BalanceDetailsResult struct { + Trusted float64 `json:"trusted"` + UntrustedPending float64 `json:"untrusted_pending"` + Immature float64 `json:"immature"` + Used *float64 `json:"used"` +} + +// GetBalancesResult models the data returned from the getbalances command. +type GetBalancesResult struct { + Mine BalanceDetailsResult `json:"mine"` + WatchOnly *BalanceDetailsResult `json:"watchonly"` +} diff --git a/rpcclient/wallet.go b/rpcclient/wallet.go index d43be26181..e92d1aab28 100644 --- a/rpcclient/wallet.go +++ b/rpcclient/wallet.go @@ -1547,6 +1547,43 @@ func (c *Client) GetBalanceMinConf(account string, minConfirms int) (btcutil.Amo return c.GetBalanceMinConfAsync(account, minConfirms).Receive() } +// FutureGetBalancesResult is a future promise to deliver the result of a +// GetBalancesAsync RPC invocation (or an applicable error). +type FutureGetBalancesResult chan *response + +// Receive waits for the response promised by the future and returns the +// available balances from the server. +func (r FutureGetBalancesResult) Receive() (*btcjson.GetBalancesResult, error) { + res, err := receiveFuture(r) + if err != nil { + return nil, err + } + + // Unmarshal result as a floating point number. + var balances btcjson.GetBalancesResult + err = json.Unmarshal(res, &balances) + if err != nil { + return nil, err + } + + return &balances, nil +} + +// GetBalancesAsync 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 GetBalances for the blocking version and more details. +func (c *Client) GetBalancesAsync() FutureGetBalancesResult { + cmd := btcjson.NewGetBalancesCmd() + return c.sendCmd(cmd) +} + +// GetBalances returns the available balances from the server. +func (c *Client) GetBalances() (*btcjson.GetBalancesResult, error) { + return c.GetBalancesAsync().Receive() +} + // FutureGetReceivedByAccountResult is a future promise to deliver the result of // a GetReceivedByAccountAsync or GetReceivedByAccountMinConfAsync RPC // invocation (or an applicable error).