decodeEvent static method

Map<String, dynamic> decodeEvent({
  1. required List<AbiType> types,
  2. required List<bool> indexed,
  3. required List<String>? names,
  4. required List<String> topics,
  5. required Uint8List data,
})

Decodes event log data.

topics contains the indexed parameters (topic0 is the event signature). data contains the non-indexed parameters.

Implementation

static Map<String, dynamic> decodeEvent({
  required List<AbiType> types,
  required List<bool> indexed,
  required List<String>? names,
  required List<String> topics,
  required Uint8List data,
}) {
  final result = <String, dynamic>{};

  var topicIndex = 1; // Skip topic[0] (event signature)

  final nonIndexedTypes = <AbiType>[];
  final nonIndexedIndices = <int>[];

  for (var i = 0; i < types.length; i++) {
    if (indexed[i]) {
      // Indexed parameters are in topics
      if (topicIndex < topics.length) {
        final topic = topics[topicIndex];
        final topicBytes = _hexToBytes(topic);

        dynamic value;
        if (types[i].isDynamic) {
          // Dynamic types are hashed, return the hash
          value = topic;
        } else {
          final (decoded, _) = types[i].decode(topicBytes, 0);
          value = decoded;
        }

        final name = names != null && i < names.length ? names[i] : 'arg$i';
        result[name] = value;
        topicIndex++;
      }
    } else {
      nonIndexedTypes.add(types[i]);
      nonIndexedIndices.add(i);
    }
  }

  // Decode non-indexed parameters from data
  if (nonIndexedTypes.isNotEmpty && data.isNotEmpty) {
    final decoded = decode(nonIndexedTypes, data);
    for (var i = 0; i < decoded.length; i++) {
      final originalIndex = nonIndexedIndices[i];
      final name = names != null && originalIndex < names.length
          ? names[originalIndex]
          : 'arg$originalIndex';
      result[name] = decoded[i];
    }
  }

  return result;
}