-
-
Notifications
You must be signed in to change notification settings - Fork 274
/
main.go
125 lines (101 loc) · 2.76 KB
/
main.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
package main
import (
"context"
"log"
"os"
"time"
maxapi "github.com/c9s/bbgo/pkg/exchange/max/maxapi"
flag "github.com/spf13/pflag"
)
func waitWithdrawalsComplete(ctx context.Context, client *maxapi.RestClient, currency string, limit int) error {
var lastState string
for {
withdrawals, err := client.WithdrawalService.NewGetWithdrawalHistoryRequest().
Currency(currency).
Limit(limit).
Do(ctx)
if err != nil {
return err
}
pending := false
for _, withdrawal := range withdrawals {
if lastState == "" {
log.Printf("-> %s", withdrawal.State)
} else if withdrawal.State != lastState {
log.Printf("%s -> %s", lastState, withdrawal.State)
log.Printf("withdrawal %s %s: %s", withdrawal.Amount, withdrawal.Currency, withdrawal.State)
log.Printf("\t%+v", withdrawal)
}
lastState = withdrawal.State
switch withdrawal.State {
case "submitting", "submitted", "pending", "processing", "approved":
pending = true
log.Printf("there is a pending withdrawal request, waiting...")
break
case "sent", "confirmed":
continue
case "rejected":
}
}
if !pending {
break
}
time.Sleep(10 * time.Second)
}
return nil
}
func main() {
var do bool
var currency string
var targetAddress string
var amount float64
flag.StringVar(¤cy, "currency", "", "currency")
flag.StringVar(&targetAddress, "targetAddress", "", "withdraw target address")
flag.Float64Var(&amount, "amount", 0, "transfer amount")
flag.BoolVar(&do, "do", false, "do")
flag.Parse()
key := os.Getenv("MAX_API_KEY")
secret := os.Getenv("MAX_API_SECRET")
if currency == "" {
log.Fatal("--targetAddress is required")
}
if targetAddress == "" {
log.Fatal("--targetAddress is required")
}
if amount == 0.0 {
log.Fatal("--amount can not be zero")
}
ctx := context.Background()
maxRest := maxapi.NewRestClient(maxapi.ProductionAPIURL)
maxRest.Auth(key, secret)
if err := waitWithdrawalsComplete(ctx, maxRest, currency, 1); err != nil {
log.Fatal(err)
}
log.Printf("all withdrawals are sent, sending new withdrawal request...")
addresses, err := maxRest.WithdrawalService.NewGetWithdrawalAddressesRequest().
Currency(currency).Do(ctx)
if err != nil {
log.Fatal(err)
}
for _, address := range addresses {
if address.Address == targetAddress {
log.Printf("found address: %+v", address)
if do {
response, err := maxRest.WithdrawalService.NewWithdrawalRequest().
Currency(currency).
Amount(amount).
AddressUUID(address.UUID).
Do(ctx)
if err != nil {
log.Fatal(err)
}
log.Printf("withdrawal request response: %+v", response)
break
}
}
}
if err := waitWithdrawalsComplete(ctx, maxRest, currency, 1); err != nil {
log.Fatal(err)
}
log.Printf("all withdrawals are sent")
}