Skip to content

Commit

Permalink
Fix: mergingSwapTx with Tx. All swaps cleared after settlement
Browse files Browse the repository at this point in the history
  • Loading branch information
i5hi committed Feb 28, 2024
1 parent 4962562 commit 2136f8a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
62 changes: 37 additions & 25 deletions lib/_pkg/wallet/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<SwapTx>.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;
Expand All @@ -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,
Expand Down
15 changes: 7 additions & 8 deletions lib/swap/bloc/swap_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -266,24 +266,21 @@ class SwapBloc extends Bloc<SwapEvent, SwapState> {
);

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));
}
}
}

FutureOr<void> _onUpdateOrClaimSwap(UpdateOrClaimSwap event, Emitter<SwapState> 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
Expand All @@ -295,7 +292,7 @@ class SwapBloc extends Bloc<SwapEvent, SwapState> {
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');
Expand Down Expand Up @@ -430,7 +427,9 @@ class SwapBloc extends Bloc<SwapEvent, SwapState> {
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) {
Expand Down

0 comments on commit 2136f8a

Please sign in to comment.