Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

idl: Support PDA resolution of call expressions that don't have any arguments #3485

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
Loading