dart_web3gas 0.0.8 copy "dart_web3gas: ^0.0.8" to clipboard
dart_web3gas: ^0.0.8 copied to clipboard

Calculate gas fees based on web3art. Refer to metamask for the calculation method.

example/main.dart

import 'dart:io';

import 'package:dart_web3gas/web3dart/web3dart.dart';

import 'package:dart_web3gas/dart_web3gas.dart';

Future<void> main() async {
  var http = HttpClient();
  http.connectionTimeout = const Duration(seconds: 2);
  {
    var web3 = Web3Client.custom(
        JsonRPCHttp('https://polygon-mumbai-bor.publicnode.com', http));
    var gas = await GasFeeController.fetchGasFeeEstimateData(web3);
    var max = gas?.gasFeeEstimates?.medium.suggestedMaxFeePerGas;
    var priority = gas?.gasFeeEstimates?.medium.suggestedMaxPriorityFeePerGas;

    //
    final eip1559GasFee = gas?.gasFeeEstimates?.medium;
    if (eip1559GasFee == null) {
      return;
    }

    //1559
    var wallet = EthPrivateKey.fromHex(
        'ae349e5dc27fe2b001ebbfe865c871e15a84929dbf42a9391941f3f7bc8f42fb');
    print('${wallet.address.hex} ');
    var transaction = Transaction(
      from: wallet.address,
      to: EthereumAddress.fromHex('0x7459e6df1f70820788809d5d25882acd3f9be3c3'),
      value: EtherAmount.fromBigInt(EtherUnit.finney, BigInt.one),
    );

    var basefee = await web3.estimateGas(
      sender: transaction.from,
      to: transaction.to,
      value: transaction.value,
    );
    if (max != null && priority != null) {
      print('basefee: $basefee');
      print(
          'max:${double.parse(max) * basefee.toDouble()} pri:${double.parse(priority) * basefee.toDouble()}');
    }

    var gasprice = await web3.getGasPrice();
    print('gasprice: $gasprice');

    final transaction0 = transaction.copyWith(
      maxFeePerGas: BigInt.from(
          double.parse(eip1559GasFee.suggestedMaxFeePerGas) *
              BigInt.from(10).pow(9).toDouble()),
      maxPriorityFeePerGas: BigInt.from(
          double.parse(eip1559GasFee.suggestedMaxPriorityFeePerGas) *
              BigInt.from(10).pow(9).toDouble()),
      // maxGas: basefee,
      // gas: EtherAmount.fromBigInt(EtherUnit.gwei, BigInt.from(2)).getInWei,
      gasPrice: gasprice,
    );

    var total = getGasFee(transaction0, gas, basefee);
    print('total:$total');
    await web3.sendTransaction(wallet, transaction0, chainId: 80001);

    // base
    final transaction1 = transaction.copyWith(
      from: wallet.address,
      to: EthereumAddress.fromHex('0x7459e6df1f70820788809d5d25882acd3f9be3c3'),
      value: EtherAmount.fromBigInt(EtherUnit.finney, BigInt.one),
      maxGas: basefee,
      // gas: EtherAmount.fromBigInt(EtherUnit.gwei, BigInt.from(40)).getInWei,
      gasPrice:
          EtherAmount.fromBigInt(EtherUnit.gwei, BigInt.from(30)).getInWei,
      //      EtherAmount.fromBigInt(
      //     EtherUnit.wei,
      //     BigInt.from(
      //         double.parse(max ?? '') * basefee.toDouble() * 10000000000))
      // .getInWei,
    );
    total = getGasFee(transaction1, gas, basefee);
    print('total:$total');
    await web3.sendTransaction(wallet, transaction1, chainId: 80001);
  }
}

BigInt getGasFee(
    Transaction? transaction, GasFeeCalculations? gasFee, BigInt? maxFee) {
  final gasPrice = transaction?.gasPrice;
  final gasLimit = transaction?.maxGas;
  if (gasPrice != null && gasLimit != null) {
    return (gasPrice * gasLimit);
  }
  return _getEip1159EthGasFee(gasFee, maxFee);
}

BigInt _getEip1159EthGasFee(GasFeeCalculations? gasFee, BigInt? maxFee) {
  ///  (base fee + priority fee) x units of gas used
  BigInt total = BigInt.zero;
  BigInt? gasPrice = gasFee?.gasPrice;

  Eip1559GasFee? eip1559Gas = gasFee?.gasFeeEstimates?.medium;
  if (eip1559Gas != null) {
    double baseFee = double.parse(eip1559Gas.suggestedMaxFeePerGas);
    double priorityFee = double.parse(eip1559Gas.suggestedMaxPriorityFeePerGas);
    gasPrice = BigInt.from(
        (baseFee + priorityFee) * BigInt.from(10).pow(9).toDouble());
    print('==== base:$baseFee priority:$priorityFee  max:$maxFee');
  }

  if (gasPrice != null && maxFee != null) {
    total += gasPrice * maxFee;
  }

  return total;
}
1
likes
130
pub points
5%
popularity

Publisher

unverified uploader

Calculate gas fees based on web3art. Refer to metamask for the calculation method.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

convert, eip55, http, json_annotation, json_rpc_2, json_serializable, logger, pointycastle, sec, stream_channel, stream_transform, typed_data, uuid

More

Packages that depend on dart_web3gas