callSCFunctionMulti method

Future<List> callSCFunctionMulti({
  1. required List<SCCallFunctionRequest> jsonRPCRequests,
})

Call a smart contract's function @param {List

Implementation

Future<List<dynamic>> callSCFunctionMulti({
  required List<SCCallFunctionRequest> jsonRPCRequests,
}) async {
  final completer = Completer<List<dynamic>>();
  try {
    log(
      'callSCFunction: requestHttp.body=${json.encode(jsonRPCRequests)}',
      logsActivation: logsActivation,
    );

    final responseHttp = await http.post(
      Uri.parse('$endpoint/api/rpc'),
      body: json.encode(jsonRPCRequests),
      headers: kRequestHeaders,
    );

    log(
      'callSCFunction: responseHttp.body=${responseHttp.body}',
      logsActivation: logsActivation,
    );

    if (responseHttp.statusCode == 200) {
      final jsonResponse = json.decode(responseHttp.body);
      if (jsonResponse is List) {
        completer.complete(jsonResponse);
      } else {
        // 20240327 - Nodes don't send [] when requests have just an element...
        if (jsonResponse is Map && jsonRPCRequests.length == 1) {
          completer.complete([jsonResponse]);
        } else {
          throw const ArchethicInvalidResponseException(
            'callSCFunction: jsonResponse is not a list',
          );
        }
      }
    }
  } catch (e) {
    log(
      'callSCFunction: error=$e',
      logsActivation: logsActivation,
    );
    rethrow;
  }
  return completer.future;
}