CAKE-18 | created fiat convertation service and fiat convertation store; added calculating price to bootstrap; applied fiat convertation store to balance view model

This commit is contained in:
Oleksandr Sobol 2020-07-24 19:29:16 +03:00
parent 76cd9f5541
commit 1055c1b539
6 changed files with 76 additions and 30 deletions

View file

@ -12,7 +12,6 @@ import 'package:cake_wallet/src/screens/wallet_keys/wallet_keys_page.dart';
import 'package:cake_wallet/store/contact_list_store.dart';
import 'package:cake_wallet/store/node_list_store.dart';
import 'package:cake_wallet/store/settings_store.dart';
import 'package:cake_wallet/src/stores/price/price_store.dart';
import 'package:cake_wallet/core/auth_service.dart';
import 'package:cake_wallet/core/key_service.dart';
import 'package:cake_wallet/monero/monero_wallet.dart';
@ -57,6 +56,7 @@ import 'package:cake_wallet/store/authentication_store.dart';
import 'package:cake_wallet/store/dashboard/trades_store.dart';
import 'package:cake_wallet/store/dashboard/trade_filter_store.dart';
import 'package:cake_wallet/store/dashboard/transaction_filter_store.dart';
import 'package:cake_wallet/store/dashboard/fiat_convertation_store.dart';
final getIt = GetIt.instance;
@ -83,8 +83,7 @@ Future setup(
{Box<WalletInfo> walletInfoSource,
Box<Node> nodeSource,
Box<Contact> contactSource,
Box<Trade> tradesSource,
PriceStore priceStore}) async {
Box<Trade> tradesSource}) async {
getIt.registerSingletonAsync<SharedPreferences>(
() => SharedPreferences.getInstance());
@ -110,6 +109,7 @@ Future setup(
getIt.registerSingleton<TradeFilterStore>(
TradeFilterStore(wallet: getIt.get<AppStore>().wallet));
getIt.registerSingleton<TransactionFilterStore>(TransactionFilterStore());
getIt.registerSingleton<FiatConvertationStore>(FiatConvertationStore());
getIt.registerFactory<KeyService>(
() => KeyService(getIt.get<FlutterSecureStorage>()));
@ -145,7 +145,7 @@ Future setup(
() => BalanceViewModel(
wallet: getIt.get<AppStore>().wallet,
settingsStore: getIt.get<SettingsStore>(),
priceStore: priceStore));
fiatConvertationStore: getIt.get<FiatConvertationStore>()));
getIt.registerFactory(
() => DashboardViewModel(

View file

@ -47,6 +47,7 @@ import 'package:cake_wallet/src/domain/common/wallet_type.dart';
import 'package:cake_wallet/src/domain/common/template.dart';
import 'package:cake_wallet/src/domain/exchange/exchange_template.dart';
import 'package:cake_wallet/src/domain/services/wallet_service.dart';
import 'package:cake_wallet/src/domain/services/fiat_convertation_service.dart';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/src/domain/common/language.dart';
import 'package:cake_wallet/src/stores/seed_language/seed_language_store.dart';
@ -87,6 +88,7 @@ void main() async {
final sharedPreferences = await SharedPreferences.getInstance();
final walletService = WalletService();
final fiatConvertationService = FiatConvertationService();
final walletListService = WalletListService(
secureStorage: secureStorage,
walletInfoSource: walletInfoSource,
@ -124,7 +126,7 @@ void main() async {
walletInfoSource: walletInfoSource,
contactSource: contacts,
tradesSource: trades,
priceStore: priceStore,
fiatConvertationService: fiatConvertationService,
initialMigrationVersion: 3);
setReactions(
@ -166,7 +168,7 @@ Future<void> initialSetup(
@required Box<WalletInfo> walletInfoSource,
@required Box<Contact> contactSource,
@required Box<Trade> tradesSource,
@required PriceStore priceStore,
@required FiatConvertationService fiatConvertationService,
int initialMigrationVersion = 3}) async {
await defaultSettingsMigration(
version: initialMigrationVersion,
@ -176,9 +178,8 @@ Future<void> initialSetup(
walletInfoSource: walletInfoSource,
nodeSource: nodes,
contactSource: contactSource,
tradesSource: tradesSource,
priceStore: priceStore);
await bootstrap();
tradesSource: tradesSource);
await bootstrap(fiatConvertationService: fiatConvertationService);
monero_wallet.onStartup();
}

View file

@ -10,10 +10,14 @@ import 'package:cake_wallet/monero/monero_wallet_service.dart';
import 'package:cake_wallet/core/wallet_base.dart';
import 'package:cake_wallet/core/wallet_service.dart';
import 'package:cake_wallet/store/app_store.dart';
import 'package:cake_wallet/store/settings_store.dart';
import 'package:cake_wallet/store/authentication_store.dart';
import 'package:cake_wallet/src/domain/common/wallet_type.dart';
import 'package:cake_wallet/src/domain/common/secret_store_key.dart';
import 'package:cake_wallet/src/domain/common/encrypt.dart';
import 'package:cake_wallet/src/domain/services/fiat_convertation_service.dart';
import 'package:cake_wallet/src/domain/common/fiat_currency.dart';
import 'package:cake_wallet/store/dashboard/fiat_convertation_store.dart';
// FIXME: move me
Future<void> loadCurrentWallet() async {
@ -44,9 +48,12 @@ Future<void> loadCurrentWallet() async {
ReactionDisposer _initialAuthReaction;
ReactionDisposer _onCurrentWalletChangeReaction;
ReactionDisposer _onWalletSyncStatusChangeReaction;
ReactionDisposer _onCurrentFiatCurrencyChangeDisposer;
Future<void> bootstrap() async {
Future<void> bootstrap({FiatConvertationService fiatConvertationService}) async {
final authenticationStore = getIt.get<AuthenticationStore>();
final settingsStore = getIt.get<SettingsStore>();
final fiatConvertationStore = getIt.get<FiatConvertationStore>();
if (authenticationStore.state == AuthenticationState.uninitialized) {
authenticationStore.state =
@ -81,5 +88,30 @@ Future<void> bootstrap() async {
.setInt('current_wallet_type', serializeToInt(wallet.type));
await wallet.connectToNode(node: null);
final cryptoCurrency = wallet.currency;
final fiatCurrency = settingsStore.fiatCurrency;
final price = await fiatConvertationService.getPrice(
crypto: cryptoCurrency,
fiat: fiatCurrency
);
fiatConvertationStore.setPrice(price);
});
//
_onCurrentFiatCurrencyChangeDisposer ??=
reaction((_) => settingsStore.fiatCurrency,
(FiatCurrency fiatCurrency) async {
final cryptoCurrency = getIt.get<AppStore>().wallet.currency;
final price = await fiatConvertationService.getPrice(
crypto: cryptoCurrency,
fiat: fiatCurrency
);
fiatConvertationStore.setPrice(price);
});
}

View file

@ -0,0 +1,10 @@
import 'dart:async';
import 'package:cake_wallet/src/domain/common/fiat_currency.dart';
import 'package:cake_wallet/src/domain/common/crypto_currency.dart';
import 'package:cake_wallet/src/domain/common/fetch_price.dart';
class FiatConvertationService {
Future<double> getPrice({CryptoCurrency crypto, FiatCurrency fiat}) async {
return await fetchPriceFor(crypto: crypto, fiat: fiat);
}
}

View file

@ -0,0 +1,19 @@
import 'package:mobx/mobx.dart';
part 'fiat_convertation_store.g.dart';
class FiatConvertationStore = FiatConvertationStoreBase with _$FiatConvertationStore;
abstract class FiatConvertationStoreBase with Store {
FiatConvertationStoreBase() {
setPrice(0.0);
}
@observable
double price;
@action
void setPrice(double price) {
this.price = price;
}
}

View file

@ -3,10 +3,9 @@ import 'package:cake_wallet/core/wallet_base.dart';
import 'package:cake_wallet/monero/monero_wallet.dart';
import 'package:cake_wallet/src/domain/common/balance_display_mode.dart';
import 'package:cake_wallet/src/domain/common/calculate_fiat_amount.dart';
import 'package:cake_wallet/src/domain/common/crypto_currency.dart';
import 'package:cake_wallet/view_model/dashboard/wallet_balance.dart';
import 'package:cake_wallet/store/settings_store.dart';
import 'package:cake_wallet/src/stores/price/price_store.dart';
import 'package:cake_wallet/store/dashboard/fiat_convertation_store.dart';
import 'package:flutter/cupertino.dart';
import 'package:mobx/mobx.dart';
@ -18,12 +17,12 @@ abstract class BalanceViewModelBase with Store {
BalanceViewModelBase({
@required this.wallet,
@required this.settingsStore,
@required this.priceStore
@required this.fiatConvertationStore
});
final WalletBase wallet;
final SettingsStore settingsStore;
final PriceStore priceStore;
final FiatConvertationStore fiatConvertationStore;
WalletBalance _getWalletBalance() {
final _wallet = wallet;
@ -50,22 +49,7 @@ abstract class BalanceViewModelBase with Store {
}
@computed
double get price {
String symbol;
final _wallet = wallet;
if (_wallet is MoneroWallet) {
symbol = PriceStoreBase.generateSymbolForPair(
fiat: settingsStore.fiatCurrency, crypto: CryptoCurrency.xmr);
}
if (_wallet is BitcoinWallet) {
symbol = PriceStoreBase.generateSymbolForPair(
fiat: settingsStore.fiatCurrency, crypto: CryptoCurrency.btc);
}
return priceStore.prices[symbol];
}
double get price => fiatConvertationStore.price;
@computed
String get cryptoBalance {