broadcastStdTx static method

Future<TransactionResult> broadcastStdTx({
  1. required Wallet wallet,
  2. required StdTx stdTx,
  3. String mode = 'sync',
  4. Client? client,
})

Broadcasts the given stdTx using the info contained inside the given wallet. Returns the hash of the transaction once it has been send, or throws an exception if an error is risen during the sending.

Implementation

static Future<TransactionResult> broadcastStdTx({
  required Wallet wallet,
  required StdTx stdTx,
  String mode = 'sync',
  http.Client? client,
}) async {
  client ??= http.Client();

  // Get the endpoint
  final apiUrl = Uri.parse('${wallet.networkInfo.lcdUrl}/txs');

  // Build the request body
  final requestBody = {'tx': stdTx.toJson(), 'mode': mode};
  final requestBodyJson = jsonEncode(requestBody);

  // Get the response
  final response = await client.post(apiUrl, body: requestBodyJson);
  if (response.statusCode != 200) {
    throw Exception(
      'Expected status code 200 but got ${response.statusCode} - ${response.body}',
    );
  }

  // Convert the response
  final json = jsonDecode(response.body) as Map<String, dynamic>;
  return _convertJson(json);
}