Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mocodesmo committed Feb 22, 2024
1 parent 6ba30fc commit bbe181b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 67 deletions.
45 changes: 44 additions & 1 deletion lib/_pkg/wallet/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down Expand Up @@ -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;
Expand Down
48 changes: 25 additions & 23 deletions lib/swap/bloc/swap_bloc.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -132,13 +130,13 @@ class SwapBloc extends Bloc<SwapEvent, SwapState> {
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));
}
Expand Down Expand Up @@ -207,17 +205,17 @@ class SwapBloc extends Bloc<SwapEvent, SwapState> {
// ),
// );

// await Future.delayed(500.ms);

// homeCubit?.updateSelectedWallet(walletBloc);

emit(
state.copyWith(
claimingSwapSwap: false,
errClaimingSwap: '',
),
);

// await Future.delayed(1000.ms);

// homeCubit?.updateSelectedWallet(walletBloc);

// await Future.delayed(500.ms);

// walletBloc.add(ListTransactions());
Expand Down Expand Up @@ -273,27 +271,31 @@ class SwapBloc extends Bloc<SwapEvent, SwapState> {
.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) {
Expand Down
6 changes: 5 additions & 1 deletion lib/wallet/bloc/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ 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);

final String txid;
final SwapTx swap;
}

class MergeSwapIntoTx extends WalletEvent {}

class ListenToSwapTxs extends WalletEvent {}
71 changes: 29 additions & 42 deletions lib/wallet/bloc/wallet_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class WalletBloc extends Bloc<WalletEvent, WalletState> {
on<GetFirstAddress>(_getFirstAddress);
on<UpdateUtxos>(_updateUtxos);
on<UpdateSwapTxWithTxId>(_onUpdateSwapWithTxId);
on<MergeSwapIntoTx>(_mergeSwapIntoTx);
on<ListenToSwapTxs>(_listenToSwapTxs);

add(LoadWallet(saveDir));
}
Expand Down Expand Up @@ -339,10 +341,10 @@ class WalletBloc extends Bloc<WalletEvent, WalletState> {
),
);

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<WalletState> emit) async {}
Expand Down Expand Up @@ -407,15 +409,21 @@ class WalletBloc extends Bloc<WalletEvent, WalletState> {
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)
Expand Down Expand Up @@ -467,7 +475,7 @@ class WalletBloc extends Bloc<WalletEvent, WalletState> {
await Future.delayed(500.ms);
}

void _listenToSwapTxs() async {
void _listenToSwapTxs(ListenToSwapTxs event, Emitter<WalletState> emit) async {
final swapTxs = state.allSwapTxs();
for (final tx in swapTxs) {
if (tx.swapTx == null) continue;
Expand All @@ -485,41 +493,20 @@ class WalletBloc extends Bloc<WalletEvent, WalletState> {
}
}

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<WalletState> 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<WalletState> emit) async {
Expand All @@ -530,12 +517,12 @@ class WalletBloc extends Bloc<WalletEvent, WalletState> {
UpdateWallet(
updatedWallet,
saveToStorage: fromStorage,
updateTypes: [UpdateWalletTypes.transactions],
updateTypes: [UpdateWalletTypes.swaps],
),
);

await Future.delayed(500.ms);

add(ListTransactions());
// add(ListTransactions());
}
}

0 comments on commit bbe181b

Please sign in to comment.