Update fiat API version

Enable Tor support for Fiat
This commit is contained in:
OmarHatem 2023-02-28 18:23:21 +02:00
parent c307e055ea
commit ce4b1cd4ae
8 changed files with 97 additions and 51 deletions

View file

@ -4,18 +4,30 @@ import 'dart:convert';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:http/http.dart'; import 'package:http/http.dart';
const fiatApiAuthority = 'fiat-api.cakewallet.com'; const _fiatApiClearNetAuthority = 'fiat-api.cakewallet.com';
const fiatApiPath = '/v1/rates'; const _fiatApiOnionAuthority = 'n4z7bdcmwk2oyddxvzaap3x2peqcplh3pzdy7tpkk5ejz5n4mhfvoxqd.onion';
const _fiatApiPath = '/v2/rates';
Future<double> _fetchPrice(Map<String, dynamic> args) async { Future<double> _fetchPrice(Map<String, dynamic> args) async {
final crypto = args['crypto'] as CryptoCurrency; final crypto = args['crypto'] as CryptoCurrency;
final fiat = args['fiat'] as FiatCurrency; final fiat = args['fiat'] as FiatCurrency;
final torOnly = args['torOnly'] as bool;
double price = 0.0; double price = 0.0;
print("@@@@@@@@@@@@@@");
print(crypto);
print(fiat);
print(torOnly);
try { try {
final fiatStringified = fiat.toString(); final uri = Uri.https(
final uri = Uri.https(fiatApiAuthority, fiatApiPath, torOnly ? _fiatApiOnionAuthority : _fiatApiClearNetAuthority,
<String, String>{'convert': fiatStringified}); _fiatApiPath,
<String, String>{
'interval_count': '1',
'base': crypto.toString(),
'quote': fiat.toString(),
},
);
final response = await get(uri); final response = await get(uri);
if (response.statusCode != 200) { if (response.statusCode != 200) {
@ -23,14 +35,12 @@ Future<double> _fetchPrice(Map<String, dynamic> args) async {
} }
final responseJSON = json.decode(response.body) as Map<String, dynamic>; final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final data = responseJSON['data'] as List<dynamic>; final results = responseJSON['results'] as Map<String, dynamic>;
for (final item in data) { if (results.isNotEmpty) {
if (item['symbol'] == crypto.title) { price = results.values.first as double;
price = item['quote'][fiatStringified]['price'] as double;
break;
}
} }
print(price);
return price; return price;
} catch (e) { } catch (e) {
@ -38,12 +48,14 @@ Future<double> _fetchPrice(Map<String, dynamic> args) async {
} }
} }
Future<double> _fetchPriceAsync( Future<double> _fetchPriceAsync(CryptoCurrency crypto, FiatCurrency fiat, bool torOnly) async =>
CryptoCurrency crypto, FiatCurrency fiat) async => compute(_fetchPrice, {'fiat': fiat, 'crypto': crypto, 'torOnly': torOnly});
compute(_fetchPrice, {'fiat': fiat, 'crypto': crypto});
class FiatConversionService { class FiatConversionService {
static Future<double> fetchPrice( static Future<double> fetchPrice({
CryptoCurrency crypto, FiatCurrency fiat) async => required CryptoCurrency crypto,
await _fetchPriceAsync(crypto, fiat); required FiatCurrency fiat,
required bool torOnly,
}) async =>
await _fetchPriceAsync(crypto, fiat, torOnly);
} }

View file

@ -1,5 +1,6 @@
import 'dart:async'; import 'dart:async';
import 'package:cake_wallet/reactions/fiat_rate_update.dart'; import 'package:cake_wallet/reactions/fiat_rate_update.dart';
import 'package:cake_wallet/reactions/on_current_fiat_api_mode_change.dart';
import 'package:cake_wallet/reactions/on_current_node_change.dart'; import 'package:cake_wallet/reactions/on_current_node_change.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
@ -31,6 +32,7 @@ Future<void> bootstrap(GlobalKey<NavigatorState> navigatorKey) async {
startCurrentWalletChangeReaction( startCurrentWalletChangeReaction(
appStore, settingsStore, fiatConversionStore); appStore, settingsStore, fiatConversionStore);
startCurrentFiatChangeReaction(appStore, settingsStore, fiatConversionStore); startCurrentFiatChangeReaction(appStore, settingsStore, fiatConversionStore);
startCurrentFiatApiModeChangeReaction(appStore, settingsStore, fiatConversionStore);
startOnCurrentNodeChangeReaction(appStore); startOnCurrentNodeChangeReaction(appStore);
startFiatRateUpdate(appStore, settingsStore, fiatConversionStore); startFiatRateUpdate(appStore, settingsStore, fiatConversionStore);
} }

View file

@ -26,7 +26,9 @@ Future<void> startFiatRateUpdate(
} else { } else {
fiatConversionStore.prices[appStore.wallet!.currency] = fiatConversionStore.prices[appStore.wallet!.currency] =
await FiatConversionService.fetchPrice( await FiatConversionService.fetchPrice(
appStore.wallet!.currency, settingsStore.fiatCurrency); crypto: appStore.wallet!.currency,
fiat: settingsStore.fiatCurrency,
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
} }
} catch (e) { } catch (e) {
print(e); print(e);

View file

@ -0,0 +1,25 @@
import 'package:cake_wallet/entities/fiat_api_mode.dart';
import 'package:mobx/mobx.dart';
import 'package:cake_wallet/core/fiat_conversion_service.dart';
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
import 'package:cake_wallet/store/settings_store.dart';
import 'package:cake_wallet/store/app_store.dart';
ReactionDisposer? _onCurrentFiatCurrencyChangeDisposer;
void startCurrentFiatApiModeChangeReaction(AppStore appStore,
SettingsStore settingsStore, FiatConversionStore fiatConversionStore) {
_onCurrentFiatCurrencyChangeDisposer?.reaction.dispose();
_onCurrentFiatCurrencyChangeDisposer = reaction(
(_) => settingsStore.fiatApiMode, (FiatApiMode fiatApiMode) async {
if (appStore.wallet == null || fiatApiMode == FiatApiMode.disabled) {
return;
}
fiatConversionStore.prices[appStore.wallet!.currency] =
await FiatConversionService.fetchPrice(
crypto: appStore.wallet!.currency,
fiat: settingsStore.fiatCurrency,
torOnly: fiatApiMode == FiatApiMode.torOnly);
});
}

View file

@ -18,7 +18,10 @@ void startCurrentFiatChangeReaction(AppStore appStore,
} }
final cryptoCurrency = appStore.wallet!.currency; final cryptoCurrency = appStore.wallet!.currency;
fiatConversionStore.prices[appStore.wallet!.currency] = fiatConversionStore.prices[cryptoCurrency] =
await FiatConversionService.fetchPrice(cryptoCurrency, fiatCurrency); await FiatConversionService.fetchPrice(
crypto: cryptoCurrency,
fiat: fiatCurrency,
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
}); });
} }

