Skip to content

Commit e7cab9b

Browse files
committed
don't merge yet
1 parent 4e48f3e commit e7cab9b

2 files changed

Lines changed: 212 additions & 9 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
"sync"
8+
"time"
9+
10+
"github.com/keybase/go-keybase-chat-bot/kbchat"
11+
)
12+
13+
func fail(msg string, args ...interface{}) {
14+
fmt.Fprintf(os.Stderr, msg+"\n", args...)
15+
os.Exit(3)
16+
}
17+
18+
func runningtime(s string) (string, time.Time) {
19+
fmt.Println("Start: ", s)
20+
return s, time.Now()
21+
}
22+
23+
func track(s string, startTime time.Time) {
24+
endTime := time.Now()
25+
fmt.Println("End: ", s, "took", endTime.Sub(startTime))
26+
}
27+
28+
func main() {
29+
var kbLoc string
30+
var kbc *kbchat.API
31+
var err error
32+
33+
flag.StringVar(&kbLoc, "keybase", "keybase", "the location of the Keybase app")
34+
flag.Parse()
35+
36+
if kbc, err = kbchat.Start(kbchat.RunOptions{KeybaseLocation: kbLoc}); err != nil {
37+
fail("Error creating API: %s", err.Error())
38+
}
39+
// send
40+
c := 200
41+
to := "TESTACCOUNT" ///
42+
txids := make([]string, 0, c)
43+
fmt.Printf("....start sending....\n")
44+
payingLabel, payingTime := runningtime("timepaying")
45+
var wg sync.WaitGroup
46+
for i := 0; i < c; i++ {
47+
wg.Add(1)
48+
go func(i int) {
49+
defer wg.Done()
50+
res, err := kbc.SendWalletTx(to, "3", nil, nil, nil, nil)
51+
if err != nil {
52+
fmt.Printf("--ERROR: i=%d, err=%+v\n", i, err.Error())
53+
} else {
54+
fmt.Printf("SUCCESS: %d, %+v\n", i, res.TxID)
55+
txids = append(txids, string(res.TxID))
56+
}
57+
}(i)
58+
}
59+
wg.Wait()
60+
track(payingLabel, payingTime)
61+
fmt.Printf("....finished sending.... len=%d\n\n", len(txids))
62+
63+
// cancel
64+
successCount := 0
65+
fmt.Printf("....start canceling....\n")
66+
cancelingLabel, cancelingTime := runningtime("timecanceling")
67+
for i, txID := range txids {
68+
wg.Add(1)
69+
go func(i int, txID string) {
70+
defer wg.Done()
71+
res, err := kbc.CancelWalletTx(txID)
72+
if err != nil {
73+
fmt.Printf("--ERROR: i=%d, err=%+v\n", i, err.Error())
74+
} else {
75+
fmt.Printf("SUCCESS: %d, %+v, %+v\n", i, txID, res.ClaimStellarID)
76+
successCount++
77+
}
78+
}(i, txID)
79+
}
80+
wg.Wait()
81+
track(cancelingLabel, cancelingTime)
82+
fmt.Printf("....finished sending.... successCount=%d\n\n", successCount)
83+
}

kbchat/wallet.go

Lines changed: 129 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,147 @@ import (
99
"github.com/keybase/go-keybase-chat-bot/kbchat/types/stellar1"
1010
)
1111

