Skip to content

Commit

Permalink
send - remove swap cubit dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mocodesmo committed May 3, 2024
1 parent 6a53fa9 commit dc83752
Show file tree
Hide file tree
Showing 9 changed files with 457 additions and 290 deletions.
144 changes: 97 additions & 47 deletions lib/receive/receive_page.dart

Large diffs are not rendered by default.

150 changes: 76 additions & 74 deletions lib/send/bloc/send_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import 'dart:async';

import 'package:bb_mobile/_model/address.dart';
import 'package:bb_mobile/_model/transaction.dart';
import 'package:bb_mobile/_model/wallet.dart';
import 'package:bb_mobile/_pkg/barcode.dart';
import 'package:bb_mobile/_pkg/bip21.dart';
import 'package:bb_mobile/_pkg/boltz/swap.dart';
import 'package:bb_mobile/_pkg/file_storage.dart';
import 'package:bb_mobile/_pkg/wallet/transaction.dart';
import 'package:bb_mobile/currency/bloc/currency_cubit.dart';
import 'package:bb_mobile/home/bloc/home_cubit.dart';
import 'package:bb_mobile/network/bloc/network_cubit.dart';
import 'package:bb_mobile/send/bloc/state.dart';
import 'package:bb_mobile/swap/bloc/swap_cubit.dart';
import 'package:bb_mobile/swap/bloc/swap_state.dart';
import 'package:bb_mobile/wallet/bloc/event.dart';
import 'package:bb_mobile/wallet/bloc/wallet_bloc.dart';
import 'package:flutter_animate/flutter_animate.dart';
Expand All @@ -25,51 +25,59 @@ class SendCubit extends Cubit<SendState> {
required FileStorage fileStorage,
required NetworkCubit networkCubit,
required this.currencyCubit,
required SwapCubit swapCubit,
required bool openScanner,
required HomeCubit homeCubit,
required bool defaultRBF,
required SwapBoltz swapBoltz,
}) : _homeCubit = homeCubit,
_networkCubit = networkCubit,
_walletTx = walletTx,
_fileStorage = fileStorage,
_barcode = barcode,
super(SendState(swapCubit: swapCubit, selectedWalletBloc: walletBloc)) {
_swapBoltz = swapBoltz,
super(SendState(selectedWalletBloc: walletBloc)) {
emit(
state.copyWith(
disableRBF: !defaultRBF,
selectedWalletBloc: walletBloc,
),
);

_currencyCubitSub = currencyCubit.stream.listen((_) {
_updateShowSend();
});

_swapCubitSub = state.swapCubit.stream.listen(swapCubitStateChanged);

if (openScanner) scanAddress();
}

final Barcode _barcode;
final FileStorage _fileStorage;
final WalletTx _walletTx;
final SwapBoltz _swapBoltz;

final NetworkCubit _networkCubit;
final CurrencyCubit currencyCubit;
final HomeCubit _homeCubit;

late StreamSubscription _currencyCubitSub;
late StreamSubscription _swapCubitSub;
// late StreamSubscription _currencyCubitSub;
// late StreamSubscription _swapCubitSub;

// void swapCubitStateChanged(SwapState swapState) {
// final amount = currencyCubit.state.amount;
// final inv = state.invoice;
// if (inv != null &&
// inv.invoice == state.address &&
// inv.getAmount() != amount) {
// final amt = state.invoice!.getAmount();
// currencyCubit.updateAmountDirect(amt);
// updateShowSend();
// }
// }

void watchCurrency() async {}
// void watchCurrency() async {}

void updateWalletBloc(WalletBloc walletBloc) {
emit(state.copyWith(selectedWalletBloc: walletBloc));
_updateShowSend(force: true);
updateShowSend(force: true);
}

void _updateShowSend({bool force = false}) {
void updateShowSend({bool force = false}) {
final amount = currencyCubit.state.amount;
emit(state.copyWith(errSending: ''));
if (amount == 0) {
Expand Down Expand Up @@ -130,18 +138,6 @@ class SendCubit extends Cubit<SendState> {
);
}

void swapCubitStateChanged(SwapState swapState) {
final amount = currencyCubit.state.amount;
final inv = swapState.invoice;
if (inv != null &&
inv.invoice == state.address &&
inv.getAmount() != amount) {
final amt = swapState.invoice!.getAmount();
currencyCubit.updateAmountDirect(amt);
_updateShowSend();
}
}

void updateAddress(String address) async {
try {
if (address.startsWith('bitcoin')) {
Expand All @@ -162,7 +158,14 @@ class SendCubit extends Cubit<SendState> {
} else if (address.startsWith('ln')) {
if (state.checkIfMainWalletSelected()) {
emit(state.copyWith(address: address));
state.swapCubit.decodeInvoice(address);
// state.swapCubit.decodeInvoice(address);
final (inv, errInv) =
await _swapBoltz.decodeInvoice(invoice: address);
if (errInv != null) {
emit(state.copyWith(errScanningAddress: errInv.toString()));
return;
}
emit(state.copyWith(invoice: inv));
} else {
emit(
state.copyWith(
Expand Down Expand Up @@ -226,7 +229,7 @@ class SendCubit extends Cubit<SendState> {
),
);
currencyCubit.updateAmountDirect(sendAll ? balance : 0);
_updateShowSend();
updateShowSend();
}

void utxoSelected(UTXO utxo) {
Expand All @@ -239,7 +242,7 @@ class SendCubit extends Cubit<SendState> {

emit(state.copyWith(selectedUtxos: selectedUtxos));

_updateShowSend();
updateShowSend();
}

void clearSelectedUtxos() {
Expand Down Expand Up @@ -295,49 +298,52 @@ class SendCubit extends Cubit<SendState> {
emit(state.copyWith(downloadingFile: false, downloaded: true));
}

void confirmClickedd({required int networkFees}) async {
if (state.sending) return;
if (state.selectedWalletBloc == null) return;
void confirmLn(SwapTx swapTx) {}

final isLn = state.isLnInvoice();
if (isLn) {
final walletId = state.selectedWalletBloc!.state.wallet!;
final isTesnet = _networkCubit.state.testnet;
final networkUrl = _networkCubit.state.getNetworkUrl();

await state.swapCubit.createSubSwapForSend(
wallet: walletId,
invoice: state.address,
amount: currencyCubit.state.amount,
isTestnet: isTesnet,
networkUrl: networkUrl,
);
await Future.delayed(const Duration(milliseconds: 500));
final walletBloc = _homeCubit.state.getWalletBlocById(walletId.id);
emit(state.copyWith(selectedWalletBloc: walletBloc));
await Future.delayed(const Duration(milliseconds: 50));
void sendLn() {}

if (state.swapCubit.state.invoice == null ||
state.swapCubit.state.swapTx == null) {
emit(
state.copyWith(
sending: false,
errSending: state.swapCubit.state.errCreatingSwapInv,
),
);
return;
}
}
void confirmClickedd({required int networkFees, SwapTx? swaptx}) async {
if (state.sending) return;
if (state.selectedWalletBloc == null) return;

final address =
isLn ? state.swapCubit.state.swapTx?.scriptAddress : state.address;
// final isLn = state.isLnInvoice();
// if (isLn) {
// final walletId = state.selectedWalletBloc!.state.wallet!;
// final isTesnet = _networkCubit.state.testnet;
// final networkUrl = _networkCubit.state.getNetworkUrl();

// await state.swapCubit.createSubSwapForSend(
// wallet: walletId,
// invoice: state.address,
// amount: currencyCubit.state.amount,
// isTestnet: isTesnet,
// networkUrl: networkUrl,
// );
// await Future.delayed(const Duration(milliseconds: 500));
// final walletBloc = _homeCubit.state.getWalletBlocById(walletId.id);
// emit(state.copyWith(selectedWalletBloc: walletBloc));
// await Future.delayed(const Duration(milliseconds: 50));

// if (state.swapCubit.state.invoice == null ||
// state.swapCubit.state.swapTx == null) {
// emit(
// state.copyWith(
// sending: false,
// errSending: state.swapCubit.state.errCreatingSwapInv,
// ),
// );
// return;
// }
// }

final address = swaptx != null ? swaptx.scriptAddress : state.address;
final fee = networkFees;
// isLn
// ? networkFeesCubit.state.feesList![0]
// : networkFeesCubit.state.feesList![networkFeesCubit.state.selectedFeesOption];

final bool enableRbf;
if (isLn)
if (swaptx != null)
enableRbf = false;
else
enableRbf = !state.disableRBF;
Expand All @@ -349,10 +355,8 @@ class SendCubit extends Cubit<SendState> {
final (buildResp, err) = await _walletTx.buildTx(
wallet: localWallet!,
isManualSend: state.selectedUtxos.isNotEmpty,
address: address!,
amount: isLn
? state.swapCubit.state.swapTx!.outAmount
: currencyCubit.state.amount,
address: address,
amount: swaptx != null ? swaptx.outAmount : currencyCubit.state.amount,
sendAllCoin: state.sendAllCoin,
feeRate: fee.toDouble(),
enableRbf: enableRbf,
Expand Down Expand Up @@ -398,7 +402,7 @@ class SendCubit extends Cubit<SendState> {
);
}

void sendClicked() async {
void sendClicked({SwapTx? swaptx}) async {
if (state.selectedWalletBloc == null) return;
emit(state.copyWith(sending: true, errSending: ''));

Expand All @@ -415,12 +419,12 @@ class SendCubit extends Cubit<SendState> {

final (wallet, txid) = wtxid!;

final isLn = state.isLnInvoice();
// final isLn = state.isLnInvoice();

if (isLn) {
if (swaptx != null) {
final (updatedWalletWithTxid, err2) = await _walletTx.addSwapTxToWallet(
wallet: wallet,
swapTx: state.swapCubit.state.swapTx!.copyWith(txid: txid),
swapTx: swaptx.copyWith(txid: txid),
);

if (err2 != null) {
Expand Down Expand Up @@ -456,8 +460,6 @@ class SendCubit extends Cubit<SendState> {
}

void dispose() {
_currencyCubitSub.cancel();
_swapCubitSub.cancel();
super.close();
}
}
23 changes: 12 additions & 11 deletions lib/send/bloc/state.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:bb_mobile/_model/address.dart';
import 'package:bb_mobile/_model/transaction.dart';
import 'package:bb_mobile/swap/bloc/swap_cubit.dart';
import 'package:bb_mobile/wallet/bloc/wallet_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

Expand Down Expand Up @@ -30,7 +29,8 @@ class SendState with _$SendState {
String? psbtSigned,
int? psbtSignedFeeAmount,
WalletBloc? selectedWalletBloc,
required SwapCubit swapCubit,
Invoice? invoice,
// required SwapCubit swapCubit,
}) = _SendState;
const SendState._();

Expand All @@ -57,16 +57,16 @@ class SendState with _$SendState {
return 'Selected ${selectedUtxos.length} addresses';
}

bool generatingSwap() => swapCubit.state.generatingSwapInv;
// bool generatingSwap() => swapCubit.state.generatingSwapInv;

bool loadingWithSwap() {
return generatingSwap() || sending || downloadingFile;
}
// bool loadingWithSwap() {
// return generatingSwap() || sending || downloadingFile;
// }

String errWithSwap() {
if (swapCubit.state.errCreatingInvoice.isNotEmpty) {
return swapCubit.state.errCreatingInvoice;
}
String errors() {
// if (swapCubit.state.errCreatingInvoice.isNotEmpty) {
// return swapCubit.state.errCreatingInvoice;
// }
if (errSending.isNotEmpty) {
return errSending;
}
Expand All @@ -81,5 +81,6 @@ class SendState with _$SendState {
return showSendButton;
}

bool checkIfMainWalletSelected() => selectedWalletBloc?.state.wallet?.mainWallet ?? false;
bool checkIfMainWalletSelected() =>
selectedWalletBloc?.state.wallet?.mainWallet ?? false;
}
Loading

0 comments on commit dc83752

Please sign in to comment.