2021-12-24 12:52:08 +00:00
|
|
|
import 'package:cw_bitcoin/bitcoin_commit_transaction_exception.dart';
|
|
|
|
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
|
|
|
|
import 'package:cw_core/pending_transaction.dart';
|
|
|
|
import 'package:cw_bitcoin/electrum.dart';
|
|
|
|
import 'package:cw_bitcoin/bitcoin_amount_format.dart';
|
|
|
|
import 'package:cw_bitcoin/electrum_transaction_info.dart';
|
|
|
|
import 'package:cw_core/transaction_direction.dart';
|
|
|
|
import 'package:cw_core/wallet_type.dart';
|
|
|
|
|
|
|
|
class PendingBitcoinTransaction with PendingTransaction {
|
|
|
|
PendingBitcoinTransaction(this._tx, this.type,
|
2023-11-30 19:17:16 +00:00
|
|
|
{required this.electrumClient, required this.amount, required this.fee, this.networkType})
|
2021-12-24 12:52:08 +00:00
|
|
|
: _listeners = <void Function(ElectrumTransactionInfo transaction)>[];
|
|
|
|
|
|
|
|
final WalletType type;
|
|
|
|
final bitcoin.Transaction _tx;
|
|
|
|
final ElectrumClient electrumClient;
|
|
|
|
final int amount;
|
|
|
|
final int fee;
|
2023-11-30 19:17:16 +00:00
|
|
|
final bitcoin.NetworkType? networkType;
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
String get id => _tx.getId();
|
|
|
|
|
2022-07-28 17:03:16 +00:00
|
|
|
@override
|
|
|
|
String get hex => _tx.toHex();
|
|
|
|
|
2021-12-24 12:52:08 +00:00
|
|
|
@override
|
|
|
|
String get amountFormatted => bitcoinAmountToString(amount: amount);
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get feeFormatted => bitcoinAmountToString(amount: fee);
|
|
|
|
|
|
|
|
final List<void Function(ElectrumTransactionInfo transaction)> _listeners;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> commit() async {
|
2023-11-30 19:17:16 +00:00
|
|
|
final result = await electrumClient.broadcastTransaction(
|
|
|
|
transactionRaw: _tx.txHex ?? _tx.toHex(), networkType: networkType);
|
2021-12-24 12:52:08 +00:00
|
|
|
|
|
|
|
if (result.isEmpty) {
|
|
|
|
throw BitcoinCommitTransactionException();
|
|
|
|
}
|
|
|
|
|
|
|
|
_listeners?.forEach((listener) => listener(transactionInfo()));
|
|
|
|
}
|
|
|
|
|
2023-11-30 10:01:58 +00:00
|
|
|
void addListener(void Function(ElectrumTransactionInfo transaction) listener) =>
|
2021-12-24 12:52:08 +00:00
|
|
|
_listeners.add(listener);
|
|
|
|
|
|
|
|
ElectrumTransactionInfo transactionInfo() => ElectrumTransactionInfo(type,
|
|
|
|
id: id,
|
|
|
|
height: 0,
|
|
|
|
amount: amount,
|
|
|
|
direction: TransactionDirection.outgoing,
|
|
|
|
date: DateTime.now(),
|
|
|
|
isPending: true,
|
|
|
|
confirmations: 0,
|
|
|
|
fee: fee);
|
|
|
|
}
|