Git Product home page Git Product logo

l1-l2-deposit-withdrawal's Introduction

L1/L2 ERC20 Deposit + Withdrawal Example

Introduction

In this example repository we will walk through how to add L1 <> L2 message passing in your application.

Message passing is automatically done for all Optimistic Ethereum transactions, but retrieving these messages is something that you must implement yourself in your application. Message passing is used to pass data from L1 to L2 or from L2 to L1.

Prerequisite Software

L1 <> L2 Communication: Brief Summary

For example, you would pass data from L1 to L2 when initiating a process on L1, and finalizing it on L2, such as an L1 deposit and L2 withdrawal. The L1CrossDomainMessenger will pass the L1 data to L2 by calling sendMessage. Then, the L2CrossDomainMessenger calls relayMessage to relay the L1 data back to the receiving user.

Similarly, for an L2 to L1 deposit-withdrawal, message passing would start at the L2CrossDomainMessenger calling sendMessage and end with the message being relayed by the L1CrossDomainMessenger to L1.

For further information, you can review our documentation on L1 <> L2 Communication on our community hub.

Message Passing in this Example

In this repository, on line 97, we wait for the message to relayed by the L2CrossDomainMessenger and use the @eth-optimism/watcher to retrieve the hash of message of the previous transaction, a deposit of an ERC20 on L1.

Likewise, on line 115, we wait for a second message to be relayed, but this time by the L1CrossDomainMessenger so that we can retrieve the message of tx3, a withdraw of an ERC20 on L2.

Running the Example

Run the following commands to get started:

yarn install
yarn compile

Make sure you have the local L1/L2 system running (open a second terminal for this):

git clone [email protected]:ethereum-optimism/optimism.git
cd optimism
yarn
yarn build
cd ops
docker-compose build
docker-compose up

Now run the example file:

node ./example.js

If everything goes well, you should see the following:

Deploying L1 ERC20...
Deploying L2 ERC20...
Deploying L1 ERC20 Gateway...
Initializing L2 ERC20...
Balance on L1: 1234
Balance on L2: 0
Approving tokens for ERC20 gateway...
Depositing tokens into L2 ERC20...
Waiting for deposit to be relayed to L2...
Balance on L1: 0
Balance on L2: 1234
Withdrawing tokens back to L1 ERC20...
Waiting for withdrawal to be relayed to L1...
Balance on L1: 1234
Balance on L2: 0

l1-l2-deposit-withdrawal's People

Contributors

k-ho avatar smartcontracts avatar ben-chain avatar platocrat avatar zzooppii avatar modagi avatar juanfranblanco avatar tynes avatar miguelmota avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.