encodeTo method

  1. @override
void encodeTo(
  1. RuntimeCall value,
  2. Output output
)
override

Convert self to a slice and append it to the destination.

Implementation

@override
void encodeTo(final RuntimeCall value, final Output output) {
  output.pushByte(value.palletIndex);
  output.pushByte(value.callIndex);

  // Find the call variant to encode arguments properly
  final pallet = registry.palletByIndex(value.palletIndex);
  if (pallet == null) {
    throw MetadataException('Pallet with index ${value.palletIndex} not found');
  }

  final calls = pallet.calls;
  if (calls == null) throw MetadataException('Pallet ${pallet.name} has no calls');

  final callVariant = registry.getVariantByIndex(calls.type, value.callIndex);
  if (callVariant == null) {
    throw MetadataException('Call with index ${value.callIndex} not found');
  }

  // Encode arguments
  for (final field in callVariant.fields) {
    final fieldName = field.name ?? 'unnamed';
    final fieldValue = value.args[fieldName];

    if (fieldValue == null && field.typeName?.contains('Option<') != true) {
      throw MetadataException('Missing required argument: $fieldName');
    }

    // Check if the value is ScaleRawBytes - if so, write bytes directly
    if (fieldValue is ScaleRawBytes) {
      ScaleRawBytes.codec.encodeTo(fieldValue, output);
      continue;
    }

    final codec = registry.codecFor(field.type);
    codec.encodeTo(fieldValue, output);
  }
}