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_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<TokenSendView> {
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)

View file

@ -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<DesktopTokenSend> {
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<DesktopTokenSend> {
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<StackColors>()!

View file

@ -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,

View file

@ -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"