-
Notifications
You must be signed in to change notification settings - Fork 308
Xc admin/propose instructions #463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export * from "./multisig"; | ||
| export * from "./propose"; | ||
| export * from "./governance_payload"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| import Squads, { getIxAuthorityPDA, getTxPDA } from "@sqds/mesh"; | ||
| import { | ||
| PublicKey, | ||
| Transaction, | ||
| TransactionInstruction, | ||
| } from "@solana/web3.js"; | ||
| import { BN } from "bn.js"; | ||
| import { AnchorProvider } from "@project-serum/anchor"; | ||
| import { | ||
| createWormholeProgramInterface, | ||
| getPostMessageAccounts, | ||
| } from "@certusone/wormhole-sdk/lib/cjs/solana/wormhole"; | ||
| import { encodeExecutePostedVaa } from "./governance_payload/ExecutePostedVaa"; | ||
|
|
||
| type SquadInstruction = { | ||
| instruction: TransactionInstruction; | ||
| authorityIndex?: number; | ||
| authorityBump?: number; | ||
| authorityType?: string; | ||
| }; | ||
|
|
||
| /** | ||
| * Propose an array of `TransactionInstructions` as a proposal | ||
| * @param squad Squads client | ||
| * @param vault vault public key (the id of the multisig where these instructions should be proposed) | ||
| * @param instructions instructions that will be proposed | ||
| * @param remote whether the instructions should be executed in the chain of the multisig or remotely on Pythnet | ||
| * @returns the newly created proposal's pubkey | ||
| */ | ||
| export async function proposeInstructions( | ||
| squad: Squads, | ||
| vault: PublicKey, | ||
| instructions: TransactionInstruction[], | ||
| remote: boolean, | ||
| wormholeAddress?: PublicKey | ||
| ): Promise<PublicKey> { | ||
| const msAccount = await squad.getMultisig(vault); | ||
| let txToSend: Transaction[] = []; | ||
|
|
||
| const createProposal = new Transaction().add( | ||
| await squad.buildCreateTransaction( | ||
| msAccount.publicKey, | ||
| msAccount.authorityIndex, | ||
| msAccount.transactionIndex + 1 | ||
| ) | ||
| ); | ||
| const newProposalAddress = getTxPDA( | ||
| vault, | ||
| new BN(msAccount.transactionIndex + 1), | ||
| squad.multisigProgramId | ||
| )[0]; | ||
| txToSend.push(createProposal); | ||
|
|
||
| if (remote) { | ||
| if (!wormholeAddress) { | ||
| throw new Error("Need wormhole address"); | ||
| } | ||
| for (let i = 0; i < instructions.length; i++) { | ||
| const squadIx = await wrapAsRemoteInstruction( | ||
| squad, | ||
| vault, | ||
| newProposalAddress, | ||
| instructions[i], | ||
| i, | ||
| wormholeAddress | ||
| ); | ||
| txToSend.push( | ||
| new Transaction().add( | ||
| await squad.buildAddInstruction( | ||
| vault, | ||
| newProposalAddress, | ||
| squadIx.instruction, | ||
| i, | ||
| squadIx.authorityIndex, | ||
| squadIx.authorityBump, | ||
| squadIx.authorityType | ||
| ) | ||
| ) | ||
| ); | ||
| } | ||
| } else { | ||
| for (let i = 0; i < instructions.length; i++) { | ||
| txToSend.push( | ||
| new Transaction().add( | ||
| await squad.buildAddInstruction( | ||
| vault, | ||
| newProposalAddress, | ||
| instructions[i], | ||
| i | ||
| ) | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| txToSend.push( | ||
| new Transaction().add( | ||
| await squad.buildActivateTransaction(vault, newProposalAddress) | ||
| ) | ||
| ); | ||
| txToSend.push( | ||
| new Transaction().add( | ||
| await squad.buildApproveTransaction(vault, newProposalAddress) | ||
| ) | ||
| ); | ||
|
|
||
| await new AnchorProvider( | ||
| squad.connection, | ||
| squad.wallet, | ||
| AnchorProvider.defaultOptions() | ||
| ).sendAll( | ||
| txToSend.map((tx) => { | ||
| return { tx, signers: [] }; | ||
| }) | ||
| ); | ||
| return newProposalAddress; | ||
| } | ||
|
|
||
| /** | ||
| * Wrap `instruction` in a Wormhole message for remote execution | ||
| * @param squad Squads client | ||
| * @param vault vault public key (the id of the multisig where these instructions should be proposed) | ||
| * @param proposalAddress address of the proposal | ||
| * @param instruction instruction to be wrapped in a Wormhole message | ||
| * @param instructionIndex index of the instruction within the proposal | ||
| * @param wormholeAddress address of the Wormhole bridge | ||
| * @returns an instruction to be proposed | ||
| */ | ||
| export async function wrapAsRemoteInstruction( | ||
| squad: Squads, | ||
| vault: PublicKey, | ||
| proposalAddress: PublicKey, | ||
| instruction: TransactionInstruction, | ||
| instructionIndex: number, | ||
| wormholeAddress: PublicKey | ||
| ): Promise<SquadInstruction> { | ||
| const emitter = squad.getAuthorityPDA(vault, 0); | ||
|
|
||
| const [messagePDA, messagePdaBump] = getIxAuthorityPDA( | ||
| proposalAddress, | ||
| new BN(instructionIndex), | ||
| squad.multisigProgramId | ||
| ); | ||
|
|
||
| const provider = new AnchorProvider( | ||
| squad.connection, | ||
| squad.wallet, | ||
| AnchorProvider.defaultOptions() | ||
| ); | ||
| const wormholeProgram = createWormholeProgramInterface( | ||
| wormholeAddress, | ||
| provider | ||
| ); | ||
|
|
||
| const buffer = encodeExecutePostedVaa({ | ||
| targetChainId: "pythnet", | ||
| instructions: [instruction], | ||
| }); | ||
|
|
||
| const accounts = getPostMessageAccounts( | ||
| wormholeAddress, | ||
| emitter, | ||
| emitter, | ||
| messagePDA | ||
| ); | ||
|
|
||
| return { | ||
| instruction: await wormholeProgram.methods | ||
| .postMessage(0, buffer, 0) | ||
| .accounts(accounts) | ||
| .instruction(), | ||
| authorityIndex: instructionIndex, | ||
| authorityBump: messagePdaBump, | ||
| authorityType: "custom", | ||
| }; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make a class like
MultisigWrapperto encapsulate some of the common arguments (squads, wormholeAddress, etc)