Fix fee check for erc20 transactions (#1915)

This commit is contained in:
Omar Hatem 2024-12-31 17:03:36 +02:00 committed by GitHub
parent 84cc0576d5
commit a2cb994c09
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -29,7 +29,6 @@ import 'package:cw_evm/evm_chain_transaction_model.dart';
import 'package:cw_evm/evm_chain_transaction_priority.dart';
import 'package:cw_evm/evm_chain_wallet_addresses.dart';
import 'package:cw_evm/evm_ledger_credentials.dart';
import 'package:flutter/foundation.dart';
import 'package:hex/hex.dart';
import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart';
@ -348,7 +347,7 @@ abstract class EVMChainWalletBase
final CryptoCurrency transactionCurrency =
balance.keys.firstWhere((element) => element.title == _credentials.currency.title);
final erc20Balance = balance[transactionCurrency]!;
final currencyBalance = balance[transactionCurrency]!;
BigInt totalAmount = BigInt.zero;
BigInt estimatedFeesForTransaction = BigInt.zero;
int exponent = transactionCurrency is Erc20Token ? transactionCurrency.decimal : 18;
@ -385,7 +384,7 @@ abstract class EVMChainWalletBase
estimatedGasUnitsForTransaction = gasFeesModel.estimatedGasUnits;
maxFeePerGasForTransaction = gasFeesModel.maxFeePerGas;
if (erc20Balance.balance < totalAmount) {
if (currencyBalance.balance < totalAmount) {
throw EVMChainTransactionCreationException(transactionCurrency);
}
} else {
@ -398,7 +397,7 @@ abstract class EVMChainWalletBase
}
if (output.sendAll && transactionCurrency is Erc20Token) {
totalAmount = erc20Balance.balance;
totalAmount = currencyBalance.balance;
}
final gasFeesModel = await calculateActualEstimatedFeeForCreateTransaction(
@ -413,14 +412,15 @@ abstract class EVMChainWalletBase
maxFeePerGasForTransaction = gasFeesModel.maxFeePerGas;
if (output.sendAll && transactionCurrency is! Erc20Token) {
totalAmount = (erc20Balance.balance - estimatedFeesForTransaction);
if (estimatedFeesForTransaction > erc20Balance.balance) {
throw EVMChainTransactionFeesException();
}
totalAmount = (currencyBalance.balance - estimatedFeesForTransaction);
}
if (erc20Balance.balance < totalAmount) {
// check the fees on the base currency (Eth/Polygon)
if (estimatedFeesForTransaction > balance[currency]!.balance) {
throw EVMChainTransactionFeesException();
}
if (currencyBalance.balance < totalAmount) {
throw EVMChainTransactionCreationException(transactionCurrency);
}
}