From 565c82755c1f5c122be0d5125e1115b703a5286f Mon Sep 17 00:00:00 2001 From: Alex S <27193857+askov@users.noreply.github.com> Date: Fri, 27 Nov 2026 15:20:52 +0700 Subject: [PATCH 1/2] fix(idl): name the instructions of a legacy Anchor IDL A legacy Anchor IDL lists no discriminators, so the name table named none of its instructions. Convert it with `formatSerdeIdl` before the table reads it, as the instruction decoder does. --- .../idl/api/resolve-program-idl-names.ts | 2 +- .../__tests__/instruction-name-table.spec.ts | 44 ++++++++++++++++--- .../idl/model/instruction-name-table.ts | 23 ++++++++-- .../idl/model/use-program-idl-names.ts | 2 +- 4 files changed, 60 insertions(+), 11 deletions(-) diff --git a/app/entities/idl/api/resolve-program-idl-names.ts b/app/entities/idl/api/resolve-program-idl-names.ts index 303b11d694..abe27c9e75 100644 --- a/app/entities/idl/api/resolve-program-idl-names.ts +++ b/app/entities/idl/api/resolve-program-idl-names.ts @@ -35,5 +35,5 @@ export async function resolveProgramIdlNames( shouldRetry: backoffOptions.shouldRetry ?? isRetryableError, })) as ProgramIdlPair; - return buildProgramIdlNames([idls.programMetadataIdl, idls.anchorIdl]); + return buildProgramIdlNames([idls.programMetadataIdl, idls.anchorIdl], programId); } diff --git a/app/entities/idl/model/__tests__/instruction-name-table.spec.ts b/app/entities/idl/model/__tests__/instruction-name-table.spec.ts index 228ffaffd5..89bfbb08ff 100644 --- a/app/entities/idl/model/__tests__/instruction-name-table.spec.ts +++ b/app/entities/idl/model/__tests__/instruction-name-table.spec.ts @@ -1,4 +1,6 @@ import { type SupportedIdl } from '../../lib/types'; +import ammV3Legacy from '../../mocks/anchor/anchor-0.29.0-devi51mZmdwUJGU9hjN27vEz64Gps7uUefqxg27EAtH.json'; +import ammV3Modern from '../../mocks/anchor/anchor-0.30.1-devi51mZmdwUJGU9hjN27vEz64Gps7uUefqxg27EAtH.json'; import codamaPmp from '../../mocks/codama/codama-1.0.0-ProgM6JCCvbYkfKqJYHePx4xxSUSqJp7rh8Lyv7nk7S.json'; import { buildInstructionNameResolver, @@ -9,9 +11,11 @@ import { matchInstructionName, } from '../instruction-name-table'; +const VOTING_ADDRESS = 'AXcxp15oz1L4YYtqZo6Qt6EkUj1jtLR6wXYqaJvn4oye'; + // A resolved Anchor IDL (snake_case names + 8-byte discriminators), shaped like the on-chain Voting DApp. const anchorIdl = { - address: 'AXcxp15oz1L4YYtqZo6Qt6EkUj1jtLR6wXYqaJvn4oye', + address: VOTING_ADDRESS, instructions: [ { accounts: [], args: [], discriminator: [193, 22, 99, 197, 18, 33, 115, 117], name: 'initialize_poll' }, { accounts: [], args: [], discriminator: [227, 110, 155, 23, 136, 126, 172, 25], name: 'vote' }, @@ -138,7 +142,7 @@ describe('instruction-name-table', () => { describe('buildProgramIdlNames', () => { it('should bundle the program name with the instruction-name resolver', () => { - const names = buildProgramIdlNames([anchorIdl]); + const names = buildProgramIdlNames([anchorIdl], VOTING_ADDRESS); expect(names?.programName).toBe('Voting'); expect(names?.resolveInstructionName?.(Uint8Array.from([227, 110, 155, 23, 136, 126, 172, 25]))).toBe( 'Vote', @@ -150,14 +154,44 @@ describe('instruction-name-table', () => { instructions: [], metadata: { name: 'voting', spec: '0.1.0' }, } as unknown as SupportedIdl; - const names = buildProgramIdlNames([nameOnly]); + const names = buildProgramIdlNames([nameOnly], VOTING_ADDRESS); expect(names?.programName).toBe('Voting'); expect(names?.resolveInstructionName).toBeUndefined(); }); it('should return undefined when no IDL yields a name or a table', () => { - expect(buildProgramIdlNames([undefined, undefined])).toBeUndefined(); - expect(buildProgramIdlNames([])).toBeUndefined(); + expect(buildProgramIdlNames([undefined, undefined], VOTING_ADDRESS)).toBeUndefined(); + expect(buildProgramIdlNames([], VOTING_ADDRESS)).toBeUndefined(); + }); + + it('should name an instruction of a legacy Anchor IDL by the discriminator its modern IDL lists', () => { + const legacyIdl = { ...ammV3Legacy, metadata: null } as unknown as SupportedIdl; + const swapV2 = ammV3Modern.instructions.find(ix => ix.name === 'swap_v2'); + const names = buildProgramIdlNames([undefined, legacyIdl], ammV3Modern.address); + expect(names?.programName).toBe('Amm V3'); + expect(names?.resolveInstructionName?.(Uint8Array.from(swapV2?.discriminator ?? []))).toBe('Swap V2'); + }); + + it('should keep the discriminators and the name of an IDL that has no metadata.spec', () => { + const idlWithoutSpec = { ...anchorIdl, metadata: { name: 'voting' } } as unknown as SupportedIdl; + const names = buildProgramIdlNames([idlWithoutSpec], VOTING_ADDRESS); + expect(names?.programName).toBe('Voting'); + expect(names?.resolveInstructionName?.(Uint8Array.from([227, 110, 155, 23, 136, 126, 172, 25]))).toBe( + 'Vote', + ); + }); + + it('should not throw on a legacy Anchor IDL that the converter rejects', () => { + const rejectedIdl = { + ...ammV3Legacy, + types: [ + { + name: 'Pair', + type: { fields: [{ name: 'pair', type: { tuple: ['u8', 'string'] } }], kind: 'struct' }, + }, + ], + } as unknown as SupportedIdl; + expect(() => buildProgramIdlNames([rejectedIdl], ammV3Modern.address)).not.toThrow(); }); }); }); diff --git a/app/entities/idl/model/instruction-name-table.ts b/app/entities/idl/model/instruction-name-table.ts index 174c430c22..0778a2931a 100644 --- a/app/entities/idl/model/instruction-name-table.ts +++ b/app/entities/idl/model/instruction-name-table.ts @@ -13,7 +13,8 @@ import { arrayOrEmpty } from '@/app/shared/lib/array'; import { startsWith } from '@/app/shared/lib/bytes'; import { type AnchorIdl, type CodamaIdl, type SupportedIdl } from '../lib/types'; -import { getIdlStandard } from './idl-version'; +import { formatSerdeIdl } from './formatters/format-serde-idl'; +import { getIdlStandard, getIdlVersion } from './idl-version'; export type InstructionNameEntry = { discriminator: Uint8Array; @@ -42,13 +43,27 @@ export type ProgramIdlNames = { * Build a program's IDL-derived names — display name and instruction-name resolver — from its IDLs in * preference order (program-metadata beats Anchor). Returns undefined when no IDL yields either. */ -export function buildProgramIdlNames(idls: readonly (SupportedIdl | undefined)[]): ProgramIdlNames | undefined { - const programName = buildProgramName(idls); - const resolveInstructionName = buildInstructionNameResolver(idls); +export function buildProgramIdlNames( + idls: readonly (SupportedIdl | undefined)[], + programAddress: string, +): ProgramIdlNames | undefined { + const convertedIdls = idls.map(idl => idl && convertIfLegacy(idl, programAddress)); + const programName = buildProgramName(convertedIdls); + const resolveInstructionName = buildInstructionNameResolver(convertedIdls); if (programName === undefined && resolveInstructionName === undefined) return undefined; return { programName, resolveInstructionName }; } +function convertIfLegacy(idl: SupportedIdl, programAddress: string): SupportedIdl { + if (getIdlVersion(idl) !== 'Legacy') return idl; + if (arrayOrEmpty((idl as AnchorIdl).instructions).some(ix => ix.discriminator)) return idl; + try { + return formatSerdeIdl(idl, programAddress); + } catch { + return idl; + } +} + /** * The program's display name from IDL metadata, title-cased — the first IDL in preference order that * carries one wins (program-metadata's `program.name`, else Anchor's `metadata.name`). Undefined when diff --git a/app/entities/idl/model/use-program-idl-names.ts b/app/entities/idl/model/use-program-idl-names.ts index 34f9b259ba..bdbb5e2850 100644 --- a/app/entities/idl/model/use-program-idl-names.ts +++ b/app/entities/idl/model/use-program-idl-names.ts @@ -70,7 +70,7 @@ export function useProgramIdlNames(programIds: string[], cluster: Cluster, url: const resolvers = new Map(); for (const [id, idls] of data ?? []) { // Program-metadata IDL is preferred; Anchor only names what program-metadata can't. - const names = buildProgramIdlNames([idls.programMetadataIdl, idls.anchorIdl]); + const names = buildProgramIdlNames([idls.programMetadataIdl, idls.anchorIdl], id); if (names) resolvers.set(id, names); } return resolvers; From 491a461482f04d06d7a125eac86c9ef7c5eab2dc Mon Sep 17 00:00:00 2001 From: Alex S <27193857+askov@users.noreply.github.com> Date: Fri, 27 Nov 2026 16:02:51 +0700 Subject: [PATCH 2/2] test(idl): assert the names of a legacy Anchor IDL that stays unconverted The rejected-IDL test checked only that nothing throws, and the no-spec fixture listed the discriminators that the converter computes. Both tests passed when buildProgramIdlNames dropped or converted the IDL. --- .../idl/api/resolve-program-idl-names.ts | 2 +- .../__tests__/instruction-name-table.spec.ts | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/entities/idl/api/resolve-program-idl-names.ts b/app/entities/idl/api/resolve-program-idl-names.ts index abe27c9e75..3a65ab205b 100644 --- a/app/entities/idl/api/resolve-program-idl-names.ts +++ b/app/entities/idl/api/resolve-program-idl-names.ts @@ -13,7 +13,7 @@ import { type ProgramIdlPair } from './types'; * IDL names it. * * Undefined covers three cases: a non-anchor program, a program with no IDL, and a - * program whose IDLs carry neither a metadata name nor a usable discriminator table. + * program whose IDLs give neither a program name nor a usable discriminator table. * @param url - The RPC URL to resolve against * @param programId - The program whose IDLs to read * @param backoffOptions - Retry configuration for handling RPC failures diff --git a/app/entities/idl/model/__tests__/instruction-name-table.spec.ts b/app/entities/idl/model/__tests__/instruction-name-table.spec.ts index 89bfbb08ff..0d70d356db 100644 --- a/app/entities/idl/model/__tests__/instruction-name-table.spec.ts +++ b/app/entities/idl/model/__tests__/instruction-name-table.spec.ts @@ -173,15 +173,18 @@ describe('instruction-name-table', () => { }); it('should keep the discriminators and the name of an IDL that has no metadata.spec', () => { - const idlWithoutSpec = { ...anchorIdl, metadata: { name: 'voting' } } as unknown as SupportedIdl; + // The converter does not compute these bytes, so a converted IDL fails the lookup. + const listedDiscriminator = [1, 2, 3, 4, 5, 6, 7, 8]; + const idlWithoutSpec = { + instructions: [{ accounts: [], args: [], discriminator: listedDiscriminator, name: 'vote' }], + metadata: { name: 'voting' }, + } as unknown as SupportedIdl; const names = buildProgramIdlNames([idlWithoutSpec], VOTING_ADDRESS); expect(names?.programName).toBe('Voting'); - expect(names?.resolveInstructionName?.(Uint8Array.from([227, 110, 155, 23, 136, 126, 172, 25]))).toBe( - 'Vote', - ); + expect(names?.resolveInstructionName?.(Uint8Array.from(listedDiscriminator))).toBe('Vote'); }); - it('should not throw on a legacy Anchor IDL that the converter rejects', () => { + it('should keep the legacy form of an Anchor IDL that the converter rejects', () => { const rejectedIdl = { ...ammV3Legacy, types: [ @@ -191,7 +194,10 @@ describe('instruction-name-table', () => { }, ], } as unknown as SupportedIdl; - expect(() => buildProgramIdlNames([rejectedIdl], ammV3Modern.address)).not.toThrow(); + expect(buildProgramIdlNames([rejectedIdl], ammV3Modern.address)).toEqual({ + programName: 'Amm V3', + resolveInstructionName: undefined, + }); }); }); });