state.gno
2.75 Kb · 91 lines
1package keeper
2
3import (
4 "gno.land/p/gnoswap/order/access/manager/v0"
5)
6
7var (
8 accessState manager.State
9
10 // roleLabels holds every registered role and its label.
11 roleLabels map[manager.RoleId]string
12)
13
14func init() {
15 resetState()
16}
17
18// resetState registers the deployment roles and wires the order realm
19// selectors. DCA rounds are restricted to DCAKeeperRole from deployment:
20// a public round could be sandwiched in one transaction, since the
21// executor sets the swap's minimum output. Admin or governance grants the
22// role to keepers, or reassigns ExecuteGroup. DCA keepers also force-cancel
23// orders that block their group's rounds.
24//
25// PoolManagerRole manages both DCA and Limit pool whitelists and DCA's per-token
26// minimum order sizes and pair price limits. It is kept apart from the keeper
27// role so that neither key alone can both pick the pool and price a round:
28// a keeper cannot swap below the manager's price limit,
29// and a manager cannot run rounds. Limit fills require LimitKeeperRole.
30func resetState() {
31 accessState = manager.NewState()
32 roleLabels = make(map[manager.RoleId]string)
33
34 registerRole(DCAKeeperRole, DCAKeeperLabel)
35 registerRole(LimitKeeperRole, LimitKeeperLabel)
36 registerRole(PoolManagerRole, PoolManagerLabel)
37
38 for _, selector := range []manager.Selector{SelectorExecuteGroup, SelectorForceCancelOrder} {
39 accessState.SetTargetFunctionRole(DCATarget, selector, DCAKeeperRole)
40 emitTargetFunctionRoleUpdated(DCATarget, selector, DCAKeeperRole)
41 }
42
43 for _, selector := range []manager.Selector{
44 SelectorSetTokenMinAmountPerExecution,
45 SelectorSetPairPriceLimit,
46 } {
47 accessState.SetTargetFunctionRole(DCATarget, selector, PoolManagerRole)
48 emitTargetFunctionRoleUpdated(DCATarget, selector, PoolManagerRole)
49 }
50
51 for _, selector := range []manager.Selector{SelectorAddWhitelistedPool, SelectorRemoveWhitelistedPool} {
52 accessState.SetTargetFunctionRole(WhitelistTarget, selector, PoolManagerRole)
53 emitTargetFunctionRoleUpdated(WhitelistTarget, selector, PoolManagerRole)
54 }
55
56 accessState.SetTargetFunctionRole(LimitTarget, SelectorExecuteOrder, LimitKeeperRole)
57 emitTargetFunctionRoleUpdated(LimitTarget, SelectorExecuteOrder, LimitKeeperRole)
58}
59
60func registerRole(role manager.RoleId, label string) {
61 roleLabels[role] = label
62 emitRoleLabel(role, label)
63}
64
65// nextCustomRoleId returns the id after the highest registered role,
66// assigned by AddRole.
67func nextCustomRoleId() manager.RoleId {
68 next := manager.RoleId(1)
69 for role := range roleLabels {
70 if role >= next {
71 next = role + 1
72 }
73 }
74
75 return next
76}
77
78func isRegisteredRole(role manager.RoleId) bool {
79 _, found := roleLabels[role]
80 return found
81}
82
83func isLabelTaken(label string) bool {
84 for _, existing := range roleLabels {
85 if existing == label {
86 return true
87 }
88 }
89
90 return false
91}