-
Notifications
You must be signed in to change notification settings - Fork 55
/
keys.go
84 lines (68 loc) · 2.73 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
package types
import (
"bytes"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
)
const (
// ModuleName is the name of the marketmaker module
ModuleName = "marketmaker"
// RouterKey is the message router key for the marketmaker module
RouterKey = ModuleName
// StoreKey is the default store key for the marketmaker module
StoreKey = ModuleName
// QuerierRoute is the querier route for the marketmaker module
QuerierRoute = ModuleName
)
// keys for marketmaker store prefixes
var (
MarketMakerKeyPrefix = []byte{0xc0}
MarketMakerIndexByPairIdKeyPrefix = []byte{0xc1}
DepositKeyPrefix = []byte{0xc2}
IncentiveKeyPrefix = []byte{0xc5}
)
// GetMarketMakerKey returns a key for a market maker record.
func GetMarketMakerKey(mmAddr sdk.AccAddress, pairId uint64) []byte {
return append(append(MarketMakerKeyPrefix, address.MustLengthPrefix(mmAddr)...), sdk.Uint64ToBigEndian(pairId)...)
}
// GetMarketMakerIndexByPairIdKey returns a key for a market maker record.
func GetMarketMakerIndexByPairIdKey(pairId uint64, mmAddress sdk.AccAddress) []byte {
return append(append(MarketMakerIndexByPairIdKeyPrefix, sdk.Uint64ToBigEndian(pairId)...), mmAddress...)
}
// GetDepositKey returns a key for a market maker record.
func GetDepositKey(mmAddr sdk.AccAddress, pairId uint64) []byte {
return append(append(DepositKeyPrefix, address.MustLengthPrefix(mmAddr)...), sdk.Uint64ToBigEndian(pairId)...)
}
// GetIncentiveKey returns kv indexing key of the incentive
func GetIncentiveKey(mmAddr sdk.AccAddress) []byte {
return append(IncentiveKeyPrefix, mmAddr...)
}
// GetMarketMakerByAddrPrefix returns a key prefix used to iterate
// market makers by a address.
func GetMarketMakerByAddrPrefix(mmAddr sdk.AccAddress) []byte {
return append(MarketMakerKeyPrefix, address.MustLengthPrefix(mmAddr)...)
}
// GetMarketMakerByPairIdPrefix returns a key prefix used to iterate
// market makers by a pair id.
func GetMarketMakerByPairIdPrefix(pairId uint64) []byte {
return append(MarketMakerIndexByPairIdKeyPrefix, sdk.Uint64ToBigEndian(pairId)...)
}
// ParseMarketMakerIndexByPairIdKey parses a market maker index by pair id key.
func ParseMarketMakerIndexByPairIdKey(key []byte) (pairId uint64, mmAddr sdk.AccAddress) {
if !bytes.HasPrefix(key, MarketMakerIndexByPairIdKeyPrefix) {
panic("key does not have proper prefix")
}
pairId = sdk.BigEndianToUint64(key[1:9])
mmAddr = key[9:]
return
}
// ParseDepositKey parses a deposit key.
func ParseDepositKey(key []byte) (mmAddr sdk.AccAddress, pairId uint64) {
if !bytes.HasPrefix(key, DepositKeyPrefix) {
panic("key does not have proper prefix")
}
addrLen := key[1]
mmAddr = key[2 : 2+addrLen]
pairId = sdk.BigEndianToUint64(key[2+addrLen:])
return
}