Skip to content

Commit

Permalink
ts: Add IDL seed types (#2752)
Browse files Browse the repository at this point in the history
Signed-off-by: Shiva953 <[email protected]>
Co-authored-by: acheron <[email protected]>
  • Loading branch information
Shiva953 and acheroncrypto authored Jan 4, 2024
1 parent 47ff77f commit 1a6b506
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
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
- lang: Add `#[interface(..)]` attribute for instruction discriminator overrides ([#2728](https://github.com/coral-xyz/anchor/pull/2728)).
- ts: Add `.interface(..)` method for instruction discriminator overrides ([#2728](https://github.com/coral-xyz/anchor/pull/2728)).
- cli: Check `anchor-lang` and CLI version compatibility ([#2753](https://github.com/coral-xyz/anchor/pull/2753)).
- ts: Add IdlSeed Type for IDL PDA seeds ([#2752](https://github.com/coral-xyz/anchor/pull/2752)).

### Fixes

Expand Down
21 changes: 20 additions & 1 deletion ts/packages/anchor/src/idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,26 @@ export type IdlPda = {
programId?: IdlSeed;
};

export type IdlSeed = any; // TODO
export type IdlSeed = IdlSeedConst | IdlSeedArg | IdlSeedAccount;

export type IdlSeedConst = {
kind: "const";
type: IdlType;
value: any;
};

export type IdlSeedArg = {
kind: "arg";
type: IdlType;
path: string;
};

export type IdlSeedAccount = {
kind: "account";
type: IdlType;
account?: string;
path: string;
};

// A nested/recursive version of IdlAccount.
export type IdlAccounts = {
Expand Down
23 changes: 12 additions & 11 deletions ts/packages/anchor/src/program/accounts-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {
IdlTypeDefTyStruct,
IdlType,
isIdlAccounts,
IdlSeedConst,
IdlSeedArg,
IdlSeedAccount,
} from "../idl.js";
import * as utf8 from "../utils/bytes/utf8.js";
import { TOKEN_PROGRAM_ID, ASSOCIATED_PROGRAM_ID } from "../utils/token.js";
Expand Down Expand Up @@ -385,6 +388,7 @@ export class AccountsResolver<IDL extends Idl> {
if (!accountDesc.pda?.programId) {
return this._programId;
}

switch (accountDesc.pda.programId.kind) {
case "const":
return new PublicKey(
Expand All @@ -396,7 +400,7 @@ export class AccountsResolver<IDL extends Idl> {
return await this.accountValue(accountDesc.pda.programId, path);
default:
throw new Error(
`Unexpected program seed kind: ${accountDesc.pda.programId.kind}`
`Unexpected program seed: ${accountDesc.pda.programId}`
);
}
}
Expand All @@ -413,7 +417,7 @@ export class AccountsResolver<IDL extends Idl> {
case "account":
return await this.toBufferAccount(seedDesc, path);
default:
throw new Error(`Unexpected seed kind: ${seedDesc.kind}`);
throw new Error(`Unexpected seed: ${seedDesc}`);
}
}

Expand All @@ -438,14 +442,11 @@ export class AccountsResolver<IDL extends Idl> {
return type as string;
}

private toBufferConst(seedDesc: IdlSeed): Buffer {
return this.toBufferValue(
this.getType(seedDesc.type, (seedDesc.path || "").split(".").slice(1)),
seedDesc.value
);
private toBufferConst(seedDesc: IdlSeedConst): Buffer {
return this.toBufferValue(this.getType(seedDesc.type), seedDesc.value);
}

private async toBufferArg(seedDesc: IdlSeed): Promise<Buffer | undefined> {
private async toBufferArg(seedDesc: IdlSeedArg): Promise<Buffer | undefined> {
const argValue = this.argValue(seedDesc);
if (typeof argValue === "undefined") {
return;
Expand All @@ -456,7 +457,7 @@ export class AccountsResolver<IDL extends Idl> {
);
}

private argValue(seedDesc: IdlSeed): any {
private argValue(seedDesc: IdlSeedArg): any {
const split = seedDesc.path.split(".");
const seedArgName = camelCase(split[0]);

Expand All @@ -473,7 +474,7 @@ export class AccountsResolver<IDL extends Idl> {
}

private async toBufferAccount(
seedDesc: IdlSeed,
seedDesc: IdlSeedAccount,
path: string[] = []
): Promise<Buffer | undefined> {
const accountValue = await this.accountValue(seedDesc, path);
Expand All @@ -484,7 +485,7 @@ export class AccountsResolver<IDL extends Idl> {
}

private async accountValue(
seedDesc: IdlSeed,
seedDesc: IdlSeedAccount,
path: string[] = []
): Promise<any> {
const pathComponents = seedDesc.path.split(".");
Expand Down

1 comment on commit 1a6b506

@vercel
Copy link

@vercel vercel bot commented on 1a6b506 Jan 4, 2024

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 – ./

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

Please sign in to comment.