Skip to content

Commit 8ea5794

Browse files
authored
fix: apply default memcmp encoding (base58) when not supplied (#2945)
1 parent 98d6c6e commit 8ea5794

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

packages/library-legacy/src/connection.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,25 @@ function extractCommitmentFromConfig<TConfig>(
381381
return {commitment, config};
382382
}
383383

384+
/**
385+
* @internal
386+
*/
387+
function applyDefaultMemcmpEncodingToFilters(
388+
filters: GetProgramAccountsFilter[],
389+
): GetProgramAccountsFilter[] {
390+
return filters.map(filter =>
391+
'memcmp' in filter
392+
? {
393+
...filter,
394+
memcmp: {
395+
...filter.memcmp,
396+
encoding: filter.memcmp.encoding ?? 'base58',
397+
},
398+
}
399+
: filter,
400+
);
401+
}
402+
384403
/**
385404
* @internal
386405
*/
@@ -2697,9 +2716,18 @@ export type MemcmpFilter = {
26972716
memcmp: {
26982717
/** offset into program account data to start comparison */
26992718
offset: number;
2700-
/** data to match, as base-58 encoded string and limited to less than 129 bytes */
2701-
bytes: string;
2702-
};
2719+
} & (
2720+
| {
2721+
encoding?: 'base58'; // Base-58 is the default when not supplied.
2722+
/** data to match, as base-58 encoded string and limited to less than 129 bytes */
2723+
bytes: string;
2724+
}
2725+
| {
2726+
encoding: 'base64';
2727+
/** data to match, as base-64 encoded string */
2728+
bytes: string;
2729+
}
2730+
);
27032731
};
27042732

27052733
/**
@@ -3731,7 +3759,16 @@ export class Connection {
37313759
[programId.toBase58()],
37323760
commitment,
37333761
encoding || 'base64',
3734-
configWithoutEncoding,
3762+
{
3763+
...configWithoutEncoding,
3764+
...(configWithoutEncoding.filters
3765+
? {
3766+
filters: applyDefaultMemcmpEncodingToFilters(
3767+
configWithoutEncoding.filters,
3768+
),
3769+
}
3770+
: null),
3771+
},
37353772
);
37363773
const unsafeRes = await this._rpcRequest('getProgramAccounts', args);
37373774
const baseSchema = array(KeyedAccountInfoResult);
@@ -6521,7 +6558,7 @@ export class Connection {
65216558
config
65226559
? config
65236560
: maybeFilters
6524-
? {filters: maybeFilters}
6561+
? {filters: applyDefaultMemcmpEncodingToFilters(maybeFilters)}
65256562
: undefined /* extra */,
65266563
);
65276564
return this._makeSubscription(

packages/library-legacy/test/connection.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ describe('Connection', function () {
595595
filters: [
596596
{
597597
memcmp: {
598+
encoding: 'base58',
598599
offset: 0,
599600
bytes: 'XzdZ3w',
600601
},
@@ -713,6 +714,7 @@ describe('Connection', function () {
713714
filters: [
714715
{
715716
memcmp: {
717+
encoding: 'base58',
716718
offset: 0,
717719
bytes: '',
718720
},
@@ -6473,15 +6475,21 @@ describe('Connection', function () {
64736475
PublicKey.default,
64746476
mockCallback,
64756477
/* commitment */ undefined,
6476-
/* filters */ [{dataSize: 123}],
6478+
/* filters */ [{dataSize: 123}, {memcmp: {bytes: 'AAA', offset: 1}}],
64776479
);
64786480
expect(rpcRequestMethod).to.have.been.calledWithExactly(
64796481
{
64806482
callback: mockCallback,
64816483
method: 'programSubscribe',
64826484
unsubscribeMethod: 'programUnsubscribe',
64836485
},
6484-
[match.any, match.has('filters', [{dataSize: 123}])],
6486+
[
6487+
match.any,
6488+
match.has('filters', [
6489+
{dataSize: 123},
6490+
{memcmp: {encoding: 'base58', bytes: 'AAA', offset: 1}},
6491+
]),
6492+
],
64856493
);
64866494
});
64876495
it('passes the commitment/encoding/filters to the RPC when calling `onProgramAccountChange`', () => {

0 commit comments

Comments
 (0)