Skip to content

Commit

Permalink
idl: Support PDA resolution of call expressions that don't have any a…
Browse files Browse the repository at this point in the history
…rguments (#3485)
  • Loading branch information
acheroncrypto authored Jan 16, 2025
1 parent db6da5c commit 23d1a2c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- idl: Disallow account discriminators that can conflict with the `zero` constraint ([#3365](https://github.com/coral-xyz/anchor/pull/3365)).
- cli: Include recommended solana args by default and add new `--max-retries` option to the `deploy` command ([#3354](https://github.com/coral-xyz/anchor/pull/3354)).
- avm: Make installation download binaries by default ([#3445](https://github.com/coral-xyz/anchor/pull/3445)).
- idl: Support PDA resolution of call expressions that don't have any arguments ([#3485](https://github.com/coral-xyz/anchor/pull/3485)).

### Fixes

Expand Down
8 changes: 8 additions & 0 deletions lang/syn/src/idl/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,14 @@ fn parse_seed(seed: &syn::Expr, accounts: &AccountsStruct) -> Result<TokenStream
})
}
}
// Support call expressions that don't have any arguments e.g. `System::id()`
syn::Expr::Call(call) if call.args.is_empty() => Ok(quote! {
#idl::IdlSeed::Const(
#idl::IdlSeedConst {
value: AsRef::<[u8]>::as_ref(&#seed).into(),
}
)
}),
syn::Expr::Path(path) => {
let seed = path
.path
Expand Down
14 changes: 14 additions & 0 deletions tests/pda-derivation/programs/pda-derivation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ pub mod pda_derivation {
pub fn unsupported_program_seed(_ctx: Context<UnsupportedProgramSeed>) -> Result<()> {
Ok(())
}

pub fn call_expr_with_no_args(_ctx: Context<CallExprWithNoArgs>) -> Result<()> {
Ok(())
}
}

#[derive(Accounts)]
Expand Down Expand Up @@ -210,6 +214,16 @@ fn external_function_with_an_argument(pk: &Pubkey) -> Pubkey {
*pk
}

#[derive(Accounts)]
pub struct CallExprWithNoArgs<'info> {
#[account(
seeds = [System::id().as_ref()],
seeds::program = System::id(),
bump
)]
pub pda: 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 @@ -142,4 +142,8 @@ describe("typescript", () => {
// @ts-expect-error
expect(acc.pda).to.be.undefined;
});

it("Can resolve call expressions with no arguments", async () => {
await program.methods.callExprWithNoArgs().rpc();
});
});

0 comments on commit 23d1a2c

Please sign in to comment.