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

v1 source realm

Readme View source

Access

Centralized role-based access control system for GnoSwap protocol contracts.

Overview

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 protocol components and user-controlled accounts can be assigned role addresses. 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 admin and devops roles are initialized with user-controlled addresses. Most other system roles use deterministic package addresses, while custom roles may use any valid address.

Architecture

The access control system consists of:

  1. Role Registry: Maps role names (strings) to role addresses (contracts or accounts)
  2. Role Management: Functions to set/remove roles (RBAC-only)
  3. Authorization Checks: Functions to verify if an address has a specific role
  4. Assert Helpers: Convenience functions that panic on authorization failure

System Roles

The following roles are used across the GnoSwap protocol:

  • admin: Protocol administrator with elevated privileges
  • devops: DevOps operations for system maintenance
  • governance: Governance contract for protocol decisions
  • router: Swap router for token exchanges
  • pool: Pool management contract
  • position: Position NFT management
  • staker: Liquidity staking contract
  • emission: GNS token emission controller
  • protocol_fee: Protocol fee collection and distribution
  • community_pool: Community treasury management
  • launchpad: Token launchpad for new projects
  • gov_staker: Governance staking contract
  • xgns: xGNS token contract for governance

Key Functions

Role Management (RBAC Only)

SetRoleAddress

Sets 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(cross(cur), "router", routerAddress)

RemoveRole

Removes a role from the system.

1// Only callable by RBAC contract
2access.RemoveRole(cross(cur), "old_role")

Role Query Functions

GetAddress

Returns the address for a role and whether it exists.

1addr, exists := access.GetAddress("router")
2if !exists {
3    // Handle missing role
4}

MustGetAddress

Returns the address for a role or panics if it doesn't exist.

1// Panics if role doesn't exist
2routerAddr := access.MustGetAddress("router")

GetRoleAddresses

Returns a copy of all role-to-address mappings.

1allRoles := access.GetRoleAddresses()
2for roleName, addr := range allRoles {
3    println(roleName, "->", addr)
4}

Authorization Functions

IsAuthorized

Checks if an address has a specific role (non-panicking).

1if access.IsAuthorized("admin", caller) {
2    // Caller is admin
3}

Assert Functions (Panic on Failure)

These functions panic with a descriptive error if authorization fails:

AssertIsAdmin

Requires admin role.

1access.AssertIsAdmin(caller)

AssertIsGovernance

Requires governance role.

1access.AssertIsGovernance(caller)

AssertIsAdminOrGovernance

Requires either admin or governance role.

1access.AssertIsAdminOrGovernance(caller)

Role-Specific Assertions

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)

AssertIsAuthorized

Generic authorization check for any role.

1access.AssertIsAuthorized("custom_role", caller)

AssertHasAnyRole

Requires the caller to have at least one of the specified roles.

1access.AssertHasAnyRole(caller, "admin", "governance", "devops")

Validation Functions

AssertIsValidAddress

Panics if the address is invalid.

1access.AssertIsValidAddress(addr)

Usage Examples

Example 1: Protecting Admin Functions

 1package pool
 2
 3import "gno.land/r/gnoswap/access/v1"
 4
 5func SetPoolFeeRate(cur realm, rate uint64) {
 6    caller := cur.Previous().Address()
 7    access.AssertIsAdminOrGovernance(caller)
 8
 9    // Admin/governance authorized, proceed
10    setFeeRate(rate)
11}

Example 2: Role-Based Function Access

 1package staker
 2
 3import "gno.land/r/gnoswap/access/v1"
 4
 5func DistributeRewards(cur realm, amount uint64) {
 6    caller := cur.Previous().Address()
 7    access.AssertIsEmission(caller)
 8
 9    // Only emission contract can distribute
10    distributeToStakers(amount)
11}

Example 3: Multi-Role Authorization

 1package common
 2
 3import "gno.land/r/gnoswap/access/v1"
 4
 5func EmergencyPause(cur realm) {
 6    caller := cur.Previous().Address()
 7    access.AssertHasAnyRole(caller, "admin", "devops", "governance")
 8
 9    // Any of the authorized roles can pause
10    pauseProtocol()
11}

Example 4: Non-Panicking Authorization Check

 1package router
 2
 3import "gno.land/r/gnoswap/access/v1"
 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}

Security Model

Centralized Management

  • All role assignments are managed through this single contract
  • Provides a unified view of permissions across the entire protocol
  • Prevents inconsistencies in authorization logic

RBAC-Only Updates

  • Only the RBAC contract can modify role assignments
  • Uses package address verification to enforce this restriction
  • Prevents unauthorized role manipulation

Explicit Authorization

  • All authorization checks are explicit and auditable
  • Panic-based assertions make authorization failures obvious
  • No implicit or default permissions

Integration with RBAC

The Access contract works in conjunction with the RBAC (Role-Based Access Control) package:

  1. RBAC: Manages role definitions and ownership transfer
  2. Access: Provides centralized role-to-address registry and authorization checks

Role updates flow: RBAC.UpdateRoleAddress()Access.SetRoleAddress()

Best Practices

  1. Use Assertions for Critical Functions: Always use assert functions for operations that should only proceed with proper authorization
  2. Check Existence Before Use: Use GetAddress when you need to handle missing roles gracefully
  3. Document Role Requirements: Clearly document which roles are required for each function
  4. Avoid Hardcoding Addresses: Always use role-based checks instead of hardcoding addresses
  5. Test Authorization: Thoroughly test all authorization paths in your contracts

Error Handling

