cake_wallet/lib/bitcoin/pending_bitcoin_transaction.dart

54 lines
1.7 KiB
Dart
Raw Normal View History

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';
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 {
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
final WalletType type;
2020-08-25 16:32:40 +00:00
final bitcoin.Transaction _tx;
final ElectrumClient electrumClient;
2020-08-25 16:32:40 +00:00
final int amount;
final int fee;
@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);
final List<void Function(ElectrumTransactionInfo transaction)> _listeners;
2020-08-25 16:32:40 +00:00
@override
Future<void> commit() async {
await electrumClient.broadcastTransaction(transactionRaw: _tx.toHex());
2020-08-25 16:32:40 +00:00
_listeners?.forEach((listener) => listener(transactionInfo()));
}
void addListener(
void Function(ElectrumTransactionInfo transaction) listener) =>
2020-08-25 16:32:40 +00:00
_listeners.add(listener);
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,
confirmations: 0,
fee: fee);
2020-08-25 16:32:40 +00:00
}