sizeHint method

  1. @override
int sizeHint(
  1. RuntimeCall value
)
override

If possible give a hint of expected size of the encoding.

This method is used inside default implementation of encode to avoid re-allocations.

Implementation

@override
int sizeHint(final RuntimeCall value) {
  int size = 2; // Pallet index + call index

  // Find the call variant to calculate argument sizes
  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');
  }

  // Calculate size of 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 - use its length directly
    if (fieldValue is ScaleRawBytes) {
      size += fieldValue.bytes.length;
      continue;
    }

    final codec = registry.codecFor(field.type);
    size += codec.sizeHint(fieldValue);
  }

  return size;
}