func AssertHasAnyRole
ActionAssertHasAnyRole panics if the caller does not have any of the specified roles. Also panics if any of the roles do not exist.
Centralized role-based access control system for GnoSwap protocol contracts.
The Access package provides a unified permission management system for all GnoSwap protocol contracts. It manages role-to-address mappings and provides convenient assertion functions for authorization checks throughout the protocol.
This package acts as a centralized registry where each protocol component (pool, router, staker, etc.) registers its address under a specific role. Other contracts can then query this registry to verify permissions before executing privileged operations. Admin role ownership is managed by the RBAC realm and is updated on ownership transfer; this package only stores the latest role address.
The access control system consists of:
The following roles are used across the GnoSwap protocol:
SetRoleAddressSets or updates a role's address. Creates new role if it doesn't exist.
The admin role is updated by RBAC ownership transfers and should not be managed directly by other contracts.
1// Only callable by RBAC contract
2access.SetRoleAddress(cur, "router", routerAddress)
RemoveRoleRemoves a role from the system.
1// Only callable by RBAC contract
2access.RemoveRole(cur, "old_role")
GetAddressReturns the address for a role and whether it exists.
1addr, exists := access.GetAddress("router")
2if !exists {
3 // Handle missing role
4}
MustGetAddressReturns the address for a role or panics if it doesn't exist.
1// Panics if role doesn't exist
2routerAddr := access.MustGetAddress("router")
GetRoleAddressesReturns a copy of all role-to-address mappings.
1allRoles := access.GetRoleAddresses()
2for roleName, addr := range allRoles {
3 println(roleName, "->", addr)
4}
IsAuthorizedChecks if an address has a specific role (non-panicking).
1if access.IsAuthorized("admin", caller) {
2 // Caller is admin
3}
These functions panic with a descriptive error if authorization fails:
AssertIsAdminRequires admin role.
1access.AssertIsAdmin(caller)
AssertIsGovernanceRequires governance role.
1access.AssertIsGovernance(caller)
AssertIsAdminOrGovernanceRequires either admin or governance role.
1access.AssertIsAdminOrGovernance(caller)
1access.AssertIsRouter(caller)
2access.AssertIsPool(caller)
3access.AssertIsPosition(caller)
4access.AssertIsStaker(caller)
5access.AssertIsEmission(caller)
6access.AssertIsProtocolFee(caller)
7access.AssertIsLaunchpad(caller)
8access.AssertIsGovStaker(caller)
9access.AssertIsGovXGNS(caller)
AssertIsAuthorizedGeneric authorization check for any role.
1access.AssertIsAuthorized("custom_role", caller)
AssertHasAnyRoleRequires the caller to have at least one of the specified roles.
1access.AssertHasAnyRole(caller, "admin", "governance", "devops")
AssertIsValidAddressPanics if the address is invalid.
1access.AssertIsValidAddress(addr)
AssertIsUserPanics if the caller is not a user realm (i.e., a contract is calling).
1access.AssertIsUser(r)
1package pool
2
3import "gno.land/r/gnoswap/access"
4
5func SetPoolFeeRate(rate uint64) {
6 caller := std.PrevRealm().Addr()
7 access.AssertIsAdminOrGovernance(caller)
8
9 // Admin/governance authorized, proceed
10 setFeeRate(rate)
11}
1package staker
2
3import "gno.land/r/gnoswap/access"
4
5func DistributeRewards(amount uint64) {
6 caller := std.PrevRealm().Addr()
7 access.AssertIsEmission(caller)
8
9 // Only emission contract can distribute
10 distributeToStakers(amount)
11}
1package common
2
3import "gno.land/r/gnoswap/access"
4
5func EmergencyPause() {
6 caller := std.PrevRealm().Addr()
7 access.AssertHasAnyRole(caller, "admin", "devops", "governance")
8
9 // Any of the authorized roles can pause
10 pauseProtocol()
11}
1package router
2
3import "gno.land/r/gnoswap/access"
4
5func GetSwapFee(caller address) uint64 {
6 // Lower fee for admin
7 if access.IsAuthorized("admin", caller) {
8 return 0 // Admin gets free swaps
9 }
10
11 return standardFee
12}
The Access contract works in conjunction with the RBAC (Role-Based Access Control) package:
Role updates flow: RBAC.UpdateRoleAddress() → Access.SetRoleAddress()
GetAddress when you need to handle missing roles gracefullyAuthorization failures result in panics with descriptive error messages:
"unauthorized: caller X is not Y" - Caller doesn't have required role"role X does not exist" - Role hasn't been registered"invalid address: X" - Address validation failed"caller is not user" - Contract called user-only functionAssertHasAnyRole panics if the caller does not have any of the specified roles. Also panics if any of the roles do not exist.
AssertIsAdmin panics if the caller is not admin. Used for admin-only functions.
AssertIsAdminOrGovernance panics if the caller is not admin or governance. Used for functions that require elevated privileges.
AssertIsAuthorized panics if the caller does not have the specified role. Also panics if the role does not exist.
AssertIsEmission panics if the caller is not emission. Used for emission-only functions.
AssertIsGovStaker panics if the caller is not governance staker. Used for governance staking functions.
AssertIsGovXGNS panics if the caller is not xGNS governance. Used for xGNS governance functions.
AssertIsGovernance panics if the caller is not governance. Used for governance-only functions.
AssertIsLaunchpad panics if the caller is not launchpad. Used for launchpad-only functions.
AssertIsPool panics if the caller is not pool. Used for pool-only functions.
AssertIsPosition panics if the caller is not position. Used for position-only functions.
AssertIsProtocolFee panics if the caller is not protocol fee. Used for protocol fee management functions.
AssertIsRouter panics if the caller is not router. Used for router-only functions.
AssertIsStaker panics if the caller is not staker. Used for staker-only functions.
AssertIsUser panics if the caller is not a user realm. Used to ensure calls come from user accounts, not other contracts.
AssertIsValidAddress panics if the provided address is invalid.
GetRoleAddresses returns a copy of all role addresses.
IsAuthorized checks if caller has the specified role.
Parameters:
Returns true if authorized, false otherwise.
RemoveRole removes a role from the system.
Parameters:
Only callable by RBAC contract.
SetRoleAddress sets or updates a role's address. Creates the role if it doesn't exist, updates it if it does.
Parameters:
Only callable by RBAC contract.