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
This tutorial explains how you can use the [Viem](https://viem.sh/op-stack/) to bridge ETH from L1 (Ethereum or Sepolia) to L2 (OP Mainnet or OP Sepolia).
12
-
The `viem/op-stack` library is an easy way to add bridging functionality to your JavaScript-based application.
13
-
It also provides some safety rails to prevent common mistakes that could cause ETH or ERC-20 tokens to be made inaccessible.
12
+
This tutorial explains how you can use [Viem](https://viem.sh) to bridge ETH from L1 (Ethereum or Sepolia) to L2 (OP Mainnet or OP Sepolia).
13
+
Viem is a TypeScript interface for Ethereum that provides low-level stateless primitives for interacting with Ethereum.
14
+
It offers an easy way to add bridging functionality to your JavaScript-based application.
14
15
16
+
Behind the scenes, Viem uses the [Standard Bridge](/builders/app-developers/bridging/standard-bridge) contracts to transfer ETH and ERC-20 tokens.
15
17
Make sure to check out the [Standard Bridge guide](/builders/app-developers/bridging/standard-bridge) if you want to learn more about how the bridge works under the hood.
16
18
17
-
## Prerequisites
19
+
## Supported Networks
18
20
19
-
Before you begin, ensure you have the following:
21
+
Viem supports any of the [Superchain networks](/chain/networks).
22
+
The OP Stack networks are included in Viem by default.
23
+
If you want to use a network that isn't included by default, you can add it to Viem's chain configurations.
20
24
21
-
* Familiarity with bridging concepts between L1 and L2.
22
-
* An understanding of the [viem library](https://viem.sh/op-stack/)
23
-
*[Node.js](https://nodejs.org/en/) (version 14 or later)
24
-
*[pnpm](https://pnpm.io/installation) installed for managing packages.
25
+
## Dependencies
25
26
26
-
Install `pnpm` if you haven't already:
27
+
*[node](https://nodejs.org/en/)
28
+
*[pnpm](https://pnpm.io/installation)
27
29
28
-
```bash
29
-
npm install -g pnpm
30
+
## Create a Demo Project
30
31
31
-
```
32
+
You're going to use Viem for this tutorial.
33
+
Since Viem is a [Node.js](https://nodejs.org/en/) library, you'll need to create a Node.js project to use it.
32
34
33
35
<Steps>
34
-
{<h3>Project Setup</h3>}
36
+
{<h3>Make a Project Folder</h3>}
35
37
36
-
Let's create a new project and install the necessary dependencies.
38
+
```bash
39
+
mkdir op-sample-project
40
+
cd op-sample-project
41
+
```
37
42
38
-
* Create a folder and a file
43
+
{<h3>Initialize the Project</h3>}
39
44
40
-
```bash
41
-
mkdir op-sample-project
42
-
cd op-sample-project
43
-
touch bridge-eth.js
44
-
```
45
+
```bash
46
+
pnpm init
47
+
```
45
48
46
-
* Initialize the project:
49
+
{<h3>Install the Optimism SDK</h3>}
47
50
48
-
```bash
49
-
pnpm init
50
-
```
51
+
```bash
52
+
pnpm add @eth-optimism/sdk
53
+
```
51
54
52
-
*Install dependencies: Install `viem`, and `dotenv` for managing environment variables.
55
+
{<h3>Install ethers.js</h3>}
53
56
54
-
```bash
55
-
pnpm add viem dotenv
56
-
```
57
+
```bash
58
+
pnpm add ethers@^5
59
+
```
57
60
58
-
{<h3>Configure Environment Variables</h3>}
61
+
</Steps>
59
62
60
-
You need a private key in order to sign transactions.
61
-
Create a `.env` file in your parent directory to securely store your environment variables
62
-
Make sure this private key corresponds to an address that has ETH on Sepolia.
63
+
<Callouttype="info">
64
+
Want to create a new wallet for this tutorial?
65
+
If you have [`cast`](https://book.getfoundry.sh/getting-started/installation) installed you can run `cast wallet new` in your terminal to create a new wallet and get the private key.
63
66
64
-
```bash
65
-
touch .env
66
-
```
67
+
</Callout>
67
68
68
-
Add your Ethereum Layer 1 (L1) and Optimism Layer 2 (L2) RPC endpoints, and wallet private key in the `.env` file:
69
+
## Get ETH on Sepolia
69
70
70
-
```bash
71
-
L1_RPC_URL=https://rpc.ankr.com/eth_sepolia
72
-
L2_RPC_URL=https://sepolia.optimism.io
73
-
TUTORIAL_PRIVATE_KEY=0x...
74
-
```
75
-
</Steps>
71
+
This tutorial explains how to bridge ETH from Sepolia to OP Sepolia.
72
+
You will need to get some ETH on Sepolia to follow along.
76
73
77
-
<Callouttype="warning">
78
-
Never share your private key and avoid committing this file to version control.
74
+
<Callouttype="info">
75
+
You can use [this faucet](https://sepoliafaucet.com) to get ETH on Sepolia.
79
76
</Callout>
80
77
81
-
## Import Dependencies
78
+
## Add a Private Key to Your Environment
82
79
83
-
{<h3>Import the necessary packages</h3>}
80
+
You need a private key in order to sign transactions.
81
+
Set your private key as an environment variable with the export command.
82
+
Make sure this private key corresponds to an address that has ETH on Sepolia.
84
83
85
-
In the `bridge-eth.js` file, import the `viem` package at the top:
You'll need a few variables throughout this tutorial.
102
-
Let's set those up now.
89
+
You're going to use the Node REPL to interact with Viem.
90
+
To start the Node REPL run the following command in your terminal:
103
91
104
-
<Tabsitems={['Create an account', 'Create RPC providers', 'Wallet Clients']}>
105
-
<Tabs.Tab>
106
-
Create an account from the private key. [`privateKeyToAccount`](https://viem.sh/docs/accounts/local/privateKeyToAccount#privatekeytoaccount) is a viem method used to sign transactions by passing a private key.
107
-
108
-
Load your private key with `privateKeyToAccount`, after the imports above.
0 commit comments