12-
type WalletOutput struct {
13-
Result stellar1.PaymentCLILocal `json:"result"`
12+
type walletMethod string
13+
14+
type sendOptions struct {
15+
Recipient string `json:"recipient"`
16+
Amount string `json:"amount"`
17+
Currency *string `json:"currency"`
18+
Message *string `json:"message"`
19+
FromAccountID *string `json:"from-account-id"`
20+
MemoText *string `json:"memo-text"`
21+
}
22+
23+
type SendOutput struct {
24+
Result stellar1.SendResultCLILocal `json:"result"`
25+
}
26+
27+
type CancelOutput struct {
28+
Result stellar1.RelayClaimResult `json:"result"`
29+
}
30+
31+
type walletTxIDAPIReq struct {
32+
Method walletMethod `json:"method"`
33+
Params walletTxIDParams `json:"params"`
34+
}
35+
36+
type walletTxIDParams struct {
37+
Options txIDOptions `json:"options"`
38+
}
39+
40+
type txIDOptions struct {
41+
TxID string `json:"txid"`
42+
}
43+
44+
type walletSendAPIReq struct {
45+
Method walletMethod `json:"method"`
46+
Params walletSendParams `json:"params"`
47+
}
48+
49+
type walletSendParams struct {
50+
Options sendOptions `json:"options"`
51+
}
52+
53+
func (a *API) GetWalletTxDetails(txID string) (result stellar1.PaymentCLILocal, err error) {
54+
a.Lock()
55+
defer a.Unlock()
56+
57+
type res struct {
58+
Result stellar1.PaymentCLILocal `json:"result"`
59+
}
60+
61+
opts := txIDOptions{
62+
TxID: txID,
63+
}
64+
args := walletTxIDAPIReq{Method: "details", Params: walletTxIDParams{Options: opts}}
65+
apiInput, err := json.Marshal(args)
66+
if err != nil {
67+
return result, err
68+
}
69+
cmd := a.runOpts.Command("wallet", "api")
70+
cmd.Stdin = strings.NewReader(string(apiInput))
71+
var out bytes.Buffer
72+
cmd.Stdout = &out
73+
err = cmd.Run()
74+
if err != nil {
75+
return result, err
76+
}
77+
78+
response := res{}
79+
if err := json.Unmarshal(out.Bytes(), &response); err != nil {
80+
return result, fmt.Errorf("unable to decode wallet output: %s", err.Error())
81+
}
82+
83+
return response.Result, nil
1484
}
1585

16-
func (a *API) GetWalletTxDetails(txID string) (wOut WalletOutput, err error) {
86+
func (a *API) SendWalletTx(recipient string, amount string, currency *string, message *string, fromAccountID *string, memoText *string) (result stellar1.SendResultCLILocal, err error) {
1787
a.Lock()
1888
defer a.Unlock()
1989

20-
apiInput := fmt.Sprintf(`{"method": "details", "params": {"options": {"txid": "%s"}}}`, txID)
90+
type res struct {
91+
Result stellar1.SendResultCLILocal `json:"result"`
92+
}
93+
94+
opts := sendOptions{
95+
Recipient: recipient,
96+
Amount: amount,
97+
Currency: currency,
98+
Message: message,
99+
FromAccountID: fromAccountID,
100+
MemoText: memoText,
101+
}
102+
args := walletSendAPIReq{Method: "send", Params: walletSendParams{Options: opts}}
103+
apiInput, err := json.Marshal(args)
104+
if err != nil {
105+
return result, err
106+
}
107+
cmd := a.runOpts.Command("wallet", "api")
108+
cmd.Stdin = strings.NewReader(string(apiInput))
109+
var out bytes.Buffer
110+
cmd.Stdout = &out
111+
err = cmd.Run()
112+
if err != nil {
113+
return result, err
114+
}
115+
116+
response := res{}
117+
if err := json.Unmarshal(out.Bytes(), &response); err != nil {
118+
return result, fmt.Errorf("unable to decode wallet output: %s", err.Error())
119+
}
120+
121+
return response.Result, nil
122+
}
123+
124+
func (a *API) CancelWalletTx(txID string) (result stellar1.RelayClaimResult, err error) {
125+
a.Lock()
126+
defer a.Unlock()
127+
128+
type res struct {
129+
Result stellar1.RelayClaimResult `json:"result"`
130+
}
131+
132+
opts := txIDOptions{
133+
TxID: txID,
134+
}
135+
args := walletTxIDAPIReq{Method: "cancel", Params: walletTxIDParams{Options: opts}}
136+
apiInput, err := json.Marshal(args)
137+
if err != nil {
138+
return result, err
139+
}
21140
cmd := a.runOpts.Command("wallet", "api")
22-
cmd.Stdin = strings.NewReader(apiInput)
141+
cmd.Stdin = strings.NewReader(string(apiInput))
23142
var out bytes.Buffer
24143
cmd.Stdout = &out
25144
err = cmd.Run()
26145
if err != nil {
27-
return wOut, err
146+
return result, err
28147
}
29148

30-
if err := json.Unmarshal(out.Bytes(), &wOut); err != nil {
31-
return wOut, fmt.Errorf("unable to decode wallet output: %s", err.Error())
149+
response := res{}
150+
if err := json.Unmarshal(out.Bytes(), &response); err != nil {
151+
return result, fmt.Errorf("unable to decode wallet output: %s", err.Error())
32152
}
33153

34-
return wOut, nil
154+
return response.Result, nil
35155
}

0 commit comments

Comments
 (0)