From 90a9037ae1e64f697bf2dbc99d92de047f07db57 Mon Sep 17 00:00:00 2001 From: guibescos <59208140+guibescos@users.noreply.github.com> Date: Wed, 7 Dec 2022 19:07:32 +0800 Subject: [PATCH] Add some function to fetch the multisig state (#416) * Add fetchers * Cleanup * Cleanup * Restore index * Format * Typo * Fix for loop * Add offset option --- .../src/multisig.ts | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 third_party/pyth/multisig-wh-message-builder/src/multisig.ts diff --git a/third_party/pyth/multisig-wh-message-builder/src/multisig.ts b/third_party/pyth/multisig-wh-message-builder/src/multisig.ts new file mode 100644 index 00000000..abfa2c1c --- /dev/null +++ b/third_party/pyth/multisig-wh-message-builder/src/multisig.ts @@ -0,0 +1,80 @@ +import { PublicKey } from "@solana/web3.js"; +import Squads, { + DEFAULT_MULTISIG_PROGRAM_ID, + getIxPDA, + getTxPDA, +} from "@sqds/mesh"; +import { InstructionAccount, TransactionAccount } from "@sqds/mesh/lib/types"; +import BN from "bn.js"; +import lodash from "lodash"; + +export async function getActiveProposals( + squad: Squads, + vault: PublicKey, + offset: number = 1 +): Promise { + const msAccount = await squad.getMultisig(vault); + let txKeys = lodash + .range(offset, msAccount.transactionIndex + 1) + .map((i) => getTxPDA(vault, new BN(i), DEFAULT_MULTISIG_PROGRAM_ID)[0]); + let msTransactions = await squad.getTransactions(txKeys); + return msTransactions + .filter( + (x: TransactionAccount | null): x is TransactionAccount => x != null + ) + .filter((x) => lodash.isEqual(x.status, { active: {} })); +} + +export async function getManyProposalsInstructions( + squad: Squads, + txAccounts: TransactionAccount[] +): Promise { + let allIxsKeys = []; + let ownerTransaction = []; + for (let [index, txAccount] of txAccounts.entries()) { + let ixKeys = lodash + .range(1, txAccount.instructionIndex + 1) + .map( + (i) => + getIxPDA( + txAccount.publicKey, + new BN(i), + DEFAULT_MULTISIG_PROGRAM_ID + )[0] + ); + for (let ixKey of ixKeys) { + allIxsKeys.push(ixKey); + ownerTransaction.push(index); + } + } + + let allTxIxsAcccounts = await squad.getInstructions(allIxsKeys); + let ixAccountsByTx: InstructionAccount[][] = Array.from( + Array(txAccounts.length), + () => [] + ); + + for (let i = 0; i < allTxIxsAcccounts.length; i++) { + const toAdd = allTxIxsAcccounts[i]; + if (toAdd) { + ixAccountsByTx[ownerTransaction[i]].push(toAdd); + } + } + return ixAccountsByTx; +} + +export async function getProposalInstructions( + squad: Squads, + txAccount: TransactionAccount +): Promise { + let ixKeys = lodash + .range(1, txAccount.instructionIndex + 1) + .map( + (i) => + getIxPDA(txAccount.publicKey, new BN(i), DEFAULT_MULTISIG_PROGRAM_ID)[0] + ); + let txIxs = await squad.getInstructions(ixKeys); + return txIxs.filter( + (x: InstructionAccount | null): x is InstructionAccount => x != null + ); +}