mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2024-11-17 01:37:40 +00:00
Merge pull request #813 from cake-tech/CW-317-change-fiat-api
CW-317 Update fiat API version
This commit is contained in:
commit
9f8e2be777
11 changed files with 134 additions and 105 deletions
|
@ -4,18 +4,31 @@ import 'dart:convert';
|
|||
import 'package:flutter/foundation.dart';
|
||||
import 'package:http/http.dart';
|
||||
|
||||
const fiatApiAuthority = 'fiat-api.cakewallet.com';
|
||||
const fiatApiPath = '/v1/rates';
|
||||
const _fiatApiClearNetAuthority = 'fiat-api.cakewallet.com';
|
||||
const _fiatApiOnionAuthority = 'n4z7bdcmwk2oyddxvzaap3x2peqcplh3pzdy7tpkk5ejz5n4mhfvoxqd.onion';
|
||||
const _fiatApiPath = '/v2/rates';
|
||||
|
||||
Future<double> _fetchPrice(Map<String, dynamic> args) async {
|
||||
final crypto = args['crypto'] as CryptoCurrency;
|
||||
final fiat = args['fiat'] as FiatCurrency;
|
||||
final torOnly = args['torOnly'] as bool;
|
||||
|
||||
final Map<String, String> queryParams = {
|
||||
'interval_count': '1',
|
||||
'base': crypto.toString(),
|
||||
'quote': fiat.toString(),
|
||||
};
|
||||
|
||||
double price = 0.0;
|
||||
|
||||
try {
|
||||
final fiatStringified = fiat.toString();
|
||||
final uri = Uri.https(fiatApiAuthority, fiatApiPath,
|
||||
<String, String>{'convert': fiatStringified});
|
||||
late final Uri uri;
|
||||
if (torOnly) {
|
||||
uri = Uri.http(_fiatApiOnionAuthority, _fiatApiPath, queryParams);
|
||||
} else {
|
||||
uri = Uri.https(_fiatApiClearNetAuthority, _fiatApiPath, queryParams);
|
||||
}
|
||||
|
||||
final response = await get(uri);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
|
@ -23,13 +36,10 @@ Future<double> _fetchPrice(Map<String, dynamic> args) async {
|
|||
}
|
||||
|
||||
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 (item['symbol'] == crypto.title) {
|
||||
price = item['quote'][fiatStringified]['price'] as double;
|
||||
break;
|
||||
}
|
||||
if (results.isNotEmpty) {
|
||||
price = results.values.first as double;
|
||||
}
|
||||
|
||||
return price;
|
||||
|
@ -38,12 +48,14 @@ Future<double> _fetchPrice(Map<String, dynamic> args) async {
|
|||
}
|
||||
}
|
||||
|
||||
Future<double> _fetchPriceAsync(
|
||||
CryptoCurrency crypto, FiatCurrency fiat) async =>
|
||||
compute(_fetchPrice, {'fiat': fiat, 'crypto': crypto});
|
||||
Future<double> _fetchPriceAsync(CryptoCurrency crypto, FiatCurrency fiat, bool torOnly) async =>
|
||||
compute(_fetchPrice, {'fiat': fiat, 'crypto': crypto, 'torOnly': torOnly});
|
||||
|
||||
class FiatConversionService {
|
||||
static Future<double> fetchPrice(
|
||||
CryptoCurrency crypto, FiatCurrency fiat) async =>
|
||||
await _fetchPriceAsync(crypto, fiat);
|
||||
static Future<double> fetchPrice({
|
||||
required CryptoCurrency crypto,
|
||||
required FiatCurrency fiat,
|
||||
required bool torOnly,
|
||||
}) async =>
|
||||
await _fetchPriceAsync(crypto, fiat, torOnly);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import 'dart:async';
|
||||
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:flutter/cupertino.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
@ -31,6 +32,7 @@ Future<void> bootstrap(GlobalKey<NavigatorState> navigatorKey) async {
|
|||
startCurrentWalletChangeReaction(
|
||||
appStore, settingsStore, fiatConversionStore);
|
||||
startCurrentFiatChangeReaction(appStore, settingsStore, fiatConversionStore);
|
||||
startCurrentFiatApiModeChangeReaction(appStore, settingsStore, fiatConversionStore);
|
||||
startOnCurrentNodeChangeReaction(appStore);
|
||||
startFiatRateUpdate(appStore, settingsStore, fiatConversionStore);
|
||||
}
|
||||
|
|
|
@ -26,7 +26,9 @@ Future<void> startFiatRateUpdate(
|
|||
} else {
|
||||
fiatConversionStore.prices[appStore.wallet!.currency] =
|
||||
await FiatConversionService.fetchPrice(
|
||||
appStore.wallet!.currency, settingsStore.fiatCurrency);
|
||||
crypto: appStore.wallet!.currency,
|
||||
fiat: settingsStore.fiatCurrency,
|
||||
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
|
||||
}
|
||||
} catch (e) {
|
||||
print(e);
|
||||
|
|
25
lib/reactions/on_current_fiat_api_mode_change.dart
Normal file
25
lib/reactions/on_current_fiat_api_mode_change.dart
Normal 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);
|
||||
});
|
||||
}
|
|
@ -18,7 +18,10 @@ void startCurrentFiatChangeReaction(AppStore appStore,
|
|||
}
|
||||
|
||||
final cryptoCurrency = appStore.wallet!.currency;
|
||||
fiatConversionStore.prices[appStore.wallet!.currency] =
|
||||
await FiatConversionService.fetchPrice(cryptoCurrency, fiatCurrency);
|
||||
fiatConversionStore.prices[cryptoCurrency] =
|
||||
await FiatConversionService.fetchPrice(
|
||||
crypto: cryptoCurrency,
|
||||
fiat: fiatCurrency,
|
||||
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -71,7 +71,7 @@ void startCurrentWalletChangeReaction(AppStore appStore,
|
|||
await updateHavenRate(fiatConversionStore);
|
||||
}
|
||||
|
||||
if (wallet.walletInfo.address?.isEmpty ?? true) {
|
||||
if (wallet.walletInfo.address.isEmpty) {
|
||||
wallet.walletInfo.address = wallet.walletAddresses.address;
|
||||
|
||||
if (wallet.walletInfo.isInBox) {
|
||||
|
@ -95,7 +95,9 @@ void startCurrentWalletChangeReaction(AppStore appStore,
|
|||
fiatConversionStore.prices[wallet.currency] = 0;
|
||||
fiatConversionStore.prices[wallet.currency] =
|
||||
await FiatConversionService.fetchPrice(
|
||||
wallet.currency, settingsStore.fiatCurrency);
|
||||
crypto: wallet.currency,
|
||||
fiat: settingsStore.fiatCurrency,
|
||||
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
|
||||
} catch (e) {
|
||||
print(e.toString());
|
||||
}
|
||||
|
|
|
@ -52,18 +52,18 @@ class _AdvancedPrivacySettingsBodyState extends State<AdvancedPrivacySettingsBod
|
|||
content: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Observer(
|
||||
builder: (_) {
|
||||
return SettingsSwitcherCell(
|
||||
Observer(builder: (_) {
|
||||
return SettingsChoicesCell(
|
||||
ChoicesListItem<FiatApiMode>(
|
||||
title: S.current.disable_fiat,
|
||||
value: widget.privacySettingsViewModel.fiatApi == FiatApiMode.disabled,
|
||||
onValueChange: (BuildContext context, bool value) {
|
||||
widget.privacySettingsViewModel.setFiatMode(value);
|
||||
});
|
||||
}
|
||||
items: FiatApiMode.all,
|
||||
selectedItem: widget.privacySettingsViewModel.fiatApiMode,
|
||||
onItemSelected: (FiatApiMode mode) =>
|
||||
widget.privacySettingsViewModel.setFiatApiMode(mode),
|
||||
),
|
||||
Observer(
|
||||
builder: (_) {
|
||||
);
|
||||
}),
|
||||
Observer(builder: (_) {
|
||||
return SettingsChoicesCell(
|
||||
ChoicesListItem<ExchangeApiMode>(
|
||||
title: S.current.exchange,
|
||||
|
@ -73,10 +73,8 @@ class _AdvancedPrivacySettingsBodyState extends State<AdvancedPrivacySettingsBod
|
|||
widget.privacySettingsViewModel.setExchangeApiMode(mode),
|
||||
),
|
||||
);
|
||||
}
|
||||
),
|
||||
Observer(
|
||||
builder: (_) {
|
||||
}),
|
||||
Observer(builder: (_) {
|
||||
return Column(
|
||||
children: [
|
||||
SettingsSwitcherCell(
|
||||
|
@ -94,9 +92,7 @@ class _AdvancedPrivacySettingsBodyState extends State<AdvancedPrivacySettingsBod
|
|||
)
|
||||
],
|
||||
);
|
||||
}
|
||||
),
|
||||
|
||||
}),
|
||||
],
|
||||
),
|
||||
bottomSectionPadding: EdgeInsets.all(24),
|
||||
|
|
|
@ -25,12 +25,15 @@ class PrivacyPage extends BasePage {
|
|||
return Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
SettingsSwitcherCell(
|
||||
title: S.current.disable_fiat,
|
||||
value: _privacySettingsViewModel.isFiatDisabled,
|
||||
onValueChange: (BuildContext context, bool value) {
|
||||
_privacySettingsViewModel.setFiatMode(value);
|
||||
}),
|
||||
SettingsChoicesCell(
|
||||
ChoicesListItem<FiatApiMode>(
|
||||
title: S.current.fiat_api,
|
||||
items: FiatApiMode.all,
|
||||
selectedItem: _privacySettingsViewModel.fiatApiMode,
|
||||
onItemSelected: (FiatApiMode fiatApiMode) =>
|
||||
_privacySettingsViewModel.setFiatMode(fiatApiMode),
|
||||
),
|
||||
),
|
||||
SettingsChoicesCell(
|
||||
ChoicesListItem<ExchangeApiMode>(
|
||||
title: S.current.exchange,
|
||||
|
@ -44,7 +47,7 @@ class PrivacyPage extends BasePage {
|
|||
value: _privacySettingsViewModel.shouldSaveRecipientAddress,
|
||||
onValueChange: (BuildContext _, bool value) {
|
||||
_privacySettingsViewModel.setShouldSaveRecipientAddress(value);
|
||||
})
|
||||
}),
|
||||
],
|
||||
);
|
||||
}),
|
||||
|
|
|
@ -16,7 +16,7 @@ abstract class AdvancedPrivacySettingsViewModelBase with Store {
|
|||
ExchangeApiMode get exchangeStatus => _settingsStore.exchangeStatus;
|
||||
|
||||
@computed
|
||||
FiatApiMode get fiatApi => _settingsStore.fiatApiMode;
|
||||
FiatApiMode get fiatApiMode => _settingsStore.fiatApiMode;
|
||||
|
||||
@observable
|
||||
bool _addCustomNode = false;
|
||||
|
@ -29,21 +29,11 @@ abstract class AdvancedPrivacySettingsViewModelBase with Store {
|
|||
bool get addCustomNode => _addCustomNode;
|
||||
|
||||
@action
|
||||
void setFiatMode(bool value) {
|
||||
if (value) {
|
||||
_settingsStore.fiatApiMode = FiatApiMode.disabled;
|
||||
return;
|
||||
}
|
||||
_settingsStore.fiatApiMode = FiatApiMode.enabled;
|
||||
}
|
||||
void setFiatApiMode(FiatApiMode fiatApiMode) => _settingsStore.fiatApiMode = fiatApiMode;
|
||||
|
||||
@action
|
||||
void setExchangeApiMode(ExchangeApiMode value) {
|
||||
_settingsStore.exchangeStatus = value;
|
||||
}
|
||||
void setExchangeApiMode(ExchangeApiMode value) => _settingsStore.exchangeStatus = value;
|
||||
|
||||
@action
|
||||
void toggleAddCustomNode() {
|
||||
_addCustomNode = !_addCustomNode;
|
||||
}
|
||||
void toggleAddCustomNode() => _addCustomNode = !_addCustomNode;
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ abstract class PrivacySettingsViewModelBase with Store {
|
|||
bool get shouldSaveRecipientAddress => _settingsStore.shouldSaveRecipientAddress;
|
||||
|
||||
@computed
|
||||
bool get isFiatDisabled => _settingsStore.fiatApiMode == FiatApiMode.disabled;
|
||||
FiatApiMode get fiatApiMode => _settingsStore.fiatApiMode;
|
||||
|
||||
@action
|
||||
void setShouldSaveRecipientAddress(bool value) => _settingsStore.shouldSaveRecipientAddress = value;
|
||||
|
@ -28,12 +28,6 @@ abstract class PrivacySettingsViewModelBase with Store {
|
|||
void setExchangeApiMode(ExchangeApiMode value) => _settingsStore.exchangeStatus = value;
|
||||
|
||||
@action
|
||||
void setFiatMode(bool value) {
|
||||
if (value) {
|
||||
_settingsStore.fiatApiMode = FiatApiMode.disabled;
|
||||
return;
|
||||
}
|
||||
_settingsStore.fiatApiMode = FiatApiMode.enabled;
|
||||
}
|
||||
void setFiatMode(FiatApiMode fiatApiMode) => _settingsStore.fiatApiMode = fiatApiMode;
|
||||
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ dependencies:
|
|||
barcode_scan2: ^4.2.1
|
||||
http: ^0.13.4
|
||||
path_provider: ^2.0.11
|
||||
mobx: ^2.0.7+4
|
||||
mobx: 2.0.7+4
|
||||
flutter_mobx: ^2.0.6+1
|
||||
flutter_slidable: ^2.0.0
|
||||
share_plus: ^4.0.10
|
||||
|
|
Loading…
Reference in a new issue