getChainInfo method

ChainInfo getChainInfo()

Implementation

ChainInfo getChainInfo() {
  final rawMetadata = decodedMetadata.metadataJson;

  _defineCalls();

  int callModuleIndex = -1;
  int eventModuleIndex = -1;

  _resultingRegistry.addCodec('Call', ProxyCodec());

  final genericCallsCodec = <int, MapEntry<String, Codec>>{};
  final genericEventsCodec = <int, MapEntry<String, Codec>>{};

  for (final module in rawMetadata['modules']) {
    // Getting the module name and converting it to camelCase
    final String moduleName = module['name']!;

    if (module['calls'] != null) {
      callModuleIndex++;

      final index = module['index'] ?? callModuleIndex;

      final callEnumCodec = <int, MapEntry<String, Codec>>{};
      for (var callIndex = 0;
          callIndex < module['calls'].length;
          callIndex++) {
        final call = module['calls'][callIndex];

        final List<dynamic> args = call['args'];
        Codec argsCodec = NullCodec.codec;

        if (args.isNotEmpty) {
          if (args.first is String) {
            // List<String>
            final codecs = <Codec>[];
            for (final String arg in args) {
              final codec = _getCodecFromType(arg, moduleName);
              codecs.add(codec);
            }
            argsCodec = TupleCodec(codecs);
          } else if (args.first is Map<String, dynamic>) {
            // List<Map<String, dynamic>>
            final Map<String, Codec> codecs = <String, Codec>{};

            for (final arg in args) {
              final String name = arg['name'];
              final String argType = arg['type'];

              codecs[name] = _getCodecFromType(argType, moduleName);
            }
            argsCodec = CompositeCodec(codecs);
          } else {
            throw Exception('Unknown type of args: $args');
          }
        }
        callEnumCodec[callIndex] = MapEntry(call['name'], argsCodec);
      }
      genericCallsCodec[index] =
          MapEntry(module['name'], ComplexEnumCodec.sparse(callEnumCodec));
    }

    if (module['events'] != null) {
      eventModuleIndex++;

      final index = module['index'] ?? eventModuleIndex;

      final eventEnumCodec = <int, MapEntry<String, Codec>>{};
      for (var eventIndex = 0;
          eventIndex < module['events'].length;
          eventIndex++) {
        final event = module['events'][eventIndex];

        final List<dynamic> args = event['args'];

        Codec argsCodec = NullCodec.codec;

        if (args.isNotEmpty) {
          if (args.first is String) {
            // List<String>
            final codecs = <Codec>[];
            for (final String arg in args) {
              final codec = _getCodecFromType(arg, moduleName);
              codecs.add(codec);
            }
            argsCodec = TupleCodec(codecs);
          } else if (args.first is Map<String, dynamic>) {
            // List<Map<String, dynamic>>
            final Map<String, Codec> codecs = <String, Codec>{};

            for (final arg in args) {
              final String name = arg['name'];
              final String argType = arg['type'];

              codecs[name] = _getCodecFromType(argType, moduleName);
            }
            argsCodec = CompositeCodec(codecs);
          } else {
            throw Exception('Unknown type of args: $args');
          }
        }
        eventEnumCodec[eventIndex] = MapEntry(event['name'], argsCodec);
      }
      genericEventsCodec[index] =
          MapEntry(module['name'], ComplexEnumCodec.sparse(eventEnumCodec));
    }
  }
  //
  // Register the Generic Call
  {
    // replace the proxy of GenericCall with the real GenericCall
    final proxyCodec = _resultingRegistry.getCodec('Call')! as ProxyCodec;
    proxyCodec.codec = ComplexEnumCodec.sparse(genericCallsCodec);
  }

  //
  // Register the Generic Event
  _resultingRegistry.addCodec(
      'GenericEvent', ComplexEnumCodec.sparse(genericEventsCodec));

  {
    //
    // Configure the events codec
    final eventCodec = _resultingRegistry.parseSpecificCodec(
        legacyTypes.types, 'EventRecord');

    _resultingRegistry
      ..addCodec('EventRecord', eventCodec)
      ..addCodec('EventCodec', SequenceCodec(eventCodec));
  }

  _createExtrinsicCodec();

  return ChainInfo(
    scaleCodec: ScaleCodec(_resultingRegistry),
    version: decodedMetadata.version,
    constants: _constants(),
  );
}