Any.fromJson constructor

Any.fromJson(
  1. Map<String, dynamic> json
)

Create a new Any instance by parsing a JSON map.

Implementation

factory Any.fromJson(Map<String, dynamic> json) {
  final String typeUrl =
      OnChainUtils.parseString(value: json['type_url'], name: 'type_url');
  final parts = typeUrl.split('type.googleapis.com/protocol.');
  if (parts.length != 2) {
    throw const TronPluginException('Invalid contract typeUrl');
  }
  final contractType = TransactionContractType.findByName(parts.last);
  if (json['value'] is String && StringUtils.isHexBytes(json['value'])) {
    final contractBytes = BytesUtils.tryFromHexString(json['value']);
    if (contractBytes != null) {
      return Any(
          typeUrl: typeUrl,
          value: TronBaseContract.deserialize(
              contractType: contractType, contractBytes: contractBytes));
    }
  }
  final Map<String, dynamic> contractDetails = OnChainUtils.parseMap(
      value: json['value'], name: 'value', throwOnNull: true)!;
  TronBaseContract contract = TronBaseContract.fromJson(
      contractType: contractType, json: contractDetails);
  return Any(typeUrl: typeUrl, value: contract);
}