From b5084c10ae16bad06f4201e2c689474ed32b7853 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Mon, 11 Dec 2023 11:34:34 -0800 Subject: [PATCH] fetch fiat price in isolate again --- lib/core/fiat_conversion_service.dart | 11 ++++++++--- lib/utils/proxy_wrapper.dart | 6 ++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/core/fiat_conversion_service.dart b/lib/core/fiat_conversion_service.dart index cdb9e3ce1..e4d263afa 100644 --- a/lib/core/fiat_conversion_service.dart +++ b/lib/core/fiat_conversion_service.dart @@ -6,6 +6,7 @@ import 'package:cw_core/crypto_currency.dart'; import 'package:cake_wallet/entities/fiat_currency.dart'; import 'dart:convert'; import 'package:cake_wallet/.secrets.g.dart' as secrets; +import 'package:flutter/foundation.dart'; const _fiatApiClearNetAuthority = 'fiat-api.cakewallet.com'; const _fiatApiOnionAuthority = 'n4z7bdcmwk2oyddxvzaap3x2peqcplh3pzdy7tpkk5ejz5n4mhfvoxqd.onion'; @@ -15,6 +16,7 @@ Future _fetchPrice(Map args) async { final crypto = args['crypto'] as String; final fiat = args['fiat'] as String; final torOnly = args['apiMode'] as String == FiatApiMode.torOnly.toString(); + final mainThreadProxyPort = args['port'] as int; final Map queryParams = { 'interval_count': '1', @@ -29,10 +31,12 @@ Future _fetchPrice(Map args) async { final Uri onionUri = Uri.http(_fiatApiOnionAuthority, _fiatApiPath, queryParams); final Uri clearnetUri = Uri.https(_fiatApiClearNetAuthority, _fiatApiPath, queryParams); - HttpClient client = await ProxyWrapper.instance.getProxyInstance(); + HttpClient client = await ProxyWrapper.instance.getProxyInstance( + portOverride: mainThreadProxyPort, + ); late HttpClientResponse response; late String responseBody; - + try { final request = await client.getUrl(onionUri); response = await request.close(); @@ -64,10 +68,11 @@ Future _fetchPrice(Map args) async { Future _fetchPriceAsync( CryptoCurrency crypto, FiatCurrency fiat, FiatApiMode apiMode) async => - _fetchPrice({ + compute(_fetchPrice, { 'fiat': fiat.toString(), 'crypto': crypto.toString(), 'apiMode': apiMode.toString(), + 'port': ProxyWrapper.port, }); class FiatConversionService { diff --git a/lib/utils/proxy_wrapper.dart b/lib/utils/proxy_wrapper.dart index 824eff30e..8b18d9cc0 100644 --- a/lib/utils/proxy_wrapper.dart +++ b/lib/utils/proxy_wrapper.dart @@ -17,8 +17,10 @@ class ProxyWrapper { // Factory method to get the singleton instance of TorSingleton static ProxyWrapper get instance => _instance; + static int get port => Tor.instance.port; + // Method to get or create the Tor instance - Future getProxyInstance() async { + Future getProxyInstance({int? portOverride}) async { if (!started) { started = true; _client = HttpClient(); @@ -27,7 +29,7 @@ class ProxyWrapper { SocksTCPClient.assignToHttpClient(_client!, [ ProxySettings( InternetAddress.loopbackIPv4, - Tor.instance.port, + portOverride ?? Tor.instance.port, password: null, ), ]);