diff --git a/cw_monero/lib/monero_wallet.dart b/cw_monero/lib/monero_wallet.dart index eea490ba9..e6d9dd033 100644 --- a/cw_monero/lib/monero_wallet.dart +++ b/cw_monero/lib/monero_wallet.dart @@ -175,9 +175,9 @@ abstract class MoneroWalletBase extends WalletBase item.sendAll diff --git a/lib/di.dart b/lib/di.dart index 0f59c702f..4705aff93 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -298,7 +298,7 @@ Future setup({ (WalletType type) => getIt.get(param1: type))); getIt.registerFactoryParam((type, _) => WalletNewVM( - getIt.get(), getIt.get(param1: type), _walletInfoSource, + getIt.get(), getIt.get(param1: type),getIt.get(), _walletInfoSource, type: type)); getIt.registerFactoryParam((args, _) { @@ -307,7 +307,7 @@ Future setup({ final mnemonic = args[2] as String; return WalletRestorationFromSeedVM( - getIt.get(), getIt.get(param1: type), _walletInfoSource, + getIt.get(), getIt.get(param1: type), _walletInfoSource , getIt.get(), type: type, language: language, seed: mnemonic); }); @@ -316,13 +316,13 @@ Future setup({ final language = args[1] as String; return WalletRestorationFromKeysVM( - getIt.get(), getIt.get(param1: type), _walletInfoSource, + getIt.get(), getIt.get(param1: type), _walletInfoSource, getIt.get(), type: type, language: language); }); getIt.registerFactoryParam((WalletType type, _) { return WalletRestorationFromQRVM(getIt.get(), - getIt.get(param1: type), _walletInfoSource, type); + getIt.get(param1: type), getIt.get(), _walletInfoSource, type); }); getIt.registerFactory(() => WalletAddressListViewModel( @@ -739,7 +739,7 @@ Future setup({ getIt.registerFactoryParam((type, _) => WalletRestoreViewModel( - getIt.get(), getIt.get(param1: type), _walletInfoSource, + getIt.get(), getIt.get(param1: type),getIt.get(), _walletInfoSource, type: type)); getIt.registerFactoryParam( diff --git a/lib/view_model/restore/restore_from_qr_vm.dart b/lib/view_model/restore/restore_from_qr_vm.dart index 04cc7aa7d..e8f19091f 100644 --- a/lib/view_model/restore/restore_from_qr_vm.dart +++ b/lib/view_model/restore/restore_from_qr_vm.dart @@ -1,4 +1,5 @@ import 'package:cake_wallet/bitcoin/bitcoin.dart'; +import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart'; import 'package:cake_wallet/view_model/restore/restore_mode.dart'; import 'package:cake_wallet/view_model/restore/restore_wallet.dart'; import 'package:hive/hive.dart'; @@ -18,14 +19,14 @@ part 'restore_from_qr_vm.g.dart'; class WalletRestorationFromQRVM = WalletRestorationFromQRVMBase with _$WalletRestorationFromQRVM; abstract class WalletRestorationFromQRVMBase extends WalletCreationVM with Store { - WalletRestorationFromQRVMBase(AppStore appStore, WalletCreationService walletCreationService, + WalletRestorationFromQRVMBase(AppStore appStore, WalletCreationService walletCreationService,FiatConversionStore fiatConversationStore, Box walletInfoSource, WalletType type) : height = 0, viewKey = '', spendKey = '', wif = '', address = '', - super(appStore, walletInfoSource, walletCreationService, type: type, isRecovery: true); + super(appStore, walletInfoSource, walletCreationService, fiatConversationStore, type: type, isRecovery: true); @observable int height; diff --git a/lib/view_model/wallet_creation_vm.dart b/lib/view_model/wallet_creation_vm.dart index adb6ddd70..d1e9ff412 100644 --- a/lib/view_model/wallet_creation_vm.dart +++ b/lib/view_model/wallet_creation_vm.dart @@ -1,6 +1,11 @@ +import 'dart:async'; + import 'package:cake_wallet/core/wallet_creation_service.dart'; +import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart'; import 'package:cake_wallet/view_model/restore/restore_wallet.dart'; +import 'package:cake_wallet/view_model/send/output.dart'; import 'package:cw_core/balance.dart'; +import 'package:cw_core/pending_transaction.dart'; import 'package:cw_core/transaction_history.dart'; import 'package:cw_core/transaction_info.dart'; import 'package:hive/hive.dart'; @@ -22,7 +27,7 @@ part 'wallet_creation_vm.g.dart'; class WalletCreationVM = WalletCreationVMBase with _$WalletCreationVM; abstract class WalletCreationVMBase with Store { - WalletCreationVMBase(this._appStore, this._walletInfoSource, this.walletCreationService, + WalletCreationVMBase(this._appStore, this._walletInfoSource, this.walletCreationService, this._fiatConversationStore, {required this.type, required this.isRecovery}) : state = InitialExecutionState(), name = ''; @@ -33,11 +38,17 @@ abstract class WalletCreationVMBase with Store { @observable ExecutionState state; + @observable + PendingTransaction? pendingTransaction; + WalletType type; final bool isRecovery; final WalletCreationService walletCreationService; final Box _walletInfoSource; final AppStore _appStore; + final FiatConversionStore _fiatConversationStore; + + Completer syncCompleter = Completer(); bool nameExists(String name) => walletCreationService.exists(name); @@ -64,8 +75,10 @@ abstract class WalletCreationVMBase with Store { //! Switch to the restoredWallet in order to activate the node connection _appStore.changeCurrentWallet(restoredWallet); + await restoredWallet.startSync(); + //! Sweep all funds from restoredWallet to newWallet - await sweepAllFundsToNewWallet(type, newWalletAddress, restoreWallet?.txId ?? ''); + await sweepAllFundsToNewWallet(restoredWallet, type, newWalletAddress, restoreWallet?.txId ?? ''); //! Switch back to new wallet _appStore.changeCurrentWallet(newWallet); @@ -117,15 +130,78 @@ abstract class WalletCreationVMBase with Store { ? await processFromRestoredWallet(credentials, restoreWallet) : await process(credentials); walletInfo.address = wallet.walletAddresses.address; + return wallet; } - Future> sweepAllFundsToNewWallet( - WalletType type, String address, String paymentId) async { - final currentNode = _appStore.settingsStore.getCurrentNode(type); - final result = await walletCreationService.sweepAllFunds(currentNode, address, paymentId); - return result; + Future sweepAllFundsToNewWallet(WalletBase, TransactionInfo> wallet, + WalletType type, String newWalletAddress, String paymentId) async { + final output = Output(wallet, _appStore.settingsStore, _fiatConversationStore, () => wallet.currency); + output.address = newWalletAddress; + output.sendAll = true; + output.note = 'testing the sweep all function'; + final credentials = _credentials(type, wallet.currency.title, output); + print('About to enter create function'); + await createTransaction(wallet, credentials); + // final currentNode = _appStore.settingsStore.getCurrentNode(type); + // final result = await walletCreationService.sweepAllFunds(currentNode, newWalletAddress, paymentId); + + } + + + Object _credentials(WalletType type,String cryptoCurrencyTitle,Output output ) { + switch (type) { + case WalletType.bitcoin: + final priority = _appStore.settingsStore.priority[type]; + + if (priority == null) { + throw Exception('Priority is null for wallet type: ${type}'); + } + + return bitcoin!.createBitcoinTransactionCredentials([output], priority: priority); + case WalletType.litecoin: + final priority = _appStore.settingsStore.priority[type]; + + if (priority == null) { + throw Exception('Priority is null for wallet type: ${type}'); + } + + return bitcoin!.createBitcoinTransactionCredentials([output], priority: priority); + case WalletType.monero: + final priority = _appStore.settingsStore.priority[type]; + + if (priority == null) { + throw Exception('Priority is null for wallet type: ${type}'); + } + + return monero!.createMoneroTransactionCreationCredentials( + outputs:[output], priority: priority); + case WalletType.haven: + final priority = _appStore.settingsStore.priority[type]; + + if (priority == null) { + throw Exception('Priority is null for wallet type: ${type}'); + } + + return haven!.createHavenTransactionCreationCredentials( + outputs: [output], priority: priority, assetType: cryptoCurrencyTitle); + default: + throw Exception('Unexpected wallet type: ${type}'); + } + } + + @action + Future createTransaction(WalletBase wallet, Object credentials) async { + try { + print('in here'); + state = IsExecutingState(); + print('about to enter wallet create transaction function'); + pendingTransaction = await wallet.createTransaction(credentials); + state = ExecutedSuccessfullyState(); + } catch (e) { + state = FailureState(e.toString()); + } } WalletCredentials getCredentials(dynamic options) { diff --git a/lib/view_model/wallet_new_vm.dart b/lib/view_model/wallet_new_vm.dart index f833cba55..ff5724035 100644 --- a/lib/view_model/wallet_new_vm.dart +++ b/lib/view_model/wallet_new_vm.dart @@ -11,16 +11,18 @@ import 'package:cake_wallet/monero/monero.dart'; import 'package:cake_wallet/bitcoin/bitcoin.dart'; import 'package:cake_wallet/haven/haven.dart'; +import '../store/dashboard/fiat_conversion_store.dart'; + part 'wallet_new_vm.g.dart'; class WalletNewVM = WalletNewVMBase with _$WalletNewVM; abstract class WalletNewVMBase extends WalletCreationVM with Store { - WalletNewVMBase(AppStore appStore, WalletCreationService walletCreationService, + WalletNewVMBase(AppStore appStore, WalletCreationService walletCreationService,FiatConversionStore fiatConversationStore, Box walletInfoSource, {required WalletType type}) : selectedMnemonicLanguage = '', - super(appStore, walletInfoSource, walletCreationService, type: type, isRecovery: false); + super(appStore, walletInfoSource, walletCreationService,fiatConversationStore, type: type, isRecovery: false); @observable String selectedMnemonicLanguage; diff --git a/lib/view_model/wallet_restoration_from_keys_vm.dart b/lib/view_model/wallet_restoration_from_keys_vm.dart index 97cb8d519..4d4cd70e5 100644 --- a/lib/view_model/wallet_restoration_from_keys_vm.dart +++ b/lib/view_model/wallet_restoration_from_keys_vm.dart @@ -1,3 +1,4 @@ +import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart'; import 'package:cake_wallet/view_model/restore/restore_wallet.dart'; import 'package:flutter/foundation.dart'; import 'package:hive/hive.dart'; @@ -21,14 +22,14 @@ class WalletRestorationFromKeysVM = WalletRestorationFromKeysVMBase abstract class WalletRestorationFromKeysVMBase extends WalletCreationVM with Store { WalletRestorationFromKeysVMBase(AppStore appStore, - WalletCreationService walletCreationService, Box walletInfoSource, + WalletCreationService walletCreationService, Box walletInfoSource,FiatConversionStore fiatConversationStore, {required WalletType type, required this.language}) : height = 0, viewKey = '', spendKey = '', wif = '', address = '', - super(appStore, walletInfoSource, walletCreationService, type: type, isRecovery: true); + super(appStore, walletInfoSource, walletCreationService, fiatConversationStore, type: type, isRecovery: true); @observable int height; diff --git a/lib/view_model/wallet_restoration_from_seed_vm.dart b/lib/view_model/wallet_restoration_from_seed_vm.dart index 0caa7f37d..67c41f7cf 100644 --- a/lib/view_model/wallet_restoration_from_seed_vm.dart +++ b/lib/view_model/wallet_restoration_from_seed_vm.dart @@ -1,3 +1,4 @@ +import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart'; import 'package:cake_wallet/view_model/restore/restore_wallet.dart'; import 'package:flutter/foundation.dart'; import 'package:hive/hive.dart'; @@ -21,10 +22,10 @@ class WalletRestorationFromSeedVM = WalletRestorationFromSeedVMBase abstract class WalletRestorationFromSeedVMBase extends WalletCreationVM with Store { WalletRestorationFromSeedVMBase(AppStore appStore, - WalletCreationService walletCreationService, Box walletInfoSource, + WalletCreationService walletCreationService, Box walletInfoSource,FiatConversionStore fiatConversationStore, {required WalletType type, required this.language, this.seed = ''}) : height = 0, - super(appStore, walletInfoSource, walletCreationService, type: type, isRecovery: true); + super(appStore, walletInfoSource, walletCreationService ,fiatConversationStore, type: type, isRecovery: true); @observable String seed; diff --git a/lib/view_model/wallet_restore_view_model.dart b/lib/view_model/wallet_restore_view_model.dart index 7af653cf1..ef33d6089 100644 --- a/lib/view_model/wallet_restore_view_model.dart +++ b/lib/view_model/wallet_restore_view_model.dart @@ -1,5 +1,6 @@ import 'package:cake_wallet/bitcoin/bitcoin.dart'; import 'package:cake_wallet/core/mnemonic_length.dart'; +import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart'; import 'package:cake_wallet/view_model/restore/restore_wallet.dart'; import 'package:flutter/foundation.dart'; import 'package:hive/hive.dart'; @@ -23,7 +24,7 @@ class WalletRestoreViewModel = WalletRestoreViewModelBase with _$WalletRestoreViewModel; abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store { - WalletRestoreViewModelBase(AppStore appStore, WalletCreationService walletCreationService, + WalletRestoreViewModelBase(AppStore appStore, WalletCreationService walletCreationService,FiatConversionStore fiatConversationStore, Box walletInfoSource, {required WalletType type}) : availableModes = (type == WalletType.monero || type == WalletType.haven) @@ -33,7 +34,7 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store { hasBlockchainHeightLanguageSelector = type == WalletType.monero || type == WalletType.haven, isButtonEnabled = false, mode = WalletRestoreMode.seed, - super(appStore, walletInfoSource, walletCreationService, type: type, isRecovery: true) { + super(appStore, walletInfoSource, walletCreationService, fiatConversationStore, type: type, isRecovery: true) { isButtonEnabled = !hasSeedLanguageSelector && !hasBlockchainHeightLanguageSelector; walletCreationService.changeWalletType(type: type);