-
Notifications
You must be signed in to change notification settings - Fork 74
/
keys.go
300 lines (244 loc) · 8.51 KB
/
keys.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
package types
import (
"errors"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/neutron-org/neutron/v2/x/dex/utils"
)
const (
// ModuleName defines the module name
ModuleName = "dex"
// StoreKey defines the primary module store key
StoreKey = ModuleName
// RouterKey is the message route for slashing
RouterKey = ModuleName
// QuerierRoute defines the module's query routing key
QuerierRoute = ModuleName
// MemStoreKey defines the in-memory store key
MemStoreKey = "mem_dex"
)
const (
// TickLiquidityKeyPrefix is the prefix to retrieve all TickLiquidity
TickLiquidityKeyPrefix = "TickLiquidity/value/"
// LimitOrderTrancheUserKeyPrefix is the prefix to retrieve all LimitOrderTrancheUser
LimitOrderTrancheUserKeyPrefix = "LimitOrderTrancheUser/value"
// InactiveLimitOrderTrancheKeyPrefix is the prefix to retrieve all InactiveLimitOrderTranche
InactiveLimitOrderTrancheKeyPrefix = "InactiveLimitOrderTranche/value/"
// LimitOrderExpirationKeyPrefix is the prefix to retrieve all LimitOrderExpiration
LimitOrderExpirationKeyPrefix = "LimitOrderExpiration/value/"
// PoolIDKeyPrefix is the prefix to retrieve all PoolIds or retrieve a specific pool by pair+tick+fee
PoolIDKeyPrefix = "Pool/id/"
// PoolMetadataKeyPrefix is the prefix to retrieve all PoolMetadata
PoolMetadataKeyPrefix = "PoolMetadata/value/"
// PoolCountKeyPrefix is the prefix to retrieve the Pool count
PoolCountKeyPrefix = "Pool/count/"
// ParamsKey is the prefix to retrieve params
ParamsKey = "Params/value/"
)
func KeyPrefix(p string) []byte {
key := []byte(p)
key = append(key, []byte("/")...)
return key
}
func TickIndexToBytes(tickTakerToMaker int64) []byte {
key := make([]byte, 9)
if tickTakerToMaker < 0 {
copy(key[1:], sdk.Uint64ToBigEndian(uint64(tickTakerToMaker)))
} else {
copy(key[:1], []byte{0x01})
copy(key[1:], sdk.Uint64ToBigEndian(uint64(tickTakerToMaker)))
}
return key
}
func BytesToTickIndex(bz []byte) (int64, error) {
if len(bz) != 9 {
return 0, errors.New("input should be 9 bytes long")
}
isNegative := bz[0] == 0
tickTakerToMaker := sdk.BigEndianToUint64(bz[1:])
if isNegative {
return int64(-tickTakerToMaker), nil
}
// else
return int64(tickTakerToMaker), nil
}
// LimitOrderTrancheUserKey returns the store key to retrieve a LimitOrderTrancheUser from the index fields
func LimitOrderTrancheUserKey(address, trancheKey string) []byte {
var key []byte
addressBytes := []byte(address)
key = append(key, addressBytes...)
key = append(key, []byte("/")...)
trancheKeyBytes := []byte(trancheKey)
key = append(key, trancheKeyBytes...)
key = append(key, []byte("/")...)
return key
}
func LimitOrderTrancheUserAddressPrefix(address string) []byte {
key := KeyPrefix(LimitOrderTrancheUserKeyPrefix)
addressBytes := []byte(address)
key = append(key, addressBytes...)
key = append(key, []byte("/")...)
return key
}
func TimeBytes(timestamp time.Time) []byte {
unixMs := uint64(timestamp.UnixMilli())
str := utils.Uint64ToSortableString(unixMs)
return []byte(str)
}
func TickLiquidityLimitOrderPrefix(
tradePairID *TradePairID,
tickIndexTakerTomMaker int64,
) []byte {
key := KeyPrefix(TickLiquidityKeyPrefix)
pairIDBytes := []byte(tradePairID.MustPairID().CanonicalString())
key = append(key, pairIDBytes...)
key = append(key, []byte("/")...)
makerDenomBytes := []byte(tradePairID.MakerDenom)
key = append(key, makerDenomBytes...)
key = append(key, []byte("/")...)
tickIndexBytes := TickIndexToBytes(tickIndexTakerTomMaker)
key = append(key, tickIndexBytes...)
key = append(key, []byte("/")...)
liquidityTypeBytes := []byte(LiquidityTypeLimitOrder)
key = append(key, liquidityTypeBytes...)
key = append(key, []byte("/")...)
return key
}
func TickLiquidityPrefix(tradePairID *TradePairID) []byte {
var key []byte
key = append(KeyPrefix(TickLiquidityKeyPrefix), KeyPrefix(tradePairID.MustPairID().CanonicalString())...)
key = append(key, KeyPrefix(tradePairID.MakerDenom)...)
return key
}
func LimitOrderExpirationKey(
goodTilDate time.Time,
trancheRef []byte,
) []byte {
var key []byte
goodTilDateBytes := TimeBytes(goodTilDate)
key = append(key, goodTilDateBytes...)
key = append(key, []byte("/")...)
key = append(key, trancheRef...)
key = append(key, []byte("/")...)
return key
}
func PoolIDKey(
pairID *PairID,
tickIndex int64,
fee uint64,
) []byte {
key := []byte(pairID.CanonicalString())
key = append(key, []byte("/")...)
tickIndexBytes := TickIndexToBytes(tickIndex)
key = append(key, tickIndexBytes...)
key = append(key, []byte("/")...)
feeBytes := sdk.Uint64ToBigEndian(fee)
key = append(key, feeBytes...)
key = append(key, []byte("/")...)
return key
}
// Deposit Event Attributes
const (
DepositEventKey = "DepositLP"
DepositEventCreator = "Creator"
DepositEventToken0 = "TokenZero"
DepositEventToken1 = "TokenOne"
DepositEventPrice = "TickIndex"
DepositEventFee = "Fee"
DepositEventReceiver = "Receiver"
DepositEventReserves0Deposited = "ReservesZeroDeposited"
DepositEventReserves1Deposited = "ReservesOneDeposited"
DepositEventSharesMinted = "SharesMinted"
)
// Withdraw Event Attributes
const (
WithdrawEventKey = "WithdrawLP"
WithdrawEventCreator = "Creator"
WithdrawEventToken0 = "TokenZero"
WithdrawEventToken1 = "TokenOne"
WithdrawEventPrice = "TickIndex"
WithdrawEventFee = "Fee"
WithdrawEventReceiver = "Receiver"
WithdrawEventReserves0Withdrawn = "ReservesZeroWithdrawn"
WithdrawEventReserves1Withdrawn = "ReservesOneWithdrawn"
WithdrawEventSharesRemoved = "SharesRemoved"
)
// Multihop-Swap Event Attributes
const (
MultihopSwapEventKey = "MultihopSwap"
MultihopSwapEventCreator = "Creator"
MultihopSwapEventReceiver = "Receiver"
MultihopSwapEventTokenIn = "TokenIn"
MultihopSwapEventTokenOut = "TokenOut"
MultihopSwapEventAmountIn = "AmountIn"
MultihopSwapEventAmountOut = "AmountOut"
MultihopSwapEventRoute = "Route"
)
// Place LimitOrder Event Attributes
const (
PlaceLimitOrderEventKey = "PlaceLimitOrder"
PlaceLimitOrderEventCreator = "Creator"
PlaceLimitOrderEventReceiver = "Receiver"
PlaceLimitOrderEventToken0 = "TokenZero"
PlaceLimitOrderEventToken1 = "TokenOne"
PlaceLimitOrderEventTokenIn = "TokenIn"
PlaceLimitOrderEventTokenOut = "TokenOut"
PlaceLimitOrderEventAmountIn = "AmountIn"
PlaceLimitOrderEventLimitTick = "LimitTick"
PlaceLimitOrderEventOrderType = "OrderType"
PlaceLimitOrderEventShares = "Shares"
PlaceLimitOrderEventTrancheKey = "TrancheKey"
)
// Withdraw LimitOrder Event Attributes
const (
WithdrawFilledLimitOrderEventKey = "WithdrawLimitOrder"
WithdrawFilledLimitOrderEventCreator = "Creator"
WithdrawFilledLimitOrderEventToken0 = "TokenZero"
WithdrawFilledLimitOrderEventToken1 = "TokenOne"
WithdrawFilledLimitOrderEventTokenIn = "TokenIn"
WithdrawFilledLimitOrderEventTokenOut = "TokenOut"
WithdrawFilledLimitOrderEventTrancheKey = "TrancheKey"
WithdrawFilledLimitOrderEventAmountOut = "AmountOut"
)
// Cancel LimitOrder Event Attributes
const (
CancelLimitOrderEventKey = "CancelLimitOrder"
CancelLimitOrderEventCreator = "Creator"
CancelLimitOrderEventToken0 = "TokenZero"
CancelLimitOrderEventToken1 = "TokenOne"
CancelLimitOrderEventTokenIn = "TokenIn"
CancelLimitOrderEventTokenOut = "TokenOut"
CancelLimitOrderEventTrancheKey = "TrancheKey"
CancelLimitOrderEventAmountOut = "AmountOut"
)
// Tick Update Event Attributes
const (
EventTypeTickUpdate = "TickUpdate"
TickUpdateEventKey = "TickUpdate"
TickUpdateEventToken0 = "TokenZero"
TickUpdateEventToken1 = "TokenOne"
TickUpdateEventTokenIn = "TokenIn"
TickUpdateEventTickIndex = "TickIndex"
TickUpdateEventFee = "Fee"
TickUpdateEventTrancheKey = "TrancheKey"
TickUpdateEventReserves = "Reserves"
)
const (
EventTypeGoodTilPurgeHitGasLimit = "GoodTilPurgeHitGasLimit"
GoodTilPurgeHitGasLimitEventGas = "Gas"
)
const (
// NOTE: have to add letter so that LP deposits are indexed ahead of LimitOrders
LiquidityTypePoolReserves = "A_PoolDeposit"
LiquidityTypeLimitOrder = "B_LODeposit"
)
func JITGoodTilTime() time.Time {
return time.Time{}
}
const (
// NOTE: This number is based current cost of all operations in EndBlock,
// if that changes this value must be updated to ensure there is enough
// remaining gas (weak proxy for timeoutPrepareProposal) to complete endBlock
GoodTilPurgeGasBuffer = 50_000
ExpiringLimitOrderGas = 10_000
)