deposit function
Makes a deposit. It detects if it's a 'createAccountDeposit' or a 'deposit' and prepares the parameters accordingly. Detects if it's an Ether, ERC 20 token and sends the transaction accordingly.
@param {BigInt} amount - The amount to be deposited @param {String} hezEthereumAddress - The Hermez address of the transaction sender @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 {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 {Number} gasLimit - Optional gas limit @param {Number} gasMultiplier - Optional gas multiplier
@returns {String} transaction hash
Implementation
Future<String?> deposit(
HermezCompressedAmount amount,
String hezEthereumAddress,
Token token,
String babyJubJub,
String privateKey,
{BigInt? approveMaxGas,
BigInt? depositMaxGas,
int gasPrice = 0}) async {
if (approveMaxGas == null || depositMaxGas == null) {
LinkedHashMap<String, BigInt> gasLimits =
await depositGasLimit(amount, hezEthereumAddress, token, babyJubJub);
if (approveMaxGas == null) {
approveMaxGas = gasLimits['approveGasLimit'];
}
if (depositMaxGas == null) {
depositMaxGas = gasLimits['depositGasLimit'];
}
}
EtherAmount ethGasPrice;
if (gasPrice > 0) {
ethGasPrice = EtherAmount.inWei(BigInt.from(gasPrice));
} else {
ethGasPrice = await HermezSDK.currentWeb3Client!.getGasPrice();
}
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['Hermez']!, "Hermez");
final credentials =
await HermezSDK.currentWeb3Client!.credentialsFromPrivateKey(privateKey);
final from = await credentials.extractAddress();
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) {
int nonce = await HermezSDK.currentWeb3Client!
.getTransactionCount(from, atBlock: BlockNum.pending());
Transaction transaction = Transaction.callContract(
contract: hermezContract,
function: _addL1Transaction(hermezContract),
from: from,
parameters: transactionParameters,
maxGas: depositMaxGas!.toInt() - 1000,
gasPrice: ethGasPrice,
value: EtherAmount.fromUnitAndValue(
EtherUnit.wei, BigInt.from(decompressedAmount)),
nonce: nonce);
print(
'deposit ETH --> privateKey: $privateKey, sender: $from, receiver: ${hermezContract.address},'
' amountInWei: $decompressedAmount, depositGasLimit: ${depositMaxGas.toInt()}, gasPrice: ${ethGasPrice.getInWei}');
String? txHash;
try {
txHash = await HermezSDK.currentWeb3Client!.sendTransaction(
credentials, transaction,
chainId: getCurrentEnvironment()!.chainId);
} catch (e) {
print(e.toString());
}
print(txHash);
return txHash;
}
//int nonceBefore = await HermezSDK.currentWeb3Client!
// .getTransactionCount(from, atBlock: BlockNum.pending());
await approve(BigInt.from(decompressedAmount), from.hex,
token.ethereumAddress!, token.name!, credentials,
gasLimit: approveMaxGas, gasPrice: gasPrice);
int nonce = await HermezSDK.currentWeb3Client!
.getTransactionCount(from, atBlock: BlockNum.pending());
/*int correctNonce = nonceAfter;
if (nonceBefore == nonceAfter) {
correctNonce = nonceAfter + 1;
}*/
// Keep in mind that web3.eth.getTransactionCount(walletAddress)
// will only give you the last CONFIRMED nonce.
// So it won't take the unmined ones into account.
Transaction transaction = Transaction.callContract(
contract: hermezContract,
function: _addL1Transaction(hermezContract),
parameters: transactionParameters,
maxGas: depositMaxGas!.toInt() - 1000,
gasPrice: ethGasPrice,
nonce: nonce);
print(
'deposit ERC20--> privateKey: $privateKey, sender: $from, receiver: ${hermezContract.address},'
' amountInWei: $amount, depositGasLimit: ${depositMaxGas.toInt()}, gasPrice: ${ethGasPrice.getInWei}');
String? txHash;
try {
txHash = await HermezSDK.currentWeb3Client!.sendTransaction(
credentials, transaction,
chainId: getCurrentEnvironment()!.chainId);
} catch (e) {
print(e.toString());
}
print(txHash);
return txHash;
}