Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

state_role.gno

2.08 Kb · 82 lines
 1package manager
 2
 3import "strconv"
 4
 5type RoleId uint64
 6
 7func NewRoleId(id uint64) RoleId {
 8	return RoleId(id)
 9}
10
11func (r RoleId) Uint64() uint64 {
12	return uint64(r)
13}
14
15func (r RoleId) String() string {
16	return strconv.FormatUint(uint64(r), 10)
17}
18
19type RoleConfig struct {
20	Members    map[address]Access
21	Admin      RoleId
22	GrantDelay Delay
23}
24
25func NewRoleConfig(admin RoleId, grantDelay uint32) *RoleConfig {
26	return &RoleConfig{
27		Members:    make(map[address]Access),
28		Admin:      admin,
29		GrantDelay: NewDelay(grantDelay),
30	}
31}
32
33func (roleConfig *RoleConfig) grant(account address, now TimePoint) bool {
34	_, found := roleConfig.Members[account]
35	if found {
36		return false
37	}
38
39	roleConfig.Members[account] = NewAccessWithDelay(now, roleConfig.grantDelay(now))
40
41	return true
42}
43
44func (roleConfig *RoleConfig) revoke(account address) bool {
45	_, found := roleConfig.Members[account]
46	if found {
47		delete(roleConfig.Members, account)
48	}
49
50	return found
51}
52
53func (roleConfig *RoleConfig) hasMember(account address, now TimePoint) bool {
54	access, found := roleConfig.Members[account]
55	if !found {
56		return false
57	}
58
59	return access.isActiveAt(now)
60}
61
62func (roleConfig *RoleConfig) setAdmin(admin RoleId) {
63	roleConfig.Admin = admin
64}
65
66func (roleConfig *RoleConfig) adminRole() RoleId {
67	return roleConfig.Admin
68}
69
70// setGrantDelay schedules newDelay behind the MinSetback setback. Callers
71// read the effect timePoint via State.GetRoleGrantDelayEffect.
72// OpenZeppelin: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/5fd1781b1454fd1ef8e722282f86f9293cacf256/contracts/access/manager/AccessManager.sol#L362-L371
73func (roleConfig *RoleConfig) setGrantDelay(now TimePoint, newDelay uint32) {
74	updated, _ := roleConfig.GrantDelay.WithUpdate(now, newDelay, MinSetback)
75	roleConfig.GrantDelay = updated
76}
77
78// grantDelay returns the grant delay active at now.
79// OpenZeppelin: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/5fd1781b1454fd1ef8e722282f86f9293cacf256/contracts/access/manager/AccessManager.sol#L194-L196
80func (roleConfig *RoleConfig) grantDelay(now TimePoint) uint32 {
81	return roleConfig.GrantDelay.Get(now)
82}