approveGasLimit function

Future<BigInt> approveGasLimit(
  1. BigInt amount,
  2. String accountAddress,
  3. String tokenContractAddress,
  4. String tokenContractName,
)

Sends an approve transaction to an ERC 20 contract for a certain amount of tokens

@param {BigInt} amount - Amount of tokens to be approved by the ERC 20 contract @param {String} accountAddress - The Ethereum address of the transaction sender @param {String} contractAddress - The token smart contract address @param {String} providerUrl - Network url (i.e, http://localhost:8545). Optional @param {Object} signerData - Signer data used to build a Signer to send the transaction

@returns {Promise} transaction

Implementation

Future<BigInt> approveGasLimit(BigInt amount, String accountAddress,
    String tokenContractAddress, String tokenContractName) async {
  BigInt gasLimit = BigInt.zero;
  EthereumAddress from = EthereumAddress.fromHex(accountAddress);
  EthereumAddress to = EthereumAddress.fromHex(tokenContractAddress);
  EthereumAddress hermezAddress =
      EthereumAddress.fromHex(getCurrentEnvironment()!.contracts['Hermez']!);
  EtherAmount value = EtherAmount.zero();
  Uint8List? data;

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

  try {
    final allowanceCall = await HermezSDK.currentWeb3Client!.call(
        contract: contract,
        function: _allowance(contract),
        params: [from, hermezAddress]);
    final allowance = allowanceCall.first as BigInt;

    if (allowance < amount) {
      Transaction transaction = Transaction.callContract(
        contract: contract,
        function: _approve(contract),
        parameters: [
          hermezAddress,
          amount,
        ],
      );
      data = transaction.data;
      gasLimit = await HermezSDK.currentWeb3Client!
          .estimateGas(sender: from, to: to, value: value, data: data);
      gasLimit += BigInt.from(GAS_LIMIT_APPROVE_OFFSET);
      return gasLimit;
    } else {
      return gasLimit;
    }
  } catch (error, trace) {
    print(error);
    print(trace);
    gasLimit = BigInt.from(GAS_LIMIT_APPROVE_DEFAULT);
    gasLimit += BigInt.from(GAS_LIMIT_APPROVE_OFFSET);
    return gasLimit;
  }
}