Browse Source

ts: fix fetching all accounts when using a custom coder (#2107)

acheron 3 years ago
parent
commit
9457180a0a
1 changed files with 16 additions and 9 deletions
  1. 16 9
      ts/src/program/namespace/account.ts

+ 16 - 9
ts/src/program/namespace/account.ts

@@ -201,21 +201,28 @@ export class AccountClient<
   async all(
   async all(
     filters?: Buffer | GetProgramAccountsFilter[]
     filters?: Buffer | GetProgramAccountsFilter[]
   ): Promise<ProgramAccount<T>[]> {
   ): Promise<ProgramAccount<T>[]> {
+    const filter: { offset?: number; bytes?: string; dataSize?: number } =
+      this.coder.accounts.memcmp(
+        this._idlAccount.name,
+        filters instanceof Buffer ? filters : undefined
+      );
+    const coderFilters: GetProgramAccountsFilter[] = [];
+    if (filter?.offset != undefined && filter?.bytes != undefined) {
+      coderFilters.push({
+        memcmp: { offset: filter.offset, bytes: filter.bytes },
+      });
+    }
+    if (filter?.dataSize != undefined) {
+      coderFilters.push({ dataSize: filter.dataSize });
+    }
     let resp = await this._provider.connection.getProgramAccounts(
     let resp = await this._provider.connection.getProgramAccounts(
       this._programId,
       this._programId,
       {
       {
         commitment: this._provider.connection.commitment,
         commitment: this._provider.connection.commitment,
-        filters: [
-          {
-            memcmp: this.coder.accounts.memcmp(
-              this._idlAccount.name,
-              filters instanceof Buffer ? filters : undefined
-            ),
-          },
-          ...(Array.isArray(filters) ? filters : []),
-        ],
+        filters: [...coderFilters, ...(Array.isArray(filters) ? filters : [])],
       }
       }
     );
     );
+
     return resp.map(({ pubkey, account }) => {
     return resp.map(({ pubkey, account }) => {
       return {
       return {
         publicKey: pubkey,
         publicKey: pubkey,