/
cmd_wallet_send_path_payment.go
166 lines (148 loc) · 4.6 KB
/
cmd_wallet_send_path_payment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package client
import (
"errors"
"strings"
"github.com/keybase/cli"
"github.com/adamwalz/keybase-client/go/libcmdline"
"github.com/adamwalz/keybase-client/go/libkb"
"github.com/adamwalz/keybase-client/go/protocol/stellar1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
context "golang.org/x/net/context"
)
type CmdWalletSendPathPayment struct {
libkb.Contextified
Recipient string
SourceAsset stellar1.Asset
DestinationAsset stellar1.Asset
DestinationAmount string
Note string
FromAccountID stellar1.AccountID
Memo string
MemoType stellar1.PublicNoteType
}
func newCmdWalletSendPathPayment(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
flags := []cli.Flag{
cli.StringFlag{
Name: "source-asset",
Usage: "Asset code/issuer for source asset.",
},
cli.StringFlag{
Name: "destination-asset",
Usage: "Asset code/issuer for destination asset.",
},
cli.StringFlag{
Name: "m, message",
Usage: "Include a message with the payment.",
},
cli.StringFlag{
Name: "from",
Usage: "Specify the source account for the payment.",
},
cli.StringFlag{
Name: "memo",
Usage: "Include a public memo in the stellar transaction.",
},
cli.StringFlag{
Name: "memo_type",
Usage: "Specify the type of memo (text, id, hash, return). hash and return should be hex-encoded.",
},
}
cmd := &CmdWalletSendPathPayment{
Contextified: libkb.NewContextified(g),
}
return cli.Command{
Name: "send-path-payment",
Usage: "Send a path payment to a keybase user or stellar address",
ArgumentHelp: "<recipient> <destination amount>",
Action: func(c *cli.Context) {
cl.ChooseCommand(cmd, "send-path-payment", c)
},
Flags: flags,
Examples: "keybase wallet send-path-payment alice 10.3923 --source-asset native --destination-asset USD/GDUKMGUGDZQK6YHYA5Z6AY2G4XDSZPSZ3SW5UN3ARVMO6QSRDWP5YLEX -m 'here's some anchorusd!'",
}
}
func (c *CmdWalletSendPathPayment) ParseArgv(ctx *cli.Context) error {
if len(ctx.Args()) != 2 {
return errors.New("send-path-payment requires recipient and amount")
}
c.Recipient = ctx.Args()[0]
c.DestinationAmount = ctx.Args()[1]
c.Note = ctx.String("message")
c.Memo = ctx.String("memo")
memoType := ctx.String("memo_type")
if memoType == "" {
if c.Memo != "" {
memoType = "text"
} else {
memoType = "none"
}
}
var ok bool
c.MemoType, ok = stellar1.PublicNoteTypeMap[strings.ToUpper(memoType)]
if !ok {
return errors.New("invalid memo type")
}
c.FromAccountID = stellar1.AccountID(ctx.String("from"))
var err error
c.SourceAsset, err = parseAssetString(ctx.String("source-asset"))
if err != nil {
return err
}
c.DestinationAsset, err = parseAssetString(ctx.String("destination-asset"))
if err != nil {
return err
}
return nil
}
func (c *CmdWalletSendPathPayment) Run() (err error) {
defer transformStellarCLIError(&err)
cli, err := GetWalletClient(c.G())
if err != nil {
return err
}
protocols := []rpc.Protocol{
NewIdentifyUIProtocol(c.G()),
}
if err := RegisterProtocolsWithContext(protocols, c.G()); err != nil {
return err
}
ui := c.G().UI.GetTerminalUI()
ui.Printf(ColorString(c.G(), "yellow", "Searching for payment path for %s to %s...\n", c.SourceAsset, c.DestinationAsset))
findArg := stellar1.FindPaymentPathLocalArg{
From: c.FromAccountID,
To: c.Recipient,
SourceAsset: c.SourceAsset,
DestinationAsset: c.DestinationAsset,
Amount: c.DestinationAmount,
}
path, err := cli.FindPaymentPathLocal(context.Background(), findArg)
if err != nil {
return err
}
// TODO: when SourceDisplay, SourceMaxDisplay, DestinationDisplay filled in, use those
ui.Printf("Sending approximately %s of %s (at most %s)\n", path.FullPath.SourceAmount, path.FullPath.SourceAsset, path.FullPath.SourceAmountMax)
ui.Printf("User %s will receive %s of %s\n\n", c.Recipient, path.FullPath.DestinationAmount, path.FullPath.DestinationAsset)
if err := ui.PromptForConfirmation("Proceed?"); err != nil {
return err
}
ui.Printf(ColorString(c.G(), "yellow", "Submitting transaction to the Stellar network..."))
sendArg := stellar1.SendPathCLILocalArg{
Source: c.FromAccountID,
Recipient: c.Recipient,
Path: path.FullPath,
Note: c.Note,
}
res, err := cli.SendPathCLILocal(context.Background(), sendArg)
if err != nil {
return err
}
ui.Printf("Sent!\nKeybase Transaction ID: %v\nStellar Transaction ID: %v\n", res.KbTxID, res.TxID)
return nil
}
func (c *CmdWalletSendPathPayment) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
KbKeyring: true,
}
}