Adding Instructions
// fetch the multsig account to get the TX index
const multisigAccount = await squadsProgram.account.fetch(multisig);
// bump up the transactionIndex by 1 to seed the new tx address
const transactionIndex = new anchor.BN(multsigAccount.transactionIndex + 1);
// find the tx address/pda for the to-be-created transaction
const [transaction] = await anchor.web3.PublicKey.findProgramAddress([
anchor.utils.bytes.utf8.encode("squad"),
multisig.toBuffer(),
transactionIndex.toBuffer("le", 4),
anchor.utils.bytes.utf8.encode("transaction")
], squadsProgram.programId);
// create the transaction account with the authorityIndex of 1
const createIx = await program.methods.createTransaction(1)
.accounts({
multisig,
transaction,
creator: wallet.publicKey
})
.rpc();
// use instructionIndex = 1, since this is the first and only ix
const instructionIndex = new anchor.BN(1);
// get the instruction address / PDA
const [instruction] = await anchor.web3.PublicKey.findProgramAddress([
anchor.utils.bytes.utf8.encode("squad"),
transaction.toBuffer(),
instructionIndex.toBuffer("le", 1),
anchor.utils.bytes.utf8.encode("instruction")
], squadsProgram.programId);
// find the authority for the authorityIndex = 1
const authorityIndex = anchor.BN(1);
const [authority] = await anchor.web3.PublicKey.findProgramAddress([
anchor.utils.bytes.utf8.encode("squad"),
multisig.toBuffer(),
authorityIndex.toBuffer("le", 4), // note authority index is an u32 (4 byte)
anchor.utils.bytes.utf8.encode("authority")
], squadsProgram.programId);
// define the instruction to withdraw funds from the authority/vault
// and supply the authority
const withdrawIx = anchor.web3.SystemProgram.transfer({
fromPubkey: authority,
lamports: anchor.web3.LAMPORTS_PER_SOL,
toPubkey: destination // public key of where to send the SOL
});
// add the instruction to the transaction, using the withdrawIx as a parameter
await program.methods.addInstruction(withdrawIx)
.accounts({
multisig,
transaction,
instruction,
creator: wallet.publicKey
})
.rpc();
Last updated