diff --git a/lib/_pkg/wallet/transaction.dart b/lib/_pkg/wallet/transaction.dart index 85f78162..e769dd7b 100644 --- a/lib/_pkg/wallet/transaction.dart +++ b/lib/_pkg/wallet/transaction.dart @@ -103,26 +103,26 @@ class WalletTx { (Wallet?, Err?) updateSwapTxs({required SwapTx swapTx, required Wallet wallet}) { final swaps = wallet.swaps; - final status = swapTx.status?.status; - if (status == null) return (null, Err('No status changed')); + // final status = swapTx.status?.status; + // if (status == null) return (null, Err('No status changed')); final idx = swaps.indexWhere((_) => _.id == swapTx.id); if (idx == -1) return (null, Err('No swapTx found')); final storedSwap = swaps[idx]; - final storedStatus = storedSwap.status?.status; - if (storedStatus != null && status == storedStatus) return (null, Err('No status changed')); + // final storedStatus = storedSwap.status?.status; + // if (storedStatus != null && status == storedStatus) return (null, Err('No status changed')); final swapTxs = List.from(swaps); final hasExpired = swapTx.status!.status.hasExpired; - final hasSettled = swapTx.status!.status.hasSettled; - if (hasExpired || hasSettled) { + // final hasSettled = swapTx.status!.status.hasSettled; + if (hasExpired) { swapTxs.removeAt(idx); return (wallet.copyWith(swaps: swapTxs), null); } final updatedSwapTx = storedSwap.copyWith( - txid: storedSwap.txid ?? swapTx.txid ?? storedSwap.txid, + txid: storedSwap.txid ?? swapTx.txid, keyIndex: storedSwap.keyIndex, ); swapTxs[idx] = updatedSwapTx; @@ -131,32 +131,44 @@ class WalletTx { Future<(Wallet?, Err?)> mergeSwapTxIntoTx({ required Wallet wallet, + required SwapTx swapTx, required SwapBloc swapBloc, }) async { try { final txs = wallet.transactions.toList(); final swaps = wallet.swaps; final updatedSwaps = swaps.toList(); + // final isMerged = txs.any((_) => _.swapTx != null && _.swapTx!.id == swapTx.id); + // if (isMerged) { + // final swapToDelete = swaps.firstWhere((_) => _.id == swapTx.id); + // swapBloc.add(DeleteSensitiveSwapTx(swapToDelete.id)); + // updatedSwaps.removeWhere((_) => _.id == 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); + // } + + final newTxExists = txs.any((_) => _.swapTx == null && _.txid == swapTx.txid); + if (!newTxExists) return (null, Err('No new tx exists')); + + final idx = txs.indexWhere((_) => _.swapTx == null && _.txid == swapTx.txid); + if (idx == -1) return (null, Err('No new matching tx')); + final newTx = txs[idx].copyWith( + swapTx: swapTx, + isSwap: true, + ); + txs[idx] = newTx; - for (final swap in swaps) { - if (swap.txid == null || swap.txid!.isEmpty) continue; - final newTxExists = txs.any((_) => _.swapTx == null && _.txid == swap.txid); - if (!newTxExists) continue; - - final idx = txs.indexWhere((_) => _.swapTx == null && _.txid == swap.txid); - if (idx == -1) continue; - final newTx = txs[idx].copyWith( - swapTx: swap, - isSwap: true, - ); - txs[idx] = newTx; - - final swapToDelete = swaps.firstWhere((_) => _.id == swap.id); - swapBloc.add(DeleteSensitiveSwapTx(swapToDelete.id)); - updatedSwaps.removeWhere((_) => _.id == swap.id); - } + final swapToDelete = swaps.firstWhere((_) => _.id == swapTx.id); + swapBloc.add(DeleteSensitiveSwapTx(swapToDelete.id)); + updatedSwaps.removeWhere((_) => _.id == swapTx.id); - if (swaps.length == updatedSwaps.length) return (null, Err('No changes', expected: true)); + // if (swaps.length == updatedSwaps.length) return (null, Err('No changes', expected: true)); final updatedWallet = wallet.copyWith( transactions: txs, diff --git a/lib/swap/bloc/swap_bloc.dart b/lib/swap/bloc/swap_bloc.dart index 38124937..51a0f19a 100644 --- a/lib/swap/bloc/swap_bloc.dart +++ b/lib/swap/bloc/swap_bloc.dart @@ -266,15 +266,12 @@ class SwapBloc extends Bloc { ); final close = status.status == SwapStatus.txnClaimed || - status.status == SwapStatus.swapExpired || + // status.status == SwapStatus.swapExpired || // this is not what we want for submarine. this is when we need to trigger refund status.status == SwapStatus.invoiceExpired || status.status == SwapStatus.invoiceSettled; if (close) { final updatedTxs = state.listeningTxs.where((_) => _.id != id).toList(); emit(state.copyWith(listeningTxs: updatedTxs)); - // final boltzWatcher = state.boltzWatcher!; - // final errClose = swapBoltz.closeSwapWatcher([id]); - // emit(state.copyWith(errWatchingInvoice: errClose.toString())); } add(UpdateOrClaimSwap(walletBloc: walletBloc, swapTx: tx)); } @@ -282,8 +279,8 @@ class SwapBloc extends Bloc { } FutureOr _onUpdateOrClaimSwap(UpdateOrClaimSwap event, Emitter emit) async { - final walletBloc = homeCubit.state.getWalletBloc(event.walletBloc.state.wallet!); - if (walletBloc == null) return; + final walletBloc = event.walletBloc; + // if (walletBloc == null) return; print('::: 1'); // final status = event.status!; // not required since swapTx is updated with latest status @@ -295,7 +292,7 @@ class SwapBloc extends Bloc { final wallet = walletBloc.state.wallet; if (wallet == null) return; final (updatedWallet, err) = - await walletTransaction.mergeSwapTxIntoTx(wallet: wallet, swapBloc: this); + await walletTransaction.mergeSwapTxIntoTx(wallet: wallet, swapTx: swapTx, swapBloc: this); if (err != null) { print('::: 2-1'); print('$err'); @@ -430,7 +427,9 @@ class SwapBloc extends Bloc { emit(state.copyWith(claimedSwapTxs: [...state.claimedSwapTxs, swapTx])); print('::: 24'); - final tx = swapTx.copyWith(txid: txid); + final tx = swapTx.copyWith( + txid: txid, + ); final (updatedWallet, err1) = walletTransaction.updateSwapTxs(swapTx: tx, wallet: walletBloc.state.wallet!); if (err1 != null) {