Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

utils.gno

2.05 Kb · 63 lines
 1package pool
 2
 3import (
 4	"strconv"
 5	"strings"
 6
 7	"gno.land/p/gnoswap/utils/v1"
 8)
 9
10const MAX_TICK int32 = 887272
11
12// GetPoolPath generates a unique pool path string based on the token paths and fee tier.
13// Parameters:
14//   - token0Path: path of the first token contract; it is reordered with
15//     token1Path when needed to form canonical pool order.
16//   - token1Path: path of the second token contract; it is reordered with
17//     token0Path when it sorts earlier.
18//   - fee: pool fee tier encoded as its decimal uint32 value in the path.
19//
20// Returns:
21//   - poolPath: canonical token0:token1:fee identifier with token paths in
22//     lexicographical order.
23func GetPoolPath(token0Path, token1Path string, fee uint32) string {
24	// All the token paths in the pool are sorted in alphabetical order.
25	if strings.Compare(token1Path, token0Path) < 0 {
26		token0Path, token1Path = token1Path, token0Path
27	}
28
29	return token0Path + ":" + token1Path + ":" + strconv.FormatUint(uint64(fee), 10)
30}
31
32// EncodeTickKey encodes a tick as a fixed-width 4-byte key whose
33// lexicographic order matches the signed numeric order.
34// Parameters:
35//   - tick: signed tick index to encode.
36//
37// Returns:
38//   - key: fixed-width ordered key whose lexicographical ordering matches the
39//     signed numeric ordering of tick.
40func EncodeTickKey(tick int32) string {
41	return utils.EncodeInt32(tick)
42}
43
44// EncodePositionKey encodes a position range as two ordered tick keys.
45// Parameters:
46//   - tickLower: lower signed tick index of the position range.
47//   - tickUpper: upper signed tick index of the position range.
48//
49// Returns:
50//   - key: concatenation of the ordered encodings of tickLower and tickUpper.
51func EncodePositionKey(tickLower, tickUpper int32) string {
52	return utils.EncodeInt32(tickLower) + utils.EncodeInt32(tickUpper)
53}
54
55// DecodeTickKey decodes a fixed-width ordered tick key.
56// Parameters:
57//   - key: fixed-width ordered tick key produced by EncodeTickKey.
58//
59// Returns:
60//   - tick: signed tick index decoded from key.
61func DecodeTickKey(key string) int32 {
62	return utils.DecodeInt32(key)
63}