Skip to content

[TUTORIAL] Alternate snippets for the Bridging ETH to OP Mainnet With the Optimism SDK Tutorial using Viem #904

@brokewhale

Description

@brokewhale

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

  1. mkdir op-sample-project

  2. cd op-sample-project

  3. pnpm init

  4. pnpm i viem

  5. 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

No one assigned

    Labels

    community-requestdocs updates requested by community or OP collectivedocumentationImprovements or additions to documentationtutorialnew tutorial request or revision to existing tutorial

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions