Skip to content
Merged
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
13 changes: 13 additions & 0 deletions cw_core/lib/amount/money_double.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'dart:math';
import 'package:cw_core/amount/money.dart';
import 'package:cw_core/currency.dart';

extension ToMoney on double {
/// Turn a double representation of a currency amount to a proper Money representation
/// truncating currencies with more than 20 decimals because double can not handle more
Money? tryToMoney(Currency currency) =>
Money.tryParse(toStringAsFixed(min(currency.decimals, 20)), currency);

Money toMoney(Currency currency) =>
Money.parse(toStringAsFixed(min(currency.decimals, 20)), currency);
}
8 changes: 4 additions & 4 deletions cw_solana/lib/solana_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert';

import 'package:blockchain_utils/blockchain_utils.dart';
import 'package:cw_core/amount/money.dart';
import 'package:cw_core/amount/money_double.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/currency.dart';
import 'package:cw_core/node.dart';
Expand Down Expand Up @@ -654,7 +655,7 @@ class SolanaWalletClient {
from: outgoingFrom,
to: outgoingTo,
id: outgoingId,
amount: Money.parse(outgoingAmount.toStringAsFixed(outgoingToken.decimals), outgoingToken),
amount: outgoingAmount.toMoney(outgoingToken),
programId: outgoingMintAddress == null
? SystemProgramConst.programId.address
: SPLTokenProgramConst.tokenProgramId.address,
Expand All @@ -672,7 +673,7 @@ class SolanaWalletClient {
from: incomingFrom,
to: incomingTo,
id: incomingId,
amount: Money.parse(incomingAmount.toStringAsFixed(incomingToken.decimals), incomingToken),
amount: incomingAmount.toMoney(incomingToken),
programId: incomingMintAddress == null
? SystemProgramConst.programId.address
: SPLTokenProgramConst.tokenProgramId.address,
Expand Down Expand Up @@ -869,8 +870,7 @@ class SolanaWalletClient {
from: sender,
to: receiver,
id: signature,
amount: Money.parse(amount.toStringAsFixed((splToken ?? CryptoCurrency.sol).decimals),
splToken ?? CryptoCurrency.sol),
amount: amount.toMoney(splToken ?? CryptoCurrency.sol),
programId: SPLTokenProgramConst.tokenProgramId.address,
blockTimeInInt: blockTime?.toInt() ?? 0,
fee: Money.fromInt(fee, CryptoCurrency.sol),
Expand Down
6 changes: 3 additions & 3 deletions lib/view_model/exchange/exchange_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import 'package:cake_wallet/store/app_store.dart';
import 'package:cake_wallet/utils/exchange_provider_logger.dart';
import 'package:cw_core/amount/amount_sanitizer.dart';
import 'package:cw_core/amount/money.dart';
import 'package:cw_core/amount/money_double.dart';
import "package:cw_core/wallet_info.dart";
import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
import 'package:cake_wallet/store/dashboard/trades_store.dart';
Expand Down Expand Up @@ -804,7 +805,7 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
}

final amount_ = _enteredAmount / (forcedProvider == null ? bestRate : forcedProviderRate);
_depositAmount = Money.tryParse(amount_.toStringAsFixed(depositCurrency.decimals), depositCurrency);
_depositAmount = amount_.tryToMoney(depositCurrency);
}

@action
Expand Down Expand Up @@ -863,8 +864,7 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
}

final amount_ = _enteredAmount * (forcedProvider == null ? bestRate : forcedProviderRate);
_receiveAmount =
Money.tryParse(amount_.toStringAsFixed(min(20, receiveCurrency.decimals)), receiveCurrency);
_receiveAmount = amount_.tryToMoney(receiveCurrency);
}

bool checkIfInputMeetsMinOrMaxCondition(String input) {
Expand Down
Loading