Transfer NFTs Between Ethereum Layer 1 and Layer 2 Blockchains

ยท

Introduction

This tutorial provides a step-by-step guide on using the RedSonic API to deposit, transfer, and withdraw NFTs between Ethereum Layer 1 (L1) and Layer 2 (L2) blockchains using TypeScript.

๐Ÿ‘‰ Get the complete GitHub example here for the NFT transfer implementation.

Prerequisites

  1. Clone the ERC721 Transfer Tutorial Example.
  2. Install dependencies using:

    npm install
  3. Start the project:

    npm run dev

Key Components

Testing Parameters

For testing purposes, use the REDDIO721 NFT contract address on Sepolia testnet:

0x941661bd1134dc7cc3c107bf006b8631f6e65ad5

Step 1: Connect to Wallet

To interact with L1/L2, connect your MetaMask wallet and generate a Stark keypair:

async function connectToWallet() {
  if (typeof window !== 'undefined') {
    const provider = new ethers.providers.Web3Provider(window.ethereum);
    await provider.send("wallet_switchEthereumChain", [
      { chainId: ethers.utils.hexValue(11155111) } // Sepolia testnet
    ]);
    const signer = provider.getSigner();
    const ethAddress = await signer.getAddress();
    const reddio = new Reddio({ provider, env: 'test' });

    const { privateKey, publicKey } = await reddio.keypair.generateFromEthSignature();
    setEventValue({ ...eventValue, ethAddress, starkKey: publicKey, privateKey });
  }
}

Note: The same MetaMask address always generates the identical Stark keypair.


Step 2: Deposit NFTs from L1 to L2

  1. Approve NFT Transfer:

    const transaction = await reddio.erc721.approve({
      tokenAddress: contractAddress,
      tokenId,
    });
    await transaction?.wait();
  2. Deposit to L2:

    await reddio.apis.depositERC721({
      starkKey,
      tokenAddress: contractAddress,
      tokenId,
    });

    ๐Ÿ‘‰ Check your L2 balance here after deposit.


Step 3: Transfer NFTs on L2

Transfer NFTs between L2 accounts:

const result = await reddio.apis.transfer({
  starkKey,
  privateKey,
  contractAddress,
  tokenId,
  type: "ERC721",
  receiver,
});
console.log(result); // Success response

Step 4: Withdraw NFTs from L2 to L1

Phase 1: Initiate Withdrawal (4-hour processing time)

const { data } = await reddio.apis.withdrawalFromL2({
  starkKey,
  privateKey,
  receiver,
  type: "ERC721",
  contractAddress,
  tokenId,
});

Phase 2: Finalize on L1

Refer to the withdrawal guide.


FAQ

Q1: How long does an L2 transfer take?

A: Transfers are near-instantaneous on Layer 2.

Q2: Why does withdrawal take 4 hours?

A: This is a security measure to prevent fraud.

Q3: Can I use any NFT contract?

A: Yes, but test with REDDIO721 first for compatibility.

Q4: How do I check my L2 balance?

A: Use the balance API.


Conclusion

This guide covers end-to-end NFT transfers between Ethereum L1 and L2. For advanced use cases, explore the Reddio documentation.

๐Ÿ‘‰ Start building with RedSonic today.