Authorization failures result in panics with descriptive error messages:

  • "unauthorized: caller X is not Y" - Caller doesn't have required role
  • "role X not found" - Role lookup failed because the role has not been registered
  • "role X does not exist" - RemoveRole was asked to remove an unknown role
  • "invalid address: X" - Address validation failed

Limitations

  • Role names are case-sensitive strings
  • Each role can only map to one address at a time
  • Role changes take effect immediately (no timelock)

Functions 20

func AssertHasAnyRole

Action
1func AssertHasAnyRole(caller address, roleNames ...string)
source

AssertHasAnyRole checks roleNames in order and panics unless caller matches one. It panics immediately if a checked role is absent, even if a later role might match.

Parameters:

  • caller: address compared against each configured role address
  • roleNames: ordered role identifiers to check; each missing role causes a panic

func AssertIsAdmin

Action
1func AssertIsAdmin(caller address)
source

AssertIsAdmin panics unless caller is the configured admin address.

Parameters:

  • caller: address whose authorization is checked against the admin role

func AssertIsAdminOrGovernance

Action
1func AssertIsAdminOrGovernance(caller address)
source

AssertIsAdminOrGovernance panics unless caller is the configured admin or governance address.

Parameters:

  • caller: address whose authorization is checked against the admin and governance roles

func AssertIsAuthorized

Action
1func AssertIsAuthorized(roleName string, caller address)
source

AssertIsAuthorized panics if caller does not have the specified role or if the role is absent.

Parameters:

  • roleName: role identifier whose configured address is required
  • caller: address that must match the configured address for roleName

func AssertIsEmission

Action
1func AssertIsEmission(caller address)
source

AssertIsEmission panics unless caller is the configured emission address.

Parameters:

  • caller: address whose authorization is checked against the emission role

func AssertIsGovStaker

Action
1func AssertIsGovStaker(caller address)
source

AssertIsGovStaker panics unless caller is the configured governance-staker address.

Parameters:

  • caller: address whose authorization is checked against the governance-staker role

func AssertIsGovXGNS

Action
1func AssertIsGovXGNS(caller address)
source

AssertIsGovXGNS panics unless caller is the configured xGNS governance address.

Parameters:

  • caller: address whose authorization is checked against the xGNS governance role

func AssertIsGovernance

Action
1func AssertIsGovernance(caller address)
source

AssertIsGovernance panics unless caller is the configured governance address.

Parameters:

  • caller: address whose authorization is checked against the governance role

func AssertIsLaunchpad

Action
1func AssertIsLaunchpad(caller address)
source

AssertIsLaunchpad panics unless caller is the configured launchpad address.

Parameters:

  • caller: address whose authorization is checked against the launchpad role

func AssertIsPool

Action
1func AssertIsPool(caller address)
source

AssertIsPool panics unless caller is the configured pool address.

Parameters:

  • caller: address whose authorization is checked against the pool role

func AssertIsPosition

Action
1func AssertIsPosition(caller address)
source

AssertIsPosition panics unless caller is the configured position address.

Parameters:

  • caller: address whose authorization is checked against the position role

func AssertIsProtocolFee

Action
1func AssertIsProtocolFee(caller address)
source

AssertIsProtocolFee panics unless caller is the configured protocol-fee address.

Parameters:

  • caller: address whose authorization is checked against the protocol-fee role

func AssertIsRlmCurrent

Action
1func AssertIsRlmCurrent(_ int, rlm realm)
source

AssertIsRlmCurrent panics if the realm token is not the current crossing frame.

Parameters:

  • _: leading realm-call discriminator; callers pass 0
  • rlm: realm context token that must represent the current crossing frame

func AssertIsRouter

Action
1func AssertIsRouter(caller address)
source

AssertIsRouter panics unless caller is the configured router address.

Parameters:

  • caller: address whose authorization is checked against the router role

func AssertIsStaker

Action
1func AssertIsStaker(caller address)
source

AssertIsStaker panics unless caller is the configured staker address.

Parameters:

  • caller: address whose authorization is checked against the staker role

func AssertIsValidAddress

Action
1func AssertIsValidAddress(addr address)
source

AssertIsValidAddress panics if addr is not a valid address.

Parameters:

  • addr: address value to validate

func GetRoleAddresses

Action
1func GetRoleAddresses() map[string]address
source

GetRoleAddresses returns an independent map copy of all stored role addresses.

Returns:

  • roleAddresses: map from normalized role names to their configured addresses

func IsAuthorized

Action
1func IsAuthorized(role string, caller address) bool
source

IsAuthorized reports whether caller is the address currently mapped to role.

Parameters:

  • role: role name to trim and look up
  • caller: address to compare with the mapped role address

Returns:

  • authorized: true when role exists and caller matches its address; false when the role is absent or does not match

func RemoveRole

crossing Action
1func RemoveRole(cur realm, roleName string)
source

RemoveRole removes a role from the system after trimming its name.

Parameters:

  • cur: current realm context; callers use cross(cur) when crossing into this realm
  • roleName: role identifier to trim and remove; an empty or unknown name panics

Only callable by the RBAC contract.

func SetRoleAddress

crossing Action
1func SetRoleAddress(cur realm, roleName string, roleAddress address)
source

SetRoleAddress sets or updates a role's address. It trims surrounding whitespace from roleName, creates missing roles, and replaces the address for an existing role.

Parameters:

  • cur: current realm context; callers use cross(cur) when crossing into this realm
  • roleName: role identifier; surrounding whitespace is ignored and an empty name panics
  • roleAddress: non-empty, valid address to associate with roleName

Only callable by the RBAC contract.

Imports 4

Source Files 5