@@ -78,18 +78,42 @@ func testTradeableVolumeInRange(t *testing.T) {
78
78
expectedVolume : 702 ,
79
79
},
80
80
{
81
- name : "trade causes sign to flip and volume crosses curves" ,
81
+ name : "buy trade causes sign to flip and full volume crosses curves" ,
82
+ price1 : num .NewUint (500 ),
83
+ price2 : num .NewUint (3500 ),
84
+ side : types .SideBuy ,
85
+ expectedVolume : 1337 ,
86
+ position : 700 , // position at full lower boundary, incoming is by so whole volume of both curves is available
87
+ },
88
+ {
89
+ name : "sell trade causes sign to flip and full volume crosses curves" ,
90
+ price1 : num .NewUint (500 ),
91
+ price2 : num .NewUint (3500 ),
92
+ side : types .SideSell ,
93
+ expectedVolume : 1337 ,
94
+ position : - 700 , // position at full upper boundary, incoming is by so whole volume of both curves is available
95
+ },
96
+ {
97
+ name : "buy trade causes sign to flip and partial volume across both curves" ,
98
+ price1 : num .NewUint (500 ),
99
+ price2 : num .NewUint (3500 ),
100
+ side : types .SideBuy ,
101
+ expectedVolume : 986 ,
102
+ position : 350 ,
103
+ },
104
+ {
105
+ name : "sell trade causes sign to flip and partial volume across both curves" ,
82
106
price1 : num .NewUint (500 ),
83
107
price2 : num .NewUint (3500 ),
84
108
side : types .SideSell ,
85
- expectedVolume : 1337 , // 635 + 702
86
- position : 10 ,
109
+ expectedVolume : 1053 ,
110
+ position : - 350 ,
87
111
},
88
112
}
89
113
90
114
for _ , tt := range tests {
91
115
t .Run (tt .name , func (t * testing.T ) {
92
- ensurePosition (t , p .pos , tt .position , num .UintZero ())
116
+ ensurePositionN (t , p .pos , tt .position , num .UintZero (), 2 )
93
117
volume := p .pool .TradableVolumeInRange (tt .side , tt .price1 , tt .price2 )
94
118
assert .Equal (t , int (tt .expectedVolume ), int (volume ))
95
119
})
@@ -100,17 +124,17 @@ func testPoolPositionFactor(t *testing.T) {
100
124
p := newTestPoolWithPositionFactor (t , num .DecimalFromInt64 (1000 ))
101
125
defer p .ctrl .Finish ()
102
126
103
- ensurePosition (t , p .pos , 0 , num .UintZero ())
127
+ ensurePositionN (t , p .pos , 0 , num .UintZero (), 2 )
104
128
volume := p .pool .TradableVolumeInRange (types .SideBuy , num .NewUint (2000 ), num .NewUint (2200 ))
105
129
// with position factot of 1 the volume is 635
106
130
assert .Equal (t , int (635395 ), int (volume ))
107
131
108
- ensurePosition (t , p .pos , 0 , num .UintZero ())
132
+ ensurePositionN (t , p .pos , 0 , num .UintZero (), 2 )
109
133
volume = p .pool .TradableVolumeInRange (types .SideSell , num .NewUint (1800 ), num .NewUint (2000 ))
110
134
// with position factot of 1 the volume is 702
111
135
assert .Equal (t , int (702411 ), int (volume ))
112
136
113
- ensurePosition (t , p .pos , - 1 , num .NewUint (2000 ))
137
+ ensurePositionN (t , p .pos , - 1 , num .NewUint (2000 ), 1 )
114
138
// now best price should be the same as if the factor were 1, since its a price and not a volume
115
139
fairPrice := p .pool .BestPrice (nil )
116
140
assert .Equal (t , "2001" , fairPrice .String ())
@@ -182,30 +206,30 @@ func testOneSidedPool(t *testing.T) {
182
206
defer p .ctrl .Finish ()
183
207
184
208
// side with liquidity returns volume
185
- ensurePosition (t , p .pos , 0 , num .UintZero ())
209
+ ensurePositionN (t , p .pos , 0 , num .UintZero (), 2 )
186
210
volume := p .pool .TradableVolumeInRange (types .SideBuy , num .NewUint (2000 ), num .NewUint (2200 ))
187
211
assert .Equal (t , int (635 ), int (volume ))
188
212
189
213
// empty side returns no volume
190
- ensurePosition (t , p .pos , 0 , num .UintZero ())
214
+ ensurePositionN (t , p .pos , 0 , num .UintZero (), 2 )
191
215
volume = p .pool .TradableVolumeInRange (types .SideSell , num .NewUint (1800 ), num .NewUint (2000 ))
192
216
assert .Equal (t , int (0 ), int (volume ))
193
217
194
218
// pool with short position and incoming sell only reports volume up to base
195
219
// empty side returns no volume
196
- ensurePosition (t , p .pos , - 10 , num .UintZero ())
220
+ ensurePositionN (t , p .pos , - 10 , num .UintZero (), 2 )
197
221
volume = p .pool .TradableVolumeInRange (types .SideSell , num .NewUint (1800 ), num .NewUint (2200 ))
198
- assert .Equal (t , int (635 ), int (volume ))
222
+ assert .Equal (t , int (10 ), int (volume ))
199
223
200
224
// fair price at 0 position is still ok
201
225
ensurePosition (t , p .pos , 0 , num .UintZero ())
202
226
price := p .pool .BestPrice (nil )
203
- assert .Equal (t , price .String (), "2000" )
227
+ assert .Equal (t , "2000" , price .String ())
204
228
205
229
// fair price at short position is still ok
206
230
ensurePosition (t , p .pos , - 10 , num .UintZero ())
207
231
price = p .pool .BestPrice (nil )
208
- assert .Equal (t , price .String (), "2003" )
232
+ assert .Equal (t , "2003" , price .String ())
209
233
210
234
// fair price when long should panic since AMM should never be able to get into that state
211
235
// fair price at short position is still ok
@@ -354,22 +378,22 @@ func TestNotebook(t *testing.T) {
354
378
355
379
pos := int64 (0 )
356
380
357
- ensurePosition (t , p .pos , pos , num .UintZero ())
381
+ ensurePositionN (t , p .pos , pos , num .UintZero (), 2 )
358
382
volume := p .pool .TradableVolumeInRange (types .SideSell , base , low )
359
383
assert .Equal (t , int (702 ), int (volume ))
360
384
361
- ensurePosition (t , p .pos , pos , num .UintZero ())
385
+ ensurePositionN (t , p .pos , pos , num .UintZero (), 2 )
362
386
volume = p .pool .TradableVolumeInRange (types .SideBuy , up , base )
363
387
assert .Equal (t , int (635 ), int (volume ))
364
388
365
389
lowmid := num .NewUint (1900 )
366
390
upmid := num .NewUint (2100 )
367
391
368
- ensurePosition (t , p .pos , pos , num .UintZero ())
392
+ ensurePositionN (t , p .pos , pos , num .UintZero (), 2 )
369
393
volume = p .pool .TradableVolumeInRange (types .SideSell , low , lowmid )
370
394
assert .Equal (t , int (365 ), int (volume ))
371
395
372
- ensurePosition (t , p .pos , pos , num .UintZero ())
396
+ ensurePositionN (t , p .pos , pos , num .UintZero (), 2 )
373
397
volume = p .pool .TradableVolumeInRange (types .SideBuy , upmid , up )
374
398
assert .Equal (t , int (306 ), int (volume ))
375
399
0 commit comments