Skip to content

Commit

Permalink
syn: Fix IDL constant seeds parsing (#2699)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnanthos authored Nov 15, 2023
1 parent cded9de commit a9c423e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- syn: Add missing `new_from_array` method to `Hash` ([#2682](https://github.com/coral-xyz/anchor/pull/2682)).
- cli: Switch to Cargo feature resolver(`resolver = "2"`) ([#2676](https://github.com/coral-xyz/anchor/pull/2676)).
- cli: Fix using user specific path for `provider.wallet` in `Anchor.toml` ([#2696](https://github.com/coral-xyz/anchor/pull/2696)).
- syn: Fix IDL constant seeds parsing ([#2699](https://github.com/coral-xyz/anchor/pull/2699)).
- cli: Display errors if toolchain override restoration fails ([#2700](https://github.com/coral-xyz/anchor/pull/2700)).

### Breaking
Expand Down
22 changes: 21 additions & 1 deletion lang/syn/src/idl/parse/pda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::ConstraintSeedsGroup;
use crate::{AccountsStruct, Field};
use std::collections::HashMap;
use std::str::FromStr;
use syn::{Expr, ExprLit, Lit};
use syn::{Expr, ExprLit, Lit, Path};

// Parses a seeds constraint, extracting the IdlSeed types.
//
Expand Down Expand Up @@ -135,6 +135,7 @@ impl<'a> PdaParser<'a> {
let seed_path: SeedPath = SeedPath(lit_byte_str.token().to_string(), Vec::new());
self.parse_str_literal(&seed_path)
}
Expr::Path(expr_path) => self.parse_const_path(&expr_path.path),
// Unknown type. Please file an issue.
_ => {
println!("WARNING: unexpected seed: {seed:?}");
Expand All @@ -151,6 +152,25 @@ impl<'a> PdaParser<'a> {
}))
}

fn parse_const_path(&self, path: &Path) -> Option<IdlSeed> {
let ident = &path.segments.first().unwrap().ident;

let const_item = self.ctx.consts().find(|c| c.ident == *ident).unwrap();
let idl_ty = IdlType::from_str(&parser::tts_to_string(&const_item.ty)).ok()?;

let idl_ty_value = parser::tts_to_string(&const_item.expr);
let idl_ty_value: String = str_lit_to_array(&idl_ty, &idl_ty_value);

let seed_path: SeedPath = SeedPath(idl_ty_value, Vec::new());

if self.is_str_literal(&seed_path) {
self.parse_str_literal(&seed_path)
} else {
println!("WARNING: unexpected constant path value: {seed_path:?}");
None
}
}

fn parse_const(&self, seed_path: &SeedPath) -> Option<IdlSeed> {
// Pull in the constant value directly into the IDL.
assert!(seed_path.components().is_empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use anchor_lang::prelude::*;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

pub const SEED: &[u8] = b"contant-seed";

#[program]
pub mod relations_derivation {
use super::*;
Expand All @@ -17,6 +19,9 @@ pub mod relations_derivation {
pub fn test_relation(_ctx: Context<TestRelation>) -> Result<()> {
Ok(())
}
pub fn test_seed_constant(_ctx: Context<TestSeedConstant>) -> Result<()> {
Ok(())
}
}

#[derive(Accounts)]
Expand Down Expand Up @@ -60,6 +65,21 @@ pub struct TestRelation<'info> {
nested: Nested<'info>,
}

#[derive(Accounts)]
pub struct TestSeedConstant<'info> {
#[account(mut)]
my_account: Signer<'info>,
#[account(
init,
payer = my_account,
seeds = [SEED],
space = 100,
bump,
)]
account: Account<'info, MyAccount>,
system_program: Program<'info, System>,
}

#[account]
pub struct MyAccount {
pub my_account: Pubkey,
Expand Down
4 changes: 4 additions & 0 deletions tests/relations-derivation/tests/typescript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ describe("typescript", () => {

await tx.rpc();
});

it("Can use relations derivation with seed constant", async () => {
await program.methods.testSeedConstant().accounts({}).rpc();
});
});

1 comment on commit a9c423e

@vercel
Copy link

@vercel vercel bot commented on a9c423e Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

anchor-docs – ./

anchor-lang.com
anchor-docs-git-master-200ms.vercel.app
anchor-docs-200ms.vercel.app
www.anchor-lang.com

Please sign in to comment.