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 accountsStrict for non-resolvable accounts input #2019

Merged
merged 3 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ com/project-serum/anchor/pull/1841)).
* client: Add send_with_spinner_and_config function to RequestBuilder ([#1926](https://github.com/coral-xyz/anchor/pull/1926)).
* ts: Implement a coder for SPL associated token program ([#1939](https://github.com/coral-xyz/anchor/pull/1939)).
* ts: verbose error for missing `ANCHOR_WALLET` variable when using `NodeWallet.local()` ([#1958](https://github.com/coral-xyz/anchor/pull/1958)).
* ts: Add `MethodsBuilder#accountsStrict` for strict typing on ix account input ([#2019](https://github.com/coral-xyz/anchor/pull/2019)).

### Fixes

Expand Down
4 changes: 2 additions & 2 deletions ts/src/program/accounts-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import { coder } from "../spl/token";
// Populates a given accounts context with PDAs and common missing accounts.
export class AccountsResolver<IDL extends Idl, I extends AllInstructions<IDL>> {
static readonly CONST_ACCOUNTS = {
systemProgram: SystemProgram.programId,
tokenProgram: TOKEN_PROGRAM_ID,
associatedTokenProgram: ASSOCIATED_PROGRAM_ID,
rent: SYSVAR_RENT_PUBKEY,
systemProgram: SystemProgram.programId,
tokenProgram: TOKEN_PROGRAM_ID,
};

private _accountStore: AccountStore<IDL>;
Expand Down
40 changes: 34 additions & 6 deletions ts/src/program/namespace/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class MethodsBuilder<IDL extends Idl, I extends AllInstructions<IDL>> {
private _preInstructions: Array<TransactionInstruction> = [];
private _postInstructions: Array<TransactionInstruction> = [];
private _accountsResolver: AccountsResolver<IDL, I>;
private _autoResolveAccounts: boolean = true;

constructor(
private _args: Array<any>,
Expand All @@ -91,13 +92,24 @@ export class MethodsBuilder<IDL extends Idl, I extends AllInstructions<IDL>> {
public async pubkeys(): Promise<
Partial<InstructionAccountAddresses<IDL, I>>
> {
await this._accountsResolver.resolve();
if (this._autoResolveAccounts) {
await this._accountsResolver.resolve();
}
return this._accounts as Partial<InstructionAccountAddresses<IDL, I>>;
}

public accounts(
accounts: Partial<Accounts<I["accounts"][number]>>
): MethodsBuilder<IDL, I> {
this._autoResolveAccounts = true;
Object.assign(this._accounts, accounts);
return this;
}

public accountsStrict(
accounts: Accounts<I["accounts"][number]>
): MethodsBuilder<IDL, I> {
this._autoResolveAccounts = false;
Object.assign(this._accounts, accounts);
return this;
}
Expand Down Expand Up @@ -129,7 +141,10 @@ export class MethodsBuilder<IDL extends Idl, I extends AllInstructions<IDL>> {
}

public async rpc(options?: ConfirmOptions): Promise<TransactionSignature> {
await this._accountsResolver.resolve();
if (this._autoResolveAccounts) {
await this._accountsResolver.resolve();
}

// @ts-ignore
return this._rpcFn(...this._args, {
accounts: this._accounts,
Expand All @@ -142,10 +157,14 @@ export class MethodsBuilder<IDL extends Idl, I extends AllInstructions<IDL>> {
}

public async view(options?: ConfirmOptions): Promise<any> {
await this._accountsResolver.resolve();
if (this._autoResolveAccounts) {
await this._accountsResolver.resolve();
}

if (!this._viewFn) {
throw new Error("Method does not support views");
}

// @ts-ignore
return this._viewFn(...this._args, {
accounts: this._accounts,
Expand All @@ -160,7 +179,10 @@ export class MethodsBuilder<IDL extends Idl, I extends AllInstructions<IDL>> {
public async simulate(
options?: ConfirmOptions
): Promise<SimulateResponse<any, any>> {
await this._accountsResolver.resolve();
if (this._autoResolveAccounts) {
await this._accountsResolver.resolve();
}

// @ts-ignore
return this._simulateFn(...this._args, {
accounts: this._accounts,
Expand All @@ -173,7 +195,10 @@ export class MethodsBuilder<IDL extends Idl, I extends AllInstructions<IDL>> {
}

public async instruction(): Promise<TransactionInstruction> {
await this._accountsResolver.resolve();
if (this._autoResolveAccounts) {
await this._accountsResolver.resolve();
}

// @ts-ignore
return this._ixFn(...this._args, {
accounts: this._accounts,
Expand All @@ -185,7 +210,10 @@ export class MethodsBuilder<IDL extends Idl, I extends AllInstructions<IDL>> {
}

public async transaction(): Promise<Transaction> {
await this._accountsResolver.resolve();
if (this._autoResolveAccounts) {
await this._accountsResolver.resolve();
}

// @ts-ignore
return this._txFn(...this._args, {
accounts: this._accounts,
Expand Down