depositGasLimit function

Future<LinkedHashMap<String, BigInt>> depositGasLimit(
  1. HermezCompressedAmount amount,
  2. String hezEthereumAddress,
  3. Token token,
  4. String babyJubJub,
)

Implementation

Future<LinkedHashMap<String, BigInt>> depositGasLimit(
    HermezCompressedAmount amount,
    String hezEthereumAddress,
    Token token,
    String babyJubJub) async {
  LinkedHashMap<String, BigInt> result = new LinkedHashMap<String, BigInt>();
  BigInt approveMaxGas = BigInt.zero;
  BigInt depositMaxGas = BigInt.zero;
  EthereumAddress from =
      EthereumAddress.fromHex(getEthereumAddress(hezEthereumAddress)!);
  EthereumAddress to = EthereumAddress.fromHex(
      getCurrentEnvironment()!.contracts[ContractName.hermez]!);
  EtherAmount value = EtherAmount.zero();
  Uint8List? data;

  final ethereumAddress = getEthereumAddress(hezEthereumAddress);

  var accounts;
  try {
    accounts = await getAccounts(hezEthereumAddress, [token.id]);
  } catch (e) {
    accounts = null;
  }

  final Account? account = accounts != null && accounts.accounts!.isNotEmpty
      ? accounts.accounts![0]
      : null;

  final hermezContract = await ContractParser.fromAssets(
      'HermezABI.json',
      getCurrentEnvironment()!.contracts[ContractName.hermez]!,
      ContractName.hermez);

  final transactionParameters = [
    account != null ? BigInt.zero : hexToInt(babyJubJub),
    account != null
        ? BigInt.from(getAccountIndex(account.accountIndex))
        : BigInt.zero,
    BigInt.from(amount.value),
    BigInt.zero,
    BigInt.from(token.id!),
    BigInt.zero,
    hexToBytes('0x')
  ];

  final decompressedAmount = HermezCompressedAmount.decompressAmount(amount);

  if (token.id == 0) {
    try {
      value = EtherAmount.fromUnitAndValue(
          EtherUnit.wei, BigInt.from(decompressedAmount));
      Transaction transaction = Transaction.callContract(
          contract: hermezContract,
          function: _addL1Transaction(hermezContract),
          from: from,
          parameters: transactionParameters,
          value: value);
      data = transaction.data;
      depositMaxGas = await HermezSDK.currentWeb3Client!
          .estimateGas(sender: from, to: to, value: value, data: data);
      depositMaxGas += BigInt.from(GAS_LIMIT_DEPOSIT_OFFSET);
    } catch (e) {
      depositMaxGas = BigInt.from(GAS_LIMIT_LOW);
    }

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

    result['depositGasLimit'] = depositMaxGas;

    print('estimate deposit ETH --> Max Gas: $depositMaxGas');

    return result;
  }

  approveMaxGas = await approveGasLimit(BigInt.from(decompressedAmount),
      ethereumAddress!, token.ethereumAddress!, token.name!);

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

  result['approveGasLimit'] = approveMaxGas;

  try {
    Transaction transaction = Transaction.callContract(
        contract: hermezContract,
        function: _addL1Transaction(hermezContract),
        from: from,
        parameters: transactionParameters,
        value: value);
    data = transaction.data;
    depositMaxGas = await HermezSDK.currentWeb3Client!
        .estimateGas(sender: from, to: to, value: value, data: data);
    depositMaxGas += BigInt.from(GAS_LIMIT_DEPOSIT_OFFSET);
  } catch (e) {
    depositMaxGas = BigInt.from(GAS_LIMIT_HIGH);
    String? fromAddress = getCurrentEnvironment()!
        .contracts[ContractName.hermez]; // Random ethereum address
    depositMaxGas += await transferGasLimit(BigInt.from(decompressedAmount),
        fromAddress, ethereumAddress, token.ethereumAddress, token.name);
  }

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

  result['depositGasLimit'] = depositMaxGas;

  print('estimate deposit ERC20 --> Max Gas: $depositMaxGas');

  return result;
}