소스 검색

ts: add getAccountInfo helper method to account namespace/client (#1084)

Dominic Lee 3 년 전
부모
커밋
bef1bd8b80
2개의 변경된 파일14개의 추가작업 그리고 3개의 파일을 삭제
  1. 2 0
      CHANGELOG.md
  2. 12 3
      ts/src/program/namespace/account.ts

+ 2 - 0
CHANGELOG.md

@@ -21,9 +21,11 @@ incremented for features.
 
 * lang: Add `ErrorCode::AccountNotInitialized` error to separate the situation when the account has the wrong owner from when it does not exist (#[1024](https://github.com/project-serum/anchor/pull/1024))
 * lang: Called instructions now log their name by default. This can be turned off with the `no-log-ix-name` flag ([#1057](https://github.com/project-serum/anchor/pull/1057))
+* ts: Add `getAccountInfo` helper method to account namespace/client ([#1084](https://github.com/project-serum/anchor/pull/1084))
 * lang: `ProgramData` and `UpgradableLoaderState` can now be passed into `Account` as generics. see [UpgradeableLoaderState](https://docs.rs/solana-program/latest/solana_program/bpf_loader_upgradeable/enum.UpgradeableLoaderState.html). `UpgradableLoaderState` can also be matched on to get `ProgramData`, but when `ProgramData` is used instead, anchor does the serialization and checking that it is actually program data for you  ([#1095](https://github.com/project-serum/anchor/pull/1095))
 * ts: Add better error msgs in the ts client if something wrong (i.e. not a pubkey or a string) is passed in as an account in an instruction accounts object ([#1098](https://github.com/project-serum/anchor/pull/1098))
 
+
 ## [0.18.2] - 2021-11-14
 
 * cli: Replace global JavaScript dependency installs with local.

+ 12 - 3
ts/src/program/namespace/account.ts

@@ -9,6 +9,7 @@ import {
   TransactionInstruction,
   Commitment,
   GetProgramAccountsFilter,
+  AccountInfo,
 } from "@solana/web3.js";
 import Provider, { getProvider } from "../../provider.js";
 import { Idl, IdlTypeDef } from "../../idl.js";
@@ -136,9 +137,7 @@ export class AccountClient<
    * @param address The address of the account to fetch.
    */
   async fetchNullable(address: Address): Promise<T | null> {
-    const accountInfo = await this._provider.connection.getAccountInfo(
-      translateAddress(address)
-    );
+    const accountInfo = await this.getAccountInfo(address);
     if (accountInfo === null) {
       return null;
     }
@@ -344,6 +343,16 @@ export class AccountClient<
   ): Promise<PublicKey> {
     return await pubkeyUtil.associated(this._programId, ...args);
   }
+
+  async getAccountInfo(
+    address: Address,
+    commitment?: Commitment
+  ): Promise<AccountInfo<Buffer> | null> {
+    return await this._provider.connection.getAccountInfo(
+      translateAddress(address),
+      commitment
+    );
+  }
 }
 
 /**