Fix fee check for erc20 transactions (#1915)

Omar Hatem committed Dec 31, 2024 at 17:03 UTC a2cb994c09352eb85ff5d8909aa07ad37933bbb7
1 file changed +9 -9
cw_evm/lib/evm_chain_wallet.dart
+9 -9
@@ -29,7 +29,6 @@ import 'package:cw_evm/evm_chain_transaction_model.dart';
29 import 'package:cw_evm/evm_chain_transaction_priority.dart';
30 import 'package:cw_evm/evm_chain_wallet_addresses.dart';
31 import 'package:cw_evm/evm_ledger_credentials.dart';
32 -import 'package:flutter/foundation.dart';
32 import 'package:hex/hex.dart';
33 import 'package:hive/hive.dart';
34 import 'package:mobx/mobx.dart';
@@ -348,7 +347,7 @@ abstract class EVMChainWalletBase
347 final CryptoCurrency transactionCurrency =
348 balance.keys.firstWhere((element) => element.title == _credentials.currency.title);
349
351 - final erc20Balance = balance[transactionCurrency]!;
350 + final currencyBalance = balance[transactionCurrency]!;
351 BigInt totalAmount = BigInt.zero;
352 BigInt estimatedFeesForTransaction = BigInt.zero;
353 int exponent = transactionCurrency is Erc20Token ? transactionCurrency.decimal : 18;
@@ -385,7 +384,7 @@ abstract class EVMChainWalletBase
384 estimatedGasUnitsForTransaction = gasFeesModel.estimatedGasUnits;
385 maxFeePerGasForTransaction = gasFeesModel.maxFeePerGas;
386
388 - if (erc20Balance.balance < totalAmount) {
387 + if (currencyBalance.balance < totalAmount) {
388 throw EVMChainTransactionCreationException(transactionCurrency);
389 }
390 } else {
@@ -398,7 +397,7 @@ abstract class EVMChainWalletBase
397 }
398
399 if (output.sendAll && transactionCurrency is Erc20Token) {
401 - totalAmount = erc20Balance.balance;
400 + totalAmount = currencyBalance.balance;
401 }
402
403 final gasFeesModel = await calculateActualEstimatedFeeForCreateTransaction(
@@ -413,14 +412,15 @@ abstract class EVMChainWalletBase
412 maxFeePerGasForTransaction = gasFeesModel.maxFeePerGas;
413
414 if (output.sendAll && transactionCurrency is! Erc20Token) {
416 - totalAmount = (erc20Balance.balance - estimatedFeesForTransaction);
415 + totalAmount = (currencyBalance.balance - estimatedFeesForTransaction);
416 + }
417
418 - if (estimatedFeesForTransaction > erc20Balance.balance) {
419 - throw EVMChainTransactionFeesException();
420 - }
418 + // check the fees on the base currency (Eth/Polygon)
419 + if (estimatedFeesForTransaction > balance[currency]!.balance) {
420 + throw EVMChainTransactionFeesException();
421 }
422
423 - if (erc20Balance.balance < totalAmount) {
423 + if (currencyBalance.balance < totalAmount) {
424 throw EVMChainTransactionCreationException(transactionCurrency);
425 }
426 }