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

state_role.gno

2.36 Kb · 84 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
73// Union: https://github.com/unionlabs/union/blob/8cff0ff34f6baa4cdb1e4650a08985dd05de0c5a/cosmwasm/access-manager/src/contract.rs#L314-L345
74func (roleConfig *RoleConfig) setGrantDelay(now TimePoint, newDelay uint32) {
75	updated, _ := roleConfig.GrantDelay.WithUpdate(now, newDelay, MinSetback)
76	roleConfig.GrantDelay = updated
77}
78
79// grantDelay returns the grant delay active at now.
80// OpenZeppelin: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/5fd1781b1454fd1ef8e722282f86f9293cacf256/contracts/access/manager/AccessManager.sol#L194-L196
81// Union: https://github.com/unionlabs/union/blob/8cff0ff34f6baa4cdb1e4650a08985dd05de0c5a/cosmwasm/access-manager/src/contract.rs#L1138-L1144
82func (roleConfig *RoleConfig) grantDelay(now TimePoint) uint32 {
83	return roleConfig.GrantDelay.Get(now)
84}