Skip to main content

Deposit ETH tokens

Learn how to deposit ETH tokens from Ethereum L1 to Myria L2 network.

Prerequisites

  • Have some tokens on Ethereum L1 (testnet or mainnet) in the Myria wallet

Implementation

info

You can find the full React implementation for ERC721 withdrawals in the Myria React Samples repository.

1. Set up a React project

Depositing ETH tokens requires client-side interaction with the MetaMask wallet. It's recommended to use React to implement such behavior.

You can create a React app via Create React App. Note, the below project relies on the Web3.js library and needs custom configuration.

2. Create a myria-client.ts helper

It's recommended to have a separate .ts file for quick access to the MyriaClient.

For more details, use the Myria Core SDK overview.

3. Deposit tokens

The depositETH() function has the following parameters:

  1. params object:
  • starkKey - the Stark Key of your wallet
  • amount - the amount of tokens to deposit
  1. sendOptions object:
  • from - public key of your wallet
  • confirmationType - confirmation type
import { ConfirmationType, DepositCommonParams, DepositModule, MyriaClient, SendOptions } from "myria-core-sdk";
import Web3 from 'web3';

function convertEthToWei(amount: string): string {
if (!amount || Number(amount) === 0) return '0';
return Web3.utils.toWei(amount.toString()).toString();
}

export async function depositETH(client: MyriaClient, starkKey: string, account: string, amount: string) {
const depositModule: DepositModule = new DepositModule(client);

const params: DepositCommonParams = {
starkKey: starkKey,
tokenType: TokenType.ETH,
amount: amount
};

const sendOnchainOptions: SendOptions = {
confirmationType: ConfirmationType.Confirmed,
from: account, // Metamask wallet address
value: String(convertEthToWei(amount.toString())),
};

const result = await depositModule.depositETH(params, sendOptions);
console.log(JSON.stringify(result, null, 2));
}