Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mocodesmo committed Apr 30, 2024
1 parent fae6113 commit a92a547
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 107 deletions.
4 changes: 2 additions & 2 deletions lib/_pkg/wallet/lwk/create.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class LWKCreate {

print('----load wallet: ' + wallet.id);

final w = await lwk.Wallet.create(
final w = await lwk.Wallet.init(
network: network,
dbPath: dbDir,
dbpath: dbDir,
descriptor: descriptor,
);

Expand Down
4 changes: 2 additions & 2 deletions lib/_pkg/wallet/lwk/sensitive_create.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ class LWKSensitiveCreate {
mnemonic: seed.mnemonic,
);

final w = await lwk.Wallet.create(
final w = await lwk.Wallet.init(
network: network,
dbPath: dbDir,
dbpath: dbDir,
descriptor: descriptor,
);

Expand Down
6 changes: 4 additions & 2 deletions lib/_pkg/wallet/lwk/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,10 @@ class LWKTransactions {
final (blockchain, err) = _networkRepository.liquidUrl;
if (err != null) throw err;

final txid =
await lwk.broadcast(electrumUrl: blockchain!, txBytes: txBytes);
final txid = await lwk.Wallet.broadcastTx(
electrumUrl: blockchain!,
txBytes: txBytes,
);
final newTx = transaction.copyWith(
txid: txid,
broadcastTime: DateTime.now().millisecondsSinceEpoch,
Expand Down
76 changes: 41 additions & 35 deletions lib/_pkg/wallet/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -381,17 +381,20 @@ class WalletTx implements IWalletTransactions {
keyIndex: storedSwap.keyIndex,
);
swapTxs[idx] = updatedSwapTx;
final txs = wallet.transactions.toList();

if (swapTx.txid != null) {
final txs = wallet.transactions.toList();
final idx = txs.indexWhere((_) => _.txid == swapTx.txid);
final isRevSub = !swapTx.isSubmarine;
if (idx == -1) {
final newTx = Transaction(
txid: swapTx.txid!,
timestamp: DateTime.now().millisecondsSinceEpoch ~/ 1000,
swapTx: updatedSwapTx,
sent: isRevSub ? 0 : swapTx.outAmount - swapTx.totalFees()!,
isSwap: true,
received: swapTx.outAmount - (swapTx.totalFees() ?? 0),
received:
isRevSub ? (swapTx.outAmount - (swapTx.totalFees() ?? 0)) : 0,
fee: swapTx.claimFees,
);
txs.add(newTx);
Expand All @@ -404,6 +407,10 @@ class WalletTx implements IWalletTransactions {
}
}

if (swapTx.settledReverse()) {
swapTxs.removeWhere((_) => _.id == swapTx.id);
}

if (deleteIfFailed) {
final swapsToDelete = <SwapTx>[
for (final s in swapTxs)
Expand All @@ -415,44 +422,43 @@ class WalletTx implements IWalletTransactions {
swapTxs.removeWhere((_) => _.id == s.id);
}

final updatedWallet = wallet.copyWith(swaps: swapTxs);
final updatedWallet = wallet.copyWith(swaps: swapTxs, transactions: txs);

return ((wallet: updatedWallet), null);
}

Future<(({Wallet wallet, SwapTx swapsToDelete})?, Err?)> mergeSwapTxIntoTx({
required Wallet wallet,
required SwapTx swapTx,
}) async {
try {
final txs = wallet.transactions.toList();
final swaps = wallet.swaps;
final updatedSwaps = swaps.toList();
// final swapsToDelete = <SwapTx>[];

final idx = txs.indexWhere((_) => _.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;

final swapToDelete = swaps.firstWhere((_) => _.id == swapTx.id);
// swapsToDelete.add(swapToDelete);
updatedSwaps.removeWhere((_) => _.id == swapTx.id);

final updatedWallet = wallet.copyWith(
transactions: txs,
swaps: updatedSwaps,
);
// Future<(({Wallet wallet, SwapTx swapsToDelete})?, Err?)> mergeSwapTxIntoTx({
// required Wallet wallet,
// required SwapTx swapTx,
// }) async {
// try {
// // final txs = wallet.transactions.toList();
// final swaps = wallet.swaps;
// final updatedSwaps = swaps.toList();
// // final swapsToDelete = <SwapTx>[];

// // final idx = txs.indexWhere((_) => _.txid == swapTx.txid);
// // if (idx == -1) return (null, Err('No new matching tx'));

// // final newTx = txs[idx].copyWith(
// // swapTx: swapTx,
// // );
// // txs[idx] = newTx;

// final swapToDelete = swaps.firstWhere((_) => _.id == swapTx.id);
// // swapsToDelete.add(swapToDelete);
// updatedSwaps.removeWhere((_) => _.id == swapTx.id);

// final updatedWallet = wallet.copyWith(
// // transactions: txs,
// swaps: updatedSwaps,
// );

return ((wallet: updatedWallet, swapsToDelete: swapToDelete), null);
} catch (e) {
return (null, Err(e.toString()));
}
}
// return ((wallet: updatedWallet, swapsToDelete: swapToDelete), null);
// } catch (e) {
// return (null, Err(e.toString()));
// }
// }

// Future<Err?> extractTx({
// required String tx,
Expand Down
120 changes: 63 additions & 57 deletions lib/swap/bloc/watchtxs_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'dart:async';
import 'dart:convert';

import 'package:bb_mobile/_model/transaction.dart';
import 'package:bb_mobile/_model/wallet.dart';
import 'package:bb_mobile/_pkg/boltz/swap.dart';
import 'package:bb_mobile/_pkg/error.dart';
import 'package:bb_mobile/_pkg/wallet/transaction.dart';
import 'package:bb_mobile/home/bloc/home_cubit.dart';
import 'package:bb_mobile/swap/bloc/watchtxs_event.dart';
Expand Down Expand Up @@ -199,59 +199,59 @@ class WatchTxsBloc extends Bloc<WatchTxsEvent, WatchTxsState> {
// final _ = await _swapBoltz.deleteSwapSensitive(id: swapId);
// }

Future<Err?> __mergeSwapIfTxExists(
Wallet w,
SwapTx swapTx,
Emitter<WatchTxsState> emit,
) async {
await Future.delayed(200.ms);

final walletBloc = _homeCubit.state.getWalletBlocById(w.id);
final wallet = walletBloc?.state.wallet;
if (walletBloc == null || wallet == null) return Err('Wallet not found');

final (walletAndTxs, err) = await _walletTx.mergeSwapTxIntoTx(
wallet: wallet,
swapTx: swapTx,
);
if (err != null) {
emit(
state.copyWith(
errWatchingInvoice: err.toString(),
),
);

return err;
}
final updatedWallet = walletAndTxs!.wallet;
final swapToDelete = walletAndTxs.swapsToDelete;
walletBloc.add(
UpdateWallet(
updatedWallet,
updateTypes: [
UpdateWalletTypes.transactions,
UpdateWalletTypes.swaps,
],
),
);

final errDelete = await _swapBoltz.deleteSwapSensitive(id: swapToDelete.id);
if (errDelete != null) {
emit(state.copyWith(errWatchingInvoice: errDelete.toString()));
return null;
}

Future.delayed(500.ms);

emit(state.copyWith(syncWallet: updatedWallet));

Future.delayed(200.ms);

// _homeCubit.updateWalletBloc(walletBloc);
// _homeCubit.getWalletsFromStorage();

return null;
}
// Future<Err?> __mergeSwapIfTxExists(
// Wallet w,
// SwapTx swapTx,
// Emitter<WatchTxsState> emit,
// ) async {
// await Future.delayed(200.ms);

// final walletBloc = _homeCubit.state.getWalletBlocById(w.id);
// final wallet = walletBloc?.state.wallet;
// if (walletBloc == null || wallet == null) return Err('Wallet not found');

// final (walletAndTxs, err) = await _walletTx.mergeSwapTxIntoTx(
// wallet: wallet,
// swapTx: swapTx,
// );
// if (err != null) {
// emit(
// state.copyWith(
// errWatchingInvoice: err.toString(),
// ),
// );

// return err;
// }
// final updatedWallet = walletAndTxs!.wallet;
// final swapToDelete = walletAndTxs.swapsToDelete;
// walletBloc.add(
// UpdateWallet(
// updatedWallet,
// updateTypes: [
// UpdateWalletTypes.transactions,
// UpdateWalletTypes.swaps,
// ],
// ),
// );

// final errDelete = await _swapBoltz.deleteSwapSensitive(id: swapToDelete.id);
// if (errDelete != null) {
// emit(state.copyWith(errWatchingInvoice: errDelete.toString()));
// return null;
// }

// Future.delayed(500.ms);

// emit(state.copyWith(syncWallet: updatedWallet));

// Future.delayed(200.ms);

// // _homeCubit.updateWalletBloc(walletBloc);
// // _homeCubit.getWalletsFromStorage();

// return null;
// }

Future<Wallet?> __updateWalletTxs(
// Wallet wallet,
Expand Down Expand Up @@ -324,7 +324,13 @@ class WatchTxsBloc extends Bloc<WatchTxsEvent, WatchTxsState> {
return null;
}

final updatedSwap = swapTx.copyWith(txid: txid);
SwapTx updatedSwap;
try {
final json = jsonDecode(txid!) as Map<String, dynamic>;
updatedSwap = swapTx.copyWith(txid: json['id'] as String);
} catch (e) {
updatedSwap = swapTx.copyWith(txid: txid);
}

emit(
state.copyWith(
Expand Down Expand Up @@ -392,8 +398,8 @@ class WatchTxsBloc extends Bloc<WatchTxsEvent, WatchTxsState> {
__swapAlert(swapTx, wallet, emit);
final w = await __updateWalletTxs(swapTx, walletBloc, emit);
if (w == null) return;
final err = await __mergeSwapIfTxExists(w, swapTx, emit);
if (err == null) await __closeSwap(swapTx, emit);
// final err = await __mergeSwapIfTxExists(w, swapTx, emit);
await __closeSwap(swapTx, emit);
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions lib/wallet/bloc/wallet_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ class WalletBloc extends Bloc<WalletEvent, WalletState> {
if (state.wallet == null) return;
if (state.syncing) return;

final (wallet, _) = await _walletsStorageRepository.readWallet(
walletHashId: state.wallet!.id,
);
if (wallet != null)
emit(
state.copyWith(
wallet: wallet,
),
);
// final (wallet, _) = await _walletsStorageRepository.readWallet(
// walletHashId: state.wallet!.id,
// );
// if (wallet != null)
// emit(
// state.copyWith(
// wallet: wallet,
// ),
// );

emit(
state.copyWith(
Expand Down

0 comments on commit a92a547

Please sign in to comment.