Anchor for EVM Developers
  • Anchor for EVM Developers
  • Program Address
  • Functions
  • Token
  • Validation
  • Mutation
  • Calling Other Contract
  • Wrap Native Token
  • Transfer Native Token
  • Transfer Fungible Token
  • Deploy A Fungible Token
  • Access Control
  • Address Mining
  • Events
  • Forking Mainnet
  • Function Selector
  • Feature Flags
  • Feature Dependencies
  • Migration / Scripts
  • Upgrade Programs
  • Block timestamp
Powered by GitBook
On this page

Validation

EVM

contract Dex {
  bool private _paused;
  
  function setPaused(bool paused) external {
    if (msg.sender != owner) {
      revert NotOwner();
    }
    
    _paused = paused;
  }
}

Solana

  1. Using constraint

#[account]
pub struct Config {
  pub authority: Pubkey,
  pub paused: bool,
}

#[derive(Accounts)]
pub struct SetPaused<'info> {
  pub signer: Signer<'info>,
  
  #[account(mut, constraint = config.authority == signer.key() @ ErrorCode::Unauthorized)]
  pub config: Account<'info, Config>
}


pub fn set_paused(Context<SetPaused, bool paused) -> Result<()> {
  ctx.accounts.config.paused = paused;
}
  1. Using require!

#[account]
pub struct Config {
  pub authority: Pubkey,
  pub paused: bool,
}

#[derive(Accounts)]
pub struct SetPaused<'info> {
  pub signer: Signer<'info>,
  
  #[account(mut, constraint = config.authority == signer.key() @ ErrorCode::Unauthorized)]
  pub config: Account<'info, Config>
}


pub fn set_paused(Context<SetPaused, bool paused) -> Result<()> {
  require!(ctx.accounts.config.authority == ctx.accounts.signer.key(), ErrorCode::Unauthorized);
  ctx.accounts.config.paused = paused;
}
  1. Using has_one

Notice how I've changed the caller's field name from signer to authority. has_one is looking for an account called authority that matches the account config's authority.

#[account]
pub struct Config {
  pub authority: Pubkey,
  pub paused: bool,
}

#[derive(Accounts)]
pub struct SetPaused<'info> {
  pub authority: Signer<'info>,
  
  #[account(mut, has_one = authority) @ ErrorCode::Unauthorized)]
  pub config: Account<'info, Config>
}


pub fn set_paused(Context<SetPaused, bool paused) -> Result<()> {
  ctx.accounts.config.paused = paused;
}

PreviousTokenNextMutation

Last updated 2 months ago