ContractAbi.fromJson constructor

ContractAbi.fromJson(
  1. String jsonData,
  2. String name
)

Implementation

factory ContractAbi.fromJson(String jsonData, String name) {
  final data = json.decode(jsonData);
  final functions = <ContractFunction>[];
  final events = <ContractEvent>[];

  for (final element in data) {
    final type = element['type'] as String;
    final name = (element['name'] as String?) ?? '';

    if (type == 'event') {
      final anonymous = element['anonymous'] as bool;
      final components = <EventComponent>[];

      for (final entry in element['inputs']) {
        components.add(
          EventComponent(
            _parseParam(entry as Map),
            entry['indexed'] as bool,
          ),
        );
      }

      events.add(ContractEvent(anonymous, name, components));
      continue;
    }

    final mutability = _mutabilityNames[element['stateMutability']];
    final parsedType = _functionTypeNames[element['type']];
    if (parsedType == null) continue;

    final inputs = _parseParams(element['inputs'] as List?);
    final outputs = _parseParams(element['outputs'] as List?);

    functions.add(
      ContractFunction(
        name,
        inputs,
        outputs: outputs,
        type: parsedType,
        mutability: mutability ?? StateMutability.nonPayable,
      ),
    );
  }

  return ContractAbi(name, functions, events);
}