Selaa lähdekoodia

idl: Fix using `address` constraint with field expressions (#3034)

acheron 1 vuosi sitten
vanhempi
sitoutus
f326b89c48

+ 1 - 0
CHANGELOG.md

@@ -51,6 +51,7 @@ The minor version will be incremented upon a breaking change and the patch versi
 - cli: Fix upgradeable program clones ([#3010](https://github.com/coral-xyz/anchor/pull/3010)).
 - ts: Fix using IDLs that have defined types as generic arguments ([#3016](https://github.com/coral-xyz/anchor/pull/3016)).
 - idl: Fix generation with unsupported expressions ([#3033](https://github.com/coral-xyz/anchor/pull/3033)).
+- idl: Fix using `address` constraint with field expressions ([#3034](https://github.com/coral-xyz/anchor/pull/3034)).
 
 ### Breaking
 

+ 1 - 0
lang/syn/src/idl/accounts.rs

@@ -161,6 +161,7 @@ fn get_address(acc: &Field) -> TokenStream {
             .address
             .as_ref()
             .map(|constraint| &constraint.address)
+            .filter(|address| !matches!(address, syn::Expr::Field(_)))
             .map(|address| quote! { Some(#address.to_string()) })
             .unwrap_or_else(|| quote! { None }),
     }

+ 14 - 0
tests/relations-derivation/programs/relations-derivation/src/lib.rs

@@ -16,12 +16,18 @@ pub mod relations_derivation {
         ctx.accounts.account.bump = ctx.bumps.account;
         Ok(())
     }
+
     pub fn test_relation(_ctx: Context<TestRelation>) -> Result<()> {
         Ok(())
     }
+
     pub fn test_seed_constant(_ctx: Context<TestSeedConstant>) -> Result<()> {
         Ok(())
     }
+
+    pub fn test_address_relation(_ctx: Context<TestAddressRelation>) -> Result<()> {
+        Ok(())
+    }
 }
 
 #[derive(Accounts)]
@@ -80,6 +86,14 @@ pub struct TestSeedConstant<'info> {
     system_program: Program<'info, System>,
 }
 
+#[derive(Accounts)]
+pub struct TestAddressRelation<'info> {
+    #[account(address = my_account.my_account)]
+    account: UncheckedAccount<'info>,
+    #[account(seeds = [b"seed"], bump = my_account.bump)]
+    my_account: Account<'info, MyAccount>,
+}
+
 #[account]
 pub struct MyAccount {
     pub my_account: Pubkey,

+ 5 - 0
tests/relations-derivation/tests/typescript.spec.ts

@@ -44,4 +44,9 @@ describe("typescript", () => {
   it("Can use relations derivation with seed constant", async () => {
     await program.methods.testSeedConstant().accounts({}).rpc();
   });
+
+  it("Can use relations derivation with `address` constraint", () => {
+    // Only compile test for now since the IDL spec doesn't currently support field access
+    // expressions for the `address` constraint
+  });
 });