From 929df9a6a97feae3064c04f8e49be17ed0f08796 Mon Sep 17 00:00:00 2001 From: mocodesmo Date: Mon, 12 Feb 2024 12:33:58 +0530 Subject: [PATCH] receive - ln swap - bloc updates --- .vscode/settings.json | 14 +++++---- lib/_model/transaction.dart | 2 +- lib/_model/transaction.freezed.dart | 29 ++++++++++--------- lib/_model/transaction.g.dart | 2 +- lib/receive/bloc/receive_cubit.dart | 33 +++++++++++++++++---- lib/receive/bloc/state.dart | 4 +-- lib/receive/bloc/state.freezed.dart | 45 ++++++++++++++++++++++++----- 7 files changed, 91 insertions(+), 38 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index fc3a28fc..15a7a7bb 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -24,10 +24,10 @@ "dart.lineLength": 100, "files.exclude": { "**/.metadata": true, - // "**/.packages": true, - // "**/pubspec.lock": true, - // "**/.flutter-plugins": true, - // "**/.dart_tool": true, + "**/.packages": true, + "**/pubspec.lock": true, + "**/.flutter-plugins": true, + "**/.dart_tool": true, "**/.flutter-plugins-dependencies": true, "**/generated_plugin_registrant.dart": true, "**/CHANGELOG.md": true, @@ -45,7 +45,7 @@ ".idea": true, ".gitignore": true, // - // "**/analysis_options.yaml": true, + "**/analysis_options.yaml": true, "build.yaml": true, "**/build": true, "**/assets": true, @@ -62,8 +62,10 @@ "ios": true, // // - // "**/pubspec.yaml": true, + "**/pubspec.yaml": true, // // + "**/test": true, + "integration_test": true, }, } \ No newline at end of file diff --git a/lib/_model/transaction.dart b/lib/_model/transaction.dart index 441b0368..669454b3 100644 --- a/lib/_model/transaction.dart +++ b/lib/_model/transaction.dart @@ -33,7 +33,7 @@ class Transaction with _$Transaction { ) bdk.TransactionDetails? bdkTx, Wallet? wallet, - @Default(false) isSwap, + @Default(false) bool isSwap, int? swapIndex, SwapTx? swapTx, }) = _Transaction; diff --git a/lib/_model/transaction.freezed.dart b/lib/_model/transaction.freezed.dart index 8d6a55f8..46073d0b 100644 --- a/lib/_model/transaction.freezed.dart +++ b/lib/_model/transaction.freezed.dart @@ -37,7 +37,7 @@ mixin _$Transaction { @JsonKey(includeFromJson: false, includeToJson: false) bdk.TransactionDetails? get bdkTx => throw _privateConstructorUsedError; Wallet? get wallet => throw _privateConstructorUsedError; - dynamic get isSwap => throw _privateConstructorUsedError; + bool get isSwap => throw _privateConstructorUsedError; int? get swapIndex => throw _privateConstructorUsedError; SwapTx? get swapTx => throw _privateConstructorUsedError; @@ -70,7 +70,7 @@ abstract class $TransactionCopyWith<$Res> { @JsonKey(includeFromJson: false, includeToJson: false) bdk.TransactionDetails? bdkTx, Wallet? wallet, - dynamic isSwap, + bool isSwap, int? swapIndex, SwapTx? swapTx}); @@ -106,7 +106,7 @@ class _$TransactionCopyWithImpl<$Res, $Val extends Transaction> Object? outAddrs = null, Object? bdkTx = freezed, Object? wallet = freezed, - Object? isSwap = freezed, + Object? isSwap = null, Object? swapIndex = freezed, Object? swapTx = freezed, }) { @@ -171,10 +171,10 @@ class _$TransactionCopyWithImpl<$Res, $Val extends Transaction> ? _value.wallet : wallet // ignore: cast_nullable_to_non_nullable as Wallet?, - isSwap: freezed == isSwap + isSwap: null == isSwap ? _value.isSwap : isSwap // ignore: cast_nullable_to_non_nullable - as dynamic, + as bool, swapIndex: freezed == swapIndex ? _value.swapIndex : swapIndex // ignore: cast_nullable_to_non_nullable @@ -236,7 +236,7 @@ abstract class _$$TransactionImplCopyWith<$Res> @JsonKey(includeFromJson: false, includeToJson: false) bdk.TransactionDetails? bdkTx, Wallet? wallet, - dynamic isSwap, + bool isSwap, int? swapIndex, SwapTx? swapTx}); @@ -272,7 +272,7 @@ class __$$TransactionImplCopyWithImpl<$Res> Object? outAddrs = null, Object? bdkTx = freezed, Object? wallet = freezed, - Object? isSwap = freezed, + Object? isSwap = null, Object? swapIndex = freezed, Object? swapTx = freezed, }) { @@ -337,7 +337,10 @@ class __$$TransactionImplCopyWithImpl<$Res> ? _value.wallet : wallet // ignore: cast_nullable_to_non_nullable as Wallet?, - isSwap: freezed == isSwap ? _value.isSwap! : isSwap, + isSwap: null == isSwap + ? _value.isSwap + : isSwap // ignore: cast_nullable_to_non_nullable + as bool, swapIndex: freezed == swapIndex ? _value.swapIndex : swapIndex // ignore: cast_nullable_to_non_nullable @@ -422,7 +425,7 @@ class _$TransactionImpl extends _Transaction { final Wallet? wallet; @override @JsonKey() - final dynamic isSwap; + final bool isSwap; @override final int? swapIndex; @override @@ -458,7 +461,7 @@ class _$TransactionImpl extends _Transaction { const DeepCollectionEquality().equals(other._outAddrs, _outAddrs) && (identical(other.bdkTx, bdkTx) || other.bdkTx == bdkTx) && (identical(other.wallet, wallet) || other.wallet == wallet) && - const DeepCollectionEquality().equals(other.isSwap, isSwap) && + (identical(other.isSwap, isSwap) || other.isSwap == isSwap) && (identical(other.swapIndex, swapIndex) || other.swapIndex == swapIndex) && (identical(other.swapTx, swapTx) || other.swapTx == swapTx)); @@ -483,7 +486,7 @@ class _$TransactionImpl extends _Transaction { const DeepCollectionEquality().hash(_outAddrs), bdkTx, wallet, - const DeepCollectionEquality().hash(isSwap), + isSwap, swapIndex, swapTx); @@ -519,7 +522,7 @@ abstract class _Transaction extends Transaction { @JsonKey(includeFromJson: false, includeToJson: false) final bdk.TransactionDetails? bdkTx, final Wallet? wallet, - final dynamic isSwap, + final bool isSwap, final int? swapIndex, final SwapTx? swapTx}) = _$TransactionImpl; const _Transaction._() : super._(); @@ -559,7 +562,7 @@ abstract class _Transaction extends Transaction { @override Wallet? get wallet; @override - dynamic get isSwap; + bool get isSwap; @override int? get swapIndex; @override diff --git a/lib/_model/transaction.g.dart b/lib/_model/transaction.g.dart index 76a61d78..3ab5f93a 100644 --- a/lib/_model/transaction.g.dart +++ b/lib/_model/transaction.g.dart @@ -27,7 +27,7 @@ _$TransactionImpl _$$TransactionImplFromJson(Map json) => wallet: json['wallet'] == null ? null : Wallet.fromJson(json['wallet'] as Map), - isSwap: json['isSwap'] ?? false, + isSwap: json['isSwap'] as bool? ?? false, swapIndex: json['swapIndex'] as int?, swapTx: json['swapTx'] == null ? null diff --git a/lib/receive/bloc/receive_cubit.dart b/lib/receive/bloc/receive_cubit.dart index 93a8315e..553c08e7 100644 --- a/lib/receive/bloc/receive_cubit.dart +++ b/lib/receive/bloc/receive_cubit.dart @@ -34,7 +34,6 @@ class ReceiveCubit extends Cubit { loadAddress(); } final SettingsCubit settingsCubit; - // final WalletBloc walletBloc; final WalletAddress walletAddress; final HiveStorage hiveStorage; final SecureStorage secureStorage; @@ -99,7 +98,7 @@ class ReceiveCubit extends Cubit { final (swap, errCreatingInv) = await swapBoltz.receive( mnemonic: seed!.mnemonic, - index: 1, + index: state.walletBloc!.state.wallet!.swapTxCount, outAmount: outAmount, network: Chain.Testnet, electrumUrl: networkCubit.state.getNetworkUrl(), @@ -144,9 +143,11 @@ class ReceiveCubit extends Cubit { return; } + final swapTxCount = updatedWallet.swapTxCount + 1; + state.walletBloc!.add( UpdateWallet( - updatedWallet, + updatedWallet.copyWith(swapTxCount: swapTxCount), updateTypes: [UpdateWalletTypes.transactions], ), ); @@ -181,6 +182,28 @@ class ReceiveCubit extends Cubit { } } + void resetToNewLnInvoice() async { + if (state.walletBloc == null) return; + + emit( + state.copyWith( + errLoadingAddress: '', + savedInvoiceAmount: 0, + creatingInvoice: true, + errCreatingInvoice: '', + defaultAddress: null, + swapTx: null, + ), + ); + currencyCubit.reset(); + } + + void loadAllSwapTxs() { + if (state.walletBloc == null) return; + final swapTxs = state.walletBloc!.state.wallet!.transactions.where((tx) => tx.isSwap).toList(); + emit(state.copyWith(swapTxs: swapTxs)); + } + void loadAddress() async { if (state.walletBloc == null) return; emit(state.copyWith(loadingAddress: true, errLoadingAddress: '')); @@ -208,11 +231,9 @@ class ReceiveCubit extends Cubit { ); } - void generateNewLightningInvoice() async {} - void generateNewAddress() async { if (state.walletType == ReceiveWalletType.lightning) { - generateNewLightningInvoice(); + resetToNewLnInvoice(); return; } diff --git a/lib/receive/bloc/state.dart b/lib/receive/bloc/state.dart index f8f09624..1a2f3ee9 100644 --- a/lib/receive/bloc/state.dart +++ b/lib/receive/bloc/state.dart @@ -27,9 +27,7 @@ class ReceiveState with _$ReceiveState { @Default('') String errGeneratingInvoice, @Default(false) bool generatingInvoice, SwapTx? swapTx, - // BtcLnSwap? btcLnSwap, - // LbtcLnSwap? lbtcLnSwap, - // Address? newInvoiceAddress, + List? swapTxs, }) = _ReceiveState; const ReceiveState._(); diff --git a/lib/receive/bloc/state.freezed.dart b/lib/receive/bloc/state.freezed.dart index 4ce0999c..24109c3b 100644 --- a/lib/receive/bloc/state.freezed.dart +++ b/lib/receive/bloc/state.freezed.dart @@ -33,6 +33,7 @@ mixin _$ReceiveState { String get errGeneratingInvoice => throw _privateConstructorUsedError; bool get generatingInvoice => throw _privateConstructorUsedError; SwapTx? get swapTx => throw _privateConstructorUsedError; + List? get swapTxs => throw _privateConstructorUsedError; @JsonKey(ignore: true) $ReceiveStateCopyWith get copyWith => @@ -62,7 +63,8 @@ abstract class $ReceiveStateCopyWith<$Res> { ReceiveWalletType walletType, String errGeneratingInvoice, bool generatingInvoice, - SwapTx? swapTx}); + SwapTx? swapTx, + List? swapTxs}); $AddressCopyWith<$Res>? get defaultAddress; $SwapTxCopyWith<$Res>? get swapTx; @@ -98,6 +100,7 @@ class _$ReceiveStateCopyWithImpl<$Res, $Val extends ReceiveState> Object? errGeneratingInvoice = null, Object? generatingInvoice = null, Object? swapTx = freezed, + Object? swapTxs = freezed, }) { return _then(_value.copyWith( loadingAddress: null == loadingAddress @@ -168,6 +171,10 @@ class _$ReceiveStateCopyWithImpl<$Res, $Val extends ReceiveState> ? _value.swapTx : swapTx // ignore: cast_nullable_to_non_nullable as SwapTx?, + swapTxs: freezed == swapTxs + ? _value.swapTxs + : swapTxs // ignore: cast_nullable_to_non_nullable + as List?, ) as $Val); } @@ -221,7 +228,8 @@ abstract class _$$ReceiveStateImplCopyWith<$Res> ReceiveWalletType walletType, String errGeneratingInvoice, bool generatingInvoice, - SwapTx? swapTx}); + SwapTx? swapTx, + List? swapTxs}); @override $AddressCopyWith<$Res>? get defaultAddress; @@ -257,6 +265,7 @@ class __$$ReceiveStateImplCopyWithImpl<$Res> Object? errGeneratingInvoice = null, Object? generatingInvoice = null, Object? swapTx = freezed, + Object? swapTxs = freezed, }) { return _then(_$ReceiveStateImpl( loadingAddress: null == loadingAddress @@ -327,6 +336,10 @@ class __$$ReceiveStateImplCopyWithImpl<$Res> ? _value.swapTx : swapTx // ignore: cast_nullable_to_non_nullable as SwapTx?, + swapTxs: freezed == swapTxs + ? _value._swapTxs + : swapTxs // ignore: cast_nullable_to_non_nullable + as List?, )); } } @@ -351,8 +364,10 @@ class _$ReceiveStateImpl extends _ReceiveState { this.walletType = ReceiveWalletType.secure, this.errGeneratingInvoice = '', this.generatingInvoice = false, - this.swapTx}) - : super._(); + this.swapTx, + final List? swapTxs}) + : _swapTxs = swapTxs, + super._(); @override @JsonKey() @@ -402,10 +417,19 @@ class _$ReceiveStateImpl extends _ReceiveState { final bool generatingInvoice; @override final SwapTx? swapTx; + final List? _swapTxs; + @override + List? get swapTxs { + final value = _swapTxs; + if (value == null) return null; + if (_swapTxs is EqualUnmodifiableListView) return _swapTxs; + // ignore: implicit_dynamic_type + return EqualUnmodifiableListView(value); + } @override String toString() { - return 'ReceiveState(loadingAddress: $loadingAddress, errLoadingAddress: $errLoadingAddress, defaultAddress: $defaultAddress, privateLabel: $privateLabel, savingLabel: $savingLabel, errSavingLabel: $errSavingLabel, labelSaved: $labelSaved, savedInvoiceAmount: $savedInvoiceAmount, description: $description, savedDescription: $savedDescription, creatingInvoice: $creatingInvoice, errCreatingInvoice: $errCreatingInvoice, walletBloc: $walletBloc, walletType: $walletType, errGeneratingInvoice: $errGeneratingInvoice, generatingInvoice: $generatingInvoice, swapTx: $swapTx)'; + return 'ReceiveState(loadingAddress: $loadingAddress, errLoadingAddress: $errLoadingAddress, defaultAddress: $defaultAddress, privateLabel: $privateLabel, savingLabel: $savingLabel, errSavingLabel: $errSavingLabel, labelSaved: $labelSaved, savedInvoiceAmount: $savedInvoiceAmount, description: $description, savedDescription: $savedDescription, creatingInvoice: $creatingInvoice, errCreatingInvoice: $errCreatingInvoice, walletBloc: $walletBloc, walletType: $walletType, errGeneratingInvoice: $errGeneratingInvoice, generatingInvoice: $generatingInvoice, swapTx: $swapTx, swapTxs: $swapTxs)'; } @override @@ -445,7 +469,8 @@ class _$ReceiveStateImpl extends _ReceiveState { other.errGeneratingInvoice == errGeneratingInvoice) && (identical(other.generatingInvoice, generatingInvoice) || other.generatingInvoice == generatingInvoice) && - (identical(other.swapTx, swapTx) || other.swapTx == swapTx)); + (identical(other.swapTx, swapTx) || other.swapTx == swapTx) && + const DeepCollectionEquality().equals(other._swapTxs, _swapTxs)); } @override @@ -467,7 +492,8 @@ class _$ReceiveStateImpl extends _ReceiveState { walletType, errGeneratingInvoice, generatingInvoice, - swapTx); + swapTx, + const DeepCollectionEquality().hash(_swapTxs)); @JsonKey(ignore: true) @override @@ -494,7 +520,8 @@ abstract class _ReceiveState extends ReceiveState { final ReceiveWalletType walletType, final String errGeneratingInvoice, final bool generatingInvoice, - final SwapTx? swapTx}) = _$ReceiveStateImpl; + final SwapTx? swapTx, + final List? swapTxs}) = _$ReceiveStateImpl; const _ReceiveState._() : super._(); @override @@ -532,6 +559,8 @@ abstract class _ReceiveState extends ReceiveState { @override SwapTx? get swapTx; @override + List? get swapTxs; + @override @JsonKey(ignore: true) _$$ReceiveStateImplCopyWith<_$ReceiveStateImpl> get copyWith => throw _privateConstructorUsedError;