Skip to main content

Module guards

Module guards 

Source
Expand description

Validation guards for Hopper programs.

These functions define the “sentence structure” of Hopper handlers. They support the canonical authored flow:

Validate -> Load -> Mutate -> Emit

Each guard reads as a clear, auditable assertion. Functions return ProgramResult (i.e. Result<(), ProgramError>) so they compose naturally with ?.

use hopper::prelude::*;

fn deposit(ctx: &Context, amount: u64) -> ProgramResult {
    let authority = ctx.account(0)?;
    let vault = ctx.account(1)?;

    require_signer(authority)?;
    require_writable(vault)?;
    require_disc(vault, 1)?;
    require_owner(vault, ctx.program_id)?;

    let state = vault.load_mut::<VaultState>()?;
    // ...
    Ok(())
}

Functions§

require
Require a boolean condition, returning err if false.
require_address
Require that the account has the given address.
require_data_len
Require the account has at least min_len bytes of data.
require_disc
Require the account has the given discriminator byte.
require_eq
Require two values to be equal.
require_gt
Require a > b.
require_gte
Require a >= b.
require_has_data
Require the account has non-empty data.
require_keys_eq
Require two addresses to be equal.
require_keys_neq
Require two addresses to be different.
require_layout
Require the account passes a full layout contract check (disc + version + layout_id).
require_neq
Require two values to be different.
require_owner
Require that the account is owned by the given program.
require_payer
Require both signer and writable (common payer pattern).
require_signer
Require that the account signed the transaction.
require_unique_2
Require that n accounts are different (pairwise uniqueness, up to 6).
require_unique_3
Require 3 accounts are pairwise unique.
require_version
Require the account’s version byte matches the layout contract’s VERSION.
require_writable
Require that the account is writable.