From 4ecad7ed0afeedf41bf35ce2ae9860cf32410cd2 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 26 Aug 2026 02:52:02 +0000 Subject: [PATCH] Reuse stored PDA bumps in Anchor v1 and v2 constraints Several Anchor examples stored a canonical bump on the account at creation and then ignored it, re-deriving the address with a bare `bump` constraint on every later instruction. A bare `bump` makes Anchor search from 255 downwards until it finds an off-curve address; `bump = .bump` reuses the stored value and costs a single `create_program_address` instead. Switch the 20 affected constraints (10 in `anchor/`, 10 in `anchor-v1/`) to reuse the stored bump: - finance/token-swap: `config` - token-extensions/transfer-hook: counter, transfer-cost, account-data-as-seed, transfer-switch - nft/metadata-pointer: `player` token-swap's `pool_config` already reuses its stored bump, so only its `config` entries are left to change. The allow-list transfer hook is not included: its per-wallet `AllowedWallet` PDA is an `UncheckedAccount` that may legitimately not exist, so there is no stored bump to read. Only accounts whose bump is written from `context.bumps.*` at initialisation are changed, so the stored value is always the canonical bump and the constraint stays as strict as before. Constraints on `init` accounts, and on PDAs that own no account data to store a bump in (token vaults, authority PDAs, and PDAs owned by other programs such as the extra-account-metas list), keep the bare `bump` - there is nothing to reuse in those cases. The stale comment in transfer-switch's `switch.rs`, which explained why `AdminConfig.bump` was not used for validation, is updated to match. Claude-Session: https://claude.ai/code/session_01YTTjefTz2Kbj8t5qg77kXg --- .../src/instructions/admin/claim_admin_fees.rs | 2 +- .../token-swap/src/instructions/initialize_pool.rs | 2 +- .../token-swap/src/instructions/swap_tokens.rs | 2 +- .../src/instructions/withdraw_liquidity.rs | 2 +- .../src/instructions/admin/claim_admin_fees.rs | 2 +- .../token-swap/src/instructions/initialize_pool.rs | 2 +- .../token-swap/src/instructions/swap_tokens.rs | 2 +- .../src/instructions/withdraw_liquidity.rs | 2 +- .../extension_nft/src/instructions/chop_tree.rs | 2 +- .../extension_nft/src/instructions/chop_tree.rs | 2 +- .../transfer-hook/src/instructions/transfer_hook.rs | 2 +- .../transfer-hook/src/instructions/transfer_hook.rs | 2 +- .../transfer-hook/src/instructions/transfer_hook.rs | 2 +- .../transfer-hook/src/instructions/transfer_hook.rs | 2 +- .../transfer-hook/src/instructions/transfer_hook.rs | 2 +- .../transfer-hook/src/instructions/transfer_hook.rs | 2 +- .../transfer-switch/src/instructions/switch.rs | 12 +++++------- .../src/instructions/transfer_hook.rs | 2 +- .../transfer-switch/src/instructions/switch.rs | 12 +++++------- .../src/instructions/transfer_hook.rs | 2 +- 20 files changed, 28 insertions(+), 32 deletions(-) diff --git a/finance/token-swap/anchor-v1/programs/token-swap/src/instructions/admin/claim_admin_fees.rs b/finance/token-swap/anchor-v1/programs/token-swap/src/instructions/admin/claim_admin_fees.rs index 72db9e02d..a38ecc25c 100644 --- a/finance/token-swap/anchor-v1/programs/token-swap/src/instructions/admin/claim_admin_fees.rs +++ b/finance/token-swap/anchor-v1/programs/token-swap/src/instructions/admin/claim_admin_fees.rs @@ -104,7 +104,7 @@ pub fn handle_claim_admin_fees(context: Context { #[account( seeds = [CONFIG_SEED], - bump, + bump = config.bump, has_one = admin, )] pub config: Account<'info, Config>, diff --git a/finance/token-swap/anchor-v1/programs/token-swap/src/instructions/initialize_pool.rs b/finance/token-swap/anchor-v1/programs/token-swap/src/instructions/initialize_pool.rs index 8a821076e..60e5b9fff 100644 --- a/finance/token-swap/anchor-v1/programs/token-swap/src/instructions/initialize_pool.rs +++ b/finance/token-swap/anchor-v1/programs/token-swap/src/instructions/initialize_pool.rs @@ -62,7 +62,7 @@ pub fn handle_initialize_pool( pub struct InitializePoolAccountConstraints<'info> { #[account( seeds = [CONFIG_SEED], - bump, + bump = config.bump, )] pub config: Box>, diff --git a/finance/token-swap/anchor-v1/programs/token-swap/src/instructions/swap_tokens.rs b/finance/token-swap/anchor-v1/programs/token-swap/src/instructions/swap_tokens.rs index 140ee5939..2c681c525 100644 --- a/finance/token-swap/anchor-v1/programs/token-swap/src/instructions/swap_tokens.rs +++ b/finance/token-swap/anchor-v1/programs/token-swap/src/instructions/swap_tokens.rs @@ -276,7 +276,7 @@ pub fn handle_swap_tokens( pub struct SwapTokensAccountConstraints<'info> { #[account( seeds = [CONFIG_SEED], - bump, + bump = config.bump, )] pub config: Account<'info, Config>, diff --git a/finance/token-swap/anchor-v1/programs/token-swap/src/instructions/withdraw_liquidity.rs b/finance/token-swap/anchor-v1/programs/token-swap/src/instructions/withdraw_liquidity.rs index 83bc62bfd..d198556d8 100644 --- a/finance/token-swap/anchor-v1/programs/token-swap/src/instructions/withdraw_liquidity.rs +++ b/finance/token-swap/anchor-v1/programs/token-swap/src/instructions/withdraw_liquidity.rs @@ -146,7 +146,7 @@ pub fn handle_withdraw_liquidity( pub struct WithdrawLiquidityAccountConstraints<'info> { #[account( seeds = [CONFIG_SEED], - bump, + bump = config.bump, )] pub config: Account<'info, Config>, diff --git a/finance/token-swap/anchor/programs/token-swap/src/instructions/admin/claim_admin_fees.rs b/finance/token-swap/anchor/programs/token-swap/src/instructions/admin/claim_admin_fees.rs index 133a978d8..17acb2be3 100644 --- a/finance/token-swap/anchor/programs/token-swap/src/instructions/admin/claim_admin_fees.rs +++ b/finance/token-swap/anchor/programs/token-swap/src/instructions/admin/claim_admin_fees.rs @@ -115,7 +115,7 @@ pub fn handle_claim_admin_fees( #[derive(Accounts)] pub struct ClaimAdminFeesAccountConstraints { #[account(seeds = [CONFIG_SEED], - bump, address = pool_config.config)] + bump = config.bump, address = pool_config.config)] pub config: BorshAccount, #[account( diff --git a/finance/token-swap/anchor/programs/token-swap/src/instructions/initialize_pool.rs b/finance/token-swap/anchor/programs/token-swap/src/instructions/initialize_pool.rs index cb9e3aa32..e62171981 100644 --- a/finance/token-swap/anchor/programs/token-swap/src/instructions/initialize_pool.rs +++ b/finance/token-swap/anchor/programs/token-swap/src/instructions/initialize_pool.rs @@ -70,7 +70,7 @@ pub fn handle_initialize_pool( pub struct InitializePoolAccountConstraints { #[account( seeds = [CONFIG_SEED], - bump, + bump = config.bump, )] pub config: BorshAccount, diff --git a/finance/token-swap/anchor/programs/token-swap/src/instructions/swap_tokens.rs b/finance/token-swap/anchor/programs/token-swap/src/instructions/swap_tokens.rs index 223ebdd2d..270c7a49f 100644 --- a/finance/token-swap/anchor/programs/token-swap/src/instructions/swap_tokens.rs +++ b/finance/token-swap/anchor/programs/token-swap/src/instructions/swap_tokens.rs @@ -288,7 +288,7 @@ pub fn handle_swap_tokens( #[derive(Accounts)] pub struct SwapTokensAccountConstraints { #[account(seeds = [CONFIG_SEED], - bump, address = pool_config.config)] + bump = config.bump, address = pool_config.config)] pub config: BorshAccount, #[account( diff --git a/finance/token-swap/anchor/programs/token-swap/src/instructions/withdraw_liquidity.rs b/finance/token-swap/anchor/programs/token-swap/src/instructions/withdraw_liquidity.rs index a73ad1e1a..969cf56bb 100644 --- a/finance/token-swap/anchor/programs/token-swap/src/instructions/withdraw_liquidity.rs +++ b/finance/token-swap/anchor/programs/token-swap/src/instructions/withdraw_liquidity.rs @@ -155,7 +155,7 @@ pub fn handle_withdraw_liquidity( pub struct WithdrawLiquidityAccountConstraints { #[account( seeds = [CONFIG_SEED], - bump, + bump = config.bump, )] pub config: BorshAccount, diff --git a/nft/metadata-pointer/anchor-v1/programs/extension_nft/src/instructions/chop_tree.rs b/nft/metadata-pointer/anchor-v1/programs/extension_nft/src/instructions/chop_tree.rs index 0fca0fc7d..8bcd6e30c 100644 --- a/nft/metadata-pointer/anchor-v1/programs/extension_nft/src/instructions/chop_tree.rs +++ b/nft/metadata-pointer/anchor-v1/programs/extension_nft/src/instructions/chop_tree.rs @@ -77,7 +77,7 @@ pub struct ChopTreeAccountConstraints<'info> { #[account( mut, seeds = [b"player".as_ref(), player.authority.key().as_ref()], - bump, + bump = player.bump, )] pub player: Account<'info, PlayerData>, diff --git a/nft/metadata-pointer/anchor/programs/extension_nft/src/instructions/chop_tree.rs b/nft/metadata-pointer/anchor/programs/extension_nft/src/instructions/chop_tree.rs index 9455e74ad..3c436d32e 100644 --- a/nft/metadata-pointer/anchor/programs/extension_nft/src/instructions/chop_tree.rs +++ b/nft/metadata-pointer/anchor/programs/extension_nft/src/instructions/chop_tree.rs @@ -89,7 +89,7 @@ pub struct ChopTreeAccountConstraints { #[account( mut, seeds = [b"player".as_ref(), player.authority.as_ref()], - bump, + bump = player.bump, )] pub player: BorshAccount, diff --git a/tokens/token-extensions/transfer-hook/account-data-as-seed/anchor-v1/programs/transfer-hook/src/instructions/transfer_hook.rs b/tokens/token-extensions/transfer-hook/account-data-as-seed/anchor-v1/programs/transfer-hook/src/instructions/transfer_hook.rs index 37125df9f..2fddb1ccb 100644 --- a/tokens/token-extensions/transfer-hook/account-data-as-seed/anchor-v1/programs/transfer-hook/src/instructions/transfer_hook.rs +++ b/tokens/token-extensions/transfer-hook/account-data-as-seed/anchor-v1/programs/transfer-hook/src/instructions/transfer_hook.rs @@ -19,7 +19,7 @@ pub struct TransferHookAccountConstraints<'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)] + #[account(mut, seeds = [b"counter", owner.key().as_ref()], bump = counter_account.bump)] pub counter_account: Account<'info, CounterAccount>, } diff --git a/tokens/token-extensions/transfer-hook/account-data-as-seed/anchor/programs/transfer-hook/src/instructions/transfer_hook.rs b/tokens/token-extensions/transfer-hook/account-data-as-seed/anchor/programs/transfer-hook/src/instructions/transfer_hook.rs index 14e614382..6fd48d336 100644 --- a/tokens/token-extensions/transfer-hook/account-data-as-seed/anchor/programs/transfer-hook/src/instructions/transfer_hook.rs +++ b/tokens/token-extensions/transfer-hook/account-data-as-seed/anchor/programs/transfer-hook/src/instructions/transfer_hook.rs @@ -20,7 +20,7 @@ pub struct TransferHookAccountConstraints { /// CHECK: ExtraAccountMetaList Account, #[account(seeds = [b"extra-account-metas", mint.address().as_ref()], bump)] pub extra_account_meta_list: UncheckedAccount, - #[account(mut, seeds = [b"counter", owner.address().as_ref()], bump)] + #[account(mut, seeds = [b"counter", owner.address().as_ref()], bump = counter_account.bump)] pub counter_account: BorshAccount, } diff --git a/tokens/token-extensions/transfer-hook/counter/anchor-v1/programs/transfer-hook/src/instructions/transfer_hook.rs b/tokens/token-extensions/transfer-hook/counter/anchor-v1/programs/transfer-hook/src/instructions/transfer_hook.rs index 4b2d13a6f..bc735dbeb 100644 --- a/tokens/token-extensions/transfer-hook/counter/anchor-v1/programs/transfer-hook/src/instructions/transfer_hook.rs +++ b/tokens/token-extensions/transfer-hook/counter/anchor-v1/programs/transfer-hook/src/instructions/transfer_hook.rs @@ -19,7 +19,7 @@ pub struct TransferHookAccountConstraints<'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)] + #[account(mut, seeds = [b"counter"], bump = counter_account.bump)] pub counter_account: Account<'info, CounterAccount>, } diff --git a/tokens/token-extensions/transfer-hook/counter/anchor/programs/transfer-hook/src/instructions/transfer_hook.rs b/tokens/token-extensions/transfer-hook/counter/anchor/programs/transfer-hook/src/instructions/transfer_hook.rs index bdaf88f4f..b749c706b 100644 --- a/tokens/token-extensions/transfer-hook/counter/anchor/programs/transfer-hook/src/instructions/transfer_hook.rs +++ b/tokens/token-extensions/transfer-hook/counter/anchor/programs/transfer-hook/src/instructions/transfer_hook.rs @@ -20,7 +20,7 @@ pub struct TransferHookAccountConstraints { /// CHECK: ExtraAccountMetaList Account, #[account(seeds = [b"extra-account-metas", mint.address().as_ref()], bump)] pub extra_account_meta_list: UncheckedAccount, - #[account(mut, seeds = [b"counter"], bump)] + #[account(mut, seeds = [b"counter"], bump = counter_account.bump)] pub counter_account: BorshAccount, } diff --git a/tokens/token-extensions/transfer-hook/transfer-cost/anchor-v1/programs/transfer-hook/src/instructions/transfer_hook.rs b/tokens/token-extensions/transfer-hook/transfer-cost/anchor-v1/programs/transfer-hook/src/instructions/transfer_hook.rs index 7a687b65f..a9a1743b7 100644 --- a/tokens/token-extensions/transfer-hook/transfer-cost/anchor-v1/programs/transfer-hook/src/instructions/transfer_hook.rs +++ b/tokens/token-extensions/transfer-hook/transfer-cost/anchor-v1/programs/transfer-hook/src/instructions/transfer_hook.rs @@ -49,7 +49,7 @@ pub struct TransferHookAccountConstraints<'info> { token::authority = owner, )] pub sender_wsol_token_account: Box>, - #[account(seeds = [b"counter"], bump)] + #[account(seeds = [b"counter"], bump = counter_account.bump)] pub counter_account: Account<'info, CounterAccount>, } diff --git a/tokens/token-extensions/transfer-hook/transfer-cost/anchor/programs/transfer-hook/src/instructions/transfer_hook.rs b/tokens/token-extensions/transfer-hook/transfer-cost/anchor/programs/transfer-hook/src/instructions/transfer_hook.rs index b32689afb..ec37cadd5 100644 --- a/tokens/token-extensions/transfer-hook/transfer-cost/anchor/programs/transfer-hook/src/instructions/transfer_hook.rs +++ b/tokens/token-extensions/transfer-hook/transfer-cost/anchor/programs/transfer-hook/src/instructions/transfer_hook.rs @@ -44,7 +44,7 @@ pub struct TransferHookAccountConstraints { token::authority = owner, )] pub sender_wsol_token_account: InterfaceAccount, - #[account(seeds = [b"counter"], bump)] + #[account(seeds = [b"counter"], bump = counter_account.bump)] pub counter_account: BorshAccount, } diff --git a/tokens/token-extensions/transfer-hook/transfer-switch/anchor-v1/programs/transfer-switch/src/instructions/switch.rs b/tokens/token-extensions/transfer-hook/transfer-switch/anchor-v1/programs/transfer-switch/src/instructions/switch.rs index a9ba1e9d4..8d151f2c8 100644 --- a/tokens/token-extensions/transfer-hook/transfer-switch/anchor-v1/programs/transfer-switch/src/instructions/switch.rs +++ b/tokens/token-extensions/transfer-hook/transfer-switch/anchor-v1/programs/transfer-switch/src/instructions/switch.rs @@ -17,7 +17,7 @@ pub struct SwitchAccountConstraints<'info> { #[account( has_one=admin, seeds=[b"admin-config"], - bump, + bump = admin_config.bump, )] pub admin_config: Account<'info, AdminConfig>, @@ -45,9 +45,7 @@ pub fn handle_switch(accounts: &mut SwitchAccountConstraints, on: bool, bump: u8 Ok(()) } -// admin_config is validated via `seeds=[b"admin-config"], bump` - Anchor -// re-derives it and fails if it doesn't match, so storing AdminConfig.bump -// isn't strictly needed to validate `admin_config` inside `Switch` (the -// bump field on AdminConfig is still populated on creation to satisfy the -// 'every PDA struct stores its bump' rule and save derivation cost in any -// future call sites). +// admin_config is validated via `seeds=[b"admin-config"], bump = +// admin_config.bump`. `configure_admin` stores the canonical bump at creation +// (from `context.bumps`), so reusing it here turns the check into a single +// `create_program_address` instead of the search a bare `bump` would run. diff --git a/tokens/token-extensions/transfer-hook/transfer-switch/anchor-v1/programs/transfer-switch/src/instructions/transfer_hook.rs b/tokens/token-extensions/transfer-hook/transfer-switch/anchor-v1/programs/transfer-switch/src/instructions/transfer_hook.rs index 75434d2e1..1392ce7d1 100644 --- a/tokens/token-extensions/transfer-hook/transfer-switch/anchor-v1/programs/transfer-switch/src/instructions/transfer_hook.rs +++ b/tokens/token-extensions/transfer-hook/transfer-switch/anchor-v1/programs/transfer-switch/src/instructions/transfer_hook.rs @@ -41,7 +41,7 @@ pub struct TransferHookAccountConstraints<'info> { /// sender transfer switch #[account( seeds=[wallet.key().as_ref()], - bump, + bump = wallet_switch.bump, )] pub wallet_switch: Account<'info, TransferSwitch>, } diff --git a/tokens/token-extensions/transfer-hook/transfer-switch/anchor/programs/transfer-switch/src/instructions/switch.rs b/tokens/token-extensions/transfer-hook/transfer-switch/anchor/programs/transfer-switch/src/instructions/switch.rs index 15bd1838b..132642ca5 100644 --- a/tokens/token-extensions/transfer-hook/transfer-switch/anchor/programs/transfer-switch/src/instructions/switch.rs +++ b/tokens/token-extensions/transfer-hook/transfer-switch/anchor/programs/transfer-switch/src/instructions/switch.rs @@ -15,7 +15,7 @@ pub struct SwitchAccountConstraints { /// admin config #[account(seeds=[b"admin-config"], - bump)] + bump = admin_config.bump)] pub admin_config: BorshAccount, /// the wallet (sender) transfer switch @@ -42,9 +42,7 @@ pub fn handle_switch(accounts: &mut SwitchAccountConstraints, on: bool, bump: u8 Ok(()) } -// admin_config is validated via `seeds=[b"admin-config"], bump` - Anchor -// re-derives it and fails if it doesn't match, so storing AdminConfig.bump -// isn't strictly needed to validate `admin_config` inside `Switch` (the -// bump field on AdminConfig is still populated on creation to satisfy the -// 'every PDA struct stores its bump' rule and save derivation cost in any -// future call sites). +// admin_config is validated via `seeds=[b"admin-config"], bump = +// admin_config.bump`. `configure_admin` stores the canonical bump at creation +// (from `context.bumps`), so reusing it here turns the check into a single +// `create_program_address` instead of the search a bare `bump` would run. diff --git a/tokens/token-extensions/transfer-hook/transfer-switch/anchor/programs/transfer-switch/src/instructions/transfer_hook.rs b/tokens/token-extensions/transfer-hook/transfer-switch/anchor/programs/transfer-switch/src/instructions/transfer_hook.rs index 8db008a05..baf33014e 100644 --- a/tokens/token-extensions/transfer-hook/transfer-switch/anchor/programs/transfer-switch/src/instructions/transfer_hook.rs +++ b/tokens/token-extensions/transfer-hook/transfer-switch/anchor/programs/transfer-switch/src/instructions/transfer_hook.rs @@ -40,7 +40,7 @@ pub struct TransferHookAccountConstraints { /// sender transfer switch #[account( seeds=[wallet.address().as_ref()], - bump, + bump = wallet_switch.bump, )] pub wallet_switch: BorshAccount, }