Browse Source

idl: Fix generation with unsupported expressions (#3033)

acheron 1 year ago
parent
commit
64c52c66e3

+ 1 - 0
CHANGELOG.md

@@ -49,6 +49,7 @@ The minor version will be incremented upon a breaking change and the patch versi
 - Remove `rust-version` from crate manifests ([#3000](https://github.com/coral-xyz/anchor/pull/3000)).
 - 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)).
 
 ### Breaking
 

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

@@ -389,6 +389,13 @@ impl SeedPath {
         // Convert the seed into the raw string representation.
         let seed_str = seed.to_token_stream().to_string();
 
+        // Check unsupported cases e.g. `&(account.field + 1).to_le_bytes()`
+        if !seed_str.contains('"')
+            && seed_str.contains(|c: char| matches!(c, '+' | '-' | '*' | '/' | '%' | '^'))
+        {
+            return Err(anyhow!("Seed expression not supported: {seed:#?}"));
+        }
+
         // Break up the seed into each subfield component.
         let mut components = seed_str.split('.').collect::<Vec<_>>();
         if components.len() <= 1 {

+ 12 - 0
tests/pda-derivation/programs/pda-derivation/src/lib.rs

@@ -42,6 +42,10 @@ pub mod pda_derivation {
     pub fn associated_token_resolution(_ctx: Context<AssociatedTokenResolution>) -> Result<()> {
         Ok(())
     }
+
+    pub fn seed_math_expr(_ctx: Context<SeedMathExpr>) -> Result<()> {
+        Ok(())
+    }
 }
 
 #[derive(Accounts)]
@@ -146,6 +150,14 @@ pub struct AssociatedTokenResolution<'info> {
     pub associated_token_program: Program<'info, AssociatedToken>,
 }
 
+#[derive(Accounts)]
+pub struct SeedMathExpr<'info> {
+    #[account(seeds = [b"const"], bump)]
+    pub my_account: Account<'info, MyAccount>,
+    #[account(seeds = [&(my_account.data + 1).to_le_bytes()], bump)]
+    pub math_expr_account: UncheckedAccount<'info>,
+}
+
 #[account]
 pub struct MyAccount {
     data: u64,

+ 7 - 2
tests/pda-derivation/tests/typescript.spec.ts

@@ -61,7 +61,7 @@ describe("typescript", () => {
       program.programId
     )[0];
 
-    const tx = program.methods.initMyAccount(seedA).accounts({
+    const tx = program.methods.initMyAccount(seedA).accountsPartial({
       base: base.publicKey,
       base2: base.publicKey,
       anotherBase: another.publicKey,
@@ -94,7 +94,7 @@ describe("typescript", () => {
     );
     await customProgram.methods
       .initMyAccount(seedA)
-      .accounts({
+      .accountsPartial({
         base: base.publicKey,
         base2: base.publicKey,
         anotherBase: another.publicKey,
@@ -112,4 +112,9 @@ describe("typescript", () => {
       .signers([mintKp])
       .rpc();
   });
+
+  // TODO: Support more expressions in the IDL e.g. math operations?
+  it("Can use unsupported expressions", () => {
+    // Compilation test to fix issues like https://github.com/coral-xyz/anchor/issues/2933
+  });
 });