2020-08-25 16:32:40 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
|
|
|
|
import 'package:cake_wallet/core/pending_transaction.dart';
|
|
|
|
import 'package:cake_wallet/bitcoin/electrum.dart';
|
2021-05-07 07:36:38 +00:00
|
|
|
import 'package:cake_wallet/bitcoin/bitcoin_amount_format.dart';
|
|
|
|
import 'package:cake_wallet/bitcoin/electrum_transaction_info.dart';
|
|
|
|
import 'package:cake_wallet/entities/transaction_direction.dart';
|
|
|
|
import 'package:cake_wallet/entities/wallet_type.dart';
|
2020-08-25 16:32:40 +00:00
|
|
|
|
|
|
|
class PendingBitcoinTransaction with PendingTransaction {
|
2021-05-07 07:36:38 +00:00
|
|
|
PendingBitcoinTransaction(this._tx, this.type,
|
|
|
|
{@required this.electrumClient,
|
|
|
|
@required this.amount,
|
|
|
|
@required this.fee})
|
|
|
|
: _listeners = <void Function(ElectrumTransactionInfo transaction)>[];
|
2020-08-25 16:32:40 +00:00
|
|
|
|
2021-05-07 07:36:38 +00:00
|
|
|
final WalletType type;
|
2020-08-25 16:32:40 +00:00
|
|
|
final bitcoin.Transaction _tx;
|
2021-05-07 07:36:38 +00:00
|
|
|
final ElectrumClient electrumClient;
|
2020-08-25 16:32:40 +00:00
|
|
|
final int amount;
|
|
|
|
final int fee;
|
|
|
|
|
2020-12-16 19:16:47 +00:00
|
|
|
@override
|
2020-08-25 16:32:40 +00:00
|
|
|
String get id => _tx.getId();
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get amountFormatted => bitcoinAmountToString(amount: amount);
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get feeFormatted => bitcoinAmountToString(amount: fee);
|
|
|
|
|
2021-05-07 07:36:38 +00:00
|
|
|
final List<void Function(ElectrumTransactionInfo transaction)> _listeners;
|
2020-08-25 16:32:40 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> commit() async {
|
2021-05-07 07:36:38 +00:00
|
|
|
await electrumClient.broadcastTransaction(transactionRaw: _tx.toHex());
|
2020-08-25 16:32:40 +00:00
|
|
|
_listeners?.forEach((listener) => listener(transactionInfo()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void addListener(
|
2021-05-07 07:36:38 +00:00
|
|
|
void Function(ElectrumTransactionInfo transaction) listener) =>
|
2020-08-25 16:32:40 +00:00
|
|
|
_listeners.add(listener);
|
|
|
|
|
2021-05-07 07:36:38 +00:00
|
|
|
ElectrumTransactionInfo transactionInfo() => ElectrumTransactionInfo(type,
|
2020-08-25 16:32:40 +00:00
|
|
|
id: id,
|
|
|
|
height: 0,
|
|
|
|
amount: amount,
|
|
|
|
direction: TransactionDirection.outgoing,
|
|
|
|
date: DateTime.now(),
|
|
|
|
isPending: true,
|
2021-05-07 07:36:38 +00:00
|
|
|
confirmations: 0,
|
|
|
|
fee: fee);
|
2020-08-25 16:32:40 +00:00
|
|
|
}
|