Skip to main content

only_role

Attribute Macro only_role 

Source
#[only_role]
Expand description

A procedural macro that ensures the parameter has the specified role and requires authorization.

IMPORTANT: This macro both checks role membership AND enforces authorization. Be aware that in Stellar contracts, duplicate require_auth() calls for the same account will cause panics. If the function already contains a require_auth() call for the same account, use #[has_role] instead to avoid duplicate authorization checks.

ยงUsage

#[only_role(account, "minter")]
pub fn mint_tokens(e: &Env, amount: u32, account: Address) {
    // Function body
}

This will expand to:

pub fn mint_tokens(e: &Env, amount: u32, account: Address) {
    stellar_access::access_control::ensure_role(
        e,
        &account,
        &soroban_sdk::Symbol::new(e, "minter"),
    );
    account.require_auth();
    // Function body
}