-
Notifications
You must be signed in to change notification settings - Fork 55
/
pool.go
682 lines (623 loc) 路 19.6 KB
/
pool.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
package amm
import (
"fmt"
"math/big"
sdk "github.com/cosmos/cosmos-sdk/types"
utils "github.com/crescent-network/crescent/v3/types"
)
var (
_ Pool = (*BasicPool)(nil)
_ Pool = (*RangedPool)(nil)
)
// Pool is the interface of a pool.
type Pool interface {
Balances() (rx, ry sdk.Int)
SetBalances(rx, ry sdk.Int, derive bool)
PoolCoinSupply() sdk.Int
Price() sdk.Dec
IsDepleted() bool
HighestBuyPrice() (sdk.Dec, bool)
LowestSellPrice() (sdk.Dec, bool)
BuyAmountOver(price sdk.Dec, inclusive bool) sdk.Int
SellAmountUnder(price sdk.Dec, inclusive bool) sdk.Int
BuyAmountTo(price sdk.Dec) sdk.Int
SellAmountTo(price sdk.Dec) sdk.Int
Clone() Pool
}
// BasicPool is the basic pool type.
type BasicPool struct {
// rx and ry are the pool's reserve balance of each x/y coin.
// In perspective of a pair, x coin is the quote coin and
// y coin is the base coin.
rx, ry sdk.Int
// ps is the pool's pool coin supply.
ps sdk.Int
}
// NewBasicPool returns a new BasicPool.
// It is OK to pass an empty sdk.Int to ps when ps is not going to be used.
func NewBasicPool(rx, ry, ps sdk.Int) *BasicPool {
return &BasicPool{
rx: rx,
ry: ry,
ps: ps,
}
}
func CreateBasicPool(rx, ry sdk.Int) (*BasicPool, error) {
if rx.IsZero() || ry.IsZero() {
return nil, fmt.Errorf("cannot create basic pool with zero reserve amount")
}
p := rx.ToDec().Quo(ry.ToDec())
if p.LT(MinPoolPrice) {
return nil, fmt.Errorf("pool price is lower than min price %s", MinPoolPrice)
}
if p.GT(MaxPoolPrice) {
return nil, fmt.Errorf("pool price is greater than max price %s", MaxPoolPrice)
}
return NewBasicPool(rx, ry, InitialPoolCoinSupply(rx, ry)), nil
}
// Balances returns the balances of the pool.
func (pool *BasicPool) Balances() (rx, ry sdk.Int) {
return pool.rx, pool.ry
}
func (pool *BasicPool) SetBalances(rx, ry sdk.Int, _ bool) {
pool.rx = rx
pool.ry = ry
}
// PoolCoinSupply returns the pool coin supply.
func (pool *BasicPool) PoolCoinSupply() sdk.Int {
return pool.ps
}
// Price returns the pool price.
func (pool *BasicPool) Price() sdk.Dec {
if pool.rx.IsZero() || pool.ry.IsZero() {
panic("pool price is not defined for a depleted pool")
}
return pool.rx.ToDec().Quo(pool.ry.ToDec())
}
// IsDepleted returns whether the pool is depleted or not.
func (pool *BasicPool) IsDepleted() bool {
return pool.ps.IsZero() || pool.rx.IsZero() || pool.ry.IsZero()
}
// HighestBuyPrice returns the highest buy price of the pool.
func (pool *BasicPool) HighestBuyPrice() (price sdk.Dec, found bool) {
// The highest buy price is actually a bit lower than pool price,
// but it's not important for our matching logic.
return pool.Price(), true
}
// LowestSellPrice returns the lowest sell price of the pool.
func (pool *BasicPool) LowestSellPrice() (price sdk.Dec, found bool) {
// The lowest sell price is actually a bit higher than the pool price,
// but it's not important for our matching logic.
return pool.Price(), true
}
// BuyAmountOver returns the amount of buy orders for price greater than
// or equal to given price.
// amt = (X - P*Y)/P
func (pool *BasicPool) BuyAmountOver(price sdk.Dec, _ bool) (amt sdk.Int) {
origPrice := price
if price.LT(MinPoolPrice) {
price = MinPoolPrice
}
if price.GTE(pool.Price()) {
return zeroInt
}
dx := pool.rx.ToDec().Sub(price.MulInt(pool.ry))
if !dx.IsPositive() {
return zeroInt
}
utils.SafeMath(func() {
amt = dx.QuoTruncate(origPrice).TruncateInt()
if amt.GT(MaxCoinAmount) {
amt = MaxCoinAmount
}
}, func() {
amt = MaxCoinAmount
})
return
}
func (pool *BasicPool) SellAmountUnder(price sdk.Dec, _ bool) (amt sdk.Int) {
if price.GT(MaxPoolPrice) {
price = MaxPoolPrice
}
if price.LTE(pool.Price()) {
return zeroInt
}
amt = pool.ry.ToDec().Sub(pool.rx.ToDec().QuoRoundUp(price)).TruncateInt()
if !amt.IsPositive() {
return zeroInt
}
return
}
// BuyAmountTo returns the amount of buy orders of the pool for price,
// where BuyAmountTo is used when the pool price is higher than the highest
// price of the order book.
func (pool *BasicPool) BuyAmountTo(price sdk.Dec) (amt sdk.Int) {
origPrice := price
if price.LT(MinPoolPrice) {
price = MinPoolPrice
}
if price.GTE(pool.Price()) {
return zeroInt
}
sqrtRx := utils.DecApproxSqrt(pool.rx.ToDec())
sqrtRy := utils.DecApproxSqrt(pool.ry.ToDec())
sqrtPrice := utils.DecApproxSqrt(price)
dx := pool.rx.ToDec().Sub(sqrtPrice.Mul(sqrtRx.Mul(sqrtRy))) // dx = rx - sqrt(P * rx * ry)
if !dx.IsPositive() {
return zeroInt
}
utils.SafeMath(func() {
amt = dx.QuoTruncate(origPrice).TruncateInt() // dy = dx / P
if amt.GT(MaxCoinAmount) {
amt = MaxCoinAmount
}
}, func() {
amt = MaxCoinAmount
})
return
}
// SellAmountTo returns the amount of sell orders of the pool for price,
// where SellAmountTo is used when the pool price is lower than the lowest
// price of the order book.
func (pool *BasicPool) SellAmountTo(price sdk.Dec) (amt sdk.Int) {
if price.GT(MaxPoolPrice) {
price = MaxPoolPrice
}
if price.LTE(pool.Price()) {
return zeroInt
}
sqrtRx := utils.DecApproxSqrt(pool.rx.ToDec())
sqrtRy := utils.DecApproxSqrt(pool.ry.ToDec())
sqrtPrice := utils.DecApproxSqrt(price)
// dy = ry - sqrt(rx * ry / P)
amt = pool.ry.ToDec().Sub(sqrtRx.Mul(sqrtRy).Quo(sqrtPrice)).TruncateInt()
if !amt.IsPositive() {
return zeroInt
}
return
}
func (pool *BasicPool) Clone() Pool {
return NewBasicPool(pool.rx, pool.ry, pool.ps)
}
type RangedPool struct {
rx, ry sdk.Int
ps sdk.Int
minPrice, maxPrice sdk.Dec
transX, transY sdk.Dec
xComp, yComp sdk.Dec
}
// NewRangedPool returns a new RangedPool.
func NewRangedPool(rx, ry, ps sdk.Int, minPrice, maxPrice sdk.Dec) *RangedPool {
transX, transY := DeriveTranslation(rx, ry, minPrice, maxPrice)
return &RangedPool{
rx: rx,
ry: ry,
ps: ps,
minPrice: minPrice,
maxPrice: maxPrice,
transX: transX,
transY: transY,
xComp: rx.ToDec().Add(transX),
yComp: ry.ToDec().Add(transY),
}
}
// CreateRangedPool creates new RangedPool from given inputs, while validating
// the inputs and using only needed amount of x/y coins(the rest should be refunded).
func CreateRangedPool(x, y sdk.Int, minPrice, maxPrice, initialPrice sdk.Dec) (pool *RangedPool, err error) {
if !x.IsPositive() && !y.IsPositive() {
return nil, fmt.Errorf("either x or y must be positive")
}
if err := ValidateRangedPoolParams(minPrice, maxPrice, initialPrice); err != nil {
return nil, err
}
// P = initialPrice, M = minPrice, L = maxPrice
var ax, ay sdk.Int
switch {
case initialPrice.Equal(minPrice): // single y asset pool
ax = zeroInt
ay = y
case initialPrice.Equal(maxPrice): // single x asset pool
ax = x
ay = zeroInt
default: // normal pool
sqrt := utils.DecApproxSqrt
xDec, yDec := x.ToDec(), y.ToDec()
sqrtP := sqrt(initialPrice) // sqrt(P)
sqrtM := sqrt(minPrice) // sqrt(M)
sqrtL := sqrt(maxPrice) // sqrt(L)
// Assume that we can accept all x
ax = x
// ay = {x / (sqrt(P)-sqrt(M))} * (1/sqrt(P) - 1/sqrt(L))
ay = xDec.Quo(sqrtP.Sub(sqrtM)).Mul(inv(sqrtP).Sub(inv(sqrtL))).Ceil().TruncateInt()
if ay.GT(y) {
// Accept all y
// ax = {y / (1/sqrt(P) - 1/sqrt(L))} * (sqrt(P) - sqrt(M))
ax = yDec.Quo(inv(sqrtP).Sub(inv(sqrtL))).Mul(sqrtP.Sub(sqrtM)).Ceil().TruncateInt()
ay = y
}
}
return NewRangedPool(ax, ay, InitialPoolCoinSupply(ax, ay), minPrice, maxPrice), nil
}
func ValidateRangedPoolParams(minPrice, maxPrice, initialPrice sdk.Dec) error {
if !initialPrice.IsPositive() {
return fmt.Errorf("initial price must be positive: %s", initialPrice)
}
if minPrice.LT(MinPoolPrice) {
return fmt.Errorf("min price must not be lower than %s", MinPoolPrice)
}
if !maxPrice.IsPositive() {
return fmt.Errorf("max price must be positive: %s", maxPrice)
}
if maxPrice.GT(MaxPoolPrice) {
return fmt.Errorf("max price must not be higher than %s", MaxPoolPrice)
}
if !maxPrice.GT(minPrice) {
return fmt.Errorf("max price must be higher than min price")
}
if maxPrice.Sub(minPrice).Quo(minPrice).LT(MinRangedPoolPriceGapRatio) {
return fmt.Errorf("min price and max price are too close")
}
if initialPrice.LT(minPrice) {
return fmt.Errorf("initial price must not be lower than min price")
}
if initialPrice.GT(maxPrice) {
return fmt.Errorf("initial price must not be higher than max price")
}
return nil
}
// Balances returns the balances of the pool.
func (pool *RangedPool) Balances() (rx, ry sdk.Int) {
return pool.rx, pool.ry
}
// SetBalances sets RangedPool's balances without recalculating
// transX and transY.
func (pool *RangedPool) SetBalances(rx, ry sdk.Int, derive bool) {
if derive {
pool.transX, pool.transY = DeriveTranslation(rx, ry, pool.minPrice, pool.maxPrice)
}
pool.rx = rx
pool.ry = ry
pool.xComp = pool.rx.ToDec().Add(pool.transX)
pool.yComp = pool.ry.ToDec().Add(pool.transY)
}
// PoolCoinSupply returns the pool coin supply.
func (pool *RangedPool) PoolCoinSupply() sdk.Int {
return pool.ps
}
func (pool *RangedPool) Translation() (transX, transY sdk.Dec) {
return pool.transX, pool.transY
}
func (pool *RangedPool) MinPrice() sdk.Dec {
return pool.minPrice
}
func (pool *RangedPool) MaxPrice() sdk.Dec {
return pool.maxPrice
}
// Price returns the pool price.
func (pool *RangedPool) Price() sdk.Dec {
if pool.rx.IsZero() && pool.ry.IsZero() {
panic("pool price is not defined for a depleted pool")
}
return pool.xComp.Quo(pool.yComp) // (rx + transX) / (ry + transY)
}
// IsDepleted returns whether the pool is depleted or not.
func (pool *RangedPool) IsDepleted() bool {
return pool.ps.IsZero() || (pool.rx.IsZero() && pool.ry.IsZero())
}
// HighestBuyPrice returns the highest buy price of the pool.
func (pool *RangedPool) HighestBuyPrice() (price sdk.Dec, found bool) {
// The highest buy price is actually a bit lower than pool price,
// but it's not important for our matching logic.
return pool.Price(), true
}
// LowestSellPrice returns the lowest sell price of the pool.
func (pool *RangedPool) LowestSellPrice() (price sdk.Dec, found bool) {
// The lowest sell price is actually a bit higher than the pool price,
// but it's not important for our matching logic.
return pool.Price(), true
}
// BuyAmountOver returns the amount of buy orders for price greater than
// or equal to given price.
func (pool *RangedPool) BuyAmountOver(price sdk.Dec, _ bool) (amt sdk.Int) {
origPrice := price
if price.LT(pool.minPrice) {
price = pool.minPrice
}
if price.GTE(pool.Price()) {
return zeroInt
}
// dx = (rx + transX) - P * (ry + transY)
dx := pool.xComp.Sub(price.Mul(pool.yComp))
if !dx.IsPositive() {
return zeroInt
} else if dx.GT(pool.rx.ToDec()) {
dx = pool.rx.ToDec()
}
utils.SafeMath(func() {
amt = dx.QuoTruncate(origPrice).TruncateInt() // dy = dx / P
if amt.GT(MaxCoinAmount) {
amt = MaxCoinAmount
}
}, func() {
amt = MaxCoinAmount
})
return
}
// SellAmountUnder returns the amount of sell orders for price less than
// or equal to given price.
func (pool *RangedPool) SellAmountUnder(price sdk.Dec, _ bool) (amt sdk.Int) {
if price.GT(pool.maxPrice) {
price = pool.maxPrice
}
if price.LTE(pool.Price()) {
return zeroInt
}
// dy = (ry + transY) - (rx + transX) / P
amt = pool.yComp.Sub(pool.xComp.QuoRoundUp(price)).TruncateInt()
if amt.GT(pool.ry) {
amt = pool.ry
}
if !amt.IsPositive() {
return zeroInt
}
return
}
// BuyAmountTo returns the amount of buy orders of the pool for price,
// where BuyAmountTo is used when the pool price is higher than the highest
// price of the order book.
func (pool *RangedPool) BuyAmountTo(price sdk.Dec) (amt sdk.Int) {
origPrice := price
if price.LT(pool.minPrice) {
price = pool.minPrice
}
if price.GTE(pool.Price()) {
return zeroInt
}
sqrtXComp := utils.DecApproxSqrt(pool.xComp)
sqrtYComp := utils.DecApproxSqrt(pool.yComp)
sqrtPrice := utils.DecApproxSqrt(price)
// dx = rx - (sqrt(P * (rx + transX) * (ry + transY)) - transX)
dx := pool.rx.ToDec().Sub(sqrtPrice.Mul(sqrtXComp.Mul(sqrtYComp)).Sub(pool.transX))
if !dx.IsPositive() {
return zeroInt
} else if dx.GT(pool.rx.ToDec()) {
dx = pool.rx.ToDec()
}
utils.SafeMath(func() {
amt = dx.QuoTruncate(origPrice).TruncateInt() // dy = dx / P
if amt.GT(MaxCoinAmount) {
amt = MaxCoinAmount
}
}, func() {
amt = MaxCoinAmount
})
return
}
// SellAmountTo returns the amount of sell orders of the pool for price,
// where SellAmountTo is used when the pool price is lower than the lowest
// price of the order book.
func (pool *RangedPool) SellAmountTo(price sdk.Dec) (amt sdk.Int) {
if price.GT(pool.maxPrice) {
price = pool.maxPrice
}
if price.LTE(pool.Price()) {
return zeroInt
}
sqrtXComp := utils.DecApproxSqrt(pool.xComp)
sqrtYComp := utils.DecApproxSqrt(pool.yComp)
sqrtPrice := utils.DecApproxSqrt(price)
// dy = ry - (sqrt((x + transX) * (y + transY) / P) - b)
amt = pool.ry.ToDec().Sub(sqrtXComp.Mul(sqrtYComp).QuoRoundUp(sqrtPrice).Sub(pool.transY)).TruncateInt()
if amt.GT(pool.ry) {
amt = pool.ry
}
if !amt.IsPositive() {
return zeroInt
}
return
}
func (pool *RangedPool) Clone() Pool {
return &RangedPool{
rx: pool.rx,
ry: pool.ry,
ps: pool.ps,
transX: pool.transX,
transY: pool.transY,
xComp: pool.xComp,
yComp: pool.yComp,
minPrice: pool.minPrice,
maxPrice: pool.maxPrice,
}
}
// Deposit returns accepted x and y coin amount and minted pool coin amount
// when someone deposits x and y coins.
func Deposit(rx, ry, ps, x, y sdk.Int) (ax, ay, pc sdk.Int) {
// Calculate accepted amount and minting amount.
// Note that we take as many coins as possible(by ceiling numbers)
// from depositor and mint as little coins as possible.
utils.SafeMath(func() {
rx, ry := rx.ToDec(), ry.ToDec()
ps := ps.ToDec()
// pc = floor(ps * min(x / rx, y / ry))
var ratio sdk.Dec
switch {
case rx.IsZero():
ratio = y.ToDec().QuoTruncate(ry)
case ry.IsZero():
ratio = x.ToDec().QuoTruncate(rx)
default:
ratio = sdk.MinDec(
x.ToDec().QuoTruncate(rx),
y.ToDec().QuoTruncate(ry),
)
}
pc = ps.MulTruncate(ratio).TruncateInt()
mintProportion := pc.ToDec().Quo(ps) // pc / ps
ax = rx.Mul(mintProportion).Ceil().TruncateInt() // ceil(rx * mintProportion)
ay = ry.Mul(mintProportion).Ceil().TruncateInt() // ceil(ry * mintProportion)
}, func() {
ax, ay, pc = sdk.ZeroInt(), sdk.ZeroInt(), sdk.ZeroInt()
})
return
}
// Withdraw returns withdrawn x and y coin amount when someone withdraws
// pc pool coin.
// Withdraw also takes care of the fee rate.
func Withdraw(rx, ry, ps, pc sdk.Int, feeRate sdk.Dec) (x, y sdk.Int) {
if pc.Equal(ps) {
// Redeeming the last pool coin - give all remaining rx and ry.
x = rx
y = ry
return
}
utils.SafeMath(func() {
proportion := pc.ToDec().QuoTruncate(ps.ToDec()) // pc / ps
multiplier := sdk.OneDec().Sub(feeRate) // 1 - feeRate
x = rx.ToDec().MulTruncate(proportion).MulTruncate(multiplier).TruncateInt() // floor(rx * proportion * multiplier)
y = ry.ToDec().MulTruncate(proportion).MulTruncate(multiplier).TruncateInt() // floor(ry * proportion * multiplier)
}, func() {
x, y = sdk.ZeroInt(), sdk.ZeroInt()
})
return
}
func DeriveTranslation(rx, ry sdk.Int, minPrice, maxPrice sdk.Dec) (transX, transY sdk.Dec) {
sqrt := utils.DecApproxSqrt
// M = minPrice, L = maxPrice
rxDec, ryDec := rx.ToDec(), ry.ToDec()
sqrtM := sqrt(minPrice)
sqrtL := sqrt(maxPrice)
var sqrtP sdk.Dec
switch {
case rxDec.IsZero(): // y asset single pool
sqrtP = sqrtM
case ryDec.IsZero(): // x asset single pool
sqrtP = sqrtL
case rxDec.Quo(ryDec).IsZero(): // y asset single pool
sqrtP = sqrtM
case ryDec.Quo(rxDec).IsZero(): // x asset single pool
sqrtP = sqrtL
default: // normal pool
// sqrtXOverY = sqrt(rx/ry)
sqrtXOverY := sqrt(rxDec.Quo(ryDec))
// alpha = sqrt(M)/sqrt(rx/ry) - sqrt(rx/ry)/sqrt(L)
alpha := sqrtM.Quo(sqrtXOverY).Sub(sqrtXOverY.Quo(sqrtL))
// sqrtP = sqrt(P) = {(alpha + sqrt(alpha^2 + 4)) / 2} * sqrt(rx/ry)
sqrtP = alpha.Add(sqrt(alpha.Power(2).Add(fourDec))).QuoInt64(2).Mul(sqrtXOverY)
}
var sqrtK sdk.Dec
if !sqrtP.Equal(sqrtM) {
// sqrtK = sqrt(K) = rx / (sqrt(P) - sqrt(M))
sqrtK = rxDec.Quo(sqrtP.Sub(sqrtM))
}
if !sqrtP.Equal(sqrtL) {
// sqrtK2 = sqrt(K') = ry / (1/sqrt(P) - 1/sqrt(L))
sqrtK2 := ryDec.Quo(inv(sqrtP).Sub(inv(sqrtL)))
if sqrtK.IsNil() { // P == M
sqrtK = sqrtK2
} else {
p := sqrtP.Power(2)
p1 := rxDec.Add(sqrtK.Mul(sqrtM)).Quo(ryDec.Add(sqrtK.Quo(sqrtL)))
p2 := rxDec.Add(sqrtK2.Mul(sqrtM)).Quo(ryDec.Add(sqrtK2.Quo(sqrtL)))
if p.Sub(p1).Abs().GT(p.Sub(p2).Abs()) {
sqrtK = sqrtK2
}
}
}
transX = sqrtK.Mul(sqrtM)
transY = sqrtK.Quo(sqrtL)
return
}
func PoolOrders(pool Pool, orderer Orderer, lowestPrice, highestPrice sdk.Dec, tickPrec int) []Order {
return append(
PoolBuyOrders(pool, orderer, lowestPrice, highestPrice, tickPrec),
PoolSellOrders(pool, orderer, lowestPrice, highestPrice, tickPrec)...)
}
func PoolBuyOrders(pool Pool, orderer Orderer, lowestPrice, highestPrice sdk.Dec, tickPrec int) (orders []Order) {
defer func() {
if r := recover(); r != nil {
orders = nil
}
}()
poolPrice := pool.Price()
if poolPrice.LTE(lowestPrice) {
return nil
}
tmpPool := pool.Clone()
placeOrder := func(price sdk.Dec, amt sdk.Int, derive bool) {
orders = append(orders, orderer.Order(Buy, price, amt))
rx, ry := tmpPool.Balances()
rx = rx.Sub(price.MulInt(amt).Ceil().TruncateInt()) // quote coin ceiling
ry = ry.Add(amt)
tmpPool.SetBalances(rx, ry, derive)
}
if poolPrice.GT(highestPrice) {
amt := tmpPool.BuyAmountTo(highestPrice)
if amt.GTE(MinCoinAmount) {
placeOrder(highestPrice, amt, true)
}
}
tick := PriceToDownTick(sdk.MinDec(highestPrice, tmpPool.Price()), tickPrec)
for tick.GTE(lowestPrice) {
amt := tmpPool.BuyAmountOver(tick, true)
if amt.LT(MinCoinAmount) {
tick = DownTick(tick, tickPrec) // TODO: check if the tick is the lowest possible tick
continue
}
placeOrder(tick, amt, false)
rx, _ := tmpPool.Balances()
if !rx.IsPositive() {
break
}
tick = PriceToDownTick(tick.Mul(oneDec.Sub(poolOrderPriceGapRatio(poolPrice, tick))), tickPrec)
}
return orders
}
func PoolSellOrders(pool Pool, orderer Orderer, lowestPrice, highestPrice sdk.Dec, tickPrec int) (orders []Order) {
defer func() {
if r := recover(); r != nil {
orders = nil
}
}()
poolPrice := pool.Price()
if poolPrice.GTE(highestPrice) {
return nil
}
tmpPool := pool.Clone()
placeOrder := func(price sdk.Dec, amt sdk.Int, derive bool) {
orders = append(orders, orderer.Order(Sell, price, amt))
rx, ry := tmpPool.Balances()
rx = rx.Add(price.MulInt(amt).TruncateInt()) // quote coin truncation
ry = ry.Sub(amt)
tmpPool.SetBalances(rx, ry, derive)
}
if poolPrice.LT(lowestPrice) {
amt := tmpPool.SellAmountTo(lowestPrice)
if amt.GTE(MinCoinAmount) && lowestPrice.MulInt(amt).TruncateInt().IsPositive() {
placeOrder(lowestPrice, amt, true)
}
}
tick := PriceToUpTick(sdk.MaxDec(lowestPrice, tmpPool.Price()), tickPrec)
for tick.LTE(highestPrice) {
amt := tmpPool.SellAmountUnder(tick, true)
if amt.LT(MinCoinAmount) || tick.MulInt(amt).TruncateInt().IsZero() {
tick = UpTick(tick, tickPrec)
continue
}
placeOrder(tick, amt, false)
_, ry := tmpPool.Balances()
if !ry.GT(MinCoinAmount) {
break
}
tick = PriceToUpTick(tick.Mul(oneDec.Add(poolOrderPriceGapRatio(poolPrice, tick))), tickPrec)
}
return orders
}
// InitialPoolCoinSupply returns ideal initial pool coin minting amount.
func InitialPoolCoinSupply(x, y sdk.Int) sdk.Int {
cx := len(x.BigInt().Text(10)) - 1 // characteristic of x
cy := len(y.BigInt().Text(10)) - 1 // characteristic of y
c := ((cx + 1) + (cy + 1) + 1) / 2 // ceil(((cx + 1) + (cy + 1)) / 2)
res := big.NewInt(10)
res.Exp(res, big.NewInt(int64(c)), nil) // 10^c
return sdk.NewIntFromBigInt(res)
}