CAKE-18 | created fiat convertation service and fiat convertation store; added calculating price to bootstrap; applied fiat convertation store to balance view model
Oleksandr Sobol committed
Jul 24, 2020 at 19:29 UTC
1055c1b539a9d22d53fb4d72f90d3d47f093e2c6
6 files changed
+76
-30
lib/di.dart
+4
-4
@@ -12,7 +12,6 @@ import 'package:cake_wallet/src/screens/wallet_keys/wallet_keys_page.dart';
12
import 'package:cake_wallet/store/contact_list_store.dart';
13
import 'package:cake_wallet/store/node_list_store.dart';
14
import 'package:cake_wallet/store/settings_store.dart';
15
-import 'package:cake_wallet/src/stores/price/price_store.dart';
15
import 'package:cake_wallet/core/auth_service.dart';
16
import 'package:cake_wallet/core/key_service.dart';
17
import 'package:cake_wallet/monero/monero_wallet.dart';
@@ -57,6 +56,7 @@ import 'package:cake_wallet/store/authentication_store.dart';
56
import 'package:cake_wallet/store/dashboard/trades_store.dart';
57
import 'package:cake_wallet/store/dashboard/trade_filter_store.dart';
58
import 'package:cake_wallet/store/dashboard/transaction_filter_store.dart';
59
+import 'package:cake_wallet/store/dashboard/fiat_convertation_store.dart';
60
61
final getIt = GetIt.instance;
62
@@ -83,8 +83,7 @@ Future setup(
83
{Box<WalletInfo> walletInfoSource,
84
Box<Node> nodeSource,
85
Box<Contact> contactSource,
86
- Box<Trade> tradesSource,
87
- PriceStore priceStore}) async {
86
+ Box<Trade> tradesSource}) async {
87
getIt.registerSingletonAsync<SharedPreferences>(
88
() => SharedPreferences.getInstance());
89
@@ -110,6 +109,7 @@ Future setup(
109
getIt.registerSingleton<TradeFilterStore>(
110
TradeFilterStore(wallet: getIt.get<AppStore>().wallet));
111
getIt.registerSingleton<TransactionFilterStore>(TransactionFilterStore());
112
+ getIt.registerSingleton<FiatConvertationStore>(FiatConvertationStore());
113
114
getIt.registerFactory<KeyService>(
115
() => KeyService(getIt.get<FlutterSecureStorage>()));
@@ -145,7 +145,7 @@ Future setup(
145
() => BalanceViewModel(
146
wallet: getIt.get<AppStore>().wallet,
147
settingsStore: getIt.get<SettingsStore>(),
148
- priceStore: priceStore));
148
+ fiatConvertationStore: getIt.get<FiatConvertationStore>()));
149
150
getIt.registerFactory(
151
() => DashboardViewModel(
lib/main.dart
+6
-5
@@ -47,6 +47,7 @@ import 'package:cake_wallet/src/domain/common/wallet_type.dart';
47
import 'package:cake_wallet/src/domain/common/template.dart';
48
import 'package:cake_wallet/src/domain/exchange/exchange_template.dart';
49
import 'package:cake_wallet/src/domain/services/wallet_service.dart';
50
+import 'package:cake_wallet/src/domain/services/fiat_convertation_service.dart';
51
import 'package:cake_wallet/generated/i18n.dart';
52
import 'package:cake_wallet/src/domain/common/language.dart';
53
import 'package:cake_wallet/src/stores/seed_language/seed_language_store.dart';
@@ -87,6 +88,7 @@ void main() async {
88
89
final sharedPreferences = await SharedPreferences.getInstance();
90
final walletService = WalletService();
91
+ final fiatConvertationService = FiatConvertationService();
92
final walletListService = WalletListService(
93
secureStorage: secureStorage,
94
walletInfoSource: walletInfoSource,
@@ -124,7 +126,7 @@ void main() async {
126
walletInfoSource: walletInfoSource,
127
contactSource: contacts,
128
tradesSource: trades,
127
- priceStore: priceStore,
129
+ fiatConvertationService: fiatConvertationService,
130
initialMigrationVersion: 3);
131
132
setReactions(
@@ -166,7 +168,7 @@ Future<void> initialSetup(
168
@required Box<WalletInfo> walletInfoSource,
169
@required Box<Contact> contactSource,
170
@required Box<Trade> tradesSource,
169
- @required PriceStore priceStore,
171
+ @required FiatConvertationService fiatConvertationService,
172
int initialMigrationVersion = 3}) async {
173
await defaultSettingsMigration(
174
version: initialMigrationVersion,
@@ -176,9 +178,8 @@ Future<void> initialSetup(
178
walletInfoSource: walletInfoSource,
179
nodeSource: nodes,
180
contactSource: contactSource,
179
- tradesSource: tradesSource,
180
- priceStore: priceStore);
181
- await bootstrap();
181
+ tradesSource: tradesSource);
182
+ await bootstrap(fiatConvertationService: fiatConvertationService);
183
monero_wallet.onStartup();
184
}
185
lib/reactions/bootstrap.dart
+33
-1
@@ -10,10 +10,14 @@ import 'package:cake_wallet/monero/monero_wallet_service.dart';
10
import 'package:cake_wallet/core/wallet_base.dart';
11
import 'package:cake_wallet/core/wallet_service.dart';
12
import 'package:cake_wallet/store/app_store.dart';
13
+import 'package:cake_wallet/store/settings_store.dart';
14
import 'package:cake_wallet/store/authentication_store.dart';
15
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
16
import 'package:cake_wallet/src/domain/common/secret_store_key.dart';
17
import 'package:cake_wallet/src/domain/common/encrypt.dart';
18
+import 'package:cake_wallet/src/domain/services/fiat_convertation_service.dart';
19
+import 'package:cake_wallet/src/domain/common/fiat_currency.dart';
20
+import 'package:cake_wallet/store/dashboard/fiat_convertation_store.dart';
21
22
// FIXME: move me
23
Future<void> loadCurrentWallet() async {
@@ -44,9 +48,12 @@ Future<void> loadCurrentWallet() async {
48
ReactionDisposer _initialAuthReaction;
49
ReactionDisposer _onCurrentWalletChangeReaction;
50
ReactionDisposer _onWalletSyncStatusChangeReaction;
51
+ReactionDisposer _onCurrentFiatCurrencyChangeDisposer;
52
48
-Future<void> bootstrap() async {
53
+Future<void> bootstrap({FiatConvertationService fiatConvertationService}) async {
54
final authenticationStore = getIt.get<AuthenticationStore>();
55
+ final settingsStore = getIt.get<SettingsStore>();
56
+ final fiatConvertationStore = getIt.get<FiatConvertationStore>();
57
58
if (authenticationStore.state == AuthenticationState.uninitialized) {
59
authenticationStore.state =
@@ -81,5 +88,30 @@ Future<void> bootstrap() async {
88
.setInt('current_wallet_type', serializeToInt(wallet.type));
89
90
await wallet.connectToNode(node: null);
91
+
92
+ final cryptoCurrency = wallet.currency;
93
+ final fiatCurrency = settingsStore.fiatCurrency;
94
+
95
+ final price = await fiatConvertationService.getPrice(
96
+ crypto: cryptoCurrency,
97
+ fiat: fiatCurrency
98
+ );
99
+
100
+ fiatConvertationStore.setPrice(price);
101
+ });
102
+
103
+ //
104
+
105
+ _onCurrentFiatCurrencyChangeDisposer ??=
106
+ reaction((_) => settingsStore.fiatCurrency,
107
+ (FiatCurrency fiatCurrency) async {
108
+ final cryptoCurrency = getIt.get<AppStore>().wallet.currency;
109
+
110
+ final price = await fiatConvertationService.getPrice(
111
+ crypto: cryptoCurrency,
112
+ fiat: fiatCurrency
113
+ );
114
+
115
+ fiatConvertationStore.setPrice(price);
116
});
117
}
lib/src/domain/services/fiat_convertation_service.dart
new
+10
@@ -0,0 +1,10 @@
1
+import 'dart:async';
2
+import 'package:cake_wallet/src/domain/common/fiat_currency.dart';
3
+import 'package:cake_wallet/src/domain/common/crypto_currency.dart';
4
+import 'package:cake_wallet/src/domain/common/fetch_price.dart';
5
+
6
+class FiatConvertationService {
7
+ Future<double> getPrice({CryptoCurrency crypto, FiatCurrency fiat}) async {
8
+ return await fetchPriceFor(crypto: crypto, fiat: fiat);
9
+ }
10
+}
\ No newline at end of file
lib/store/dashboard/fiat_convertation_store.dart
new
+19
@@ -0,0 +1,19 @@
1
+import 'package:mobx/mobx.dart';
2
+
3
+part 'fiat_convertation_store.g.dart';
4
+
5
+class FiatConvertationStore = FiatConvertationStoreBase with _$FiatConvertationStore;
6
+
7
+abstract class FiatConvertationStoreBase with Store {
8
+ FiatConvertationStoreBase() {
9
+ setPrice(0.0);
10
+ }
11
+
12
+ @observable
13
+ double price;
14
+
15
+ @action
16
+ void setPrice(double price) {
17
+ this.price = price;
18
+ }
19
+}
\ No newline at end of file
lib/view_model/dashboard/balance_view_model.dart
+4
-20
@@ -3,10 +3,9 @@ import 'package:cake_wallet/core/wallet_base.dart';
3
import 'package:cake_wallet/monero/monero_wallet.dart';
4
import 'package:cake_wallet/src/domain/common/balance_display_mode.dart';
5
import 'package:cake_wallet/src/domain/common/calculate_fiat_amount.dart';
6
-import 'package:cake_wallet/src/domain/common/crypto_currency.dart';
6
import 'package:cake_wallet/view_model/dashboard/wallet_balance.dart';
7
import 'package:cake_wallet/store/settings_store.dart';
9
-import 'package:cake_wallet/src/stores/price/price_store.dart';
8
+import 'package:cake_wallet/store/dashboard/fiat_convertation_store.dart';
9
import 'package:flutter/cupertino.dart';
10
import 'package:mobx/mobx.dart';
11
@@ -18,12 +17,12 @@ abstract class BalanceViewModelBase with Store {
17
BalanceViewModelBase({
18
@required this.wallet,
19
@required this.settingsStore,
21
- @required this.priceStore
20
+ @required this.fiatConvertationStore
21
});
22
23
final WalletBase wallet;
24
final SettingsStore settingsStore;
26
- final PriceStore priceStore;
25
+ final FiatConvertationStore fiatConvertationStore;
26
27
WalletBalance _getWalletBalance() {
28
final _wallet = wallet;
@@ -50,22 +49,7 @@ abstract class BalanceViewModelBase with Store {
49
}
50
51
@computed
53
- double get price {
54
- String symbol;
55
- final _wallet = wallet;
56
-
57
- if (_wallet is MoneroWallet) {
58
- symbol = PriceStoreBase.generateSymbolForPair(
59
- fiat: settingsStore.fiatCurrency, crypto: CryptoCurrency.xmr);
60
- }
61
-
62
- if (_wallet is BitcoinWallet) {
63
- symbol = PriceStoreBase.generateSymbolForPair(
64
- fiat: settingsStore.fiatCurrency, crypto: CryptoCurrency.btc);
65
- }
66
-
67
- return priceStore.prices[symbol];
68
- }
52
+ double get price => fiatConvertationStore.price;
53
54
@computed
55
String get cryptoBalance {