Attribute Macro anchor_lang::interface

source ·
#[interface]
Available on crate feature interface-instructions only.
Expand description

The #[interface] attribute is used to mark an instruction as belonging to an interface implementation, thus transforming its discriminator to the proper bytes for that interface instruction.

§Example

use anchor_lang::prelude::*;

// SPL Transfer Hook Interface: `Execute` instruction.
//
// This instruction is invoked by Token-2022 when a transfer occurs,
// if a mint has specified this program as its transfer hook.
#[interface(spl_transfer_hook_interface::execute)]
pub fn execute_transfer(ctx: Context<Execute>, amount: u64) -> Result<()> {
    // Check that all extra accounts were provided
    let data = ctx.accounts.extra_metas_account.try_borrow_data()?;
    ExtraAccountMetaList::check_account_infos::<ExecuteInstruction>(
        &ctx.accounts.to_account_infos(),
        &TransferHookInstruction::Execute { amount }.pack(),
        &ctx.program_id,
        &data,
    )?;

    // Or maybe perform some custom logic
    if ctx.accounts.token_metadata.mint != ctx.accounts.token_account.mint {
        return Err(ProgramError::IncorrectAccount);
    }

    Ok(())
}