Skip to content

Commit 1f405c6

Browse files
committed
refactor: improve type safety in configuration handling
1 parent a891f7e commit 1f405c6

File tree

4 files changed

+35
-51
lines changed

4 files changed

+35
-51
lines changed

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,13 @@ function sortData(data: DownloadableConfig): DownloadableConfig {
132132
return sortedData;
133133
}
134134

135-
/**
136-
* Get the Pyth Core program address for the given cluster
137-
*/
138-
export function getProgramAddress(cluster: PythCluster): PublicKey {
139-
return getPythProgramKeyForCluster(cluster);
140-
}
141-
142135
/**
143136
* Parse raw on-chain accounts into the Pyth Core configuration format
144137
*/
145-
export function getConfig(params: GetConfigParams): RawConfig {
146-
if (params.programType !== ProgramType.PYTH_CORE) {
147-
throw new Error("Invalid program type for Core getConfig");
148-
}
149-
138+
export function getConfig(
139+
params: CoreConfigParams & { programType: ProgramType.PYTH_CORE },
140+
): RawConfig {
141+
// No need for runtime check since it's enforced by the type system
150142
const accounts = params.accounts;
151143

152144
// 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: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { ProgramType } from "../types";
1111
/**
1212
* Program ID for the Pyth Lazer program
1313
*/
14-
const LAZER_PROGRAM_ID = new PublicKey(
14+
export const LAZER_PROGRAM_ID = new PublicKey(
1515
"pytd2yyk641x7ak7mkaasSJVXh6YYZnC7wTmtgAyxPt",
1616
);
1717

@@ -60,18 +60,6 @@ export type LazerFeed = {
6060
// Add other feed-specific properties as needed
6161
};
6262

63-
/**
64-
* Get the program address for the given cluster
65-
*
66-
* @param cluster The Pyth cluster to get the address for
67-
* @returns The program address
68-
*/
69-
export function getProgramAddress(cluster: PythCluster): PublicKey {
70-
// Currently using the same mock address for all clusters
71-
// Will be updated with actual addresses for each cluster when available
72-
return LAZER_PROGRAM_ID;
73-
}
74-
7563
/**
7664
* Check if the Pyth Lazer program is available on the specified cluster
7765
*
@@ -80,9 +68,9 @@ export function getProgramAddress(cluster: PythCluster): PublicKey {
8068
*/
8169
export function isAvailableOnCluster(cluster: PythCluster): boolean {
8270
return (
83-
cluster === "pythnet" ||
84-
cluster === "mainnet-beta" ||
85-
cluster === "devnet" ||
71+
cluster === "pythnet" ??
72+
cluster === "mainnet-beta" ??
73+
cluster === "devnet" ??
8674
cluster === "testnet"
8775
);
8876
}
@@ -93,16 +81,13 @@ export function isAvailableOnCluster(cluster: PythCluster): boolean {
9381
* @param params Parameters to fetch Lazer configuration
9482
* @returns Promise resolving to Lazer-specific configuration object
9583
*/
96-
export function getConfig(params: GetConfigParams): LazerConfig {
97-
// Only process if this is a Lazer config request
98-
if (params.programType !== ProgramType.PYTH_LAZER) {
99-
throw new Error("Invalid program type for Lazer getConfig");
100-
}
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
10188

102-
// Cast to LazerConfigParams to extract the properties
103-
const { endpoint, network, options } = params as {
104-
programType: ProgramType.PYTH_LAZER;
105-
} & LazerConfigParams;
89+
// Extract the properties
90+
const { endpoint, network, options } = params;
10691

10792
// Example implementation that would fetch data from a non-Solana source
10893
// For now, return a placeholder with empty feeds

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { AccountInfo, PublicKey } from "@solana/web3.js";
2-
import { PythCluster } from "@pythnetwork/client";
2+
import { getPythProgramKeyForCluster, PythCluster } from "@pythnetwork/client";
33
import {
44
DownloadableConfig,
55
ProgramType,
66
ProgramConfig,
7-
GetConfigParams,
87
ValidationResult,
8+
RawConfig,
9+
GetConfigParams,
910
} from "./types";
1011

1112
// Import functions from each program implementation
1213
import * as pythCore from "./core/core_functions";
1314
import * as pythLazer from "./lazer/lazer_functions";
15+
import { LazerConfig, LAZER_PROGRAM_ID } from "./lazer/lazer_functions";
1416

1517
/**
1618
* Function to get the program address for each program type
@@ -19,8 +21,9 @@ export const getProgramAddress: Record<
1921
ProgramType,
2022
(cluster: PythCluster) => PublicKey
2123
> = {
22-
[ProgramType.PYTH_CORE]: pythCore.getProgramAddress,
23-
[ProgramType.PYTH_LAZER]: pythLazer.getProgramAddress,
24+
[ProgramType.PYTH_CORE]: (cluster: PythCluster) =>
25+
getPythProgramKeyForCluster(cluster),
26+
[ProgramType.PYTH_LAZER]: () => LAZER_PROGRAM_ID,
2427
};
2528

2629
/**
@@ -36,13 +39,18 @@ export const isAvailableOnCluster: Record<
3639

3740
/**
3841
* Function to get configuration for each program type
42+
* Uses discriminated union to ensure type safety
3943
*/
40-
export const getConfig: Record<
41-
ProgramType,
42-
(params: GetConfigParams) => ProgramConfig
43-
> = {
44-
[ProgramType.PYTH_CORE]: pythCore.getConfig,
45-
[ProgramType.PYTH_LAZER]: pythLazer.getConfig,
44+
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;
51+
} = {
52+
[ProgramType.PYTH_CORE]: (params) => pythCore.getConfig(params),
53+
[ProgramType.PYTH_LAZER]: (params) => pythLazer.getConfig(params),
4654
};
4755

4856
/**

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,11 @@ export type RawConfig = {
7272

7373
/**
7474
* 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
7576
*/
7677
export type GetConfigParams =
77-
| ({
78-
programType: ProgramType.PYTH_CORE;
79-
} & CoreConfigParams)
80-
| ({ programType: ProgramType.PYTH_LAZER } & LazerConfigParams);
78+
| (CoreConfigParams & { programType: ProgramType.PYTH_CORE })
79+
| (LazerConfigParams & { programType: ProgramType.PYTH_LAZER });
8180

8281
/**
8382
* Type for downloadable price account configuration

0 commit comments

Comments
 (0)