Kaynağa Gözat

ts: Export more TypeScript types (#753)

Ian Macalinao 4 yıl önce
ebeveyn
işleme
9426918c4b

+ 1 - 19
ts/src/index.ts

@@ -20,22 +20,4 @@ export { Instruction } from "./coder/instruction";
 export { Idl } from "./idl";
 export { default as workspace } from "./workspace";
 export * as utils from "./utils";
-export { Program } from "./program";
-export { Address } from "./program/common";
-export { Event } from "./program/event";
-export {
-  ProgramAccount,
-  AccountNamespace,
-  AccountClient,
-  StateClient,
-  RpcNamespace,
-  RpcFn,
-  SimulateNamespace,
-  SimulateFn,
-  TransactionNamespace,
-  TransactionFn,
-  InstructionNamespace,
-  InstructionFn,
-} from "./program/namespace";
-export { Context, Accounts } from "./program/context";
-export { EventParser } from "./program/event";
+export * from "./program";

+ 2 - 2
ts/src/program/context.ts

@@ -10,11 +10,11 @@ import { IdlInstruction } from "../idl";
 /**
  * Context provides all non-argument inputs for generating Anchor transactions.
  */
-export type Context = {
+export type Context<A extends Accounts = Accounts> = {
   /**
    * Accounts used in the instruction context.
    */
-  accounts?: Accounts;
+  accounts?: A;
 
   /**
    * All accounts to pass into an instruction *after* the main `accounts`.

+ 5 - 0
ts/src/program/index.ts

@@ -16,6 +16,11 @@ import { utf8 } from "../utils/bytes";
 import { EventManager } from "./event";
 import { Address, translateAddress } from "./common";
 
+export * from "./common";
+export * from "./context";
+export * from "./event";
+export * from "./namespace";
+
 /**
  * ## Program
  *

+ 19 - 6
ts/src/program/namespace/account.ts

@@ -67,7 +67,7 @@ export interface AccountNamespace {
   [key: string]: AccountClient;
 }
 
-export class AccountClient {
+export class AccountClient<T = any> {
   /**
    * Returns the number of bytes in this account.
    */
@@ -118,16 +118,16 @@ export class AccountClient {
   }
 
   /**
-   * Returns a deserialized account.
+   * Returns a deserialized account, returning null if it doesn't exist.
    *
    * @param address The address of the account to fetch.
    */
-  async fetch(address: Address): Promise<Object> {
+  async fetchNullable(address: Address): Promise<T | null> {
     const accountInfo = await this._provider.connection.getAccountInfo(
       translateAddress(address)
     );
     if (accountInfo === null) {
-      throw new Error(`Account does not exist ${address.toString()}`);
+      return null;
     }
 
     // Assert the account discriminator is correct.
@@ -139,10 +139,23 @@ export class AccountClient {
     return this._coder.accounts.decode(this._idlAccount.name, accountInfo.data);
   }
 
+  /**
+   * Returns a deserialized account.
+   *
+   * @param address The address of the account to fetch.
+   */
+  async fetch(address: Address): Promise<T> {
+    const data = await this.fetchNullable(address);
+    if (data === null) {
+      throw new Error(`Account does not exist ${address.toString()}`);
+    }
+    return data;
+  }
+
   /**
    * Returns all instances of this account type for the program.
    */
-  async all(filter?: Buffer): Promise<ProgramAccount<any>[]> {
+  async all(filter?: Buffer): Promise<ProgramAccount<T>[]> {
     let bytes = await accountDiscriminator(this._idlAccount.name);
     if (filter !== undefined) {
       bytes = Buffer.concat([bytes, filter]);
@@ -250,7 +263,7 @@ export class AccountClient {
    * Function returning the associated account. Args are keys to associate.
    * Order matters.
    */
-  async associated(...args: Array<PublicKey | Buffer>): Promise<any> {
+  async associated(...args: Array<PublicKey | Buffer>): Promise<T> {
     const addr = await this.associatedAddress(...args);
     return await this.fetch(addr);
   }