Skip to content

Commit

Permalink
fix: linting
Browse files Browse the repository at this point in the history
  • Loading branch information
uF4No committed Aug 20, 2024
1 parent 9d5fbd2 commit 4b8eab9
Showing 1 changed file with 61 additions and 55 deletions.
116 changes: 61 additions & 55 deletions content/tutorials/how-to-send-l1-l2-transaction/10.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,83 +86,89 @@ Before continue, make sure you have the following:
WALLET_PRIVATE_KEY=0x..;
```
## Step-by-step
## Step-by-step overview
Sending an L1->L2 transaction requires following these steps:
1. Initialise the L1 and L2 providers and wallet that will send the transaction:
1. Initialise the providers and the wallet that will send the transaction:
```ts
const l1provider = new Provider(L1_RPC_ENDPOINT);
const l2provider = new Provider(L2_RPC_ENDPOINT);
const wallet = new Wallet(WALLET_PRIV_KEY, l2provider, l1provider);
```
```ts
const l1provider = new Provider(L1_RPC_ENDPOINT);
const l2provider = new Provider(L2_RPC_ENDPOINT);
const wallet = new Wallet(WALLET_PRIV_KEY, l2provider, l1provider);
```
1. Retrieve the current gas price on L1.
```ts
// retrieve L1 gas price
const l1GasPrice = await l1provider.getGasPrice();
console.log(`L1 gasPrice ${ethers.utils.formatEther(l1GasPrice)} ETH`);
```
```ts
// retrieve L1 gas price
const l1GasPrice = await l1provider.getGasPrice();
console.log(`L1 gasPrice ${ethers.utils.formatEther(l1GasPrice)} ETH`);
```
1. Populate the transaction that you want to execute on L2:
```ts
const contract = new Contract(L2_CONTRACT_ADDRESS, ABI, wallet);
// populate tx object
const tx = await contract.populateTransaction.setGreeting(message);
```
```ts
const contract = new Contract(L2_CONTRACT_ADDRESS, ABI, wallet);
// populate tx object
const tx = await contract.populateTransaction.setGreeting(message);
```
1. Retrieve the gas limit for sending the populated transaction to L2 using `estimateGasL1()` from `zksync-ethers` `Provider` class.
This function calls the `zks_estimateGasL1ToL2` RPC method under the hood:
```ts
// Estimate gas limit for L1-L2 tx
const l2GasLimit = await l2provider.estimateGasL1(tx);
console.log(`L2 gasLimit ${l2GasLimit.toString()}`);
```
```ts
// Estimate gas limit for L1-L2 tx
const l2GasLimit = await l2provider.estimateGasL1(tx);
console.log(`L2 gasLimit ${l2GasLimit.toString()}`);
```
1. Calculate the total transaction fee to cover the cost of sending the transaction on L1 and executing the transaction on L2:
1. Calculate the total transaction fee to cover the cost of sending the
transaction on L1 and executing the transaction on L2 using
`getBaseCost` from the `Wallet` class. This method:
```ts
const baseCost = await wallet.getBaseCost({
// L2 computation
gasLimit: l2GasLimit,
// L1 gas price
gasPrice: l1GasPrice,
});
- Retrieves the ZKsync BridgeHub contract address calling `zks_getBridgehubContract` RPC method.
- Calls the `l2TransactionBaseCost` function on the ZKsync BridgeHub system contract to retrieve the fee.
console.log(`Executing this transaction will cost ${ethers.utils.formatEther(baseCost)} ETH`);
```
```ts
const baseCost = await wallet.getBaseCost({
// L2 computation
gasLimit: l2GasLimit,
// L1 gas price
gasPrice: l1GasPrice,
});
console.log(`Executing this transaction will cost ${ethers.utils.formatEther(baseCost)} ETH`);
```
1. Encode the transaction calldata:
```ts
const iface = new ethers.utils.Interface(ABI);
const calldata = iface.encodeFunctionData("setGreeting", [message]);
```
```ts
const iface = new ethers.utils.Interface(ABI);
const calldata = iface.encodeFunctionData("setGreeting", [message]);
```
1. Finally, send the transaction from your wallet using the `requestExecute` method. This helper method:
1. Retrieves the Zksync BridgeHub contract address calling `zks_getBridgehubContract`.
2. Populates the L1-L2 transaction.
3. Sends the transaction to `requestL2TransactionDirect` function of the ZKsync BridgeHub system contract on L1.
```ts
const txReceipt = await wallet.requestExecute({
// destination contract in L2
contractAddress: L2_CONTRACT_ADDRESS,
calldata,
l2GasLimit: l2GasLimit,
refundRecipient: wallet.address,
overrides: {
// send the required amount of ETH
value: baseCost,
gasPrice: l1GasPrice,
},
});
txReceipt.wait();
```
- Retrieves the Zksync BridgeHub contract address calling `zks_getBridgehubContract`.
- Populates the L1-L2 transaction.
- Sends the transaction to the `requestL2TransactionDirect` function of the ZKsync BridgeHub system contract on L1.
```ts
const txReceipt = await wallet.requestExecute({
// destination contract in L2
contractAddress: L2_CONTRACT_ADDRESS,
calldata,
l2GasLimit: l2GasLimit,
refundRecipient: wallet.address,
overrides: {
// send the required amount of ETH
value: baseCost,
gasPrice: l1GasPrice,
},
});
txReceipt.wait();
```
## Full example
Expand Down

0 comments on commit 4b8eab9

Please sign in to comment.