keys.gno
4.92 Kb · 136 lines
1package dca
2
3import (
4 "strconv"
5 "strings"
6
7 "gno.land/p/gnoswap/utils/v1"
8)
9
10// MakeGroupID builds the group key
11// "inputToken:outputToken:interval:slotOffset".
12func MakeGroupID(inputToken string, outputToken string, interval int64, slotOffset int64) string {
13 return inputToken + ":" + outputToken + ":" + utils.FormatInt(interval) + ":" + utils.FormatInt(slotOffset)
14}
15
16// OrderKey encodes an order ID so B+tree order matches numeric order.
17func OrderKey(orderID uint64) string {
18 return utils.EncodeUint64(orderID)
19}
20
21// RoundKey encodes a round so B+tree order matches numeric order.
22func RoundKey(round uint64) string {
23 return utils.EncodeUint64(round)
24}
25
26// OwnerOrderPrefix is the common prefix of an owner's index keys.
27func OwnerOrderPrefix(owner address) string {
28 return owner.String() + "/"
29}
30
31// OwnerOrderKey sorts an owner's orders by ID under OwnerOrderPrefix.
32func OwnerOrderKey(owner address, orderID uint64) string {
33 return OwnerOrderPrefix(owner) + utils.EncodeUint64(orderID)
34}
35
36// GroupOrderPrefix is the common prefix of a group's order index keys.
37// Token paths contain no ':', so one group's prefix never starts another's.
38func GroupOrderPrefix(groupID string) string {
39 return groupID + "/"
40}
41
42// GroupOrderKey sorts a group's orders by ID under GroupOrderPrefix.
43func GroupOrderKey(groupID string, orderID uint64) string {
44 return GroupOrderPrefix(groupID) + utils.EncodeUint64(orderID)
45}
46
47// GroupOrderFeeRatePrefix is the common prefix of the GroupOrderAmountKeys
48// of a group's orders with executionFeeRate, under GroupOrderPrefix.
49// executionFeeRate is in [0, MaxExecutionFeeRate], so its first byte is 0
50// and PrefixEnd of GroupOrderPrefix still bounds every rate's keys.
51func GroupOrderFeeRatePrefix(groupID string, executionFeeRate int64) string {
52 return GroupOrderPrefix(groupID) + utils.EncodeUint64(uint64(executionFeeRate))
53}
54
55// GroupOrderAmountKey sorts a group's orders by executionFeeRate, then
56// amountPerExecution, then ID, under GroupOrderFeeRatePrefix.
57// amountPerExecution is positive, so its first byte is below 0x80
58// and PrefixEnd of GroupOrderFeeRatePrefix still bounds the rate's keys.
59func GroupOrderAmountKey(groupID string, executionFeeRate int64, amountPerExecution int64, orderID uint64) string {
60 return GroupOrderFeeRatePrefix(groupID, executionFeeRate) + utils.EncodeUint64(uint64(amountPerExecution)) + utils.EncodeUint64(orderID)
61}
62
63// GroupOrderAmountKeyFeeRate extracts executionFeeRate from a
64// GroupOrderAmountKey of groupID.
65func GroupOrderAmountKeyFeeRate(groupID string, key string) int64 {
66 start := len(GroupOrderPrefix(groupID))
67 return int64(utils.DecodeUint64(key[start : start+8]))
68}
69
70// GroupOrderAmountKeyAmount extracts amountPerExecution from a
71// GroupOrderAmountKey of groupID.
72func GroupOrderAmountKeyAmount(groupID string, key string) int64 {
73 start := len(GroupOrderPrefix(groupID)) + 8
74 return int64(utils.DecodeUint64(key[start : start+8]))
75}
76
77// ExecutionLogPrefix is the common prefix of an order's execution log keys.
78func ExecutionLogPrefix(orderID uint64) string {
79 return utils.EncodeUint64(orderID)
80}
81
82// ExecutionLogKey sorts an order's execution logs by round
83// under ExecutionLogPrefix.
84func ExecutionLogKey(orderID uint64, round uint64) string {
85 return ExecutionLogPrefix(orderID) + utils.EncodeUint64(round)
86}
87
88// PrefixEnd is an exclusive upper bound for prefix + 8-byte encoded ID keys.
89// Nine 0xff bytes sort after every 8-byte suffix.
90func PrefixEnd(prefix string) string {
91 return prefix + "\xff\xff\xff\xff\xff\xff\xff\xff\xff"
92}
93
94// ScheduleKey orders groups by next execution timestamp, then group ID.
95func ScheduleKey(nextExecutionTimestamp int64, groupID string) string {
96 return utils.EncodeUint64(uint64(nextExecutionTimestamp)) + groupID
97}
98
99// ScheduleKeyTimestamp extracts the next execution timestamp from a ScheduleKey.
100func ScheduleKeyTimestamp(key string) int64 {
101 return int64(utils.DecodeUint64(key[:8]))
102}
103
104// FormatScheduleCursor renders a schedule entry as
105// "<nextExecutionTimestamp>|<groupID>".
106func FormatScheduleCursor(nextExecutionTimestamp int64, groupID string) string {
107 return utils.FormatInt(nextExecutionTimestamp) + "|" + groupID
108}
109
110// ParseScheduleCursor converts a "<nextExecutionTimestamp>|<groupID>" cursor
111// back to its ScheduleKey.
112// An empty cursor yields an empty key, i.e. the start of the index.
113// A malformed cursor returns ErrInvalidCursor.
114func ParseScheduleCursor(cursor string) (string, error) {
115 if cursor == "" {
116 return "", nil
117 }
118
119 sep := strings.Index(cursor, "|")
120 if sep <= 0 {
121 return "", ErrInvalidCursor
122 }
123
124 nextExecutionTimestamp, err := strconv.ParseInt(cursor[:sep], 10, 64)
125 if err != nil || nextExecutionTimestamp < 0 {
126 return "", ErrInvalidCursor
127 }
128
129 return ScheduleKey(nextExecutionTimestamp, cursor[sep+1:]), nil
130}
131
132// PairKey builds the key of a pair direction, "inputToken:outputToken".
133// Token paths contain no ':', so the key is unambiguous.
134func PairKey(inputToken string, outputToken string) string {
135 return inputToken + ":" + outputToken
136}