/
abci.go
213 lines (204 loc) · 5.78 KB
/
abci.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
package dex
import (
"fmt"
"strconv"
abci "github.com/tendermint/tendermint/abci/types"
sdk "github.com/aximchain/axc-cosmos-sdk/types"
app "github.com/aximchain/flash-node/common/types"
"github.com/aximchain/flash-node/plugins/dex/order"
"github.com/aximchain/flash-node/plugins/dex/store"
"github.com/aximchain/flash-node/plugins/dex/types"
"github.com/aximchain/flash-node/plugins/dex/utils"
)
const MaxDepthLevels = 1000 // matches UI requirement
const DefaultDepthLevels = 100 // matches UI requirement
func createAbciQueryHandler(keeper *DexKeeper, abciQueryPrefix string) app.AbciQueryHandler {
queryPrefix := abciQueryPrefix
return func(app app.ChainApp, req abci.RequestQuery, path []string) (res *abci.ResponseQuery) {
// expects at least two query path segments.
if path[0] != queryPrefix || len(path) < 2 {
return nil
}
switch path[1] {
case "pairs": // args: ["dex" or "dex-mini", "pairs", <offset>, <limit>]
if len(path) < 4 {
return &abci.ResponseQuery{
Code: uint32(sdk.CodeUnknownRequest),
Log: fmt.Sprintf(
"%s %s query requires offset and limit in the path",
queryPrefix, path[1]),
}
}
ctx := app.GetContextForCheckState()
pairs := listPairs(keeper, ctx, queryPrefix)
var offset, limit, end int
var err error
if len(pairs) == 0 {
pairs = make([]types.TradingPair, 0)
goto respond
}
offset, err = strconv.Atoi(path[2])
if err != nil || offset < 0 || offset > len(pairs)-1 {
return &abci.ResponseQuery{
Code: uint32(sdk.CodeInternal),
Log: "unable to parse offset",
}
}
limit, err = strconv.Atoi(path[3])
if err != nil || limit <= 0 {
return &abci.ResponseQuery{
Code: uint32(sdk.CodeInternal),
Log: "unable to parse limit",
}
}
end = offset + limit
if end > len(pairs) {
end = len(pairs)
}
if end <= 0 || end <= offset {
return &abci.ResponseQuery{
Code: uint32(sdk.CodeInternal),
Log: "malformed range",
}
}
respond:
bz, err := app.GetCodec().MarshalBinaryLengthPrefixed(
pairs[offset:end],
)
if err != nil {
return &abci.ResponseQuery{
Code: uint32(sdk.CodeInternal),
Log: err.Error(),
}
}
return &abci.ResponseQuery{
Code: uint32(sdk.ABCICodeOK),
Value: bz,
}
case "orderbook": // args: ["dex", "orderbook"]
if queryPrefix == DexMiniAbciQueryPrefix {
return &abci.ResponseQuery{
Code: uint32(sdk.ABCICodeOK),
Info: fmt.Sprintf(
"Unknown `%s` query path: %v",
queryPrefix, path),
}
}
if len(path) < 3 {
return &abci.ResponseQuery{
Code: uint32(sdk.CodeUnknownRequest),
Log: "OrderBook query requires the pair symbol and levels",
}
}
pair := path[2]
height := app.GetContextForCheckState().BlockHeight()
levelLimit := DefaultDepthLevels
if len(path) == 4 {
if l, err := strconv.Atoi(path[3]); err != nil {
return &abci.ResponseQuery{
Code: uint32(sdk.CodeUnknownRequest),
Log: fmt.Sprintf("OrderBook query requires valid int levels parameter: %v", err),
}
} else if l <= 0 || l > MaxDepthLevels {
return &abci.ResponseQuery{
Code: uint32(sdk.CodeUnknownRequest),
Log: "OrderBook query requires valid levels (>0 && <1000)",
}
} else {
levelLimit = l
}
}
levels, pendingMatch := keeper.GetOrderBookLevels(pair, levelLimit)
book := store.OrderBook{
Height: height,
Levels: levels,
PendingMatch: pendingMatch,
}
bz, err := app.GetCodec().MarshalBinaryLengthPrefixed(book)
if err != nil {
return &abci.ResponseQuery{
Code: uint32(sdk.CodeInternal),
Log: err.Error(),
}
}
return &abci.ResponseQuery{
Code: uint32(sdk.ABCICodeOK),
Value: bz,
}
case "openorders": // args: ["dex", "openorders", <pair>, <bech32Str>]
if queryPrefix == DexMiniAbciQueryPrefix {
return &abci.ResponseQuery{
Code: uint32(sdk.ABCICodeOK),
Info: fmt.Sprintf(
"Unknown `%s` query path: %v",
queryPrefix, path),
}
}
if len(path) < 4 {
return &abci.ResponseQuery{
Code: uint32(sdk.CodeUnknownRequest),
Log: "OpenOrders query requires the pair symbol and address",
}
}
// verify pair is legal
pair := path[2]
baseAsset, quoteAsset, err := utils.TradingPair2Assets(pair)
if err != nil {
return &abci.ResponseQuery{
Code: uint32(sdk.CodeInternal),
Log: "pair is not valid",
}
}
ctx := app.GetContextForCheckState()
if !keeper.PairMapper.Exists(ctx, baseAsset, quoteAsset) {
return &abci.ResponseQuery{
Code: uint32(sdk.CodeInternal),
Log: "pair is not listed",
}
}
bech32Str := path[3]
addr, err := sdk.AccAddressFromBech32(bech32Str)
if err != nil {
return &abci.ResponseQuery{
Code: uint32(sdk.CodeInternal),
Log: "address is not valid",
}
}
openOrders := keeper.GetOpenOrders(pair, addr)
bz, err := app.GetCodec().MarshalBinaryLengthPrefixed(openOrders)
if err != nil {
return &abci.ResponseQuery{
Code: uint32(sdk.CodeInternal),
Log: err.Error(),
}
}
return &abci.ResponseQuery{
Code: uint32(sdk.ABCICodeOK),
Value: bz,
}
default:
return &abci.ResponseQuery{
Code: uint32(sdk.ABCICodeOK),
Info: fmt.Sprintf(
"Unknown `%s` query path: %v",
queryPrefix, path),
}
}
}
}
func listPairs(keeper *DexKeeper, ctx sdk.Context, abciPrefix string) []types.TradingPair {
pairs := keeper.PairMapper.ListAllTradingPairs(ctx)
rs := make([]types.TradingPair, 0, len(pairs))
for _, pair := range pairs {
if keeper.GetPairType(pair.GetSymbol()) == order.PairType.MINI {
if abciPrefix == DexMiniAbciQueryPrefix {
rs = append(rs, pair)
}
} else {
if abciPrefix == DexAbciQueryPrefix {
rs = append(rs, pair)
}
}
}
return rs
}