ts: Export more TypeScript types (#753)

This commit is contained in:
Ian Macalinao 2021-09-19 16:11:24 -07:00 committed by GitHub
parent 0f4142e97c
commit 9426918c4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 27 deletions

View File

@ -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";

View File

@ -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`.

View File

@ -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
*

View File

@ -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);
}