Prechádzať zdrojové kódy

idl: Fix using `Pubkey` constants with `seeds::program` (#3559)

acheron 7 mesiacov pred
rodič
commit
7ab0becd91

+ 1 - 0
CHANGELOG.md

@@ -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
 

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

@@ -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(),
                             }
                         )
                     }

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

@@ -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)]
@@ -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,

+ 4 - 0
tests/pda-derivation/tests/typescript.spec.ts

@@ -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();
+  });
 });