Skip to content

Commit

Permalink
Add getbalances RPC client command
Browse files Browse the repository at this point in the history
  • Loading branch information
federicobond authored and jcvernaleo committed Jun 29, 2020
1 parent e4f5902 commit 7b2ff5d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
10 changes: 10 additions & 0 deletions btcjson/walletsvrcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions btcjson/walletsvrcmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions btcjson/walletsvrresults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
37 changes: 37 additions & 0 deletions rpcclient/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down

0 comments on commit 7b2ff5d

Please sign in to comment.