From 6277785cf9037222f708431a3185f50ef7b884ac Mon Sep 17 00:00:00 2001 From: Shiva953 Date: Sat, 30 Dec 2023 10:04:16 +0530 Subject: [PATCH 01/10] added IdlSeed Type for IDL PDA seeds Signed-off-by: Shiva953 --- ts/packages/anchor/src/idl.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ts/packages/anchor/src/idl.ts b/ts/packages/anchor/src/idl.ts index dc88cd586e..9bc1b0b3fc 100644 --- a/ts/packages/anchor/src/idl.ts +++ b/ts/packages/anchor/src/idl.ts @@ -67,7 +67,16 @@ export type IdlPda = { programId?: IdlSeed; }; -export type IdlSeed = any; // TODO +// Based on the rust type defined in lang/sync/src/idl/types.rs and the definitions in lang/syn/src/idl/pda.rs +export type IdlSeed = + | { kind: 'Const'; value: IdlConstant } + | { kind: 'Arg'; value: IdlSeedArg } + | { kind: 'Account'; value: IdlAccount }; + +export interface IdlSeedArg { + type: IdlType; + path: string; +} // A nested/recursive version of IdlAccount. export type IdlAccounts = { From 5192e328c0ef69a615a9e02977477de55474635e Mon Sep 17 00:00:00 2001 From: Shiva953 Date: Sun, 31 Dec 2023 09:50:31 +0530 Subject: [PATCH 02/10] updated types --- ts/packages/anchor/src/idl.ts | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/ts/packages/anchor/src/idl.ts b/ts/packages/anchor/src/idl.ts index 9bc1b0b3fc..f990f2c909 100644 --- a/ts/packages/anchor/src/idl.ts +++ b/ts/packages/anchor/src/idl.ts @@ -69,14 +69,36 @@ export type IdlPda = { // Based on the rust type defined in lang/sync/src/idl/types.rs and the definitions in lang/syn/src/idl/pda.rs export type IdlSeed = - | { kind: 'Const'; value: IdlConstant } - | { kind: 'Arg'; value: IdlSeedArg } - | { kind: 'Account'; value: IdlAccount }; + | IdlSeedConst + | IdlSeedArg + | IdlSeedAccount; -export interface IdlSeedArg { +export type IdlSeedConst = { + kind: "const"; + type: IdlType; + value: SerdeJsonValue; +}; + +export type SerdeJsonValue = + | null + | boolean + | number + | string + | SerdeJsonValue[] + | { [key: string]: SerdeJsonValue }; + +export type IdlSeedArg = { + kind: "arg"; type: IdlType; path: string; -} +}; + +export type IdlSeedAccount = { + kind: "account"; + type: IdlType; + account?: string; // Adjust the type based on the actual type of account in your use case + path: string; +}; // A nested/recursive version of IdlAccount. export type IdlAccounts = { From baac5894ed4ec8908a99f2cd72f9766f32b08f15 Mon Sep 17 00:00:00 2001 From: Shiva953 Date: Sun, 31 Dec 2023 11:08:21 +0530 Subject: [PATCH 03/10] removed the SerdeJsonValue type and added any --- ts/packages/anchor/src/idl.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/ts/packages/anchor/src/idl.ts b/ts/packages/anchor/src/idl.ts index f990f2c909..c4e149f3f6 100644 --- a/ts/packages/anchor/src/idl.ts +++ b/ts/packages/anchor/src/idl.ts @@ -76,17 +76,9 @@ export type IdlSeed = export type IdlSeedConst = { kind: "const"; type: IdlType; - value: SerdeJsonValue; + value: any; }; -export type SerdeJsonValue = - | null - | boolean - | number - | string - | SerdeJsonValue[] - | { [key: string]: SerdeJsonValue }; - export type IdlSeedArg = { kind: "arg"; type: IdlType; @@ -96,7 +88,7 @@ export type IdlSeedArg = { export type IdlSeedAccount = { kind: "account"; type: IdlType; - account?: string; // Adjust the type based on the actual type of account in your use case + account?: string; path: string; }; From f37b3e0d0bc0d8326e4ead6ecfde9fd555d2c494 Mon Sep 17 00:00:00 2001 From: Shiva953 Date: Sun, 31 Dec 2023 11:10:42 +0530 Subject: [PATCH 04/10] made required changes Signed-off-by: Shiva953 --- ts/packages/anchor/src/idl.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/ts/packages/anchor/src/idl.ts b/ts/packages/anchor/src/idl.ts index c4e149f3f6..f8afd46e57 100644 --- a/ts/packages/anchor/src/idl.ts +++ b/ts/packages/anchor/src/idl.ts @@ -67,7 +67,6 @@ export type IdlPda = { programId?: IdlSeed; }; -// Based on the rust type defined in lang/sync/src/idl/types.rs and the definitions in lang/syn/src/idl/pda.rs export type IdlSeed = | IdlSeedConst | IdlSeedArg From 0da13ce69a6a75aa6fb2e8e3113787d756ae14a1 Mon Sep 17 00:00:00 2001 From: Shiva953 Date: Sun, 31 Dec 2023 11:18:43 +0530 Subject: [PATCH 05/10] updated changelog Signed-off-by: Shiva953 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55cc47e2b7..5181d2db3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ The minor version will be incremented upon a breaking change and the patch versi - lang: Add `InstructionData::write_to` implementation ([#2733](https://github.com/coral-xyz/anchor/pull/2733)). - 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)). +- ts: Add IdlSeed Type for IDL PDA seeds ([#2752](https://github.com/coral-xyz/anchor/pull/2752)) ### Fixes From 47fafae84c448288c98d494fa1afa52a3ee7e668 Mon Sep 17 00:00:00 2001 From: Shiva953 Date: Tue, 2 Jan 2024 19:19:21 +0530 Subject: [PATCH 06/10] fixed build errors Signed-off-by: Shiva953 --- ts/packages/anchor/src/idl.ts | 5 +++++ ts/packages/anchor/src/program/accounts-resolver.ts | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ts/packages/anchor/src/idl.ts b/ts/packages/anchor/src/idl.ts index f8afd46e57..cc34a77a27 100644 --- a/ts/packages/anchor/src/idl.ts +++ b/ts/packages/anchor/src/idl.ts @@ -76,12 +76,16 @@ export type IdlSeedConst = { kind: "const"; type: IdlType; value: any; + path: string; + account?: string; }; export type IdlSeedArg = { kind: "arg"; type: IdlType; path: string; + value: any; + account?: string; }; export type IdlSeedAccount = { @@ -89,6 +93,7 @@ export type IdlSeedAccount = { type: IdlType; account?: string; path: string; + value: any; }; // A nested/recursive version of IdlAccount. diff --git a/ts/packages/anchor/src/program/accounts-resolver.ts b/ts/packages/anchor/src/program/accounts-resolver.ts index 42b483e7d2..b1089df0c2 100644 --- a/ts/packages/anchor/src/program/accounts-resolver.ts +++ b/ts/packages/anchor/src/program/accounts-resolver.ts @@ -385,6 +385,7 @@ export class AccountsResolver { if (!accountDesc.pda?.programId) { return this._programId; } + console.log(accountDesc.pda.programId) switch (accountDesc.pda.programId.kind) { case "const": return new PublicKey( @@ -396,7 +397,7 @@ export class AccountsResolver { return await this.accountValue(accountDesc.pda.programId, path); default: throw new Error( - `Unexpected program seed kind: ${accountDesc.pda.programId.kind}` + `Unexpected program seed kind` ); } } @@ -413,7 +414,7 @@ export class AccountsResolver { case "account": return await this.toBufferAccount(seedDesc, path); default: - throw new Error(`Unexpected seed kind: ${seedDesc.kind}`); + throw new Error(`Unexpected seed kind`); } } From 829e567a971bbe0898e43905d4f6a3842e6ab5b4 Mon Sep 17 00:00:00 2001 From: Shiva953 Date: Tue, 2 Jan 2024 19:26:45 +0530 Subject: [PATCH 07/10] removed console.log check Signed-off-by: Shiva953 --- ts/packages/anchor/src/program/accounts-resolver.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ts/packages/anchor/src/program/accounts-resolver.ts b/ts/packages/anchor/src/program/accounts-resolver.ts index b1089df0c2..8510046a50 100644 --- a/ts/packages/anchor/src/program/accounts-resolver.ts +++ b/ts/packages/anchor/src/program/accounts-resolver.ts @@ -385,7 +385,7 @@ export class AccountsResolver { if (!accountDesc.pda?.programId) { return this._programId; } - console.log(accountDesc.pda.programId) + switch (accountDesc.pda.programId.kind) { case "const": return new PublicKey( From b1d695340528cf21c8dc2e12e834695fcfa3b1c5 Mon Sep 17 00:00:00 2001 From: Shiva953 Date: Wed, 3 Jan 2024 09:44:55 +0530 Subject: [PATCH 08/10] fixed linting errors and changelog update Signed-off-by: Shiva953 --- CHANGELOG.md | 3 ++- ts/packages/anchor/src/idl.ts | 5 +---- ts/packages/anchor/src/program/accounts-resolver.ts | 6 ++---- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5181d2db3a..c21bf728b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,8 @@ The minor version will be incremented upon a breaking change and the patch versi - lang: Add `InstructionData::write_to` implementation ([#2733](https://github.com/coral-xyz/anchor/pull/2733)). - 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)). -- ts: Add IdlSeed Type for IDL PDA seeds ([#2752](https://github.com/coral-xyz/anchor/pull/2752)) +- 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 diff --git a/ts/packages/anchor/src/idl.ts b/ts/packages/anchor/src/idl.ts index cc34a77a27..65c9df63b9 100644 --- a/ts/packages/anchor/src/idl.ts +++ b/ts/packages/anchor/src/idl.ts @@ -67,10 +67,7 @@ export type IdlPda = { programId?: IdlSeed; }; -export type IdlSeed = - | IdlSeedConst - | IdlSeedArg - | IdlSeedAccount; +export type IdlSeed = IdlSeedConst | IdlSeedArg | IdlSeedAccount; export type IdlSeedConst = { kind: "const"; diff --git a/ts/packages/anchor/src/program/accounts-resolver.ts b/ts/packages/anchor/src/program/accounts-resolver.ts index 8510046a50..19dd09dc4b 100644 --- a/ts/packages/anchor/src/program/accounts-resolver.ts +++ b/ts/packages/anchor/src/program/accounts-resolver.ts @@ -385,7 +385,7 @@ export class AccountsResolver { if (!accountDesc.pda?.programId) { return this._programId; } - + switch (accountDesc.pda.programId.kind) { case "const": return new PublicKey( @@ -396,9 +396,7 @@ export class AccountsResolver { case "account": return await this.accountValue(accountDesc.pda.programId, path); default: - throw new Error( - `Unexpected program seed kind` - ); + throw new Error(`Unexpected program seed kind`); } } From 6fc79cda99e03270d1560def2ce1184220061f55 Mon Sep 17 00:00:00 2001 From: acheron Date: Thu, 4 Jan 2024 09:39:50 +0100 Subject: [PATCH 09/10] Add correct types --- ts/packages/anchor/src/idl.ts | 5 ----- .../anchor/src/program/accounts-resolver.ts | 18 +++++++++--------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/ts/packages/anchor/src/idl.ts b/ts/packages/anchor/src/idl.ts index 65c9df63b9..4a6c71fb72 100644 --- a/ts/packages/anchor/src/idl.ts +++ b/ts/packages/anchor/src/idl.ts @@ -73,16 +73,12 @@ export type IdlSeedConst = { kind: "const"; type: IdlType; value: any; - path: string; - account?: string; }; export type IdlSeedArg = { kind: "arg"; type: IdlType; path: string; - value: any; - account?: string; }; export type IdlSeedAccount = { @@ -90,7 +86,6 @@ export type IdlSeedAccount = { type: IdlType; account?: string; path: string; - value: any; }; // A nested/recursive version of IdlAccount. diff --git a/ts/packages/anchor/src/program/accounts-resolver.ts b/ts/packages/anchor/src/program/accounts-resolver.ts index 19dd09dc4b..683337f514 100644 --- a/ts/packages/anchor/src/program/accounts-resolver.ts +++ b/ts/packages/anchor/src/program/accounts-resolver.ts @@ -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"; @@ -437,14 +440,11 @@ export class AccountsResolver { 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 { + private async toBufferArg(seedDesc: IdlSeedArg): Promise { const argValue = this.argValue(seedDesc); if (typeof argValue === "undefined") { return; @@ -455,7 +455,7 @@ export class AccountsResolver { ); } - private argValue(seedDesc: IdlSeed): any { + private argValue(seedDesc: IdlSeedArg): any { const split = seedDesc.path.split("."); const seedArgName = camelCase(split[0]); @@ -472,7 +472,7 @@ export class AccountsResolver { } private async toBufferAccount( - seedDesc: IdlSeed, + seedDesc: IdlSeedAccount, path: string[] = [] ): Promise { const accountValue = await this.accountValue(seedDesc, path); @@ -483,7 +483,7 @@ export class AccountsResolver { } private async accountValue( - seedDesc: IdlSeed, + seedDesc: IdlSeedAccount, path: string[] = [] ): Promise { const pathComponents = seedDesc.path.split("."); From 8bc506ba9d69b6a5cc119cac897ae04f65a77f2f Mon Sep 17 00:00:00 2001 From: acheron Date: Thu, 4 Jan 2024 09:44:01 +0100 Subject: [PATCH 10/10] Add back specifying the seed that caused the error --- ts/packages/anchor/src/program/accounts-resolver.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ts/packages/anchor/src/program/accounts-resolver.ts b/ts/packages/anchor/src/program/accounts-resolver.ts index 683337f514..0bf4be87b1 100644 --- a/ts/packages/anchor/src/program/accounts-resolver.ts +++ b/ts/packages/anchor/src/program/accounts-resolver.ts @@ -399,7 +399,9 @@ export class AccountsResolver { case "account": return await this.accountValue(accountDesc.pda.programId, path); default: - throw new Error(`Unexpected program seed kind`); + throw new Error( + `Unexpected program seed: ${accountDesc.pda.programId}` + ); } } @@ -415,7 +417,7 @@ export class AccountsResolver { case "account": return await this.toBufferAccount(seedDesc, path); default: - throw new Error(`Unexpected seed kind`); + throw new Error(`Unexpected seed: ${seedDesc}`); } }