2023-12-06 20:10:44 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-12-07 16:44:07 +00:00
|
|
|
import 'package:cake_wallet/entities/fiat_api_mode.dart';
|
|
|
|
import 'package:cake_wallet/utils/proxy_wrapper.dart';
|
2021-12-24 12:37:24 +00:00
|
|
|
import 'package:cw_core/crypto_currency.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:cake_wallet/entities/fiat_currency.dart';
|
2020-01-04 19:31:52 +00:00
|
|
|
import 'dart:convert';
|
2023-03-31 18:22:51 +00:00
|
|
|
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
2023-12-11 19:34:34 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
2023-12-12 20:29:06 +00:00
|
|
|
import 'package:http/http.dart';
|
2023-03-31 18:22:51 +00:00
|
|
|
|
2023-02-28 16:23:21 +00:00
|
|
|
const _fiatApiClearNetAuthority = 'fiat-api.cakewallet.com';
|
|
|
|
const _fiatApiOnionAuthority = 'n4z7bdcmwk2oyddxvzaap3x2peqcplh3pzdy7tpkk5ejz5n4mhfvoxqd.onion';
|
|
|
|
const _fiatApiPath = '/v2/rates';
|
2020-01-04 19:31:52 +00:00
|
|
|
|
2020-09-21 11:50:26 +00:00
|
|
|
Future<double> _fetchPrice(Map<String, dynamic> args) async {
|
2023-08-04 17:01:49 +00:00
|
|
|
final crypto = args['crypto'] as String;
|
|
|
|
final fiat = args['fiat'] as String;
|
2023-12-21 16:42:52 +00:00
|
|
|
final torOnly = args['torOnly'] as bool;
|
2023-12-11 19:34:34 +00:00
|
|
|
final mainThreadProxyPort = args['port'] as int;
|
2023-03-01 21:24:52 +00:00
|
|
|
|
|
|
|
final Map<String, String> queryParams = {
|
|
|
|
'interval_count': '1',
|
2023-12-02 02:26:43 +00:00
|
|
|
'base': crypto.split(".").first,
|
2023-08-04 17:01:49 +00:00
|
|
|
'quote': fiat,
|
|
|
|
'key': secrets.fiatApiKey,
|
2023-03-01 21:24:52 +00:00
|
|
|
};
|
|
|
|
|
2023-09-01 15:06:18 +00:00
|
|
|
num price = 0.0;
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
try {
|
2023-12-07 16:44:07 +00:00
|
|
|
final Uri onionUri = Uri.http(_fiatApiOnionAuthority, _fiatApiPath, queryParams);
|
|
|
|
final Uri clearnetUri = Uri.https(_fiatApiClearNetAuthority, _fiatApiPath, queryParams);
|
2023-12-06 20:10:44 +00:00
|
|
|
|
2023-12-11 19:34:34 +00:00
|
|
|
HttpClient client = await ProxyWrapper.instance.getProxyInstance(
|
|
|
|
portOverride: mainThreadProxyPort,
|
|
|
|
);
|
2023-12-12 20:29:06 +00:00
|
|
|
|
|
|
|
late HttpClientResponse httpResponse;
|
2023-12-07 16:44:07 +00:00
|
|
|
late String responseBody;
|
2023-12-12 20:29:06 +00:00
|
|
|
late int statusCode;
|
2023-12-11 19:34:34 +00:00
|
|
|
|
2023-12-12 20:29:06 +00:00
|
|
|
// we might have tor enabled (no way of knowing), so we try to use it first
|
2023-12-06 20:10:44 +00:00
|
|
|
try {
|
2023-12-12 20:29:06 +00:00
|
|
|
try {
|
|
|
|
final request = await client.getUrl(onionUri);
|
|
|
|
httpResponse = await request.close();
|
|
|
|
responseBody = await utf8.decodeStream(httpResponse);
|
|
|
|
} catch (e) {
|
|
|
|
// if the onion url fails, and not set to tor only, try the clearnet url, (still using tor!):
|
|
|
|
if (!torOnly) {
|
|
|
|
final request = await client.getUrl(clearnetUri);
|
|
|
|
httpResponse = await request.close();
|
|
|
|
responseBody = await utf8.decodeStream(httpResponse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
statusCode = httpResponse.statusCode;
|
2023-12-06 20:10:44 +00:00
|
|
|
} catch (e) {
|
2023-12-12 20:29:06 +00:00
|
|
|
// connections all failed / tor is not enabled, so we use the clearnet url directly as normal:
|
|
|
|
if (torOnly) {
|
|
|
|
// we failed to connect through tor
|
|
|
|
return 0.0;
|
2023-12-07 16:44:07 +00:00
|
|
|
}
|
2023-12-12 20:29:06 +00:00
|
|
|
final response = await get(clearnetUri);
|
|
|
|
responseBody = response.body;
|
|
|
|
statusCode = response.statusCode;
|
2023-12-06 20:10:44 +00:00
|
|
|
}
|
|
|
|
|
2023-12-12 20:29:06 +00:00
|
|
|
if (statusCode != 200) {
|
2020-01-04 19:31:52 +00:00
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
2023-12-07 16:44:07 +00:00
|
|
|
final responseJSON = json.decode(responseBody) as Map<String, dynamic>;
|
2023-02-28 16:23:21 +00:00
|
|
|
final results = responseJSON['results'] as Map<String, dynamic>;
|
2020-01-04 19:31:52 +00:00
|
|
|
|
2023-02-28 16:23:21 +00:00
|
|
|
if (results.isNotEmpty) {
|
2023-09-01 15:06:18 +00:00
|
|
|
price = results.values.first as num;
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 15:06:18 +00:00
|
|
|
return price.toDouble();
|
2020-01-04 19:31:52 +00:00
|
|
|
} catch (e) {
|
2023-09-01 15:06:18 +00:00
|
|
|
return price.toDouble();
|
2020-01-04 19:31:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-21 11:50:26 +00:00
|
|
|
|
2023-12-07 16:44:07 +00:00
|
|
|
Future<double> _fetchPriceAsync(
|
2023-12-21 16:42:52 +00:00
|
|
|
CryptoCurrency crypto, FiatCurrency fiat, bool torOnly) async =>
|
2023-12-11 19:34:34 +00:00
|
|
|
compute(_fetchPrice, {
|
2023-08-04 17:01:49 +00:00
|
|
|
'fiat': fiat.toString(),
|
|
|
|
'crypto': crypto.toString(),
|
2023-12-21 16:44:08 +00:00
|
|
|
'torOnly': torOnly,
|
2023-12-11 19:34:34 +00:00
|
|
|
'port': ProxyWrapper.port,
|
2023-12-12 20:29:06 +00:00
|
|
|
'torEnabled': ProxyWrapper.enabled,
|
2023-08-04 17:01:49 +00:00
|
|
|
});
|
2020-09-21 11:50:26 +00:00
|
|
|
|
|
|
|
class FiatConversionService {
|
2023-02-28 16:23:21 +00:00
|
|
|
static Future<double> fetchPrice({
|
|
|
|
required CryptoCurrency crypto,
|
|
|
|
required FiatCurrency fiat,
|
2023-12-21 16:42:52 +00:00
|
|
|
required bool torOnly,
|
2023-02-28 16:23:21 +00:00
|
|
|
}) async =>
|
2023-12-21 16:42:52 +00:00
|
|
|
await _fetchPriceAsync(crypto, fiat, torOnly);
|
2020-09-21 11:50:26 +00:00
|
|
|
}
|