From 8ea5794b4e8455dc32d3578c2f86bc0d7b827b8d Mon Sep 17 00:00:00 2001 From: Steven Luscher Date: Wed, 17 Jul 2024 13:06:59 -0700 Subject: [PATCH] fix: apply default `memcmp` encoding (`base58`) when not supplied (#2945) --- packages/library-legacy/src/connection.ts | 47 +++++++++++++++++-- .../library-legacy/test/connection.test.ts | 12 ++++- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/packages/library-legacy/src/connection.ts b/packages/library-legacy/src/connection.ts index a0fcd2e033d5..1f97a19322dd 100644 --- a/packages/library-legacy/src/connection.ts +++ b/packages/library-legacy/src/connection.ts @@ -381,6 +381,25 @@ function extractCommitmentFromConfig( return {commitment, config}; } +/** + * @internal + */ +function applyDefaultMemcmpEncodingToFilters( + filters: GetProgramAccountsFilter[], +): GetProgramAccountsFilter[] { + return filters.map(filter => + 'memcmp' in filter + ? { + ...filter, + memcmp: { + ...filter.memcmp, + encoding: filter.memcmp.encoding ?? 'base58', + }, + } + : filter, + ); +} + /** * @internal */ @@ -2697,9 +2716,18 @@ export type MemcmpFilter = { memcmp: { /** offset into program account data to start comparison */ offset: number; - /** data to match, as base-58 encoded string and limited to less than 129 bytes */ - bytes: string; - }; + } & ( + | { + encoding?: 'base58'; // Base-58 is the default when not supplied. + /** data to match, as base-58 encoded string and limited to less than 129 bytes */ + bytes: string; + } + | { + encoding: 'base64'; + /** data to match, as base-64 encoded string */ + bytes: string; + } + ); }; /** @@ -3731,7 +3759,16 @@ export class Connection { [programId.toBase58()], commitment, encoding || 'base64', - configWithoutEncoding, + { + ...configWithoutEncoding, + ...(configWithoutEncoding.filters + ? { + filters: applyDefaultMemcmpEncodingToFilters( + configWithoutEncoding.filters, + ), + } + : null), + }, ); const unsafeRes = await this._rpcRequest('getProgramAccounts', args); const baseSchema = array(KeyedAccountInfoResult); @@ -6521,7 +6558,7 @@ export class Connection { config ? config : maybeFilters - ? {filters: maybeFilters} + ? {filters: applyDefaultMemcmpEncodingToFilters(maybeFilters)} : undefined /* extra */, ); return this._makeSubscription( diff --git a/packages/library-legacy/test/connection.test.ts b/packages/library-legacy/test/connection.test.ts index 84b90efadf1d..d125a74e8a38 100644 --- a/packages/library-legacy/test/connection.test.ts +++ b/packages/library-legacy/test/connection.test.ts @@ -595,6 +595,7 @@ describe('Connection', function () { filters: [ { memcmp: { + encoding: 'base58', offset: 0, bytes: 'XzdZ3w', }, @@ -713,6 +714,7 @@ describe('Connection', function () { filters: [ { memcmp: { + encoding: 'base58', offset: 0, bytes: '', }, @@ -6473,7 +6475,7 @@ describe('Connection', function () { PublicKey.default, mockCallback, /* commitment */ undefined, - /* filters */ [{dataSize: 123}], + /* filters */ [{dataSize: 123}, {memcmp: {bytes: 'AAA', offset: 1}}], ); expect(rpcRequestMethod).to.have.been.calledWithExactly( { @@ -6481,7 +6483,13 @@ describe('Connection', function () { method: 'programSubscribe', unsubscribeMethod: 'programUnsubscribe', }, - [match.any, match.has('filters', [{dataSize: 123}])], + [ + match.any, + match.has('filters', [ + {dataSize: 123}, + {memcmp: {encoding: 'base58', bytes: 'AAA', offset: 1}}, + ]), + ], ); }); it('passes the commitment/encoding/filters to the RPC when calling `onProgramAccountChange`', () => {