Skip to main content

Deposit ERC20 (Myria) Tokens

Learn how to deposit ERC20 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 Deposit ERC20 tokens in the Myria React Samples repository.

1. Set up a React project

Depositing ERC20 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 depositErc20() function has the following parameters:

  1. params object:
  • starkKey - the Stark Key of your wallet
  • contractAddress - contract address of the token to deposit
  • amount - the amount of tokens to deposit
  • ethAddress - the public key of your wallet
  1. sendOptions object:
  • from - public key of your wallet
  • confirmationType - confirmation type
import { ConfirmationType, DepositERC20Params, 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 depositErc20(client: MyriaClient, starkKey: string, account: string, contractAddress: string, amount: string) {
const depositModule: DepositModule = new DepositModule(client);

const params: DepositERC20Params = {
starkKey: starkKey,
contractAddress: contractAddress,
amount: String(convertEthToWei(amount.toString())),
ethAddress: account
}

const sendOptions: SendOptions = {
from: account,
confirmationType: ConfirmationType.Confirmed,
}

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