From 2b020c0c1b2a88c8e94281c5a2ed64db14b67c49 Mon Sep 17 00:00:00 2001 From: Serhii Date: Thu, 10 Apr 2025 04:55:31 +0300 Subject: [PATCH] Cw 1032 verify on ramper integration (#2172) * refactor: cache onramp metadata * remove unused parameters * Skip onramper transaction page and go directly to provider (#1940) * Skip onramper transaction page and go directly to provider * change parameter to follow docs correctly * remove old deprecated monero nodes [skip ci] * Update onramper_buy_provider.dart --------- Co-authored-by: Serhii * refactor: normalise network --------- Co-authored-by: Omar Hatem --- lib/buy/onramper/onramper_buy_provider.dart | 73 ++++++++++----------- 1 file changed, 34 insertions(+), 39 deletions(-) diff --git a/lib/buy/onramper/onramper_buy_provider.dart b/lib/buy/onramper/onramper_buy_provider.dart index b48228bd8..ac198b488 100644 --- a/lib/buy/onramper/onramper_buy_provider.dart +++ b/lib/buy/onramper/onramper_buy_provider.dart @@ -37,6 +37,7 @@ class OnRamperBuyProvider extends BuyProvider { static const List _notSupportedCrypto = []; static const List _notSupportedFiat = []; + static Map _onrampMetadata = {}; final SettingsStore _settingsStore; @@ -59,11 +60,8 @@ class OnRamperBuyProvider extends BuyProvider { Future> getAvailablePaymentTypes( String fiatCurrency, CryptoCurrency cryptoCurrency, bool isBuyAction) async { - final params = { - 'fiatCurrency': fiatCurrency, - 'type': isBuyAction ? 'buy' : 'sell', - 'isRecurringPayment': 'false' - }; + + final params = {'type': isBuyAction ? 'buy' : 'sell'}; final url = Uri.https(_baseApiUrl, '$supported$paymentTypes/$fiatCurrency', params); @@ -136,16 +134,14 @@ class OnRamperBuyProvider extends BuyProvider { final actionType = isBuyAction ? 'buy' : 'sell'; - final normalizedCryptoCurrency = _getNormalizeCryptoCurrency(cryptoCurrency); + final normalizedCryptoCurrency = + cryptoCurrency.title + _getNormalizeNetwork(cryptoCurrency).toUpperCase(); final params = { 'amount': amount.toString(), if (paymentMethod != null) 'paymentMethod': paymentMethod, 'clientName': 'CakeWallet', - 'type': actionType, - 'walletAddress': walletAddress, - 'isRecurringPayment': 'false', - 'input': 'source', + if (actionType == 'sell') 'type': actionType, }; log('Onramper: Fetching $actionType quote: ${isBuyAction ? normalizedCryptoCurrency : fiatCurrency.name} -> ${isBuyAction ? fiatCurrency.name : normalizedCryptoCurrency}, amount: $amount, paymentMethod: $paymentMethod'); @@ -165,7 +161,7 @@ class OnRamperBuyProvider extends BuyProvider { List validQuotes = []; - final onrampMetadata = await getOnrampMetadata(); + if (_onrampMetadata.isEmpty) _onrampMetadata = await getOnrampMetadata(); for (var item in data) { @@ -174,12 +170,12 @@ class OnRamperBuyProvider extends BuyProvider { final paymentMethod = (item as Map)['paymentMethod'] as String; final rampId = item['ramp'] as String?; - final rampMetaData = onrampMetadata[rampId] as Map?; + final rampMetaData = _onrampMetadata[rampId] as Map?; if (rampMetaData == null) continue; final quote = Quote.fromOnramperJson( - item, isBuyAction, onrampMetadata, _getPaymentTypeByString(paymentMethod)); + item, isBuyAction, _onrampMetadata, _getPaymentTypeByString(paymentMethod)); quote.setFiatCurrency = fiatCurrency; quote.setCryptoCurrency = cryptoCurrency; validQuotes.add(quote); @@ -206,7 +202,6 @@ class OnRamperBuyProvider extends BuyProvider { required String cryptoCurrencyAddress, String? countryCode}) async { final actionType = isBuyAction ? 'buy' : 'sell'; - final prefix = actionType == 'sell' ? actionType + '_' : ''; final primaryColor = getColorStr(Theme.of(context).primaryColor); final secondaryColor = getColorStr(Theme.of(context).colorScheme.background); @@ -220,18 +215,20 @@ class OnRamperBuyProvider extends BuyProvider { cardColor = getColorStr(Colors.white); } - final defaultCrypto = _getNormalizeCryptoCurrency(quote.cryptoCurrency); + final defaultCrypto = + quote.cryptoCurrency.title + _getNormalizeNetwork(quote.cryptoCurrency).toLowerCase(); final paymentMethod = normalizePaymentMethod(quote.paymentType); final uri = Uri.https(_baseUrl, '', { 'apiKey': _apiKey, - 'mode': actionType, - '${prefix}defaultFiat': quote.fiatCurrency.name, - '${prefix}defaultCrypto': defaultCrypto, - '${prefix}defaultAmount': amount.toString(), - if (paymentMethod != null) '${prefix}defaultPaymentMethod': paymentMethod, - 'onlyOnramps': quote.rampId, + 'txnType': actionType, + 'txnFiat': quote.fiatCurrency.name, + 'txnCrypto': defaultCrypto, + 'txnAmount': amount.toString(), + 'skipTransactionScreen': "true", + if (paymentMethod != null) 'txnPaymentMethod': paymentMethod, + 'txnOnramp': quote.rampId, 'networkWallets': '${_tagToNetwork(quote.cryptoCurrency.tag ?? quote.cryptoCurrency.title)}:$cryptoCurrencyAddress', 'supportSwap': "false", 'primaryColor': primaryColor, @@ -257,31 +254,29 @@ class OnRamperBuyProvider extends BuyProvider { String _tagToNetwork(String tag) { switch (tag) { - case 'OMNI': - case 'BSC': - return tag; case 'POL': return 'POLYGON'; + case 'ETH': + return 'ETHEREUM'; + case 'TRX': + return 'TRON'; + case 'SOL': + return 'SOLANA'; case 'ZEC': return 'ZCASH'; - default: - try { - return CryptoCurrency.fromString(tag).fullName!; - } catch (_) { - return tag; - } + default: + return tag; } } - String _getNormalizeCryptoCurrency(Currency currency) { - if (currency is CryptoCurrency) { - if (!mainCurrency.contains(currency)) { - final network = currency.tag == null ? currency.fullName : _tagToNetwork(currency.tag!); - return '${currency.title}_${network?.replaceAll(' ', '')}'.toUpperCase(); - } - return currency.title.toUpperCase(); - } - return currency.name.toUpperCase(); + String _getNormalizeNetwork(CryptoCurrency currency) { + if (mainCurrency.contains(currency)) return ''; + + if (currency == CryptoCurrency.eos) return '_EOSIO'; + + if (currency.tag != null) return '_' + _tagToNetwork(currency.tag!); + + return '_' + (currency.fullName?.replaceAll(' ', '') ?? currency.title);; } String? normalizePaymentMethod(PaymentType paymentType) {