forked from eAndrius/BitfinexLendingBot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
94 lines (77 loc) · 2.2 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
// Copyright Andrius Sutas BitfinexLendingBot [at] motoko [dot] sutas [dot] eu
package main
import (
"encoding/json"
"flag"
"log"
"os"
"strconv"
"strings"
"github.com/eAndrius/bitfinex-go"
)
var (
configFile = flag.String("conf", "default.conf", "Configuration file")
updateLends = flag.Bool("updatelends", false, "Update lend offerings")
dryRun = flag.Bool("dryrun", false, "Output strategy decisions without placing orders")
logToFile = flag.Bool("logtofile", false, "Write log to file instead of stdout")
)
// BotConfig ...
type BotConfig struct {
Bitfinex BitfinexConf
Strategy StrategyConf
API *bitfinex.API
}
// BotConfigs ...
type BotConfigs []BotConfig
// BitfinexConf ...
type BitfinexConf struct {
APIKey string
APISecret string
ActiveWallet string
MaxActiveAmount float64
MinLoanUSD float64
}
func init() {
flag.Parse()
if *logToFile {
f, err := os.OpenFile("blb.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic("error opening file: " + err.Error())
}
log.SetOutput(f)
}
}
func main() {
file, err := os.Open(*configFile)
if err != nil {
log.Fatal("Failed to open config file: " + err.Error())
}
decoder := json.NewDecoder(file)
confs := BotConfigs{}
err = decoder.Decode(&confs)
if err != nil {
log.Fatal("Failed to parse config file:" + err.Error())
}
for _, conf := range confs {
log.Println("Using Bitfinex user API key: " + conf.Bitfinex.APIKey)
conf.API = bitfinex.New(conf.Bitfinex.APIKey, conf.Bitfinex.APISecret)
balance, err := conf.API.WalletBalances()
if err != nil {
log.Println("WARNING: Failed to get wallet funds, skipping: " + err.Error())
continue
}
activeWallet := strings.ToLower(conf.Bitfinex.ActiveWallet)
log.Println("\tDeposit wallet: " +
strconv.FormatFloat(balance[bitfinex.WalletKey{"deposit", activeWallet}].Amount, 'f', -1, 64) +
" " + activeWallet + " (swappable: " +
strconv.FormatFloat(balance[bitfinex.WalletKey{"deposit", activeWallet}].Available, 'f', -1, 64) +
" " + activeWallet + ")")
if *updateLends {
err = executeStrategy(conf, *dryRun)
if err != nil {
log.Println("WARNING: Failed to execute strategy: " + err.Error())
continue
}
}
}
}