diff --git a/CHANGELOG.md b/CHANGELOG.md index 23c343c4de..6d32257a48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,14 @@ incremented for features. * lang: Add new `AccountSysvarMismatch` error code and test cases for sysvars ([#1535](https://github.com/project-serum/anchor/pull/1535)). +### Fixes + +* ts: Fix the loss of strict typing using the `methods` namespace on builder functions ([#1539](https://github.com/project-serum/anchor/pull/1539)). + +### Breaking + +* ts: Mark `transaction`, `instruction`, `simulate` and `rpc` program namespaces as deprecated in favor of `methods` ([#1539](https://github.com/project-serum/anchor/pull/1539)). + ## [0.22.1] - 2022-02-28 ### Fixes diff --git a/ts/src/program/index.ts b/ts/src/program/index.ts index 7971fa396a..04c6bca041 100644 --- a/ts/src/program/index.ts +++ b/ts/src/program/index.ts @@ -77,6 +77,7 @@ export class Program { * }, * }); * ``` + * @deprecated */ readonly rpc: RpcNamespace; @@ -130,6 +131,7 @@ export class Program { * }, * }); * ``` + * @deprecated */ readonly instruction: InstructionNamespace; @@ -161,6 +163,7 @@ export class Program { * }, * }); * ``` + * @deprecated */ readonly transaction: TransactionNamespace; @@ -197,6 +200,7 @@ export class Program { * }, * }); * ``` + * @deprecated */ readonly simulate: SimulateNamespace; diff --git a/ts/src/program/namespace/index.ts b/ts/src/program/namespace/index.ts index f7e994dfd7..e84695cfff 100644 --- a/ts/src/program/namespace/index.ts +++ b/ts/src/program/namespace/index.ts @@ -10,7 +10,6 @@ import RpcFactory, { RpcNamespace } from "./rpc.js"; import AccountFactory, { AccountNamespace } from "./account.js"; import SimulateFactory, { SimulateNamespace } from "./simulate.js"; import { parseIdlErrors } from "../common.js"; -import { AllInstructions } from "./types.js"; import { MethodsBuilderFactory, MethodsNamespace } from "./methods"; // Re-exports. @@ -55,8 +54,8 @@ export default class NamespaceFactory { const state = StateFactory.build(idl, coder, programId, provider); - idl.instructions.forEach(>(idlIx: I) => { - const ixItem = InstructionFactory.build( + idl.instructions.forEach((idlIx) => { + const ixItem = InstructionFactory.build( idlIx, (ixName, ix) => coder.instruction.encode(ixName, ix), programId @@ -72,7 +71,7 @@ export default class NamespaceFactory { programId, idl ); - const methodItem = MethodsBuilderFactory.build( + const methodItem = MethodsBuilderFactory.build( provider, programId, idlIx, diff --git a/ts/src/program/namespace/methods.ts b/ts/src/program/namespace/methods.ts index bf28812acc..ceba472603 100644 --- a/ts/src/program/namespace/methods.ts +++ b/ts/src/program/namespace/methods.ts @@ -17,6 +17,7 @@ import { SimulateFn } from "./simulate.js"; import Provider from "../../provider.js"; import { AccountNamespace } from "./account.js"; import { AccountsResolver } from "../accounts-resolver.js"; +import { Accounts } from "../context.js"; export type MethodsNamespace< IDL extends Idl = Idl, @@ -33,9 +34,9 @@ export class MethodsBuilderFactory { rpcFn: RpcFn, simulateFn: SimulateFn, accountNamespace: AccountNamespace - ): MethodsFn { - const request: MethodsFn = (...args) => { - return new MethodsBuilder( + ): MethodsFn> { + return (...args) => + new MethodsBuilder( args, ixFn, txFn, @@ -46,13 +47,11 @@ export class MethodsBuilderFactory { idlIx, accountNamespace ); - }; - return request; } } export class MethodsBuilder> { - readonly _accounts: { [name: string]: PublicKey } = {}; + private readonly _accounts: { [name: string]: PublicKey } = {}; private _remainingAccounts: Array = []; private _signers: Array = []; private _preInstructions: Array = []; @@ -80,8 +79,9 @@ export class MethodsBuilder> { ); } - // TODO: don't use any. - public accounts(accounts: any): MethodsBuilder { + public accounts( + accounts: Accounts + ): MethodsBuilder { Object.assign(this._accounts, accounts); return this; } @@ -112,7 +112,7 @@ export class MethodsBuilder> { return this; } - public async rpc(options: ConfirmOptions): Promise { + public async rpc(options?: ConfirmOptions): Promise { await this._accountsResolver.resolve(); // @ts-ignore return this._rpcFn(...this._args, { @@ -126,7 +126,7 @@ export class MethodsBuilder> { } public async simulate( - options: ConfirmOptions + options?: ConfirmOptions ): Promise> { await this._accountsResolver.resolve(); // @ts-ignore diff --git a/ts/src/program/namespace/types.ts b/ts/src/program/namespace/types.ts index 1fe703a651..337adde69b 100644 --- a/ts/src/program/namespace/types.ts +++ b/ts/src/program/namespace/types.ts @@ -10,6 +10,7 @@ import { IdlTypeDefTyStruct, } from "../../idl"; import { Accounts, Context } from "../context"; +import { MethodsBuilder } from "./methods"; /** * All instructions for an IDL. @@ -66,7 +67,11 @@ export type MakeInstructionsNamespace< }; export type MakeMethodsNamespace = { - [M in keyof InstructionMap]: MethodsFn[M], any>; + [M in keyof InstructionMap]: MethodsFn< + IDL, + InstructionMap[M], + MethodsBuilder[M]> + >; }; export type InstructionContextFn<