2023-12-06 20:10:44 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
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';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
2020-01-04 19:31:52 +00:00
|
|
|
import 'package:http/http.dart';
|
2023-03-31 18:22:51 +00:00
|
|
|
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
2023-12-06 20:10:44 +00:00
|
|
|
import 'package:socks5_proxy/socks_client.dart';
|
|
|
|
import 'package:tor/tor.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-02-28 16:23:21 +00:00
|
|
|
final torOnly = args['torOnly'] as bool;
|
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-03-01 21:24:52 +00:00
|
|
|
late final Uri uri;
|
|
|
|
if (torOnly) {
|
|
|
|
uri = Uri.http(_fiatApiOnionAuthority, _fiatApiPath, queryParams);
|
|
|
|
} else {
|
|
|
|
uri = Uri.https(_fiatApiClearNetAuthority, _fiatApiPath, queryParams);
|
|
|
|
}
|
|
|
|
|
2023-12-06 20:10:44 +00:00
|
|
|
print("getting price from: $uri");
|
|
|
|
print("tor port: ${Tor.instance.port}");
|
|
|
|
|
|
|
|
// Create HttpClient object
|
|
|
|
final client = HttpClient();
|
|
|
|
|
|
|
|
// Assign connection factory.
|
|
|
|
SocksTCPClient.assignToHttpClient(client, [
|
|
|
|
ProxySettings(
|
|
|
|
InternetAddress.loopbackIPv4,
|
|
|
|
Tor.instance.port,
|
|
|
|
password: null,
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
// late Response response;
|
|
|
|
late HttpClientResponse response;
|
|
|
|
String responseBody = "";
|
|
|
|
try {
|
|
|
|
// response = await get(uri);
|
|
|
|
// GET request.
|
|
|
|
final request = await client.getUrl(uri);
|
|
|
|
response = await request.close();
|
|
|
|
responseBody = await utf8.decodeStream(response);
|
|
|
|
} catch (e) {
|
|
|
|
print("error getting price! $e");
|
|
|
|
}
|
|
|
|
|
|
|
|
print(responseBody);
|
|
|
|
|
|
|
|
// final response = await get(uri);
|
2020-01-04 19:31:52 +00:00
|
|
|
|
|
|
|
if (response.statusCode != 200) {
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
2023-12-06 20:10:44 +00:00
|
|
|
final responseJSON = json.decode(/*response.body*/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-02-28 16:23:21 +00:00
|
|
|
Future<double> _fetchPriceAsync(CryptoCurrency crypto, FiatCurrency fiat, bool torOnly) async =>
|
2023-12-06 20:10:44 +00:00
|
|
|
// compute(_fetchPrice, {
|
|
|
|
// 'fiat': fiat.toString(),
|
|
|
|
// 'crypto': crypto.toString(),
|
|
|
|
// 'torOnly': torOnly,
|
|
|
|
// });
|
|
|
|
_fetchPrice({
|
2023-08-04 17:01:49 +00:00
|
|
|
'fiat': fiat.toString(),
|
|
|
|
'crypto': crypto.toString(),
|
|
|
|
'torOnly': torOnly,
|
|
|
|
});
|
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,
|
|
|
|
required bool torOnly,
|
|
|
|
}) async =>
|
|
|
|
await _fetchPriceAsync(crypto, fiat, torOnly);
|
2020-09-21 11:50:26 +00:00
|
|
|
}
|