Skip to content
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
4 changes: 2 additions & 2 deletions app/entities/idl/api/resolve-program-idl-names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
50 changes: 45 additions & 5 deletions app/entities/idl/model/__tests__/instruction-name-table.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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' },
Expand Down Expand Up @@ -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',
Expand All @@ -150,14 +154,50 @@ 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', () => {
// 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(listedDiscriminator))).toBe('Vote');
});

it('should keep the legacy form of an 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)).toEqual({
programName: 'Amm V3',
resolveInstructionName: undefined,
});
});
});
});
23 changes: 19 additions & 4 deletions app/entities/idl/model/instruction-name-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/entities/idl/model/use-program-idl-names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function useProgramIdlNames(programIds: string[], cluster: Cluster, url:
const resolvers = new Map<string, ProgramIdlNames>();
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;
Expand Down