Skip to content

Commit

Permalink
idl: Fix using Pubkey constants with seeds::program (#3559)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Feb 13, 2025
1 parent fa38102 commit 7ab0bec
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 1 addition & 11 deletions lang/syn/src/idl/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,20 +366,10 @@ fn parse_seed(seed: &syn::Expr, accounts: &AccountsStruct) -> Result<TokenStream
}
})
.unwrap_or_else(|| {
// Not all types can be converted to `Vec<u8>` 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(),
}
)
}
Expand Down
16 changes: 16 additions & 0 deletions tests/pda-derivation/programs/pda-derivation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ pub mod pda_derivation {
pub fn call_expr_with_no_args(_ctx: Context<CallExprWithNoArgs>) -> Result<()> {
Ok(())
}

pub fn pubkey_const(_ctx: Context<PubkeyConst>) -> Result<()> {
Ok(())
}
}

#[derive(Accounts)]
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions tests/pda-derivation/tests/typescript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit 7ab0bec

Please sign in to comment.