|
| 1 | +import { PublicKey } from "@solana/web3.js"; |
| 2 | +import Squads, { |
| 3 | + DEFAULT_MULTISIG_PROGRAM_ID, |
| 4 | + getIxPDA, |
| 5 | + getTxPDA, |
| 6 | +} from "@sqds/mesh"; |
| 7 | +import { InstructionAccount, TransactionAccount } from "@sqds/mesh/lib/types"; |
| 8 | +import BN from "bn.js"; |
| 9 | +import lodash from "lodash"; |
| 10 | + |
| 11 | +export async function getActiveProposals( |
| 12 | + squad: Squads, |
| 13 | + vault: PublicKey, |
| 14 | + offset: number = 1 |
| 15 | +): Promise<TransactionAccount[]> { |
| 16 | + const msAccount = await squad.getMultisig(vault); |
| 17 | + let txKeys = lodash |
| 18 | + .range(offset, msAccount.transactionIndex + 1) |
| 19 | + .map((i) => getTxPDA(vault, new BN(i), DEFAULT_MULTISIG_PROGRAM_ID)[0]); |
| 20 | + let msTransactions = await squad.getTransactions(txKeys); |
| 21 | + return msTransactions |
| 22 | + .filter( |
| 23 | + (x: TransactionAccount | null): x is TransactionAccount => x != null |
| 24 | + ) |
| 25 | + .filter((x) => lodash.isEqual(x.status, { active: {} })); |
| 26 | +} |
| 27 | + |
| 28 | +export async function getManyProposalsInstructions( |
| 29 | + squad: Squads, |
| 30 | + txAccounts: TransactionAccount[] |
| 31 | +): Promise<InstructionAccount[][]> { |
| 32 | + let allIxsKeys = []; |
| 33 | + let ownerTransaction = []; |
| 34 | + for (let [index, txAccount] of txAccounts.entries()) { |
| 35 | + let ixKeys = lodash |
| 36 | + .range(1, txAccount.instructionIndex + 1) |
| 37 | + .map( |
| 38 | + (i) => |
| 39 | + getIxPDA( |
| 40 | + txAccount.publicKey, |
| 41 | + new BN(i), |
| 42 | + DEFAULT_MULTISIG_PROGRAM_ID |
| 43 | + )[0] |
| 44 | + ); |
| 45 | + for (let ixKey of ixKeys) { |
| 46 | + allIxsKeys.push(ixKey); |
| 47 | + ownerTransaction.push(index); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + let allTxIxsAcccounts = await squad.getInstructions(allIxsKeys); |
| 52 | + let ixAccountsByTx: InstructionAccount[][] = Array.from( |
| 53 | + Array(txAccounts.length), |
| 54 | + () => [] |
| 55 | + ); |
| 56 | + |
| 57 | + for (let i = 0; i < allTxIxsAcccounts.length; i++) { |
| 58 | + const toAdd = allTxIxsAcccounts[i]; |
| 59 | + if (toAdd) { |
| 60 | + ixAccountsByTx[ownerTransaction[i]].push(toAdd); |
| 61 | + } |
| 62 | + } |
| 63 | + return ixAccountsByTx; |
| 64 | +} |
| 65 | + |
| 66 | +export async function getProposalInstructions( |
| 67 | + squad: Squads, |
| 68 | + txAccount: TransactionAccount |
| 69 | +): Promise<InstructionAccount[]> { |
| 70 | + let ixKeys = lodash |
| 71 | + .range(1, txAccount.instructionIndex + 1) |
| 72 | + .map( |
| 73 | + (i) => |
| 74 | + getIxPDA(txAccount.publicKey, new BN(i), DEFAULT_MULTISIG_PROGRAM_ID)[0] |
| 75 | + ); |
| 76 | + let txIxs = await squad.getInstructions(ixKeys); |
| 77 | + return txIxs.filter( |
| 78 | + (x: InstructionAccount | null): x is InstructionAccount => x != null |
| 79 | + ); |
| 80 | +} |
0 commit comments