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

ts: Add IDL seed types #2752

Merged
merged 11 commits into from
Jan 4, 2024
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
Loading