/
sort_orders.go
142 lines (114 loc) · 4.61 KB
/
sort_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
package types
import (
"fmt"
"sort"
)
// SortedLongTermOrderPlacements is type alias for `[]LongTermOrderPlacement` which supports deterministic
// sorting. Long Term order placements are first ordered by block height, followed by
// transaction index.
// This list assumes that all order placements are unique and will panic if
// any two order placements have the same block height and transaction index.
type SortedLongTermOrderPlacements []LongTermOrderPlacement
// The below methods are required to implement `sort.Interface` for sorting using the sort package.
var _ sort.Interface = SortedLongTermOrderPlacements{}
func (s SortedLongTermOrderPlacements) Len() int {
return len(s)
}
func (s SortedLongTermOrderPlacements) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s SortedLongTermOrderPlacements) Less(i, j int) bool {
si := s[i]
sj := s[j]
siPlacementIndex := si.GetPlacementIndex()
sjPlacementIndex := sj.GetPlacementIndex()
if siPlacementIndex.BlockHeight != sjPlacementIndex.BlockHeight {
return siPlacementIndex.BlockHeight < sjPlacementIndex.BlockHeight
}
if siPlacementIndex.TransactionIndex != sjPlacementIndex.TransactionIndex {
return siPlacementIndex.TransactionIndex < sjPlacementIndex.TransactionIndex
}
panic(
fmt.Errorf(
"Less: long term order placements (%v) and (%v) have the same block height and transaction index",
si,
sj,
),
)
}
// StatefulOrderPlacement represents any type of order placement that is stored in state.
// An stateful order placement exposes a `TransactionOrdering` transaction index that is used when
// sorting the stateful order placements. Ordering is first by block height, then transaction index.
// The following types are currently supported:
// - LongTermOrderPlacement
// - ConditionalOrderPlacement
type StatefulOrderPlacement interface {
GetTransactionIndex() TransactionOrdering
}
// GetTransactionIndex returns a LongTermOrderPlacement's placement index.
func (orderPlacement *LongTermOrderPlacement) GetTransactionIndex() TransactionOrdering {
return orderPlacement.GetPlacementIndex()
}
// GetTransactionIndex returns a ConditionalOrderPlacement's TransactionOrdering.
// If the conditional order is triggered, the trigger index is used. If the conditional order
// is placed but not triggered, the placement index is used.
func (orderPlacement *ConditionalOrderPlacement) GetTransactionIndex() TransactionOrdering {
if orderPlacement.GetTriggerIndex() != nil {
return *orderPlacement.GetTriggerIndex()
}
return orderPlacement.GetPlacementIndex()
}
// SortedStatefulOrderPlacement is type alias for `[]StatefulOrderPlacement` which supports deterministic
// sorting. Stateful orders must expose one TransactionOrdering to be sorted on. Ordering is first by
// block height, then transaction index.
// The following types are currently supported:
// - LongTermOrderPlacement
// - ConditionalOrderPlacement
// This list assumes that all order placements are unique and will panic if
// any two stateful order placements have the same TransactionOrdering.
type SortedStatefulOrderPlacement []StatefulOrderPlacement
// The below methods are required to implement `sort.Interface` for sorting using the sort package.
var _ sort.Interface = SortedStatefulOrderPlacement{}
func (s SortedStatefulOrderPlacement) Len() int {
return len(s)
}
func (s SortedStatefulOrderPlacement) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s SortedStatefulOrderPlacement) Less(i, j int) bool {
si := s[i]
sj := s[j]
siPlacementIndex := si.GetTransactionIndex()
sjPlacementIndex := sj.GetTransactionIndex()
if siPlacementIndex.BlockHeight != sjPlacementIndex.BlockHeight {
return siPlacementIndex.BlockHeight < sjPlacementIndex.BlockHeight
}
if siPlacementIndex.TransactionIndex != sjPlacementIndex.TransactionIndex {
return siPlacementIndex.TransactionIndex < sjPlacementIndex.TransactionIndex
}
panic(
fmt.Errorf(
"Less: stateful order placements (%v) and (%v) have the same TransactionOrdering",
si,
sj,
),
)
}
// SortedClobPairId is type alias for `[]ClobPairId` which supports deterministic
// sorting of clob pair Ids.
// This list assumes that all clob pair ids are unique and will panic if
// any two order placements have the same block height and transaction index.
type SortedClobPairId []ClobPairId
// The below methods are required to implement `sort.Interface` for sorting using the sort package.
var _ sort.Interface = SortedClobPairId{}
func (s SortedClobPairId) Len() int {
return len(s)
}
func (s SortedClobPairId) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s SortedClobPairId) Less(i, j int) bool {
si := s[i]
sj := s[j]
return uint32(si) < uint32(sj)
}