-
Notifications
You must be signed in to change notification settings - Fork 791
/
orders.go
244 lines (218 loc) · 6.19 KB
/
orders.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
package types
import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sei-protocol/sei-chain/utils"
)
type OrderBook struct {
Contract ContractAddress
Pair Pair
Longs *CachedSortedOrderBookEntries
Shorts *CachedSortedOrderBookEntries
}
// entries are always sorted by prices in ascending order, regardless of side
type CachedSortedOrderBookEntries struct {
CachedEntries []OrderBookEntry
currentPtr int
currentChanged bool
loader func(ctx sdk.Context, startingPriceExclusive sdk.Dec, withLimit bool) []OrderBookEntry
setter func(sdk.Context, OrderBookEntry)
deleter func(sdk.Context, OrderBookEntry)
}
func NewCachedSortedOrderBookEntries(
loader func(ctx sdk.Context, startingPriceExclusive sdk.Dec, withLimit bool) []OrderBookEntry,
setter func(sdk.Context, OrderBookEntry),
deleter func(sdk.Context, OrderBookEntry),
) *CachedSortedOrderBookEntries {
return &CachedSortedOrderBookEntries{
CachedEntries: []OrderBookEntry{},
currentPtr: 0,
currentChanged: false,
loader: loader,
setter: setter,
deleter: deleter,
}
}
func (c *CachedSortedOrderBookEntries) load(ctx sdk.Context) {
var loaded []OrderBookEntry
if len(c.CachedEntries) == 0 {
loaded = c.loader(ctx, sdk.ZeroDec(), false)
} else {
loaded = c.loader(ctx, c.CachedEntries[len(c.CachedEntries)-1].GetOrderEntry().Price, true)
}
c.CachedEntries = append(c.CachedEntries, loaded...)
}
// Reduce quantity of the order book entry currently being pointed at by the specified quantity.
// Also remove/reduce allocations of the order book entry in FIFO order. If the order book entry
// does not have enough quantity to settle against, the returned `settled` value will equal to
// the quantity of the order book entry; otherwise it will equal to the specified quantity.
func (c *CachedSortedOrderBookEntries) SettleQuantity(_ sdk.Context, quantity sdk.Dec) (res []ToSettle, settled sdk.Dec) {
if quantity.IsZero() {
return []ToSettle{}, quantity
}
currentEntry := c.CachedEntries[c.currentPtr].GetOrderEntry()
c.currentChanged = true
if quantity.GTE(currentEntry.Quantity) {
res = utils.Map(currentEntry.Allocations, AllocationToSettle)
settled = currentEntry.Quantity
currentEntry.Quantity = sdk.ZeroDec()
currentEntry.Allocations = []*Allocation{}
return res, settled
}
settled = sdk.ZeroDec()
newFirstAllocationIdx := 0
for idx, a := range currentEntry.Allocations {
postSettle := settled.Add(a.Quantity)
if postSettle.LTE(quantity) {
settled = postSettle
res = append(res, AllocationToSettle(a))
} else {
newFirstAllocationIdx = idx
if settled.Equal(quantity) {
break
}
res = append(res, ToSettle{
OrderID: a.OrderId,
Account: a.Account,
Amount: quantity.Sub(settled),
})
a.Quantity = a.Quantity.Sub(quantity.Sub(settled))
settled = quantity
break
}
}
currentEntry.Quantity = currentEntry.Quantity.Sub(quantity)
currentEntry.Allocations = currentEntry.Allocations[newFirstAllocationIdx:]
return res, settled
}
// Discard all dirty changes and reload
func (c *CachedSortedOrderBookEntries) Refresh(ctx sdk.Context) {
c.CachedEntries = c.loader(ctx, sdk.ZeroDec(), false)
c.currentPtr = 0
c.currentChanged = false
}
func (c *CachedSortedOrderBookEntries) Flush(ctx sdk.Context) {
stop := c.currentPtr
if !c.currentChanged {
stop--
}
for i := 0; i <= stop; i++ {
if i >= len(c.CachedEntries) {
break
}
entry := c.CachedEntries[i]
if entry.GetOrderEntry().Quantity.IsZero() {
c.deleter(ctx, entry)
} else {
c.setter(ctx, entry)
}
}
c.CachedEntries = c.CachedEntries[c.currentPtr:]
c.currentPtr = 0
c.currentChanged = false
}
// Next will only move on to the next order if the current order quantity hits zero.
// So it should not be used for read-only iteration
func (c *CachedSortedOrderBookEntries) Next(ctx sdk.Context) OrderBookEntry {
for c.currentPtr < len(c.CachedEntries) && c.CachedEntries[c.currentPtr].GetOrderEntry().Quantity.IsZero() {
c.currentPtr++
c.currentChanged = false
}
if c.currentPtr >= len(c.CachedEntries) {
c.load(ctx)
// if nothing is loaded, we've reached the end
if c.currentPtr >= len(c.CachedEntries) {
return nil
}
}
return c.CachedEntries[c.currentPtr]
}
type OrderBookEntry interface {
GetPrice() sdk.Dec
GetOrderEntry() *OrderEntry
DeepCopy() OrderBookEntry
SetEntry(*OrderEntry)
SetPrice(sdk.Dec)
}
func (m *LongBook) SetPrice(p sdk.Dec) {
m.Price = p
}
type PriceStore struct {
Store prefix.Store
PriceKeys [][]byte
}
func (m *LongBook) GetPrice() sdk.Dec {
return m.Price
}
func (m *LongBook) GetOrderEntry() *OrderEntry {
return m.Entry
}
func (m *LongBook) SetEntry(newEntry *OrderEntry) {
m.Entry = newEntry
}
func (m *LongBook) DeepCopy() OrderBookEntry {
allocations := []*Allocation{}
for _, allo := range m.Entry.Allocations {
allocations = append(allocations, &Allocation{
OrderId: allo.OrderId,
Quantity: allo.Quantity,
Account: allo.Account,
})
}
newOrderEntry := OrderEntry{
Price: m.Entry.Price,
Quantity: m.Entry.Quantity,
PriceDenom: m.Entry.PriceDenom,
AssetDenom: m.Entry.AssetDenom,
Allocations: allocations,
}
return &LongBook{
Price: m.Price,
Entry: &newOrderEntry,
}
}
func (m *ShortBook) SetPrice(p sdk.Dec) {
m.Price = p
}
func (m *ShortBook) GetPrice() sdk.Dec {
return m.Price
}
func (m *ShortBook) GetOrderEntry() *OrderEntry {
return m.Entry
}
func (m *ShortBook) SetEntry(newEntry *OrderEntry) {
m.Entry = newEntry
}
func (m *ShortBook) DeepCopy() OrderBookEntry {
allocations := []*Allocation{}
for _, allo := range m.Entry.Allocations {
allocations = append(allocations, &Allocation{
OrderId: allo.OrderId,
Quantity: allo.Quantity,
Account: allo.Account,
})
}
newOrderEntry := OrderEntry{
Price: m.Entry.Price,
Quantity: m.Entry.Quantity,
PriceDenom: m.Entry.PriceDenom,
AssetDenom: m.Entry.AssetDenom,
Allocations: allocations,
}
return &ShortBook{
Price: m.Price,
Entry: &newOrderEntry,
}
}
type ToSettle struct {
OrderID uint64
Amount sdk.Dec
Account string
}
func AllocationToSettle(a *Allocation) ToSettle {
return ToSettle{
OrderID: a.OrderId,
Amount: a.Quantity,
Account: a.Account,
}
}