Skip to content

Commit ceec09b

Browse files
committed
feat(programs): add program adapter interface and factory implementation
1 parent 3dcf09e commit ceec09b

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ export * from "./chains";
1414
export * from "./deterministic_stake_accounts";
1515
export * from "./price_store";
1616
export { default as lazerIdl } from "./multisig_transaction/idl/lazer.json";
17+
18+
// Export program-related modules
19+
export * from "./programs/program_adapter";
20+
export * from "./programs/types";
21+
export * from "./programs/adapter_factory";
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ProgramAdapter } from "./program_adapter";
2+
import { ProgramType } from "./types";
3+
4+
/**
5+
* Factory function to get the appropriate program adapter based on program type.
6+
*
7+
* @param type The type of program to get the adapter for
8+
* @returns The corresponding program adapter
9+
* @throws Error if the adapter for the given program type is not implemented
10+
*/
11+
export function getProgramAdapter(type: ProgramType): ProgramAdapter {
12+
switch (type) {
13+
case ProgramType.PYTH_CORE:
14+
// Will be implemented in a future commit
15+
throw new Error("Pyth Core adapter not yet implemented");
16+
case ProgramType.PYTH_LAZER:
17+
// Will be implemented in a future commit
18+
throw new Error("Pyth Lazer adapter not yet implemented");
19+
default:
20+
const exhaustiveCheck: never = type;
21+
throw new Error(`Unknown program type: ${exhaustiveCheck}`);
22+
}
23+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { PublicKey, TransactionInstruction } from "@solana/web3.js";
2+
import { PythCluster } from "@pythnetwork/client";
3+
4+
/**
5+
* Program adapter interface that defines the contract for interacting with
6+
* different Pyth programs (Core, Lazer, etc).
7+
*
8+
* This adapter pattern allows the frontend to use a consistent interface
9+
* while supporting multiple program implementations.
10+
*/
11+
export interface ProgramAdapter {
12+
/**
13+
* Program address on Solana
14+
* Each program may have different addresses on different clusters
15+
*/
16+
getProgramAddress(cluster: PythCluster): PublicKey;
17+
18+
/**
19+
* Human-readable name of the program
20+
*/
21+
readonly name: string;
22+
23+
/**
24+
* Brief description of the program
25+
*/
26+
readonly description: string;
27+
28+
/**
29+
* Type identifier for the program
30+
*/
31+
readonly type: string;
32+
33+
/**
34+
* Parse raw on-chain accounts into a standardized configuration object
35+
* for the specific program.
36+
*
37+
* @param accounts Array of account data from the blockchain
38+
* @param cluster The Pyth cluster where the accounts were fetched from
39+
* @returns Program-specific configuration object
40+
*/
41+
getConfigFromRawAccounts(accounts: any[], cluster: PythCluster): any;
42+
43+
/**
44+
* Format the configuration for downloading as a JSON file
45+
*
46+
* @param config The program's configuration object
47+
* @returns Configuration formatted for download
48+
*/
49+
getDownloadableConfig(config: any): any;
50+
51+
/**
52+
* Validate an uploaded configuration against the current configuration
53+
*
54+
* @param existingConfig Current configuration
55+
* @param uploadedConfig Configuration from an uploaded file
56+
* @returns Object with validation result and optional error message
57+
*/
58+
validateUploadedConfig(
59+
existingConfig: any,
60+
uploadedConfig: any,
61+
cluster: PythCluster,
62+
): {
63+
isValid: boolean;
64+
error?: string;
65+
changes?: any;
66+
};
67+
68+
/**
69+
* Generate the necessary instructions to apply configuration changes
70+
*
71+
* @param changes Configuration changes to apply
72+
* @param cluster The Pyth cluster where the changes will be applied
73+
* @param accounts Additional context needed for generating instructions
74+
* @returns Promise resolving to an array of TransactionInstructions
75+
*/
76+
generateInstructions(
77+
changes: any,
78+
cluster: PythCluster,
79+
accounts: {
80+
fundingAccount: PublicKey;
81+
[key: string]: any;
82+
},
83+
): Promise<TransactionInstruction[]>;
84+
85+
/**
86+
* Check if the program is available on the specified cluster
87+
*
88+
* @param cluster The Pyth cluster to check
89+
* @returns True if the program is available on the cluster
90+
*/
91+
isAvailableOnCluster(cluster: PythCluster): boolean;
92+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Represents the different Pyth programs supported by the application.
3+
*/
4+
export enum ProgramType {
5+
/**
6+
* Original Pyth oracle program
7+
*/
8+
PYTH_CORE = "PYTH_CORE",
9+
10+
/**
11+
* Next-generation Pyth oracle program
12+
*/
13+
PYTH_LAZER = "PYTH_LAZER",
14+
}

0 commit comments

Comments
 (0)