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

Fix generics being parsed wrong in idl #3016

Merged
merged 5 commits into from
Jun 10, 2024
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 @@ -44,6 +44,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- lang: Make tuple struct fields public in `declare_program!` ([#2994](https://github.com/coral-xyz/anchor/pull/2994)).
- 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)).

### Breaking

Expand Down
33 changes: 33 additions & 0 deletions tests/idl/programs/new-idl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ pub mod new_idl {
Ok(())
}

pub fn generic_custom_struct(
ctx: Context<GenericCustomStruct>,
generic_arg: GenericStruct<SomeStruct, 4>,
) -> Result<()> {
ctx.accounts.my_account.field = generic_arg;
Ok(())
}

pub fn full_path(
ctx: Context<FullPath>,
named_struct: NamedStruct,
Expand Down Expand Up @@ -315,11 +323,36 @@ pub struct Generic<'info> {
pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct GenericCustomStruct<'info> {
#[account(mut)]
pub signer: Signer<'info>,
#[account(
init,
payer = signer,
space = 1024,
seeds = [b"genericCustomStruct", signer.key.as_ref()],
bump
)]
pub my_account: Account<'info, GenericAccountCustomStruct>,
pub system_program: Program<'info, System>,
}

#[account]
pub struct GenericAccount {
pub field: GenericStruct<u16, 4>,
}

#[account]
pub struct GenericAccountCustomStruct {
pub field: GenericStruct<SomeStruct, 4>,
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct SomeStruct {
pub field: u16,
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug)]
pub struct GenericStruct<T, const N: usize> {
arr: [T; N],
Expand Down
22 changes: 22 additions & 0 deletions tests/idl/tests/new-idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,28 @@ describe("New IDL", () => {
assert.deepEqual(myAccount.field, arg);
});

it("Can use generics populated with custom struct", async () => {
const arg = {
arr: [{ field: 1 }, { field: 2 }, { field: 3 }, { field: 4 }],
subField: {
subArr: new Array(8).fill(null).map((_, i) => ({ field: i })),
another: [
{ field: 42 },
{ field: 420 },
{ field: 4_200 },
{ field: 42_000 },
],
},
};
const { pubkeys } = await program.methods
.genericCustomStruct(arg)
.rpcAndKeys();
const myAccount = await program.account.genericAccountCustomStruct.fetch(
pubkeys.myAccount
);
assert.deepEqual(myAccount.field, arg);
});

it("Can use full module path types", async () => {
const kp = anchor.web3.Keypair.generate();

Expand Down
5 changes: 4 additions & 1 deletion ts/packages/anchor/src/coder/borsh/idl.ts
cryptopapi997 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ export class IdlCoder {
throw new IdlError(`Invalid generic field: ${field.name}`);
}

return IdlCoder.fieldLayout({ ...field, type: genericArg.type });
return IdlCoder.fieldLayout(
{ ...field, type: genericArg.type },
types
);
}

throw new IdlError(
Expand Down
Loading