-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
367 lines (331 loc) · 8.85 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"sats-stacker/exchange"
"sats-stacker/notifier"
)
// Global Variables
var log = logrus.New()
var ex exchange.Exchange
var nf notifier.Notifier
// TODO Use a struct for result and set action inside of it rather than use 2 string variables
var result string
var action string
// Set version at compile time
var Version string
func init() {
// Setup Logging
log.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
log.SetOutput(os.Stderr)
log.SetLevel(logrus.InfoLevel)
// Set Version if not passed by compiler
if len(Version) == 0 {
out, err := exec.Command("git", "rev-parse", "--short", "HEAD").Output()
if err != nil {
Version = "master"
} else {
Version = string(out)
}
}
}
func main() {
usage := `
a cli-tool to stack sats on exchanges.
`
flags := []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"d"},
Value: false,
Usage: "debug logging",
EnvVars: []string{"STACKER_DEBUG"},
},
&cli.BoolFlag{
Name: "dry-run",
Aliases: []string{"validate"},
Value: true,
Usage: "dry-run",
EnvVars: []string{"STACKER_VALIDATE", "STACKER_DRY_RUN"},
},
&cli.StringFlag{
Name: "exchange",
Usage: "Exchange ['kraken', 'binance']",
EnvVars: []string{"STACKER_EXCHANGE"},
Value: "kraken",
},
&cli.StringFlag{
Name: "api-key",
Usage: "Exchange Api Key",
EnvVars: []string{"STACKER_API_KEY"},
Required: true,
},
&cli.StringFlag{
Name: "secret-key",
Usage: "Exchange Api Secret",
EnvVars: []string{"STACKER_SECRET_KEY", "STACKER_API_SECRET"},
Required: true,
},
}
notifierFlags := []cli.Flag{
&cli.StringFlag{
Name: "notifier",
Usage: "What notifier to use ['stdout','simplepush']",
Value: "stdout",
EnvVars: []string{"STACKER_NOTIFIER"},
},
&cli.BoolFlag{
Name: "sp-encrypt",
Value: true,
Usage: "Simplepush: If set, the message will be sent end-to-end encrypted with the provided Password/Salt. If false, the message is sent unencrypted.",
EnvVars: []string{"STACKER_SP_ENCRYPT"},
},
&cli.StringFlag{
Name: "sp-key",
Usage: "Simplepush: Your simplepush.io Key",
Value: "",
EnvVars: []string{"STACKER_SP_KEY"},
},
&cli.StringFlag{
Name: "sp-event",
Usage: "Simplepush: The event the message should be associated with",
Value: "",
EnvVars: []string{"STACKER_SP_EVENT"},
},
&cli.StringFlag{
Name: "sp-password",
Usage: "Simplepush: Encryption Password",
Value: "",
EnvVars: []string{"STACKER_SP_PASSWORD"},
},
&cli.StringFlag{
Name: "sp-salt",
Usage: "Simplepush: The salt for the encrypted message",
Value: "",
EnvVars: []string{"STACKER_SP_SALT"},
},
}
stackCommand := cli.Command{
Name: "stack",
Usage: "Stack some sats",
Description: "Stack some sats at market value, best used for DCA (Dollar Cost Averaging)",
Flags: []cli.Flag{
&cli.Float64Flag{
Name: "amount",
Usage: "Amount of fiat to exchange",
EnvVars: []string{"STACKER_STACK_AMOUNT"},
Required: true,
},
&cli.StringFlag{
Name: "fiat",
Usage: "Fiat to exchange",
EnvVars: []string{"STACKER_STACK_FIAT"},
Required: true,
},
},
Action: func(c *cli.Context) error {
action = "stack"
var err error
// Initialize the exchange plugin
err = ex.Init(c)
if err != nil {
return cli.Exit(err, 1)
}
// Stack sats on the exchange selected
result, err = ex.Stack(c)
if err != nil {
return cli.Exit(err, 1)
}
return nil
},
}
buyTheDipsCommand := cli.Command{
Name: "btd",
Usage: "Buy the DIPs",
Description: `Places a series of orders to buy the DIPs at different discounted prices
`,
Flags: []cli.Flag{
&cli.Float64Flag{
Name: "budget",
Usage: "Budget to allocate for the DIPs, set to 0 to allocate all of the available budget",
EnvVars: []string{"STACKER_BTD_BUDGET"},
Required: true,
},
&cli.Int64Flag{
Name: "dip-percentage",
Value: 10,
Usage: "Initial percentage of the firt dip, the other values will be calculated",
EnvVars: []string{"STACKER_BTD_DIP_PERCENTAGE"},
},
&cli.Int64Flag{
Name: "dip-increments",
Value: 5,
Usage: "Increment of dip percentage for each order",
EnvVars: []string{"STACKER_BTD_DIP_INCREMENTS_PERCENTAGE"},
},
&cli.Int64Flag{
Name: "n-orders",
Value: 5,
Usage: "Number of DIPS orders to place",
EnvVars: []string{"STACKER_BTD_DIP_N_ORDERS"},
},
&cli.Int64Flag{
Name: "high-price-days-modifier",
Value: 7,
Hidden: true,
Usage: "Days behind to use to detect high-price, used to calculate a modifier to the discount percentage, the higher the gap from the high price the bigger the modifier will be.",
EnvVars: []string{"STACKER_BTD_HIGH_PRICE_DAYS"},
},
&cli.Int64Flag{
Name: "high-price-gap-percentage",
Value: 5,
Usage: "Gap between current price and high price to trigger modifier",
EnvVars: []string{"STACKER_BTD_HIGH_PRICE_GAP_PERCENTAGE"},
},
&cli.StringFlag{
Name: "fiat",
Usage: "Fiat to exchange",
EnvVars: []string{"STACKER_BTD_FIAT"},
Required: true,
},
},
Action: func(c *cli.Context) error {
action = "btd"
var err error
// Initialize the exchange plugin
err = ex.Init(c)
if err != nil {
return cli.Exit(err, 1)
}
// Place orders to try and buy DIPS on the exchange selected
result, err = ex.BuyTheDips(c)
if err != nil {
return cli.Exit(err, 1)
}
return nil
},
}
withdrawCommand := cli.Command{
Name: "withdraw",
Usage: "Withdraw some sats",
Description: "Withdraw some sats from the exchange.",
Flags: []cli.Flag{
&cli.Float64Flag{
Name: "max-fee",
Usage: "Max fee in percentage, only withdraw if the relative fee does not exceed this limit",
EnvVars: []string{"STACKER_WITHDRAW_MAX_FEE"},
Required: true,
},
&cli.StringFlag{
Name: "address",
Usage: "Address to withdraw to, the actual value will depend on the exchange selected",
EnvVars: []string{"STACKER_WITHDRAW_ADDRESS"},
Required: true,
},
},
Action: func(c *cli.Context) error {
action = "withdraw"
var err error
// Initialize the exchange plugin
err = ex.Init(c)
if err != nil {
return cli.Exit(err, 1)
}
// Withdraw Funds from the exchange selected
result, err = ex.Withdraw(c)
if err != nil {
return cli.Exit(err, 1)
}
return nil
},
}
// Group all commands together
allCommands := []*cli.Command{
&stackCommand,
&buyTheDipsCommand,
&withdrawCommand,
}
// Initialize the cli app
app := &cli.App{
Name: "sats-stacker",
Version: Version,
Compiled: time.Now(),
Authors: []*cli.Author{
&cli.Author{
Name: "Francesco Ciocchetti",
Email: "primeroznl@gmail.com",
},
},
Copyright: "GPL",
HelpName: "SATs Stacker",
Usage: "stack and withdraw sats",
UsageText: usage,
Flags: append(flags, notifierFlags...),
Commands: allCommands,
Before: func(c *cli.Context) error {
if c.Bool("debug") {
log.SetLevel(logrus.DebugLevel)
}
// Load Exchange Plugin
switch c.String("exchange") {
case "kraken":
exchange.UseLogger(log, "kraken")
ex = &exchange.Kraken{}
case "binance":
return cli.Exit("Binance Exchange not implemented yet", 4)
default:
return cli.Exit("Only supported exchange are ['kraken', 'binance']", 1)
}
// Configure the exchange plugin
err := ex.Config(c)
if err != nil {
return cli.Exit(fmt.Sprintf("Error Configuring the Exchange Plugin: %s", err), 1)
}
// Load Notification Plugin
switch c.String("notifier") {
case "simplepush":
notifier.UseLogger(log, "simplepush")
nf = ¬ifier.SimplePush{}
case "stdout":
notifier.UseLogger(log, "stdout")
nf = ¬ifier.Stdout{}
default:
return cli.Exit("Only supported notifiers are ['stdout', 'simplepush']", 1)
}
// Configure the notification plugin
err = nf.Config(c)
if err != nil {
return cli.Exit(fmt.Sprintf("Error Configuring the Notification Plugin: %s", err), 1)
}
return nil
},
After: func(c *cli.Context) error {
// Handle notification at the end of the CLI app run
title := fmt.Sprintf("%s - %s Sats",
strings.Title(c.String("exchange")),
strings.Title(action),
)
// Do not notify if result is not set ( for example if the required args where not specified )
// TODO Add support for notification on errors
if result != "" {
err := nf.Notify(title, result)
if err != nil {
return cli.Exit(fmt.Sprintf("Notification Error: %s", err), 1)
}
}
return nil
},
}
// Run the cli App
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}