Skip to content

Commit

Permalink
feat(xc_admin_cli): make deactivate stake take in vote accounts (#2036)
Browse files Browse the repository at this point in the history
* feat: make deactivate stake take in vote accounts

* fix node version
  • Loading branch information
guibescos authored Oct 16, 2024
1 parent 9973287 commit c7d5cf0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci-ethereum-contract.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
working-directory: target_chains/ethereum/contracts/
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v4
with:
node-version-file: "package.json"

- uses: pnpm/action-setup@v4
name: Install pnpm
Expand Down
33 changes: 20 additions & 13 deletions governance/xc_admin/packages/xc_admin_cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
PROGRAM_AUTHORITY_ESCROW,
createDetermisticPriceStoreInitializePublisherInstruction,
createPriceStoreInstruction,
fetchStakeAccounts,
findDetermisticStakeAccountAddress,
getMultisigCluster,
getProposalInstructions,
Expand All @@ -59,6 +60,7 @@ import {
DEFAULT_PRIORITY_FEE_CONFIG,
TransactionBuilder,
} from "@pythnetwork/solana-utils";
import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";

export async function loadHotWalletOrLedger(
wallet: string,
Expand Down Expand Up @@ -389,8 +391,8 @@ multisigCommand(
"Deactivate the delegated stake from the account"
)
.requiredOption(
"-s, --stake-accounts <comma_separated_stake_account>",
"stake accounts to be deactivated"
"-d, --vote-pubkeys <comma_separated_voter_pubkeys>",
"vote account unstake from"
)
.action(async (options: any) => {
const vault = await loadVaultFromOptions(options);
Expand All @@ -399,20 +401,25 @@ multisigCommand(
cluster
);

const stakeAccounts = options.stakeAccounts
? options.stakeAccounts.split(",").map((m: string) => new PublicKey(m))
const voteAccounts: PublicKey[] = options.votePubkeys
? options.votePubkeys.split(",").map((m: string) => new PublicKey(m))
: [];

const instructions = stakeAccounts.reduce(
(instructions: TransactionInstruction[], stakeAccount: PublicKey) => {
const transaction = StakeProgram.deactivate({
stakePubkey: stakeAccount,
authorizedPubkey,
});
const stakeAccounts = (
await Promise.all(
voteAccounts.map((voteAccount: PublicKey) =>
fetchStakeAccounts(
new Connection(getPythClusterApiUrl(cluster)),
voteAccount
)
)
)
).flat();

return instructions.concat(transaction.instructions);
},
[]
const instructions = stakeAccounts.flatMap(
(stakeAccount) =>
StakeProgram.deactivate({ stakePubkey: stakeAccount, authorizedPubkey })
.instructions
);

await vault.proposeInstructions(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { TransactionInstruction } from "@solana/web3.js";
import {
Connection,
PublicKey,
StakeProgram,
TransactionInstruction,
} from "@solana/web3.js";
import {
MultisigInstruction,
MultisigInstructionProgram,
UNRECOGNIZED_INSTRUCTION,
} from ".";
import { AnchorAccounts } from "./anchor";
import { StakeInstruction } from "@solana/web3.js";
import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";

export class SolanaStakingMultisigInstruction implements MultisigInstruction {
readonly program = MultisigInstructionProgram.SolanaStakingProgram;
Expand Down Expand Up @@ -115,3 +121,31 @@ export class SolanaStakingMultisigInstruction implements MultisigInstruction {
}
}
}

export async function fetchStakeAccounts(
connection: Connection,
voterAccount: PublicKey
) {
const stakeAccounts = await connection.getProgramAccounts(
StakeProgram.programId,
{
encoding: "base64",
filters: [
{
memcmp: {
offset: 0,
bytes: bs58.encode(Buffer.from([2, 0, 0, 0])),
},
},
{
memcmp: {
offset: 124,
bytes: voterAccount.toBase58(),
},
},
],
}
);

return stakeAccounts.map((account) => account.pubkey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,7 @@ export { PythMultisigInstruction } from "./PythMultisigInstruction";
export { AnchorMultisigInstruction } from "./MessageBufferMultisigInstruction";
export { SystemProgramMultisigInstruction } from "./SystemProgramInstruction";
export { BpfUpgradableLoaderInstruction } from "./BpfUpgradableLoaderMultisigInstruction";
export { SolanaStakingMultisigInstruction } from "./SolanaStakingMultisigInstruction";
export {
SolanaStakingMultisigInstruction,
fetchStakeAccounts,
} from "./SolanaStakingMultisigInstruction";

0 comments on commit c7d5cf0

Please sign in to comment.