From 7ab0becd915f613bfd46389c8ea55c5e4bf57cdf Mon Sep 17 00:00:00 2001 From: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Thu, 13 Feb 2025 21:26:42 +0100 Subject: [PATCH] idl: Fix using `Pubkey` constants with `seeds::program` (#3559) --- CHANGELOG.md | 1 + lang/syn/src/idl/accounts.rs | 12 +----------- .../programs/pda-derivation/src/lib.rs | 16 ++++++++++++++++ tests/pda-derivation/tests/typescript.spec.ts | 4 ++++ 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0da55978d9..addcdcb934 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -115,6 +115,7 @@ The minor version will be incremented upon a breaking change and the patch versi - lang: Fix adding `derive`s and `repr`s to type alias definitions in `declare_program!` ([#3504](https://github.com/coral-xyz/anchor/pull/3504)). - idl: Fix using constant identifiers as generic arguments ([#3522](https://github.com/coral-xyz/anchor/pull/3522)). - client: Remove `std::process::exit` usage ([#3544](https://github.com/coral-xyz/anchor/pull/3544)). +- idl: Fix using `Pubkey` constants with `seeds::program` ([#3559](https://github.com/coral-xyz/anchor/pull/3559)). ### Breaking diff --git a/lang/syn/src/idl/accounts.rs b/lang/syn/src/idl/accounts.rs index 56d85618bc..6a410d9d08 100644 --- a/lang/syn/src/idl/accounts.rs +++ b/lang/syn/src/idl/accounts.rs @@ -366,20 +366,10 @@ fn parse_seed(seed: &syn::Expr, accounts: &AccountsStruct) -> Result` with `.into` call e.g. `Pubkey`. - // This is problematic for `seeds::program` but a hacky way to handle this - // scenerio is to check whether the last segment of the path ends with `ID`. - let seed = path - .path - .segments - .last() - .filter(|seg| seg.ident.to_string().ends_with("ID")) - .map(|_| quote! { #seed.as_ref() }) - .unwrap_or_else(|| quote! { #seed }); quote! { #idl::IdlSeed::Const( #idl::IdlSeedConst { - value: #seed.into(), + value: AsRef::<[u8]>::as_ref(&#seed).into(), } ) } diff --git a/tests/pda-derivation/programs/pda-derivation/src/lib.rs b/tests/pda-derivation/programs/pda-derivation/src/lib.rs index f2a5352c63..d18154d2be 100644 --- a/tests/pda-derivation/programs/pda-derivation/src/lib.rs +++ b/tests/pda-derivation/programs/pda-derivation/src/lib.rs @@ -63,6 +63,10 @@ pub mod pda_derivation { pub fn call_expr_with_no_args(_ctx: Context) -> Result<()> { Ok(()) } + + pub fn pubkey_const(_ctx: Context) -> Result<()> { + Ok(()) + } } #[derive(Accounts)] @@ -224,6 +228,18 @@ pub struct CallExprWithNoArgs<'info> { pub pda: UncheckedAccount<'info>, } +const PUBKEY_CONST: Pubkey = pubkey!("4LVUJzLugULF1PemZ1StknKJEEtJM6rJZaGijpNqCouG"); + +#[derive(Accounts)] +pub struct PubkeyConst<'info> { + #[account( + seeds = [], + seeds::program = PUBKEY_CONST, + bump + )] + pub acc: UncheckedAccount<'info>, +} + #[account] pub struct MyAccount { data: u64, diff --git a/tests/pda-derivation/tests/typescript.spec.ts b/tests/pda-derivation/tests/typescript.spec.ts index 203b270b23..a1f9fcf385 100644 --- a/tests/pda-derivation/tests/typescript.spec.ts +++ b/tests/pda-derivation/tests/typescript.spec.ts @@ -146,4 +146,8 @@ describe("typescript", () => { it("Can resolve call expressions with no arguments", async () => { await program.methods.callExprWithNoArgs().rpc(); }); + + it("Can use `Pubkey` constants with `seeds::program`", async () => { + await program.methods.pubkeyConst().rpc(); + }); });