MessagePackerInstructionPlan

type MessagePackerInstructionPlan = Readonly<{
  getMessagePacker: () => MessagePacker;
  kind: "messagePacker";
  planType: "instructionPlan";
}>;

A plan that can dynamically pack instructions into transaction messages.

This plan provides a MessagePacker via the getMessagePacker method, which enables instructions to be dynamically packed into the provided transaction message until there are no more instructions to pack. The returned MessagePacker offers a packMessageToCapacity(message) method that packs the provided message — when possible — and a done() method that checks whether there are more instructions to pack.

Several helper functions are provided to create objects of this type such as getLinearMessagePackerInstructionPlan or getMessagePackerInstructionPlanFromInstructions.

Examples

An message packer plan for a write instruction that uses as many bytes as possible.

const plan = getLinearMessagePackerInstructionPlan({
  totalLength: dataToWrite.length,
  getInstruction: (offset, length) =>
    getWriteInstruction({
      offset,
      data: dataToWrite.slice(offset, offset + length),
    }),
});
plan satisfies MessagePackerInstructionPlan;

A message packer plan for multiple realloc instructions.

const plan = getReallocMessagePackerInstructionPlan({
  totalSize: additionalDataSize,
  getInstruction: (size) => getExtendInstruction({ length: size }),
});
plan satisfies MessagePackerInstructionPlan;

Using a message packer plan.

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

See

On this page