@@ -2,6 +2,7 @@ package main
2
2
3
3
import (
4
4
"encoding/hex"
5
+ "errors"
5
6
"fmt"
6
7
"os"
7
8
"strconv"
@@ -26,6 +27,8 @@ var accountsCommands = []cli.Command{
26
27
Subcommands : []cli.Command {
27
28
createAccountCommand ,
28
29
updateAccountCommand ,
30
+ creditCommand ,
31
+ debitCommand ,
29
32
listAccountsCommand ,
30
33
accountInfoCommand ,
31
34
removeAccountCommand ,
@@ -232,6 +235,115 @@ func updateAccount(cli *cli.Context) error {
232
235
return nil
233
236
}
234
237
238
+ var creditCommand = cli.Command {
239
+ Name : "credit" ,
240
+ Usage : "Increase an account's balance by the given amount." ,
241
+ ArgsUsage : "[id | label] amount" ,
242
+ Description : "Increases an existing off-chain account's balance by " +
243
+ "the given amount." ,
244
+ Flags : []cli.Flag {
245
+ cli.StringFlag {
246
+ Name : idName ,
247
+ Usage : "The ID of the account to credit." ,
248
+ },
249
+ cli.StringFlag {
250
+ Name : labelName ,
251
+ Usage : "(optional) The unique label of the account." ,
252
+ },
253
+ cli.Uint64Flag {
254
+ Name : "amount" ,
255
+ Usage : "The amount to credit the account." ,
256
+ },
257
+ },
258
+ Action : creditBalance ,
259
+ }
260
+
261
+ func creditBalance (cli * cli.Context ) error {
262
+ return updateBalance (cli , true )
263
+ }
264
+
265
+ var debitCommand = cli.Command {
266
+ Name : "debit" ,
267
+ Usage : "Decrease an account's balance by the given amount." ,
268
+ ArgsUsage : "[id | label] amount" ,
269
+ Description : "Decreases an existing off-chain account's balance by " +
270
+ "the given amount." ,
271
+ Flags : []cli.Flag {
272
+ cli.StringFlag {
273
+ Name : idName ,
274
+ Usage : "The ID of the account to debit." ,
275
+ },
276
+ cli.StringFlag {
277
+ Name : labelName ,
278
+ Usage : "(optional) The unique label of the account." ,
279
+ },
280
+ cli.Uint64Flag {
281
+ Name : "amount" ,
282
+ Usage : "The amount to debit the account." ,
283
+ },
284
+ },
285
+ Action : debitBalance ,
286
+ }
287
+
288
+ func debitBalance (cli * cli.Context ) error {
289
+ return updateBalance (cli , false )
290
+ }
291
+
292
+ func updateBalance (cli * cli.Context , add bool ) error {
293
+ ctx := getContext ()
294
+ clientConn , cleanup , err := connectClient (cli , false )
295
+ if err != nil {
296
+ return err
297
+ }
298
+ defer cleanup ()
299
+ client := litrpc .NewAccountsClient (clientConn )
300
+
301
+ id , label , args , err := parseIDOrLabel (cli )
302
+ if err != nil {
303
+ return err
304
+ }
305
+
306
+ if (! cli .IsSet ("amount" ) && len (args ) != 1 ) ||
307
+ (cli .IsSet ("amount" ) && len (args ) != 0 ) {
308
+
309
+ return errors .New ("invalid number of arguments" )
310
+ }
311
+
312
+ var amount uint64
313
+ switch {
314
+ case cli .IsSet ("amount" ):
315
+ amount = cli .Uint64 ("amount" )
316
+ case args .Present ():
317
+ amount , err = strconv .ParseUint (args .First (), 10 , 64 )
318
+ if err != nil {
319
+ return fmt .Errorf ("unable to decode amount %v" , err )
320
+ }
321
+ args = args .Tail ()
322
+ default :
323
+ return errors .New ("must set a value for amount" )
324
+ }
325
+
326
+ req := & litrpc.UpdateAccountBalanceRequest {
327
+ Id : id ,
328
+ Label : label ,
329
+ Amount : amount ,
330
+ }
331
+
332
+ var resp * litrpc.Account
333
+ if add {
334
+ resp , err = client .CreditAccount (ctx , req )
335
+ } else {
336
+ resp , err = client .DebitAccount (ctx , req )
337
+ }
338
+
339
+ if err != nil {
340
+ return err
341
+ }
342
+
343
+ printRespJSON (resp )
344
+ return nil
345
+ }
346
+
235
347
var listAccountsCommand = cli.Command {
236
348
Name : "list" ,
237
349
ShortName : "l" ,
0 commit comments