From bbe181b59f9d2a5f5f5a91b4f5cd7d21f1c8be60 Mon Sep 17 00:00:00 2001 From: mocodesmo Date: Thu, 22 Feb 2024 17:44:54 +0530 Subject: [PATCH] fixes --- lib/_pkg/wallet/transaction.dart | 45 +++++++++++++++++++- lib/swap/bloc/swap_bloc.dart | 48 ++++++++++----------- lib/wallet/bloc/event.dart | 6 ++- lib/wallet/bloc/wallet_bloc.dart | 71 +++++++++++++------------------- 4 files changed, 103 insertions(+), 67 deletions(-) diff --git a/lib/_pkg/wallet/transaction.dart b/lib/_pkg/wallet/transaction.dart index 9daaf4fa..38765615 100644 --- a/lib/_pkg/wallet/transaction.dart +++ b/lib/_pkg/wallet/transaction.dart @@ -92,13 +92,53 @@ class WalletTx { wallet, Err( e.message, - title: 'Error occurred while adding unsigned transaction', + title: 'Error occurred while adding swap transaction', solution: 'Please try again.', ) ); // returning original wallet in case of error } } + Future<(Wallet?, Err?)> mergeSwapTxIntoTx({ + required Wallet wallet, + }) async { + try { + final txs = wallet.transactions.toList(); + final swaps = wallet.swaps; + final updatedSwaps = swaps.toList(); + + for (final swap in swaps) { + if (swap.swapTx?.txid == null || swap.swapTx!.txid!.isEmpty) continue; + final newTxExists = txs.any((_) => _.swapTx == null && _.txid == swap.swapTx!.txid); + if (!newTxExists) continue; + + final idx = txs.indexWhere((_) => _.swapTx == null && _.txid == swap.swapTx!.txid); + if (idx == -1) continue; + final newTx = txs[idx].copyWith( + swapTx: swap.swapTx, + isSwap: true, + swapIndex: swap.swapIndex, + ); + txs[idx] = newTx; + + // final swapToDelete = swaps.firstWhere((_) => _.swapTx!.id == swap.swapTx!.id); + // swapBloc.add(DeleteSensitiveSwapTx(swapToDelete.swapTx!.id)); + updatedSwaps.removeWhere((_) => _.swapTx!.id == swap.swapTx!.id); + } + + if (swaps.length == updatedSwaps.length) return (null, Err('No changes', expected: true)); + + final updatedWallet = wallet.copyWith( + transactions: txs, + swaps: updatedSwaps, + ); + + return (updatedWallet, null); + } catch (e) { + return (null, Err(e.toString())); + } + } + // // THIS NEEDS WORK // @@ -140,6 +180,9 @@ class WalletTx { bdkTx: tx, rbfEnabled: storedTx?.rbfEnabled ?? false, outAddrs: storedTx?.outAddrs ?? [], + swapTx: storedTx?.swapTx, + swapIndex: storedTx?.swapIndex, + isSwap: storedTx?.isSwap ?? false, ); // var outAddrs; // var inAddres; diff --git a/lib/swap/bloc/swap_bloc.dart b/lib/swap/bloc/swap_bloc.dart index 27a3a8a1..343594fa 100644 --- a/lib/swap/bloc/swap_bloc.dart +++ b/lib/swap/bloc/swap_bloc.dart @@ -1,5 +1,3 @@ -import 'dart:async'; - import 'package:bb_mobile/_model/transaction.dart'; import 'package:bb_mobile/_pkg/boltz/swap.dart'; import 'package:bb_mobile/_pkg/consts/configs.dart'; @@ -132,13 +130,13 @@ class SwapBloc extends Bloc { walletBloc.add( UpdateWallet( updatedWallet, - updateTypes: [UpdateWalletTypes.transactions], + updateTypes: [UpdateWalletTypes.swaps], ), ); - await Future.delayed(100.ms); + // await Future.delayed(100.ms); - homeCubit?.updateSelectedWallet(walletBloc); + // homeCubit?.updateSelectedWallet(walletBloc); add(WatchInvoiceStatus(tx: tx, walletBloc: walletBloc)); } @@ -207,10 +205,6 @@ class SwapBloc extends Bloc { // ), // ); - // await Future.delayed(500.ms); - - // homeCubit?.updateSelectedWallet(walletBloc); - emit( state.copyWith( claimingSwapSwap: false, @@ -218,6 +212,10 @@ class SwapBloc extends Bloc { ), ); + // await Future.delayed(1000.ms); + + // homeCubit?.updateSelectedWallet(walletBloc); + // await Future.delayed(500.ms); // walletBloc.add(ListTransactions()); @@ -273,27 +271,31 @@ class SwapBloc extends Bloc { .toList(), ), ); - final wallet = walletBloc.state.wallet; - if (wallet == null) return; + // final wallet = walletBloc.state.wallet; + // if (wallet == null) return; final close = status.status == SwapStatus.txnClaimed || status.status == SwapStatus.swapExpired || status.status == SwapStatus.invoiceExpired; - final updatedWallet = wallet.updateSwapTxs(tx); - - walletBloc.add( - UpdateWallet( - updatedWallet, - updateTypes: [UpdateWalletTypes.transactions], - ), - ); - await Future.delayed(100.ms); - homeCubit?.updateSelectedWallet(walletBloc); + // await Future.delayed(100.ms); + // homeCubit?.updateSelectedWallet(walletBloc); final canClaim = status.status.canClaim; - if (canClaim) add(ClaimSwap(walletBloc, tx)); - + if (canClaim) + add(ClaimSwap(walletBloc, tx)); + else { + await Future.delayed(1000.ms); + final wallet = homeCubit?.state.getWalletBloc(walletBloc.state.wallet!); + if (wallet == null) return; + final updatedWallet = wallet.state.wallet!.updateSwapTxs(tx); + walletBloc.add( + UpdateWallet( + updatedWallet, + updateTypes: [UpdateWalletTypes.swaps], + ), + ); + } if (close) { final errClose = swapBoltz.closeStream(id); if (errClose != null) { diff --git a/lib/wallet/bloc/event.dart b/lib/wallet/bloc/event.dart index 074bc7b6..130495ad 100644 --- a/lib/wallet/bloc/event.dart +++ b/lib/wallet/bloc/event.dart @@ -34,7 +34,7 @@ class GetFirstAddress extends WalletEvent {} class GetNewAddress extends WalletEvent {} -enum UpdateWalletTypes { load, balance, transactions, addresses, settings, utxos } +enum UpdateWalletTypes { load, balance, transactions, swaps, addresses, settings, utxos } class UpdateSwapTxWithTxId extends WalletEvent { UpdateSwapTxWithTxId(this.txid, this.swap); @@ -42,3 +42,7 @@ class UpdateSwapTxWithTxId extends WalletEvent { final String txid; final SwapTx swap; } + +class MergeSwapIntoTx extends WalletEvent {} + +class ListenToSwapTxs extends WalletEvent {} diff --git a/lib/wallet/bloc/wallet_bloc.dart b/lib/wallet/bloc/wallet_bloc.dart index 2446d72b..72ad6d68 100644 --- a/lib/wallet/bloc/wallet_bloc.dart +++ b/lib/wallet/bloc/wallet_bloc.dart @@ -54,6 +54,8 @@ class WalletBloc extends Bloc { on(_getFirstAddress); on(_updateUtxos); on(_onUpdateSwapWithTxId); + on(_mergeSwapIntoTx); + on(_listenToSwapTxs); add(LoadWallet(saveDir)); } @@ -339,10 +341,10 @@ class WalletBloc extends Bloc { ), ); - await Future.delayed(100.ms); - _mergeSwapIntoTx(); - await Future.delayed(100.ms); - _listenToSwapTxs(); + await Future.delayed(1000.ms); + add(MergeSwapIntoTx()); + // await Future.delayed(100.ms); + add(ListenToSwapTxs()); } void _updateUtxos(UpdateUtxos event, Emitter emit) async {} @@ -407,15 +409,21 @@ class WalletBloc extends Bloc { storageWallet = storageWallet!.copyWith( transactions: eventWallet.transactions, ); + if (eventWallet.unsignedTxs.isNotEmpty) storageWallet = storageWallet!.copyWith( unsignedTxs: eventWallet.unsignedTxs, ); - if (eventWallet.swaps.isNotEmpty) + + case UpdateWalletTypes.swaps: + if (eventWallet.swaps.isNotEmpty) { + final trace = StackTrace.current; + print(trace); storageWallet = storageWallet!.copyWith( swaps: eventWallet.swaps, swapTxCount: eventWallet.swapTxCount, ); + } case UpdateWalletTypes.addresses: if (eventWallet.myAddressBook.isNotEmpty) @@ -467,7 +475,7 @@ class WalletBloc extends Bloc { await Future.delayed(500.ms); } - void _listenToSwapTxs() async { + void _listenToSwapTxs(ListenToSwapTxs event, Emitter emit) async { final swapTxs = state.allSwapTxs(); for (final tx in swapTxs) { if (tx.swapTx == null) continue; @@ -485,41 +493,20 @@ class WalletBloc extends Bloc { } } - void _mergeSwapIntoTx() { - final txs = state.wallet?.transactions.toList() ?? []; - final swaps = state.wallet?.swaps ?? []; - final updatedSwaps = swaps.toList(); - - for (final swap in swaps) { - if (swap.swapTx?.txid == null || swap.swapTx!.txid!.isEmpty) continue; - final newTxExists = txs.any((_) => _.swapTx == null && _.txid == swap.swapTx!.txid); - if (!newTxExists) continue; - - final idx = txs.indexWhere((_) => _.swapTx == null && _.txid == swap.swapTx!.txid); - if (idx == -1) continue; - final newTx = txs[idx].copyWith( - swapTx: swap.swapTx, - isSwap: true, - swapIndex: swap.swapIndex, - ); - txs[idx] = newTx; - - final swapToDelete = swaps.firstWhere((_) => _.swapTx!.id == swap.swapTx!.id); - swapBloc.add(DeleteSensitiveSwapTx(swapToDelete.swapTx!.id)); - updatedSwaps.removeWhere((_) => _.swapTx!.id == swap.swapTx!.id); - - final updatedWallet = state.wallet!.copyWith( - transactions: txs, - swaps: updatedSwaps, - ); - add( - UpdateWallet( - updatedWallet, - saveToStorage: fromStorage, - updateTypes: [UpdateWalletTypes.transactions], - ), - ); + void _mergeSwapIntoTx(MergeSwapIntoTx event, Emitter emit) async { + final (updatedWallet, err) = await walletTransaction.mergeSwapTxIntoTx(wallet: state.wallet!); + if (err != null) { + emit(state.copyWith(errLoadingWallet: err.toString())); + return; } + + add( + UpdateWallet( + updatedWallet!, + saveToStorage: fromStorage, + updateTypes: [UpdateWalletTypes.transactions, UpdateWalletTypes.swaps], + ), + ); } void _onUpdateSwapWithTxId(UpdateSwapTxWithTxId event, Emitter emit) async { @@ -530,12 +517,12 @@ class WalletBloc extends Bloc { UpdateWallet( updatedWallet, saveToStorage: fromStorage, - updateTypes: [UpdateWalletTypes.transactions], + updateTypes: [UpdateWalletTypes.swaps], ), ); await Future.delayed(500.ms); - add(ListTransactions()); + // add(ListTransactions()); } }