withdraw function

Future<String?> withdraw(
  1. double amount,
  2. String? accountIndex,
  3. Token token,
  4. String babyJubJub,
  5. int batchNumber,
  6. List<BigInt>? merkleSiblings,
  7. String privateKey, {
  8. bool isInstant = true,
  9. BigInt? gasLimit,
  10. int gasPrice = 0,
})

Finalise the withdraw. This a L1 transaction.

@param {BigInt} amount - The amount to be withdrawn @param {String} accountIndex - The account index in hez address format e.g. hez:DAI:4444 @param {Object} token - The token information object as returned from the API @param {String} babyJubJub - The compressed BabyJubJub in hexadecimal format of the transaction sender. @param {BigInt} merkleRoot - The merkle root of the exit being withdrawn. @param {Array} merkleSiblings - An array of BigInts representing the siblings of the exit being withdrawn. @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 @param {Boolean} isInstant - Whether it should be an Instant Withdrawal @param {Boolean} filterSiblings - Whether siblings should be filtered @param {Number} gasLimit - Optional gas limit @param {Number} gasPrice - Optional gas price

Implementation

Future<String?> withdraw(
    double amount,
    String? accountIndex,
    Token token,
    String babyJubJub,
    int batchNumber,
    List<BigInt>? merkleSiblings,
    String privateKey,
    {bool isInstant = true,
    BigInt? gasLimit,
    int gasPrice = 0}) async {
  final credentials =
      await HermezSDK.currentWeb3Client!.credentialsFromPrivateKey(privateKey);
  final from = await credentials.extractAddress();

  if (gasLimit == null) {
    gasLimit = await withdrawGasLimit(amount, from.hex, accountIndex, token,
        babyJubJub, batchNumber, merkleSiblings!,
        isInstant: isInstant);
  }

  EtherAmount ethGasPrice;
  if (gasPrice > 0) {
    ethGasPrice = EtherAmount.inWei(BigInt.from(gasPrice));
  } else {
    ethGasPrice = await HermezSDK.currentWeb3Client!.getGasPrice();
  }

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

  int nonce = await HermezSDK.currentWeb3Client!
      .getTransactionCount(from, atBlock: BlockNum.pending());

  final transactionParameters = [
    BigInt.from(token.id!),
    BigInt.from(amount),
    hexToInt(babyJubJub),
    BigInt.from(batchNumber),
    merkleSiblings,
    BigInt.from(getAccountIndex(accountIndex)),
    isInstant,
  ];

  print(transactionParameters);

  Transaction transaction = Transaction.callContract(
      contract: hermezContract,
      function: _withdrawMerkleProof(hermezContract),
      parameters: transactionParameters,
      maxGas: gasLimit.toInt(),
      gasPrice: ethGasPrice,
      nonce: nonce);

  String? txHash;
  try {
    txHash = await HermezSDK.currentWeb3Client!.sendTransaction(
        credentials, transaction,
        chainId: getCurrentEnvironment()!.chainId);
  } catch (e) {
    print(e.toString());
  }

  print(txHash);

  return txHash;
}