-
Notifications
You must be signed in to change notification settings - Fork 55
/
orderbook.go
162 lines (148 loc) · 5.04 KB
/
orderbook.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
package types
import (
"fmt"
"sort"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/crescent-network/crescent/v4/x/liquidity/amm"
)
func OrderBookBasePrice(ov amm.OrderView, tickPrec int) (sdk.Dec, bool) {
highestBuyPrice, foundHighestBuyPrice := ov.HighestBuyPrice()
lowestSellPrice, foundLowestSellPrice := ov.LowestSellPrice()
switch {
case foundHighestBuyPrice && foundLowestSellPrice:
return amm.RoundPrice(highestBuyPrice.Add(lowestSellPrice).QuoInt64(2), tickPrec), true
case foundHighestBuyPrice:
return highestBuyPrice, true
case foundLowestSellPrice:
return lowestSellPrice, true
default: // not found
return sdk.Dec{}, false
}
}
// OrderBookConfig defines configuration parameter for an order book response.
type OrderBookConfig struct {
PriceUnitPower int
MaxNumTicks int
}
func MakeOrderBookPairResponse(pairId uint64, ov *amm.OrderBookView, lowestPrice, highestPrice sdk.Dec, tickPrec int, configs ...OrderBookConfig) OrderBookPairResponse {
resp := OrderBookPairResponse{
PairId: pairId,
}
basePrice, found := OrderBookBasePrice(ov, tickPrec)
if !found {
return resp
}
resp.BasePrice = basePrice
ammTickPrec := amm.TickPrecision(tickPrec)
sort.Slice(configs, func(i, j int) bool {
return configs[i].PriceUnitPower < configs[j].PriceUnitPower
})
lowestPriceUnitMaxNumTicks := configs[0].MaxNumTicks
highestBuyPrice, foundHighestBuyPrice := ov.HighestBuyPrice()
lowestSellPrice, foundLowestSellPrice := ov.LowestSellPrice()
var smallestPriceUnit sdk.Dec
if foundLowestSellPrice {
currentPrice := lowestSellPrice
for i := 0; i < lowestPriceUnitMaxNumTicks && currentPrice.LTE(highestPrice); {
amtInclusive := ov.SellAmountOver(currentPrice, true)
amtExclusive := ov.SellAmountOver(currentPrice, false)
amt := amtInclusive.Sub(amtExclusive)
if amt.IsPositive() {
i++
if i == lowestPriceUnitMaxNumTicks {
break
}
}
if !amtExclusive.IsPositive() {
break
}
currentPrice = ammTickPrec.UpTick(currentPrice)
}
smallestPriceUnit = ammTickPrec.TickGap(currentPrice)
} else {
smallestPriceUnit = ammTickPrec.TickGap(highestBuyPrice)
}
for _, config := range configs {
priceUnit := smallestPriceUnit
for j := 0; j < config.PriceUnitPower; j++ {
priceUnit = priceUnit.MulInt64(10)
}
ob := OrderBookResponse{
PriceUnit: priceUnit,
Buys: nil,
Sells: nil,
}
if foundLowestSellPrice {
startPrice := FitPriceToTickGap(lowestSellPrice, priceUnit, false)
currentPrice := startPrice
accAmt := sdk.ZeroInt()
for j := 0; j < config.MaxNumTicks && currentPrice.LTE(highestPrice); {
amt := ov.SellAmountUnder(currentPrice, true).Sub(accAmt)
if amt.IsPositive() {
ob.Sells = append(ob.Sells, OrderBookTickResponse{
Price: currentPrice,
UserOrderAmount: amt,
PoolOrderAmount: sdk.ZeroInt(),
})
accAmt = accAmt.Add(amt)
j++
}
if !ov.SellAmountOver(currentPrice, false).IsPositive() {
break
}
currentPrice = currentPrice.Add(priceUnit)
}
// Reverse sell ticks.
for l, r := 0, len(ob.Sells)-1; l < r; l, r = l+1, r-1 {
ob.Sells[l], ob.Sells[r] = ob.Sells[r], ob.Sells[l]
}
}
if foundHighestBuyPrice {
startPrice := FitPriceToTickGap(highestBuyPrice, priceUnit, true)
currentPrice := startPrice
accAmt := sdk.ZeroInt()
for j := 0; j < config.MaxNumTicks && currentPrice.GTE(lowestPrice) && !currentPrice.IsNegative(); {
amt := ov.BuyAmountOver(currentPrice, true).Sub(accAmt)
if amt.IsPositive() {
ob.Buys = append(ob.Buys, OrderBookTickResponse{
Price: currentPrice,
UserOrderAmount: amt,
PoolOrderAmount: sdk.ZeroInt(),
})
accAmt = accAmt.Add(amt)
j++
}
if !ov.BuyAmountUnder(currentPrice, false).IsPositive() {
break
}
currentPrice = currentPrice.Sub(priceUnit)
}
}
resp.OrderBooks = append(resp.OrderBooks, ob)
}
return resp
}
// PrintOrderBookResponse prints out OrderBookResponse in human-readable form.
func PrintOrderBookResponse(ob OrderBookResponse, basePrice sdk.Dec) {
fmt.Println("+------------------------------------------------------------------------+")
for _, tick := range ob.Sells {
fmt.Printf("| %18s | %28s | |\n", tick.UserOrderAmount, tick.Price.String())
}
fmt.Println("|------------------------------------------------------------------------|")
fmt.Printf("| %28s |\n", basePrice.String())
fmt.Println("|------------------------------------------------------------------------|")
for _, tick := range ob.Buys {
fmt.Printf("| | %28s | %-18s |\n", tick.Price.String(), tick.UserOrderAmount)
}
fmt.Println("+------------------------------------------------------------------------+")
}
// FitPriceToTickGap fits price into given tick gap.
func FitPriceToTickGap(price, gap sdk.Dec, down bool) sdk.Dec {
b := price.BigInt()
b.Quo(b, gap.BigInt()).Mul(b, gap.BigInt())
tick := sdk.NewDecFromBigIntWithPrec(b, sdk.Precision)
if !down && !tick.Equal(price) {
tick = tick.Add(gap)
}
return tick
}