-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
89 lines (73 loc) · 2.89 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
package types
import (
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/merlinslair/merlin/osmoutils"
)
const (
ModuleName = "concentratedliquidity"
RouterKey = ModuleName
StoreKey = ModuleName
KeySeparator = "|"
)
// Key prefixes
var (
TickPrefix = []byte{0x01}
PositionPrefix = []byte{0x02}
PoolPrefix = []byte{0x03}
)
// TickIndexToBytes converts a tick index to a byte slice. Negative tick indexes
// are prefixed with 0x00 a byte and positive tick indexes are prefixed with a
// 0x01 byte. We do this because big endian byte encoding does not give us in
// order iteration in state due to the tick index values being signed integers.
func TickIndexToBytes(tickIndex int64) []byte {
key := make([]byte, 9)
if tickIndex < 0 {
copy(key[1:], sdk.Uint64ToBigEndian(uint64(tickIndex)))
} else {
copy(key[:1], []byte{0x01})
copy(key[1:], sdk.Uint64ToBigEndian(uint64(tickIndex)))
}
return key
}
// TickIndexFromBytes converts an encoded tick index to an int64 value. It returns
// an error if the encoded tick has invalid length.
func TickIndexFromBytes(bz []byte) (int64, error) {
if len(bz) != 9 {
return 0, fmt.Errorf("invalid encoded tick index length; expected: 9, got: %d", len(bz))
}
return int64(sdk.BigEndianToUint64(bz[1:])), nil
}
// KeyTick returns a key for storing a TickInfo object.
func KeyTick(poolId uint64, tickIndex int64) []byte {
key := KeyTickPrefix(poolId)
key = append(key, TickIndexToBytes(tickIndex)...)
return key
}
// KeyTickPrefix constructs a key prefix for storing a TickInfo object.
func KeyTickPrefix(poolId uint64) []byte {
var key []byte
key = append(key, TickPrefix...)
key = append(key, sdk.Uint64ToBigEndian(poolId)...)
return key
}
// KeyFullPosition uses pool Id, owner, lower tick, upper tick, and frozenUntil for keys
func KeyFullPosition(poolId uint64, addr sdk.AccAddress, lowerTick, upperTick int64, frozenUntil time.Time) []byte {
frozenUntilKey := osmoutils.FormatTimeString(frozenUntil)
addrKey := address.MustLengthPrefix(addr.Bytes())
return []byte(fmt.Sprintf("%s%s%s%s%d%s%d%s%d%s%s", PositionPrefix, KeySeparator, addrKey, KeySeparator, poolId, KeySeparator, lowerTick, KeySeparator, upperTick, KeySeparator, frozenUntilKey))
}
// KeyPosition uses pool Id, owner, lower tick and upper tick for keys
func KeyPosition(poolId uint64, addr sdk.AccAddress, lowerTick, upperTick int64) []byte {
addrKey := address.MustLengthPrefix(addr.Bytes())
return []byte(fmt.Sprintf("%s%s%s%s%d%s%d%s%d", PositionPrefix, KeySeparator, addrKey, KeySeparator, poolId, KeySeparator, lowerTick, KeySeparator, upperTick))
}
func KeyUserPositions(addr sdk.AccAddress) []byte {
addrKey := address.MustLengthPrefix(addr.Bytes())
return []byte(fmt.Sprintf("%s%s%s", PositionPrefix, KeySeparator, addrKey))
}
func KeyPool(poolId uint64) []byte {
return []byte(fmt.Sprintf("%s%d", PoolPrefix, poolId))
}