|
| 1 | +use anchor_lang::{ |
| 2 | + prelude::*, |
| 3 | + system_program::{create_account, CreateAccount}, |
| 4 | +}; |
| 5 | +use anchor_spl::{ |
| 6 | + associated_token::AssociatedToken, |
| 7 | + token_interface::{Mint, TokenAccount, TokenInterface}, |
| 8 | +}; |
| 9 | +use spl_tlv_account_resolution::{ |
| 10 | + account::ExtraAccountMeta, seeds::Seed, state::ExtraAccountMetaList, |
| 11 | +}; |
| 12 | +use spl_transfer_hook_interface::instruction::{ExecuteInstruction, TransferHookInstruction}; |
| 13 | + |
| 14 | +declare_id!("DrWbQtYJGtsoRwzKqAbHKHKsCJJfpysudF39GBVFSxub"); |
| 15 | + |
| 16 | +#[error_code] |
| 17 | +pub enum MyError { |
| 18 | + #[msg("The amount is too big")] |
| 19 | + AmountTooBig, |
| 20 | +} |
| 21 | + |
| 22 | +#[program] |
| 23 | +pub mod transfer_hook { |
| 24 | + use super::*; |
| 25 | + |
| 26 | + pub fn initialize_extra_account_meta_list( |
| 27 | + ctx: Context<InitializeExtraAccountMetaList>, |
| 28 | + ) -> Result<()> { |
| 29 | + |
| 30 | + let account_metas = vec![ |
| 31 | + ExtraAccountMeta::new_with_seeds( |
| 32 | + &[Seed::Literal { |
| 33 | + bytes: "counter".as_bytes().to_vec(), |
| 34 | + }], |
| 35 | + false, // is_signer |
| 36 | + true, // is_writable |
| 37 | + )?, |
| 38 | + ]; |
| 39 | + |
| 40 | + // calculate account size |
| 41 | + let account_size = ExtraAccountMetaList::size_of(account_metas.len())? as u64; |
| 42 | + // calculate minimum required lamports |
| 43 | + let lamports = Rent::get()?.minimum_balance(account_size as usize); |
| 44 | + |
| 45 | + let mint = ctx.accounts.mint.key(); |
| 46 | + let signer_seeds: &[&[&[u8]]] = &[&[ |
| 47 | + b"extra-account-metas", |
| 48 | + &mint.as_ref(), |
| 49 | + &[ctx.bumps.extra_account_meta_list], |
| 50 | + ]]; |
| 51 | + |
| 52 | + // create ExtraAccountMetaList account |
| 53 | + create_account( |
| 54 | + CpiContext::new( |
| 55 | + ctx.accounts.system_program.to_account_info(), |
| 56 | + CreateAccount { |
| 57 | + from: ctx.accounts.payer.to_account_info(), |
| 58 | + to: ctx.accounts.extra_account_meta_list.to_account_info(), |
| 59 | + }, |
| 60 | + ) |
| 61 | + .with_signer(signer_seeds), |
| 62 | + lamports, |
| 63 | + account_size, |
| 64 | + ctx.program_id, |
| 65 | + )?; |
| 66 | + |
| 67 | + // initialize ExtraAccountMetaList account with extra accounts |
| 68 | + ExtraAccountMetaList::init::<ExecuteInstruction>( |
| 69 | + &mut ctx.accounts.extra_account_meta_list.try_borrow_mut_data()?, |
| 70 | + &account_metas, |
| 71 | + )?; |
| 72 | + |
| 73 | + Ok(()) |
| 74 | + } |
| 75 | + |
| 76 | + pub fn transfer_hook(ctx: Context<TransferHook>, amount: u64) -> Result<()> { |
| 77 | + |
| 78 | + if amount > 50 { |
| 79 | + msg!("The amount is too big {0}", amount); |
| 80 | + //return err!(MyError::AmountTooBig); |
| 81 | + } |
| 82 | + |
| 83 | + ctx.accounts.counter_account.counter.checked_add(1).unwrap(); |
| 84 | + |
| 85 | + msg!("This token has been transferred {0} times", ctx.accounts.counter_account.counter); |
| 86 | + |
| 87 | + Ok(()) |
| 88 | + } |
| 89 | + |
| 90 | + // fallback instruction handler as workaround to anchor instruction discriminator check |
| 91 | + pub fn fallback<'info>( |
| 92 | + program_id: &Pubkey, |
| 93 | + accounts: &'info [AccountInfo<'info>], |
| 94 | + data: &[u8], |
| 95 | + ) -> Result<()> { |
| 96 | + let instruction = TransferHookInstruction::unpack(data)?; |
| 97 | + |
| 98 | + // match instruction discriminator to transfer hook interface execute instruction |
| 99 | + // token2022 program CPIs this instruction on token transfer |
| 100 | + match instruction { |
| 101 | + TransferHookInstruction::Execute { amount } => { |
| 102 | + let amount_bytes = amount.to_le_bytes(); |
| 103 | + |
| 104 | + // invoke custom transfer hook instruction on our program |
| 105 | + __private::__global::transfer_hook(program_id, accounts, &amount_bytes) |
| 106 | + } |
| 107 | + _ => return Err(ProgramError::InvalidInstructionData.into()), |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +#[derive(Accounts)] |
| 113 | +pub struct InitializeExtraAccountMetaList<'info> { |
| 114 | + #[account(mut)] |
| 115 | + payer: Signer<'info>, |
| 116 | + |
| 117 | + /// CHECK: ExtraAccountMetaList Account, must use these seeds |
| 118 | + #[account( |
| 119 | + mut, |
| 120 | + seeds = [b"extra-account-metas", mint.key().as_ref()], |
| 121 | + bump |
| 122 | + )] |
| 123 | + pub extra_account_meta_list: AccountInfo<'info>, |
| 124 | + pub mint: InterfaceAccount<'info, Mint>, |
| 125 | + #[account( |
| 126 | + init_if_needed, |
| 127 | + seeds = [b"counter"], |
| 128 | + bump, |
| 129 | + payer = payer, |
| 130 | + space = 16 |
| 131 | + )] |
| 132 | + pub counter_account: Account<'info, CounterAccount>, |
| 133 | + pub token_program: Interface<'info, TokenInterface>, |
| 134 | + pub associated_token_program: Program<'info, AssociatedToken>, |
| 135 | + pub system_program: Program<'info, System>, |
| 136 | +} |
| 137 | + |
| 138 | +// Order of accounts matters for this struct. |
| 139 | +// The first 4 accounts are the accounts required for token transfer (source, mint, destination, owner) |
| 140 | +// Remaining accounts are the extra accounts required from the ExtraAccountMetaList account |
| 141 | +// These accounts are provided via CPI to this program from the token2022 program |
| 142 | +#[derive(Accounts)] |
| 143 | +pub struct TransferHook<'info> { |
| 144 | + #[account( |
| 145 | + token::mint = mint, |
| 146 | + token::authority = owner, |
| 147 | + )] |
| 148 | + pub source_token: InterfaceAccount<'info, TokenAccount>, |
| 149 | + pub mint: InterfaceAccount<'info, Mint>, |
| 150 | + #[account( |
| 151 | + token::mint = mint, |
| 152 | + )] |
| 153 | + pub destination_token: InterfaceAccount<'info, TokenAccount>, |
| 154 | + /// CHECK: source token account owner, can be SystemAccount or PDA owned by another program |
| 155 | + pub owner: UncheckedAccount<'info>, |
| 156 | + /// CHECK: ExtraAccountMetaList Account, |
| 157 | + #[account( |
| 158 | + seeds = [b"extra-account-metas", mint.key().as_ref()], |
| 159 | + bump |
| 160 | + )] |
| 161 | + pub extra_account_meta_list: UncheckedAccount<'info>, |
| 162 | + #[account( |
| 163 | + seeds = [b"counter"], |
| 164 | + bump |
| 165 | + )] |
| 166 | + pub counter_account: Account<'info, CounterAccount>, |
| 167 | +} |
| 168 | + |
| 169 | +#[account] |
| 170 | +pub struct CounterAccount { |
| 171 | + counter: u64, |
| 172 | +} |
0 commit comments