sendTx method

Future<TransactionStatus> sendTx(
  1. Transaction transaction
)

Send a transaction to the network

Implementation

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

  var transactionStatus = const TransactionStatus();

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

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

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

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

    completer.complete(transactionStatus);
  } catch (e, stack) {
    _logger.severe(
      'sendTx: (${transaction.address}) failed',
      e,
      stack,
    );
    rethrow;
  }

  return completer.future;
}