decodeResults method

List decodeResults(
  1. List<String?> topics,
  2. String data
)

Decodes the fields of this event from the event's topics and its data payload.

components of this event which are EventComponent.indexed will be read from the topics, whereas non-indexed components will be read from the data section of the event. Indexed parameters which would take more than 32 bytes to encode are not included in the result. Apart from that, the order of the data returned is identical to the order of the components.

Implementation

List<dynamic> decodeResults(List<String?> topics, String data) {
  final topicOffset = anonymous ? 0 : 1;

  // non-indexed parameters are decoded like a tuple
  final notIndexed = components
      .where((c) => !c.indexed)
      .map((c) => c.parameter.type)
      .toList();
  final tuple = TupleType(notIndexed);

  final decodedNotIndexed = tuple.decode(hexToBytes(data).buffer, 0).data;

  // Merge indexed components (which are encoded as topics) and non-indexed
  // components (which were already decoded in decodedNotIndexed) together
  // into the result list.
  var dataIndex = 0;
  var topicIndex = topicOffset;

  final result = [];
  for (final component in components) {
    if (component.indexed) {
      // components that are bigger than 32 bytes when decoded, or have a
      // dynamic type, are not included in [topics]. A hash of the data will
      // be included instead. We can't decode these, so they will be skipped.
      final length = component.parameter.type.encodingLength;
      if (length.isDynamic ||
          length.length! > 32 ||
          topics[topicIndex] == null) {
        topicIndex++;
        continue;
      }

      final topicBuffer = hexToBytes(topics[topicIndex]!).buffer;
      result.add(component.parameter.type.decode(topicBuffer, 0).data);

      topicIndex++;
    } else {
      result.add(decodedNotIndexed[dataIndex]);
      dataIndex++;
    }
  }

  return result;
}