@@ -2,6 +2,7 @@ package main
22
33import (
44 "encoding/hex"
5+ "errors"
56 "fmt"
67 "os"
78 "strconv"
@@ -26,6 +27,7 @@ var accountsCommands = []cli.Command{
2627 Subcommands : []cli.Command {
2728 createAccountCommand ,
2829 updateAccountCommand ,
30+ updateBalanceCommands ,
2931 listAccountsCommand ,
3032 accountInfoCommand ,
3133 removeAccountCommand ,
@@ -232,6 +234,131 @@ func updateAccount(cli *cli.Context) error {
232234 return nil
233235}
234236
237+ var updateBalanceCommands = cli.Command {
238+ Name : "updatebalance" ,
239+ ShortName : "b" ,
240+ Usage : "Update the balance of an existing off-chain account." ,
241+ Subcommands : []cli.Command {
242+ addBalanceCommand ,
243+ deductBalanceCommand ,
244+ },
245+ Description : "Updates the balance of an existing off-chain account." ,
246+ }
247+
248+ var addBalanceCommand = cli.Command {
249+ Name : "add" ,
250+ ShortName : "a" ,
251+ Usage : "Adds the given amount to an account's balance." ,
252+ ArgsUsage : "[id | label] amount" ,
253+ Description : "Adds the given amount to an existing off-chain " +
254+ "account's balance." ,
255+ Flags : []cli.Flag {
256+ cli.StringFlag {
257+ Name : idName ,
258+ Usage : "The ID of the account to add balance to." ,
259+ },
260+ cli.StringFlag {
261+ Name : labelName ,
262+ Usage : "(optional) The unique label of the account." ,
263+ },
264+ cli.Uint64Flag {
265+ Name : "amount" ,
266+ Usage : "The amount to add to the account." ,
267+ },
268+ },
269+ Action : addBalance ,
270+ }
271+
272+ func addBalance (cli * cli.Context ) error {
273+ return updateBalance (cli , true )
274+ }
275+
276+ var deductBalanceCommand = cli.Command {
277+ Name : "deduct" ,
278+ ShortName : "d" ,
279+ Usage : "Deducts the given amount from an account's balance." ,
280+ ArgsUsage : "[id | label] amount" ,
281+ Description : "Deducts the given amount from an existing off-chain " +
282+ "account's balance." ,
283+ Flags : []cli.Flag {
284+ cli.StringFlag {
285+ Name : idName ,
286+ Usage : "The ID of the account to deduct balance from." ,
287+ },
288+ cli.StringFlag {
289+ Name : labelName ,
290+ Usage : "(optional) The unique label of the account." ,
291+ },
292+ cli.Uint64Flag {
293+ Name : "amount" ,
294+ Usage : "The amount to deduct from the account." ,
295+ },
296+ },
297+ Action : deductBalance ,
298+ }
299+
300+ func deductBalance (cli * cli.Context ) error {
301+ return updateBalance (cli , false )
302+ }
303+
304+ func updateBalance (cli * cli.Context , add bool ) error {
305+ ctx := getContext ()
306+ clientConn , cleanup , err := connectClient (cli , false )
307+ if err != nil {
308+ return err
309+ }
310+ defer cleanup ()
311+ client := litrpc .NewAccountsClient (clientConn )
312+
313+ id , label , args , err := parseIDOrLabel (cli )
314+ if err != nil {
315+ return err
316+ }
317+
318+ if (! cli .IsSet ("amount" ) && len (args ) != 1 ) ||
319+ (cli .IsSet ("amount" ) && len (args ) != 0 ) {
320+
321+ return errors .New ("invalid number of arguments" )
322+ }
323+
324+ var amount int64
325+ switch {
326+ case cli .IsSet ("amount" ):
327+ amount = cli .Int64 ("amount" )
328+ case args .Present ():
329+ amount , err = strconv .ParseInt (args .First (), 10 , 64 )
330+ if err != nil {
331+ return fmt .Errorf ("unable to decode balance %v" , err )
332+ }
333+ args = args .Tail ()
334+ default :
335+ return errors .New ("must set a value for amount" )
336+ }
337+
338+ req := & litrpc.UpdateAccountBalanceRequest {
339+ Id : id ,
340+ Label : label ,
341+ }
342+
343+ if add {
344+ req .Update = & litrpc.UpdateAccountBalanceRequest_Add {
345+ Add : amount ,
346+ }
347+ } else {
348+ req .Update = & litrpc.UpdateAccountBalanceRequest_Deduct {
349+ Deduct : amount ,
350+ }
351+ }
352+
353+ resp , err := client .UpdateBalance (ctx , req )
354+ if err != nil {
355+ return err
356+ }
357+
358+ printRespJSON (resp )
359+ return nil
360+ }
361+
235362var listAccountsCommand = cli.Command {
236363 Name : "list" ,
237364 ShortName : "l" ,
0 commit comments