CW-672: Enhance ETH Transaction Fee Calculation (#1545)

* fix: Eth transaction fees WIP

* Revert "fix: Eth transaction fees WIP"

This reverts commit b9a469bc7e.

* fix: Modifying fee WIP

* fix: Enhance ETH Wallet fee calculation WIP

* feat: Enhance Transaction fees for ETH Transactions, Native transactions done, left with ERC20 transactions

* fix: Pre PR cleanups

* minor things [skip ci]

---------

Co-authored-by: OmarHatem <omarh.ismail1@gmail.com>
This commit is contained in:
Adegoke David 2024-07-21 00:04:22 +01:00 committed by GitHub
parent 4410101672
commit 415d2a3573
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 186 additions and 45 deletions

View file

@ -10,7 +10,7 @@ import 'package:cw_evm/evm_chain_transaction_priority.dart';
import 'package:cw_evm/evm_erc20_balance.dart';
import 'package:cw_evm/pending_evm_chain_transaction.dart';
import 'package:cw_evm/.secrets.g.dart' as secrets;
import 'package:flutter/services.dart';
import 'package:flutter/foundation.dart';
import 'package:hex/hex.dart' as hex;
import 'package:http/http.dart';
import 'package:web3dart/web3dart.dart';
@ -65,16 +65,65 @@ abstract class EVMChainClient {
Future<int> getGasUnitPrice() async {
try {
final gasPrice = await _client!.getGasPrice();
return gasPrice.getInWei.toInt();
} catch (_) {
return 0;
}
}
Future<int> getEstimatedGas() async {
Future<int> getGasBaseFee() async {
try {
final estimatedGas = await _client!.estimateGas();
return estimatedGas.toInt();
final blockInfo = await _client!.getBlockInformation(isContainFullObj: false);
final baseFee = blockInfo.baseFeePerGas;
return baseFee!.getInWei.toInt();
} catch (_) {
return 0;
}
}
Future<int> getEstimatedGas({
String? contractAddress,
required EthereumAddress toAddress,
required EthereumAddress senderAddress,
required EtherAmount value,
EtherAmount? gasPrice,
// EtherAmount? maxFeePerGas,
// EtherAmount? maxPriorityFeePerGas,
}) async {
try {
if (contractAddress == null) {
final estimatedGas = await _client!.estimateGas(
sender: senderAddress,
gasPrice: gasPrice,
to: toAddress,
value: value,
// maxPriorityFeePerGas: maxPriorityFeePerGas,
// maxFeePerGas: maxFeePerGas,
);
return estimatedGas.toInt();
} else {
final contract = DeployedContract(
ethereumContractAbi,
EthereumAddress.fromHex(contractAddress),
);
final transferFunction = contract.function('transferFrom');
final estimatedGas = await _client!.estimateGas(
sender: senderAddress,
to: toAddress,
value: value,
data: transferFunction.encodeCall([
senderAddress,
toAddress,
value.getInWei,
]),
);
return estimatedGas.toInt();
}
} catch (_) {
return 0;
}
@ -84,7 +133,7 @@ abstract class EVMChainClient {
required Credentials privateKey,
required String toAddress,
required BigInt amount,
required int gas,
required BigInt gas,
required EVMChainTransactionPriority priority,
required CryptoCurrency currency,
required int exponent,
@ -97,8 +146,6 @@ abstract class EVMChainClient {
bool isNativeToken = currency == CryptoCurrency.eth || currency == CryptoCurrency.maticpoly;
final price = _client!.getGasPrice();
final Transaction transaction = createTransaction(
from: privateKey.address,
to: EthereumAddress.fromHex(toAddress),
@ -130,11 +177,10 @@ abstract class EVMChainClient {
_sendTransaction = () async => await sendTransaction(signedTransaction);
return PendingEVMChainTransaction(
signedTransaction: signedTransaction,
amount: amount.toString(),
fee: BigInt.from(gas) * (await price).getInWei,
fee: gas,
sendTransaction: _sendTransaction,
exponent: exponent,
);
@ -233,7 +279,6 @@ abstract class EVMChainClient {
final decodedResponse = jsonDecode(response.body)[0] as Map<String, dynamic>;
final symbol = (decodedResponse['symbol'] ?? '') as String;
String filteredSymbol = symbol.replaceFirst(RegExp('^\\\$'), '');