From c023628aacaadd9b16663d966cd8098c245eb51b Mon Sep 17 00:00:00 2001 From: Chewing Glass Date: Tue, 20 Sep 2022 16:53:57 -0500 Subject: [PATCH 01/10] feat: Recursively derive seeds --- .../anchor/src/program/accounts-resolver.ts | 59 +++++++++++++------ 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/ts/packages/anchor/src/program/accounts-resolver.ts b/ts/packages/anchor/src/program/accounts-resolver.ts index 186909de15..cd7d25b427 100644 --- a/ts/packages/anchor/src/program/accounts-resolver.ts +++ b/ts/packages/anchor/src/program/accounts-resolver.ts @@ -84,22 +84,8 @@ export class AccountsResolver> { } } - for (let k = 0; k < this._idlIx.accounts.length; k += 1) { - // Cast is ok because only a non-nested IdlAccount can have a seeds - // cosntraint. - const accountDesc = this._idlIx.accounts[k] as IdlAccount; - const accountDescName = camelCase(accountDesc.name); - - // PDA derived from IDL seeds. - if ( - accountDesc.pda && - accountDesc.pda.seeds.length > 0 && - !this._accounts[accountDescName] - ) { - await this.autoPopulatePda(accountDesc); - continue; - } - } + // Auto populate pdas until we stop finding new accounts + while ((await this.resolvePdas(this._idlIx.accounts)) > 0) {} // Auto populate has_one relationships until we stop finding new accounts while ((await this.resolveRelations(this._idlIx.accounts)) > 0) {} @@ -130,6 +116,43 @@ export class AccountsResolver> { }); } + private async resolvePdas( + accounts: IdlAccountItem[], + path: string[] = [] + ): Promise { + for (let k = 0; k < this._idlIx.accounts.length; k += 1) { + // Cast is ok because only a non-nested IdlAccount can have a seeds + // cosntraint. + const accountDesc = this._idlIx.accounts[k] as IdlAccount; + const accountDescName = camelCase(accountDesc.name); + } + + let found = 0; + for (let k = 0; k < accounts.length; k += 1) { + const accountDesc = accounts[k]; + const subAccounts = (accountDesc as IdlAccounts).accounts; + if (subAccounts) { + found += await this.resolveRelations(subAccounts, [ + ...path, + accountDesc.name, + ]); + } + + const accountDescCasted: IdlAccount = accountDesc as IdlAccount; + const accountDescName = camelCase(accountDesc.name); + // PDA derived from IDL seeds. + if ( + accountDescCasted.pda && + accountDescCasted.pda.seeds.length > 0 && + !this._accounts[accountDescName] + ) { + await this.autoPopulatePda(accountDescCasted, path); + found += 1; + } + } + return found; + } + private async resolveRelations( accounts: IdlAccountItem[], path: string[] = [] @@ -172,7 +195,7 @@ export class AccountsResolver> { return found; } - private async autoPopulatePda(accountDesc: IdlAccount) { + private async autoPopulatePda(accountDesc: IdlAccount, path: string[] = []) { if (!accountDesc.pda || !accountDesc.pda.seeds) throw new Error("Must have seeds"); @@ -183,7 +206,7 @@ export class AccountsResolver> { const programId = await this.parseProgramId(accountDesc); const [pubkey] = await PublicKey.findProgramAddress(seeds, programId); - this._accounts[camelCase(accountDesc.name)] = pubkey; + this.set([...path, camelCase(accountDesc.name)], pubkey); } private async parseProgramId(accountDesc: IdlAccount): Promise { From a0461047e3907a3c79be43d9b8ae9353efbc3223 Mon Sep 17 00:00:00 2001 From: Chewing Glass Date: Tue, 20 Sep 2022 17:31:16 -0500 Subject: [PATCH 02/10] Add a nested pda --- .../programs/pda-derivation/src/lib.rs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/pda-derivation/programs/pda-derivation/src/lib.rs b/tests/pda-derivation/programs/pda-derivation/src/lib.rs index 43fdbc15bf..4a0a3993c3 100644 --- a/tests/pda-derivation/programs/pda-derivation/src/lib.rs +++ b/tests/pda-derivation/programs/pda-derivation/src/lib.rs @@ -67,11 +67,34 @@ pub struct InitMyAccount<'info> { bump, )] account: Account<'info, MyAccount>, + nested: Nested<'info>, #[account(mut)] payer: Signer<'info>, system_program: Program<'info, System>, } +#[derive(Accounts)] +#[instruction(seed_a: u8)] +pub struct Nested<'info> { + #[account( + init, + payer = payer, + space = 8+8, + seeds = [ + &seed_a.to_le_bytes(), + "nested-seed".as_bytes(), + b"test".as_ref(), + MY_SEED.as_ref(), + MY_SEED_STR.as_bytes(), + MY_SEED_U8.to_le_bytes().as_ref(), + &MY_SEED_U32.to_le_bytes(), + &MY_SEED_U64.to_le_bytes(), + ], + bump, + )] + account_nested: Account<'info, MyAccount>, +} + #[account] pub struct MyAccount { data: u64, From 3581e9f2581cd657bde53a2667c3613b998ded3d Mon Sep 17 00:00:00 2001 From: Chewing Glass Date: Tue, 20 Sep 2022 17:32:26 -0500 Subject: [PATCH 03/10] Add to changelog --- CHANGELOG.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b670ca9e3..13e1c3c860 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,10 +20,11 @@ The minor version will be incremented upon a breaking change and the patch versi * lang: Add parsing for consts from impl blocks for IDL PDA seeds generation ([#2128](https://github.com/coral-xyz/anchor/pull/2014)) * lang: Account closing reassigns to system program and reallocates ([#2169](https://github.com/coral-xyz/anchor/pull/2169)). * ts: Add coders for SPL programs ([#2143](https://github.com/coral-xyz/anchor/pull/2143)). -* ts: Add `has_one` relations inference so accounts mapped via has_one relationships no longer need to be provided -* ts: Add ability to set args after setting accounts and retriving pubkyes -* ts: Add `.prepare()` to builder pattern +* ts: Add `has_one` relations inference so accounts mapped via has_one relationships no longer need to be provided ([#2160](https://github.com/coral-xyz/anchor/pull/2160)) +* ts: Add ability to set args after setting accounts and retriving pubkyes ([#2160](https://github.com/coral-xyz/anchor/pull/2160)) +* ts: Add `.prepare()` to builder pattern ([#2160](https://github.com/coral-xyz/anchor/pull/2160)) * spl: Add `freeze_delegated_account` and `thaw_delegated_account` wrappers ([#2164](https://github.com/coral-xyz/anchor/pull/2164)) +* ts: Add nested PDA inference ### Fixes From 98c642d88e3358626230b55ef8b41d88190f3e02 Mon Sep 17 00:00:00 2001 From: Chewing Glass Date: Tue, 20 Sep 2022 17:50:15 -0500 Subject: [PATCH 04/10] Fix --- tests/pda-derivation/programs/pda-derivation/src/lib.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/pda-derivation/programs/pda-derivation/src/lib.rs b/tests/pda-derivation/programs/pda-derivation/src/lib.rs index 4a0a3993c3..9df26f26b0 100644 --- a/tests/pda-derivation/programs/pda-derivation/src/lib.rs +++ b/tests/pda-derivation/programs/pda-derivation/src/lib.rs @@ -77,9 +77,6 @@ pub struct InitMyAccount<'info> { #[instruction(seed_a: u8)] pub struct Nested<'info> { #[account( - init, - payer = payer, - space = 8+8, seeds = [ &seed_a.to_le_bytes(), "nested-seed".as_bytes(), @@ -92,7 +89,8 @@ pub struct Nested<'info> { ], bump, )] - account_nested: Account<'info, MyAccount>, + /// CHECK: Not needed + account_nested: AccountInfo<'info>, } #[account] From 0159a82a78151822869abb712647729ea234649b Mon Sep 17 00:00:00 2001 From: Chewing Glass Date: Tue, 20 Sep 2022 18:05:47 -0500 Subject: [PATCH 05/10] Fix changelog and resolver --- CHANGELOG.md | 2 +- ts/packages/anchor/src/program/accounts-resolver.ts | 9 +-------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13e1c3c860..810985516a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,7 +24,7 @@ The minor version will be incremented upon a breaking change and the patch versi * ts: Add ability to set args after setting accounts and retriving pubkyes ([#2160](https://github.com/coral-xyz/anchor/pull/2160)) * ts: Add `.prepare()` to builder pattern ([#2160](https://github.com/coral-xyz/anchor/pull/2160)) * spl: Add `freeze_delegated_account` and `thaw_delegated_account` wrappers ([#2164](https://github.com/coral-xyz/anchor/pull/2164)) -* ts: Add nested PDA inference +* ts: Add nested PDA inference ([#2194](https://github.com/coral-xyz/anchor/pull/2194)) ### Fixes diff --git a/ts/packages/anchor/src/program/accounts-resolver.ts b/ts/packages/anchor/src/program/accounts-resolver.ts index cd7d25b427..5352585820 100644 --- a/ts/packages/anchor/src/program/accounts-resolver.ts +++ b/ts/packages/anchor/src/program/accounts-resolver.ts @@ -120,13 +120,6 @@ export class AccountsResolver> { accounts: IdlAccountItem[], path: string[] = [] ): Promise { - for (let k = 0; k < this._idlIx.accounts.length; k += 1) { - // Cast is ok because only a non-nested IdlAccount can have a seeds - // cosntraint. - const accountDesc = this._idlIx.accounts[k] as IdlAccount; - const accountDescName = camelCase(accountDesc.name); - } - let found = 0; for (let k = 0; k < accounts.length; k += 1) { const accountDesc = accounts[k]; @@ -144,7 +137,7 @@ export class AccountsResolver> { if ( accountDescCasted.pda && accountDescCasted.pda.seeds.length > 0 && - !this._accounts[accountDescName] + !this.get([...path, accountDescName]) ) { await this.autoPopulatePda(accountDescCasted, path); found += 1; From 0e115653d7fa05a326afa3501d0befb6e416e9c0 Mon Sep 17 00:00:00 2001 From: Chewing Glass Date: Tue, 20 Sep 2022 18:45:43 -0500 Subject: [PATCH 06/10] bug --- 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 5352585820..9483891721 100644 --- a/ts/packages/anchor/src/program/accounts-resolver.ts +++ b/ts/packages/anchor/src/program/accounts-resolver.ts @@ -125,7 +125,7 @@ export class AccountsResolver> { const accountDesc = accounts[k]; const subAccounts = (accountDesc as IdlAccounts).accounts; if (subAccounts) { - found += await this.resolveRelations(subAccounts, [ + found += await this.resolvePdas(subAccounts, [ ...path, accountDesc.name, ]); From de7d0c96710e633a18327725cda68cf26e3d7436 Mon Sep 17 00:00:00 2001 From: Chewing Glass Date: Wed, 21 Sep 2022 09:58:13 -0500 Subject: [PATCH 07/10] Try to fix deserialize issue --- tests/pda-derivation/programs/pda-derivation/src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/pda-derivation/programs/pda-derivation/src/lib.rs b/tests/pda-derivation/programs/pda-derivation/src/lib.rs index 9df26f26b0..b56b039bc3 100644 --- a/tests/pda-derivation/programs/pda-derivation/src/lib.rs +++ b/tests/pda-derivation/programs/pda-derivation/src/lib.rs @@ -3,7 +3,7 @@ use anchor_lang::prelude::*; -declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"); +declare_id!("ATD5DsyrGhh6bV5PigQ5qQ48uYAChk68YBTkUvdqyJZj"); pub const MY_SEED: [u8; 2] = *b"hi"; pub const MY_SEED_STR: &str = "hi"; @@ -74,11 +74,9 @@ pub struct InitMyAccount<'info> { } #[derive(Accounts)] -#[instruction(seed_a: u8)] pub struct Nested<'info> { #[account( seeds = [ - &seed_a.to_le_bytes(), "nested-seed".as_bytes(), b"test".as_ref(), MY_SEED.as_ref(), From cd74a57e91e2c989e5143e589b26fbf4261fd04d Mon Sep 17 00:00:00 2001 From: Chewing Glass Date: Wed, 21 Sep 2022 11:13:57 -0500 Subject: [PATCH 08/10] Add custom accounts resolver option --- CHANGELOG.md | 1 + .../programs/pda-derivation/src/lib.rs | 2 +- tests/pda-derivation/tests/typescript.spec.ts | 24 ++++++++++++++ .../anchor/src/program/accounts-resolver.ts | 31 ++++++++++++++++--- ts/packages/anchor/src/program/index.ts | 19 ++++++++++-- .../anchor/src/program/namespace/index.ts | 9 ++++-- .../anchor/src/program/namespace/methods.ts | 14 ++++++--- 7 files changed, 83 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 810985516a..2a4e68c3b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ The minor version will be incremented upon a breaking change and the patch versi * ts: Add `.prepare()` to builder pattern ([#2160](https://github.com/coral-xyz/anchor/pull/2160)) * spl: Add `freeze_delegated_account` and `thaw_delegated_account` wrappers ([#2164](https://github.com/coral-xyz/anchor/pull/2164)) * ts: Add nested PDA inference ([#2194](https://github.com/coral-xyz/anchor/pull/2194)) +* ts: Add ability to resolve missing accounts with a custom resolver ([#2194](https://github.com/coral-xyz/anchor/pull/2194)) ### Fixes diff --git a/tests/pda-derivation/programs/pda-derivation/src/lib.rs b/tests/pda-derivation/programs/pda-derivation/src/lib.rs index b56b039bc3..c42ec29be9 100644 --- a/tests/pda-derivation/programs/pda-derivation/src/lib.rs +++ b/tests/pda-derivation/programs/pda-derivation/src/lib.rs @@ -3,7 +3,7 @@ use anchor_lang::prelude::*; -declare_id!("ATD5DsyrGhh6bV5PigQ5qQ48uYAChk68YBTkUvdqyJZj"); +declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"); pub const MY_SEED: [u8; 2] = *b"hi"; pub const MY_SEED_STR: &str = "hi"; diff --git a/tests/pda-derivation/tests/typescript.spec.ts b/tests/pda-derivation/tests/typescript.spec.ts index 490b49ab37..ce67259cd5 100644 --- a/tests/pda-derivation/tests/typescript.spec.ts +++ b/tests/pda-derivation/tests/typescript.spec.ts @@ -65,4 +65,28 @@ describe("typescript", () => { .data; expect(actualData.toNumber()).is.equal(1337); }); + + it("should allow custom resolvers", async () => { + let called = false; + const customProgram = new Program( + program.idl, + program.programId, + program.provider, + program.coder, + (instruction) => { + if (instruction.name === "initMyAccount") { + return async ({ accounts }) => { + called = true; + return accounts; + } + } + } + ) + await customProgram.methods.initMyAccount(seedA).accounts({ + base: base.publicKey, + base2: base.publicKey, + }).pubkeys(); + + expect(called).is.true; + }) }); diff --git a/ts/packages/anchor/src/program/accounts-resolver.ts b/ts/packages/anchor/src/program/accounts-resolver.ts index 9483891721..3b19b7aedc 100644 --- a/ts/packages/anchor/src/program/accounts-resolver.ts +++ b/ts/packages/anchor/src/program/accounts-resolver.ts @@ -17,6 +17,14 @@ import { BorshAccountsCoder } from "src/coder/index.js"; type Accounts = { [name: string]: PublicKey | Accounts }; +export type CustomAccountResolver = (params: { + args: Array, + accounts: Accounts, + provider: Provider, + programId: PublicKey, + idlIx: AllInstructions, +}) => Promise; + // Populates a given accounts context with PDAs and common missing accounts. export class AccountsResolver> { _args: Array; @@ -35,7 +43,8 @@ export class AccountsResolver> { private _provider: Provider, private _programId: PublicKey, private _idlIx: AllInstructions, - _accountNamespace: AccountNamespace + _accountNamespace: AccountNamespace, + private _customResolver?: CustomAccountResolver ) { this._args = _args; this._accountStore = new AccountStore(_provider, _accountNamespace); @@ -84,11 +93,23 @@ export class AccountsResolver> { } } - // Auto populate pdas until we stop finding new accounts - while ((await this.resolvePdas(this._idlIx.accounts)) > 0) {} - // Auto populate has_one relationships until we stop finding new accounts - while ((await this.resolveRelations(this._idlIx.accounts)) > 0) {} + // Auto populate pdas and relations until we stop finding new accounts + while ( + (await this.resolvePdas(this._idlIx.accounts)) + + (await this.resolveRelations(this._idlIx.accounts)) > + 0 + ) {} + + if (this._customResolver) { + this._accounts = await this._customResolver({ + args: this._args, + accounts: this._accounts, + provider: this._provider, + programId: this._programId, + idlIx: this._idlIx, + }); + } } private get(path: string[]): PublicKey | undefined { diff --git a/ts/packages/anchor/src/program/index.ts b/ts/packages/anchor/src/program/index.ts index 916eeb8275..5a1b054a42 100644 --- a/ts/packages/anchor/src/program/index.ts +++ b/ts/packages/anchor/src/program/index.ts @@ -1,7 +1,7 @@ import { inflate } from "pako"; import { PublicKey } from "@solana/web3.js"; import Provider, { getProvider } from "../provider.js"; -import { Idl, idlAddress, decodeIdlAccount } from "../idl.js"; +import { Idl, idlAddress, decodeIdlAccount, IdlInstruction } from "../idl.js"; import { Coder, BorshCoder } from "../coder/index.js"; import NamespaceFactory, { RpcNamespace, @@ -16,6 +16,7 @@ import NamespaceFactory, { import { utf8 } from "../utils/bytes/index.js"; import { EventManager } from "./event.js"; import { Address, translateAddress } from "./common.js"; +import { CustomAccountResolver } from "./accounts-resolver.js"; export * from "./common.js"; export * from "./context.js"; @@ -263,12 +264,18 @@ export class Program { * @param programId The on-chain address of the program. * @param provider The network and wallet context to use. If not provided * then uses [[getProvider]]. + * @param getCustomResolver A function that returns a custom account resolver + * for the given instruction. This is useful for resolving + * public keys of missing accounts when building instructions */ public constructor( idl: IDL, programId: Address, provider?: Provider, - coder?: Coder + coder?: Coder, + getCustomResolver?: ( + instruction: IdlInstruction + ) => CustomAccountResolver | undefined ) { programId = translateAddress(programId); @@ -293,7 +300,13 @@ export class Program { methods, state, views, - ] = NamespaceFactory.build(idl, this._coder, programId, provider); + ] = NamespaceFactory.build( + idl, + this._coder, + programId, + provider, + getCustomResolver ?? (() => undefined) + ); this.rpc = rpc; this.instruction = instruction; this.transaction = transaction; diff --git a/ts/packages/anchor/src/program/namespace/index.ts b/ts/packages/anchor/src/program/namespace/index.ts index 180719d3cf..48b4a370f1 100644 --- a/ts/packages/anchor/src/program/namespace/index.ts +++ b/ts/packages/anchor/src/program/namespace/index.ts @@ -2,7 +2,7 @@ import camelCase from "camelcase"; import { PublicKey } from "@solana/web3.js"; import { Coder } from "../../coder/index.js"; import Provider from "../../provider.js"; -import { Idl } from "../../idl.js"; +import { Idl, IdlInstruction } from "../../idl.js"; import StateFactory, { StateClient } from "./state.js"; import InstructionFactory, { InstructionNamespace } from "./instruction.js"; import TransactionFactory, { TransactionNamespace } from "./transaction.js"; @@ -12,6 +12,7 @@ import SimulateFactory, { SimulateNamespace } from "./simulate.js"; import { parseIdlErrors } from "../common.js"; import { MethodsBuilderFactory, MethodsNamespace } from "./methods"; import ViewFactory, { ViewNamespace } from "./views"; +import { CustomAccountResolver } from "../accounts-resolver.js"; // Re-exports. export { StateClient } from "./state.js"; @@ -32,7 +33,8 @@ export default class NamespaceFactory { idl: IDL, coder: Coder, programId: PublicKey, - provider: Provider + provider: Provider, + getCustomResolver?: (instruction: IdlInstruction) => (CustomAccountResolver | undefined) ): [ RpcNamespace, InstructionNamespace, @@ -85,7 +87,8 @@ export default class NamespaceFactory { rpcItem, simulateItem, viewItem, - account + account, + getCustomResolver && getCustomResolver(idlIx) ); const name = camelCase(idlIx.name); diff --git a/ts/packages/anchor/src/program/namespace/methods.ts b/ts/packages/anchor/src/program/namespace/methods.ts index f5a656f496..19cc6f578c 100644 --- a/ts/packages/anchor/src/program/namespace/methods.ts +++ b/ts/packages/anchor/src/program/namespace/methods.ts @@ -22,7 +22,7 @@ import { SimulateFn } from "./simulate.js"; import { ViewFn } from "./views.js"; import Provider from "../../provider.js"; import { AccountNamespace } from "./account.js"; -import { AccountsResolver } from "../accounts-resolver.js"; +import { AccountsResolver, CustomAccountResolver } from "../accounts-resolver.js"; import { Accounts } from "../context.js"; export type MethodsNamespace< @@ -40,7 +40,8 @@ export class MethodsBuilderFactory { rpcFn: RpcFn, simulateFn: SimulateFn, viewFn: ViewFn | undefined, - accountNamespace: AccountNamespace + accountNamespace: AccountNamespace, + customResolver?: CustomAccountResolver ): MethodsFn> { return (...args) => new MethodsBuilder( @@ -53,7 +54,8 @@ export class MethodsBuilderFactory { provider, programId, idlIx, - accountNamespace + accountNamespace, + customResolver ); } } @@ -78,7 +80,8 @@ export class MethodsBuilder> { _provider: Provider, _programId: PublicKey, _idlIx: AllInstructions, - _accountNamespace: AccountNamespace + _accountNamespace: AccountNamespace, + _customResolver?: CustomAccountResolver ) { this._args = _args; this._accountsResolver = new AccountsResolver( @@ -87,7 +90,8 @@ export class MethodsBuilder> { _provider, _programId, _idlIx, - _accountNamespace + _accountNamespace, + _customResolver ); } From 060ad0be1d341c6b1cdb7e532a6aa998798e2067 Mon Sep 17 00:00:00 2001 From: Chewing Glass Date: Wed, 21 Sep 2022 11:26:35 -0500 Subject: [PATCH 09/10] Lint fix --- ts/packages/anchor/src/program/accounts-resolver.ts | 11 +++++------ ts/packages/anchor/src/program/namespace/index.ts | 4 +++- ts/packages/anchor/src/program/namespace/methods.ts | 5 ++++- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/ts/packages/anchor/src/program/accounts-resolver.ts b/ts/packages/anchor/src/program/accounts-resolver.ts index 3b19b7aedc..3b159fcdbb 100644 --- a/ts/packages/anchor/src/program/accounts-resolver.ts +++ b/ts/packages/anchor/src/program/accounts-resolver.ts @@ -18,11 +18,11 @@ import { BorshAccountsCoder } from "src/coder/index.js"; type Accounts = { [name: string]: PublicKey | Accounts }; export type CustomAccountResolver = (params: { - args: Array, - accounts: Accounts, - provider: Provider, - programId: PublicKey, - idlIx: AllInstructions, + args: Array; + accounts: Accounts; + provider: Provider; + programId: PublicKey; + idlIx: AllInstructions; }) => Promise; // Populates a given accounts context with PDAs and common missing accounts. @@ -93,7 +93,6 @@ export class AccountsResolver> { } } - // Auto populate pdas and relations until we stop finding new accounts while ( (await this.resolvePdas(this._idlIx.accounts)) + diff --git a/ts/packages/anchor/src/program/namespace/index.ts b/ts/packages/anchor/src/program/namespace/index.ts index 48b4a370f1..ec05fbb0a4 100644 --- a/ts/packages/anchor/src/program/namespace/index.ts +++ b/ts/packages/anchor/src/program/namespace/index.ts @@ -34,7 +34,9 @@ export default class NamespaceFactory { coder: Coder, programId: PublicKey, provider: Provider, - getCustomResolver?: (instruction: IdlInstruction) => (CustomAccountResolver | undefined) + getCustomResolver?: ( + instruction: IdlInstruction + ) => CustomAccountResolver | undefined ): [ RpcNamespace, InstructionNamespace, diff --git a/ts/packages/anchor/src/program/namespace/methods.ts b/ts/packages/anchor/src/program/namespace/methods.ts index 19cc6f578c..3b300cd840 100644 --- a/ts/packages/anchor/src/program/namespace/methods.ts +++ b/ts/packages/anchor/src/program/namespace/methods.ts @@ -22,7 +22,10 @@ import { SimulateFn } from "./simulate.js"; import { ViewFn } from "./views.js"; import Provider from "../../provider.js"; import { AccountNamespace } from "./account.js"; -import { AccountsResolver, CustomAccountResolver } from "../accounts-resolver.js"; +import { + AccountsResolver, + CustomAccountResolver, +} from "../accounts-resolver.js"; import { Accounts } from "../context.js"; export type MethodsNamespace< From 6422270ff488fd6a4e03800632743c2da0d43b46 Mon Sep 17 00:00:00 2001 From: Chewing Glass Date: Wed, 21 Sep 2022 11:35:35 -0500 Subject: [PATCH 10/10] Pretty --- CHANGELOG.md | 2 +- tests/pda-derivation/tests/typescript.spec.ts | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a4e68c3b7..56ec3eeda7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,7 +21,7 @@ The minor version will be incremented upon a breaking change and the patch versi * lang: Account closing reassigns to system program and reallocates ([#2169](https://github.com/coral-xyz/anchor/pull/2169)). * ts: Add coders for SPL programs ([#2143](https://github.com/coral-xyz/anchor/pull/2143)). * ts: Add `has_one` relations inference so accounts mapped via has_one relationships no longer need to be provided ([#2160](https://github.com/coral-xyz/anchor/pull/2160)) -* ts: Add ability to set args after setting accounts and retriving pubkyes ([#2160](https://github.com/coral-xyz/anchor/pull/2160)) +* ts: Add ability to set args after setting accounts and retrieving pubkyes ([#2160](https://github.com/coral-xyz/anchor/pull/2160)) * ts: Add `.prepare()` to builder pattern ([#2160](https://github.com/coral-xyz/anchor/pull/2160)) * spl: Add `freeze_delegated_account` and `thaw_delegated_account` wrappers ([#2164](https://github.com/coral-xyz/anchor/pull/2164)) * ts: Add nested PDA inference ([#2194](https://github.com/coral-xyz/anchor/pull/2194)) diff --git a/tests/pda-derivation/tests/typescript.spec.ts b/tests/pda-derivation/tests/typescript.spec.ts index ce67259cd5..65d00df94e 100644 --- a/tests/pda-derivation/tests/typescript.spec.ts +++ b/tests/pda-derivation/tests/typescript.spec.ts @@ -78,15 +78,18 @@ describe("typescript", () => { return async ({ accounts }) => { called = true; return accounts; - } + }; } } - ) - await customProgram.methods.initMyAccount(seedA).accounts({ - base: base.publicKey, - base2: base.publicKey, - }).pubkeys(); + ); + await customProgram.methods + .initMyAccount(seedA) + .accounts({ + base: base.publicKey, + base2: base.publicKey, + }) + .pubkeys(); expect(called).is.true; - }) + }); });