-
Notifications
You must be signed in to change notification settings - Fork 55
/
keys.go
90 lines (73 loc) · 2.57 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
package types
import (
"bytes"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
utils "github.com/crescent-network/crescent/v5/types"
)
const (
// ModuleName defines the module name
ModuleName = "lpfarm"
// 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 (
LastBlockTimeKey = []byte{0xd0}
LastPlanIdKey = []byte{0xd1}
NumPrivatePlansKey = []byte{0xd2}
PlanKeyPrefix = []byte{0xd3}
FarmKeyPrefix = []byte{0xd4}
PositionKeyPrefix = []byte{0xd5}
HistoricalRewardsKeyPrefix = []byte{0xd6}
)
func GetPlanKey(id uint64) []byte {
return append(PlanKeyPrefix, sdk.Uint64ToBigEndian(id)...)
}
func GetFarmKey(denom string) []byte {
return append(FarmKeyPrefix, denom...)
}
func GetPositionKey(farmerAddr sdk.AccAddress, denom string) []byte {
return append(append(PositionKeyPrefix, address.MustLengthPrefix(farmerAddr)...), denom...)
}
// GetPositionsByFarmerKeyPrefix returns a key prefix for iterating through
// all the positions owned by a farmer.
func GetPositionsByFarmerKeyPrefix(farmerAddr sdk.AccAddress) []byte {
return append(PositionKeyPrefix, address.MustLengthPrefix(farmerAddr)...)
}
func GetHistoricalRewardsKey(denom string, period uint64) []byte {
return append(append(HistoricalRewardsKeyPrefix, utils.LengthPrefixString(denom)...), sdk.Uint64ToBigEndian(period)...)
}
// GetHistoricalRewardsByDenomKeyPrefix returns a key prefix for iterating
// through all the historical rewards belong to a denom.
func GetHistoricalRewardsByDenomKeyPrefix(denom string) []byte {
return append(HistoricalRewardsKeyPrefix, utils.LengthPrefixString(denom)...)
}
func ParseFarmKey(key []byte) (denom string) {
if !bytes.HasPrefix(key, FarmKeyPrefix) {
panic("key does not have proper prefix")
}
denom = string(key[1:])
return
}
func ParsePositionKey(key []byte) (farmerAddr sdk.AccAddress, denom string) {
if !bytes.HasPrefix(key, PositionKeyPrefix) {
panic("key does not have proper prefix")
}
farmerAddrLen := key[1]
farmerAddr = key[2 : 2+farmerAddrLen]
denom = string(key[2+farmerAddrLen:])
return
}
func ParseHistoricalRewardsKey(key []byte) (denom string, period uint64) {
if !bytes.HasPrefix(key, HistoricalRewardsKeyPrefix) {
panic("key does not have proper prefix")
}
denomLen := key[1]
denom = string(key[2 : 2+denomLen])
period = sdk.BigEndianToUint64(key[2+denomLen:])
return
}