From 3566d75d586050e84b916d7ae501fc06604d8694 Mon Sep 17 00:00:00 2001 From: julian Date: Thu, 21 Nov 2024 09:14:27 -0600 Subject: [PATCH] Do token balance check before allowing send and fix some amount formatting bugs --- lib/pages/send_view/token_send_view.dart | 5 +++- .../sub_widgets/desktop_token_send.dart | 23 ++++++++----------- .../impl/sub_wallets/eth_token_wallet.dart | 13 +++++++++++ pubspec.lock | 4 ++-- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/lib/pages/send_view/token_send_view.dart b/lib/pages/send_view/token_send_view.dart index 3ad00fe40..4837b9773 100644 --- a/lib/pages/send_view/token_send_view.dart +++ b/lib/pages/send_view/token_send_view.dart @@ -27,6 +27,7 @@ import '../../utilities/address_utils.dart'; import '../../utilities/amount/amount.dart'; import '../../utilities/amount/amount_formatter.dart'; import '../../utilities/amount/amount_input_formatter.dart'; +import '../../utilities/amount/amount_unit.dart'; import '../../utilities/assets.dart'; import '../../utilities/barcode_scanner_interface.dart'; import '../../utilities/clipboard_interface.dart'; @@ -1033,7 +1034,9 @@ class _TokenSendViewState extends ConsumerState { child: Padding( padding: const EdgeInsets.all(12), child: Text( - tokenContract.symbol, + ref + .watch(pAmountUnit(coin)) + .unitForContract(tokenContract), style: STextStyles.smallMed14(context) .copyWith( color: Theme.of(context) diff --git a/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/desktop_token_send.dart b/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/desktop_token_send.dart index e05a9f46a..158beed86 100644 --- a/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/desktop_token_send.dart +++ b/lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/desktop_token_send.dart @@ -28,6 +28,7 @@ import '../../../../utilities/address_utils.dart'; import '../../../../utilities/amount/amount.dart'; import '../../../../utilities/amount/amount_formatter.dart'; import '../../../../utilities/amount/amount_input_formatter.dart'; +import '../../../../utilities/amount/amount_unit.dart'; import '../../../../utilities/barcode_scanner_interface.dart'; import '../../../../utilities/clipboard_interface.dart'; import '../../../../utilities/constants.dart'; @@ -394,19 +395,13 @@ class _DesktopTokenSendState extends ConsumerState { void _cryptoAmountChanged() async { if (!_cryptoAmountChangeLock) { - final String cryptoAmount = cryptoAmountController.text; - if (cryptoAmount.isNotEmpty && - cryptoAmount != "." && - cryptoAmount != ",") { - _amountToSend = cryptoAmount.contains(",") - ? Decimal.parse(cryptoAmount.replaceFirst(",", ".")).toAmount( - fractionDigits: - ref.read(pCurrentTokenWallet)!.tokenContract.decimals, - ) - : Decimal.parse(cryptoAmount).toAmount( - fractionDigits: - ref.read(pCurrentTokenWallet)!.tokenContract.decimals, - ); + final cryptoAmount = ref.read(pAmountFormatter(coin)).tryParse( + cryptoAmountController.text, + ethContract: ref.read(pCurrentTokenWallet)!.tokenContract, + ); + + if (cryptoAmount != null) { + _amountToSend = cryptoAmount; if (_cachedAmountToSend != null && _cachedAmountToSend == _amountToSend) { return; @@ -805,7 +800,7 @@ class _DesktopTokenSendState extends ConsumerState { child: Padding( padding: const EdgeInsets.all(12), child: Text( - tokenContract.symbol, + ref.watch(pAmountUnit(coin)).unitForContract(tokenContract), style: STextStyles.smallMed14(context).copyWith( color: Theme.of(context) .extension()! diff --git a/lib/wallets/wallet/impl/sub_wallets/eth_token_wallet.dart b/lib/wallets/wallet/impl/sub_wallets/eth_token_wallet.dart index ad6e31d79..99d831f4b 100644 --- a/lib/wallets/wallet/impl/sub_wallets/eth_token_wallet.dart +++ b/lib/wallets/wallet/impl/sub_wallets/eth_token_wallet.dart @@ -242,6 +242,19 @@ class EthTokenWallet extends Wallet { final amount = txData.recipients!.first.amount; final address = txData.recipients!.first.address; + await updateBalance(); + final info = await mainDB.isar.tokenWalletInfo + .where() + .walletIdTokenAddressEqualTo(walletId, tokenContract.address) + .findFirst(); + final availableBalance = info?.getCachedBalance().spendable ?? + Amount.zeroWith( + fractionDigits: tokenContract.decimals, + ); + if (amount > availableBalance) { + throw Exception("Insufficient balance"); + } + final tx = web3dart.Transaction.callContract( contract: _deployedContract, function: _sendFunction, diff --git a/pubspec.lock b/pubspec.lock index 7cee10d78..a973ef92f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -814,8 +814,8 @@ packages: dependency: "direct main" description: path: "." - ref: cc7b43b731e4a7906dd25d4364a08e34554cee19 - resolved-ref: cc7b43b731e4a7906dd25d4364a08e34554cee19 + ref: "9318bdd8e76e4dc8a49e3d64e8851c85e017eff3" + resolved-ref: "9318bdd8e76e4dc8a49e3d64e8851c85e017eff3" url: "https://github.com/cypherstack/flutter_libsparkmobile.git" source: git version: "0.0.1"