Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions cw_tron/lib/tron_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class TronClient {

int get chainId => 1000;

int retryCount = 0;
static const int MAX_RETRIES = 3;

Future<void> switchNodes() async {}

Future<List<TronTransactionModel>> fetchTransactions(String address,
{String? contractAddress}) async {
try {
Expand All @@ -50,9 +55,14 @@ class TronClient {
return (jsonResponse['data'] as List).map((e) {
return TronTransactionModel.fromJson(e as Map<String, dynamic>);
}).toList();
} else {
if (retryCount > MAX_RETRIES) {
retryCount++;
await switchNodes();
return fetchTransactions(address, contractAddress: contractAddress);
}
return [];
}

return [];
} catch (e, s) {
log('Error getting tx: ${e.toString()}\n ${s.toString()}');
return [];
Expand Down Expand Up @@ -83,8 +93,13 @@ class TronClient {
return (jsonResponse['data'] as List).map((e) {
return TronTRC20TransactionModel.fromJson(e as Map<String, dynamic>);
}).toList();
} else {
if (retryCount < MAX_RETRIES) {
retryCount++;
await switchNodes();
return fetchTrc20ExcludedTransactions(address);
}
}

return [];
} catch (e, s) {
log('Error getting trc20 tx: ${e.toString()}\n ${s.toString()}');
Expand Down
3 changes: 3 additions & 0 deletions lib/anonpay/anonpay_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:cake_wallet/anonpay/anonpay_status_response.dart';
import 'package:cake_wallet/core/fiat_conversion_service.dart';
import 'package:cake_wallet/entities/fiat_currency.dart';
import 'package:cake_wallet/exchange/limits.dart';
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
import 'package:cw_core/wallet_base.dart';
import 'package:http/http.dart';
import 'package:cw_core/crypto_currency.dart';
Expand Down Expand Up @@ -130,13 +131,15 @@ class AnonPayApi {
Future<Limits> fetchLimits({
FiatCurrency? fiatCurrency,
required CryptoCurrency cryptoCurrency,
required FiatConversionStore fiatConversionStore,
}) async {
double fiatRate = 0.0;
if (fiatCurrency != null) {
fiatRate = await FiatConversionService.fetchPrice(
crypto: cryptoCurrency,
fiat: fiatCurrency,
torOnly: useTorOnly,
fiatConversionStore: fiatConversionStore,
);
}

Expand Down
54 changes: 30 additions & 24 deletions lib/core/fiat_conversion_service.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cake_wallet/entities/fiat_currency.dart';
import 'dart:convert';
Expand All @@ -22,46 +23,51 @@ Future<double> _fetchPrice(Map<String, dynamic> args) async {
};

num price = 0.0;
late final Uri uri;
if (torOnly) {
uri = Uri.http(_fiatApiOnionAuthority, _fiatApiPath, queryParams);
} else {
uri = Uri.https(_fiatApiClearNetAuthority, _fiatApiPath, queryParams);
}

try {
late final Uri uri;
if (torOnly) {
uri = Uri.http(_fiatApiOnionAuthority, _fiatApiPath, queryParams);
} else {
uri = Uri.https(_fiatApiClearNetAuthority, _fiatApiPath, queryParams);
}

final response = await get(uri);

if (response.statusCode != 200) {
return 0.0;
}
final response = await get(uri);

final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final results = responseJSON['results'] as Map<String, dynamic>;
if (response.statusCode != 200) {
throw Exception('Fiat conversion service unavailable: ${response.statusCode}');
}

if (results.isNotEmpty) {
price = results.values.first as num;
}
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final results = responseJSON['results'] as Map<String, dynamic>;

return price.toDouble();
} catch (e) {
return price.toDouble();
if (results.isNotEmpty) {
price = results.values.first as num;
}

return price.toDouble();
}

Future<double> _fetchPriceAsync(CryptoCurrency crypto, FiatCurrency fiat, bool torOnly) async =>
compute(_fetchPrice, {
Future<double> _fetchPriceAsync(CryptoCurrency crypto, FiatCurrency fiat, bool torOnly,
FiatConversionStore fiatConversionStore) async {
try {
final price = await compute(_fetchPrice, {
'fiat': fiat.toString(),
'crypto': crypto.toString(),
'torOnly': torOnly,
});
fiatConversionStore.unavailable = false;
return price;
} catch (e) {
fiatConversionStore.unavailable = true;
return 0.0;
}
}

class FiatConversionService {
static Future<double> fetchPrice({
required CryptoCurrency crypto,
required FiatCurrency fiat,
required bool torOnly,
required FiatConversionStore fiatConversionStore,
}) async =>
await _fetchPriceAsync(crypto, fiat, torOnly);
await _fetchPriceAsync(crypto, fiat, torOnly, fiatConversionStore);
}
3 changes: 2 additions & 1 deletion lib/di.dart
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ Future<void> setup({
getIt.registerFactory(() => BalanceViewModel(
appStore: getIt.get<AppStore>(),
settingsStore: getIt.get<SettingsStore>(),
fiatConvertationStore: getIt.get<FiatConversionStore>()));
fiatConversionStore: getIt.get<FiatConversionStore>()));

getIt.registerFactory(() => DashboardViewModel(
balanceViewModel: getIt.get<BalanceViewModel>(),
Expand Down Expand Up @@ -703,6 +703,7 @@ Future<void> setup({
_anonpayInvoiceInfoSource,
getIt.get<SharedPreferences>(),
pageOption,
getIt.get<FiatConversionStore>(),
);
});

Expand Down
2 changes: 0 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ Future<void> initializeAppConfigs({bool loadWallet = true}) async {
tradesSource: trades,
ordersSource: orders,
unspentCoinsInfoSource: unspentCoinsInfoSource,
// fiatConvertationService: fiatConvertationService,
templates: templates,
exchangeTemplates: exchangeTemplates,
transactionDescriptions: transactionDescriptions,
Expand All @@ -234,7 +233,6 @@ Future<void> initialSetup(
required Box<Contact> contactSource,
required Box<Trade> tradesSource,
required Box<Order> ordersSource,
// required FiatConvertationService fiatConvertationService,
required Box<Template> templates,
required Box<ExchangeTemplate> exchangeTemplates,
required Box<TransactionDescription> transactionDescriptions,
Expand Down
6 changes: 4 additions & 2 deletions lib/reactions/fiat_rate_update.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Future<void> startFiatRateUpdate(
await FiatConversionService.fetchPrice(
crypto: appStore.wallet!.currency,
fiat: settingsStore.fiatCurrency,
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly,
fiatConversionStore: fiatConversionStore);

Iterable<CryptoCurrency>? currencies;
if (appStore.wallet!.type == WalletType.ethereum) {
Expand Down Expand Up @@ -64,7 +65,8 @@ Future<void> startFiatRateUpdate(
fiatConversionStore.prices[currency] = await FiatConversionService.fetchPrice(
crypto: currency,
fiat: settingsStore.fiatCurrency,
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly,
fiatConversionStore: fiatConversionStore);
}.call();
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/reactions/on_current_fiat_api_mode_change.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void startCurrentFiatApiModeChangeReaction(AppStore appStore,
await FiatConversionService.fetchPrice(
crypto: appStore.wallet!.currency,
fiat: settingsStore.fiatCurrency,
torOnly: fiatApiMode == FiatApiMode.torOnly);
torOnly: fiatApiMode == FiatApiMode.torOnly,
fiatConversionStore: fiatConversionStore);
});
}
3 changes: 2 additions & 1 deletion lib/reactions/on_current_fiat_change.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void startCurrentFiatChangeReaction(AppStore appStore,
await FiatConversionService.fetchPrice(
crypto: cryptoCurrency,
fiat: fiatCurrency,
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly,
fiatConversionStore: fiatConversionStore);
});
}
6 changes: 4 additions & 2 deletions lib/reactions/on_current_wallet_change.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ void startCurrentWalletChangeReaction(
fiatConversionStore.prices[wallet.currency] = await FiatConversionService.fetchPrice(
crypto: wallet.currency,
fiat: settingsStore.fiatCurrency,
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly,
fiatConversionStore: fiatConversionStore);

Iterable<CryptoCurrency>? currencies;
if (wallet.type == WalletType.ethereum) {
Expand All @@ -134,7 +135,8 @@ void startCurrentWalletChangeReaction(
fiatConversionStore.prices[currency] = await FiatConversionService.fetchPrice(
crypto: currency,
fiat: settingsStore.fiatCurrency,
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly);
torOnly: settingsStore.fiatApiMode == FiatApiMode.torOnly,
fiatConversionStore: fiatConversionStore);
}.call();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class CryptoBalanceWidget extends StatelessWidget {
child: Observer(
builder: (_) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
dashboardViewModel.balanceViewModel.asset,
Expand Down Expand Up @@ -119,6 +120,57 @@ class CryptoBalanceWidget extends StatelessWidget {
),
],
)),
if (!dashboardViewModel.isFiatConversionServiceAvailable)
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
InkWell(
onTap: () async {
final learnMorePressed = await showPopUp<bool>(
context: context,
builder: (BuildContext context) {
return AlertWithTwoActions(
alertTitle: S.of(context).fiat_unavailable,
alertContent: S.of(context).fiat_unavailable_desc,
leftButtonText: S.of(context).learn_more,
rightButtonText: S.of(context).ok,
actionLeftButton: () => Navigator.of(context).pop(true),
actionRightButton: () => Navigator.of(context).pop(false));
}) ??
false;
if (learnMorePressed) {
launchUrl(
Uri.parse("https://docs.cakewallet.com/fiat-api-unavailable"),
mode: LaunchMode.externalApplication,
);
}
},
child: Row(
children: [
Text(
S.of(context).fiat_unavailable,
style: TextStyle(
fontSize: 12,
fontFamily: 'Lato',
fontWeight: FontWeight.w400,
color: Theme.of(context).extension<BalancePageTheme>()!.labelTextColor,
height: 1,
),
softWrap: true,
),
Padding(
padding: const EdgeInsets.only(left: 4, right: 4, bottom: 16, top: 16),
child: Icon(Icons.help_outline,
size: 16,
color: Theme.of(context).extension<BalancePageTheme>()!.labelTextColor),
),
SizedBox(width: 16),
],
),
),
SizedBox(width: 16),
],
),
Observer(
builder: (_) {
if (dashboardViewModel.balanceViewModel.isShowCard && FeatureFlag.isCakePayEnabled) {
Expand Down Expand Up @@ -321,7 +373,8 @@ class CryptoBalanceWidget extends StatelessWidget {
leftButtonTitle: S.of(context).litecoin_mweb_dismiss,
rightButtonTitle: S.of(context).learn_more,
leftButtonAction: () => dashboardViewModel.dismissDecredInfoCard(),
rightButtonAction: () => launchUrl(Uri.parse("https://docs.cakewallet.com/cryptos/decred/#spv-sync")),
rightButtonAction: () => launchUrl(
Uri.parse("https://docs.cakewallet.com/cryptos/decred/#spv-sync")),
),
),
],
Expand Down
3 changes: 3 additions & 0 deletions lib/store/dashboard/fiat_conversion_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ abstract class FiatConversionStoreBase with Store {

@observable
ObservableMap<CryptoCurrency, double> prices;

@observable
bool unavailable = false;
}
4 changes: 4 additions & 0 deletions lib/view_model/anon_invoice_page_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:cake_wallet/anonpay/anonpay_request.dart';
import 'package:cake_wallet/core/execution_state.dart';
import 'package:cake_wallet/entities/fiat_currency.dart';
import 'package:cake_wallet/entities/preferences_key.dart';
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
import 'package:cw_core/receive_page_option.dart';
import 'package:cake_wallet/store/settings_store.dart';
import 'package:cw_core/crypto_currency.dart';
Expand All @@ -27,6 +28,7 @@ abstract class AnonInvoicePageViewModelBase with Store {
this._anonpayInvoiceInfoSource,
this.sharedPreferences,
this.pageOption,
this.fiatConversionStore,
) : receipientEmail = '',
receipientName = '',
description = '',
Expand All @@ -46,6 +48,7 @@ abstract class AnonInvoicePageViewModelBase with Store {
final Box<AnonpayInvoiceInfo> _anonpayInvoiceInfoSource;
final SharedPreferences sharedPreferences;
final ReceivePageOption pageOption;
final FiatConversionStore fiatConversionStore;

@observable
Currency selectedCurrency;
Expand Down Expand Up @@ -159,6 +162,7 @@ abstract class AnonInvoicePageViewModelBase with Store {
final limit = await anonPayApi.fetchLimits(
cryptoCurrency: cryptoCurrency,
fiatCurrency: selectedCurrency is FiatCurrency ? selectedCurrency as FiatCurrency : null,
fiatConversionStore: fiatConversionStore,
);
minimum = limit.min;
maximum = limit.max != null ? limit.max! / 4 : null;
Expand Down
Loading