compileInstructions method

List<CompiledInstruction> compileInstructions(
  1. List<TransactionInstruction> instructions
)

Compile transaction instructions into compiled instructions.

Implementation

List<CompiledInstruction> compileInstructions(
    List<TransactionInstruction> instructions) {
  if (length > SolanaTransactionConstant.maximumAccountKeys) {
    throw const MessageException(
        'Account index overflow encountered during compilation');
  }
  Map<String, int> keyIndexMap = {};
  for (final keySegment in _keySegments.expand((segment) => segment)) {
    keyIndexMap[keySegment.address] = keyIndexMap.length;
  }
  int findKeyIndex(String key) {
    final keyIndex = keyIndexMap[key];
    if (keyIndex == null) {
      throw const MessageException(
          'Encountered an unknown instruction account key during compilation');
    }
    return keyIndex;
  }

  {
    return instructions.map((instruction) {
      return CompiledInstruction(
        programIdIndex: findKeyIndex(instruction.programId.address),
        accounts: instruction.keys
            .map((meta) => findKeyIndex(meta.publicKey.address))
            .toList(),
        data: instruction.data,
      );
    }).toList();
  }
}