Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions assets/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -490,13 +490,17 @@
"withdrawTronEnergyUsed": "Energy used",
"withdrawTronEnergyFee": "Energy fee",
"withdrawTronEnergySource": "Energy source",
"withdrawTronAccountActivationFee": "Account activation fee",
"withdrawTronFeeSummary": "TRON fee summary",
"withdrawTronFeePaidIn": "Paid in {}",
"withdrawTronBandwidthCovered": "Covered by free NET bandwidth",
"withdrawTronEnergyCovered": "Covered by free energy",
"withdrawTronResourceNotUsed": "Not used",
"withdrawTronFeeSummaryCharged": "Network will charge {} {}.",
"withdrawTronFeeSummaryCovered": "No {} fee will be charged (covered by resources).",
"withdrawTronPreviewExpired": "This TRON transaction preview expired. Regenerate it to continue.",
"withdrawTronPreviewRefreshFailed": "This TRON transaction preview expired and could not be refreshed.",
"withdrawTronPreviewRegenerate": "Regenerate",
"txHistoryFetchError": "Error fetching tx history from the endpoint. Unsupported type: {}",
"txHistoryNoTransactions": "Transactions are not available",
"maxGapLimitReached": "Maximum gap limit reached - please use existing unused addresses first",
Expand Down
27 changes: 24 additions & 3 deletions lib/bloc/coins_bloc/coins_repo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -808,17 +808,38 @@ class CoinsRepo {
_invalidateActivatedAssetsCache();
}

double? getUsdPriceByAmount(String amount, String coinAbbr) {
/// Calculates USD value for a numeric [amount] of [coinAbbr].
///
/// Prefer this method over [getUsdPriceByAmount] to avoid string parsing
/// issues (e.g. accidentally passing display-formatted values like
/// `"1.1 TRX"`).
double? getUsdPriceForAmount(num amount, String coinAbbr) {
final Coin? coin = getCoin(coinAbbr);
final double? parsedAmount = double.tryParse(amount);
final double parsedAmount = amount.toDouble();
final double? usdPrice = coin?.usdPrice?.price?.toDouble();

if (coin == null || usdPrice == null || parsedAmount == null) {
if (coin == null || usdPrice == null) {
return null;
}
return parsedAmount * usdPrice;
}

@Deprecated(
'Use getUsdPriceForAmount(num amount, String coinAbbr) to avoid '
'string-parsing bugs from display-formatted values.',
)
double? getUsdPriceByAmount(String amount, String coinAbbr) {
final double? parsedAmount = double.tryParse(amount);
if (parsedAmount == null) {
_log.warning(
'Invalid amount "$amount" passed to getUsdPriceByAmount for $coinAbbr. '
'Use getUsdPriceForAmount() with a numeric value.',
);
return null;
}
return getUsdPriceForAmount(parsedAmount, coinAbbr);
}

/// Fetches current prices for a broad set of assets
///
/// This method is used to fetch prices for a broad set of assets so unauthenticated users
Expand Down
24 changes: 15 additions & 9 deletions lib/bloc/coins_bloc/coins_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,34 @@ class CoinsState extends Equatable {
return getPriceForAsset(assetId)?.change24h?.toDouble();
}

/// Calculates the USD price for a given amount of a coin
///
/// [amount] The amount of the coin as a string
/// [coinAbbr] The abbreviation/symbol of the coin
/// Calculates the USD price for a given numeric [amount] of [coinAbbr].
///
/// Returns null if:
/// - The coin is not found in the state
/// - The amount cannot be parsed to a double
/// - The coin does not have a USD price
///
/// Note: This will be migrated to use the SDK's price functionality in the future.
/// See the MarketDataManager in the SDK for the new implementation.
@Deprecated('Use sdk.prices.fiatPrice(assetId) * amount instead')
double? getUsdPriceByAmount(String amount, String coinAbbr) {
double? getUsdPriceForAmount(num amount, String coinAbbr) {
final Coin? coin = coins[coinAbbr];
final double? parsedAmount = double.tryParse(amount);
final double parsedAmount = amount.toDouble();
final CexPrice? cexPrice = prices[coinAbbr.toUpperCase()];
final double? usdPrice = cexPrice?.price?.toDouble();

if (coin == null || usdPrice == null || parsedAmount == null) {
if (coin == null || usdPrice == null) {
return null;
}
return parsedAmount * usdPrice;
}

/// Backward-compatible string overload.
@Deprecated(
'Use getUsdPriceForAmount(num amount, String coinAbbr) to avoid '
'string-parsing bugs from display-formatted values.',
)
double? getUsdPriceByAmount(String amount, String coinAbbr) {
final double? parsedAmount = double.tryParse(amount);
if (parsedAmount == null) return null;
return getUsdPriceForAmount(parsedAmount, coinAbbr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ class CustomTokenImportBloc

final balanceInfo = await _coinsRepo.tryGetBalanceInfo(tokenData.id);
final balance = balanceInfo.spendable;
final usdBalance = _coinsRepo.getUsdPriceByAmount(
balance.toString(),
final usdBalance = _coinsRepo.getUsdPriceForAmount(
balance.toDouble(),
tokenData.id.id,
);

Expand Down
Loading
Loading