123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- import {
- ConfirmOptions,
- AccountMeta,
- Signer,
- Transaction,
- TransactionInstruction,
- TransactionSignature,
- PublicKey,
- } from "@solana/web3.js";
- import { SimulateResponse } from "./simulate.js";
- import { TransactionFn } from "./transaction.js";
- import { Idl } from "../../idl.js";
- import { AllInstructions, MethodsFn, MakeMethodsNamespace } from "./types.js";
- import { InstructionFn } from "./instruction.js";
- import { RpcFn } from "./rpc.js";
- 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 { Accounts } from "../context.js";
- export type MethodsNamespace<
- IDL extends Idl = Idl,
- I extends AllInstructions<IDL> = AllInstructions<IDL>
- > = MakeMethodsNamespace<IDL, I>;
- export class MethodsBuilderFactory {
- public static build<IDL extends Idl, I extends AllInstructions<IDL>>(
- provider: Provider,
- programId: PublicKey,
- idlIx: AllInstructions<IDL>,
- ixFn: InstructionFn<IDL>,
- txFn: TransactionFn<IDL>,
- rpcFn: RpcFn<IDL>,
- simulateFn: SimulateFn<IDL>,
- viewFn: ViewFn<IDL> | undefined,
- accountNamespace: AccountNamespace<IDL>
- ): MethodsFn<IDL, I, MethodsBuilder<IDL, I>> {
- return (...args) =>
- new MethodsBuilder(
- args,
- ixFn,
- txFn,
- rpcFn,
- simulateFn,
- viewFn,
- provider,
- programId,
- idlIx,
- accountNamespace
- );
- }
- }
- export class MethodsBuilder<IDL extends Idl, I extends AllInstructions<IDL>> {
- private readonly _accounts: { [name: string]: PublicKey } = {};
- private _remainingAccounts: Array<AccountMeta> = [];
- private _signers: Array<Signer> = [];
- private _preInstructions: Array<TransactionInstruction> = [];
- private _postInstructions: Array<TransactionInstruction> = [];
- private _accountsResolver: AccountsResolver<IDL, I>;
- constructor(
- private _args: Array<any>,
- private _ixFn: InstructionFn<IDL>,
- private _txFn: TransactionFn<IDL>,
- private _rpcFn: RpcFn<IDL>,
- private _simulateFn: SimulateFn<IDL>,
- private _viewFn: ViewFn<IDL> | undefined,
- _provider: Provider,
- _programId: PublicKey,
- _idlIx: AllInstructions<IDL>,
- _accountNamespace: AccountNamespace<IDL>
- ) {
- this._accountsResolver = new AccountsResolver(
- _args,
- this._accounts,
- _provider,
- _programId,
- _idlIx,
- _accountNamespace
- );
- }
- public accounts(
- accounts: Partial<Accounts<I["accounts"][number]>>
- ): MethodsBuilder<IDL, I> {
- Object.assign(this._accounts, accounts);
- return this;
- }
- public signers(signers: Array<Signer>): MethodsBuilder<IDL, I> {
- this._signers = this._signers.concat(signers);
- return this;
- }
- public remainingAccounts(
- accounts: Array<AccountMeta>
- ): MethodsBuilder<IDL, I> {
- this._remainingAccounts = this._remainingAccounts.concat(accounts);
- return this;
- }
- public preInstructions(
- ixs: Array<TransactionInstruction>
- ): MethodsBuilder<IDL, I> {
- this._preInstructions = this._preInstructions.concat(ixs);
- return this;
- }
- public postInstructions(
- ixs: Array<TransactionInstruction>
- ): MethodsBuilder<IDL, I> {
- this._postInstructions = this._postInstructions.concat(ixs);
- return this;
- }
- public async rpc(options?: ConfirmOptions): Promise<TransactionSignature> {
- await this._accountsResolver.resolve();
- // @ts-ignore
- return this._rpcFn(...this._args, {
- accounts: this._accounts,
- signers: this._signers,
- remainingAccounts: this._remainingAccounts,
- preInstructions: this._preInstructions,
- postInstructions: this._postInstructions,
- options: options,
- });
- }
- public async view(options?: ConfirmOptions): Promise<any> {
- 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,
- signers: this._signers,
- remainingAccounts: this._remainingAccounts,
- preInstructions: this._preInstructions,
- postInstructions: this._postInstructions,
- options: options,
- });
- }
- public async simulate(
- options?: ConfirmOptions
- ): Promise<SimulateResponse<any, any>> {
- await this._accountsResolver.resolve();
- // @ts-ignore
- return this._simulateFn(...this._args, {
- accounts: this._accounts,
- signers: this._signers,
- remainingAccounts: this._remainingAccounts,
- preInstructions: this._preInstructions,
- postInstructions: this._postInstructions,
- options: options,
- });
- }
- public async instruction(): Promise<TransactionInstruction> {
- await this._accountsResolver.resolve();
- // @ts-ignore
- return this._ixFn(...this._args, {
- accounts: this._accounts,
- signers: this._signers,
- remainingAccounts: this._remainingAccounts,
- preInstructions: this._preInstructions,
- postInstructions: this._postInstructions,
- });
- }
- public async transaction(): Promise<Transaction> {
- await this._accountsResolver.resolve();
- // @ts-ignore
- return this._txFn(...this._args, {
- accounts: this._accounts,
- signers: this._signers,
- remainingAccounts: this._remainingAccounts,
- preInstructions: this._preInstructions,
- postInstructions: this._postInstructions,
- });
- }
- }
|