forked from crescent-network/crescent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
240 lines (196 loc) · 8.99 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
package types
import (
"bytes"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
)
const (
// ModuleName defines the module name
ModuleName = "liquidity"
// 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
)
var (
LastPairIdKey = []byte{0xa0} // key for the latest pair id
LastPoolIdKey = []byte{0xa1} // key for the latest pool id
PairKeyPrefix = []byte{0xa5}
PairIndexKeyPrefix = []byte{0xa6}
PairsByDenomsIndexKeyPrefix = []byte{0xa7}
PoolKeyPrefix = []byte{0xab}
PoolByReserveAddressIndexKeyPrefix = []byte{0xac}
PoolsByPairIndexKeyPrefix = []byte{0xad}
DepositRequestKeyPrefix = []byte{0xb0}
DepositRequestIndexKeyPrefix = []byte{0xb4} // TODO: rearrange prefixes
WithdrawRequestKeyPrefix = []byte{0xb1}
WithdrawRequestIndexKeyPrefix = []byte{0xb5}
OrderKeyPrefix = []byte{0xb2}
OrderIndexKeyPrefix = []byte{0xb3}
NumMMOrdersKeyPrefix = []byte{0xb7}
)
// GetPairKey returns the store key to retrieve pair object from the pair id.
func GetPairKey(pairId uint64) []byte {
return append(PairKeyPrefix, sdk.Uint64ToBigEndian(pairId)...)
}
// GetPairIndexKey returns the index key to get a pair by denoms.
func GetPairIndexKey(baseCoinDenom, quoteCoinDenom string) []byte {
return append(append(PairIndexKeyPrefix, LengthPrefixString(baseCoinDenom)...), LengthPrefixString(quoteCoinDenom)...)
}
// GetPairsByDenomsIndexKey returns the index key to lookup pairs with given denoms.
func GetPairsByDenomsIndexKey(denomA, denomB string, pairId uint64) []byte {
return append(append(append(PairsByDenomsIndexKeyPrefix, LengthPrefixString(denomA)...), LengthPrefixString(denomB)...), sdk.Uint64ToBigEndian(pairId)...)
}
// GetPairsByDenomIndexKeyPrefix returns the index key prefix to lookup pairs with given denom.
func GetPairsByDenomIndexKeyPrefix(denomA string) []byte {
return append(PairsByDenomsIndexKeyPrefix, LengthPrefixString(denomA)...)
}
// GetPairsByDenomsIndexKeyPrefix returns the index key prefix to lookup pairs with given denoms.
func GetPairsByDenomsIndexKeyPrefix(denomA, denomB string) []byte {
return append(append(PairsByDenomsIndexKeyPrefix, LengthPrefixString(denomA)...), LengthPrefixString(denomB)...)
}
// GetPoolKey returns the store key to retrieve pool object from the pool id.
func GetPoolKey(poolId uint64) []byte {
return append(PoolKeyPrefix, sdk.Uint64ToBigEndian(poolId)...)
}
// GetPoolByReserveAddressIndexKey returns the index key to retrieve the particular pool.
func GetPoolByReserveAddressIndexKey(reserveAddr sdk.AccAddress) []byte {
return append(PoolByReserveAddressIndexKeyPrefix, address.MustLengthPrefix(reserveAddr)...)
}
// GetPoolsByPairIndexKey returns the index key to retrieve pool id that is used to iterate pools.
func GetPoolsByPairIndexKey(pairId, poolId uint64) []byte {
return append(append(PoolsByPairIndexKeyPrefix, sdk.Uint64ToBigEndian(pairId)...), sdk.Uint64ToBigEndian(poolId)...)
}
// GetPoolsByPairIndexKeyPrefix returns the store key to retrieve pool id to iterate pools.
func GetPoolsByPairIndexKeyPrefix(pairId uint64) []byte {
return append(PoolsByPairIndexKeyPrefix, sdk.Uint64ToBigEndian(pairId)...)
}
// GetDepositRequestKey returns the store key to retrieve deposit request object from the pool id and request id.
func GetDepositRequestKey(poolId, id uint64) []byte {
return append(append(DepositRequestKeyPrefix, sdk.Uint64ToBigEndian(poolId)...), sdk.Uint64ToBigEndian(id)...)
}
// GetDepositRequestIndexKey returns the index key to map deposit requests
// with a depositor.
func GetDepositRequestIndexKey(depositor sdk.AccAddress, poolId, reqId uint64) []byte {
return append(append(append(DepositRequestIndexKeyPrefix, address.MustLengthPrefix(depositor)...),
sdk.Uint64ToBigEndian(poolId)...), sdk.Uint64ToBigEndian(reqId)...)
}
// GetDepositRequestIndexKeyPrefix returns the index key prefix to iterate
// deposit requests by a depositor.
func GetDepositRequestIndexKeyPrefix(depositor sdk.AccAddress) []byte {
return append(DepositRequestIndexKeyPrefix, address.MustLengthPrefix(depositor)...)
}
// GetWithdrawRequestKey returns the store key to retrieve withdraw request object from the pool id and request id.
func GetWithdrawRequestKey(poolId, id uint64) []byte {
return append(append(WithdrawRequestKeyPrefix, sdk.Uint64ToBigEndian(poolId)...), sdk.Uint64ToBigEndian(id)...)
}
// GetWithdrawRequestIndexKey returns the index key to map withdraw requests
// with a withdrawer.
func GetWithdrawRequestIndexKey(withdrawer sdk.AccAddress, poolId, reqId uint64) []byte {
return append(append(append(WithdrawRequestIndexKeyPrefix, address.MustLengthPrefix(withdrawer)...),
sdk.Uint64ToBigEndian(poolId)...), sdk.Uint64ToBigEndian(reqId)...)
}
// GetWithdrawRequestIndexKeyPrefix returns the index key prefix to iterate
// withdraw requests by a withdrawer.
func GetWithdrawRequestIndexKeyPrefix(depositor sdk.AccAddress) []byte {
return append(WithdrawRequestIndexKeyPrefix, address.MustLengthPrefix(depositor)...)
}
// GetOrderKey returns the store key to retrieve order object from the pair id and request id.
func GetOrderKey(pairId, id uint64) []byte {
return append(append(OrderKeyPrefix, sdk.Uint64ToBigEndian(pairId)...), sdk.Uint64ToBigEndian(id)...)
}
// GetOrdersByPairKeyPrefix returns the store key to iterate orders by pair.
func GetOrdersByPairKeyPrefix(pairId uint64) []byte {
return append(OrderKeyPrefix, sdk.Uint64ToBigEndian(pairId)...)
}
// GetOrderIndexKey returns the index key to map orders with an orderer.
func GetOrderIndexKey(orderer sdk.AccAddress, pairId, orderId uint64) []byte {
return append(append(append(OrderIndexKeyPrefix, address.MustLengthPrefix(orderer)...),
sdk.Uint64ToBigEndian(pairId)...), sdk.Uint64ToBigEndian(orderId)...)
}
// GetOrderIndexKeyPrefix returns the index key prefix to iterate orders
// by an orderer.
func GetOrderIndexKeyPrefix(orderer sdk.AccAddress) []byte {
return append(OrderIndexKeyPrefix, address.MustLengthPrefix(orderer)...)
}
// GetNumMMOrdersKey returns the store key to retrieve the number of MM orders
// by orderer and pair id.
func GetNumMMOrdersKey(orderer sdk.AccAddress, pairId uint64) []byte {
return append(append(NumMMOrdersKeyPrefix, address.MustLengthPrefix(orderer)...), sdk.Uint64ToBigEndian(pairId)...)
}
// ParsePairsByDenomsIndexKey parses a pair by denom index key.
func ParsePairsByDenomsIndexKey(key []byte) (denomA, denomB string, pairId uint64) {
if !bytes.HasPrefix(key, PairsByDenomsIndexKeyPrefix) {
panic("key does not have proper prefix")
}
denomALen := key[1]
denomA = string(key[2 : 2+denomALen])
denomBLen := key[2+denomALen]
denomB = string(key[3+denomALen : 3+denomALen+denomBLen])
pairId = sdk.BigEndianToUint64(key[3+denomALen+denomBLen:])
return
}
// ParsePoolsByPairIndexKey parses a pool id from the index key.
func ParsePoolsByPairIndexKey(key []byte) (poolId uint64) {
if !bytes.HasPrefix(key, PoolsByPairIndexKeyPrefix) {
panic("key does not have proper prefix")
}
bytesLen := 8
poolId = sdk.BigEndianToUint64(key[1+bytesLen:])
return
}
// ParseDepositRequestIndexKey parses a deposit request index key.
func ParseDepositRequestIndexKey(key []byte) (depositor sdk.AccAddress, poolId, reqId uint64) {
if !bytes.HasPrefix(key, DepositRequestIndexKeyPrefix) {
panic("key does not have proper prefix")
}
addrLen := key[1]
depositor = key[2 : 2+addrLen]
poolId = sdk.BigEndianToUint64(key[2+addrLen : 2+addrLen+8])
reqId = sdk.BigEndianToUint64(key[2+addrLen+8:])
return
}
// ParseWithdrawRequestIndexKey parses a withdraw request index key.
func ParseWithdrawRequestIndexKey(key []byte) (withdrawer sdk.AccAddress, poolId, reqId uint64) {
if !bytes.HasPrefix(key, WithdrawRequestIndexKeyPrefix) {
panic("key does not have proper prefix")
}
addrLen := key[1]
withdrawer = key[2 : 2+addrLen]
poolId = sdk.BigEndianToUint64(key[2+addrLen : 2+addrLen+8])
reqId = sdk.BigEndianToUint64(key[2+addrLen+8:])
return
}
// ParseOrderIndexKey parses an order index key.
func ParseOrderIndexKey(key []byte) (orderer sdk.AccAddress, pairId, orderId uint64) {
if !bytes.HasPrefix(key, OrderIndexKeyPrefix) {
panic("key does not have proper prefix")
}
addrLen := key[1]
orderer = key[2 : 2+addrLen]
pairId = sdk.BigEndianToUint64(key[2+addrLen : 2+addrLen+8])
orderId = sdk.BigEndianToUint64(key[2+addrLen+8:])
return
}
// ParseNumMMOrdersKey parses NumMMOrdersKey.
func ParseNumMMOrdersKey(key []byte) (ordererAddr sdk.AccAddress, pairId uint64) {
if !bytes.HasPrefix(key, NumMMOrdersKeyPrefix) {
panic("key does not have proper prefix")
}
addrLen := key[1]
ordererAddr = key[2 : 2+addrLen]
pairId = sdk.BigEndianToUint64(key[2+addrLen:])
return
}
// LengthPrefixString returns length-prefixed bytes representation
// of a string.
func LengthPrefixString(s string) []byte {
bz := []byte(s)
bzLen := len(bz)
if bzLen == 0 {
return bz
}
return append([]byte{byte(bzLen)}, bz...)
}