callSCFunctionMulti method

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

Call a smart contract's function

Implementation

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

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

    _logger.fine('callSCFunction: responseHttp.body=${responseHttp.body}');

    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, stack) {
    _logger.severe(
      'callSCFunction failed',
      e,
      stack,
    );
    rethrow;
  }
  return completer.future;
}