|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/lightninglabs/loop/looprpc" |
| 7 | + "github.com/urfave/cli" |
| 8 | +) |
| 9 | + |
| 10 | +var ( |
| 11 | + assetDepositsCommands = cli.Command{ |
| 12 | + Name: "asset deposits", |
| 13 | + ShortName: "ad", |
| 14 | + Usage: "TAP asset deposit commands.", |
| 15 | + Subcommands: []cli.Command{ |
| 16 | + newAssetDepositCommand, |
| 17 | + listAssetDepositsCommand, |
| 18 | + withdrawAssetDepositCommand, |
| 19 | + }, |
| 20 | + } |
| 21 | + |
| 22 | + newAssetDepositCommand = cli.Command{ |
| 23 | + Name: "new", |
| 24 | + ShortName: "n", |
| 25 | + Usage: "Create a new TAP asset deposit.", |
| 26 | + Description: "Create a new TAP asset deposit.", |
| 27 | + Action: newAssetDeposit, |
| 28 | + Flags: []cli.Flag{ |
| 29 | + cli.StringFlag{ |
| 30 | + Name: "asset_id", |
| 31 | + Usage: "The asset id of the asset to deposit.", |
| 32 | + }, |
| 33 | + cli.Uint64Flag{ |
| 34 | + Name: "amt", |
| 35 | + Usage: "the amount to deposit (in asset " + |
| 36 | + "units).", |
| 37 | + }, |
| 38 | + cli.UintFlag{ |
| 39 | + Name: "expiry", |
| 40 | + Usage: "the deposit expiry in blocks.", |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + listAssetDepositsCommand = cli.Command{ |
| 46 | + Name: "list", |
| 47 | + ShortName: "l", |
| 48 | + Usage: "List TAP asset deposits.", |
| 49 | + Description: "List TAP asset deposits.", |
| 50 | + Flags: []cli.Flag{ |
| 51 | + cli.UintFlag{ |
| 52 | + Name: "min_confs", |
| 53 | + Usage: "The minimum amount of confirmations " + |
| 54 | + "an anchor output should have to be " + |
| 55 | + "listed.", |
| 56 | + }, |
| 57 | + cli.UintFlag{ |
| 58 | + Name: "max_confs", |
| 59 | + Usage: "The maximum number of confirmations " + |
| 60 | + "an anchor output could have to be " + |
| 61 | + "listed.", |
| 62 | + }, |
| 63 | + }, |
| 64 | + Action: listAssetDeposits, |
| 65 | + } |
| 66 | + |
| 67 | + withdrawAssetDepositCommand = cli.Command{ |
| 68 | + Name: "withdraw", |
| 69 | + ShortName: "w", |
| 70 | + Usage: "Withdraw TAP asset deposits.", |
| 71 | + Description: "Withdraw TAP asset deposits.", |
| 72 | + Action: withdrawAssetDeposit, |
| 73 | + Flags: []cli.Flag{ |
| 74 | + cli.StringSliceFlag{ |
| 75 | + Name: "deposit_ids", |
| 76 | + Usage: "The deposit ids of the asset " + |
| 77 | + "deposits to withdraw.", |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | +) |
| 82 | + |
| 83 | +func init() { |
| 84 | + commands = append(commands, assetDepositsCommands) |
| 85 | +} |
| 86 | + |
| 87 | +func newAssetDeposit(ctx *cli.Context) error { |
| 88 | + ctxb := context.Background() |
| 89 | + if ctx.NArg() > 0 { |
| 90 | + return cli.ShowCommandHelp(ctx, "newdeposit") |
| 91 | + } |
| 92 | + |
| 93 | + client, cleanup, err := getAssetDepositsClient(ctx) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + defer cleanup() |
| 98 | + |
| 99 | + assetID := ctx.String("asset_id") |
| 100 | + amt := ctx.Uint64("amt") |
| 101 | + expiry := int32(ctx.Uint("expiry")) |
| 102 | + |
| 103 | + resp, err := client.NewAssetDeposit( |
| 104 | + ctxb, &looprpc.NewAssetDepositRequest{ |
| 105 | + AssetId: assetID, |
| 106 | + Amount: amt, |
| 107 | + CsvExpiry: expiry, |
| 108 | + }, |
| 109 | + ) |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + printJSON(resp) |
| 115 | + |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +func listAssetDeposits(ctx *cli.Context) error { |
| 120 | + ctxb := context.Background() |
| 121 | + if ctx.NArg() > 0 { |
| 122 | + return cli.ShowCommandHelp(ctx, "list") |
| 123 | + } |
| 124 | + |
| 125 | + client, cleanup, err := getAssetDepositsClient(ctx) |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + defer cleanup() |
| 130 | + |
| 131 | + resp, err := client.ListAssetDeposits( |
| 132 | + ctxb, &looprpc.ListAssetDepositsRequest{ |
| 133 | + MinConfs: uint32(ctx.Int("min_confs")), |
| 134 | + MaxConfs: uint32(ctx.Int("max_confs")), |
| 135 | + }) |
| 136 | + if err != nil { |
| 137 | + return err |
| 138 | + } |
| 139 | + |
| 140 | + printRespJSON(resp) |
| 141 | + |
| 142 | + return nil |
| 143 | +} |
| 144 | + |
| 145 | +func withdrawAssetDeposit(ctx *cli.Context) error { |
| 146 | + ctxb := context.Background() |
| 147 | + if ctx.NArg() > 0 { |
| 148 | + return cli.ShowCommandHelp(ctx, "withdraw") |
| 149 | + } |
| 150 | + |
| 151 | + client, cleanup, err := getAssetDepositsClient(ctx) |
| 152 | + if err != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + defer cleanup() |
| 156 | + |
| 157 | + depositIDs := ctx.StringSlice("deposit_ids") |
| 158 | + |
| 159 | + resp, err := client.WithdrawAssetDeposits( |
| 160 | + ctxb, &looprpc.WithdrawAssetDepositsRequest{ |
| 161 | + DepositIds: depositIDs, |
| 162 | + }, |
| 163 | + ) |
| 164 | + if err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + |
| 168 | + printRespJSON(resp) |
| 169 | + |
| 170 | + return nil |
| 171 | +} |
0 commit comments