-
Notifications
You must be signed in to change notification settings - Fork 263
Closed
Labels
community-requestdocs updates requested by community or OP collectivedocs updates requested by community or OP collectivedocumentationImprovements or additions to documentationImprovements or additions to documentationtutorialnew tutorial request or revision to existing tutorialnew tutorial request or revision to existing tutorial
Description
Tutorial title
Bridging ETH to OP stack With the Viem SDK
Tutorial description
This tutorial explains how you can use the Viem to bridge ETH from L1 (Ethereum or Sepolia) to L2 (OP Mainnet or OP Sepolia). The Optimism SDK is an easy way to add bridging functionality to your JavaScript-based application. It also provides some safety rails to prevent common mistakes that could cause ETH or ERC-20 tokens to be made inaccessible.
This is just the code snippet
Tutorial tags
bridging
Skill level
Beginner
Hosted on Optimism.io or hosted elsewhere?
Hosted on optimism.io
For tutorials to be hosted on Optimism.io: Tutorial Content
-
mkdir op-sample-project
-
cd op-sample-project
-
pnpm init
-
pnpm i viem
-
node
// STEP 1
const { createPublicClient, http, createWalletClient, parseEther } =
await import('viem');
const {sepolia, optimismSepolia } = await import("viem/chains"); /* baseSepolia, liskSepolia, zoraSepolia, modeTestnet etc..... */
const { privateKeyToAccount } = await import('viem/accounts');
const { getL2TransactionHashes, publicActionsL2, walletActionsL1 } =
await import('viem/op-stack');
// STEP 2
const account = privateKeyToAccount(
'0x....'
);
// STEP 3
const publicClientL1 = createPublicClient({
chain: sepolia, /* or mainnet */
transport: http(),
});
// STEP 4
const walletClientL1 = createWalletClient({
account,
chain: sepolia, /* or mainnet */
transport: http(),
}).extend(walletActionsL1());
// STEP 5
const publicClientL2 = createPublicClient({
chain: optimismSepolia, /* or baseSepolia, liskSepolia, zoraSepolia, modeTestnet etc..... */
transport: http(
'https://opt-sepolia.g.alchemy.com/v2/keyyyyy'
),
}).extend(publicActionsL2());
// Build parameters for the transaction on the L2.
const args = await publicClientL2.buildDepositTransaction({
mint: parseEther('0.5'),
to: account.address,
});
// Execute the deposit transaction on the L1.
const hash = await walletClientL1.depositTransaction(args);
// Wait for the L1 transaction to be processed.
const receipt = await publicClientL1.waitForTransactionReceipt({ hash });
// Get the L2 transaction hash from the L1 transaction receipt.
const [l2Hash] = getL2TransactionHashes(receipt);
// Wait for the L2 transaction to be processed.
const l2Receipt = await publicClientL2.waitForTransactionReceipt({
hash: l2Hash,
});
For tutorials hosted elsewhere: URL to tutorial
No response
Additional context
This is just a snippet, the wordings will need to be modified to suit the doc spec
Metadata
Metadata
Assignees
Labels
community-requestdocs updates requested by community or OP collectivedocs updates requested by community or OP collectivedocumentationImprovements or additions to documentationImprovements or additions to documentationtutorialnew tutorial request or revision to existing tutorialnew tutorial request or revision to existing tutorial