sendTx method

Future<TransactionStatus> sendTx(
  1. Transaction transaction
)

Send a transaction to the network @param {Object} tx Transaction to send

Implementation

Future<TransactionStatus> sendTx(Transaction transaction) async {
  final completer = Completer<TransactionStatus>();

  var transactionStatus = const TransactionStatus();

  try {
    final jsonRPCRequest = setJsonRPCRequest(
      'send_transaction',
      {
        'transaction': jsonDecode(
          transaction.convertToJSON(),
        ),
      },
    );

    log(
      'sendTx: (${transaction.address}) requestHttp.body=$jsonRPCRequest',
      logsActivation: logsActivation,
    );

    final responseHttp = await http.post(
      Uri.parse('$endpoint/api/rpc'),
      body: jsonRPCRequest,
      headers: kRequestHeaders,
    );
    log(
      'sendTx: (${transaction.address}) responseHttp.body=${responseHttp.body}',
      logsActivation: logsActivation,
    );

    final result = getJsonRPCResult(responseHttp.body);
    transactionStatus = transactionStatusFromJson(
      json.encode(
        result,
      ),
    );

    completer.complete(transactionStatus);
  } catch (e) {
    log(
      'sendTx: (${transaction.address}) $e',
      logsActivation: logsActivation,
    );
    rethrow;
  }

  return completer.future;
}