Adding Instructions

After creating a transaction it will be in the "Draft" status which allows the creator of the Transaction to attach one or multiple instructions.

Similarly to creating a transaction, each instruction attached to a transaction will have its corresponding data saved in an account represented by an instruction PDA.

Attaching an instruction will automatically bump up the instructionIndex, so when attaching an instruction ensure that the supplied instruction PDA is derived correctly (instructionIndex + 1).

Here is an example of creating a transaction and attaching an instruction, which will withdraw 1 SOL from the vault (authority) to a destination address:

// 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();

We now have a new transaction, with a single instruction attached to it.

Last updated