broadcastTransaction function

Future<String> broadcastTransaction({
  1. required String rawTxHex,
  2. required UTXONetworkType type,
})

Implementation

Future<String> broadcastTransaction({
  required String rawTxHex,
  required UTXONetworkType type,
}) async {
  final (result, _, error) = await fetchFromRandomElectrumXNode(
    (client) async {
      final broadcastResult =
          await client.broadcastTransaction(rawTxHex: rawTxHex);
      return broadcastResult;
    },
    client: null,
    token: type.coin,
    endpoints: type.endpoints,
  );

  if (result == null) {
    throw SendFailure("Broadcasting failed: ${error?.message}");
  }

  final json = jsonDecode(result);

  if (result.contains('error')) {
    if (json
        case {
          "error": {"error": {"code": int code, "message": String message}}
        }) {
      throw SendFailure("$code $message");
    }
    throw SendFailure("Unknown error: $result");
  }

  if (result.contains('result') == false) {
    throw SendFailure("Unknown error: $result");
  }

  final hash = json['result'];

  return hash;
}