Skip to content

Commit a550d0c

Browse files
committed
Simplify programs registry (#29)
1 parent 92162ce commit a550d0c

File tree

5 files changed

+75
-58
lines changed

5 files changed

+75
-58
lines changed

src/idl/_type_test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,11 @@ t33.instructions.encoders.transferChecked({
299299
});
300300

301301
let a3 = idl.defineIDL(TokenIDL);
302-
a3.associatedToken.pdas.associatedToken({ owner: '1', tokenProgram: '1', mint: '1' });
302+
a3.token.additionalPrograms.associatedToken.pdas.associatedToken({
303+
owner: '1',
304+
tokenProgram: '1',
305+
mint: '1',
306+
});
303307

304308
assertType<{
305309
mint: string;

src/idl/index.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,16 +1183,24 @@ type IDL = {
11831183
};
11841184

11851185
type GetTypeIDL<T extends IDL> = {
1186-
[K in
1187-
| T['program']['name']
1188-
| T['additionalPrograms'][number]['name']]: K extends T['program']['name']
1189-
? GetTypeProgram<T['program']>
1190-
: GetTypeProgram<Extract<T['additionalPrograms'][number], { name: K }>>;
1186+
[P in T['program']['name']]: {
1187+
program: GetTypeProgram<T['program']>;
1188+
additionalPrograms: {
1189+
[K in T['additionalPrograms'][number]['name']]: GetTypeProgram<
1190+
Extract<T['additionalPrograms'][number], { name: K }>
1191+
>;
1192+
};
1193+
};
11911194
};
11921195

11931196
export function defineIDL<T extends IDL>(idl: T): GetTypeIDL<T> {
1194-
const res: Record<string, any> = {};
1195-
res[idl.program.name] = defineProgram(idl.program);
1196-
for (const program of idl.additionalPrograms) res[program.name] = defineProgram(program);
1197+
const res: Record<string, any> = {
1198+
[idl.program.name]: {
1199+
program: defineProgram(idl.program),
1200+
additionalPrograms: {},
1201+
},
1202+
};
1203+
for (const program of idl.additionalPrograms)
1204+
res[idl.program.name].additionalPrograms[program.name] = defineProgram(program);
11971205
return res as GetTypeIDL<T>;
11981206
}

src/index.ts

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -315,79 +315,81 @@ export function AddressLookupTables(tables: Record<string, string[]>) {
315315
}
316316

317317
export const PROGRAMS = {
318-
System: idl.defineIDL(SystemIDL),
319-
Token: idl.defineIDL(TokenIDL),
320-
Token2022: idl.defineIDL(Token2022IDL),
321-
ALT: idl.defineIDL(ALTIDL),
322-
ComputeBudget: idl.defineIDL(ComputeBudgetIDL),
323-
Config: idl.defineIDL(ConfigIDL),
324-
Memo: idl.defineIDL(MemoIDL),
318+
...idl.defineIDL(SystemIDL),
319+
...idl.defineIDL(TokenIDL),
320+
...idl.defineIDL(Token2022IDL),
321+
...idl.defineIDL(ALTIDL),
322+
...idl.defineIDL(ComputeBudgetIDL),
323+
...idl.defineIDL(ConfigIDL),
324+
...idl.defineIDL(MemoIDL),
325325
};
326326
// Old API compat
327-
export const sys = PROGRAMS.System.system.instructions.encoders;
328-
export const token = PROGRAMS.Token.token.instructions.encoders;
327+
export const sys = PROGRAMS.system.program.instructions.encoders;
328+
export const token = PROGRAMS.token.program.instructions.encoders;
329329
// TODO: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.
330-
export const token2022 = PROGRAMS.Token2022['token-2022'].instructions.encoders as any;
331-
export const associatedToken = PROGRAMS.Token.associatedToken.instructions.encoders;
330+
export const token2022 = PROGRAMS['token-2022'].program.instructions.encoders as any;
331+
export const associatedToken =
332+
PROGRAMS.token.additionalPrograms.associatedToken.instructions.encoders;
332333

333-
export const SYS_PROGRAM = PROGRAMS.System.system.contract;
334-
export const TOKEN_PROGRAM = PROGRAMS.Token.token.contract;
335-
export const TOKEN_PROGRAM2022 = PROGRAMS.Token2022['token-2022'].contract;
336-
export const ASSOCIATED_TOKEN_PROGRAM = PROGRAMS.Token.associatedToken.contract;
334+
export const SYS_PROGRAM = PROGRAMS.system.program.contract;
335+
export const TOKEN_PROGRAM = PROGRAMS.token.program.contract;
336+
export const TOKEN_PROGRAM2022 = PROGRAMS['token-2022'].program.contract;
337+
export const ASSOCIATED_TOKEN_PROGRAM = PROGRAMS.token.additionalPrograms.associatedToken.contract;
337338

338-
export const tokenAddress = PROGRAMS.Token.associatedToken.pdas.associatedToken;
339-
export const TokenAccount = PROGRAMS.Token.token.accounts.decoder;
340-
export const AddressTableLookupData = PROGRAMS.ALT.addressLookupTable.accounts.decoder;
339+
export const tokenAddress = PROGRAMS.token.additionalPrograms.associatedToken.pdas.associatedToken;
340+
export const TokenAccount = PROGRAMS.token.program.accounts.decoder;
341+
export const AddressTableLookupData = PROGRAMS.addressLookupTable.program.accounts.decoder;
341342

342343
export const isOnCurve = idl.isOnCurve;
343344
export const programAddress = idl.programAddress;
344345

345346
const TOKENS_ENCODE: Record<string, any> = {
346-
[TOKEN_PROGRAM]: PROGRAMS.Token.token.instructions.encoders,
347-
[TOKEN_PROGRAM2022]: PROGRAMS.Token2022['token-2022'].instructions.encoders,
347+
[TOKEN_PROGRAM]: PROGRAMS.token.program.instructions.encoders,
348+
[TOKEN_PROGRAM2022]: PROGRAMS['token-2022'].program.instructions.encoders,
348349
};
349350

350351
const ACCOUNTS_DECODE: Record<string, any> = {
351-
[SYS_PROGRAM]: PROGRAMS.System.system.accounts.decoder,
352-
[TOKEN_PROGRAM]: PROGRAMS.Token.token.accounts.decoder,
353-
[TOKEN_PROGRAM2022]: PROGRAMS.Token2022['token-2022'].accounts.decoder,
354-
[ASSOCIATED_TOKEN_PROGRAM]: PROGRAMS.Token.associatedToken.accounts.decoder,
355-
[PROGRAMS.ALT.addressLookupTable.contract]: PROGRAMS.ALT.addressLookupTable.accounts.decoder,
356-
[PROGRAMS.ComputeBudget.computeBudget.contract]:
357-
PROGRAMS.ComputeBudget.computeBudget.accounts.decoder,
358-
[PROGRAMS.Config.solanaConfig.contract]: PROGRAMS.Config.solanaConfig.accounts.decoder,
359-
[PROGRAMS.Memo.memo.contract]: PROGRAMS.Memo.memo.accounts.decoder,
352+
[SYS_PROGRAM]: PROGRAMS.system.program.accounts.decoder,
353+
[TOKEN_PROGRAM]: PROGRAMS.token.program.accounts.decoder,
354+
[TOKEN_PROGRAM2022]: PROGRAMS['token-2022'].program.accounts.decoder,
355+
[ASSOCIATED_TOKEN_PROGRAM]: PROGRAMS.token.additionalPrograms.associatedToken.accounts.decoder,
356+
[PROGRAMS.addressLookupTable.program.contract]:
357+
PROGRAMS.addressLookupTable.program.accounts.decoder,
358+
[PROGRAMS.computeBudget.program.contract]: PROGRAMS.computeBudget.program.accounts.decoder,
359+
[PROGRAMS.solanaConfig.program.contract]: PROGRAMS.solanaConfig.program.accounts.decoder,
360+
[PROGRAMS.memo.program.contract]: PROGRAMS.memo.program.accounts.decoder,
360361
};
361362
export function decodeAccount(contract: string, data: Bytes): unknown {
362363
if (ACCOUNTS_DECODE[contract] === undefined) throw new Error('unknown contract');
363364
return removeUndefined(ACCOUNTS_DECODE[contract](data));
364365
}
365366

366367
const REGISTRY: Record<string, any> = {
367-
[SYS_PROGRAM]: PROGRAMS.System.system.instructions.decoder,
368-
[TOKEN_PROGRAM]: PROGRAMS.Token.token.instructions.decoder,
369-
[TOKEN_PROGRAM2022]: PROGRAMS.Token2022['token-2022'].instructions.decoder,
370-
[ASSOCIATED_TOKEN_PROGRAM]: PROGRAMS.Token.associatedToken.instructions.decoder,
371-
[PROGRAMS.ALT.addressLookupTable.contract]: PROGRAMS.ALT.addressLookupTable.instructions.decoder,
372-
[PROGRAMS.ComputeBudget.computeBudget.contract]:
373-
PROGRAMS.ComputeBudget.computeBudget.instructions.decoder,
374-
[PROGRAMS.Config.solanaConfig.contract]: PROGRAMS.Config.solanaConfig.instructions.decoder,
375-
[PROGRAMS.Memo.memo.contract]: PROGRAMS.Memo.memo.instructions.decoder,
368+
[SYS_PROGRAM]: PROGRAMS.system.program.instructions.decoder,
369+
[TOKEN_PROGRAM]: PROGRAMS.token.program.instructions.decoder,
370+
[TOKEN_PROGRAM2022]: PROGRAMS['token-2022'].program.instructions.decoder,
371+
[ASSOCIATED_TOKEN_PROGRAM]:
372+
PROGRAMS.token.additionalPrograms.associatedToken.instructions.decoder,
373+
[PROGRAMS.addressLookupTable.program.contract]:
374+
PROGRAMS.addressLookupTable.program.instructions.decoder,
375+
[PROGRAMS.computeBudget.program.contract]: PROGRAMS.computeBudget.program.instructions.decoder,
376+
[PROGRAMS.solanaConfig.program.contract]: PROGRAMS.solanaConfig.program.instructions.decoder,
377+
[PROGRAMS.memo.program.contract]: PROGRAMS.memo.program.instructions.decoder,
376378
};
377379
export function parseInstruction(instruction: Instruction): unknown {
378380
if (REGISTRY[instruction.program] === undefined) throw new Error('unknown contract');
379381
return removeUndefined(REGISTRY[instruction.program](instruction));
380382
}
381383

382384
export const CONTRACTS: Record<string, any> = {
383-
[SYS_PROGRAM]: PROGRAMS.System.system,
384-
[TOKEN_PROGRAM]: PROGRAMS.Token.token,
385-
[TOKEN_PROGRAM2022]: PROGRAMS.Token2022['token-2022'],
386-
[ASSOCIATED_TOKEN_PROGRAM]: PROGRAMS.Token.associatedToken,
387-
[PROGRAMS.ALT.addressLookupTable.contract]: PROGRAMS.ALT.addressLookupTable,
388-
[PROGRAMS.ComputeBudget.computeBudget.contract]: PROGRAMS.ComputeBudget.computeBudget,
389-
[PROGRAMS.Config.solanaConfig.contract]: PROGRAMS.Config.solanaConfig,
390-
[PROGRAMS.Memo.memo.contract]: PROGRAMS.Memo.memo,
385+
[SYS_PROGRAM]: PROGRAMS.system.program,
386+
[TOKEN_PROGRAM]: PROGRAMS.token.program,
387+
[TOKEN_PROGRAM2022]: PROGRAMS['token-2022'].program,
388+
[ASSOCIATED_TOKEN_PROGRAM]: PROGRAMS.token.additionalPrograms.associatedToken,
389+
[PROGRAMS.addressLookupTable.program.contract]: PROGRAMS.addressLookupTable.program,
390+
[PROGRAMS.computeBudget.program.contract]: PROGRAMS.computeBudget.program,
391+
[PROGRAMS.solanaConfig.program.contract]: PROGRAMS.solanaConfig.program,
392+
[PROGRAMS.memo.program.contract]: PROGRAMS.memo.program,
391393
};
392394

393395
// Basic tx stuff

test/idl.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,8 +1137,11 @@ describe('Solana', () => {
11371137
}
11381138
deepStrictEqual(count, { ok: 10, failed: 13 });
11391139
});
1140-
should('IDL type size', () => {
1141-
deepStrictEqual(sol.PROGRAMS.Token.token.accounts.coders.token.size, 165);
1140+
should('IDL type size (token)', () => {
1141+
deepStrictEqual(sol.PROGRAMS.token.program.accounts.coders.token.size, 165);
1142+
});
1143+
should('IDL type size (token-2022)', () => {
1144+
deepStrictEqual(sol.PROGRAMS['token-2022'].program.accounts.coders.token.size, 165);
11421145
});
11431146
});
11441147

test/tokenmetatada.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('TokenMetadata', () => {
3636
})
3737
);
3838
deepStrictEqual(
39-
TokenMetadata.tokenMetadata.accounts.coders.metadata.decode(metadata, {
39+
TokenMetadata.tokenMetadata.program.accounts.coders.metadata.decode(metadata, {
4040
allowUnreadBytes: true,
4141
}),
4242
{

0 commit comments

Comments
 (0)