Skip to content

Commit 056ac7a

Browse files
authored
add conditional decoding for ConstituentMap (#2022)
* add conditional decoding * update to config var * revert * revert bun lock changes * revert bun.lock changes
1 parent d06f9a9 commit 056ac7a

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

sdk/bun.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1400,4 +1400,4 @@
14001400

14011401
"@solana/spl-token-metadata/@solana/codecs/@solana/options/@solana/errors/commander": ["[email protected]", "", {}, "sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA=="],
14021402
}
1403-
}
1403+
}

sdk/src/constituentMap/constituentMap.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export type ConstituentMapConfig = {
3434
lpPoolId?: number;
3535
// potentially use these to filter Constituent accounts
3636
additionalFilters?: MemcmpFilter[];
37+
decoder?: 'base64' | 'base64+zstd';
3738
};
3839

3940
export interface ConstituentMapInterface {
@@ -65,13 +66,15 @@ export class ConstituentMap implements ConstituentMapInterface {
6566
private spotMarketIndexToKeyMap = new Map<number, string>();
6667

6768
private lpPoolId: number;
69+
private decoder: 'base64' | 'base64+zstd';
6870

6971
constructor(config: ConstituentMapConfig) {
7072
this.driftClient = config.driftClient;
7173
this.additionalFilters = config.additionalFilters;
7274
this.commitment = config.subscriptionConfig.commitment;
7375
this.connection = config.connection || this.driftClient.connection;
7476
this.lpPoolId = config.lpPoolId ?? 0;
77+
this.decoder = config.decoder ?? 'base64+zstd';
7578

7679
if (config.subscriptionConfig.type === 'polling') {
7780
this.constituentAccountSubscriber =
@@ -129,7 +132,7 @@ export class ConstituentMap implements ConstituentMapInterface {
129132
{
130133
commitment: this.commitment,
131134
filters: this.getFilters(),
132-
encoding: 'base64+zstd',
135+
encoding: this.decoder,
133136
withContext: true,
134137
},
135138
];
@@ -146,15 +149,22 @@ export class ConstituentMap implements ConstituentMapInterface {
146149

147150
const promises = rpcResponseAndContext.value.map(
148151
async (programAccount) => {
149-
const compressedUserData = Buffer.from(
150-
programAccount.account.data[0],
151-
'base64'
152-
);
153-
const decoder = new ZSTDDecoder();
154-
await decoder.init();
155-
const buffer = Buffer.from(
156-
decoder.decode(compressedUserData, MAX_CONSTITUENT_SIZE_BYTES)
157-
);
152+
let buffer: Buffer;
153+
154+
if (this.decoder === 'base64+zstd') {
155+
const compressedUserData = Buffer.from(
156+
programAccount.account.data[0],
157+
'base64'
158+
);
159+
const decoder = new ZSTDDecoder();
160+
await decoder.init();
161+
buffer = Buffer.from(
162+
decoder.decode(compressedUserData, MAX_CONSTITUENT_SIZE_BYTES)
163+
);
164+
} else {
165+
buffer = Buffer.from(programAccount.account.data[0], 'base64');
166+
}
167+
158168
const key = programAccount.pubkey.toString();
159169
const currAccountWithSlot = this.getWithSlot(key);
160170

0 commit comments

Comments
 (0)