/
coin.go
110 lines (93 loc) · 2.95 KB
/
coin.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
package repository
import (
"fmt"
"time"
"go.uber.org/zap"
"gorm.io/gorm"
"github.com/erwanlbp/trading-bot/pkg/model"
)
func (r *Repository) DisableCoin(coin string) error {
return r.DB.DB.Table(model.CoinTableName).Where("coin = ?", coin).Update("enabled", 0).Error
}
func (r *Repository) GetCurrentCoin() (model.CurrentCoin, bool, error) {
var res model.CurrentCoin
err := r.DB.Order("timestamp desc").Limit(1).Find(&res).Error
if err != nil {
return res, false, err
}
if res.Coin != "" {
return res, true, nil
}
// Default case, get start coin from config
if r.ConfigFile.StartCoin != nil {
return model.CurrentCoin{
Coin: *r.ConfigFile.StartCoin,
}, true, nil
}
// If never jumped, leave the coin choice to the algo
return res, false, nil
}
func (r *Repository) SetCurrentCoin(coin string, ts time.Time) (model.CurrentCoin, error) {
currentCoin := model.CurrentCoin{
Coin: coin,
Timestamp: ts,
}
if err := SimpleUpsert(r.DB.DB, currentCoin); err != nil {
return currentCoin, fmt.Errorf("failed to save: %w", err)
}
return currentCoin, nil
}
func (r *Repository) LogCurrentCoin() {
cc, hasEverJumped, err := r.GetCurrentCoin()
if err != nil {
r.Logger.Error("Failed to get current coin", zap.Error(err))
return
}
if cc.Coin != "" {
r.Logger.Info("Current coin is " + cc.Coin)
return
}
if !hasEverJumped {
r.Logger.Info("No current because never jumped")
return
}
r.Logger.Warn("No current but jumped before ? that's weird")
}
func (r *Repository) GetAllCoins() ([]model.Coin, error) {
var res []model.Coin
err := r.DB.Find(&res).Error
return res, err
}
func (r *Repository) GetEnabledCoins() ([]string, error) {
var res []string
err := r.DB.
Select("coin").
Table(model.CoinTableName).
Where("enabled = ?", true).
Find(&res).Error
return res, err
}
func (r *Repository) DeleteAllCoins(tx *gorm.DB) error {
return tx.Exec("DELETE FROM " + model.CoinTableName).Error
}
func (r *Repository) CleanCoinPriceHistory() (inserted int64, deleted int64, err error) {
if err := r.DB.Transaction(func(tx *gorm.DB) error {
resInsert := r.DB.DB.Exec(`
INSERT OR REPLACE INTO ` + model.CoinPriceTableName + ` (coin, alt_coin, timestamp, price, averaged)
SELECT coin, alt_coin, strftime('%Y-%m-%d %H:00:00',timestamp) AS "timestamp" , avg(price) AS price , 1 FROM ` + model.CoinPriceTableName + ` cph WHERE (cph.averaged IS NULL OR cph.averaged = 0) AND timestamp < strftime('%Y-%m-%d 00:00:00','now')
GROUP BY 1,2,3`)
if resInsert.Error != nil {
return fmt.Errorf("failed to insert aggregated data: %w", err)
}
inserted = resInsert.RowsAffected
resDelete := r.DB.DB.Exec(`DELETE FROM ` + model.CoinPriceTableName + ` WHERE (averaged IS NULL OR averaged = 0) AND timestamp < strftime('%Y-%m-%d 00:00:00','now')`)
if resDelete.Error != nil {
return fmt.Errorf("failed to delete old data after aggregation: %w", err)
}
deleted = resDelete.RowsAffected
return nil
}); err != nil {
return 0, 0, err
}
return
}