Update fiat API version Enable Tor support for Fiat

Update fiat API version Enable Tor support for Fiat

OmarHatem committed Feb 28, 2023 at 18:23 UTC ce4b1cd4aefbee242e2492e62e9d6154af969ea5
8 files changed +97 -51
lib/core/fiat_conversion_service.dart
+29 -17
@@ -4,18 +4,30 @@ import 'dart:convert';
4 import 'package:flutter/foundation.dart';
5 import 'package:http/http.dart';
6
7 -const fiatApiAuthority = 'fiat-api.cakewallet.com';
8 -const fiatApiPath = '/v1/rates';
7 +const _fiatApiClearNetAuthority = 'fiat-api.cakewallet.com';
8 +const _fiatApiOnionAuthority = 'n4z7bdcmwk2oyddxvzaap3x2peqcplh3pzdy7tpkk5ejz5n4mhfvoxqd.onion';
9 +const _fiatApiPath = '/v2/rates';
10
11 Future<double> _fetchPrice(Map<String, dynamic> args) async {
12 final crypto = args['crypto'] as CryptoCurrency;
13 final fiat = args['fiat'] as FiatCurrency;
14 + final torOnly = args['torOnly'] as bool;
15 double price = 0.0;
16 + print("@@@@@@@@@@@@@@");
17 + print(crypto);
18 + print(fiat);
19 + print(torOnly);
20
21 try {
16 - final fiatStringified = fiat.toString();
17 - final uri = Uri.https(fiatApiAuthority, fiatApiPath,
18 - <String, String>{'convert': fiatStringified});
22 + final uri = Uri.https(
23 + torOnly ? _fiatApiOnionAuthority : _fiatApiClearNetAuthority,
24 + _fiatApiPath,
25 + <String, String>{
26 + 'interval_count': '1',
27 + 'base': crypto.toString(),
28 + 'quote': fiat.toString(),
29 + },
30 + );
31 final response = await get(uri);
32
33 if (response.statusCode != 200) {
@@ -23,14 +35,12 @@ Future<double> _fetchPrice(Map<String, dynamic> args) async {
35 }
36
37 final responseJSON = json.decode(response.body) as Map<String, dynamic>;
26 - final data = responseJSON['data'] as List<dynamic>;
38 + final results = responseJSON['results'] as Map<String, dynamic>;
39
28 - for (final item in data) {
29 - if (item['symbol'] == crypto.title) {
30 - price = item['quote'][fiatStringified]['price'] as double;
31 - break;
32 - }
40 + if (results.isNotEmpty) {
41 + price = results.values.first as double;
42 }
43 + print(price);
44
45 return price;
46 } catch (e) {
@@ -38,12 +48,14 @@ Future<double> _fetchPrice(Map<String, dynamic> args) async {
48 }
49 }
50
41 -Future<double> _fetchPriceAsync(
42 - CryptoCurrency crypto, FiatCurrency fiat) async =>
43 - compute(_fetchPrice, {'fiat': fiat, 'crypto': crypto});
51 +Future<double> _fetchPriceAsync(CryptoCurrency crypto, FiatCurrency fiat, bool torOnly) async =>
52 + compute(_fetchPrice, {'fiat': fiat, 'crypto': crypto, 'torOnly': torOnly});
53
54 class FiatConversionService {
46 - static Future<double> fetchPrice(
47 - CryptoCurrency crypto, FiatCurrency fiat) async =>
48 - await _fetchPriceAsync(crypto, fiat);
55 + static Future<double> fetchPrice({
56 + required CryptoCurrency crypto,
57 + required FiatCurrency fiat,
58 + required bool torOnly,
59 + }) async =>
60 + await _fetchPriceAsync(crypto, fiat, torOnly);
61 }
lib/reactions/bootstrap.dart
+2
@@ -1,5 +1,6 @@
1 import 'dart:async';
2 import 'package:cake_wallet/reactions/fiat_rate_update.dart';
3 +import 'package:cake_wallet/reactions/on_current_fiat_api_mode_change.dart';
4 import 'package:cake_wallet/reactions/on_current_node_change.dart';
5 import 'package:flutter/cupertino.dart';
6 import 'package:flutter/widgets.dart';
@@ -31,6 +32,7 @@ Future<void> bootstrap(GlobalKey<NavigatorState> navigatorKey) async {
32 startCurrentWalletChangeReaction(
33 appStore, settingsStore, fiatConversionStore);
34 startCurrentFiatChangeReaction(appStore, settingsStore, fiatConversionStore);
35 + startCurrentFiatApiModeChangeReaction(appStore, settingsStore, fiatConversionStore);
36 startOnCurrentNodeChangeReaction(appStore);
37 startFiatRateUpdate(appStore, settingsStore, fiatConversionStore);
38 }
lib/reactions/fiat_rate_update.dart
+3 -1
@@ -26,7 +26,9 @@ Future<void> startFiatRateUpdate(
26 } else {
27 fiatConversionStore.prices[appStore.wallet!.currency] =
28 await FiatConversionService.fetchPrice(
29 - appStore.wallet!.currency, settingsStore.fiatCurrency);
29 + crypto: appStore.wallet!.currency,
30 + fiat: settingsStore.fiatCurrency,
31 + torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
32 }
33 } catch (e) {
34 print(e);
lib/reactions/on_current_fiat_api_mode_change.dart new
+25
@@ -0,0 +1,25 @@
1 +import 'package:cake_wallet/entities/fiat_api_mode.dart';
2 +import 'package:mobx/mobx.dart';
3 +import 'package:cake_wallet/core/fiat_conversion_service.dart';
4 +import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
5 +import 'package:cake_wallet/store/settings_store.dart';
6 +import 'package:cake_wallet/store/app_store.dart';
7 +
8 +ReactionDisposer? _onCurrentFiatCurrencyChangeDisposer;
9 +
10 +void startCurrentFiatApiModeChangeReaction(AppStore appStore,
11 + SettingsStore settingsStore, FiatConversionStore fiatConversionStore) {
12 + _onCurrentFiatCurrencyChangeDisposer?.reaction.dispose();
13 + _onCurrentFiatCurrencyChangeDisposer = reaction(
14 + (_) => settingsStore.fiatApiMode, (FiatApiMode fiatApiMode) async {
15 + if (appStore.wallet == null || fiatApiMode == FiatApiMode.disabled) {
16 + return;
17 + }
18 +
19 + fiatConversionStore.prices[appStore.wallet!.currency] =
20 + await FiatConversionService.fetchPrice(
21 + crypto: appStore.wallet!.currency,
22 + fiat: settingsStore.fiatCurrency,
23 + torOnly: fiatApiMode == FiatApiMode.torOnly);
24 + });
25 +}
lib/reactions/on_current_fiat_change.dart
+5 -2
@@ -18,7 +18,10 @@ void startCurrentFiatChangeReaction(AppStore appStore,
18 }
19
20 final cryptoCurrency = appStore.wallet!.currency;
21 - fiatConversionStore.prices[appStore.wallet!.currency] =
22 - await FiatConversionService.fetchPrice(cryptoCurrency, fiatCurrency);
21 + fiatConversionStore.prices[cryptoCurrency] =
22 + await FiatConversionService.fetchPrice(
23 + crypto: cryptoCurrency,
24 + fiat: fiatCurrency,
25 + torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
26 });
27 }
lib/reactions/on_current_wallet_change.dart
+4 -2
@@ -71,7 +71,7 @@ void startCurrentWalletChangeReaction(AppStore appStore,
71 await updateHavenRate(fiatConversionStore);
72 }
73
74 - if (wallet.walletInfo.address?.isEmpty ?? true) {
74 + if (wallet.walletInfo.address.isEmpty) {
75 wallet.walletInfo.address = wallet.walletAddresses.address;
76
77 if (wallet.walletInfo.isInBox) {
@@ -95,7 +95,9 @@ void startCurrentWalletChangeReaction(AppStore appStore,
95 fiatConversionStore.prices[wallet.currency] = 0;
96 fiatConversionStore.prices[wallet.currency] =
97 await FiatConversionService.fetchPrice(
98 - wallet.currency, settingsStore.fiatCurrency);
98 + crypto: wallet.currency,
99 + fiat: settingsStore.fiatCurrency,
100 + torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
101 } catch (e) {
102 print(e.toString());
103 }
lib/src/screens/settings/privacy_page.dart
+27 -21
@@ -1,6 +1,9 @@
1 +import 'package:cake_wallet/entities/fiat_api_mode.dart';
2 import 'package:cake_wallet/generated/i18n.dart';
3 import 'package:cake_wallet/src/screens/base_page.dart';
4 +import 'package:cake_wallet/src/screens/settings/widgets/settings_choices_cell.dart';
5 import 'package:cake_wallet/src/screens/settings/widgets/settings_switcher_cell.dart';
6 +import 'package:cake_wallet/view_model/settings/choices_list_item.dart';
7 import 'package:cake_wallet/view_model/settings/privacy_settings_view_model.dart';
8 import 'package:flutter/material.dart';
9 import 'package:flutter_mobx/flutter_mobx.dart';
@@ -18,29 +21,32 @@ class PrivacyPage extends BasePage {
21 return Container(
22 padding: EdgeInsets.only(top: 10),
23 child: Observer(builder: (_) {
21 - return Column(
22 - mainAxisSize: MainAxisSize.min,
23 - children: [
24 - SettingsSwitcherCell(
25 - title: S.current.disable_fiat,
26 - value: _privacySettingsViewModel.isFiatDisabled,
24 + return Column(
25 + mainAxisSize: MainAxisSize.min,
26 + children: [
27 + SettingsChoicesCell(
28 + ChoicesListItem<FiatApiMode>(
29 + title: S.current.fiat_api,
30 + items: FiatApiMode.all,
31 + selectedItem: _privacySettingsViewModel.fiatApiMode,
32 + onItemSelected: (FiatApiMode fiatApiMode) =>
33 + _privacySettingsViewModel.setFiatMode(fiatApiMode),
34 + ),
35 + ),
36 + SettingsSwitcherCell(
37 + title: S.current.disable_exchange,
38 + value: _privacySettingsViewModel.disableExchange,
39 onValueChange: (BuildContext context, bool value) {
28 - _privacySettingsViewModel.setFiatMode(value);
40 + _privacySettingsViewModel.setEnableExchange(value);
41 }),
30 - SettingsSwitcherCell(
31 - title: S.current.disable_exchange,
32 - value: _privacySettingsViewModel.disableExchange,
33 - onValueChange: (BuildContext context, bool value) {
34 - _privacySettingsViewModel.setEnableExchange(value);
35 - }),
36 - SettingsSwitcherCell(
37 - title: S.current.settings_save_recipient_address,
38 - value: _privacySettingsViewModel.shouldSaveRecipientAddress,
39 - onValueChange: (BuildContext _, bool value) {
40 - _privacySettingsViewModel.setShouldSaveRecipientAddress(value);
41 - })
42 - ],
43 - );
42 + SettingsSwitcherCell(
43 + title: S.current.settings_save_recipient_address,
44 + value: _privacySettingsViewModel.shouldSaveRecipientAddress,
45 + onValueChange: (BuildContext _, bool value) {
46 + _privacySettingsViewModel.setShouldSaveRecipientAddress(value);
47 + }),
48 + ],
49 + );
50 }),
51 );
52 }
lib/view_model/settings/privacy_settings_view_model.dart
+2 -8
@@ -18,7 +18,7 @@ abstract class PrivacySettingsViewModelBase with Store {
18 bool get shouldSaveRecipientAddress => _settingsStore.shouldSaveRecipientAddress;
19
20 @computed
21 - bool get isFiatDisabled => _settingsStore.fiatApiMode == FiatApiMode.disabled;
21 + FiatApiMode get fiatApiMode => _settingsStore.fiatApiMode;
22
23 @action
24 void setShouldSaveRecipientAddress(bool value) => _settingsStore.shouldSaveRecipientAddress = value;
@@ -27,12 +27,6 @@ abstract class PrivacySettingsViewModelBase with Store {
27 void setEnableExchange(bool value) => _settingsStore.disableExchange = value;
28
29 @action
30 - void setFiatMode(bool value) {
31 - if (value) {
32 - _settingsStore.fiatApiMode = FiatApiMode.disabled;
33 - return;
34 - }
35 - _settingsStore.fiatApiMode = FiatApiMode.enabled;
36 - }
30 + void setFiatMode(FiatApiMode fiatApiMode) => _settingsStore.fiatApiMode = fiatApiMode;
31
32 }