forked from stellar-deprecated/kelp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
volumeFilter_test.go
268 lines (245 loc) · 7.89 KB
/
volumeFilter_test.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package plugins
import (
"database/sql"
"fmt"
"testing"
"github.com/openlyinc/pointy"
"github.com/stellar/kelp/queries"
"github.com/stellar/kelp/support/utils"
"github.com/stellar/go/txnbuild"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/kelp/model"
"github.com/stretchr/testify/assert"
)
func makeTestVolumeFilterConfig(baseCapInBase, baseCapInQuote float64, additionalMarketIDs, optionalAccountIDs []string, mode volumeFilterMode) *VolumeFilterConfig {
var baseCapInBasePtr *float64
if baseCapInBase >= 0 {
baseCapInBasePtr = pointy.Float64(baseCapInBase)
}
var baseCapInQuotePtr *float64
if baseCapInQuote >= 0 {
baseCapInQuotePtr = pointy.Float64(baseCapInQuote)
}
return &VolumeFilterConfig{
SellBaseAssetCapInBaseUnits: baseCapInBasePtr,
SellBaseAssetCapInQuoteUnits: baseCapInQuotePtr,
mode: mode,
additionalMarketIDs: additionalMarketIDs,
optionalAccountIDs: optionalAccountIDs,
}
}
func makeWantVolumeFilter(config *VolumeFilterConfig, firstMarketID string, marketIDs []string, optionalAccountIDs []string, action string) *volumeFilter {
queryMarketIDs := utils.Dedupe(append([]string{firstMarketID}, marketIDs...))
query, e := queries.MakeDailyVolumeByDateForMarketIdsAction(&sql.DB{}, queryMarketIDs, action, optionalAccountIDs)
if e != nil {
panic(e)
}
return &volumeFilter{
name: "volumeFilter",
baseAsset: utils.NativeAsset,
quoteAsset: utils.NativeAsset,
config: config,
dailyVolumeByDateQuery: query,
}
}
func TestMakeFilterVolume(t *testing.T) {
testAssetDisplayFn := model.MakeSdexMappedAssetDisplayFn(map[model.Asset]hProtocol.Asset{model.Asset("XLM"): utils.NativeAsset})
configValue := ""
exchangeName := ""
tradingPair := &model.TradingPair{Base: "XLM", Quote: "XLM"}
modes := []volumeFilterMode{volumeFilterModeExact, volumeFilterModeIgnore}
firstMarketID := MakeMarketID(exchangeName, "native", "native")
testCases := []struct {
name string
marketIDs []string
accountIDs []string
wantFilter *volumeFilter
}{
// TODO DS Confirm the empty config fails once validation is added to the constructor
{
name: "1 market id",
marketIDs: []string{"marketID"},
accountIDs: []string{},
},
{
name: "2 market ids",
marketIDs: []string{"marketID1", "marketID2"},
accountIDs: []string{},
},
{
name: "2 dupe market ids, 1 distinct",
marketIDs: []string{"marketID1", "marketID1", "marketID2"},
accountIDs: []string{},
},
{
name: "1 account id",
marketIDs: []string{},
accountIDs: []string{"accountID"},
},
{
name: "2 account ids",
marketIDs: []string{},
accountIDs: []string{"accountID1", "accountID2"},
},
{
name: "account and market ids",
marketIDs: []string{"marketID"},
accountIDs: []string{"accountID"},
},
}
for _, k := range testCases {
// this lets us test both types of modes when varying the market and account ids
for _, m := range modes {
// this lets us test both constraints within the config
baseCapInBaseConfig := makeTestVolumeFilterConfig(1.0, -1.0, k.marketIDs, k.accountIDs, m)
baseCapInQuoteConfig := makeTestVolumeFilterConfig(-1.0, 1.0, k.marketIDs, k.accountIDs, m)
for _, config := range []*VolumeFilterConfig{baseCapInBaseConfig, baseCapInQuoteConfig} {
// configType is used to represent the type of config when printing test name
var configType string
if config.SellBaseAssetCapInBaseUnits != nil {
configType = "base"
} else {
configType = "quote"
}
// TODO DS Vary filter action between buy and sell, once buy logic is implemented.
wantFilter := makeWantVolumeFilter(config, firstMarketID, k.marketIDs, k.accountIDs, "sell")
t.Run(fmt.Sprintf("%s/%s/%s", k.name, configType, m), func(t *testing.T) {
actual, e := makeFilterVolume(
configValue,
exchangeName,
tradingPair,
testAssetDisplayFn,
utils.NativeAsset,
utils.NativeAsset,
&sql.DB{},
config,
)
if !assert.Nil(t, e) {
return
}
assert.Equal(t, wantFilter, actual)
})
}
}
}
}
func makeManageSellOffer(price, amount string) *txnbuild.ManageSellOffer {
if amount == "" {
return nil
}
return &txnbuild.ManageSellOffer{
Buying: txnbuild.NativeAsset{},
Selling: txnbuild.NativeAsset{},
Price: price,
Amount: amount,
}
}
func TestVolumeFilterFn(t *testing.T) {
testCases := []struct {
name string
filter *volumeFilter
sellBaseCapInBase *float64
sellBaseCapInQuote *float64
otbBaseCap float64
otbQuoteCap float64
tbbBaseCap float64
tbbQuoteCap float64
price string
inputAmount string
wantAmount string
wantTbbBaseCap float64
wantTbbQuoteCap float64
}{
{
name: "selling, base units sell cap, don't keep selling base",
sellBaseCapInBase: pointy.Float64(0.0),
sellBaseCapInQuote: nil,
otbBaseCap: 0.0,
otbQuoteCap: 0.0,
tbbBaseCap: 0.0,
tbbQuoteCap: 0.0,
price: "2.0",
inputAmount: "100.0",
wantAmount: "",
wantTbbBaseCap: 0.0,
wantTbbQuoteCap: 0.0,
},
{
name: "selling, base units sell cap, keep selling base",
sellBaseCapInBase: pointy.Float64(1.0),
sellBaseCapInQuote: nil,
otbBaseCap: 0.0,
otbQuoteCap: 0.0,
tbbBaseCap: 0.0,
tbbQuoteCap: 0.0,
price: "2.0",
inputAmount: "100.0",
wantAmount: "1.0000000",
wantTbbBaseCap: 1.0,
wantTbbQuoteCap: 2.0,
},
{
name: "selling, quote units sell cap, don't keep selling quote",
sellBaseCapInBase: nil,
sellBaseCapInQuote: pointy.Float64(0),
otbBaseCap: 0.0,
otbQuoteCap: 0.0,
tbbBaseCap: 0.0,
tbbQuoteCap: 0.0,
price: "2.0",
inputAmount: "100.0",
wantAmount: "",
wantTbbBaseCap: 0.0,
wantTbbQuoteCap: 0.0,
},
{
name: "selling, quote units sell cap, keep selling quote",
sellBaseCapInBase: nil,
sellBaseCapInQuote: pointy.Float64(1.),
otbBaseCap: 0.0,
otbQuoteCap: 0.0,
tbbBaseCap: 0.0,
tbbQuoteCap: 0.0,
price: "2.0",
inputAmount: "100.0",
wantAmount: "0.5000000",
wantTbbBaseCap: 0.5,
wantTbbQuoteCap: 1.0,
},
{
name: "selling, base and quote units sell cap, keep selling base and quote",
sellBaseCapInBase: pointy.Float64(1.),
sellBaseCapInQuote: pointy.Float64(1.),
otbBaseCap: 0.0,
otbQuoteCap: 0.0,
tbbBaseCap: 0.0,
tbbQuoteCap: 0.0,
price: "2.0",
inputAmount: "100.0",
wantAmount: "0.5000000",
wantTbbBaseCap: 0.5,
wantTbbQuoteCap: 1.0,
},
}
for _, k := range testCases {
t.Run(k.name, func(t *testing.T) {
marketIDs := []string{}
accountIDs := []string{}
mode := volumeFilterModeExact
dailyOTB := makeTestVolumeFilterConfig(k.otbBaseCap, k.otbQuoteCap, marketIDs, accountIDs, mode)
dailyTBB := makeTestVolumeFilterConfig(k.tbbBaseCap, k.tbbQuoteCap, marketIDs, accountIDs, mode)
wantTBB := makeTestVolumeFilterConfig(k.wantTbbBaseCap, k.wantTbbQuoteCap, marketIDs, accountIDs, mode)
op := makeManageSellOffer(k.price, k.inputAmount)
wantOp := makeManageSellOffer(k.price, k.wantAmount)
lp := limitParameters{
sellBaseAssetCapInBaseUnits: k.sellBaseCapInBase,
sellBaseAssetCapInQuoteUnits: k.sellBaseCapInQuote,
mode: volumeFilterModeExact,
}
actual, e := volumeFilterFn(dailyOTB, dailyTBB, op, utils.NativeAsset, utils.NativeAsset, lp)
assert.Nil(t, e)
assert.Equal(t, wantOp, actual)
assert.Equal(t, wantTBB, dailyTBB)
})
}
}