diff --git a/assets/images/trocador.png b/assets/images/trocador.png new file mode 100644 index 000000000..67c9f221c Binary files /dev/null and b/assets/images/trocador.png differ diff --git a/lib/entities/preferences_key.dart b/lib/entities/preferences_key.dart index 90d4dcad7..2eafb836c 100644 --- a/lib/entities/preferences_key.dart +++ b/lib/entities/preferences_key.dart @@ -13,6 +13,7 @@ class PreferencesKey { static const allowBiometricalAuthenticationKey = 'allow_biometrical_authentication'; static const disableExchangeKey = 'disable_exchange'; + static const exchangeStatusKey = 'exchange_status'; static const currentTheme = 'current_theme'; static const isDarkThemeLegacy = 'dark_theme'; static const displayActionListModeKey = 'display_list_mode'; diff --git a/lib/exchange/changenow/changenow_exchange_provider.dart b/lib/exchange/changenow/changenow_exchange_provider.dart index fc60be18e..88f52cd70 100644 --- a/lib/exchange/changenow/changenow_exchange_provider.dart +++ b/lib/exchange/changenow/changenow_exchange_provider.dart @@ -269,6 +269,7 @@ class ChangeNowExchangeProvider extends ExchangeProvider { : currency.title.toLowerCase(); } } + } String normalizeCryptoCurrency(CryptoCurrency currency) { diff --git a/lib/exchange/exchange_provider.dart b/lib/exchange/exchange_provider.dart index 763350c94..99e6da913 100644 --- a/lib/exchange/exchange_provider.dart +++ b/lib/exchange/exchange_provider.dart @@ -14,6 +14,7 @@ abstract class ExchangeProvider { bool get isAvailable; bool get isEnabled; bool get supportsFixedRate; + bool get shouldUseOnionAddress => false; @override String toString() => title; diff --git a/lib/exchange/exchange_provider_description.dart b/lib/exchange/exchange_provider_description.dart index 2fd231085..e545f69ce 100644 --- a/lib/exchange/exchange_provider_description.dart +++ b/lib/exchange/exchange_provider_description.dart @@ -1,31 +1,30 @@ import 'package:cw_core/enumerable_item.dart'; -class ExchangeProviderDescription extends EnumerableItem - with Serializable { - const ExchangeProviderDescription({ - required String title, - required int raw, - required this.image, - this.horizontalLogo = false}) +class ExchangeProviderDescription extends EnumerableItem with Serializable { + const ExchangeProviderDescription( + {required String title, required int raw, required this.image, this.horizontalLogo = false}) : super(title: title, raw: raw); final bool horizontalLogo; final String image; - static const xmrto = ExchangeProviderDescription(title: 'XMR.TO', raw: 0, image: 'assets/images/xmrto.png'); + static const xmrto = + ExchangeProviderDescription(title: 'XMR.TO', raw: 0, image: 'assets/images/xmrto.png'); static const changeNow = ExchangeProviderDescription(title: 'ChangeNOW', raw: 1, image: 'assets/images/changenow.png'); static const morphToken = ExchangeProviderDescription(title: 'MorphToken', raw: 2, image: 'assets/images/morph.png'); - static const sideShift = + static const sideShift = ExchangeProviderDescription(title: 'SideShift', raw: 3, image: 'assets/images/sideshift.png'); - static const simpleSwap = - ExchangeProviderDescription(title: 'SimpleSwap', raw: 4, image: 'assets/images/simpleSwap.png'); + static const simpleSwap = ExchangeProviderDescription( + title: 'SimpleSwap', raw: 4, image: 'assets/images/simpleSwap.png'); - static const all = - ExchangeProviderDescription(title: 'All trades', raw: 5, image:''); + static const trocador = + ExchangeProviderDescription(title: 'Trocador', raw: 5, image: 'assets/images/trocador.png'); + + static const all = ExchangeProviderDescription(title: 'All trades', raw: 6, image: ''); static ExchangeProviderDescription deserialize({required int raw}) { switch (raw) { @@ -40,6 +39,8 @@ class ExchangeProviderDescription extends EnumerableItem case 4: return simpleSwap; case 5: + return trocador; + case 6: return all; default: throw Exception('Unexpected token: $raw for ExchangeProviderDescription deserialize'); diff --git a/lib/exchange/trocador/trocador_exchange_provider.dart b/lib/exchange/trocador/trocador_exchange_provider.dart new file mode 100644 index 000000000..18ea27895 --- /dev/null +++ b/lib/exchange/trocador/trocador_exchange_provider.dart @@ -0,0 +1,268 @@ +import 'dart:convert'; + +import 'package:cake_wallet/exchange/exchange_pair.dart'; +import 'package:cake_wallet/exchange/exchange_provider.dart'; +import 'package:cake_wallet/exchange/trade_state.dart'; +import 'package:cake_wallet/exchange/trocador/trocador_request.dart'; +import 'package:cw_core/crypto_currency.dart'; +import 'package:cake_wallet/exchange/trade_request.dart'; +import 'package:cake_wallet/exchange/trade.dart'; +import 'package:cake_wallet/exchange/limits.dart'; +import 'package:cake_wallet/exchange/exchange_provider_description.dart'; +import 'package:cake_wallet/.secrets.g.dart' as secrets; +import 'package:http/http.dart'; + +class TrocadorExchangeProvider extends ExchangeProvider { + TrocadorExchangeProvider() + : _lastUsedRateId = '', + super(pairList: _supportedPairs()); + + static const List _notSupported = [ + CryptoCurrency.xhv, + CryptoCurrency.dcr, + CryptoCurrency.oxt, + CryptoCurrency.pivx, + CryptoCurrency.scrt, + CryptoCurrency.stx, + CryptoCurrency.bttc, + CryptoCurrency.zaddr, + CryptoCurrency.usdcpoly, + CryptoCurrency.maticpoly, + ]; + + static List _supportedPairs() { + final supportedCurrencies = + CryptoCurrency.all.where((element) => !_notSupported.contains(element)).toList(); + + return supportedCurrencies + .map((i) => supportedCurrencies.map((k) => ExchangePair(from: i, to: k, reverse: true))) + .expand((i) => i) + .toList(); + } + + static const onionApiAuthority = 'trocadorfyhlu27aefre5u7zri66gudtzdyelymftvr4yjwcxhfaqsid.onion'; + static const clearNetAuthority = 'trocador.app'; + static const apiKey = secrets.trocadorApiKey; + static const newRatePath = '/api/new_rate'; + static const createTradePath = 'api/new_trade'; + static const tradePath = 'api/trade'; + String _lastUsedRateId; + + @override + Future checkIsAvailable() async => true; + + @override + Future createTrade({required TradeRequest request, required bool isFixedRateMode}) { + final _request = request as TrocadorRequest; + return _createTrade(request: _request, isFixedRateMode: isFixedRateMode); + } + + Future _createTrade({ + required TrocadorRequest request, + required bool isFixedRateMode, + }) async { + final params = { + 'api_key': apiKey, + 'ticker_from': request.from.title.toLowerCase(), + 'ticker_to': request.to.title.toLowerCase(), + 'network_from': _networkFor(request.from), + 'network_to': _networkFor(request.to), + 'payment': isFixedRateMode ? 'True' : 'False', + 'min_kycrating': 'C', + 'markup': '3', + 'best_only': 'True', + if (!isFixedRateMode) 'amount_from': request.fromAmount, + if (isFixedRateMode) 'amount_to': request.toAmount, + 'address': request.address, + 'refund': request.refundAddress + }; + + if (isFixedRateMode) { + await fetchRate( + from: request.from, + to: request.to, + amount: double.tryParse(request.toAmount) ?? 0, + isFixedRateMode: true, + isReceiveAmount: true, + ); + params['id'] = _lastUsedRateId; + } + + final String apiAuthority = shouldUseOnionAddress ? await _getAuthority() : clearNetAuthority; + + final uri = Uri.https(apiAuthority, createTradePath, params); + final response = await get(uri); + + if (response.statusCode == 400) { + final responseJSON = json.decode(response.body) as Map; + final error = responseJSON['error'] as String; + final message = responseJSON['message'] as String; + throw Exception('${error}\n$message'); + } + + if (response.statusCode != 200) { + throw Exception('Unexpected http status: ${response.statusCode}'); + } + + final responseJSON = json.decode(response.body) as Map; + final id = responseJSON['trade_id'] as String; + final inputAddress = responseJSON['address_provider'] as String; + final refundAddress = responseJSON['refund_address'] as String; + final status = responseJSON['status'] as String; + final state = TradeState.deserialize(raw: status); + final payoutAddress = responseJSON['address_user'] as String; + final date = responseJSON['date'] as String; + + return Trade( + id: id, + from: request.from, + to: request.to, + provider: description, + inputAddress: inputAddress, + refundAddress: refundAddress, + state: state, + createdAt: DateTime.tryParse(date)?.toLocal(), + amount: responseJSON['amount_from']?.toString() ?? request.fromAmount, + payoutAddress: payoutAddress); + } + + @override + ExchangeProviderDescription get description => ExchangeProviderDescription.trocador; + + @override + Future fetchLimits( + {required CryptoCurrency from, + required CryptoCurrency to, + required bool isFixedRateMode}) async { + //TODO: implement limits from trocador api + return Limits( + min: 0.0, + ); + } + + @override + Future fetchRate( + {required CryptoCurrency from, + required CryptoCurrency to, + required double amount, + required bool isFixedRateMode, + required bool isReceiveAmount}) async { + try { + if (amount == 0) { + return 0.0; + } + + final String apiAuthority = shouldUseOnionAddress ? await _getAuthority() : clearNetAuthority; + + final params = { + 'api_key': apiKey, + 'ticker_from': from.title.toLowerCase(), + 'ticker_to': to.title.toLowerCase(), + 'network_from': _networkFor(from), + 'network_to': _networkFor(to), + 'amount_from': amount.toString(), + 'amount_to': amount.toString(), + 'payment': isFixedRateMode ? 'True' : 'False', + 'min_kycrating': 'C', + 'markup': '3', + 'best_only': 'True', + }; + + final uri = Uri.https(apiAuthority, newRatePath, params); + final response = await get(uri); + final responseJSON = json.decode(response.body) as Map; + final fromAmount = double.parse(responseJSON['amount_from'].toString()); + final toAmount = double.parse(responseJSON['amount_to'].toString()); + final rateId = responseJSON['trade_id'] as String? ?? ''; + + if (rateId.isNotEmpty) { + _lastUsedRateId = rateId; + } + + return isReceiveAmount ? (amount / fromAmount) : (toAmount / amount); + } catch (e) { + print(e.toString()); + return 0.0; + } + } + + @override + Future findTradeById({required String id}) async { + final String apiAuthority = shouldUseOnionAddress ? await _getAuthority() : clearNetAuthority; + final uri = Uri.https(apiAuthority, tradePath, {'api_key': apiKey, 'id': id}); + return get(uri).then((response) { + if (response.statusCode != 200) { + throw Exception('Unexpected http status: ${response.statusCode}'); + } + + final responseListJson = json.decode(response.body) as List; + + final responseJSON = responseListJson.first; + final id = responseJSON['trade_id'] as String; + final inputAddress = responseJSON['address_user'] as String; + final refundAddress = responseJSON['refund_address'] as String; + final payoutAddress = responseJSON['address_provider'] as String; + final fromAmount = responseJSON['amount_from']?.toString() ?? '0'; + final from = CryptoCurrency.fromString(responseJSON['ticker_from'] as String); + final to = CryptoCurrency.fromString(responseJSON['ticker_to'] as String); + final state = TradeState.deserialize(raw: responseJSON['status'] as String); + final date = DateTime.parse(responseJSON['date'] as String); + + return Trade( + id: id, + from: from, + to: to, + provider: description, + inputAddress: inputAddress, + refundAddress: refundAddress, + createdAt: date, + amount: fromAmount, + state: state, + payoutAddress: payoutAddress, + ); + }); + } + + @override + bool get isAvailable => true; + + @override + bool get isEnabled => true; + + @override + bool get supportsFixedRate => true; + + @override + bool get shouldUseOnionAddress => true; + + @override + String get title => 'Trocador'; + + String _networkFor(CryptoCurrency currency) { + switch (currency) { + case CryptoCurrency.usdt: + return CryptoCurrency.btc.title.toLowerCase(); + default: + return currency.tag != null ? _normalizeTag(currency.tag!) : 'Mainnet'; + } + } + + String _normalizeTag(String tag) { + switch (tag) { + case 'ETH': + return 'ERC20'; + default: + return tag.toLowerCase(); + } + } + + Future _getAuthority() async { + try { + final uri = Uri.https(onionApiAuthority, '/api/trade'); + await get(uri); + return onionApiAuthority; + } catch (e) { + return clearNetAuthority; + } + } +} diff --git a/lib/exchange/trocador/trocador_request.dart b/lib/exchange/trocador/trocador_request.dart new file mode 100644 index 000000000..fbb8fdc84 --- /dev/null +++ b/lib/exchange/trocador/trocador_request.dart @@ -0,0 +1,21 @@ +import 'package:cake_wallet/exchange/trade_request.dart'; +import 'package:cw_core/crypto_currency.dart'; + +class TrocadorRequest extends TradeRequest { + TrocadorRequest( + {required this.from, + required this.to, + required this.address, + required this.fromAmount, + required this.toAmount, + required this.refundAddress, + required this.isReverse}); + + CryptoCurrency from; + CryptoCurrency to; + String address; + String fromAmount; + String toAmount; + String refundAddress; + bool isReverse; +} diff --git a/lib/src/screens/dashboard/widgets/trade_row.dart b/lib/src/screens/dashboard/widgets/trade_row.dart index 21952140e..3b613b0e2 100644 --- a/lib/src/screens/dashboard/widgets/trade_row.dart +++ b/lib/src/screens/dashboard/widgets/trade_row.dart @@ -9,7 +9,8 @@ class TradeRow extends StatelessWidget { required this.to, required this.createdAtFormattedDate, this.onTap, - this.formattedAmount,}); + this.formattedAmount, + }); final VoidCallback? onTap; final ExchangeProviderDescription provider; @@ -35,47 +36,40 @@ class TradeRow extends StatelessWidget { SizedBox(width: 12), Expanded( child: Column( - mainAxisSize: MainAxisSize.min, - children: [ - Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Text('${from.toString()} → ${to.toString()}', - style: TextStyle( - fontSize: 16, - fontWeight: FontWeight.w500, - color: Theme.of(context).accentTextTheme!.headline2!.backgroundColor! - )), - formattedAmount != null - ? Text(formattedAmount! + ' ' + amountCrypto, - style: TextStyle( - fontSize: 16, - fontWeight: FontWeight.w500, - color: Theme.of(context).accentTextTheme!.headline2!.backgroundColor! - )) - : Container() - ]), - SizedBox(height: 5), - Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - if (createdAtFormattedDate != null) - Text(createdAtFormattedDate!, - style: TextStyle( - fontSize: 14, - color: Theme.of(context).textTheme! - .overline!.backgroundColor!)) - ]) - ], - ) - ) + mainAxisSize: MainAxisSize.min, + children: [ + Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ + Text('${from.toString()} → ${to.toString()}', + style: TextStyle( + fontSize: 16, + fontWeight: FontWeight.w500, + color: Theme.of(context).accentTextTheme!.headline2!.backgroundColor!)), + formattedAmount != null + ? Text(formattedAmount! + ' ' + amountCrypto, + style: TextStyle( + fontSize: 16, + fontWeight: FontWeight.w500, + color: + Theme.of(context).accentTextTheme!.headline2!.backgroundColor!)) + : Container() + ]), + SizedBox(height: 5), + Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ + if (createdAtFormattedDate != null) + Text(createdAtFormattedDate!, + style: TextStyle( + fontSize: 14, + color: Theme.of(context).textTheme!.overline!.backgroundColor!)) + ]) + ], + )) ], ), )); } - Image? _getPoweredImage(ExchangeProviderDescription provider) { - Image? image; + Widget? _getPoweredImage(ExchangeProviderDescription provider) { + Widget? image; switch (provider) { case ExchangeProviderDescription.xmrto: @@ -93,10 +87,15 @@ class TradeRow extends StatelessWidget { case ExchangeProviderDescription.simpleSwap: image = Image.asset('assets/images/simpleSwap.png', width: 36, height: 36); break; + case ExchangeProviderDescription.trocador: + image = ClipRRect( + borderRadius: BorderRadius.circular(50), + child: Image.asset('assets/images/trocador.png', width: 36, height: 36)); + break; default: image = null; } return image; } -} \ No newline at end of file +} diff --git a/lib/src/screens/new_wallet/advanced_privacy_settings_page.dart b/lib/src/screens/new_wallet/advanced_privacy_settings_page.dart index 6c2f996df..2a9f4d291 100644 --- a/lib/src/screens/new_wallet/advanced_privacy_settings_page.dart +++ b/lib/src/screens/new_wallet/advanced_privacy_settings_page.dart @@ -1,7 +1,11 @@ +import 'package:cake_wallet/entities/fiat_api_mode.dart'; import 'package:cake_wallet/src/screens/nodes/widgets/node_form.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/view_model/node_list/node_create_or_edit_view_model.dart'; import 'package:cake_wallet/view_model/advanced_privacy_settings_view_model.dart'; +import 'package:cake_wallet/view_model/settings/choices_list_item.dart'; +import 'package:cake_wallet/view_model/settings/switcher_list_item.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:flutter/material.dart'; import 'package:cake_wallet/generated/i18n.dart'; @@ -48,9 +52,14 @@ class _AdvancedPrivacySettingsBodyState extends State>().map( (item) => Observer( - builder: (_) => SettingsSwitcherCell( + builder: (_) => SettingsChoicesCell(item) + ), + ), + ...widget.privacySettingsViewModel.settings.whereType().map( + (item) => Observer( + builder: (_) => SettingsSwitcherCell( title: item.title, value: item.value(), onValueChange: item.onValueChange, diff --git a/lib/src/screens/settings/privacy_page.dart b/lib/src/screens/settings/privacy_page.dart index 2f15cc225..974f9a3d7 100644 --- a/lib/src/screens/settings/privacy_page.dart +++ b/lib/src/screens/settings/privacy_page.dart @@ -1,6 +1,9 @@ +import 'package:cake_wallet/entities/fiat_api_mode.dart'; import 'package:cake_wallet/generated/i18n.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/view_model/settings/choices_list_item.dart'; import 'package:cake_wallet/view_model/settings/privacy_settings_view_model.dart'; import 'package:flutter/material.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; @@ -21,18 +24,22 @@ 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); - }), - SettingsSwitcherCell( - title: S.current.disable_exchange, - value: _privacySettingsViewModel.disableExchange, - onValueChange: (BuildContext context, bool value) { - _privacySettingsViewModel.setEnableExchange(value); - }), + SettingsChoicesCell( + ChoicesListItem( + title: S.current.fiat_api, + items: FiatApiMode.all, + selectedItem: _privacySettingsViewModel.fiatApi, + onItemSelected: (FiatApiMode mode) => _privacySettingsViewModel.setFiatMode(mode), + ), + ), + SettingsChoicesCell( + ChoicesListItem( + title: S.current.exchange, + items: FiatApiMode.all, + selectedItem: _privacySettingsViewModel.exchangeStatus, + onItemSelected: (FiatApiMode mode) => _privacySettingsViewModel.setEnableExchange(mode), + ), + ), SettingsSwitcherCell( title: S.current.settings_save_recipient_address, value: _privacySettingsViewModel.shouldSaveRecipientAddress, diff --git a/lib/store/dashboard/trade_filter_store.dart b/lib/store/dashboard/trade_filter_store.dart index 87fa749a9..c772a35d6 100644 --- a/lib/store/dashboard/trade_filter_store.dart +++ b/lib/store/dashboard/trade_filter_store.dart @@ -12,7 +12,8 @@ abstract class TradeFilterStoreBase with Store { displayChangeNow = true, displaySideShift = true, displayMorphToken = true, - displaySimpleSwap = true; + displaySimpleSwap = true, + displayTrocador = true; @observable bool displayXMRTO; @@ -29,8 +30,11 @@ abstract class TradeFilterStoreBase with Store { @observable bool displaySimpleSwap; + @observable + bool displayTrocador; + @computed - bool get displayAllTrades => displayChangeNow && displaySideShift && displaySimpleSwap; + bool get displayAllTrades => displayChangeNow && displaySideShift && displaySimpleSwap && displayTrocador; @action void toggleDisplayExchange(ExchangeProviderDescription provider) { @@ -50,6 +54,9 @@ abstract class TradeFilterStoreBase with Store { case ExchangeProviderDescription.morphToken: displayMorphToken = !displayMorphToken; break; + case ExchangeProviderDescription.trocador: + displayTrocador = !displayTrocador; + break; case ExchangeProviderDescription.all: if (displayAllTrades) { displayChangeNow = false; @@ -57,12 +64,14 @@ abstract class TradeFilterStoreBase with Store { displayXMRTO = false; displayMorphToken = false; displaySimpleSwap = false; + displayTrocador = false; } else { displayChangeNow = true; displaySideShift = true; displayXMRTO = true; displayMorphToken = true; displaySimpleSwap = true; + displayTrocador = true; } break; } @@ -88,7 +97,8 @@ abstract class TradeFilterStoreBase with Store { ExchangeProviderDescription.morphToken) ||(displaySimpleSwap && item.trade.provider == - ExchangeProviderDescription.simpleSwap)) + ExchangeProviderDescription.simpleSwap) + ||(displayTrocador && item.trade.provider == ExchangeProviderDescription.trocador)) .toList() : _trades; } diff --git a/lib/store/settings_store.dart b/lib/store/settings_store.dart index b10e0d08d..989fdae33 100644 --- a/lib/store/settings_store.dart +++ b/lib/store/settings_store.dart @@ -31,7 +31,7 @@ abstract class SettingsStoreBase with Store { required bool initialSaveRecipientAddress, required FiatApiMode initialFiatMode, required bool initialAllowBiometricalAuthentication, - required bool initialExchangeEnabled, + required FiatApiMode initialExchangeStatus, required ThemeBase initialTheme, required int initialPinLength, required String initialLanguageCode, @@ -53,7 +53,7 @@ abstract class SettingsStoreBase with Store { shouldSaveRecipientAddress = initialSaveRecipientAddress, fiatApiMode = initialFiatMode, allowBiometricalAuthentication = initialAllowBiometricalAuthentication, - disableExchange = initialExchangeEnabled, + exchangeStatus = initialExchangeStatus, currentTheme = initialTheme, pinCodeLength = initialPinLength, languageCode = initialLanguageCode, @@ -153,9 +153,9 @@ abstract class SettingsStoreBase with Store { PreferencesKey.currentBalanceDisplayModeKey, mode.serialize())); reaction( - (_) => disableExchange, - (bool disableExchange) => sharedPreferences.setBool( - PreferencesKey.disableExchangeKey, disableExchange)); + (_) => exchangeStatus, + (FiatApiMode mode) => sharedPreferences.setInt( + PreferencesKey.exchangeStatusKey, mode.serialize())); this .nodes @@ -192,7 +192,7 @@ abstract class SettingsStoreBase with Store { bool allowBiometricalAuthentication; @observable - bool disableExchange; + FiatApiMode exchangeStatus; @observable ThemeBase currentTheme; @@ -284,8 +284,9 @@ abstract class SettingsStoreBase with Store { final allowBiometricalAuthentication = sharedPreferences .getBool(PreferencesKey.allowBiometricalAuthenticationKey) ?? false; - final disableExchange = sharedPreferences - .getBool(PreferencesKey.disableExchangeKey) ?? false; + final exchangeStatus = FiatApiMode.deserialize( + raw: sharedPreferences + .getInt(PreferencesKey.exchangeStatusKey) ?? FiatApiMode.enabled.raw); final legacyTheme = (sharedPreferences.getBool(PreferencesKey.isDarkThemeLegacy) ?? false) ? ThemeType.dark.index @@ -354,7 +355,7 @@ abstract class SettingsStoreBase with Store { initialSaveRecipientAddress: shouldSaveRecipientAddress, initialFiatMode: currentFiatApiMode, initialAllowBiometricalAuthentication: allowBiometricalAuthentication, - initialExchangeEnabled: disableExchange, + initialExchangeStatus: exchangeStatus, initialTheme: savedTheme, actionlistDisplayMode: actionListDisplayMode, initialPinLength: pinLength, @@ -400,7 +401,9 @@ abstract class SettingsStoreBase with Store { allowBiometricalAuthentication = sharedPreferences .getBool(PreferencesKey.allowBiometricalAuthenticationKey) ?? allowBiometricalAuthentication; - disableExchange = sharedPreferences.getBool(PreferencesKey.disableExchangeKey) ?? disableExchange; + exchangeStatus = FiatApiMode.deserialize( + raw: sharedPreferences + .getInt(PreferencesKey.exchangeStatusKey) ?? FiatApiMode.enabled.raw); final legacyTheme = (sharedPreferences.getBool(PreferencesKey.isDarkThemeLegacy) ?? false) ? ThemeType.dark.index diff --git a/lib/view_model/advanced_privacy_settings_view_model.dart b/lib/view_model/advanced_privacy_settings_view_model.dart index f800e3418..80b62fdd0 100644 --- a/lib/view_model/advanced_privacy_settings_view_model.dart +++ b/lib/view_model/advanced_privacy_settings_view_model.dart @@ -1,5 +1,7 @@ import 'package:cake_wallet/entities/fiat_api_mode.dart'; import 'package:cake_wallet/store/settings_store.dart'; +import 'package:cake_wallet/view_model/settings/choices_list_item.dart'; +import 'package:cake_wallet/view_model/settings/settings_list_item.dart'; import 'package:cake_wallet/view_model/settings/switcher_list_item.dart'; import 'package:cw_core/wallet_type.dart'; import 'package:mobx/mobx.dart'; @@ -14,18 +16,19 @@ abstract class AdvancedPrivacySettingsViewModelBase with Store { AdvancedPrivacySettingsViewModelBase(this.type, this._settingsStore) : _addCustomNode = false { settings = [ - SwitcherListItem( - title: S.current.disable_fiat, - value: () => _settingsStore.fiatApiMode == FiatApiMode.disabled, - onValueChange: (_, bool value) => setFiatMode(value), - ), - SwitcherListItem( - title: S.current.disable_exchange, - value: () => _settingsStore.disableExchange, - onValueChange: (_, bool value) { - _settingsStore.disableExchange = value; - }, - ), + ChoicesListItem( + title: S.current.fiat_api, + items: FiatApiMode.all, + selectedItem: _settingsStore.fiatApiMode, + onItemSelected: (FiatApiMode mode) => setFiatMode(mode), + ), + + ChoicesListItem( + title: S.current.exchange, + items: FiatApiMode.all, + selectedItem: _settingsStore.exchangeStatus, + onItemSelected: (FiatApiMode mode) => _settingsStore.exchangeStatus = mode, + ), SwitcherListItem( title: S.current.add_custom_node, value: () => _addCustomNode, @@ -34,7 +37,7 @@ abstract class AdvancedPrivacySettingsViewModelBase with Store { ]; } - late List settings; + late List settings; @observable bool _addCustomNode = false; @@ -46,11 +49,7 @@ 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 setFiatMode(FiatApiMode value) { + _settingsStore.fiatApiMode = value; } } diff --git a/lib/view_model/dashboard/dashboard_view_model.dart b/lib/view_model/dashboard/dashboard_view_model.dart index 57720d92f..dab2bafd1 100644 --- a/lib/view_model/dashboard/dashboard_view_model.dart +++ b/lib/view_model/dashboard/dashboard_view_model.dart @@ -1,3 +1,4 @@ +import 'package:cake_wallet/entities/fiat_api_mode.dart'; import 'package:cake_wallet/wallet_type_utils.dart'; import 'package:cw_core/transaction_history.dart'; import 'package:cw_core/balance.dart'; @@ -96,6 +97,11 @@ abstract class DashboardViewModelBase with Store { caption: ExchangeProviderDescription.simpleSwap.title, onChanged: () => tradeFilterStore .toggleDisplayExchange(ExchangeProviderDescription.simpleSwap)), + FilterItem( + value: () => tradeFilterStore.displayTrocador, + caption: ExchangeProviderDescription.trocador.title, + onChanged: () => tradeFilterStore + .toggleDisplayExchange(ExchangeProviderDescription.trocador)), ] }, subname = '', @@ -268,7 +274,7 @@ abstract class DashboardViewModelBase with Store { settingsStore.shouldShowYatPopup = shouldShow; @computed - bool get isEnabledExchangeAction => !settingsStore.disableExchange; + bool get isEnabledExchangeAction => settingsStore.exchangeStatus != FiatApiMode.disabled; @observable bool hasExchangeAction; diff --git a/lib/view_model/exchange/exchange_trade_view_model.dart b/lib/view_model/exchange/exchange_trade_view_model.dart index 541b74396..94c874979 100644 --- a/lib/view_model/exchange/exchange_trade_view_model.dart +++ b/lib/view_model/exchange/exchange_trade_view_model.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:cake_wallet/exchange/sideshift/sideshift_exchange_provider.dart'; import 'package:cake_wallet/exchange/simpleswap/simpleswap_exchange_provider.dart'; +import 'package:cake_wallet/exchange/trocador/trocador_exchange_provider.dart'; import 'package:cw_core/wallet_base.dart'; import 'package:cw_core/crypto_currency.dart'; import 'package:cake_wallet/exchange/changenow/changenow_exchange_provider.dart'; @@ -46,6 +47,9 @@ abstract class ExchangeTradeViewModelBase with Store { case ExchangeProviderDescription.simpleSwap: _provider = SimpleSwapExchangeProvider(); break; + case ExchangeProviderDescription.trocador: + _provider = TrocadorExchangeProvider(); + break; } _updateItems(); diff --git a/lib/view_model/exchange/exchange_view_model.dart b/lib/view_model/exchange/exchange_view_model.dart index 5c1f696b8..f9f5683d9 100644 --- a/lib/view_model/exchange/exchange_view_model.dart +++ b/lib/view_model/exchange/exchange_view_model.dart @@ -7,6 +7,8 @@ import 'package:cake_wallet/exchange/sideshift/sideshift_exchange_provider.dart' import 'package:cake_wallet/exchange/sideshift/sideshift_request.dart'; import 'package:cake_wallet/exchange/simpleswap/simpleswap_exchange_provider.dart'; import 'package:cake_wallet/exchange/simpleswap/simpleswap_request.dart'; +import 'package:cake_wallet/exchange/trocador/trocador_exchange_provider.dart'; +import 'package:cake_wallet/exchange/trocador/trocador_request.dart'; import 'package:cw_core/transaction_priority.dart'; import 'package:cw_core/wallet_base.dart'; import 'package:cw_core/crypto_currency.dart'; @@ -60,7 +62,7 @@ abstract class ExchangeViewModelBase with Store { limitsState = LimitsInitialState(), receiveCurrency = wallet.currency, depositCurrency = wallet.currency, - providerList = [ChangeNowExchangeProvider(), SideShiftExchangeProvider(), SimpleSwapExchangeProvider()], + providerList = [ChangeNowExchangeProvider(), SideShiftExchangeProvider(), SimpleSwapExchangeProvider(), TrocadorExchangeProvider()], selectedProviders = ObservableList() { const excludeDepositCurrencies = [CryptoCurrency.btt, CryptoCurrency.nano]; const excludeReceiveCurrencies = [CryptoCurrency.xlm, CryptoCurrency.xrp, @@ -449,6 +451,18 @@ abstract class ExchangeViewModelBase with Store { amount = isFixedRateMode ? receiveAmount : depositAmount; } + if (provider is TrocadorExchangeProvider) { + request = TrocadorRequest( + from: depositCurrency, + to: receiveCurrency, + fromAmount: depositAmount.replaceAll(',', '.'), + toAmount: receiveAmount.replaceAll(',', '.'), + refundAddress: depositAddress, + address: receiveAddress, + isReverse: isFixedRateMode); + amount = isFixedRateMode ? receiveAmount : depositAmount; + } + amount = amount.replaceAll(',', '.'); if (limitsState is LimitsLoadedSuccessfully) { diff --git a/lib/view_model/settings/privacy_settings_view_model.dart b/lib/view_model/settings/privacy_settings_view_model.dart index f8c3e5b50..db345d0f2 100644 --- a/lib/view_model/settings/privacy_settings_view_model.dart +++ b/lib/view_model/settings/privacy_settings_view_model.dart @@ -12,27 +12,23 @@ abstract class PrivacySettingsViewModelBase with Store { final SettingsStore _settingsStore; @computed - bool get disableExchange => _settingsStore.disableExchange; + FiatApiMode get exchangeStatus => _settingsStore.exchangeStatus; @computed bool get shouldSaveRecipientAddress => _settingsStore.shouldSaveRecipientAddress; @computed - bool get isFiatDisabled => _settingsStore.fiatApiMode == FiatApiMode.disabled; + FiatApiMode get fiatApi => _settingsStore.fiatApiMode; @action void setShouldSaveRecipientAddress(bool value) => _settingsStore.shouldSaveRecipientAddress = value; @action - void setEnableExchange(bool value) => _settingsStore.disableExchange = value; + void setEnableExchange(FiatApiMode value) => _settingsStore.exchangeStatus = value; @action - void setFiatMode(bool value) { - if (value) { - _settingsStore.fiatApiMode = FiatApiMode.disabled; - return; - } - _settingsStore.fiatApiMode = FiatApiMode.enabled; + void setFiatMode(FiatApiMode value) { + _settingsStore.fiatApiMode = value; } } diff --git a/lib/view_model/trade_details_view_model.dart b/lib/view_model/trade_details_view_model.dart index 5a1f78774..97bb40fe4 100644 --- a/lib/view_model/trade_details_view_model.dart +++ b/lib/view_model/trade_details_view_model.dart @@ -6,6 +6,7 @@ import 'package:cake_wallet/exchange/morphtoken/morphtoken_exchange_provider.dar import 'package:cake_wallet/exchange/sideshift/sideshift_exchange_provider.dart'; import 'package:cake_wallet/exchange/simpleswap/simpleswap_exchange_provider.dart'; import 'package:cake_wallet/exchange/trade.dart'; +import 'package:cake_wallet/exchange/trocador/trocador_exchange_provider.dart'; import 'package:cake_wallet/exchange/xmrto/xmrto_exchange_provider.dart'; import 'package:cake_wallet/store/settings_store.dart'; import 'package:cake_wallet/utils/date_formatter.dart'; @@ -48,6 +49,9 @@ abstract class TradeDetailsViewModelBase with Store { case ExchangeProviderDescription.simpleSwap: _provider = SimpleSwapExchangeProvider(); break; + case ExchangeProviderDescription.trocador: + _provider = TrocadorExchangeProvider(); + break; } items = ObservableList(); diff --git a/tool/utils/secret_key.dart b/tool/utils/secret_key.dart index e5202a829..ff74da8a7 100644 --- a/tool/utils/secret_key.dart +++ b/tool/utils/secret_key.dart @@ -29,6 +29,7 @@ class SecretKey { SecretKey('anypayToken', () => ''), SecretKey('onramperApiKey', () => ''), SecretKey('ioniaClientId', () => ''), + SecretKey('trocadorApiKey', () => ''), ]; final String name;