-
Notifications
You must be signed in to change notification settings - Fork 3
/
types.go
65 lines (55 loc) · 1.88 KB
/
types.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
package v1
import (
"bytes"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
)
const (
// ModuleName is the name of the farming module
ModuleName = "farming"
// StoreKey is the default store key for the farming module
StoreKey = ModuleName
)
// keys for farming store prefixes
var (
QueuedStakingKeyPrefix = []byte{0x23}
QueuedStakingIndexKeyPrefix = []byte{0x24}
)
// GetQueuedStakingKey returns a key for a queued staking.
func GetQueuedStakingKey(stakingCoinDenom string, farmerAcc sdk.AccAddress) []byte {
return append(append(QueuedStakingKeyPrefix, LengthPrefixString(stakingCoinDenom)...), farmerAcc...)
}
// GetQueuedStakingIndexKey returns an indexing key for a queued staking.
func GetQueuedStakingIndexKey(farmerAcc sdk.AccAddress, stakingCoinDenom string) []byte {
return append(append(QueuedStakingIndexKeyPrefix, address.MustLengthPrefix(farmerAcc)...), []byte(stakingCoinDenom)...)
}
// ParseQueuedStakingKey parses a queued staking key.
func ParseQueuedStakingKey(key []byte) (stakingCoinDenom string, farmerAcc sdk.AccAddress) {
if !bytes.HasPrefix(key, QueuedStakingKeyPrefix) {
panic("key does not have proper prefix")
}
denomLen := key[1]
stakingCoinDenom = string(key[2 : 2+denomLen])
farmerAcc = key[2+denomLen:]
return
}
// ParseQueuedStakingIndexKey parses a queued staking index key.
func ParseQueuedStakingIndexKey(key []byte) (farmerAcc sdk.AccAddress, stakingCoinDenom string) {
if !bytes.HasPrefix(key, QueuedStakingIndexKeyPrefix) {
panic("key does not have proper prefix")
}
addrLen := key[1]
farmerAcc = key[2 : 2+addrLen]
stakingCoinDenom = string(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...)
}