forked from stellar-deprecated/kelp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fillDBWriter.go
225 lines (192 loc) · 6.83 KB
/
fillDBWriter.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
package plugins
import (
"crypto/sha256"
"database/sql"
"fmt"
"log"
"strings"
"time"
_ "github.com/lib/pq"
"github.com/stellar/kelp/api"
"github.com/stellar/kelp/kelpdb"
"github.com/stellar/kelp/model"
"github.com/stellar/kelp/support/postgresdb"
"github.com/stellar/kelp/support/utils"
)
const marketIdHashLength = 10
type tradingMarket struct {
ID string
ExchangeName string
BaseAsset string
QuoteAsset string
}
func (t *tradingMarket) equals(other tradingMarket) bool {
if t.ExchangeName != other.ExchangeName {
return false
} else if t.BaseAsset != other.BaseAsset {
return false
} else if t.QuoteAsset != other.QuoteAsset {
return false
}
return true
}
// FillDBWriter is a FillHandler that writes fills to a SQL database
type FillDBWriter struct {
db *sql.DB
assetDisplayFn model.AssetDisplayFn
exchangeName string
accountID string
// uninitialized
market *tradingMarket
}
// MakeMarketID generates a universal marketID
func MakeMarketID(exchangeName string, baseAsset string, quoteAsset string) string {
idString := fmt.Sprintf("%s_%s_%s", exchangeName, baseAsset, quoteAsset)
h := sha256.New()
h.Write([]byte(idString))
sha256Hash := fmt.Sprintf("%x", h.Sum(nil))
return sha256Hash[0:marketIdHashLength]
}
// makeTradingMarket makes a market along with the ID field
func makeTradingMarket(exchangeName string, baseAsset string, quoteAsset string) *tradingMarket {
sha256HashPrefix := MakeMarketID(exchangeName, baseAsset, quoteAsset)
return &tradingMarket{
ID: sha256HashPrefix,
ExchangeName: exchangeName,
BaseAsset: baseAsset,
QuoteAsset: quoteAsset,
}
}
// String is the Stringer method
func (m *tradingMarket) String() string {
return fmt.Sprintf("tradingMarket[ID=%s, ExchangeName=%s, BaseAsset=%s, QuoteAsset=%s]", m.ID, m.ExchangeName, m.BaseAsset, m.QuoteAsset)
}
var _ api.FillHandler = &FillDBWriter{}
// MakeFillDBWriter is a factory method
func MakeFillDBWriter(db *sql.DB, assetDisplayFn model.AssetDisplayFn, exchangeName string, accountID string) api.FillHandler {
return &FillDBWriter{
db: db,
assetDisplayFn: assetDisplayFn,
exchangeName: exchangeName,
accountID: accountID,
}
}
func (f *FillDBWriter) fetchOrRegisterMarket(trade model.Trade) (*tradingMarket, error) {
if f.market != nil {
return f.market, nil
}
txid := utils.CheckedString(trade.TransactionID)
baseAssetString, e := f.assetDisplayFn(trade.Pair.Base)
if e != nil {
return nil, fmt.Errorf("bot is not configured to recognize the base asset from this trade (txid=%s), base asset = %s, error: %s", txid, string(trade.Pair.Base), e)
}
quoteAssetString, e := f.assetDisplayFn(trade.Pair.Quote)
if e != nil {
return nil, fmt.Errorf("bot is not configured to recognize the quote asset from this trade (txid=%s), quote asset = %s, error: %s", txid, string(trade.Pair.Quote), e)
}
market, e := fetchOrRegisterMarketByDetails(f.db, f.exchangeName, baseAssetString, quoteAssetString)
if e != nil {
return nil, fmt.Errorf("error while calling fetchOrRegisterMarketByDetails (exchangeName=%s, baseAsset=%s, quoteAsset=%s): %s", f.exchangeName, baseAssetString, quoteAssetString, e)
}
f.market = market
return market, nil
}
// fetchOrRegisterMarketByDetails uses the market details
func fetchOrRegisterMarketByDetails(db *sql.DB, exchangeName string, baseAsset string, quoteAsset string) (*tradingMarket, error) {
market := makeTradingMarket(exchangeName, baseAsset, quoteAsset)
fetchedMarket, e := fetchMarketFromDb(db, market.ID)
if e != nil {
return nil, fmt.Errorf("error while fetching market (ID=%s) from db: %s", market.ID, e)
}
if fetchedMarket == nil {
e = registerMarket(db, market)
if e != nil {
return nil, fmt.Errorf("unable to register market: %s", market.String())
}
log.Printf("registered market in db: %s", market.String())
return market, nil
} else if !market.equals(*fetchedMarket) {
return nil, fmt.Errorf("fetched market (%s) was different from computed market (%s)", *fetchedMarket, *market)
}
return market, nil
}
// FetchOrRegisterMarketID is the public method, so we don't need to export tradingMarket unncessarily
func FetchOrRegisterMarketID(db *sql.DB, exchangeName string, baseAsset string, quoteAsset string) (string, error) {
market, e := fetchOrRegisterMarketByDetails(db, exchangeName, baseAsset, quoteAsset)
if e != nil {
return "", fmt.Errorf("error while calling fetchOrRegisterMarketByDetails (exchangeName=%s, baseAsset=%s, quoteAsset=%s): %s", exchangeName, baseAsset, quoteAsset, e)
}
return market.ID, nil
}
func fetchMarketFromDb(db *sql.DB, marketId string) (*tradingMarket, error) {
rows, e := db.Query(kelpdb.SqlQueryMarketsById, marketId)
if e != nil {
return nil, fmt.Errorf("could not execute sql select query (%s) for marketId (%s): %s", kelpdb.SqlQueryMarketsById, marketId, e)
}
defer rows.Close()
for rows.Next() {
var market tradingMarket
e = rows.Scan(&market.ID, &market.ExchangeName, &market.BaseAsset, &market.QuoteAsset)
if e != nil {
return nil, fmt.Errorf("could not scan row into tradingMarket struct: %s", e)
}
log.Printf("fetched market from db: %s", market.String())
return &market, nil
}
return nil, nil
}
func registerMarket(db *sql.DB, market *tradingMarket) error {
sqlInsert := fmt.Sprintf(kelpdb.SqlMarketsInsertTemplate,
market.ID,
market.ExchangeName,
market.BaseAsset,
market.QuoteAsset,
)
_, e := db.Exec(sqlInsert)
if e != nil {
// duplicate insert should return an error
return fmt.Errorf("could not execute sql insert values statement (%s): %s", sqlInsert, e)
}
return nil
}
// HandleFill impl.
func (f *FillDBWriter) HandleFill(trade model.Trade) error {
txid := utils.CheckedString(trade.TransactionID)
timeSeconds := trade.Timestamp.AsInt64() / 1000
date := time.Unix(timeSeconds, 0).UTC()
dateString := date.Format(postgresdb.TimestampFormatString)
market, e := f.fetchOrRegisterMarket(trade)
if e != nil {
return fmt.Errorf("cannot fetch or register market for trade (txid=%s): %s", txid, e)
}
sqlInsert := fmt.Sprintf(kelpdb.SqlTradesInsertTemplate,
market.ID,
txid,
dateString,
trade.OrderAction.String(),
trade.OrderType.String(),
f.checkedFloat(trade.Price),
f.checkedFloat(trade.Volume),
f.checkedFloat(trade.Cost),
f.checkedFloat(trade.Fee),
f.accountID,
trade.OrderID,
)
_, e = f.db.Exec(sqlInsert)
if e != nil {
if strings.Contains(e.Error(), "duplicate key value violates unique constraint \"trades_pkey\"") {
log.Printf("trying to reinsert trade (txid=%s) to db, ignore and continue\n", txid)
return nil
}
// return an error on any other errors
return fmt.Errorf("could not execute sql insert values statement (%s): %s", sqlInsert, e)
}
log.Printf("wrote trade (txid=%s) to db\n", txid)
return nil
}
func (f *FillDBWriter) checkedFloat(n *model.Number) interface{} {
if n == nil {
return nil
}
return n.AsFloat()
}