> ## Documentation Index
> Fetch the complete documentation index at: https://solana.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> This page is part of the Solana developer documentation at https://solana.com/developers/docs. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get the page as Markdown.
> Index of all pages: https://solana.com/llms.txt. Per-section indexes such as https://solana.com/developers/docs/payments/llms.txt are smaller; prefer them when working on one task.
> Coding agents: install the official Solana skill for current APIs and conventions with `npx skills add https://github.com/solana-foundation/solana-dev-skill` (also served at https://solana.com/SKILL.md).
> Code in the cookbook and task pages comes from tested example files. Prefer it over writing API calls from memory.

# How to use the Transfer Hook extension

> The Transfer Hook extension and Transfer Hook Interface introduce the ability to create Mint Accounts that execute custom instruction logic on every token transfer.

This enables new use cases for token transfers, such as:

- Enforcing NFT royalties
- Black or white list wallets that can receive tokens
- Implementing custom fees on token transfers
- Creating custom token transfer events
- Track statistics over your token transfers
- And many more

To achieve this, developers must build a program that implements the
[Transfer Hook Interface](https://www.solana-program.com/docs/transfer-hook-interface)
and initialize a Mint Account with the Transfer Hook extension enabled.

For every token transfer involving tokens from the Mint Account, the Token
Extensions program makes a cross-program invocation (CPI) to execute an
instruction on the Transfer Hook program.

<Info>

When the Token Extensions program CPIs to a Transfer Hook program, all accounts
from the initial transfer are converted to read-only accounts. This means the
signer privileges of the sender do not extend to the Transfer Hook program.

This design decision is made to prevent malicious use of Transfer Hook programs.

</Info>

In this guide, we will create Transfer Hook programs with the Anchor framework.
You can also implement the Transfer Hook Interface in a native program. Learn
more about Anchor at [anchor-lang.com](https://www.anchor-lang.com/).

<Info>

The programs in this guide use Anchor 1.1.2 and run on your machine. You build
them in one Anchor project, one stage at a time: a hello-world hook, a counter,
and a hook that charges a wSOL fee. Each stage's TypeScript test creates a mint
that uses the hook and transfers it on a local validator. Start with
[Set up the project](#set-up-the-project).

</Info>

## Transfer Hook Interface Overview

The Transfer Hook Interface provides a way for developers to implement custom
instruction logic that is executed on every token transfer for a specific Mint
Account.

The Transfer Hook Interface specifies the following
[instructions](https://github.com/solana-program/transfer-hook/blob/main/interface/src/instruction.rs):

- `Execute`: An instruction that the Token Extension program invokes on every
  token transfer.
- `InitializeExtraAccountMetaList` (optional): Creates an account that stores a
  list of additional accounts required by the custom `Execute` instruction.
- `UpdateExtraAccountMetaList` (optional): Updates the list of additional
  accounts by overwriting the existing list.

It is technically not required to implement the `InitializeExtraAccountMetaList`
instruction using the interface. The account can be created by any instruction
on a Transfer Hook program.

However, the program-derived address (PDA) for the account must be derived using
the following seeds:

- The hard coded string "extra-account-metas"
- The Mint Account address
- The Transfer Hook program ID

<CodeGroup>

```js title="Web3.js v3"
const [pda] = await PublicKey.findProgramAddress(
  [Buffer.from("extra-account-metas"), mint.publicKey.toBytes()],
  program.programId // transfer hook program ID
);
```

<Warning>
  **`@solana/web3.js` 1.x is no longer actively maintained**

  It receives critical fixes only. Use [Kit](/developers/docs/clients/official/javascript#solana-kit) for new code. To migrate existing code, follow the [Kit upgrade guide](https://www.solanakit.com/docs/upgrade-guide), or the [web3.js v3 migration guide](https://github.com/solana-foundation/solana-web3.js/blob/main/docs/web3js-v1-to-v3-migration.md) to keep the class-based API.

  To run this code, install 1.x with `npm install @solana/web3.js@1`. A plain `npm install @solana/web3.js` installs v3, which this code doesn't run on.
</Warning>

```js title="Web3.js (legacy)"
const [pda] = PublicKey.findProgramAddressSync(
  [Buffer.from("extra-account-metas"), mint.publicKey.toBuffer()],
  program.programId // transfer hook program ID
);
```

</CodeGroup>

By storing the extra accounts required by the `Execute` instruction in the
predefined PDA, these accounts can be automatically added to a token transfer
instruction from the client.

## Set up the project

Install Rust, the Solana CLI, the Anchor CLI, Node.js, and Yarn as shown in the
[Quickstart](/developers/docs/quickstart). This guide was tested with Anchor CLI
1.1.2 and Solana CLI 3.1.10, the release that AVM installs with Anchor 1.1.2.

<Steps titleSize="h3">

<Step title="Create the project">

Create an Anchor project with a single-file program and a Mocha test:

```bash Terminal
anchor init transfer-hook --template single --test-template mocha --package-manager yarn
cd transfer-hook
```

`--package-manager yarn` sets the project up with Yarn, which the installer
includes. Without it, `anchor init` uses the first of pnpm, Yarn, and npm that
it finds. The program is in `programs/transfer-hook/src/lib.rs` and its test is
in `tests/transfer-hook.ts`. Each example in this guide replaces one or both
files.

</Step>

<Step title="Add the Rust dependencies">

From the program's folder, add `anchor-spl` and the crates for the Transfer Hook
Interface. These versions build against the same Solana 3.x crates as Anchor
1.1.2:

```bash Terminal
cd programs/transfer-hook
cargo add anchor-spl@1.1.2 spl-transfer-hook-interface@2.1.0 spl-tlv-account-resolution@0.11.4 spl-discriminator@0.5.2
cd ../..
```

Then add `anchor-spl/idl-build` to the `idl-build` feature in
`programs/transfer-hook/Cargo.toml`. Without it, `anchor build` fails with
`Building IDL failed`:

```toml programs/transfer-hook/Cargo.toml
[features]
idl-build = ["anchor-lang/idl-build", "anchor-spl/idl-build"]
```

</Step>

<Step title="Add the TypeScript packages">

The tests build token instructions with `@solana/spl-token`, which runs on
`@solana/web3.js` 1.x:

```bash Terminal
yarn add @solana/web3.js@1 @solana/spl-token@^0.4.15
```

</Step>

</Steps>

Run each example's test with:

```bash Terminal
anchor test --validator legacy
```

`anchor test` builds the program, starts a local validator, deploys the program
to it, runs `tests/transfer-hook.ts`, then stops the validator. With
`--validator legacy` the local validator is `solana-test-validator`. Without the
flag, Anchor 1.1.2 starts Surfpool, and on Surfpool 1.7.0 the wSOL fee example's
transfer fails with `Blockhash not found`.

<Warning>

If `anchor build` or `anchor test` stops with
`Platform tools v1.52 is incompatible with SBPFv3`, you're on Solana CLI 4.3 or
later without AVM (see
[Build your first program](/developers/docs/programs/build-first-program#run-test)).
Build the program for SBPF v0 without the IDL, build the IDL and its TypeScript
types on their own, then test without rebuilding:

```bash Terminal
anchor build --no-idl -- --arch v0
anchor idl build -o target/idl/transfer_hook.json -t target/types/transfer_hook.ts
anchor test --skip-build --validator legacy
```

</Warning>

## Hello-world Transfer hook

This example is the hello world of transfer hooks. It is a simple transfer hook
that will just print a message on every token transfer.

This program has 2 instructions:

1. `initialize_extra_account_meta_list`: Creates an account that stores a list
   of extra accounts required by the `transfer_hook` instruction. In the hello
   world we leave this empty.
2. `transfer_hook`: This instruction is invoked via CPI on every token transfer.
   In the hello world it only logs a message.

Every time the token gets transferred this `transfer_hook` function will be
called by the token program. Its `#[instruction(discriminator = ...)]`
attribute gives it the discriminator of the interface's `Execute` instruction,
which is the instruction the Token Extensions program calls.
[Instruction discriminator](#instruction-discriminator) explains why.

```rust
#[instruction(discriminator = ExecuteInstruction::SPL_DISCRIMINATOR_SLICE)]
pub fn transfer_hook(_ctx: Context<TransferHook>, _amount: u64) -> Result<()> {
    msg!("Hello Transfer Hook!");

    Ok(())
}
```

To run the example, replace `programs/transfer-hook/src/lib.rs` with the
complete program:

```rust programs/transfer-hook/src/lib.rs
use anchor_lang::{
    prelude::*,
    system_program::{create_account, CreateAccount},
};
use anchor_spl::token_interface::{Mint, TokenAccount};
use spl_discriminator::SplDiscriminate;
use spl_tlv_account_resolution::{account::ExtraAccountMeta, state::ExtraAccountMetaList};
use spl_transfer_hook_interface::instruction::ExecuteInstruction;

declare_id!("DrWbQtYJGtsoRwzKqAbHKHKsCJJfpysudF39GBVFSxub");

#[program]
pub mod transfer_hook {
    use super::*;

    pub fn initialize_extra_account_meta_list(
        ctx: Context<InitializeExtraAccountMetaList>,
    ) -> Result<()> {
        // The hello world hook needs no extra accounts, so the list is empty
        let account_metas: Vec<ExtraAccountMeta> = vec![];

        // calculate account size
        let account_size = ExtraAccountMetaList::size_of(account_metas.len())? as u64;
        // calculate minimum required lamports
        let lamports = Rent::get()?.minimum_balance(account_size as usize);

        let mint = ctx.accounts.mint.key();
        let signer_seeds: &[&[&[u8]]] = &[&[
            b"extra-account-metas",
            mint.as_ref(),
            &[ctx.bumps.extra_account_meta_list],
        ]];

        // create ExtraAccountMetaList account
        create_account(
            CpiContext::new(
                ctx.accounts.system_program.key(),
                CreateAccount {
                    from: ctx.accounts.payer.to_account_info(),
                    to: ctx.accounts.extra_account_meta_list.to_account_info(),
                },
            )
            .with_signer(signer_seeds),
            lamports,
            account_size,
            ctx.program_id,
        )?;

        // initialize ExtraAccountMetaList account with extra accounts
        ExtraAccountMetaList::init::<ExecuteInstruction>(
            &mut ctx.accounts.extra_account_meta_list.try_borrow_mut_data()?,
            &account_metas,
        )?;

        Ok(())
    }

    // Token Extensions calls this instruction on every transfer, using the
    // Transfer Hook Interface's `Execute` discriminator
    #[instruction(discriminator = ExecuteInstruction::SPL_DISCRIMINATOR_SLICE)]
    pub fn transfer_hook(_ctx: Context<TransferHook>, _amount: u64) -> Result<()> {
        msg!("Hello Transfer Hook!");

        Ok(())
    }
}

#[derive(Accounts)]
pub struct InitializeExtraAccountMetaList<'info> {
    #[account(mut)]
    pub payer: Signer<'info>,

    /// CHECK: ExtraAccountMetaList Account, must use these seeds
    #[account(
        mut,
        seeds = [b"extra-account-metas", mint.key().as_ref()],
        bump
    )]
    pub extra_account_meta_list: UncheckedAccount<'info>,
    pub mint: InterfaceAccount<'info, Mint>,
    pub system_program: Program<'info, System>,
}

// Order of accounts matters for this struct.
// The first 4 accounts are the accounts required for token transfer (source, mint, destination, owner)
// Remaining accounts are the extra accounts required from the ExtraAccountMetaList account
// These accounts are provided via CPI to this program from the Token Extensions program
#[derive(Accounts)]
pub struct TransferHook<'info> {
    #[account(
        token::mint = mint,
        token::authority = owner,
    )]
    pub source_token: InterfaceAccount<'info, TokenAccount>,
    pub mint: InterfaceAccount<'info, Mint>,
    #[account(
        token::mint = mint,
    )]
    pub destination_token: InterfaceAccount<'info, TokenAccount>,
    /// CHECK: source token account owner, can be SystemAccount or PDA owned by another program
    pub owner: UncheckedAccount<'info>,
    /// CHECK: ExtraAccountMetaList Account
    #[account(
        seeds = [b"extra-account-metas", mint.key().as_ref()],
        bump
    )]
    pub extra_account_meta_list: UncheckedAccount<'info>,
}
```

Then point `declare_id!` at your project's program ID:

```bash Terminal
anchor keys sync
```

Next, replace `tests/transfer-hook.ts` with the test below. It creates a mint
whose Transfer Hook extension points at your program, creates two token
accounts and mints 100 tokens, creates the ExtraAccountMetaList account, then
transfers one token and prints the program logs from the transfer.

<Warning>
  **`@solana/web3.js` 1.x is no longer actively maintained**

  It receives critical fixes only. Use [Kit](/developers/docs/clients/official/javascript#solana-kit) for new code. To migrate existing code, follow the [Kit upgrade guide](https://www.solanakit.com/docs/upgrade-guide), or the [web3.js v3 migration guide](https://github.com/solana-foundation/solana-web3.js/blob/main/docs/web3js-v1-to-v3-migration.md) to keep the class-based API.

  To run this code, install 1.x with `npm install @solana/web3.js@1`. A plain `npm install @solana/web3.js` installs v3, which this code doesn't run on.
</Warning>

```ts tests/transfer-hook.ts expandable
import * as anchor from "@anchor-lang/core";
import { Program } from "@anchor-lang/core";
import { TransferHook } from "../target/types/transfer_hook";
import {
  PublicKey,
  SystemProgram,
  Transaction,
  sendAndConfirmTransaction,
  Keypair,
} from "@solana/web3.js";
import {
  ExtensionType,
  TOKEN_2022_PROGRAM_ID,
  getMintLen,
  createInitializeMintInstruction,
  createInitializeTransferHookInstruction,
  ASSOCIATED_TOKEN_PROGRAM_ID,
  createAssociatedTokenAccountInstruction,
  createMintToInstruction,
  getAssociatedTokenAddressSync,
  createTransferCheckedWithTransferHookInstruction,
} from "@solana/spl-token";

describe("transfer-hook", () => {
  // Configure the client to use the local cluster.
  const provider = anchor.AnchorProvider.env();
  anchor.setProvider(provider);

  const program = anchor.workspace.transferHook as Program<TransferHook>;
  const wallet = provider.wallet as anchor.Wallet;
  const connection = provider.connection;

  // Generate keypair to use as address for the transfer-hook enabled mint
  const mint = new Keypair();
  const decimals = 9;

  // Sender token account address
  const sourceTokenAccount = getAssociatedTokenAddressSync(
    mint.publicKey,
    wallet.publicKey,
    false,
    TOKEN_2022_PROGRAM_ID,
    ASSOCIATED_TOKEN_PROGRAM_ID
  );

  // Recipient token account address
  const recipient = Keypair.generate();
  const destinationTokenAccount = getAssociatedTokenAddressSync(
    mint.publicKey,
    recipient.publicKey,
    false,
    TOKEN_2022_PROGRAM_ID,
    ASSOCIATED_TOKEN_PROGRAM_ID
  );

  // ExtraAccountMetaList address
  // Store extra accounts required by the custom transfer hook instruction
  const [extraAccountMetaListPDA] = PublicKey.findProgramAddressSync(
    [Buffer.from("extra-account-metas"), mint.publicKey.toBuffer()],
    program.programId
  );

  it("Create Mint Account with Transfer Hook Extension", async () => {
    const extensions = [ExtensionType.TransferHook];
    const mintLen = getMintLen(extensions);
    const lamports =
      await provider.connection.getMinimumBalanceForRentExemption(mintLen);

    const transaction = new Transaction().add(
      SystemProgram.createAccount({
        fromPubkey: wallet.publicKey,
        newAccountPubkey: mint.publicKey,
        space: mintLen,
        lamports: lamports,
        programId: TOKEN_2022_PROGRAM_ID,
      }),
      createInitializeTransferHookInstruction(
        mint.publicKey,
        wallet.publicKey,
        program.programId, // Transfer Hook Program ID
        TOKEN_2022_PROGRAM_ID
      ),
      createInitializeMintInstruction(
        mint.publicKey,
        decimals,
        wallet.publicKey,
        null,
        TOKEN_2022_PROGRAM_ID
      )
    );

    const txSig = await sendAndConfirmTransaction(
      provider.connection,
      transaction,
      [wallet.payer, mint],
      { commitment: "confirmed" }
    );
    console.log(`Transaction Signature: ${txSig}`);
  });

  // Create the two token accounts for the transfer-hook enabled mint
  // Fund the sender token account with 100 tokens
  it("Create Token Accounts and Mint Tokens", async () => {
    // 100 tokens
    const amount = 100 * 10 ** decimals;

    const transaction = new Transaction().add(
      createAssociatedTokenAccountInstruction(
        wallet.publicKey,
        sourceTokenAccount,
        wallet.publicKey,
        mint.publicKey,
        TOKEN_2022_PROGRAM_ID,
        ASSOCIATED_TOKEN_PROGRAM_ID
      ),
      createAssociatedTokenAccountInstruction(
        wallet.publicKey,
        destinationTokenAccount,
        recipient.publicKey,
        mint.publicKey,
        TOKEN_2022_PROGRAM_ID,
        ASSOCIATED_TOKEN_PROGRAM_ID
      ),
      createMintToInstruction(
        mint.publicKey,
        sourceTokenAccount,
        wallet.publicKey,
        amount,
        [],
        TOKEN_2022_PROGRAM_ID
      )
    );

    const txSig = await sendAndConfirmTransaction(
      connection,
      transaction,
      [wallet.payer],
      { commitment: "confirmed" }
    );

    console.log(`Transaction Signature: ${txSig}`);
  });

  // Account to store extra accounts required by the transfer hook instruction
  it("Create ExtraAccountMetaList Account", async () => {
    const txSig = await program.methods
      .initializeExtraAccountMetaList()
      .accountsPartial({
        mint: mint.publicKey,
        extraAccountMetaList: extraAccountMetaListPDA,
      })
      .rpc({ commitment: "confirmed" });
    console.log("Transaction Signature:", txSig);
  });

  it("Transfer Hook with Extra Account Meta", async () => {
    // 1 token
    const amount = 1 * 10 ** decimals;
    const amountBigInt = BigInt(amount);

    // Transfer instruction with the extra accounts the hook needs
    const transferInstruction =
      await createTransferCheckedWithTransferHookInstruction(
        connection,
        sourceTokenAccount,
        mint.publicKey,
        destinationTokenAccount,
        wallet.publicKey,
        amountBigInt,
        decimals,
        [],
        "confirmed",
        TOKEN_2022_PROGRAM_ID
      );

    const transaction = new Transaction().add(transferInstruction);

    const txSig = await sendAndConfirmTransaction(
      connection,
      transaction,
      [wallet.payer],
      { commitment: "confirmed" }
    );
    console.log("Transfer Signature:", txSig);

    // Print the log lines the transfer hook program wrote
    const tx = await connection.getTransaction(txSig, {
      commitment: "confirmed",
      maxSupportedTransactionVersion: 0,
    });
    console.log(
      tx?.meta?.logMessages?.filter((log) => log.startsWith("Program log:"))
    );
  });
});
```

Run the test:

```bash Terminal
anchor test --validator legacy
```

The output looks like this, with different signatures. The logs show the Token
Extensions program calling your hook during the transfer:

```txt
  transfer-hook
Transaction Signature: 3c6S1wKHCUyLn3jhL12eSG2sh2KPgbm2LQ9nzTkNkEyqj9iZtpGpogqvA4VvHD2xXp15kfokEMQkKkJYh4J7xUuX
    ✔ Create Mint Account with Transfer Hook Extension (391ms)
Transaction Signature: 5cvRC6J5dq8BLYTJt6qzbmeChPxo5ozh2LWcT6yPU1ZJpvVAoVKeUwFFfGamFSxYFxAvW9xj9QiFBE4kYCGCMoKD
    ✔ Create Token Accounts and Mint Tokens (438ms)
Transaction Signature: 2QwKUSkhQRB26FU4gQzaCpQySp48M6Uo5Wzfjcc2cBhrJwj5cfkzUNuumWswHqdT8ZBb1NeEVDh2uMamnzVWACqX
    ✔ Create ExtraAccountMetaList Account (463ms)
Transfer Signature: YyJAExq7XeSQnDZfitH2Uv9pjbGyhPbjPS1EEBYBukpv9EauEGsN5zJrHggRhv5JpoZv4u5LUshPbLQD1QRAtaa
[
  'Program log: Instruction: TransferChecked',
  'Program log: Instruction: TransferHook',
  'Program log: Hello Transfer Hook!'
]
    ✔ Transfer Hook with Extra Account Meta (373ms)

  4 passing (2s)
```

In this function you can now add your additional logic. For example, you could
let the transfer fail whenever more than 50 tokens are transferred. `amount` is
in base units, so scale the limit by the mint's decimals: the test mints a token
with 9 decimals and transfers `1 * 10 ** decimals` (one token), which a plain
`amount > 50` check would reject. Put the `MyError` enum outside the
`#[program]` module:

```rust
#[error_code]
pub enum MyError {
    #[msg("The amount is too big")]
    AmountTooBig,
}

#[instruction(discriminator = ExecuteInstruction::SPL_DISCRIMINATOR_SLICE)]
pub fn transfer_hook(ctx: Context<TransferHook>, amount: u64) -> Result<()> {
    msg!("Hello Transfer Hook!");

    // `amount` is in base units: 50 tokens = 50 * 10^decimals
    let max_amount = 50 * 10u64.pow(ctx.accounts.mint.decimals as u32);
    if amount > max_amount {
        return err!(MyError::AmountTooBig);
    }

    Ok(())
}
```

With this check, the test's one-token transfer still passes, and a transfer of
51 tokens fails with `AmountTooBig`.

To create a mint that uses your hook from the command line instead, for example
on devnet after you deploy the program there, use the `spl-token` CLI. The mint
still needs its ExtraAccountMetaList account, which
`initialize_extra_account_meta_list` creates.

```shell
spl-token --program-id TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb create-token --transfer-hook yourTransferHookProgramId
```

## Counter Transfer hook

The next example increases a counter every time your token is transferred.

If you want to add logic to your transfer hook that needs additional accounts
you need to add them to the ExtraAccountMetaList account. In our case here we
want a PDA that saves how often the token has been transferred.

This can be done by adding the following code to the
`initialize_extra_account_meta_list` instruction:

```rust
let account_metas = vec![
    ExtraAccountMeta::new_with_seeds(
        &[Seed::Literal {
            bytes: "counter".as_bytes().to_vec(),
        }],
        false, // is_signer
        true,  // is_writable
    )?,
];
```

And we also need to create this account when we initialize the new mint account
and we need to pass it in every time we transfer the token.

```rust
#[derive(Accounts)]
pub struct InitializeExtraAccountMetaList<'info> {
    #[account(mut)]
    pub payer: Signer<'info>,

    /// CHECK: ExtraAccountMetaList Account, must use these seeds
    #[account(
        mut,
        seeds = [b"extra-account-metas", mint.key().as_ref()],
        bump
    )]
    pub extra_account_meta_list: UncheckedAccount<'info>,
    pub mint: InterfaceAccount<'info, Mint>,
    #[account(
        init_if_needed,
        seeds = [b"counter"],
        bump,
        payer = payer,
        space = 16
    )]
    pub counter_account: Account<'info, CounterAccount>,
    pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct TransferHook<'info> {
    #[account(
        token::mint = mint,
        token::authority = owner,
    )]
    pub source_token: InterfaceAccount<'info, TokenAccount>,
    pub mint: InterfaceAccount<'info, Mint>,
    #[account(
        token::mint = mint,
    )]
    pub destination_token: InterfaceAccount<'info, TokenAccount>,
    /// CHECK: source token account owner, can be SystemAccount or PDA owned by another program
    pub owner: UncheckedAccount<'info>,
    /// CHECK: ExtraAccountMetaList Account
    #[account(
        seeds = [b"extra-account-metas", mint.key().as_ref()],
        bump
    )]
    pub extra_account_meta_list: UncheckedAccount<'info>,
    #[account(
        mut,
        seeds = [b"counter"],
        bump
    )]
    pub counter_account: Account<'info, CounterAccount>,
}
```

`init_if_needed` creates the counter account if it doesn't exist yet. It needs
Anchor's `init-if-needed` feature, which you turn on below.

And the account will hold a `u64` counter variable (`space = 16` is the 8-byte
account discriminator plus the `u64`):

```rust
#[account]
pub struct CounterAccount {
    counter: u64,
}
```

Now in our transfer hook function we can just increase this counter by one every
time it gets called:

```rust
#[instruction(discriminator = ExecuteInstruction::SPL_DISCRIMINATOR_SLICE)]
pub fn transfer_hook(ctx: Context<TransferHook>, _amount: u64) -> Result<()> {
    ctx.accounts.counter_account.counter =
        ctx.accounts.counter_account.counter.checked_add(1).unwrap();
    msg!(
        "This token has been transferred {0} times",
        ctx.accounts.counter_account.counter
    );

    Ok(())
}
```

In the client these additional accounts are added automatically by a helper
function: `getTransferCheckedWithTransferHookInstructionAsync` from
`@solana-program/token-2022` on web3.js v3, or
`createTransferCheckedWithTransferHookInstruction` from `@solana/spl-token` on
legacy web3.js. The v3 helper takes a Kit RPC, so build one for the same
endpoint as the `Connection` with `createSolanaRpc` from `@solana/kit`.

<CodeGroup>

```js title="Web3.js v3"
const rpc = createSolanaRpc(connection.rpcEndpoint);

let transferInstructionWithHelper =
  await getTransferCheckedWithTransferHookInstructionAsync(
    { rpc },
    {
      source: sourceTokenAccount.toBase58(),
      mint: mint.publicKey.toBase58(),
      destination: destinationTokenAccount.toBase58(),
      authority: wallet, // a v3 Keypair, passed as the transfer's signer
      amount: amountBigInt,
      decimals
    }
  );
```

<Warning>
  **`@solana/web3.js` 1.x is no longer actively maintained**

  It receives critical fixes only. Use [Kit](/developers/docs/clients/official/javascript#solana-kit) for new code. To migrate existing code, follow the [Kit upgrade guide](https://www.solanakit.com/docs/upgrade-guide), or the [web3.js v3 migration guide](https://github.com/solana-foundation/solana-web3.js/blob/main/docs/web3js-v1-to-v3-migration.md) to keep the class-based API.

  To run this code, install 1.x with `npm install @solana/web3.js@1`. A plain `npm install @solana/web3.js` installs v3, which this code doesn't run on.
</Warning>

```js title="Web3.js (legacy)"
let transferInstructionWithHelper =
  await createTransferCheckedWithTransferHookInstruction(
    connection,
    sourceTokenAccount,
    mint.publicKey,
    destinationTokenAccount,
    wallet.publicKey,
    amountBigInt,
    decimals,
    [],
    "confirmed",
    TOKEN_2022_PROGRAM_ID
  );
```

</CodeGroup>

To run the example, turn on the `init-if-needed` feature from the program's
folder:

```bash Terminal
cd programs/transfer-hook
cargo add anchor-lang@1.1.2 --features init-if-needed
cd ../..
```

Replace `programs/transfer-hook/src/lib.rs` with the complete counter program,
then run `anchor keys sync` again:

<Accordion title="Complete counter program">

```rust programs/transfer-hook/src/lib.rs
use anchor_lang::{
    prelude::*,
    system_program::{create_account, CreateAccount},
};
use anchor_spl::token_interface::{Mint, TokenAccount};
use spl_discriminator::SplDiscriminate;
use spl_tlv_account_resolution::{
    account::ExtraAccountMeta, seeds::Seed, state::ExtraAccountMetaList,
};
use spl_transfer_hook_interface::instruction::ExecuteInstruction;

declare_id!("DrWbQtYJGtsoRwzKqAbHKHKsCJJfpysudF39GBVFSxub");

#[program]
pub mod transfer_hook {
    use super::*;

    pub fn initialize_extra_account_meta_list(
        ctx: Context<InitializeExtraAccountMetaList>,
    ) -> Result<()> {
        let account_metas = vec![
            ExtraAccountMeta::new_with_seeds(
                &[Seed::Literal {
                    bytes: "counter".as_bytes().to_vec(),
                }],
                false, // is_signer
                true,  // is_writable
            )?,
        ];

        // calculate account size
        let account_size = ExtraAccountMetaList::size_of(account_metas.len())? as u64;
        // calculate minimum required lamports
        let lamports = Rent::get()?.minimum_balance(account_size as usize);

        let mint = ctx.accounts.mint.key();
        let signer_seeds: &[&[&[u8]]] = &[&[
            b"extra-account-metas",
            mint.as_ref(),
            &[ctx.bumps.extra_account_meta_list],
        ]];

        // create ExtraAccountMetaList account
        create_account(
            CpiContext::new(
                ctx.accounts.system_program.key(),
                CreateAccount {
                    from: ctx.accounts.payer.to_account_info(),
                    to: ctx.accounts.extra_account_meta_list.to_account_info(),
                },
            )
            .with_signer(signer_seeds),
            lamports,
            account_size,
            ctx.program_id,
        )?;

        // initialize ExtraAccountMetaList account with extra accounts
        ExtraAccountMetaList::init::<ExecuteInstruction>(
            &mut ctx.accounts.extra_account_meta_list.try_borrow_mut_data()?,
            &account_metas,
        )?;

        Ok(())
    }

    #[instruction(discriminator = ExecuteInstruction::SPL_DISCRIMINATOR_SLICE)]
    pub fn transfer_hook(ctx: Context<TransferHook>, _amount: u64) -> Result<()> {
        ctx.accounts.counter_account.counter =
            ctx.accounts.counter_account.counter.checked_add(1).unwrap();
        msg!(
            "This token has been transferred {0} times",
            ctx.accounts.counter_account.counter
        );

        Ok(())
    }
}

#[derive(Accounts)]
pub struct InitializeExtraAccountMetaList<'info> {
    #[account(mut)]
    pub payer: Signer<'info>,

    /// CHECK: ExtraAccountMetaList Account, must use these seeds
    #[account(
        mut,
        seeds = [b"extra-account-metas", mint.key().as_ref()],
        bump
    )]
    pub extra_account_meta_list: UncheckedAccount<'info>,
    pub mint: InterfaceAccount<'info, Mint>,
    #[account(
        init_if_needed,
        seeds = [b"counter"],
        bump,
        payer = payer,
        space = 16
    )]
    pub counter_account: Account<'info, CounterAccount>,
    pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct TransferHook<'info> {
    #[account(
        token::mint = mint,
        token::authority = owner,
    )]
    pub source_token: InterfaceAccount<'info, TokenAccount>,
    pub mint: InterfaceAccount<'info, Mint>,
    #[account(
        token::mint = mint,
    )]
    pub destination_token: InterfaceAccount<'info, TokenAccount>,
    /// CHECK: source token account owner, can be SystemAccount or PDA owned by another program
    pub owner: UncheckedAccount<'info>,
    /// CHECK: ExtraAccountMetaList Account
    #[account(
        seeds = [b"extra-account-metas", mint.key().as_ref()],
        bump
    )]
    pub extra_account_meta_list: UncheckedAccount<'info>,
    #[account(
        mut,
        seeds = [b"counter"],
        bump
    )]
    pub counter_account: Account<'info, CounterAccount>,
}

#[account]
pub struct CounterAccount {
    counter: u64,
}
```

</Accordion>

The hello-world test runs this program without changes:
`initializeExtraAccountMetaList()` fills in `counterAccount` from the seeds in
the program's IDL, and `createTransferCheckedWithTransferHookInstruction` adds
the counter PDA to the transfer. To also read the saved count, add these lines
at the end of the `Transfer Hook with Extra Account Meta` test in
`tests/transfer-hook.ts`:

```ts
    // Read the count that the hook saved
    const [counterPDA] = PublicKey.findProgramAddressSync(
      [Buffer.from("counter")],
      program.programId
    );
    const counterAccount = await program.account.counterAccount.fetch(
      counterPDA
    );
    console.log("Transfer count:", counterAccount.counter.toString());
```

Then run `anchor test --validator legacy`. The transfer's logs now include the
count:

```txt
  transfer-hook
Transaction Signature: kwSy3FANcR8T27yf17bwtGYtECcxhpmAEEURbmDrVRxnfUQEFVHEeTA8NTis7Y6ykq55gdvkdeabqAMutNDoKxs
    ✔ Create Mint Account with Transfer Hook Extension (344ms)
Transaction Signature: HAYM1PjM7FqZBqMFPz2wBMZu5JgV6KogNDtdk1ppjth6cmW9RbpGmL6RPcW6WMphgoZdBfrB3smWkQJP82cpWcP
    ✔ Create Token Accounts and Mint Tokens (430ms)
Transaction Signature: 4aVcgHYRj8Boyp54pwdsSqGMeNgoqcpLFPKLuPSsus5Ph8vP6EJqV7myhPeLRt52fCFm66PZMgw7iBaBAxf9NoDL
    ✔ Create ExtraAccountMetaList Account (469ms)
Transfer Signature: 4boZq3GvHJeLFyWYZo5qyTuGJejLPCPGRKC4Y9Z1jfBiAisFjCMjXkDV1VitRREAKeCeRG9A6DFTzcFCC2Sy5bHa
[
  'Program log: Instruction: TransferChecked',
  'Program log: Instruction: TransferHook',
  'Program log: This token has been transferred 1 times'
]
Transfer count: 1
    ✔ Transfer Hook with Extra Account Meta (369ms)

  4 passing (2s)
```

<Security title="Anyone can call your hook directly">

Since here we are increasing a counter whenever the token is transferred we need
to make sure that the transfer hook instruction can only be called during a
transfer, otherwise someone could just call the transfer hook instruction
directly and mess up our counter. This is a check you should add to any of your
transfer hooks.

</Security>

The source token account carries a `TransferHookAccount` extension whose
`transferring` flag Token-2022 sets only while it runs a transfer. You can check
it like this (add the `use` lines at the top of `lib.rs`, and the error enum and
the function outside the `#[program]` module):

```rust
use anchor_spl::token_2022::spl_token_2022::{
    extension::{transfer_hook::TransferHookAccount, BaseStateWithExtensions, StateWithExtensions},
    state::Account as Token2022Account,
};

#[error_code]
pub enum TransferError {
    #[msg("The token is not currently transferring")]
    IsNotCurrentlyTransferring,
}

fn assert_is_transferring(ctx: &Context<TransferHook>) -> Result<()> {
    let source_token_info = ctx.accounts.source_token.to_account_info();
    let account_data = source_token_info.try_borrow_data()?;
    let account = StateWithExtensions::<Token2022Account>::unpack(&account_data)?;
    let account_extension = account.get_extension::<TransferHookAccount>()?;

    if !bool::from(account_extension.transferring) {
        return err!(TransferError::IsNotCurrentlyTransferring);
    }

    Ok(())
}
```

And then call it at the start of your `transfer_hook` function:

```rust
    #[instruction(discriminator = ExecuteInstruction::SPL_DISCRIMINATOR_SLICE)]
    pub fn transfer_hook(ctx: Context<TransferHook>, _amount: u64) -> Result<()> {
        // Fail this instruction if it is not called from within a transfer hook
        assert_is_transferring(&ctx)?;

        ctx.accounts.counter_account.counter =
            ctx.accounts.counter_account.counter.checked_add(1).unwrap();
        msg!(
            "This token has been transferred {0} times",
            ctx.accounts.counter_account.counter
        );

        Ok(())
    }
```

A direct call to the hook (outside a transfer) now fails with
`IsNotCurrentlyTransferring`. To see it, add this test at the end of the
`describe` block in `tests/transfer-hook.ts`, then run
`anchor test --validator legacy` again:

```ts
  it("Rejects a direct call to the hook", async () => {
    try {
      await program.methods
        .transferHook(new anchor.BN(1))
        .accountsPartial({
          sourceToken: sourceTokenAccount,
          mint: mint.publicKey,
          destinationToken: destinationTokenAccount,
          owner: wallet.publicKey,
          extraAccountMetaList: extraAccountMetaListPDA,
        })
        .rpc();
    } catch (error) {
      console.log((error as anchor.AnchorError).error.errorCode.code);
      return;
    }
    throw new Error("The direct call should have failed");
  });
```

The transfer still passes, and the new test prints the error code:

```txt
Transfer Signature: 3EvbtHtMgNu8WZgnaafxCgRrrzXujzWTU5aqe1PBf1u2ENELJoDkJWCV3t6kC4CgDEGw54TUCjdPgqJiVXdDxtCD
[
  'Program log: Instruction: TransferChecked',
  'Program log: Instruction: TransferHook',
  'Program log: This token has been transferred 1 times'
]
Transfer count: 1
    ✔ Transfer Hook with Extra Account Meta (421ms)
IsNotCurrentlyTransferring
    ✔ Rejects a direct call to the hook

  5 passing (2s)
```

## Transfer Hook with wSOL Transfer fee (advanced example)

In the next part of this guide, we will build a more advanced Transfer Hook
program using the Anchor framework. This program will require the sender to pay
a wSOL fee for every token transfer.

The wSOL transfers will be executed using a delegate that is a PDA derived from
the Transfer Hook program. This is necessary because the signature from the
initial sender of the token transfer instruction is not accessible in the
Transfer Hook program.

This program has 2 instructions:

1. `initialize_extra_account_meta_list`: Creates an account that stores a list
   of extra accounts required by the `transfer_hook` instruction.
2. `transfer_hook`: This instruction is invoked via CPI on every token transfer
   to perform a wrapped SOL token transfer. Its discriminator matches the
   interface's `Execute` instruction, as in the earlier examples.

This program will require the sender to pay a fee in wrapped SOL (wSOL) on every
token transfer. The complete program is under
[Build the program](#build-the-program).

### Getting Started

Continue in the same project. Replace `programs/transfer-hook/src/lib.rs` with
the starter code below, which is scaffolded for the program we will be creating:

```rust programs/transfer-hook/src/lib.rs
use anchor_lang::{
    prelude::*,
    system_program::{create_account, CreateAccount},
};
use anchor_spl::{
    associated_token::AssociatedToken,
    token_interface::{transfer_checked, Mint, TokenAccount, TokenInterface, TransferChecked},
};
use spl_discriminator::SplDiscriminate;
use spl_tlv_account_resolution::{
    account::ExtraAccountMeta, seeds::Seed, state::ExtraAccountMetaList,
};
use spl_transfer_hook_interface::instruction::ExecuteInstruction;

declare_id!("E6wu6Nykdra8gXs57Zqo7hY6DLaWugTmD3uuuBmX2Vxt");

#[program]
pub mod transfer_hook {
    use super::*;

    pub fn initialize_extra_account_meta_list(
        ctx: Context<InitializeExtraAccountMetaList>,
    ) -> Result<()> {
        Ok(())
    }

    #[instruction(discriminator = ExecuteInstruction::SPL_DISCRIMINATOR_SLICE)]
    pub fn transfer_hook(ctx: Context<TransferHook>, amount: u64) -> Result<()> {
        Ok(())
    }
}

#[derive(Accounts)]
pub struct InitializeExtraAccountMetaList {}

#[derive(Accounts)]
pub struct TransferHook {}
```

Point `declare_id!` at your program ID and build the starter program:

```bash Terminal
anchor keys sync
anchor build
```

The build prints warnings about unused imports and variables. The next steps
use them.

### Initialize ExtraAccountMetas Account Instruction

In this step, we will implement the `initialize_extra_account_meta_list`
instruction for our Transfer Hook program. This instruction creates an
ExtraAccountMetas account, which will store the additional accounts required by
our `transfer_hook` instruction.

In this example, the `initialize_extra_account_meta_list` instruction requires 7
accounts:

- `payer`: The account used to pay for the creation of the ExtraAccountMetas
  account.
- `extra_account_meta_list`: The ExtraAccountMetas account created to store the
  list of accounts required by our `transfer_hook` instruction.
- `mint`: The Mint Account that points to this Transfer Hook program. The mint
  address is a required seed for deriving the `extra_account_meta_list` PDA.
- `wsol_mint`: The wrapped SOL mint.
- `token_program`: The original Token program ID
- `associated_token_program`: The Associated Token program ID.
- `system_program`: The system program, which is a required account when
  creating new accounts.

The addresses for `mint`, `wsol_mint`, and `associated_token_program` will be
used to derive the addresses for the wSOL Associated Token Accounts. These
accounts are required by the `transfer_hook` instruction and will be stored on
the ExtraAccountMetas account.

Update the `InitializeExtraAccountMetaList` struct by replacing the following
starter code:

```rust
#[derive(Accounts)]
pub struct InitializeExtraAccountMetaList {}
```

With the code provided below:

```rust
#[derive(Accounts)]
pub struct InitializeExtraAccountMetaList<'info> {
    #[account(mut)]
    pub payer: Signer<'info>,

    /// CHECK: ExtraAccountMetaList Account, must use these seeds
    #[account(
        mut,
        seeds = [b"extra-account-metas", mint.key().as_ref()],
        bump
    )]
    pub extra_account_meta_list: UncheckedAccount<'info>,
    pub mint: InterfaceAccount<'info, Mint>,
    pub wsol_mint: InterfaceAccount<'info, Mint>,
    pub token_program: Interface<'info, TokenInterface>,
    pub associated_token_program: Program<'info, AssociatedToken>,
    pub system_program: Program<'info, System>,
}
```

Next, update the `initialize_extra_account_meta_list` instruction by replacing
the following starter code:

```rust
pub fn initialize_extra_account_meta_list(
    ctx: Context<InitializeExtraAccountMetaList>,
) -> Result<()> {
    Ok(())
}
```

With the code below:

```rust
pub fn initialize_extra_account_meta_list(
    ctx: Context<InitializeExtraAccountMetaList>,
) -> Result<()> {
    // index 0-3 are the accounts required for token transfer (source, mint, destination, owner)
    // index 4 is address of ExtraAccountMetaList account
    let account_metas = vec![
        // index 5, wrapped SOL mint
        ExtraAccountMeta::new_with_pubkey(&ctx.accounts.wsol_mint.key(), false, false)?,
        // index 6, token program
        ExtraAccountMeta::new_with_pubkey(&ctx.accounts.token_program.key(), false, false)?,
        // index 7, associated token program
        ExtraAccountMeta::new_with_pubkey(
            &ctx.accounts.associated_token_program.key(),
            false,
            false,
        )?,
        // index 8, delegate PDA
        ExtraAccountMeta::new_with_seeds(
            &[Seed::Literal {
                bytes: "delegate".as_bytes().to_vec(),
            }],
            false, // is_signer
            true,  // is_writable
        )?,
        // index 9, delegate wrapped SOL token account
        ExtraAccountMeta::new_external_pda_with_seeds(
            7, // associated token program index
            &[
                Seed::AccountKey { index: 8 }, // owner index (delegate PDA)
                Seed::AccountKey { index: 6 }, // token program index
                Seed::AccountKey { index: 5 }, // wsol mint index
            ],
            false, // is_signer
            true,  // is_writable
        )?,
        // index 10, sender wrapped SOL token account
        ExtraAccountMeta::new_external_pda_with_seeds(
            7, // associated token program index
            &[
                Seed::AccountKey { index: 3 }, // owner index
                Seed::AccountKey { index: 6 }, // token program index
                Seed::AccountKey { index: 5 }, // wsol mint index
            ],
            false, // is_signer
            true,  // is_writable
        )?,
    ];

    // calculate account size
    let account_size = ExtraAccountMetaList::size_of(account_metas.len())? as u64;
    // calculate minimum required lamports
    let lamports = Rent::get()?.minimum_balance(account_size as usize);

    let mint = ctx.accounts.mint.key();
    let signer_seeds: &[&[&[u8]]] = &[&[
        b"extra-account-metas",
        mint.as_ref(),
        &[ctx.bumps.extra_account_meta_list],
    ]];

    // create ExtraAccountMetaList account
    create_account(
        CpiContext::new(
            ctx.accounts.system_program.key(),
            CreateAccount {
                from: ctx.accounts.payer.to_account_info(),
                to: ctx.accounts.extra_account_meta_list.to_account_info(),
            },
        )
        .with_signer(signer_seeds),
        lamports,
        account_size,
        ctx.program_id,
    )?;

    // initialize ExtraAccountMetaList account with extra accounts
    ExtraAccountMetaList::init::<ExecuteInstruction>(
        &mut ctx.accounts.extra_account_meta_list.try_borrow_mut_data()?,
        &account_metas,
    )?;

    Ok(())
}
```

Let's walk through the updated instruction logic. We begin by listing the
additional accounts that need to be stored on the ExtraAccountMetas account.

```rust
// index 0-3 are the accounts required for token transfer (source, mint, destination, owner)
// index 4 is address of ExtraAccountMetaList account
let account_metas = vec![
    // index 5, wrapped SOL mint
    ExtraAccountMeta::new_with_pubkey(&ctx.accounts.wsol_mint.key(), false, false)?,
    // index 6, token program
    ExtraAccountMeta::new_with_pubkey(&ctx.accounts.token_program.key(), false, false)?,
    // index 7, associated token program
    ExtraAccountMeta::new_with_pubkey(
        &ctx.accounts.associated_token_program.key(),
        false,
        false,
    )?,
    // index 8, delegate PDA
    ExtraAccountMeta::new_with_seeds(
        &[Seed::Literal {
            bytes: "delegate".as_bytes().to_vec(),
        }],
        false, // is_signer
        true,  // is_writable
    )?,
    // index 9, delegate wrapped SOL token account
    ExtraAccountMeta::new_external_pda_with_seeds(
        7, // associated token program index
        &[
            Seed::AccountKey { index: 8 }, // owner index (delegate PDA)
            Seed::AccountKey { index: 6 }, // token program index
            Seed::AccountKey { index: 5 }, // wsol mint index
        ],
        false, // is_signer
        true,  // is_writable
    )?,
    // index 10, sender wrapped SOL token account
    ExtraAccountMeta::new_external_pda_with_seeds(
        7, // associated token program index
        &[
            Seed::AccountKey { index: 3 }, // owner index
            Seed::AccountKey { index: 6 }, // token program index
            Seed::AccountKey { index: 5 }, // wsol mint index
        ],
        false, // is_signer
        true,  // is_writable
    )?,
];
```

There are three methods for storing these accounts:

1. Directly store the account address:
   - Wrapped SOL mint address
   - Token Program ID
   - Associated Token Program ID

```rust
// index 5, wrapped SOL mint
ExtraAccountMeta::new_with_pubkey(&ctx.accounts.wsol_mint.key(), false, false)?,
// index 6, token program
ExtraAccountMeta::new_with_pubkey(&ctx.accounts.token_program.key(), false, false)?,
// index 7, associated token program
ExtraAccountMeta::new_with_pubkey(
    &ctx.accounts.associated_token_program.key(),
    false,
    false,
)?,

```

2. Store the seeds to derive a PDA for the Transfer Hook program:
   - Delegate PDA

```rust
// index 8, delegate PDA
ExtraAccountMeta::new_with_seeds(
    &[Seed::Literal {
        bytes: "delegate".as_bytes().to_vec(),
    }],
    false, // is_signer
    true,  // is_writable
)?,

```

3. Store the seeds to derive a PDA for a program other than the Transfer Hook
   program:
   - Delegate wSOL Associated Token Account
   - Sender wSOL Associated Token Account

```rust
// index 9, delegate wrapped SOL token account
ExtraAccountMeta::new_external_pda_with_seeds(
    7, // associated token program index
    &[
        Seed::AccountKey { index: 8 }, // owner index (delegate PDA)
        Seed::AccountKey { index: 6 }, // token program index
        Seed::AccountKey { index: 5 }, // wsol mint index
    ],
    false, // is_signer
    true,  // is_writable
)?,
// index 10, sender wrapped SOL token account
ExtraAccountMeta::new_external_pda_with_seeds(
    7, // associated token program index
    &[
        Seed::AccountKey { index: 3 }, // owner index
        Seed::AccountKey { index: 6 }, // token program index
        Seed::AccountKey { index: 5 }, // wsol mint index
    ],
    false, // is_signer
    true,  // is_writable
)?,
```

Next, we calculate the size and rent required to store the list of
ExtraAccountMetas.

```rust
// calculate account size
let account_size = ExtraAccountMetaList::size_of(account_metas.len())? as u64;
// calculate minimum required lamports
let lamports = Rent::get()?.minimum_balance(account_size as usize);
```

Next, we make a CPI to the System Program to create an account and set the
Transfer Hook Program as the owner. The PDA seeds are included as signer seeds
on the CPI because we are using the PDA as the address of the new account. In
Anchor 1.x, `CpiContext::new` takes the address of the program being invoked,
here `ctx.accounts.system_program.key()`.

```rust
let mint = ctx.accounts.mint.key();
let signer_seeds: &[&[&[u8]]] = &[&[
    b"extra-account-metas",
    mint.as_ref(),
    &[ctx.bumps.extra_account_meta_list],
]];

// create ExtraAccountMetaList account
create_account(
    CpiContext::new(
        ctx.accounts.system_program.key(),
        CreateAccount {
            from: ctx.accounts.payer.to_account_info(),
            to: ctx.accounts.extra_account_meta_list.to_account_info(),
        },
    )
    .with_signer(signer_seeds),
    lamports,
    account_size,
    ctx.program_id,
)?;
```

Once we've created the account, we initialize the account data to store the list
of ExtraAccountMetas.

```rust
// initialize ExtraAccountMetaList account with extra accounts
ExtraAccountMetaList::init::<ExecuteInstruction>(
    &mut ctx.accounts.extra_account_meta_list.try_borrow_mut_data()?,
    &account_metas,
)?;
```

<Info>

In this example, we are not using the Transfer Hook interface to create the
ExtraAccountMetas account.

</Info>

### Custom Transfer Hook Instruction

Next, let's implement the custom `transfer_hook` instruction. This is the
instruction the Token Extension program will invoke on every token transfer.

In this example, we will require a fee paid in wSOL for every token transfer.
For simplicity, the fee amount equals the token transfer amount.

Update the `TransferHook` struct by replacing the following starter code:

```rust
#[derive(Accounts)]
pub struct TransferHook {}
```

With the updated code below:

<Info>

Note that the order of accounts in this struct matters. This is the order in
which the Token Extensions program provides these accounts when it CPIs to this
Transfer Hook program.

</Info>

```rust
// Order of accounts matters for this struct.
// The first 4 accounts are the accounts required for token transfer (source, mint, destination, owner)
// Remaining accounts are the extra accounts required from the ExtraAccountMetaList account
// These accounts are provided via CPI to this program from the Token Extensions program
#[derive(Accounts)]
pub struct TransferHook<'info> {
    #[account(
        token::mint = mint,
        token::authority = owner,
    )]
    pub source_token: InterfaceAccount<'info, TokenAccount>,
    pub mint: InterfaceAccount<'info, Mint>,
    #[account(
        token::mint = mint,
    )]
    pub destination_token: InterfaceAccount<'info, TokenAccount>,
    /// CHECK: source token account owner, can be SystemAccount or PDA owned by another program
    pub owner: UncheckedAccount<'info>,
    /// CHECK: ExtraAccountMetaList Account
    #[account(
        seeds = [b"extra-account-metas", mint.key().as_ref()],
        bump
    )]
    pub extra_account_meta_list: UncheckedAccount<'info>,
    pub wsol_mint: InterfaceAccount<'info, Mint>,
    pub token_program: Interface<'info, TokenInterface>,
    pub associated_token_program: Program<'info, AssociatedToken>,
    #[account(
        mut,
        seeds = [b"delegate"],
        bump
    )]
    pub delegate: SystemAccount<'info>,
    #[account(
        mut,
        token::mint = wsol_mint,
        token::authority = delegate,
    )]
    pub delegate_wsol_token_account: Box<InterfaceAccount<'info, TokenAccount>>,
    #[account(
        mut,
        token::mint = wsol_mint,
        token::authority = owner,
    )]
    pub sender_wsol_token_account: Box<InterfaceAccount<'info, TokenAccount>>,
}
```

The first 4 accounts are the accounts required by the initial token transfer.

```rust
#[account(
    token::mint = mint,
    token::authority = owner,
)]
pub source_token: InterfaceAccount<'info, TokenAccount>,
pub mint: InterfaceAccount<'info, Mint>,
#[account(
    token::mint = mint,
)]
pub destination_token: InterfaceAccount<'info, TokenAccount>,
/// CHECK: source token account owner, can be SystemAccount or PDA owned by another program
pub owner: UncheckedAccount<'info>,
```

The 5th account is the address of the ExtraAccountMeta account that stores the
list of extra accounts required by our `transfer_hook` instruction.

```rust
/// CHECK: ExtraAccountMetaList Account
#[account(
    seeds = [b"extra-account-metas", mint.key().as_ref()],
    bump
)]
pub extra_account_meta_list: UncheckedAccount<'info>,
```

The remaining accounts are the accounts listed in the ExtraAccountMetas account
in the order we defined them in the `initialize_extra_account_meta_list`
instruction.

```rust
pub wsol_mint: InterfaceAccount<'info, Mint>,
pub token_program: Interface<'info, TokenInterface>,
pub associated_token_program: Program<'info, AssociatedToken>,
#[account(
    mut,
    seeds = [b"delegate"],
    bump
)]
pub delegate: SystemAccount<'info>,
#[account(
    mut,
    token::mint = wsol_mint,
    token::authority = delegate,
)]
pub delegate_wsol_token_account: Box<InterfaceAccount<'info, TokenAccount>>,
#[account(
    mut,
    token::mint = wsol_mint,
    token::authority = owner,
)]
pub sender_wsol_token_account: Box<InterfaceAccount<'info, TokenAccount>>,
```

The two wSOL token accounts are boxed, which keeps them on the heap. Anchor
deserializes all 11 accounts in one function (`try_accounts`). Without the
boxes, that function's stack frame grows past the 4,096-byte limit for one
function: `anchor build` prints `Stack offset of ... exceeded max offset of
4096`, and the transfer fails with `Access violation in stack frame`.

Next, update the `transfer_hook` instruction by replacing the following starter
code:

```rust
#[instruction(discriminator = ExecuteInstruction::SPL_DISCRIMINATOR_SLICE)]
pub fn transfer_hook(ctx: Context<TransferHook>, amount: u64) -> Result<()> {
    Ok(())
}
```

With the updated code below:

```rust
// Require SOL fee on transfer, lamport fee is equal to transfer amount
// If this fails, the initial token transfer fails
#[instruction(discriminator = ExecuteInstruction::SPL_DISCRIMINATOR_SLICE)]
pub fn transfer_hook(ctx: Context<TransferHook>, amount: u64) -> Result<()> {
    msg!("Transfer WSOL using delegate PDA");

    let signer_seeds: &[&[&[u8]]] = &[&[b"delegate", &[ctx.bumps.delegate]]];

    // transfer WSOL from sender to delegate token account using delegate PDA
    transfer_checked(
        CpiContext::new(
            ctx.accounts.token_program.key(),
            TransferChecked {
                from: ctx.accounts.sender_wsol_token_account.to_account_info(),
                mint: ctx.accounts.wsol_mint.to_account_info(),
                to: ctx.accounts.delegate_wsol_token_account.to_account_info(),
                authority: ctx.accounts.delegate.to_account_info(),
            },
        )
        .with_signer(signer_seeds),
        amount,
        ctx.accounts.wsol_mint.decimals,
    )?;
    Ok(())
}
```

Within the instruction logic, we make a CPI to transfer wSOL from the sender's
wSOL token account. This transfer is signed for using the delegate PDA. For
every token transfer, the sender must first approve the delegate for the
transfer amount.

### Instruction discriminator

The starter code already sets the `transfer_hook` instruction's discriminator.
Here is why the program needs it.

The Token Extensions program calls the hook with the Transfer Hook Interface's
`Execute` instruction. Its instruction data starts with an 8-byte discriminator,
the first 8 bytes of the SHA-256 hash of `spl-transfer-hook-interface:execute`,
followed by the `u64` amount. Anchor derives an instruction's discriminator from
its name (the hash of `global:transfer_hook`), so without an override the CPI
matches no instruction in the program and every transfer fails with
`InstructionFallbackNotFound`.

The `#[instruction(discriminator = ...)]` attribute replaces the derived
discriminator with the interface's:

```rust
use spl_discriminator::SplDiscriminate;
use spl_transfer_hook_interface::instruction::ExecuteInstruction;

#[instruction(discriminator = ExecuteInstruction::SPL_DISCRIMINATOR_SLICE)]
pub fn transfer_hook(ctx: Context<TransferHook>, amount: u64) -> Result<()> {
    // ...
}
```

Anchor then reads the bytes after the discriminator as the `amount` argument.
The IDL records the same discriminator, so the TypeScript client's
`program.methods.transferHook()` sends the bytes the Token Extensions program
sends. Examples written for Anchor 0.29 route `Execute` through a `fallback`
function instead; in Anchor 1.x, set the discriminator on the instruction.

### Build the program

The Transfer Hook program is now complete:

<Accordion title="Complete wSOL fee program">

```rust programs/transfer-hook/src/lib.rs
use anchor_lang::{
    prelude::*,
    system_program::{create_account, CreateAccount},
};
use anchor_spl::{
    associated_token::AssociatedToken,
    token_interface::{transfer_checked, Mint, TokenAccount, TokenInterface, TransferChecked},
};
use spl_discriminator::SplDiscriminate;
use spl_tlv_account_resolution::{
    account::ExtraAccountMeta, seeds::Seed, state::ExtraAccountMetaList,
};
use spl_transfer_hook_interface::instruction::ExecuteInstruction;

declare_id!("E6wu6Nykdra8gXs57Zqo7hY6DLaWugTmD3uuuBmX2Vxt");

#[program]
pub mod transfer_hook {
    use super::*;

    pub fn initialize_extra_account_meta_list(
        ctx: Context<InitializeExtraAccountMetaList>,
    ) -> Result<()> {
        // index 0-3 are the accounts required for token transfer (source, mint, destination, owner)
        // index 4 is address of ExtraAccountMetaList account
        let account_metas = vec![
            // index 5, wrapped SOL mint
            ExtraAccountMeta::new_with_pubkey(&ctx.accounts.wsol_mint.key(), false, false)?,
            // index 6, token program
            ExtraAccountMeta::new_with_pubkey(&ctx.accounts.token_program.key(), false, false)?,
            // index 7, associated token program
            ExtraAccountMeta::new_with_pubkey(
                &ctx.accounts.associated_token_program.key(),
                false,
                false,
            )?,
            // index 8, delegate PDA
            ExtraAccountMeta::new_with_seeds(
                &[Seed::Literal {
                    bytes: "delegate".as_bytes().to_vec(),
                }],
                false, // is_signer
                true,  // is_writable
            )?,
            // index 9, delegate wrapped SOL token account
            ExtraAccountMeta::new_external_pda_with_seeds(
                7, // associated token program index
                &[
                    Seed::AccountKey { index: 8 }, // owner index (delegate PDA)
                    Seed::AccountKey { index: 6 }, // token program index
                    Seed::AccountKey { index: 5 }, // wsol mint index
                ],
                false, // is_signer
                true,  // is_writable
            )?,
            // index 10, sender wrapped SOL token account
            ExtraAccountMeta::new_external_pda_with_seeds(
                7, // associated token program index
                &[
                    Seed::AccountKey { index: 3 }, // owner index
                    Seed::AccountKey { index: 6 }, // token program index
                    Seed::AccountKey { index: 5 }, // wsol mint index
                ],
                false, // is_signer
                true,  // is_writable
            )?,
        ];

        // calculate account size
        let account_size = ExtraAccountMetaList::size_of(account_metas.len())? as u64;
        // calculate minimum required lamports
        let lamports = Rent::get()?.minimum_balance(account_size as usize);

        let mint = ctx.accounts.mint.key();
        let signer_seeds: &[&[&[u8]]] = &[&[
            b"extra-account-metas",
            mint.as_ref(),
            &[ctx.bumps.extra_account_meta_list],
        ]];

        // create ExtraAccountMetaList account
        create_account(
            CpiContext::new(
                ctx.accounts.system_program.key(),
                CreateAccount {
                    from: ctx.accounts.payer.to_account_info(),
                    to: ctx.accounts.extra_account_meta_list.to_account_info(),
                },
            )
            .with_signer(signer_seeds),
            lamports,
            account_size,
            ctx.program_id,
        )?;

        // initialize ExtraAccountMetaList account with extra accounts
        ExtraAccountMetaList::init::<ExecuteInstruction>(
            &mut ctx.accounts.extra_account_meta_list.try_borrow_mut_data()?,
            &account_metas,
        )?;

        Ok(())
    }

    // Require SOL fee on transfer, lamport fee is equal to transfer amount
    // If this fails, the initial token transfer fails
    #[instruction(discriminator = ExecuteInstruction::SPL_DISCRIMINATOR_SLICE)]
    pub fn transfer_hook(ctx: Context<TransferHook>, amount: u64) -> Result<()> {
        msg!("Transfer WSOL using delegate PDA");

        let signer_seeds: &[&[&[u8]]] = &[&[b"delegate", &[ctx.bumps.delegate]]];

        // transfer WSOL from sender to delegate token account using delegate PDA
        transfer_checked(
            CpiContext::new(
                ctx.accounts.token_program.key(),
                TransferChecked {
                    from: ctx.accounts.sender_wsol_token_account.to_account_info(),
                    mint: ctx.accounts.wsol_mint.to_account_info(),
                    to: ctx.accounts.delegate_wsol_token_account.to_account_info(),
                    authority: ctx.accounts.delegate.to_account_info(),
                },
            )
            .with_signer(signer_seeds),
            amount,
            ctx.accounts.wsol_mint.decimals,
        )?;
        Ok(())
    }
}

#[derive(Accounts)]
pub struct InitializeExtraAccountMetaList<'info> {
    #[account(mut)]
    pub payer: Signer<'info>,

    /// CHECK: ExtraAccountMetaList Account, must use these seeds
    #[account(
        mut,
        seeds = [b"extra-account-metas", mint.key().as_ref()],
        bump
    )]
    pub extra_account_meta_list: UncheckedAccount<'info>,
    pub mint: InterfaceAccount<'info, Mint>,
    pub wsol_mint: InterfaceAccount<'info, Mint>,
    pub token_program: Interface<'info, TokenInterface>,
    pub associated_token_program: Program<'info, AssociatedToken>,
    pub system_program: Program<'info, System>,
}

// Order of accounts matters for this struct.
// The first 4 accounts are the accounts required for token transfer (source, mint, destination, owner)
// Remaining accounts are the extra accounts required from the ExtraAccountMetaList account
// These accounts are provided via CPI to this program from the Token Extensions program
#[derive(Accounts)]
pub struct TransferHook<'info> {
    #[account(
        token::mint = mint,
        token::authority = owner,
    )]
    pub source_token: InterfaceAccount<'info, TokenAccount>,
    pub mint: InterfaceAccount<'info, Mint>,
    #[account(
        token::mint = mint,
    )]
    pub destination_token: InterfaceAccount<'info, TokenAccount>,
    /// CHECK: source token account owner, can be SystemAccount or PDA owned by another program
    pub owner: UncheckedAccount<'info>,
    /// CHECK: ExtraAccountMetaList Account
    #[account(
        seeds = [b"extra-account-metas", mint.key().as_ref()],
        bump
    )]
    pub extra_account_meta_list: UncheckedAccount<'info>,
    pub wsol_mint: InterfaceAccount<'info, Mint>,
    pub token_program: Interface<'info, TokenInterface>,
    pub associated_token_program: Program<'info, AssociatedToken>,
    #[account(
        mut,
        seeds = [b"delegate"],
        bump
    )]
    pub delegate: SystemAccount<'info>,
    #[account(
        mut,
        token::mint = wsol_mint,
        token::authority = delegate,
    )]
    pub delegate_wsol_token_account: Box<InterfaceAccount<'info, TokenAccount>>,
    #[account(
        mut,
        token::mint = wsol_mint,
        token::authority = owner,
    )]
    pub sender_wsol_token_account: Box<InterfaceAccount<'info, TokenAccount>>,
}
```

</Accordion>

Build it. If you pasted the complete program, run `anchor keys sync` first to
point its `declare_id!` at your project's program ID:

```bash Terminal
anchor keys sync
anchor build
```

`anchor test` deploys the program to the local validator it starts, so you
don't need to deploy it to run the test. To deploy it to devnet, see
[Deploy Program](/developers/docs/programs/build-first-program#deploy-program).

### Test File Overview

Next, let's test the program. Replace `tests/transfer-hook.ts` with the
following starter code:

<Info>
  The test file below uses legacy web3.js (`@solana/web3.js` v1) and
  `@solana/spl-token`, because Anchor's TypeScript client (`@anchor-lang/core`,
  with `AnchorProvider`, `program.methods`, and the `Wallet` it exposes) is
  built on legacy web3.js types. For a client-side send path written against
  web3.js v3 and `@solana-program/token-2022`, see the [Transfer Hook
  Integration
  Guide](/developers/docs/tokens/extensions/transfer-hook-integration#sending-a-transfer-hook-token).
</Info>

<Warning>
  **`@solana/web3.js` 1.x is no longer actively maintained**

  It receives critical fixes only. Use [Kit](/developers/docs/clients/official/javascript#solana-kit) for new code. To migrate existing code, follow the [Kit upgrade guide](https://www.solanakit.com/docs/upgrade-guide), or the [web3.js v3 migration guide](https://github.com/solana-foundation/solana-web3.js/blob/main/docs/web3js-v1-to-v3-migration.md) to keep the class-based API.

  To run this code, install 1.x with `npm install @solana/web3.js@1`. A plain `npm install @solana/web3.js` installs v3, which this code doesn't run on.
</Warning>

```ts tests/transfer-hook.ts expandable
import * as anchor from "@anchor-lang/core";
import { Program } from "@anchor-lang/core";
import { TransferHook } from "../target/types/transfer_hook";
import {
  PublicKey,
  SystemProgram,
  Transaction,
  sendAndConfirmTransaction,
  Keypair,
} from "@solana/web3.js";
import {
  ExtensionType,
  TOKEN_2022_PROGRAM_ID,
  getMintLen,
  createInitializeMintInstruction,
  createInitializeTransferHookInstruction,
  ASSOCIATED_TOKEN_PROGRAM_ID,
  createAssociatedTokenAccountInstruction,
  createMintToInstruction,
  getAssociatedTokenAddressSync,
  createApproveInstruction,
  createSyncNativeInstruction,
  NATIVE_MINT,
  TOKEN_PROGRAM_ID,
  getAccount,
  getOrCreateAssociatedTokenAccount,
} from "@solana/spl-token";
import assert from "assert";

describe("transfer-hook", () => {
  // Configure the client to use the local cluster.
  const provider = anchor.AnchorProvider.env();
  anchor.setProvider(provider);

  const program = anchor.workspace.transferHook as Program<TransferHook>;
  const wallet = provider.wallet as anchor.Wallet;
  const connection = provider.connection;

  // Generate keypair to use as address for the transfer-hook enabled mint
  const mint = new Keypair();
  const decimals = 9;

  // Sender token account address
  const sourceTokenAccount = getAssociatedTokenAddressSync(
    mint.publicKey,
    wallet.publicKey,
    false,
    TOKEN_2022_PROGRAM_ID,
    ASSOCIATED_TOKEN_PROGRAM_ID
  );

  // Recipient token account address
  const recipient = Keypair.generate();
  const destinationTokenAccount = getAssociatedTokenAddressSync(
    mint.publicKey,
    recipient.publicKey,
    false,
    TOKEN_2022_PROGRAM_ID,
    ASSOCIATED_TOKEN_PROGRAM_ID
  );

  // ExtraAccountMetaList address
  // Store extra accounts required by the custom transfer hook instruction
  const [extraAccountMetaListPDA] = PublicKey.findProgramAddressSync(
    [Buffer.from("extra-account-metas"), mint.publicKey.toBuffer()],
    program.programId
  );

  // PDA delegate to transfer wSOL tokens from sender
  const [delegatePDA] = PublicKey.findProgramAddressSync(
    [Buffer.from("delegate")],
    program.programId
  );

  // Sender wSOL token account address
  const senderWSolTokenAccount = getAssociatedTokenAddressSync(
    NATIVE_MINT, // mint
    wallet.publicKey // owner
  );

  // Delegate PDA wSOL token account address, to receive wSOL tokens from sender
  const delegateWSolTokenAccount = getAssociatedTokenAddressSync(
    NATIVE_MINT, // mint
    delegatePDA, // owner
    true // allowOwnerOffCurve
  );

  // Create the two WSol token accounts as part of setup
  before(async () => {
    // WSol Token Account for sender
    await getOrCreateAssociatedTokenAccount(
      connection,
      wallet.payer,
      NATIVE_MINT,
      wallet.publicKey
    );

    // WSol Token Account for delegate PDA
    await getOrCreateAssociatedTokenAccount(
      connection,
      wallet.payer,
      NATIVE_MINT,
      delegatePDA,
      true
    );
  });

  it("Create Mint Account with Transfer Hook Extension", async () => {});

  it("Create Token Accounts and Mint Tokens", async () => {});

  it("Create ExtraAccountMetaList Account", async () => {});

  it("Transfer Hook with Extra Account Meta", async () => {});
});
```

First, we generate a keypair to use as the address for a new Mint Account. Using
the mint address, we derive the Associated Token Account (ATA) addresses that we
will use for the token transfer.

```js
// Generate keypair to use as address for the transfer-hook enabled mint
const mint = new Keypair();
const decimals = 9;

// Sender token account address
const sourceTokenAccount = getAssociatedTokenAddressSync(
  mint.publicKey,
  wallet.publicKey,
  false,
  TOKEN_2022_PROGRAM_ID,
  ASSOCIATED_TOKEN_PROGRAM_ID
);

// Recipient token account address
const recipient = Keypair.generate();
const destinationTokenAccount = getAssociatedTokenAddressSync(
  mint.publicKey,
  recipient.publicKey,
  false,
  TOKEN_2022_PROGRAM_ID,
  ASSOCIATED_TOKEN_PROGRAM_ID
);
```

Next, we derive the PDA for the ExtraAccountMetas account. This account is
created to store the additional accounts required by the custom transfer hook
instruction.

```js
// ExtraAccountMetaList address
// Store extra accounts required by the custom transfer hook instruction
const [extraAccountMetaListPDA] = PublicKey.findProgramAddressSync(
  [Buffer.from("extra-account-metas"), mint.publicKey.toBuffer()],
  program.programId
);
```

We also derive the PDA that will be used as the delegate. The sender must
approve this address as a delegate for their wSOL token account. This delegate
PDA is used to "sign" for the wSOL transfer in the custom transfer hook
instruction.

```js
// PDA delegate to transfer wSOL tokens from sender
const [delegatePDA] = PublicKey.findProgramAddressSync(
  [Buffer.from("delegate")],
  program.programId
);
```

Additionally, we derive the addresses for the wSOL token accounts. The first
address is for the sender's wSOL token account, which needs to be funded to pay
for the transfer fee required by the transfer hook instruction. The second
address is for the wSOL token account owned by the delegate PDA. In this
example, all wSOL fees are sent to this account.

```js
// Sender wSOL token account address
const senderWSolTokenAccount = getAssociatedTokenAddressSync(
  NATIVE_MINT, // mint
  wallet.publicKey // owner
);

// Delegate PDA wSOL token account address, to receive wSOL tokens from sender
const delegateWSolTokenAccount = getAssociatedTokenAddressSync(
  NATIVE_MINT, // mint
  delegatePDA, // owner
  true // allowOwnerOffCurve
);
```

Finally, as part of the setup, we create the wSOL token accounts.

```js
// Create the two WSol token accounts as part of setup
before(async () => {
  // WSol Token Account for sender
  await getOrCreateAssociatedTokenAccount(
    connection,
    wallet.payer,
    NATIVE_MINT,
    wallet.publicKey
  );

  // WSol Token Account for delegate PDA
  await getOrCreateAssociatedTokenAccount(
    connection,
    wallet.payer,
    NATIVE_MINT,
    delegatePDA,
    true
  );
});
```

### Create Mint Account

To begin, build a transaction to create a new Mint Account with the Transfer
Hook extension enabled. In this transaction, make sure to specify our program as
the Transfer Hook program stored on the extension.

Enabling the Transfer Hook extension allows the Transfer Extension program to
determine which program to invoke on every token transfer.

Replace the placeholder test:

```js
it("Create Mint Account with Transfer Hook Extension", async () => {});
```

With the updated test below:

```js
it("Create Mint Account with Transfer Hook Extension", async () => {
  const extensions = [ExtensionType.TransferHook];
  const mintLen = getMintLen(extensions);
  const lamports =
    await provider.connection.getMinimumBalanceForRentExemption(mintLen);

  const transaction = new Transaction().add(
    SystemProgram.createAccount({
      fromPubkey: wallet.publicKey,
      newAccountPubkey: mint.publicKey,
      space: mintLen,
      lamports: lamports,
      programId: TOKEN_2022_PROGRAM_ID,
    }),
    createInitializeTransferHookInstruction(
      mint.publicKey,
      wallet.publicKey,
      program.programId, // Transfer Hook Program ID
      TOKEN_2022_PROGRAM_ID
    ),
    createInitializeMintInstruction(
      mint.publicKey,
      decimals,
      wallet.publicKey,
      null,
      TOKEN_2022_PROGRAM_ID
    )
  );

  const txSig = await sendAndConfirmTransaction(
    provider.connection,
    transaction,
    [wallet.payer, mint],
    { commitment: "confirmed" }
  );
  console.log(`Transaction Signature: ${txSig}`);
});
```

### Creating Token Accounts

Next, as part of the setup, create the Associated Token Accounts for both the
sender and recipient. Also, fund the sender's account with some tokens.

Replace the placeholder test:

```js
it("Create Token Accounts and Mint Tokens", async () => {});
```

With the updated test below:

```js
// Create the two token accounts for the transfer-hook enabled mint
// Fund the sender token account with 100 tokens
it("Create Token Accounts and Mint Tokens", async () => {
  // 100 tokens
  const amount = 100 * 10 ** decimals;

  const transaction = new Transaction().add(
    createAssociatedTokenAccountInstruction(
      wallet.publicKey,
      sourceTokenAccount,
      wallet.publicKey,
      mint.publicKey,
      TOKEN_2022_PROGRAM_ID,
      ASSOCIATED_TOKEN_PROGRAM_ID
    ),
    createAssociatedTokenAccountInstruction(
      wallet.publicKey,
      destinationTokenAccount,
      recipient.publicKey,
      mint.publicKey,
      TOKEN_2022_PROGRAM_ID,
      ASSOCIATED_TOKEN_PROGRAM_ID
    ),
    createMintToInstruction(
      mint.publicKey,
      sourceTokenAccount,
      wallet.publicKey,
      amount,
      [],
      TOKEN_2022_PROGRAM_ID
    )
  );

  const txSig = await sendAndConfirmTransaction(
    connection,
    transaction,
    [wallet.payer],
    { commitment: "confirmed" }
  );

  console.log(`Transaction Signature: ${txSig}`);
});
```

### Create ExtraAccountMeta Account

Before sending a token transfer, we need to create the ExtraAccountMetas account
to store all the additional accounts required by the transfer hook instruction.

To create this account, we invoke the instruction from our program.
`accountsPartial` lets the test pass accounts that the client could also derive
from the IDL, such as the PDA and the program addresses.

Replace the placeholder test:

```js
it("Create ExtraAccountMetaList Account", async () => {});
```

With the updated test below:

```js
// Account to store extra accounts required by the transfer hook instruction
it("Create ExtraAccountMetaList Account", async () => {
  const initializeExtraAccountMetaListInstruction = await program.methods
    .initializeExtraAccountMetaList()
    .accountsPartial({
      payer: wallet.publicKey,
      extraAccountMetaList: extraAccountMetaListPDA,
      mint: mint.publicKey,
      wsolMint: NATIVE_MINT,
      tokenProgram: TOKEN_PROGRAM_ID,
      associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
    })
    .instruction();

  const transaction = new Transaction().add(
    initializeExtraAccountMetaListInstruction
  );

  const txSig = await sendAndConfirmTransaction(
    provider.connection,
    transaction,
    [wallet.payer],
    { commitment: "confirmed" }
  );
  console.log("Transaction Signature:", txSig);
});
```

### Transfer Tokens

Finally, we are ready to send a token transfer. In addition to the transfer
instruction, there are a few additional instructions that need to be included.

- The sender must transfer SOL to their wSOL token account to cover the fee
  required by the transfer hook instruction.
- The sender must approve the delegate PDA for the amount of the wSOL fee.
- Include an instruction to sync the wSOL balance.
- The token transfer instruction must include all the extra accounts required by
  the transfer hook instruction.

The test builds the transfer with
`createTransferCheckedWithTransferHookInstruction`, which the starter file
doesn't import. Add it to the `@solana/spl-token` import at the top of the file:

```js
import {
  // ...the existing imports
  createTransferCheckedWithTransferHookInstruction,
} from "@solana/spl-token";
```

Then replace the placeholder test:

```js
it("Transfer Hook with Extra Account Meta", async () => {});
```

With the updated test below. After the transfer, it reads the delegate PDA's
wSOL token account and checks that it received the fee:

```js
it("Transfer Hook with Extra Account Meta", async () => {
  // 1 token
  const amount = 1 * 10 ** decimals;
  const amountBigInt = BigInt(amount);

  // Instruction for sender to fund their WSol token account
  const solTransferInstruction = SystemProgram.transfer({
    fromPubkey: wallet.publicKey,
    toPubkey: senderWSolTokenAccount,
    lamports: amount,
  });

  // Approve delegate PDA to transfer WSol tokens from sender WSol token account
  const approveInstruction = createApproveInstruction(
    senderWSolTokenAccount,
    delegatePDA,
    wallet.publicKey,
    amount,
    [],
    TOKEN_PROGRAM_ID
  );

  // Sync sender WSol token account
  const syncWrappedSolInstruction = createSyncNativeInstruction(
    senderWSolTokenAccount
  );

  // This helper function will automatically derive all the additional accounts that were defined in the ExtraAccountMetas account
  const transferInstructionWithHelper =
    await createTransferCheckedWithTransferHookInstruction(
      connection,
      sourceTokenAccount,
      mint.publicKey,
      destinationTokenAccount,
      wallet.publicKey,
      amountBigInt,
      decimals,
      [],
      "confirmed",
      TOKEN_2022_PROGRAM_ID
    );

  const transaction = new Transaction().add(
    solTransferInstruction,
    syncWrappedSolInstruction,
    approveInstruction,
    transferInstructionWithHelper
  );

  const txSig = await sendAndConfirmTransaction(
    connection,
    transaction,
    [wallet.payer],
    { commitment: "confirmed" }
  );
  console.log("Transfer Signature:", txSig);

  // The hook moved the wSOL fee to the delegate PDA's wSOL token account
  const delegateWSol = await getAccount(
    connection,
    delegateWSolTokenAccount,
    "confirmed"
  );
  console.log("Fee collected (lamports):", delegateWSol.amount.toString());
  assert.equal(Number(delegateWSol.amount), amount);
});
```

The transfer instruction must include all additional AccountMetas, the address
of the ExtraAccountMetas account, and the address of the Transfer Hook program.

<Accordion title="Complete test file">

```ts tests/transfer-hook.ts
import * as anchor from "@anchor-lang/core";
import { Program } from "@anchor-lang/core";
import { TransferHook } from "../target/types/transfer_hook";
import {
  PublicKey,
  SystemProgram,
  Transaction,
  sendAndConfirmTransaction,
  Keypair,
} from "@solana/web3.js";
import {
  ExtensionType,
  TOKEN_2022_PROGRAM_ID,
  getMintLen,
  createInitializeMintInstruction,
  createInitializeTransferHookInstruction,
  ASSOCIATED_TOKEN_PROGRAM_ID,
  createAssociatedTokenAccountInstruction,
  createMintToInstruction,
  getAssociatedTokenAddressSync,
  createApproveInstruction,
  createSyncNativeInstruction,
  NATIVE_MINT,
  TOKEN_PROGRAM_ID,
  getAccount,
  getOrCreateAssociatedTokenAccount,
  createTransferCheckedWithTransferHookInstruction,
} from "@solana/spl-token";
import assert from "assert";

describe("transfer-hook", () => {
  // Configure the client to use the local cluster.
  const provider = anchor.AnchorProvider.env();
  anchor.setProvider(provider);

  const program = anchor.workspace.transferHook as Program<TransferHook>;
  const wallet = provider.wallet as anchor.Wallet;
  const connection = provider.connection;

  // Generate keypair to use as address for the transfer-hook enabled mint
  const mint = new Keypair();
  const decimals = 9;

  // Sender token account address
  const sourceTokenAccount = getAssociatedTokenAddressSync(
    mint.publicKey,
    wallet.publicKey,
    false,
    TOKEN_2022_PROGRAM_ID,
    ASSOCIATED_TOKEN_PROGRAM_ID
  );

  // Recipient token account address
  const recipient = Keypair.generate();
  const destinationTokenAccount = getAssociatedTokenAddressSync(
    mint.publicKey,
    recipient.publicKey,
    false,
    TOKEN_2022_PROGRAM_ID,
    ASSOCIATED_TOKEN_PROGRAM_ID
  );

  // ExtraAccountMetaList address
  // Store extra accounts required by the custom transfer hook instruction
  const [extraAccountMetaListPDA] = PublicKey.findProgramAddressSync(
    [Buffer.from("extra-account-metas"), mint.publicKey.toBuffer()],
    program.programId
  );

  // PDA delegate to transfer wSOL tokens from sender
  const [delegatePDA] = PublicKey.findProgramAddressSync(
    [Buffer.from("delegate")],
    program.programId
  );

  // Sender wSOL token account address
  const senderWSolTokenAccount = getAssociatedTokenAddressSync(
    NATIVE_MINT, // mint
    wallet.publicKey // owner
  );

  // Delegate PDA wSOL token account address, to receive wSOL tokens from sender
  const delegateWSolTokenAccount = getAssociatedTokenAddressSync(
    NATIVE_MINT, // mint
    delegatePDA, // owner
    true // allowOwnerOffCurve
  );

  // Create the two WSol token accounts as part of setup
  before(async () => {
    // WSol Token Account for sender
    await getOrCreateAssociatedTokenAccount(
      connection,
      wallet.payer,
      NATIVE_MINT,
      wallet.publicKey
    );

    // WSol Token Account for delegate PDA
    await getOrCreateAssociatedTokenAccount(
      connection,
      wallet.payer,
      NATIVE_MINT,
      delegatePDA,
      true
    );
  });

  it("Create Mint Account with Transfer Hook Extension", async () => {
    const extensions = [ExtensionType.TransferHook];
    const mintLen = getMintLen(extensions);
    const lamports =
      await provider.connection.getMinimumBalanceForRentExemption(mintLen);

    const transaction = new Transaction().add(
      SystemProgram.createAccount({
        fromPubkey: wallet.publicKey,
        newAccountPubkey: mint.publicKey,
        space: mintLen,
        lamports: lamports,
        programId: TOKEN_2022_PROGRAM_ID,
      }),
      createInitializeTransferHookInstruction(
        mint.publicKey,
        wallet.publicKey,
        program.programId, // Transfer Hook Program ID
        TOKEN_2022_PROGRAM_ID
      ),
      createInitializeMintInstruction(
        mint.publicKey,
        decimals,
        wallet.publicKey,
        null,
        TOKEN_2022_PROGRAM_ID
      )
    );

    const txSig = await sendAndConfirmTransaction(
      provider.connection,
      transaction,
      [wallet.payer, mint],
      { commitment: "confirmed" }
    );
    console.log(`Transaction Signature: ${txSig}`);
  });

  // Create the two token accounts for the transfer-hook enabled mint
  // Fund the sender token account with 100 tokens
  it("Create Token Accounts and Mint Tokens", async () => {
    // 100 tokens
    const amount = 100 * 10 ** decimals;

    const transaction = new Transaction().add(
      createAssociatedTokenAccountInstruction(
        wallet.publicKey,
        sourceTokenAccount,
        wallet.publicKey,
        mint.publicKey,
        TOKEN_2022_PROGRAM_ID,
        ASSOCIATED_TOKEN_PROGRAM_ID
      ),
      createAssociatedTokenAccountInstruction(
        wallet.publicKey,
        destinationTokenAccount,
        recipient.publicKey,
        mint.publicKey,
        TOKEN_2022_PROGRAM_ID,
        ASSOCIATED_TOKEN_PROGRAM_ID
      ),
      createMintToInstruction(
        mint.publicKey,
        sourceTokenAccount,
        wallet.publicKey,
        amount,
        [],
        TOKEN_2022_PROGRAM_ID
      )
    );

    const txSig = await sendAndConfirmTransaction(
      connection,
      transaction,
      [wallet.payer],
      { commitment: "confirmed" }
    );

    console.log(`Transaction Signature: ${txSig}`);
  });

  // Account to store extra accounts required by the transfer hook instruction
  it("Create ExtraAccountMetaList Account", async () => {
    const initializeExtraAccountMetaListInstruction = await program.methods
      .initializeExtraAccountMetaList()
      .accountsPartial({
        payer: wallet.publicKey,
        extraAccountMetaList: extraAccountMetaListPDA,
        mint: mint.publicKey,
        wsolMint: NATIVE_MINT,
        tokenProgram: TOKEN_PROGRAM_ID,
        associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
      })
      .instruction();

    const transaction = new Transaction().add(
      initializeExtraAccountMetaListInstruction
    );

    const txSig = await sendAndConfirmTransaction(
      provider.connection,
      transaction,
      [wallet.payer],
      { commitment: "confirmed" }
    );
    console.log("Transaction Signature:", txSig);
  });

  it("Transfer Hook with Extra Account Meta", async () => {
    // 1 token
    const amount = 1 * 10 ** decimals;
    const amountBigInt = BigInt(amount);

    // Instruction for sender to fund their WSol token account
    const solTransferInstruction = SystemProgram.transfer({
      fromPubkey: wallet.publicKey,
      toPubkey: senderWSolTokenAccount,
      lamports: amount,
    });

    // Approve delegate PDA to transfer WSol tokens from sender WSol token account
    const approveInstruction = createApproveInstruction(
      senderWSolTokenAccount,
      delegatePDA,
      wallet.publicKey,
      amount,
      [],
      TOKEN_PROGRAM_ID
    );

    // Sync sender WSol token account
    const syncWrappedSolInstruction = createSyncNativeInstruction(
      senderWSolTokenAccount
    );

    // This helper function will automatically derive all the additional accounts that were defined in the ExtraAccountMetas account
    const transferInstructionWithHelper =
      await createTransferCheckedWithTransferHookInstruction(
        connection,
        sourceTokenAccount,
        mint.publicKey,
        destinationTokenAccount,
        wallet.publicKey,
        amountBigInt,
        decimals,
        [],
        "confirmed",
        TOKEN_2022_PROGRAM_ID
      );

    const transaction = new Transaction().add(
      solTransferInstruction,
      syncWrappedSolInstruction,
      approveInstruction,
      transferInstructionWithHelper
    );

    const txSig = await sendAndConfirmTransaction(
      connection,
      transaction,
      [wallet.payer],
      { commitment: "confirmed" }
    );
    console.log("Transfer Signature:", txSig);

    // The hook moved the wSOL fee to the delegate PDA's wSOL token account
    const delegateWSol = await getAccount(
      connection,
      delegateWSolTokenAccount,
      "confirmed"
    );
    console.log("Fee collected (lamports):", delegateWSol.amount.toString());
    assert.equal(Number(delegateWSol.amount), amount);
  });
});
```

</Accordion>

### Run Test File

Once you have updated all the tests, the final step is to run the test:

```bash Terminal
anchor test --validator legacy
```

You should see output similar to the following. The fee equals the transfer
amount, 1 token with 9 decimals, so the delegate's wSOL account receives
1,000,000,000 lamports (1 SOL):

```txt
  transfer-hook
Transaction Signature: 65BFGvoSy4rpJYXouUHYzfG7GYJu8nMnZFbyh6tN7pBYbige4EThJVoY9qf6yMAz1MZbTaswR4ezocKurdZERMU6
    ✔ Create Mint Account with Transfer Hook Extension (485ms)
Transaction Signature: 5h1E1YcwuXWJ9TQ1jBYoQbkyLnWsQwAoZSc1qPdZcg3fCqo4ZV7nmnKBf7dnhdZ5wFnketBqYQSmgDUgqFNXqR3z
    ✔ Create Token Accounts and Mint Tokens (370ms)
Transaction Signature: 3AoqkfjGBvcnz1iUQeHvygncBhuJFbnfJVXdRno944orf9R9HxhA4U7mxufLCF4sm2Y8kM6zgiAUPm9zeH5Fa38T
    ✔ Create ExtraAccountMetaList Account (418ms)
Transfer Signature: 58ycVQxBVCYioDXWd8bg4LuwNp9ctLPU1izZYaaVuQzVvBjrrSPBEsEkDqFVxBfYx5brvb6aW8SCCwhdBJtf1JvY
Fee collected (lamports): 1000000000
    ✔ Transfer Hook with Extra Account Meta (413ms)

  4 passing (2s)
```

### Using token account data in transfer hook

Sometimes you may want to use account data to derive additional accounts in the
extra account metas. This is useful if, for example, you want to use the token
account's owner as a seed for a PDA.

When creating the ExtraAccountMeta you can use the data of any account as an
extra seed. In this case we want to derive a counter account from the token
account owner and the string 'counter'. This means we will be always able to see
how often that token account owner has transferred tokens.

This is how you set it up in the `extra_account_metas()` function.

```rust
// Define extra account metas to store on extra_account_meta_list account
impl<'info> InitializeExtraAccountMetaList<'info> {
    pub fn extra_account_metas() -> Result<Vec<ExtraAccountMeta>> {
        Ok(
            vec![
                ExtraAccountMeta::new_with_seeds(
                    &[
                        Seed::Literal {
                            bytes: b"counter".to_vec(),
                        },
                        Seed::AccountData { account_index: 0, data_index: 32, length: 32 },
                    ],
                    false, // is_signer
                    true // is_writable
                )?
            ]
        )
    }
}
```

Let's look at the token account struct to understand how the account data is
stored. Below is an example of a token account structure. So we can take 32
bytes at position 32 to 64 as the owner of the token account, which is at
`account_index: 0`. `account_index` refers to the index of the account in the
accounts array. In the case of a transfer hook, the source token account is the
first entry in the accounts array. The second account is always the mint and the
third account is the destination token account. This account order is the same
as in the old token program.

```rust
/// Account data.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Account {
    /// The mint associated with this account
    pub mint: Pubkey,
    /// The owner of this account.
    pub owner: Pubkey,
    /// The amount of tokens this account holds.
    pub amount: u64,
    pub delegate: COption<Pubkey>,
    pub state: AccountState,
    pub is_native: COption<u64>,
    pub delegated_amount: u64,
    pub close_authority: COption<Pubkey>,
}
```

In our case, we want to derive a counter account from the owner of the sender
token account so when we create the ExtraAccountMeta accounts we `init` this PDA
counter account that is derived from the sender token account owner and the
string 'counter'. When the PDA counter account is initialized we will be able to
use it within the transfer hook to increment the value in every transfer.

Add the counter account to the `InitializeExtraAccountMetaList` struct. The
struct uses `Program<'info, Token2022>`, so also import `Token2022` from
`anchor_spl`:

```rust
use anchor_spl::token_2022::Token2022;

#[derive(Accounts)]
pub struct InitializeExtraAccountMetaList<'info> {
    #[account(mut)]
    payer: Signer<'info>,

    /// CHECK: ExtraAccountMetaList Account, must use these seeds
    #[account(
        init,
        seeds = [b"extra-account-metas", mint.key().as_ref()],
        bump,
        space = ExtraAccountMetaList::size_of(
            InitializeExtraAccountMetaList::extra_account_metas()?.len()
        )?,
        payer = payer
    )]
    pub extra_account_meta_list: UncheckedAccount<'info>,
    pub mint: InterfaceAccount<'info, Mint>,
    #[account(init, seeds = [b"counter", payer.key().as_ref()], bump, payer = payer, space = 16)]
    pub counter_account: Account<'info, CounterAccount>,
    pub token_program: Program<'info, Token2022>,
    pub associated_token_program: Program<'info, AssociatedToken>,
    pub system_program: Program<'info, System>,
}
```

Because the struct creates the ExtraAccountMetaList account with `init`, the
instruction only writes the list into it:

```rust
pub fn initialize_extra_account_meta_list(
    ctx: Context<InitializeExtraAccountMetaList>,
) -> Result<()> {
    let extra_account_metas = InitializeExtraAccountMetaList::extra_account_metas()?;

    // initialize ExtraAccountMetaList account with extra accounts
    ExtraAccountMetaList::init::<ExecuteInstruction>(
        &mut ctx.accounts.extra_account_meta_list.try_borrow_mut_data()?,
        &extra_account_metas,
    )?;

    Ok(())
}
```

We also need to define this extra counter account in the TransferHook struct.
These are the accounts that are passed to our TransferHook program every time a
transfer is done. The client gets these additional accounts from the
ExtraAccountsMetaList PDA and includes them in token transfer instruction, but
here in the program we still need to define it. Mark `counter_account` as `mut`:
Anchor only writes back accounts marked `mut`, so without it the new count is
never saved.

```rust
#[derive(Accounts)]
pub struct TransferHook<'info> {
    #[account(token::mint = mint, token::authority = owner)]
    pub source_token: InterfaceAccount<'info, TokenAccount>,
    pub mint: InterfaceAccount<'info, Mint>,
    #[account(token::mint = mint)]
    pub destination_token: InterfaceAccount<'info, TokenAccount>,
    /// CHECK: source token account owner, can be SystemAccount or PDA owned by another program
    pub owner: UncheckedAccount<'info>,
    /// CHECK: ExtraAccountMetaList Account,
    #[account(seeds = [b"extra-account-metas", mint.key().as_ref()], bump)]
    pub extra_account_meta_list: UncheckedAccount<'info>,
    #[account(mut, seeds = [b"counter", owner.key().as_ref()], bump)]
    pub counter_account: Account<'info, CounterAccount>,
}
```

The hook then increments the owner's counter on every transfer:

```rust
#[instruction(discriminator = ExecuteInstruction::SPL_DISCRIMINATOR_SLICE)]
pub fn transfer_hook(ctx: Context<TransferHook>, _amount: u64) -> Result<()> {
    ctx.accounts.counter_account.counter =
        ctx.accounts.counter_account.counter.checked_add(1).unwrap();
    msg!(
        "{0} has transferred this token {1} times",
        ctx.accounts.owner.key(),
        ctx.accounts.counter_account.counter
    );
    Ok(())
}
```

In the client this account is auto generated and you can use it as follows.

```ts
const transferInstructionWithHelper =
  await createTransferCheckedWithTransferHookInstruction(
    connection,
    sourceTokenAccount,
    mint.publicKey,
    destinationTokenAccount,
    wallet.publicKey,
    amountBigInt,
    decimals,
    [],
    "confirmed",
    TOKEN_2022_PROGRAM_ID
  );
```

The helper function is resolving the account automatically from the
ExtraAccounts data account. How the account would be resolved in the client is
like this:

<CodeGroup>

```js title="Web3.js v3"
const [counterPDA] = await PublicKey.findProgramAddress(
  [Buffer.from("counter"), wallet.publicKey.toBytes()],
  program.programId
);
```

<Warning>
  **`@solana/web3.js` 1.x is no longer actively maintained**

  It receives critical fixes only. Use [Kit](/developers/docs/clients/official/javascript#solana-kit) for new code. To migrate existing code, follow the [Kit upgrade guide](https://www.solanakit.com/docs/upgrade-guide), or the [web3.js v3 migration guide](https://github.com/solana-foundation/solana-web3.js/blob/main/docs/web3js-v1-to-v3-migration.md) to keep the class-based API.

  To run this code, install 1.x with `npm install @solana/web3.js@1`. A plain `npm install @solana/web3.js` installs v3, which this code doesn't run on.
</Warning>

```js title="Web3.js (legacy)"
const [counterPDA] = PublicKey.findProgramAddressSync(
  [Buffer.from("counter"), wallet.publicKey.toBuffer()],
  program.programId
);
```

</CodeGroup>

Note that the counter account is derived from the owner of the token account and
needs to be initialized before doing a transfer. In the case of this example we
initialize the counter account when we initialize the extra account metas. So we
will only have a counter PDA for the owner of the token account that called that
function. If you want to have a counter account for every token account for your
mint out there you will need to have some functionality to create these PDAs
before hand. There could be a button on your dapp to sign up for a counter that
creates this PDA account and from then on the users can use this counter token.

To run this version, replace `programs/transfer-hook/src/lib.rs` with the
complete program below and run `anchor keys sync`. Replace
`tests/transfer-hook.ts` with the [hello-world test](#hello-world-transfer-hook)
again, then run `anchor test --validator legacy`. The transfer's logs name the
sender's wallet address:

<Accordion title="Complete program with an owner-derived counter">

```rust programs/transfer-hook/src/lib.rs
use anchor_lang::prelude::*;
use anchor_spl::{
    associated_token::AssociatedToken,
    token_2022::Token2022,
    token_interface::{Mint, TokenAccount},
};
use spl_discriminator::SplDiscriminate;
use spl_tlv_account_resolution::{
    account::ExtraAccountMeta, seeds::Seed, state::ExtraAccountMetaList,
};
use spl_transfer_hook_interface::instruction::ExecuteInstruction;

declare_id!("DrWbQtYJGtsoRwzKqAbHKHKsCJJfpysudF39GBVFSxub");

#[program]
pub mod transfer_hook {
    use super::*;

    pub fn initialize_extra_account_meta_list(
        ctx: Context<InitializeExtraAccountMetaList>,
    ) -> Result<()> {
        let extra_account_metas = InitializeExtraAccountMetaList::extra_account_metas()?;

        // initialize ExtraAccountMetaList account with extra accounts
        ExtraAccountMetaList::init::<ExecuteInstruction>(
            &mut ctx.accounts.extra_account_meta_list.try_borrow_mut_data()?,
            &extra_account_metas,
        )?;

        Ok(())
    }

    #[instruction(discriminator = ExecuteInstruction::SPL_DISCRIMINATOR_SLICE)]
    pub fn transfer_hook(ctx: Context<TransferHook>, _amount: u64) -> Result<()> {
        ctx.accounts.counter_account.counter =
            ctx.accounts.counter_account.counter.checked_add(1).unwrap();
        msg!(
            "{0} has transferred this token {1} times",
            ctx.accounts.owner.key(),
            ctx.accounts.counter_account.counter
        );
        Ok(())
    }
}

// Define extra account metas to store on extra_account_meta_list account
impl<'info> InitializeExtraAccountMetaList<'info> {
    pub fn extra_account_metas() -> Result<Vec<ExtraAccountMeta>> {
        Ok(
            vec![
                ExtraAccountMeta::new_with_seeds(
                    &[
                        Seed::Literal {
                            bytes: b"counter".to_vec(),
                        },
                        Seed::AccountData { account_index: 0, data_index: 32, length: 32 },
                    ],
                    false, // is_signer
                    true // is_writable
                )?
            ]
        )
    }
}

#[derive(Accounts)]
pub struct InitializeExtraAccountMetaList<'info> {
    #[account(mut)]
    payer: Signer<'info>,

    /// CHECK: ExtraAccountMetaList Account, must use these seeds
    #[account(
        init,
        seeds = [b"extra-account-metas", mint.key().as_ref()],
        bump,
        space = ExtraAccountMetaList::size_of(
            InitializeExtraAccountMetaList::extra_account_metas()?.len()
        )?,
        payer = payer
    )]
    pub extra_account_meta_list: UncheckedAccount<'info>,
    pub mint: InterfaceAccount<'info, Mint>,
    #[account(init, seeds = [b"counter", payer.key().as_ref()], bump, payer = payer, space = 16)]
    pub counter_account: Account<'info, CounterAccount>,
    pub token_program: Program<'info, Token2022>,
    pub associated_token_program: Program<'info, AssociatedToken>,
    pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct TransferHook<'info> {
    #[account(token::mint = mint, token::authority = owner)]
    pub source_token: InterfaceAccount<'info, TokenAccount>,
    pub mint: InterfaceAccount<'info, Mint>,
    #[account(token::mint = mint)]
    pub destination_token: InterfaceAccount<'info, TokenAccount>,
    /// CHECK: source token account owner, can be SystemAccount or PDA owned by another program
    pub owner: UncheckedAccount<'info>,
    /// CHECK: ExtraAccountMetaList Account,
    #[account(seeds = [b"extra-account-metas", mint.key().as_ref()], bump)]
    pub extra_account_meta_list: UncheckedAccount<'info>,
    #[account(mut, seeds = [b"counter", owner.key().as_ref()], bump)]
    pub counter_account: Account<'info, CounterAccount>,
}

#[account]
pub struct CounterAccount {
    counter: u64,
}
```

</Accordion>

```txt
Transfer Signature: 5dQkyLR4u6qjcFTwtUuigkMveZGkPV5ZeXzLnWrspe3zwxZ1U5SHVjun842zBgHRdYhqXc34ZH6eSjGH3P4V3Lz7
[
  'Program log: Instruction: TransferChecked',
  'Program log: Instruction: TransferHook',
  'Program log: 4kyEEzZpZwjto2KrrfGEyDrqEsVcr9jMnyWjEyj1YyVx has transferred this token 1 times'
]
    ✔ Transfer Hook with Extra Account Meta (420ms)

  4 passing (2s)
```

### Conclusion

The Transfer Hook extension and Transfer Hook Interface allow for the creation
of Mint Accounts that execute custom instruction logic on every token transfer.
This guide serves as a reference to help you create your own Transfer Hook
programs. Use them to build the transfer rules your token needs.
