Skip to content

Commit

Permalink
feat(governance/xc_admin): add init-price-feed-index cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-bahjati committed Sep 2, 2024
1 parent 4bdfddf commit d7f777b
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions governance/xc_admin/packages/xc_admin_cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { Program } from "@coral-xyz/anchor";
import NodeWallet from "@coral-xyz/anchor/dist/cjs/nodewallet";
import { Wallet } from "@coral-xyz/anchor/dist/cjs/provider";
import { TOKEN_PROGRAM_ID } from "@coral-xyz/anchor/dist/cjs/utils/token";
import { pythOracleProgram } from "@pythnetwork/client";
import {
pythOracleProgram,
PythHttpClient,
parseBaseData,
AccountType,
parsePriceData,
} from "@pythnetwork/client";
import {
PythCluster,
getPythClusterApiUrl,
Expand Down Expand Up @@ -33,9 +39,9 @@ import {
MultisigParser,
MultisigVault,
PROGRAM_AUTHORITY_ESCROW,
findDetermisticStakeAccountAddress,
getMultisigCluster,
getProposalInstructions,
findDetermisticStakeAccountAddress,
} from "@pythnetwork/xc-admin-common";

import {
Expand Down Expand Up @@ -500,6 +506,59 @@ multisigCommand(
);
});

multisigCommand(
"init-price-feed-index",
"Init price feed indexes to migrate old price feed accounts to the new index"
).action(async (options: any) => {
const vault = await loadVaultFromOptions(options);

const cluster: PythCluster = options.cluster;
const oracleProgramId = getPythProgramKeyForCluster(cluster);
const connection = new Connection(getPythClusterApiUrl(cluster));

const allPythAccounts = await connection.getProgramAccounts(oracleProgramId);

const pricePubkeysToInitialize = [];

for (const account of allPythAccounts) {
const data = account.account.data;
const pubkey = account.pubkey;

const base = parseBaseData(data);
if (base?.type === AccountType.Price) {
const parsed = parsePriceData(data);
if (parsed.feedIndex === 0) {
pricePubkeysToInitialize.push(pubkey);
}
}
}

// Create instructions to initialize the price feed indexes
const oracleProgram = pythOracleProgram(
oracleProgramId,
vault.getAnchorProvider()
);

const instructions: TransactionInstruction[] = [];
for (const pubkey of pricePubkeysToInitialize) {
instructions.push(
await oracleProgram.methods
.initPriceFeedIndex()
.accounts({
fundingAccount: await vault.getVaultAuthorityPDA(cluster),
priceAccount: pubkey,
})
.instruction()
);
}

await vault.proposeInstructions(
instructions,
cluster,
DEFAULT_PRIORITY_FEE_CONFIG
);
});

program
.command("parse-transaction")
.description("Parse a transaction sitting in the multisig")
Expand Down

0 comments on commit d7f777b

Please sign in to comment.