View file

@ -71,7 +71,7 @@ void startCurrentWalletChangeReaction(AppStore appStore,
await updateHavenRate(fiatConversionStore); await updateHavenRate(fiatConversionStore);
} }
if (wallet.walletInfo.address?.isEmpty ?? true) { if (wallet.walletInfo.address.isEmpty) {
wallet.walletInfo.address = wallet.walletAddresses.address; wallet.walletInfo.address = wallet.walletAddresses.address;
if (wallet.walletInfo.isInBox) { if (wallet.walletInfo.isInBox) {
@ -95,7 +95,9 @@ void startCurrentWalletChangeReaction(AppStore appStore,
fiatConversionStore.prices[wallet.currency] = 0; fiatConversionStore.prices[wallet.currency] = 0;
fiatConversionStore.prices[wallet.currency] = fiatConversionStore.prices[wallet.currency] =
await FiatConversionService.fetchPrice( await FiatConversionService.fetchPrice(
wallet.currency, settingsStore.fiatCurrency); crypto: wallet.currency,
fiat: settingsStore.fiatCurrency,
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
} catch (e) { } catch (e) {
print(e.toString()); print(e.toString());
} }

View file

@ -1,6 +1,9 @@
import 'package:cake_wallet/entities/fiat_api_mode.dart';
import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/src/screens/base_page.dart'; import 'package:cake_wallet/src/screens/base_page.dart';
import 'package:cake_wallet/src/screens/settings/widgets/settings_choices_cell.dart';
import 'package:cake_wallet/src/screens/settings/widgets/settings_switcher_cell.dart'; import 'package:cake_wallet/src/screens/settings/widgets/settings_switcher_cell.dart';
import 'package:cake_wallet/view_model/settings/choices_list_item.dart';
import 'package:cake_wallet/view_model/settings/privacy_settings_view_model.dart'; import 'package:cake_wallet/view_model/settings/privacy_settings_view_model.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:flutter_mobx/flutter_mobx.dart';
@ -18,29 +21,32 @@ class PrivacyPage extends BasePage {
return Container( return Container(
padding: EdgeInsets.only(top: 10), padding: EdgeInsets.only(top: 10),
child: Observer(builder: (_) { child: Observer(builder: (_) {
return Column( return Column(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
SettingsSwitcherCell( SettingsChoicesCell(
title: S.current.disable_fiat, ChoicesListItem<FiatApiMode>(
value: _privacySettingsViewModel.isFiatDisabled, title: S.current.fiat_api,
items: FiatApiMode.all,
selectedItem: _privacySettingsViewModel.fiatApiMode,
onItemSelected: (FiatApiMode fiatApiMode) =>
_privacySettingsViewModel.setFiatMode(fiatApiMode),
),
),
SettingsSwitcherCell(
title: S.current.disable_exchange,
value: _privacySettingsViewModel.disableExchange,
onValueChange: (BuildContext context, bool value) { onValueChange: (BuildContext context, bool value) {
_privacySettingsViewModel.setFiatMode(value); _privacySettingsViewModel.setEnableExchange(value);
}), }),
SettingsSwitcherCell( SettingsSwitcherCell(
title: S.current.disable_exchange, title: S.current.settings_save_recipient_address,
value: _privacySettingsViewModel.disableExchange, value: _privacySettingsViewModel.shouldSaveRecipientAddress,
onValueChange: (BuildContext context, bool value) { onValueChange: (BuildContext _, bool value) {
_privacySettingsViewModel.setEnableExchange(value); _privacySettingsViewModel.setShouldSaveRecipientAddress(value);
}), }),
SettingsSwitcherCell( ],
title: S.current.settings_save_recipient_address, );
value: _privacySettingsViewModel.shouldSaveRecipientAddress,
onValueChange: (BuildContext _, bool value) {
_privacySettingsViewModel.setShouldSaveRecipientAddress(value);
})
],
);
}), }),
); );
} }

View file

@ -18,7 +18,7 @@ abstract class PrivacySettingsViewModelBase with Store {
bool get shouldSaveRecipientAddress => _settingsStore.shouldSaveRecipientAddress; bool get shouldSaveRecipientAddress => _settingsStore.shouldSaveRecipientAddress;
@computed @computed
bool get isFiatDisabled => _settingsStore.fiatApiMode == FiatApiMode.disabled; FiatApiMode get fiatApiMode => _settingsStore.fiatApiMode;
@action @action
void setShouldSaveRecipientAddress(bool value) => _settingsStore.shouldSaveRecipientAddress = value; void setShouldSaveRecipientAddress(bool value) => _settingsStore.shouldSaveRecipientAddress = value;
@ -27,12 +27,6 @@ abstract class PrivacySettingsViewModelBase with Store {
void setEnableExchange(bool value) => _settingsStore.disableExchange = value; void setEnableExchange(bool value) => _settingsStore.disableExchange = value;
@action @action
void setFiatMode(bool value) { void setFiatMode(FiatApiMode fiatApiMode) => _settingsStore.fiatApiMode = fiatApiMode;
if (value) {
_settingsStore.fiatApiMode = FiatApiMode.disabled;
return;
}
_settingsStore.fiatApiMode = FiatApiMode.enabled;
}
} }