From 9999816850560da68040f3150ead330fc92e1518 Mon Sep 17 00:00:00 2001 From: Justin Ehrenhofer Date: Thu, 24 Aug 2023 16:30:53 -0500 Subject: [PATCH] Improve ChangeNOW and Trocador network mapping (#1058) * Improve ChangeNOW network mapping Follows Trocador code to use the title instead of the const name, and to allow normalizing across the tag level instead of needing to map the `legacyTicker` I will test * Missing `}` * Remove `:` * Add normalizeTitle for zec * Missing `;` * Make functions private and fix zec for Trocador * Add supported assets [skip ci] * Change name [skip ci] --- .../changenow_exchange_provider.dart | 65 ++++++++++--------- .../sideshift_exchange_provider.dart | 1 - .../trocador/trocador_exchange_provider.dart | 20 ++++-- 3 files changed, 50 insertions(+), 36 deletions(-) diff --git a/lib/exchange/changenow/changenow_exchange_provider.dart b/lib/exchange/changenow/changenow_exchange_provider.dart index ecfe888e7..6166a8875 100644 --- a/lib/exchange/changenow/changenow_exchange_provider.dart +++ b/lib/exchange/changenow/changenow_exchange_provider.dart @@ -68,14 +68,12 @@ class ChangeNowExchangeProvider extends ExchangeProvider { required CryptoCurrency to, required bool isFixedRateMode}) async { final headers = {apiHeaderKey: apiKey}; - final normalizedFrom = normalizeCryptoCurrency(from); - final normalizedTo = normalizeCryptoCurrency(to); final flow = getFlow(isFixedRateMode); final params = { - 'fromCurrency': normalizedFrom, - 'toCurrency': normalizedTo, - 'fromNetwork': networkFor(from), - 'toNetwork': networkFor(to), + 'fromCurrency': _normalizeCurrency(from), + 'toCurrency': _normalizeCurrency(to), + 'fromNetwork': _networkFor(from), + 'toNetwork': _networkFor(to), 'flow': flow }; final uri = Uri.https(apiAuthority, rangePath, params); @@ -112,10 +110,10 @@ class ChangeNowExchangeProvider extends ExchangeProvider { final flow = getFlow(isFixedRateMode); final type = isFixedRateMode ? 'reverse' : 'direct'; final body = { - 'fromCurrency': normalizeCryptoCurrency(_request.from), - 'toCurrency': normalizeCryptoCurrency(_request.to), - 'fromNetwork': networkFor(_request.from), - 'toNetwork': networkFor(_request.to), + 'fromCurrency': _normalizeCurrency(_request.from), + 'toCurrency': _normalizeCurrency(_request.to), + 'fromNetwork': _networkFor(_request.from), + 'toNetwork': _networkFor(_request.to), if (!isFixedRateMode) 'fromAmount': _request.fromAmount, if (isFixedRateMode) 'toAmount': _request.toAmount, 'address': _request.address, @@ -241,10 +239,10 @@ class ChangeNowExchangeProvider extends ExchangeProvider { final type = isReverse ? 'reverse' : 'direct'; final flow = getFlow(isFixedRateMode); final params = { - 'fromCurrency': normalizeCryptoCurrency(from), - 'toCurrency': normalizeCryptoCurrency(to), - 'fromNetwork': networkFor(from), - 'toNetwork': networkFor(to), + 'fromCurrency': _normalizeCurrency(from), + 'toCurrency': _normalizeCurrency(to), + 'fromNetwork': _networkFor(from), + 'toNetwork': _networkFor(to), 'type': type, 'flow': flow }; @@ -273,25 +271,34 @@ class ChangeNowExchangeProvider extends ExchangeProvider { } } - String networkFor(CryptoCurrency currency) { + String _networkFor(CryptoCurrency currency) { switch (currency) { case CryptoCurrency.usdt: - return CryptoCurrency.btc.title.toLowerCase(); + return 'btc'; default: - return currency.tag != null ? currency.tag!.toLowerCase() : currency.title.toLowerCase(); + return currency.tag != null ? _normalizeTag(currency.tag!) : currency.title.toLowerCase(); } } -} -String normalizeCryptoCurrency(CryptoCurrency currency) { - switch (currency) { - case CryptoCurrency.zec: - return 'zec'; - case CryptoCurrency.usdcpoly: - return 'usdcmatic'; - case CryptoCurrency.maticpoly: - return 'maticmainnet'; - default: - return currency.title.toLowerCase(); + String _normalizeCurrency(CryptoCurrency currency) { + switch (currency) { + case CryptoCurrency.zec: + return 'zec'; + default: + return currency.title.toLowerCase(); + } } -} + + String _normalizeTag(String tag) { + switch (tag) { + case 'POLY': + return 'matic'; + case 'LN': + return 'lightning'; + case 'AVAXC': + return 'cchain'; + default: + return tag.toLowerCase(); + } + } +} \ No newline at end of file diff --git a/lib/exchange/sideshift/sideshift_exchange_provider.dart b/lib/exchange/sideshift/sideshift_exchange_provider.dart index 26575f2c1..257d339cf 100644 --- a/lib/exchange/sideshift/sideshift_exchange_provider.dart +++ b/lib/exchange/sideshift/sideshift_exchange_provider.dart @@ -28,7 +28,6 @@ class SideShiftExchangeProvider extends ExchangeProvider { CryptoCurrency.xhv, CryptoCurrency.dcr, CryptoCurrency.kmd, - CryptoCurrency.mkr, CryptoCurrency.oxt, CryptoCurrency.pivx, CryptoCurrency.rune, diff --git a/lib/exchange/trocador/trocador_exchange_provider.dart b/lib/exchange/trocador/trocador_exchange_provider.dart index fb6109bdf..b42291ed7 100644 --- a/lib/exchange/trocador/trocador_exchange_provider.dart +++ b/lib/exchange/trocador/trocador_exchange_provider.dart @@ -20,7 +20,6 @@ class TrocadorExchangeProvider extends ExchangeProvider { bool useTorOnly; static const List _notSupported = [ - CryptoCurrency.scrt, CryptoCurrency.stx, CryptoCurrency.zaddr, ]; @@ -60,8 +59,8 @@ class TrocadorExchangeProvider extends ExchangeProvider { }) async { final params = { 'api_key': apiKey, - 'ticker_from': request.from.title.toLowerCase(), - 'ticker_to': request.to.title.toLowerCase(), + 'ticker_from': _normalizeCurrency(request.from), + 'ticker_to': _normalizeCurrency(request.to), 'network_from': _networkFor(request.from), 'network_to': _networkFor(request.to), 'payment': isFixedRateMode ? 'True' : 'False', @@ -137,7 +136,7 @@ class TrocadorExchangeProvider extends ExchangeProvider { required bool isFixedRateMode}) async { final params = { 'api_key': apiKey, - 'ticker': from.title.toLowerCase(), + 'ticker': _normalizeCurrency(from), 'name': from.name, }; @@ -177,8 +176,8 @@ class TrocadorExchangeProvider extends ExchangeProvider { final params = { 'api_key': apiKey, - 'ticker_from': from.title.toLowerCase(), - 'ticker_to': to.title.toLowerCase(), + 'ticker_from': _normalizeCurrency(from), + 'ticker_to': _normalizeCurrency(to), 'network_from': _networkFor(from), 'network_to': _networkFor(to), if (!isFixedRateMode) 'amount_from': amount.toString(), @@ -279,6 +278,15 @@ class TrocadorExchangeProvider extends ExchangeProvider { } } + String _normalizeCurrency(CryptoCurrency currency) { + switch (currency) { + case CryptoCurrency.zec: + return 'zec'; + default: + return currency.title.toLowerCase(); + } + } + String _normalizeTag(String tag) { switch (tag) { case 'ETH':