transferGasLimit function

Future<BigInt> transferGasLimit(
  1. BigInt amount,
  2. String? fromAddress,
  3. String? toAddress,
  4. String? tokenContractAddress,
  5. String? tokenContractName,
)

Calculates the gas limit for a transfer transaction to an ERC 20 contract for a certain amount of tokens

@param {BigInt} amount - Amount of tokens to be transferred by the ERC 20 contract @param {String} accountAddress - The Ethereum address of the transaction sender @param {String} tokenContractAddress - The token smart contract address @param {String} tokenContractName - The token smart contract name @param {Web3Client} web3client - Web3 Client

@returns {BigInt}

Implementation

Future<BigInt> transferGasLimit(
    BigInt amount,
    String? fromAddress,
    String? toAddress,
    String? tokenContractAddress,
    String? tokenContractName) async {
  BigInt gasLimit = BigInt.zero;
  if (fromAddress == null ||
      fromAddress.isEmpty ||
      toAddress == null ||
      toAddress.isEmpty ||
      amount.sign == 0) {
    gasLimit = BigInt.from(GAS_STANDARD_ERC20_TX);
    gasLimit =
        BigInt.from((gasLimit.toInt() / pow(10, 3)).floor() * pow(10, 3));
    print('estimate transfer default ERC20 --> Max Gas: $gasLimit');
    return gasLimit;
  } else {
    // TODO: Uncomment when estimation is working well
    /*try {
      EthereumAddress from = EthereumAddress.fromHex(fromAddress);
      EthereumAddress to = EthereumAddress.fromHex(toAddress);
      EtherAmount value = EtherAmount.zero();
      Uint8List data;

      final contract = await ContractParser.fromAssets(
          'ERC20ABI.json', tokenContractAddress.toString(), tokenContractName);

      Transaction transaction = Transaction.callContract(
        contract: contract,
        function: _transfer(contract),
        parameters: [to, amount],
        from: from,
      );

      data = transaction.data;


      gasLimit = await web3client.estimateGas(
          sender: from, to: to, value: value, data: data);
      print('estimate transfer ERC20 --> Max Gas: $gasLimit');
    } catch (e) {
      print(e.toString());*/
    gasLimit = BigInt.from(GAS_STANDARD_ERC20_TX);
    print('estimate transfer default ERC20 --> Max Gas: $gasLimit');
    //}

    gasLimit =
        BigInt.from((gasLimit.toInt() / pow(10, 3)).floor() * pow(10, 3));

    return gasLimit;
  }
}