diff --git a/cmds.go b/cmds.go index 59efa075..550bdac4 100644 --- a/cmds.go +++ b/cmds.go @@ -18,6 +18,7 @@ func init() { btcjson.RegisterCustomCmd("getbalances", parseGetBalancesCmd) btcjson.RegisterCustomCmd("getbestblock", parseGetBestBlockCmd) btcjson.RegisterCustomCmd("getcurrentnet", parseGetCurrentNetCmd) + btcjson.RegisterCustomCmd("getunconfirmedbalance", parseGetUnconfirmedBalanceCmd) btcjson.RegisterCustomCmd("listaddresstransactions", parseListAddressTransactionsCmd) btcjson.RegisterCustomCmd("listalltransactions", parseListAllTransactionsCmd) btcjson.RegisterCustomCmd("notifynewtxs", parseNotifyNewTXsCmd) @@ -95,6 +96,108 @@ func (cmd *GetCurrentNetCmd) UnmarshalJSON(b []byte) error { return nil } +// GetUnconfirmedBalanceCmd is a type handling custom marshaling and +// unmarshaling of getunconfirmedbalance JSON websocket extension +// commands. +type GetUnconfirmedBalanceCmd struct { + id interface{} + Account string +} + +// Enforce that GetUnconfirmedBalanceCmd satisifies the btcjson.Cmd +// interface. +var _ btcjson.Cmd = &GetUnconfirmedBalanceCmd{} + +// NewGetUnconfirmedBalanceCmd creates a new GetUnconfirmedBalanceCmd. +func NewGetUnconfirmedBalanceCmd(id interface{}, + optArgs ...string) (*GetUnconfirmedBalanceCmd, error) { + + if len(optArgs) > 1 { + return nil, btcjson.ErrTooManyOptArgs + } + + // Optional parameters set to their defaults. + account := "" + + if len(optArgs) == 1 { + account = optArgs[0] + } + + return &GetUnconfirmedBalanceCmd{ + id: id, + Account: account, + }, nil +} + +// parseGetUnconfirmedBalanceCmd parses a RawCmd into a concrete type +// satisifying the btcjson.Cmd interface. This is used when registering +// the custom command with the btcjson parser. +func parseGetUnconfirmedBalanceCmd(r *btcjson.RawCmd) (btcjson.Cmd, error) { + if len(r.Params) > 1 { + return nil, btcjson.ErrWrongNumberOfParams + } + + if len(r.Params) == 0 { + // No optional args. + return NewGetUnconfirmedBalanceCmd(r.Id) + } + + // One optional parameter for account. + account, ok := r.Params[0].(string) + if !ok { + return nil, errors.New("first parameter account must be a string") + } + return NewGetUnconfirmedBalanceCmd(r.Id, account) +} + +// Id satisifies the Cmd interface by returning the ID of the command. +func (cmd *GetUnconfirmedBalanceCmd) Id() interface{} { + return cmd.id +} + +// Method satisifies the Cmd interface by returning the RPC method. +func (cmd *GetUnconfirmedBalanceCmd) Method() string { + return "getunconfirmedbalance" +} + +// MarshalJSON returns the JSON encoding of cmd. Part of the Cmd interface. +func (cmd *GetUnconfirmedBalanceCmd) MarshalJSON() ([]byte, error) { + // Fill a RawCmd and marshal. + raw := btcjson.RawCmd{ + Jsonrpc: "1.0", + Method: "getunconfirmedbalance", + Id: cmd.id, + } + + if cmd.Account != "" { + raw.Params = append(raw.Params, cmd.Account) + } + + return json.Marshal(raw) +} + +// UnmarshalJSON unmarshals the JSON encoding of cmd into cmd. Part of +// the Cmd interface. +func (cmd *GetUnconfirmedBalanceCmd) UnmarshalJSON(b []byte) error { + // Unmarshal into a RawCmd. + var r btcjson.RawCmd + if err := json.Unmarshal(b, &r); err != nil { + return err + } + + newCmd, err := parseGetUnconfirmedBalanceCmd(&r) + if err != nil { + return err + } + + concreteCmd, ok := newCmd.(*GetUnconfirmedBalanceCmd) + if !ok { + return btcjson.ErrInternal + } + *cmd = *concreteCmd + return nil +} + // GetBestBlockCmd is a type handling custom marshaling and // unmarshaling of getbestblock JSON websocket extension // commands. diff --git a/cmds_test.go b/cmds_test.go index 6c50c4d3..a41a81b9 100644 --- a/cmds_test.go +++ b/cmds_test.go @@ -89,6 +89,27 @@ var cmdtests = []struct { id: float64(1), }, }, + { + name: "getunconfirmedbalance no optargs", + f: func() (btcjson.Cmd, error) { + return NewGetUnconfirmedBalanceCmd(float64(1)) + }, + result: &GetUnconfirmedBalanceCmd{ + id: float64(1), + Account: "", + }, + }, + { + name: "getunconfirmedbalance one optarg", + f: func() (btcjson.Cmd, error) { + return NewGetUnconfirmedBalanceCmd(float64(1), + "abcde") + }, + result: &GetUnconfirmedBalanceCmd{ + id: float64(1), + Account: "abcde", + }, + }, { name: "listaddresstransactions no optargs", f: func() (btcjson.Cmd, error) { diff --git a/test_coverage.txt b/test_coverage.txt index 0548b2d9..e7992e4b 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -1,113 +1,119 @@ -github.com/conformal/btcws/cmds.go init 100.00% (11/11) +github.com/conformal/btcws/cmds.go init 100.00% (12/12) github.com/conformal/btcws/notifications.go init 100.00% (7/7) github.com/conformal/btcws/cmds.go ListAddressTransactionsCmd.MarshalJSON 100.00% (4/4) +github.com/conformal/btcws/cmds.go ListAllTransactionsCmd.MarshalJSON 100.00% (4/4) github.com/conformal/btcws/cmds.go WalletIsLockedCmd.MarshalJSON 100.00% (4/4) github.com/conformal/btcws/cmds.go RescanCmd.MarshalJSON 100.00% (4/4) github.com/conformal/btcws/cmds.go GetAddressBalanceCmd.MarshalJSON 100.00% (4/4) -github.com/conformal/btcws/cmds.go ListAllTransactionsCmd.MarshalJSON 100.00% (4/4) -github.com/conformal/btcws/cmds.go NotifySpentCmd.MarshalJSON 100.00% (2/2) -github.com/conformal/btcws/notifications.go TxNtfn.MarshalJSON 100.00% (2/2) -github.com/conformal/btcws/notifications.go AccountBalanceNtfn.MarshalJSON 100.00% (2/2) -github.com/conformal/btcws/cmds.go GetBestBlockCmd.MarshalJSON 100.00% (2/2) -github.com/conformal/btcws/cmds.go NotifyNewTXsCmd.MarshalJSON 100.00% (2/2) -github.com/conformal/btcws/notifications.go BlockConnectedNtfn.MarshalJSON 100.00% (2/2) +github.com/conformal/btcws/cmds.go GetUnconfirmedBalanceCmd.MarshalJSON 100.00% (4/4) github.com/conformal/btcws/cmds.go GetBalancesCmd.MarshalJSON 100.00% (2/2) -github.com/conformal/btcws/cmds.go GetCurrentNetCmd.MarshalJSON 100.00% (2/2) github.com/conformal/btcws/notifications.go WalletLockStateNtfn.MarshalJSON 100.00% (2/2) +github.com/conformal/btcws/notifications.go BlockDisconnectedNtfn.MarshalJSON 100.00% (2/2) github.com/conformal/btcws/notifications.go TxMinedNtfn.MarshalJSON 100.00% (2/2) +github.com/conformal/btcws/cmds.go NotifySpentCmd.MarshalJSON 100.00% (2/2) +github.com/conformal/btcws/notifications.go AccountBalanceNtfn.MarshalJSON 100.00% (2/2) +github.com/conformal/btcws/notifications.go BlockConnectedNtfn.MarshalJSON 100.00% (2/2) +github.com/conformal/btcws/notifications.go TxNtfn.MarshalJSON 100.00% (2/2) +github.com/conformal/btcws/cmds.go GetBestBlockCmd.MarshalJSON 100.00% (2/2) github.com/conformal/btcws/cmds.go CreateEncryptedWalletCmd.MarshalJSON 100.00% (2/2) github.com/conformal/btcws/notifications.go BtcdConnectedNtfn.MarshalJSON 100.00% (2/2) -github.com/conformal/btcws/notifications.go BlockDisconnectedNtfn.MarshalJSON 100.00% (2/2) -github.com/conformal/btcws/notifications.go TxMinedNtfn.Method 100.00% (1/1) +github.com/conformal/btcws/cmds.go NotifyNewTXsCmd.MarshalJSON 100.00% (2/2) +github.com/conformal/btcws/cmds.go GetCurrentNetCmd.MarshalJSON 100.00% (2/2) +github.com/conformal/btcws/cmds.go NotifySpentCmd.Id 100.00% (1/1) +github.com/conformal/btcws/cmds.go NotifySpentCmd.Method 100.00% (1/1) +github.com/conformal/btcws/cmds.go NewGetCurrentNetCmd 100.00% (1/1) github.com/conformal/btcws/cmds.go NewCreateEncryptedWalletCmd 100.00% (1/1) -github.com/conformal/btcws/notifications.go TxMinedNtfn.Id 100.00% (1/1) +github.com/conformal/btcws/notifications.go NewWalletLockStateNtfn 100.00% (1/1) github.com/conformal/btcws/cmds.go CreateEncryptedWalletCmd.Id 100.00% (1/1) github.com/conformal/btcws/cmds.go CreateEncryptedWalletCmd.Method 100.00% (1/1) -github.com/conformal/btcws/cmds.go NotifySpentCmd.Id 100.00% (1/1) -github.com/conformal/btcws/notifications.go NewTxMinedNtfn 100.00% (1/1) +github.com/conformal/btcws/cmds.go WalletIsLockedCmd.Method 100.00% (1/1) github.com/conformal/btcws/cmds.go NewGetBalancesCmd 100.00% (1/1) +github.com/conformal/btcws/cmds.go ListAddressTransactionsCmd.Method 100.00% (1/1) github.com/conformal/btcws/cmds.go GetBalancesCmd.Id 100.00% (1/1) github.com/conformal/btcws/cmds.go GetBalancesCmd.Method 100.00% (1/1) -github.com/conformal/btcws/notifications.go NewAccountBalanceNtfn 100.00% (1/1) -github.com/conformal/btcws/cmds.go NewGetCurrentNetCmd 100.00% (1/1) -github.com/conformal/btcws/notifications.go BtcdConnectedNtfn.Method 100.00% (1/1) github.com/conformal/btcws/cmds.go WalletIsLockedCmd.Id 100.00% (1/1) -github.com/conformal/btcws/cmds.go WalletIsLockedCmd.Method 100.00% (1/1) +github.com/conformal/btcws/cmds.go GetUnconfirmedBalanceCmd.Id 100.00% (1/1) github.com/conformal/btcws/notifications.go BtcdConnectedNtfn.Id 100.00% (1/1) -github.com/conformal/btcws/notifications.go NewBtcdConnectedNtfn 100.00% (1/1) -github.com/conformal/btcws/cmds.go ListAddressTransactionsCmd.Id 100.00% (1/1) -github.com/conformal/btcws/cmds.go ListAddressTransactionsCmd.Method 100.00% (1/1) -github.com/conformal/btcws/notifications.go BlockConnectedNtfn.Id 100.00% (1/1) -github.com/conformal/btcws/notifications.go BlockDisconnectedNtfn.Method 100.00% (1/1) -github.com/conformal/btcws/cmds.go ListAllTransactionsCmd.Id 100.00% (1/1) -github.com/conformal/btcws/cmds.go ListAllTransactionsCmd.Method 100.00% (1/1) +github.com/conformal/btcws/notifications.go NewBlockDisconnectedNtfn 100.00% (1/1) +github.com/conformal/btcws/notifications.go NewTxNtfn 100.00% (1/1) github.com/conformal/btcws/notifications.go BlockDisconnectedNtfn.Id 100.00% (1/1) -github.com/conformal/btcws/cmds.go GetCurrentNetCmd.Id 100.00% (1/1) -github.com/conformal/btcws/cmds.go GetAddressBalanceCmd.Id 100.00% (1/1) -github.com/conformal/btcws/cmds.go GetAddressBalanceCmd.Method 100.00% (1/1) -github.com/conformal/btcws/notifications.go AccountBalanceNtfn.Id 100.00% (1/1) -github.com/conformal/btcws/notifications.go AccountBalanceNtfn.Method 100.00% (1/1) -github.com/conformal/btcws/notifications.go NewBlockConnectedNtfn 100.00% (1/1) -github.com/conformal/btcws/cmds.go GetCurrentNetCmd.Method 100.00% (1/1) -github.com/conformal/btcws/notifications.go WalletLockStateNtfn.Method 100.00% (1/1) -github.com/conformal/btcws/cmds.go NewGetBestBlockCmd 100.00% (1/1) -github.com/conformal/btcws/notifications.go WalletLockStateNtfn.Id 100.00% (1/1) +github.com/conformal/btcws/notifications.go BlockDisconnectedNtfn.Method 100.00% (1/1) github.com/conformal/btcws/cmds.go GetBestBlockCmd.Id 100.00% (1/1) +github.com/conformal/btcws/notifications.go NewBtcdConnectedNtfn 100.00% (1/1) +github.com/conformal/btcws/notifications.go BtcdConnectedNtfn.Method 100.00% (1/1) +github.com/conformal/btcws/notifications.go TxMinedNtfn.Method 100.00% (1/1) +github.com/conformal/btcws/notifications.go NewTxMinedNtfn 100.00% (1/1) +github.com/conformal/btcws/notifications.go TxMinedNtfn.Id 100.00% (1/1) github.com/conformal/btcws/cmds.go GetBestBlockCmd.Method 100.00% (1/1) -github.com/conformal/btcws/notifications.go NewWalletLockStateNtfn 100.00% (1/1) +github.com/conformal/btcws/cmds.go GetCurrentNetCmd.Id 100.00% (1/1) github.com/conformal/btcws/cmds.go RescanCmd.Id 100.00% (1/1) github.com/conformal/btcws/cmds.go RescanCmd.Method 100.00% (1/1) -github.com/conformal/btcws/notifications.go TxNtfn.Method 100.00% (1/1) +github.com/conformal/btcws/cmds.go GetCurrentNetCmd.Method 100.00% (1/1) github.com/conformal/btcws/cmds.go NewNotifyNewTXsCmd 100.00% (1/1) -github.com/conformal/btcws/notifications.go TxNtfn.Id 100.00% (1/1) github.com/conformal/btcws/cmds.go NotifyNewTXsCmd.Id 100.00% (1/1) -github.com/conformal/btcws/cmds.go NotifyNewTXsCmd.Method 100.00% (1/1) -github.com/conformal/btcws/notifications.go NewTxNtfn 100.00% (1/1) +github.com/conformal/btcws/notifications.go WalletLockStateNtfn.Method 100.00% (1/1) github.com/conformal/btcws/cmds.go NewNotifySpentCmd 100.00% (1/1) -github.com/conformal/btcws/cmds.go NotifySpentCmd.Method 100.00% (1/1) -github.com/conformal/btcws/notifications.go NewBlockDisconnectedNtfn 100.00% (1/1) +github.com/conformal/btcws/notifications.go WalletLockStateNtfn.Id 100.00% (1/1) +github.com/conformal/btcws/cmds.go ListAllTransactionsCmd.Method 100.00% (1/1) +github.com/conformal/btcws/cmds.go GetAddressBalanceCmd.Id 100.00% (1/1) +github.com/conformal/btcws/cmds.go GetAddressBalanceCmd.Method 100.00% (1/1) +github.com/conformal/btcws/notifications.go NewAccountBalanceNtfn 100.00% (1/1) +github.com/conformal/btcws/notifications.go TxNtfn.Method 100.00% (1/1) +github.com/conformal/btcws/notifications.go AccountBalanceNtfn.Id 100.00% (1/1) +github.com/conformal/btcws/notifications.go AccountBalanceNtfn.Method 100.00% (1/1) +github.com/conformal/btcws/cmds.go NewGetBestBlockCmd 100.00% (1/1) +github.com/conformal/btcws/notifications.go NewBlockConnectedNtfn 100.00% (1/1) +github.com/conformal/btcws/notifications.go TxNtfn.Id 100.00% (1/1) +github.com/conformal/btcws/notifications.go BlockConnectedNtfn.Id 100.00% (1/1) github.com/conformal/btcws/notifications.go BlockConnectedNtfn.Method 100.00% (1/1) +github.com/conformal/btcws/cmds.go ListAddressTransactionsCmd.Id 100.00% (1/1) +github.com/conformal/btcws/cmds.go NotifyNewTXsCmd.Method 100.00% (1/1) +github.com/conformal/btcws/cmds.go GetUnconfirmedBalanceCmd.Method 100.00% (1/1) +github.com/conformal/btcws/cmds.go ListAllTransactionsCmd.Id 100.00% (1/1) github.com/conformal/btcws/cmds.go parseListAllTransactionsCmd 87.50% (7/8) +github.com/conformal/btcws/cmds.go NewGetAddressBalanceCmd 83.33% (5/6) +github.com/conformal/btcws/cmds.go NewListAddressTransactionsCmd 83.33% (5/6) +github.com/conformal/btcws/cmds.go NewRescanCmd 83.33% (5/6) github.com/conformal/btcws/cmds.go NewListAllTransactionsCmd 83.33% (5/6) github.com/conformal/btcws/cmds.go NewWalletIsLockedCmd 83.33% (5/6) -github.com/conformal/btcws/cmds.go NewListAddressTransactionsCmd 83.33% (5/6) -github.com/conformal/btcws/cmds.go NewGetAddressBalanceCmd 83.33% (5/6) -github.com/conformal/btcws/cmds.go NewRescanCmd 83.33% (5/6) +github.com/conformal/btcws/cmds.go NewGetUnconfirmedBalanceCmd 83.33% (5/6) github.com/conformal/btcws/cmds.go parseRescanCmd 77.78% (14/18) github.com/conformal/btcws/cmds.go parseListAddressTransactionsCmd 76.47% (13/17) github.com/conformal/btcws/cmds.go parseNotifyNewTXsCmd 75.00% (9/12) +github.com/conformal/btcws/cmds.go parseGetUnconfirmedBalanceCmd 75.00% (6/8) github.com/conformal/btcws/cmds.go parseWalletIsLockedCmd 75.00% (6/8) -github.com/conformal/btcws/cmds.go parseGetAddressBalanceCmd 72.73% (8/11) -github.com/conformal/btcws/cmds.go ListAllTransactionsCmd.UnmarshalJSON 72.73% (8/11) -github.com/conformal/btcws/notifications.go WalletLockStateNtfn.UnmarshalJSON 72.73% (8/11) -github.com/conformal/btcws/cmds.go ListAddressTransactionsCmd.UnmarshalJSON 72.73% (8/11) -github.com/conformal/btcws/notifications.go BlockDisconnectedNtfn.UnmarshalJSON 72.73% (8/11) -github.com/conformal/btcws/cmds.go WalletIsLockedCmd.UnmarshalJSON 72.73% (8/11) -github.com/conformal/btcws/notifications.go AccountBalanceNtfn.UnmarshalJSON 72.73% (8/11) -github.com/conformal/btcws/cmds.go GetBalancesCmd.UnmarshalJSON 72.73% (8/11) github.com/conformal/btcws/cmds.go GetAddressBalanceCmd.UnmarshalJSON 72.73% (8/11) -github.com/conformal/btcws/notifications.go BlockConnectedNtfn.UnmarshalJSON 72.73% (8/11) -github.com/conformal/btcws/cmds.go CreateEncryptedWalletCmd.UnmarshalJSON 72.73% (8/11) -github.com/conformal/btcws/cmds.go NotifySpentCmd.UnmarshalJSON 72.73% (8/11) -github.com/conformal/btcws/notifications.go TxMinedNtfn.UnmarshalJSON 72.73% (8/11) -github.com/conformal/btcws/cmds.go NotifyNewTXsCmd.UnmarshalJSON 72.73% (8/11) -github.com/conformal/btcws/cmds.go RescanCmd.UnmarshalJSON 72.73% (8/11) -github.com/conformal/btcws/notifications.go TxNtfn.UnmarshalJSON 72.73% (8/11) -github.com/conformal/btcws/cmds.go GetBestBlockCmd.UnmarshalJSON 72.73% (8/11) github.com/conformal/btcws/cmds.go GetCurrentNetCmd.UnmarshalJSON 72.73% (8/11) +github.com/conformal/btcws/cmds.go GetUnconfirmedBalanceCmd.UnmarshalJSON 72.73% (8/11) +github.com/conformal/btcws/cmds.go GetBestBlockCmd.UnmarshalJSON 72.73% (8/11) +github.com/conformal/btcws/cmds.go RescanCmd.UnmarshalJSON 72.73% (8/11) +github.com/conformal/btcws/cmds.go NotifyNewTXsCmd.UnmarshalJSON 72.73% (8/11) +github.com/conformal/btcws/cmds.go NotifySpentCmd.UnmarshalJSON 72.73% (8/11) +github.com/conformal/btcws/cmds.go CreateEncryptedWalletCmd.UnmarshalJSON 72.73% (8/11) +github.com/conformal/btcws/cmds.go GetBalancesCmd.UnmarshalJSON 72.73% (8/11) +github.com/conformal/btcws/cmds.go WalletIsLockedCmd.UnmarshalJSON 72.73% (8/11) +github.com/conformal/btcws/cmds.go ListAddressTransactionsCmd.UnmarshalJSON 72.73% (8/11) +github.com/conformal/btcws/cmds.go ListAllTransactionsCmd.UnmarshalJSON 72.73% (8/11) +github.com/conformal/btcws/cmds.go parseGetAddressBalanceCmd 72.73% (8/11) +github.com/conformal/btcws/notifications.go AccountBalanceNtfn.UnmarshalJSON 72.73% (8/11) +github.com/conformal/btcws/notifications.go BlockConnectedNtfn.UnmarshalJSON 72.73% (8/11) +github.com/conformal/btcws/notifications.go BlockDisconnectedNtfn.UnmarshalJSON 72.73% (8/11) +github.com/conformal/btcws/notifications.go TxMinedNtfn.UnmarshalJSON 72.73% (8/11) +github.com/conformal/btcws/notifications.go TxNtfn.UnmarshalJSON 72.73% (8/11) +github.com/conformal/btcws/notifications.go WalletLockStateNtfn.UnmarshalJSON 72.73% (8/11) github.com/conformal/btcws/notifications.go parseTxMinedNtfn 70.00% (14/20) github.com/conformal/btcws/cmds.go parseCreateEncryptedWalletCmd 69.23% (9/13) github.com/conformal/btcws/cmds.go parseNotifySpentCmd 66.67% (10/15) -github.com/conformal/btcws/cmds.go parseGetBalancesCmd 66.67% (2/3) github.com/conformal/btcws/cmds.go parseGetBestBlockCmd 66.67% (2/3) +github.com/conformal/btcws/cmds.go parseGetBalancesCmd 66.67% (2/3) github.com/conformal/btcws/cmds.go parseGetCurrentNetCmd 66.67% (2/3) github.com/conformal/btcws/notifications.go parseAccountBalanceNtfn 64.29% (9/14) -github.com/conformal/btcws/notifications.go parseBlockDisconnectedNtfn 63.64% (7/11) -github.com/conformal/btcws/notifications.go parseTxNtfn 63.64% (7/11) -github.com/conformal/btcws/notifications.go parseWalletLockStateNtfn 63.64% (7/11) github.com/conformal/btcws/notifications.go parseBlockConnectedNtfn 63.64% (7/11) +github.com/conformal/btcws/notifications.go parseWalletLockStateNtfn 63.64% (7/11) +github.com/conformal/btcws/notifications.go parseTxNtfn 63.64% (7/11) +github.com/conformal/btcws/notifications.go parseBlockDisconnectedNtfn 63.64% (7/11) github.com/conformal/btcws/notifications.go parseBtcdConnectedNtfn 62.50% (5/8) github.com/conformal/btcws/notifications.go BtcdConnectedNtfn.UnmarshalJSON 45.45% (5/11) -github.com/conformal/btcws ---------------------------------------- 77.51% (417/538) +github.com/conformal/btcws ---------------------------------------- 77.72% (443/570)