forked from trezor/blockbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiat.go
212 lines (193 loc) · 5.89 KB
/
fiat.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
package db
import (
"encoding/binary"
"encoding/json"
"math"
"time"
vlq "github.com/bsm/go-vlq"
"github.com/golang/glog"
"github.com/juju/errors"
"github.com/linxGnu/grocksdb"
"github.com/trezor/blockbook/common"
)
// FiatRatesTimeFormat is a format string for storing FiatRates timestamps in rocksdb
const FiatRatesTimeFormat = "20060102150405" // YYYYMMDDhhmmss
func packTimestamp(t *time.Time) []byte {
return []byte(t.UTC().Format(FiatRatesTimeFormat))
}
func packFloat32(buf []byte, n float32) int {
binary.BigEndian.PutUint32(buf, math.Float32bits(n))
return 4
}
func unpackFloat32(buf []byte) (float32, int) {
return math.Float32frombits(binary.BigEndian.Uint32(buf)), 4
}
func packCurrencyRatesTicker(ticker *common.CurrencyRatesTicker) []byte {
buf := make([]byte, 0, 32)
varBuf := make([]byte, vlq.MaxLen64)
l := packVaruint(uint(len(ticker.Rates)), varBuf)
buf = append(buf, varBuf[:l]...)
for c, v := range ticker.Rates {
buf = append(buf, packString(c)...)
l = packFloat32(varBuf, v)
buf = append(buf, varBuf[:l]...)
}
l = packVaruint(uint(len(ticker.TokenRates)), varBuf)
buf = append(buf, varBuf[:l]...)
for c, v := range ticker.TokenRates {
buf = append(buf, packString(c)...)
l = packFloat32(varBuf, v)
buf = append(buf, varBuf[:l]...)
}
return buf
}
func unpackCurrencyRatesTicker(buf []byte) (*common.CurrencyRatesTicker, error) {
var (
ticker common.CurrencyRatesTicker
s string
l int
len uint
v float32
)
len, l = unpackVaruint(buf)
buf = buf[l:]
if len > 0 {
ticker.Rates = make(map[string]float32, len)
for i := 0; i < int(len); i++ {
s, l = unpackString(buf)
buf = buf[l:]
v, l = unpackFloat32(buf)
buf = buf[l:]
ticker.Rates[s] = v
}
}
len, l = unpackVaruint(buf)
buf = buf[l:]
if len > 0 {
ticker.TokenRates = make(map[string]float32, len)
for i := 0; i < int(len); i++ {
s, l = unpackString(buf)
buf = buf[l:]
v, l = unpackFloat32(buf)
buf = buf[l:]
ticker.TokenRates[s] = v
}
}
return &ticker, nil
}
// FiatRatesStoreTicker stores ticker data at the specified time
func (d *RocksDB) FiatRatesStoreTicker(wb *grocksdb.WriteBatch, ticker *common.CurrencyRatesTicker) error {
if len(ticker.Rates) == 0 {
return errors.New("Error storing ticker: empty rates")
}
wb.PutCF(d.cfh[cfFiatRates], packTimestamp(&ticker.Timestamp), packCurrencyRatesTicker(ticker))
return nil
}
func getTickerFromIterator(it *grocksdb.Iterator, vsCurrency string, token string) (*common.CurrencyRatesTicker, error) {
timeObj, err := time.Parse(FiatRatesTimeFormat, string(it.Key().Data()))
if err != nil {
return nil, err
}
ticker, err := unpackCurrencyRatesTicker(it.Value().Data())
if err != nil {
return nil, err
}
if !common.IsSuitableTicker(ticker, vsCurrency, token) {
return nil, nil
}
ticker.Timestamp = timeObj.UTC()
return ticker, nil
}
// FiatRatesGetTicker gets FiatRates ticker at the specified timestamp if it exist
func (d *RocksDB) FiatRatesGetTicker(tickerTime *time.Time) (*common.CurrencyRatesTicker, error) {
tickerTimeFormatted := tickerTime.UTC().Format(FiatRatesTimeFormat)
val, err := d.db.GetCF(d.ro, d.cfh[cfFiatRates], []byte(tickerTimeFormatted))
if err != nil {
return nil, err
}
defer val.Free()
data := val.Data()
if len(data) == 0 {
return nil, nil
}
ticker, err := unpackCurrencyRatesTicker(data)
if err != nil {
return nil, err
}
ticker.Timestamp = tickerTime.UTC()
return ticker, nil
}
// FiatRatesFindTicker gets FiatRates data closest to the specified timestamp, of the base currency, vsCurrency or the token if specified
func (d *RocksDB) FiatRatesFindTicker(tickerTime *time.Time, vsCurrency string, token string) (*common.CurrencyRatesTicker, error) {
tickerTimeFormatted := tickerTime.UTC().Format(FiatRatesTimeFormat)
it := d.db.NewIteratorCF(d.ro, d.cfh[cfFiatRates])
defer it.Close()
for it.Seek([]byte(tickerTimeFormatted)); it.Valid(); it.Next() {
ticker, err := getTickerFromIterator(it, vsCurrency, token)
if err != nil {
glog.Error("FiatRatesFindTicker error: ", err)
return nil, err
}
if ticker != nil {
return ticker, nil
}
}
return nil, nil
}
// FiatRatesGetAllTickers gets FiatRates data closest to the specified timestamp, of the base currency, vsCurrency or the token if specified
func (d *RocksDB) FiatRatesGetAllTickers(fn func(ticker *common.CurrencyRatesTicker) error) error {
it := d.db.NewIteratorCF(d.ro, d.cfh[cfFiatRates])
defer it.Close()
for it.SeekToFirst(); it.Valid(); it.Next() {
ticker, err := getTickerFromIterator(it, "", "")
if err != nil {
return err
}
if ticker == nil {
return errors.New("FiatRatesGetAllTickers got nil ticker")
}
if err = fn(ticker); err != nil {
return err
}
}
return nil
}
// FiatRatesFindLastTicker gets the last FiatRates record, of the base currency, vsCurrency or the token if specified
func (d *RocksDB) FiatRatesFindLastTicker(vsCurrency string, token string) (*common.CurrencyRatesTicker, error) {
it := d.db.NewIteratorCF(d.ro, d.cfh[cfFiatRates])
defer it.Close()
for it.SeekToLast(); it.Valid(); it.Prev() {
ticker, err := getTickerFromIterator(it, vsCurrency, token)
if err != nil {
glog.Error("FiatRatesFindLastTicker error: ", err)
return nil, err
}
if ticker != nil {
return ticker, nil
}
}
return nil, nil
}
func (d *RocksDB) FiatRatesGetSpecialTickers(key string) (*[]common.CurrencyRatesTicker, error) {
val, err := d.db.GetCF(d.ro, d.cfh[cfDefault], []byte(key))
if err != nil {
return nil, err
}
defer val.Free()
data := val.Data()
if data == nil {
return nil, nil
}
var tickers []common.CurrencyRatesTicker
if err := json.Unmarshal(data, &tickers); err != nil {
return nil, err
}
return &tickers, nil
}
func (d *RocksDB) FiatRatesStoreSpecialTickers(key string, tickers *[]common.CurrencyRatesTicker) error {
data, err := json.Marshal(tickers)
if err != nil {
return err
}
return d.db.PutCF(d.wo, d.cfh[cfDefault], []byte(key), data)
}