Add getbalances RPC client command

This commit is contained in:
Federico Bond 2020-06-17 23:29:50 -03:00 committed by John C. Vernaleo
parent e4f59022a3
commit 7b2ff5d180
4 changed files with 72 additions and 0 deletions

View file

@ -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)

View file

@ -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) {

View file

@ -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"`
}

View file

@ -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).