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 <borodenko.sv@gmail.com> * refactor: normalise network --------- Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
Serhii committed
Apr 10, 2025 at 04:55 UTC
2b020c0c1b2a88c8e94281c5a2ed64db14b67c49
1 file changed
+34
-39
lib/buy/onramper/onramper_buy_provider.dart
+34
-39
@@ -37,6 +37,7 @@ class OnRamperBuyProvider extends BuyProvider {
37
38
static const List<CryptoCurrency> _notSupportedCrypto = [];
39
static const List<FiatCurrency> _notSupportedFiat = [];
40
+ static Map<String, dynamic> _onrampMetadata = {};
41
42
final SettingsStore _settingsStore;
43
@@ -59,11 +60,8 @@ class OnRamperBuyProvider extends BuyProvider {
60
61
Future<List<PaymentMethod>> getAvailablePaymentTypes(
62
String fiatCurrency, CryptoCurrency cryptoCurrency, bool isBuyAction) async {
62
- final params = {
63
- 'fiatCurrency': fiatCurrency,
64
- 'type': isBuyAction ? 'buy' : 'sell',
65
- 'isRecurringPayment': 'false'
66
- };
63
+
64
+ final params = {'type': isBuyAction ? 'buy' : 'sell'};
65
66
final url = Uri.https(_baseApiUrl, '$supported$paymentTypes/$fiatCurrency', params);
67
@@ -136,16 +134,14 @@ class OnRamperBuyProvider extends BuyProvider {
134
135
final actionType = isBuyAction ? 'buy' : 'sell';
136
139
- final normalizedCryptoCurrency = _getNormalizeCryptoCurrency(cryptoCurrency);
137
+ final normalizedCryptoCurrency =
138
+ cryptoCurrency.title + _getNormalizeNetwork(cryptoCurrency).toUpperCase();
139
140
final params = {
141
'amount': amount.toString(),
142
if (paymentMethod != null) 'paymentMethod': paymentMethod,
143
'clientName': 'CakeWallet',
145
- 'type': actionType,
146
- 'walletAddress': walletAddress,
147
- 'isRecurringPayment': 'false',
148
- 'input': 'source',
144
+ if (actionType == 'sell') 'type': actionType,
145
};
146
147
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 {
161
162
List<Quote> validQuotes = [];
163
168
- final onrampMetadata = await getOnrampMetadata();
164
+ if (_onrampMetadata.isEmpty) _onrampMetadata = await getOnrampMetadata();
165
166
for (var item in data) {
167
@@ -174,12 +170,12 @@ class OnRamperBuyProvider extends BuyProvider {
170
final paymentMethod = (item as Map<String, dynamic>)['paymentMethod'] as String;
171
172
final rampId = item['ramp'] as String?;
177
- final rampMetaData = onrampMetadata[rampId] as Map<String, dynamic>?;
173
+ final rampMetaData = _onrampMetadata[rampId] as Map<String, dynamic>?;
174
175
if (rampMetaData == null) continue;
176
177
final quote = Quote.fromOnramperJson(
182
- item, isBuyAction, onrampMetadata, _getPaymentTypeByString(paymentMethod));
178
+ item, isBuyAction, _onrampMetadata, _getPaymentTypeByString(paymentMethod));
179
quote.setFiatCurrency = fiatCurrency;
180
quote.setCryptoCurrency = cryptoCurrency;
181
validQuotes.add(quote);
@@ -206,7 +202,6 @@ class OnRamperBuyProvider extends BuyProvider {
202
required String cryptoCurrencyAddress,
203
String? countryCode}) async {
204
final actionType = isBuyAction ? 'buy' : 'sell';
209
- final prefix = actionType == 'sell' ? actionType + '_' : '';
205
206
final primaryColor = getColorStr(Theme.of(context).primaryColor);
207
final secondaryColor = getColorStr(Theme.of(context).colorScheme.background);
@@ -220,18 +215,20 @@ class OnRamperBuyProvider extends BuyProvider {
215
cardColor = getColorStr(Colors.white);
216
}
217
223
- final defaultCrypto = _getNormalizeCryptoCurrency(quote.cryptoCurrency);
218
+ final defaultCrypto =
219
+ quote.cryptoCurrency.title + _getNormalizeNetwork(quote.cryptoCurrency).toLowerCase();
220
221
final paymentMethod = normalizePaymentMethod(quote.paymentType);
222
223
final uri = Uri.https(_baseUrl, '', {
224
'apiKey': _apiKey,
229
- 'mode': actionType,
230
- '${prefix}defaultFiat': quote.fiatCurrency.name,
231
- '${prefix}defaultCrypto': defaultCrypto,
232
- '${prefix}defaultAmount': amount.toString(),
233
- if (paymentMethod != null) '${prefix}defaultPaymentMethod': paymentMethod,
234
- 'onlyOnramps': quote.rampId,
225
+ 'txnType': actionType,
226
+ 'txnFiat': quote.fiatCurrency.name,
227
+ 'txnCrypto': defaultCrypto,
228
+ 'txnAmount': amount.toString(),
229
+ 'skipTransactionScreen': "true",
230
+ if (paymentMethod != null) 'txnPaymentMethod': paymentMethod,
231
+ 'txnOnramp': quote.rampId,
232
'networkWallets': '${_tagToNetwork(quote.cryptoCurrency.tag ?? quote.cryptoCurrency.title)}:$cryptoCurrencyAddress',
233
'supportSwap': "false",
234
'primaryColor': primaryColor,
@@ -257,31 +254,29 @@ class OnRamperBuyProvider extends BuyProvider {
254
255
String _tagToNetwork(String tag) {
256
switch (tag) {
260
- case 'OMNI':
261
- case 'BSC':
262
- return tag;
257
case 'POL':
258
return 'POLYGON';
259
+ case 'ETH':
260
+ return 'ETHEREUM';
261
+ case 'TRX':
262
+ return 'TRON';
263
+ case 'SOL':
264
+ return 'SOLANA';
265
case 'ZEC':
266
return 'ZCASH';
267
- default:
268
- try {
269
- return CryptoCurrency.fromString(tag).fullName!;
270
- } catch (_) {
271
- return tag;
272
- }
267
+ default:
268
+ return tag;
269
}
270
}
271
276
- String _getNormalizeCryptoCurrency(Currency currency) {
277
- if (currency is CryptoCurrency) {
278
- if (!mainCurrency.contains(currency)) {
279
- final network = currency.tag == null ? currency.fullName : _tagToNetwork(currency.tag!);
280
- return '${currency.title}_${network?.replaceAll(' ', '')}'.toUpperCase();
281
- }
282
- return currency.title.toUpperCase();
283
- }
284
- return currency.name.toUpperCase();
272
+ String _getNormalizeNetwork(CryptoCurrency currency) {
273
+ if (mainCurrency.contains(currency)) return '';
274
+
275
+ if (currency == CryptoCurrency.eos) return '_EOSIO';
276
+
277
+ if (currency.tag != null) return '_' + _tagToNetwork(currency.tag!);
278
+
279
+ return '_' + (currency.fullName?.replaceAll(' ', '') ?? currency.title);;
280
}
281
282
String? normalizePaymentMethod(PaymentType paymentType) {