Skip to content

Commit 9818216

Browse files
committed
litcli: add accounts credit & debit commands
This commit implements the `credit` and `debit` commands in `litcli`, allowing users to increase or decrease the balance of an existing off-chain account, respectively.
1 parent 4554b33 commit 9818216

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

cmd/litcli/accounts.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"encoding/hex"
5+
"errors"
56
"fmt"
67
"os"
78
"strconv"
@@ -26,6 +27,8 @@ var accountsCommands = []cli.Command{
2627
Subcommands: []cli.Command{
2728
createAccountCommand,
2829
updateAccountCommand,
30+
creditCommand,
31+
debitCommand,
2932
listAccountsCommand,
3033
accountInfoCommand,
3134
removeAccountCommand,
@@ -232,6 +235,130 @@ func updateAccount(cli *cli.Context) error {
232235
return nil
233236
}
234237

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+
account := &litrpc.AccountIdentifier{
327+
Id: id,
328+
Label: label,
329+
}
330+
331+
var respAccount *litrpc.Account
332+
if add {
333+
req := &litrpc.CreditAccountRequest{
334+
Account: account,
335+
Amount: amount,
336+
}
337+
338+
resp, err := client.CreditAccount(ctx, req)
339+
if err != nil {
340+
return err
341+
}
342+
343+
respAccount = resp.Account
344+
} else {
345+
req := &litrpc.DebitAccountRequest{
346+
Account: account,
347+
Amount: amount,
348+
}
349+
350+
resp, err := client.DebitAccount(ctx, req)
351+
if err != nil {
352+
return err
353+
}
354+
355+
respAccount = resp.Account
356+
}
357+
358+
printRespJSON(respAccount)
359+
return nil
360+
}
361+
235362
var listAccountsCommand = cli.Command{
236363
Name: "list",
237364
ShortName: "l",

0 commit comments

Comments
 (0)