-
Notifications
You must be signed in to change notification settings - Fork 128
/
volumes.go
208 lines (185 loc) · 5.43 KB
/
volumes.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
package models
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"time"
"github.com/diadata-org/diadata/pkg/dia"
)
const (
WindowVolume = 60 * 60 * 24
)
var (
volumeKey = "VOL" + strconv.Itoa(dia.BlockSizeSeconds)
)
// GetVolumeInflux returns the volume of @asset on @exchange using the VOL120 filter in the given time-range.
// Both, @asset and @exchange may be empty.
// If @starttime,@endtime are empty, the last 24h are taken into account.
func (datastore *DB) GetVolumeInflux(asset dia.Asset, exchange string, starttime time.Time, endtime time.Time) (*float64, error) {
if endtime.IsZero() {
endtime = time.Now()
starttime = endtime.AddDate(0, 0, -1)
}
var q string
if asset == (dia.Asset{}) {
queryString := `
SELECT SUM(value)
FROM %s
WHERE exchange='%s'
AND filter='%s'
AND time > %d AND time<= %d
`
q = fmt.Sprintf(queryString, influxDbFiltersTable, exchange, volumeKey, starttime.UnixNano(), endtime.UnixNano())
} else if exchange == "" {
queryString := `
SELECT SUM(value)
FROM %s
WHERE address='%s' AND blockchain='%s'
AND exchange=''
AND filter='%s'
AND time > %d AND time<= %d
`
q = fmt.Sprintf(queryString, influxDbFiltersTable, asset.Address, asset.Blockchain, volumeKey, starttime.UnixNano(), endtime.UnixNano())
} else {
queryString := `
SELECT SUM(value)
FROM %s
WHERE address='%s' AND blockchain='%s'
AND exchange='%s'
AND filter='%s'
AND time > %d AND time<= %d
`
q = fmt.Sprintf(queryString, influxDbFiltersTable, asset.Address, asset.Blockchain, exchange, volumeKey, starttime.UnixNano(), endtime.UnixNano())
}
var errorString string
res, err := queryInfluxDB(datastore.influxClient, q)
if err != nil {
log.Errorln("GetVolumeInflux ", err)
return nil, err
}
if len(res) > 0 && len(res[0].Series) > 0 {
var result float64
v, o := res[0].Series[0].Values[0][1].(json.Number)
if o {
result, err = v.Float64()
if err != nil {
log.Error(err)
return nil, err
}
return &result, nil
}
errorString = "error on parsing row 1"
return nil, errors.New(errorString)
} else {
volume := float64(0)
log.Warnf("no volume on %s in influx filter table", exchange)
return &volume, nil
}
}
// Get24HoursAssetVolume returns the 24h trading volume of @asset across exchanges.
func (datastore *DB) Get24HoursAssetVolume(asset dia.Asset) (*float64, error) {
endtime := time.Now()
return datastore.GetVolumeInflux(asset, "", endtime.AddDate(0, 0, -1), endtime)
}
// Get24HoursExchangeVolume returns 24h trade volume on @exchange using VOL120 filtered data from influx.
func (datastore *DB) Get24HoursExchangeVolume(exchange string) (*float64, error) {
endtime := time.Now()
return datastore.GetVolumeInflux(dia.Asset{}, exchange, endtime.AddDate(0, 0, -1), endtime)
}
// Returns aggregated volumes from filters measurement on all exchanges.
func (datastore *DB) GetVolumesAllExchanges(asset dia.Asset, starttime time.Time, endtime time.Time) (exchVolumes dia.ExchangeVolumesList, err error) {
q := fmt.Sprintf(
`SELECT SUM(value)
FROM %s
WHERE filter='VOL120'
AND address='%s'
AND blockchain='%s'
AND exchange!=''
AND time>%d
AND time<=%d
GROUP BY exchange`,
influxDbFiltersTable,
asset.Address,
asset.Blockchain,
starttime.UnixNano(),
endtime.UnixNano(),
)
res, err := queryInfluxDB(datastore.influxClient, q)
if err != nil {
log.Errorln("GetLastTrades", err)
return
}
if len(res) > 0 && len(res[0].Series) > 0 {
for i, group := range res[0].Series {
var exchangevolume dia.ExchangeVolume
if val, ok := group.Tags["exchange"]; ok {
exchangevolume.Exchange = val
}
if len(group.Values) > 0 && len(group.Values[0]) > 1 {
exchangevolume.Volume, err = group.Values[0][1].(json.Number).Float64()
if err != nil {
return
}
}
exchVolumes.Volumes = append(exchVolumes.Volumes, exchangevolume)
if i == 0 {
exchVolumes.Timestamp, err = time.Parse(time.RFC3339, res[0].Series[0].Values[0][0].(string))
if err != nil {
return
}
}
}
} else {
log.Error("Empty response GetVolumesAllExchanges")
}
return
}
func (datastore *DB) GetExchangePairVolumes(asset dia.Asset, starttime time.Time, endtime time.Time) (map[string][]dia.PairVolume, error) {
volumeMap := make(map[string][]dia.PairVolume)
query := fmt.Sprintf(
`
SELECT SUM(multiplication)
FROM (
SELECT ABS(estimatedUSDPrice*volume)
AS multiplication
FROM %s
WHERE quotetokenaddress='%s'
AND quotetokenblockchain='%s'
AND time>%d
AND time<=%d
)
GROUP BY "exchange","basetokenaddress","basetokenblockchain"
`,
influxDbTradesTable,
asset.Address,
asset.Blockchain,
starttime.UnixNano(),
endtime.UnixNano(),
)
res, err := queryInfluxDB(datastore.influxClient, query)
if err != nil {
return volumeMap, err
}
if len(res) > 0 && len(res[0].Series) > 0 {
for _, row := range res[0].Series {
if len(row.Values[0]) > 1 {
var (
pairvolume dia.PairVolume
err error
)
exchange := row.Tags["exchange"]
pairvolume.Volume, err = row.Values[0][1].(json.Number).Float64()
if err != nil {
log.Warn("parse volume: ", err)
}
pairvolume.Pair = dia.Pair{
QuoteToken: dia.Asset{Blockchain: asset.Blockchain, Address: asset.Address},
BaseToken: dia.Asset{Blockchain: row.Tags["basetokenblockchain"], Address: row.Tags["basetokenaddress"]},
}
volumeMap[exchange] = append(volumeMap[exchange], pairvolume)
}
}
}
return volumeMap, nil
}