Do token balance check before allowing send and fix some amount formatting bugs

This commit is contained in:
julian 2024-11-21 09:14:27 -06:00 committed by julian-CStack
parent 31e785c23f
commit 3566d75d58
4 changed files with 28 additions and 17 deletions

View file

@ -27,6 +27,7 @@ import '../../utilities/address_utils.dart';
import '../../utilities/amount/amount.dart'; import '../../utilities/amount/amount.dart';
import '../../utilities/amount/amount_formatter.dart'; import '../../utilities/amount/amount_formatter.dart';
import '../../utilities/amount/amount_input_formatter.dart'; import '../../utilities/amount/amount_input_formatter.dart';
import '../../utilities/amount/amount_unit.dart';
import '../../utilities/assets.dart'; import '../../utilities/assets.dart';
import '../../utilities/barcode_scanner_interface.dart'; import '../../utilities/barcode_scanner_interface.dart';
import '../../utilities/clipboard_interface.dart'; import '../../utilities/clipboard_interface.dart';
@ -1033,7 +1034,9 @@ class _TokenSendViewState extends ConsumerState<TokenSendView> {
child: Padding( child: Padding(
padding: const EdgeInsets.all(12), padding: const EdgeInsets.all(12),
child: Text( child: Text(
tokenContract.symbol, ref
.watch(pAmountUnit(coin))
.unitForContract(tokenContract),
style: STextStyles.smallMed14(context) style: STextStyles.smallMed14(context)
.copyWith( .copyWith(
color: Theme.of(context) color: Theme.of(context)

View file

@ -28,6 +28,7 @@ import '../../../../utilities/address_utils.dart';
import '../../../../utilities/amount/amount.dart'; import '../../../../utilities/amount/amount.dart';
import '../../../../utilities/amount/amount_formatter.dart'; import '../../../../utilities/amount/amount_formatter.dart';
import '../../../../utilities/amount/amount_input_formatter.dart'; import '../../../../utilities/amount/amount_input_formatter.dart';
import '../../../../utilities/amount/amount_unit.dart';
import '../../../../utilities/barcode_scanner_interface.dart'; import '../../../../utilities/barcode_scanner_interface.dart';
import '../../../../utilities/clipboard_interface.dart'; import '../../../../utilities/clipboard_interface.dart';
import '../../../../utilities/constants.dart'; import '../../../../utilities/constants.dart';
@ -394,19 +395,13 @@ class _DesktopTokenSendState extends ConsumerState<DesktopTokenSend> {
void _cryptoAmountChanged() async { void _cryptoAmountChanged() async {
if (!_cryptoAmountChangeLock) { if (!_cryptoAmountChangeLock) {
final String cryptoAmount = cryptoAmountController.text; final cryptoAmount = ref.read(pAmountFormatter(coin)).tryParse(
if (cryptoAmount.isNotEmpty && cryptoAmountController.text,
cryptoAmount != "." && ethContract: ref.read(pCurrentTokenWallet)!.tokenContract,
cryptoAmount != ",") { );
_amountToSend = cryptoAmount.contains(",")
? Decimal.parse(cryptoAmount.replaceFirst(",", ".")).toAmount( if (cryptoAmount != null) {
fractionDigits: _amountToSend = cryptoAmount;
ref.read(pCurrentTokenWallet)!.tokenContract.decimals,
)
: Decimal.parse(cryptoAmount).toAmount(
fractionDigits:
ref.read(pCurrentTokenWallet)!.tokenContract.decimals,
);
if (_cachedAmountToSend != null && if (_cachedAmountToSend != null &&
_cachedAmountToSend == _amountToSend) { _cachedAmountToSend == _amountToSend) {
return; return;
@ -805,7 +800,7 @@ class _DesktopTokenSendState extends ConsumerState<DesktopTokenSend> {
child: Padding( child: Padding(
padding: const EdgeInsets.all(12), padding: const EdgeInsets.all(12),
child: Text( child: Text(
tokenContract.symbol, ref.watch(pAmountUnit(coin)).unitForContract(tokenContract),
style: STextStyles.smallMed14(context).copyWith( style: STextStyles.smallMed14(context).copyWith(
color: Theme.of(context) color: Theme.of(context)
.extension<StackColors>()! .extension<StackColors>()!

View file

@ -242,6 +242,19 @@ class EthTokenWallet extends Wallet {
final amount = txData.recipients!.first.amount; final amount = txData.recipients!.first.amount;
final address = txData.recipients!.first.address; 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( final tx = web3dart.Transaction.callContract(
contract: _deployedContract, contract: _deployedContract,
function: _sendFunction, function: _sendFunction,

View file

@ -814,8 +814,8 @@ packages:
dependency: "direct main" dependency: "direct main"
description: description:
path: "." path: "."
ref: cc7b43b731e4a7906dd25d4364a08e34554cee19 ref: "9318bdd8e76e4dc8a49e3d64e8851c85e017eff3"
resolved-ref: cc7b43b731e4a7906dd25d4364a08e34554cee19 resolved-ref: "9318bdd8e76e4dc8a49e3d64e8851c85e017eff3"
url: "https://github.com/cypherstack/flutter_libsparkmobile.git" url: "https://github.com/cypherstack/flutter_libsparkmobile.git"
source: git source: git
version: "0.0.1" version: "0.0.1"