xclud / web3dart

Ethereum library, written in Dart.
https://pub.dev/packages/web3dart
MIT License
170 stars 94 forks source link

EIP 1559 support not working #85

Closed gizemgizg closed 1 year ago

gizemgizg commented 1 year ago

I get this error when I define maxFeePerGas and maxPriorityFeePerGas parameters in the transaction. I give maxFeePerGas the value of gasPrice. I define maxPriorityFeePerGas as 0.1 double. Can you help me?

"message":"Returned error: rlp: input string too short for common.Address, decoding into (types.LegacyTx).To"

MahmoudKEA commented 1 year ago

I think this issue is related with address, make sure addresses that used For this gasPrice is not true to use with maxFeePerGas

1 - Now after ethereum upgraded to PoS, i recommended you send transaction as legacy instead EIP1559, because legacy had become cheaper than EIP1559 Check the latest transactions.

2 - If you want to get maxFeePerGas and maxPriorityFeePerGas easily, i recommended you merge my commits #65 to your package ( you'll get simple examples there ).

3 - If you want to learn how you can calculate EIP1559 gas fee check this article.

4 - If you want to calculate EIP1559 gas fee by code check this article.

makinghappen commented 1 year ago

@gizemgizg this issue has been fixed in web3dart for https://github.com/devopsdao/webthree

gizemgizg commented 1 year ago

I added two parameters but I get the same error :( Should I use gasprice? @MahmoudKhalid Transaction( from: fromEth, to: to, maxGas: maxGas, gasPrice: EtherAmount.inWei(BigInt.one), value: EtherAmount.fromInt(EtherUnit.ether, 1), data: Uint8List(0), nonce: nonce, maxFeePerGas: gasEIPInfo.maxFeePerGas, maxPriorityFeePerGas: gasEIPInfo.maxPriorityFeePerGas, );

MahmoudKEA commented 1 year ago

@gizemgizg You must set gasPrice value for legacy transaction only, For EIP1559 gasPrice must be null

Check this method below for adding gas in two cases (legacy and EIP 1559), it includes more than you need, but I want you to see how to build your tx in general

Note: My commit #65 must be merged to be able to call this method web3.getGasInEIP1559

static Future<TxGasDetailsData> addGas({
    required Transaction tx,
    bool amountAdjustment = true,
    bool eip1559Enabled = false,
    String rate = 'medium',
  }) async {
    // Gas reset
    tx = Transaction(
      from: tx.from,
      to: tx.to,
      value: tx.value,
      data: tx.data,
      nonce: tx.nonce,
    );

    // Gas limit
    final BigInt gasLimit = await web3.estimateGas(
      sender: tx.from,
      to: tx.to,
      value: tx.value,
      data: tx.data,
    );

    final BigInt estimateGas;
    final BigInt maxFee;

    // Add gas as legacy or EIP1559
    if (eip1559Enabled) {
      final Map<String, EIP1559Information> gasEIP =
          await web3.getGasInEIP1559();
      tx = tx.copyWith(
        maxGas: gasLimit.toInt(),
        maxFeePerGas: gasEIP[rate]!.maxFeePerGas,
        maxPriorityFeePerGas: gasEIP[rate]!.maxPriorityFeePerGas,
      );
      estimateGas = gasLimit * gasEIP[rate]!.estimatedGas;
      maxFee = gasLimit * tx.maxFeePerGas!.getInWei;
    } else {
      final EtherAmount gasPrice = await web3.getGasPrice();
      tx = tx.copyWith(
        maxGas: gasLimit.toInt(),
        gasPrice: gasPrice,
      );
      estimateGas = gasLimit * gasPrice.getInWei;
      maxFee = estimateGas;
    }

    BigInt total = estimateGas + tx.value!.getInWei;
    BigInt maxAmount = maxFee + tx.value!.getInWei;

    // Adjust the amount if it exceeds the balance
    if (amountAdjustment && tx.value!.getInWei > BigInt.zero) {
      final EtherAmount balance = await web3.getBalance(tx.from!);
      if (maxAmount > balance.getInWei) {
        final BigInt amountLeft = balance.getInWei - maxFee;
        if (amountLeft <= BigInt.zero) {
          throw Exception(
            "Insufficient funds for transfer, maybe it needs gas fee.",
          );
        }

        tx = tx.copyWith(value: EtherAmount.inWei(amountLeft));
        total = estimateGas + tx.value!.getInWei;
        maxAmount = maxFee + tx.value!.getInWei;
      }
    }

    return TxGasDetailsData(
      tx: tx,
      estimateGas: EtherAmount.inWei(estimateGas),
      maxFee: EtherAmount.inWei(maxFee),
      total: EtherAmount.inWei(total),
      maxAmount: EtherAmount.inWei(maxAmount),
    );
  }