MessagePacker

type MessagePacker = Readonly<{
  done: () => boolean;
  packMessageToCapacity: (transactionMessage, config?) => TransactionMessage & TransactionMessageWithFeePayer;
}>;

The message packer returned by the MessagePackerInstructionPlan.

It offers a packMessageToCapacity(transactionMessage) method that packs as many instructions as possible into the provided transaction message, while still being able to fit into the transaction size limit and the default or configured instruction-count limit. It returns the updated transaction message with the packed instructions or throws an error if the current transaction message cannot accommodate this plan.

The done() method checks whether there are more instructions to pack into transaction messages.

Custom message packers can rely on resolveMaxInstructionsPerTransaction, assertMaxInstructionsPerTransaction and assertMessageCanAccommodateSize to enforce the instruction-count and size limits, and may throw SOLANA_ERROR__INSTRUCTION_PLANS__MESSAGE_REJECTED_BY_PACKER to refuse a message for any other reason. Consumers can use isMessagePackerErrorThatRequiresNewCandidate to identify errors that call for a new transaction message.

Example

let plan: MessagePackerInstructionPlan;
const messagePacker = plan.getMessagePacker();
 
while (!messagePacker.done()) {
  try {
    transactionMessage = messagePacker.packMessageToCapacity(transactionMessage);
  } catch (error) {
    if (!isMessagePackerErrorThatRequiresNewCandidate(error)) throw error;
    // The current transaction message cannot be used to pack this plan.
    // We should create a new one and try again.
  }
}

See

MessagePackerInstructionPlan

On this page