You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/app-developers/tutorials/bridging/cross-dom-bridge-erc20.mdx
+30-10Lines changed: 30 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -139,6 +139,7 @@ Let's set those up now.
139
139
{<h3>Load your private key</h3>}
140
140
141
141
This step retrieves your private key from the environment variable you set earlier and converts it into an account object that Viem can use for transaction signing.
142
+
142
143
The private key is essential for authorizing transactions on both L1 and L2 networks.
143
144
For security reasons, we access it from an environment variable rather than hardcoding it.
144
145
@@ -156,14 +157,16 @@ Let's set those up now.
156
157
157
158
Each client is configured with the appropriate chain information and RPC endpoint.
158
159
This dual-network setup allows us to seamlessly interact with both layers using the same account.
160
+
Replace `<YOU_API_KEY>` with your API key from a RPC provider.
Now we'll call the `faucet` function on the L1 test token contract to receive free tokens for testing.
206
-
This transaction will mint new tokens directly to our wallet address. The function doesn't require any parameters - it simply credits a predetermined amount to whoever calls it.
209
+
This transaction will mint new tokens directly to our wallet address.
210
+
211
+
The function doesn't require any parameters - it simply credits a predetermined amount to whoever calls it.
207
212
We store the transaction hash for later reference and wait for the transaction to be confirmed.
@@ -212,8 +217,9 @@ The L1 testing token located at [`0x5589BB8228C07c4e15558875fAf2B859f678d129`](h
212
217
{<h3>Check your token balance</h3>}
213
218
214
219
After using the faucet, we verify our token balance by calling the `balanceOf` function on the L1 token contract.
220
+
215
221
This step confirms that we've successfully received tokens before proceeding with the bridging process.
216
-
The balance is returned in the smallest unit (wei), but we format it into a more readable form using the `formatEther` function since this token uses 18 decimal places.
222
+
The balance is returned in the smallest unit (wei), but we format it into a more readable form using the `formatEther`utility function from `viem`, since this token uses 18 decimal places.
@@ -227,7 +233,10 @@ You'll then receive the same number of tokens on L2 in return.
227
233
<Steps>
228
234
{<h3>Define the amount to deposit</h3>}
229
235
230
-
We define a variable `oneToken` that represents 1 full token in its base units (wei). ERC-20 tokens typically use 18 decimal places, so 1 token equals 10^18 wei. This constant helps us work with precise token amounts in our transactions, avoiding rounding errors and ensuring exact value transfers.
236
+
We define a variable `oneToken` that represents 1 full token in its base units (wei).
237
+
ERC-20 tokens typically use 18 decimal places, so 1 token equals 10^18 wei.
238
+
239
+
This constant helps us work with precise token amounts in our transactions, avoiding rounding errors and ensuring exact value transfers.
231
240
We'll use this value for both deposits and withdrawals
After submitting the approval transaction, we need to wait for it to be confirmed on the L1 blockchain.
249
-
We use the `waitForTransactionReceipt` function to monitor the transaction until it's included in a block. The receipt provides confirmation details, including which block includes our transaction.
258
+
After submitting the approval transaction, we need to wait for it to be confirmed on L1.
259
+
We use the `waitForTransactionReceipt` function to monitor the transaction until it's included in a block.
260
+
261
+
The receipt provides confirmation details, including which block includes our transaction.
250
262
This step ensures our approval is finalized before attempting to bridge tokens.
Now we can execute the actual bridging operation using the `depositERC20` function from the `@eth-optimism/viem` package. This function handles all the complex interactions with the L1StandardBridge contract for us. We provide:
269
+
Now we can execute the actual bridging operation using the `depositERC20` function from the `@eth-optimism/viem` package.
270
+
271
+
This function handles all the complex interactions with the `L1StandardBridge` contract for us.
272
+
We provide:
258
273
259
274
* The addresses of both the L1 and L2 tokens
260
275
* The amount to bridge
@@ -280,6 +295,7 @@ You'll then receive the same number of tokens on L2 in return.
280
295
281
296
After initiating the deposit, we need to wait for the L1 transaction to be confirmed.
282
297
This function tracks the transaction until it's included in an L1 block.
298
+
283
299
Note that while this confirms the deposit was accepted on L1, there will still be a short delay (typically a few minutes) before the tokens appear on L2, as the transaction needs to be processed by the Optimism sequencer.
@@ -288,6 +304,7 @@ You'll then receive the same number of tokens on L2 in return.
288
304
{<h3>Check your token balance on L1</h3>}
289
305
290
306
After the deposit transaction is confirmed, we check our token balance on L1 again to verify that the tokens have been deducted.
307
+
291
308
This balance should be lower by the amount we bridged, as those tokens are now escrowed in the `L1StandardBridge` contract.
292
309
This step helps confirm that the first part of the bridging process completed successfully:
293
310
@@ -297,6 +314,7 @@ You'll then receive the same number of tokens on L2 in return.
297
314
{<h3>Check your token balance on L2</h3>}
298
315
299
316
After allowing some time for the L2 transaction to be processed, we check our token balance on L2 to verify that we've received the bridged tokens.
317
+
300
318
The newly minted L2 tokens should appear in our wallet at the same address we used on L1.
301
319
This step confirms the complete success of the bridge operation from L1 to L2.
302
320
@@ -331,6 +349,7 @@ Now you're going to repeat the process in reverse to bridge some tokens from L2
331
349
332
350
Similar to deposits, we wait for the withdrawal transaction to be confirmed on L2.
333
351
This receipt provides confirmation that the withdrawal has been initiated.
352
+
334
353
The transaction logs contain critical information that will be used later in the withdrawal verification process.
335
354
This is only the first step in the withdrawal - the tokens are now locked on L2, but not yet available on L1.
336
355
@@ -346,6 +365,7 @@ Now you're going to repeat the process in reverse to bridge some tokens from L2
346
365
347
366
After the withdrawal transaction is confirmed, we check our token balance on L2 again to verify that the tokens have been deducted.
348
367
Our L2 balance should now be lower by the amount we initiated for withdrawal.
368
+
349
369
At this point, the withdrawal process has begun, but the tokens are not yet available on L1 - they will become accessible after the 7-day challenge period and after completing the "prove" and "finalize" withdrawal steps.
0 commit comments