Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

fix: apply default memcmp encoding (base58) when not supplied #2945

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions packages/library-legacy/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,25 @@ function extractCommitmentFromConfig<TConfig>(
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
*/
Expand Down Expand Up @@ -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;
}
);
};

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -6521,7 +6558,7 @@ export class Connection {
config
? config
: maybeFilters
? {filters: maybeFilters}
? {filters: applyDefaultMemcmpEncodingToFilters(maybeFilters)}
: undefined /* extra */,
);
return this._makeSubscription(
Expand Down
12 changes: 10 additions & 2 deletions packages/library-legacy/test/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ describe('Connection', function () {
filters: [
{
memcmp: {
encoding: 'base58',
offset: 0,
bytes: 'XzdZ3w',
},
Expand Down Expand Up @@ -713,6 +714,7 @@ describe('Connection', function () {
filters: [
{
memcmp: {
encoding: 'base58',
offset: 0,
bytes: '',
},
Expand Down Expand Up @@ -6473,15 +6475,21 @@ 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(
{
callback: mockCallback,
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`', () => {
Expand Down