-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
86 lines (69 loc) · 1.6 KB
/
helpers.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
package main
import (
"encoding/json"
"errors"
"fmt"
"log"
swissknife "github.com/Sagleft/swiss-knife"
"github.com/fatih/color"
"github.com/shopspring/decimal"
)
func (b *bot) parseConfig() error {
if err := swissknife.ParseStructFromJSONFile(configPath, &b.Config); err != nil {
return err
}
debugMode = b.Config.IsDebug
debug("parse config")
success("config parsed")
return nil
}
func (b *bot) verifyConfig() error {
debug("verify config..")
if b.Config.IntervalDepositMaxPercent == 0 {
return errors.New("invalid `intervalDepositMaxPercent`: value must be set")
}
success("config verified")
return nil
}
func (o order) ToString() string {
jsonBytes, err := json.Marshal(o)
if err != nil {
return "{}"
}
return string(jsonBytes)
}
func (b *bot) isStrategyBuy() bool {
return b.Config.Strategy == botStrategyBuy
}
func (b *bot) isFirstInterval() bool {
return b.Lap.IntervalNumber == 0
}
func (b *bot) setLastPriceLevel(price float64) {
b.Lap.LastPriceLevel = price
}
func success(info string, a ...interface{}) {
if info == "" {
return
}
color.Green(" [ "+info+" ]\n", a...)
}
func roundFloatFloor(val float64, precision int) float64 {
f, _ := decimal.NewFromFloat(val).RoundFloor(int32(precision)).Float64()
return f
}
func warn(info string, a ...interface{}) {
color.Yellow("[WARN] "+info, a...)
}
func toJSON(v any) string {
dataBytes, err := json.Marshal(v)
if err != nil {
return fmt.Sprintf(`{"error": "failed to json convert: %s"}`, err.Error())
}
return string(dataBytes)
}
func debug(info string, a ...any) {
if !debugMode {
return
}
log.Printf(info+"\n", a...)
}