ContractABI.fromAbi constructor

ContractABI.fromAbi(
  1. String jsonAbi
)

Implementation

factory ContractABI.fromAbi(String jsonAbi) {
  final abi = jsonDecode(jsonAbi);
  final functions = <LocalContractFunction>[];
  final events = <ContractEvent>[];
  for (final item in abi) {
    final type = item['type'];
    final name = item['name'];

    if (name == null) {
      continue;
    }

    if (type == "fallback" || type == "constructor" || type == "error") {
      continue;
    }

    if (type == "event") {
      final anonymous = item['anonymous'] as bool;
      final parameters = <FunctionParam>[];

      for (final param in item["inputs"]) {
        parameters.add(FunctionParam.fromJson(param));
      }
      events.add(
        ContractEvent(
          name: name,
          parameters: parameters,
          anonymous: anonymous,
        ),
      );
      continue;
    }
    final stateMutability = StateMutability.fromString(
      item['stateMutability'] as String,
    );
    final parameters = <FunctionParam>[];
    final outputs = <FunctionParam>[];

    for (final param in item["inputs"]) {
      parameters.add(FunctionParam.fromJson(param));
    }
    if (item["outputs"] != null) {
      for (final param in item["outputs"]) {
        outputs.add(FunctionParam.fromJson(param));
      }
    }
    functions.add(
      LocalContractFunction(
        name: name,
        parameters: parameters,
        stateMutability: stateMutability,
        outputTypes: outputs,
      ),
    );
  }
  return ContractABI(functions, events);
}