Xc admin/crank for creating accounts (#568)

* Crank can create accounts

* Cleanup

* Cleanup

* Cleanup

* Comment

* Done
This commit is contained in:
guibescos 2023-02-06 21:24:57 -06:00 committed by GitHub
parent d331393c81
commit 747886a089
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 97 additions and 15 deletions

View File

@ -4,7 +4,6 @@ import {
Connection,
Keypair,
PublicKey,
SendTransactionError,
SystemProgram,
Transaction,
} from "@solana/web3.js";
@ -14,18 +13,21 @@ import NodeWallet from "@project-serum/anchor/dist/cjs/nodewallet";
import {
getProposals,
MultisigParser,
PythMultisigInstruction,
WormholeMultisigInstruction,
} from "xc_admin_common";
import BN from "bn.js";
import { AnchorProvider } from "@project-serum/anchor";
import {
getPythClusterApiUrl,
getPythProgramKeyForCluster,
PythCluster,
} from "@pythnetwork/client/lib/cluster";
import {
deriveFeeCollectorKey,
getWormholeBridgeData,
} from "@certusone/wormhole-sdk/lib/cjs/solana/wormhole";
import { parseProductData } from "@pythnetwork/client";
export function envOrErr(env: string): string {
const val = process.env[env];
@ -35,6 +37,9 @@ export function envOrErr(env: string): string {
return String(process.env[env]);
}
const PRODUCT_ACCOUNT_SIZE = 512;
const PRICE_ACCOUNT_SIZE = 3312;
const CLUSTER: string = envOrErr("CLUSTER");
const COMMITMENT: Commitment =
(process.env.COMMITMENT as Commitment) ?? "confirmed";
@ -65,6 +70,7 @@ async function run() {
: 0;
const proposals = await getProposals(squad, VAULT, undefined, "executeReady");
for (const proposal of proposals) {
console.log("Trying to execute: ", proposal.publicKey.toBase58());
// If we have previously cancelled because the proposal was failing, don't attempt
@ -100,6 +106,92 @@ async function run() {
fromPubkey: squad.wallet.publicKey,
})
);
} else if (
parsedInstruction instanceof PythMultisigInstruction &&
parsedInstruction.name == "addProduct"
) {
/// Add product, fetch the symbol from updProduct to get the address
i += 1;
const nextInstructionPda = getIxPDA(
proposal.publicKey,
new BN(i),
squad.multisigProgramId
)[0];
const nextInstruction = await squad.getInstruction(
nextInstructionPda
);
const nextParsedInstruction = multisigParser.parseInstruction({
programId: nextInstruction.programId,
data: nextInstruction.data as Buffer,
keys: nextInstruction.keys as AccountMeta[],
});
if (
nextParsedInstruction instanceof PythMultisigInstruction &&
nextParsedInstruction.name == "updProduct"
) {
const productSeed = "product:" + nextParsedInstruction.args.symbol;
const productAddress = await PublicKey.createWithSeed(
squad.wallet.publicKey,
productSeed,
getPythProgramKeyForCluster(CLUSTER as PythCluster)
);
transaction.add(
SystemProgram.createAccountWithSeed({
fromPubkey: squad.wallet.publicKey,
basePubkey: squad.wallet.publicKey,
newAccountPubkey: productAddress,
seed: productSeed,
space: PRODUCT_ACCOUNT_SIZE,
lamports:
await squad.connection.getMinimumBalanceForRentExemption(
PRODUCT_ACCOUNT_SIZE
),
programId: getPythProgramKeyForCluster(CLUSTER as PythCluster),
})
);
transaction.add(
await squad.buildExecuteInstruction(
proposal.publicKey,
getIxPDA(
proposal.publicKey,
new BN(i - 1),
squad.multisigProgramId
)[0]
)
);
}
} else if (
parsedInstruction instanceof PythMultisigInstruction &&
parsedInstruction.name == "addPrice"
) {
/// Add price, fetch the symbol from the product account
const productAccount = await squad.connection.getAccountInfo(
parsedInstruction.accounts.named.productAccount.pubkey
);
if (productAccount?.data) {
const priceSeed =
"price:" + parseProductData(productAccount.data).product.symbol;
const priceAddress = await PublicKey.createWithSeed(
squad.wallet.publicKey,
priceSeed,
getPythProgramKeyForCluster(CLUSTER as PythCluster)
);
transaction.add(
SystemProgram.createAccountWithSeed({
fromPubkey: squad.wallet.publicKey,
basePubkey: squad.wallet.publicKey,
newAccountPubkey: priceAddress,
seed: priceSeed,
space: PRICE_ACCOUNT_SIZE,
lamports:
await squad.connection.getMinimumBalanceForRentExemption(
PRICE_ACCOUNT_SIZE
),
programId: getPythProgramKeyForCluster(CLUSTER as PythCluster),
})
);
}
}
transaction.add(
@ -109,20 +201,10 @@ async function run() {
)
);
try {
await new AnchorProvider(squad.connection, squad.wallet, {
commitment: COMMITMENT,
preflightCommitment: COMMITMENT,
}).sendAndConfirm(transaction, []);
} catch (error) {
// Mark the transaction as cancelled if we failed to run it
if (error instanceof SendTransactionError) {
console.error(error);
await squad.cancelTransaction(proposal.publicKey);
console.log("Cancelled: ", proposal.publicKey.toBase58());
}
break;
}
await new AnchorProvider(squad.connection, squad.wallet, {
commitment: COMMITMENT,
preflightCommitment: COMMITMENT,
}).sendAndConfirm(transaction, [], { skipPreflight: true });
}
} else {
console.log("Skipping: ", proposal.publicKey.toBase58());