2025-06-20 21:56:18 +02:00
|
|
|
import 'package:cw_core/utils/proxy_wrapper.dart';
|
2021-12-24 14:37:24 +02:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
2020-09-21 14:50:26 +03:00
|
|
|
import 'package:cake_wallet/entities/fiat_currency.dart';
|
2020-01-04 21:31:52 +02:00
|
|
|
import 'dart:convert';
|
2023-03-31 13:22:51 -05:00
|
|
|
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
|
|
|
|
2023-02-28 18:23:21 +02:00
|
|
|
const _fiatApiClearNetAuthority = 'fiat-api.cakewallet.com';
|
2025-06-20 21:56:18 +02:00
|
|
|
// const _fiatApiOnionAuthority = 'n4z7bdcmwk2oyddxvzaap3x2peqcplh3pzdy7tpkk5ejz5n4mhfvoxqd.onion';
|
|
|
|
const _fiatApiOnionAuthority = _fiatApiClearNetAuthority;
|
2023-02-28 18:23:21 +02:00
|
|
|
const _fiatApiPath = '/v2/rates';
|
2020-01-04 21:31:52 +02:00
|
|
|
|
2025-06-20 21:56:18 +02:00
|
|
|
Future<double> _fetchPrice(String crypto, String fiat, bool torOnly) async {
|
2023-03-01 23:24:52 +02:00
|
|
|
|
|
|
|
final Map<String, String> queryParams = {
|
|
|
|
'interval_count': '1',
|
2023-12-02 03:26:43 +01:00
|
|
|
'base': crypto.split(".").first,
|
2023-08-04 20:01:49 +03:00
|
|
|
'quote': fiat,
|
|
|
|
'key': secrets.fiatApiKey,
|
2023-03-01 23:24:52 +02:00
|
|
|
};
|
|
|
|
|
2023-09-01 18:06:18 +03:00
|
|
|
num price = 0.0;
|
2020-01-04 21:31:52 +02:00
|
|
|
|
|
|
|
try {
|
2025-06-20 21:56:18 +02:00
|
|
|
final onionUri = Uri.http(_fiatApiOnionAuthority, _fiatApiPath, queryParams);
|
|
|
|
final clearnetUri = Uri.https(_fiatApiClearNetAuthority, _fiatApiPath, queryParams);
|
2023-03-01 23:24:52 +02:00
|
|
|
|
2025-06-20 21:56:18 +02:00
|
|
|
final response = await ProxyWrapper().get(
|
|
|
|
onionUri: onionUri,
|
|
|
|
clearnetUri: torOnly ? onionUri : clearnetUri,
|
|
|
|
);
|
|
|
|
|
2020-01-04 21:31:52 +02:00
|
|
|
|
|
|
|
if (response.statusCode != 200) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
2020-01-08 14:26:34 +02:00
|
|
|
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
2023-02-28 18:23:21 +02:00
|
|
|
final results = responseJSON['results'] as Map<String, dynamic>;
|
2020-01-04 21:31:52 +02:00
|
|
|
|
2023-02-28 18:23:21 +02:00
|
|
|
if (results.isNotEmpty) {
|
2023-09-01 18:06:18 +03:00
|
|
|
price = results.values.first as num;
|
2020-01-04 21:31:52 +02:00
|
|
|
}
|
|
|
|
|
2023-09-01 18:06:18 +03:00
|
|
|
return price.toDouble();
|
2020-01-04 21:31:52 +02:00
|
|
|
} catch (e) {
|
2023-09-01 18:06:18 +03:00
|
|
|
return price.toDouble();
|
2020-01-04 21:31:52 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-21 14:50:26 +03:00
|
|
|
|
|
|
|
class FiatConversionService {
|
2023-02-28 18:23:21 +02:00
|
|
|
static Future<double> fetchPrice({
|
|
|
|
required CryptoCurrency crypto,
|
|
|
|
required FiatCurrency fiat,
|
|
|
|
required bool torOnly,
|
|
|
|
}) async =>
|
2025-06-20 21:56:18 +02:00
|
|
|
await _fetchPrice(crypto.toString(), fiat.toString(), torOnly);
|
2020-09-21 14:50:26 +03:00
|
|
|
}
|