|
| 1 | +use anchor_lang::prelude::*; |
| 2 | +use anchor_spl::{ |
| 3 | + associated_token::AssociatedToken, |
| 4 | + token::Token, |
| 5 | + token_interface::{transfer_checked, Mint, TokenAccount, TransferChecked}, |
| 6 | +}; |
| 7 | + |
| 8 | +use crate::{check_is_transferring, CounterAccount, TransferError}; |
| 9 | + |
| 10 | +// Order of accounts matters for this struct. |
| 11 | +// The first 4 accounts are the accounts required for token transfer (source, mint, destination, owner) |
| 12 | +// Remaining accounts are the extra accounts required from the ExtraAccountMetaList account |
| 13 | +// These accounts are provided via CPI to this program from the token2022 program |
| 14 | +// |
| 15 | +// Box<InterfaceAccount> used for source_token, destination_token, wsol_mint, |
| 16 | +// delegate_wsol_token_account, and sender_wsol_token_account to avoid exceeding |
| 17 | +// the 4096-byte BPF stack frame limit in try_accounts deserialization. |
| 18 | +// This struct has 12 accounts — without Box, the generated code uses ~4160 bytes of stack. |
| 19 | +#[derive(Accounts)] |
| 20 | +pub struct TransferHook<'info> { |
| 21 | + #[account(token::mint = mint, token::authority = owner)] |
| 22 | + pub source_token: Box<InterfaceAccount<'info, TokenAccount>>, |
| 23 | + pub mint: Box<InterfaceAccount<'info, Mint>>, |
| 24 | + #[account(token::mint = mint)] |
| 25 | + pub destination_token: Box<InterfaceAccount<'info, TokenAccount>>, |
| 26 | + /// CHECK: source token account owner, can be SystemAccount or PDA owned by another program |
| 27 | + pub owner: UncheckedAccount<'info>, |
| 28 | + /// CHECK: ExtraAccountMetaList Account, |
| 29 | + #[account(seeds = [b"extra-account-metas", mint.key().as_ref()], bump)] |
| 30 | + pub extra_account_meta_list: UncheckedAccount<'info>, |
| 31 | + pub wsol_mint: Box<InterfaceAccount<'info, Mint>>, |
| 32 | + pub token_program: Program<'info, Token>, |
| 33 | + pub associated_token_program: Program<'info, AssociatedToken>, |
| 34 | + #[account( |
| 35 | + mut, |
| 36 | + seeds = [b"delegate"], |
| 37 | + bump |
| 38 | + )] |
| 39 | + pub delegate: SystemAccount<'info>, |
| 40 | + #[account( |
| 41 | + mut, |
| 42 | + token::mint = wsol_mint, |
| 43 | + token::authority = delegate, |
| 44 | + )] |
| 45 | + pub delegate_wsol_token_account: Box<InterfaceAccount<'info, TokenAccount>>, |
| 46 | + #[account( |
| 47 | + mut, |
| 48 | + token::mint = wsol_mint, |
| 49 | + token::authority = owner, |
| 50 | + )] |
| 51 | + pub sender_wsol_token_account: Box<InterfaceAccount<'info, TokenAccount>>, |
| 52 | + #[account(seeds = [b"counter"], bump)] |
| 53 | + pub counter_account: Account<'info, CounterAccount>, |
| 54 | +} |
| 55 | + |
| 56 | +pub fn handler(context: Context<TransferHook>, amount: u64) -> Result<()> { |
| 57 | + // Fail this instruction if it is not called from within a transfer hook |
| 58 | + check_is_transferring(&context)?; |
| 59 | + |
| 60 | + if amount > 50 { |
| 61 | + msg!("The amount is too big {0}", amount); |
| 62 | + } |
| 63 | + |
| 64 | + context.accounts.counter_account.counter += 1; |
| 65 | + |
| 66 | + msg!( |
| 67 | + "This token has been transferred {0} times", |
| 68 | + context.accounts.counter_account.counter |
| 69 | + ); |
| 70 | + |
| 71 | + msg!( |
| 72 | + "Is writable mint {0}", |
| 73 | + context.accounts.mint.to_account_info().is_writable |
| 74 | + ); |
| 75 | + msg!( |
| 76 | + "Is destination mint {0}", |
| 77 | + context.accounts.destination_token.to_account_info().is_writable |
| 78 | + ); |
| 79 | + msg!( |
| 80 | + "Is source mint {0}", |
| 81 | + context.accounts.source_token.to_account_info().is_writable |
| 82 | + ); |
| 83 | + |
| 84 | + let signer_seeds: &[&[&[u8]]] = &[&[b"delegate", &[context.bumps.delegate]]]; |
| 85 | + |
| 86 | + // Transfer WSOL from sender to delegate token account using delegate PDA |
| 87 | + // transfer lamports amount equal to token transfer amount |
| 88 | + transfer_checked( |
| 89 | + CpiContext::new( |
| 90 | + context.accounts.token_program.key(), |
| 91 | + TransferChecked { |
| 92 | + from: context.accounts.sender_wsol_token_account.to_account_info(), |
| 93 | + mint: context.accounts.wsol_mint.to_account_info(), |
| 94 | + to: context.accounts.delegate_wsol_token_account.to_account_info(), |
| 95 | + authority: context.accounts.delegate.to_account_info(), |
| 96 | + }, |
| 97 | + ) |
| 98 | + .with_signer(signer_seeds), |
| 99 | + amount, |
| 100 | + context.accounts.wsol_mint.decimals, |
| 101 | + )?; |
| 102 | + Ok(()) |
| 103 | +} |
0 commit comments