Skip to content

Commit f38560a

Browse files
committed
refactor: simplify configuration retrieval and enhance type safety across programs
1 parent 2ef8732 commit f38560a

File tree

5 files changed

+15
-39
lines changed

5 files changed

+15
-39
lines changed

governance/xc_admin/packages/xc_admin_common/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export {
2222
ProductRawConfig,
2323
MappingRawConfig,
2424
RawConfig,
25-
GetConfigParams,
2625
DownloadablePriceAccount,
2726
DownloadableProduct,
2827
DownloadableConfig,

governance/xc_admin/packages/xc_admin_common/src/programs/core/core_functions.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ function sortData(data: DownloadableConfig): DownloadableConfig {
128128
/**
129129
* Parse raw on-chain accounts into the Pyth Core configuration format
130130
*/
131-
export function getConfig(
132-
params: CoreConfigParams & { programType: ProgramType.PYTH_CORE },
133-
): RawConfig {
131+
export function getConfig(params: CoreConfigParams): RawConfig {
134132
const accounts = params.accounts;
135133

136134
// Create a map of parsed base data for each account to avoid repeated parsing

governance/xc_admin/packages/xc_admin_common/src/programs/lazer/lazer_functions.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import {
44
ValidationResult,
55
DownloadableProduct,
66
DownloadableConfig,
7-
GetConfigParams,
7+
ProgramType,
88
} from "../types";
9-
import { ProgramType } from "../types";
109

1110
/**
1211
* Program ID for the Pyth Lazer program
@@ -81,11 +80,7 @@ export function isAvailableOnCluster(cluster: PythCluster): boolean {
8180
* @param params Parameters to fetch Lazer configuration
8281
* @returns Promise resolving to Lazer-specific configuration object
8382
*/
84-
export function getConfig(
85-
params: LazerConfigParams & { programType: ProgramType.PYTH_LAZER },
86-
): LazerConfig {
87-
// No need for runtime check since it's enforced by the type system
88-
83+
export function getConfig(params: LazerConfigParams): LazerConfig {
8984
// Extract the properties
9085
const { endpoint, network, options } = params;
9186

governance/xc_admin/packages/xc_admin_common/src/programs/program_registry.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ import {
66
ProgramConfig,
77
ValidationResult,
88
RawConfig,
9-
GetConfigParams,
109
} from "./types";
1110

1211
// Import functions from each program implementation
1312
import * as pythCore from "./core/core_functions";
1413
import * as pythLazer from "./lazer/lazer_functions";
15-
import { LazerConfig, LAZER_PROGRAM_ID } from "./lazer/lazer_functions";
14+
import {
15+
LazerConfig,
16+
LAZER_PROGRAM_ID,
17+
LazerConfigParams,
18+
} from "./lazer/lazer_functions";
19+
import { CoreConfigParams } from "./core/core_functions";
1620

1721
/**
1822
* Function to get the program address for each program type
@@ -39,18 +43,13 @@ export const isAvailableOnCluster: Record<
3943

4044
/**
4145
* Function to get configuration for each program type
42-
* Uses discriminated union to ensure type safety
4346
*/
4447
export const getConfig: {
45-
[ProgramType.PYTH_CORE]: (
46-
params: Extract<GetConfigParams, { programType: ProgramType.PYTH_CORE }>,
47-
) => RawConfig;
48-
[ProgramType.PYTH_LAZER]: (
49-
params: Extract<GetConfigParams, { programType: ProgramType.PYTH_LAZER }>,
50-
) => LazerConfig;
48+
[ProgramType.PYTH_CORE]: (params: CoreConfigParams) => RawConfig;
49+
[ProgramType.PYTH_LAZER]: (params: LazerConfigParams) => LazerConfig;
5150
} = {
52-
[ProgramType.PYTH_CORE]: (params) => pythCore.getConfig(params),
53-
[ProgramType.PYTH_LAZER]: (params) => pythLazer.getConfig(params),
51+
[ProgramType.PYTH_CORE]: pythCore.getConfig,
52+
[ProgramType.PYTH_LAZER]: pythLazer.getConfig,
5453
};
5554

5655
/**

governance/xc_admin/packages/xc_admin_common/src/programs/types.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
import { PublicKey } from "@solana/web3.js";
22
import { PermissionData, Product } from "@pythnetwork/client";
3-
import {
4-
LazerConfig,
5-
LazerConfigParams,
6-
LazerInstructionAccounts,
7-
} from "./lazer/lazer_functions";
8-
import {
9-
CoreConfigParams,
10-
CoreInstructionAccounts,
11-
} from "./core/core_functions";
3+
import { LazerConfig, LazerInstructionAccounts } from "./lazer/lazer_functions";
4+
import { CoreInstructionAccounts } from "./core/core_functions";
125
/**
136
* Represents the different Pyth programs supported by the application.
147
*/
@@ -70,14 +63,6 @@ export type RawConfig = {
7063
permissionAccount?: PermissionData;
7164
};
7265

73-
/**
74-
* Union type for configuration parameters that can vary by program type
75-
* This is a discriminated union type that enforces program type at compile time
76-
*/
77-
export type GetConfigParams =
78-
| (CoreConfigParams & { programType: ProgramType.PYTH_CORE })
79-
| (LazerConfigParams & { programType: ProgramType.PYTH_LAZER });
80-
8166
/**
8267
* Type for downloadable price account configuration
8368
*/

0 commit comments

Comments
 (0)