decodeRawWithFetch static method

Future<ContractFunctionWithValues> decodeRawWithFetch({
  1. required Uint8List data,
  2. Map<String, String>? functionMap,
  3. bool openChain = true,
  4. bool fourByte = true,
})

Try to decode the raw data using the abiList If the function is not found locally it will try to fetch the function from an external source (4byte.directory) If the function is not found, return null If a local function is found, return a LocalContractFunctionWithValues If an external function is found, return a ExternalContractFunctionWithValues

Implementation

static Future<ContractFunctionWithValues> decodeRawWithFetch({
  required Uint8List data,
  Map<String, String>? functionMap,
  bool openChain = true,
  bool fourByte = true,
}) async {
  if (data.length < 4) return UnknownContractFunction(data: data);

  if (functionMap != null) {
    final localResult = decodeRaw(data: data, functionMap: functionMap);

    if (localResult is! UnknownContractFunction &&
        localResult is! NotDecodableContractFunction) {
      return localResult;
    }
  }

  final function_selector = data.sublist(0, 4).toHex;

  try {
    final externalFunction = await FunctionSelectorRepository().fetchSelector(
      function_selector,
      openChain: openChain,
      fourByte: fourByte,
    );
    if (externalFunction == null) {
      return UnknownContractFunction(data: data);
    }

    return decode(data: data, function: externalFunction);
  } catch (e) {
    return UnknownContractFunction(data: data);
  }
}