.env
file to store the secrets.npm init
. Answer the questions in the terminal to create a sample package.json
file for your project.
.env
file to store the secrets.env
file in the root directory of your project and paste and fill in the following:
index.js
file in your project’s root directory.
The first step is to import the required packages and create the global variables. To do that, at the top of the file, paste the following:
getSigners()
function uses the private key to create wallet instances with the Ethers library. These instances will be used to query balances and sign transactions.CrossChainMessenger
instance using the Optimism JavaScript SDK:
chainIds()
function uses the Ethers library to query the Chainstack endpoints and retrieve the chain IDs that will then be used to create the crossChainMessenger
instance.
The initialize()
function uses the getSigners()
and chainIds()
functions to retrieve the parameters required to create the crossChainMessenger
using the Optimism JavaScript SDK. The crossChainMessenger instance allows us to interact with the L1 and L2 networks.
console.log
statements with the -
are there only to make the response in the console more legible.const wei
holds the amount that will be transferred, expressed in the Wei unit.
The default for this script is set to 0.1 ether, equivalent to 100,000,000,000,000,000 Wei. You can use a Wei converter to include other amounts, or you can use the following:
bridgeEth()
function transfers the amount of ether specified in the wei
constant from Ethereum (L1) to Optimism (L2). It uses the depositEth method of the crossChainMessenger
instance.
It then waits for the MessageStatus
to become RELAYED
. The RELAYED
status indicates that a message has been successfully transmitted from one network to another and is under processing on the recipient network.
bridgeEth()
function gives some updates and retrieves the balances again.
At the bottom of the file, add the main
function and call it:
wei
constant.
To start the script, run the following command: