Bridging ETH with the Specular SDK
This tutorial teaches you how to use the Specular SDK to transfer ETH between Layer 1 (Sepolia) and Layer 2 (Specular Testnet).
Setup
Clone this repository and enter it.
Install the necessary packages.
Copy
.env.example
to.env
and edit it:Set
PRIVATE_KEY
to point to an account that has ETH on Sepolia.Set
SEPOLIA_RPC_ENDPOINT
to your Sepolia RPC url or leave it to the default that we provide.Set
SPECULAR_RPC_ENDPOINT
to your Specular RPC url or leave it to the default that we provide.
If you don't have ETH on your wallet fetch some from a Sepolia Faucet and bridge some of those native ETH to Specular;
Run the sample code
The sample code is in index.js
, execute it. After you execute it, wait. It is not unusual for each operation to take minutes on Sepolia. On the production network the withdrawals take around a week each, because of the challenge period.
How does it work?
The libraries we need: ethers
, dotenv
and the Specular SDK.
Configuration, read from .env
.
The configuration parameters required for transfers.
getSigners
getSigners
This function returns the two signers (one for each layer).
Finally, create and return the wallets. We need to use wallets, rather than providers, because we need to sign transactions.
setup
setup
This function sets up the parameters we need for transfers.
Get the signers we need, and our address.
Create the serviceBridge object that we use to transfer assets.
Variables that make it easier to convert between WEI and ETH
Both ETH and DAI are denominated in units that are 10^18 of their basic unit. These variables simplify the conversion.
reportBalances
reportBalances
This function reports the ETH balances of the address on both layers.
depositETH
depositETH
This function shows how to deposit ETH from Ethereum to Specular.
To show that the deposit actually happened we show before and after balances.
serviceBridge.depositETH()
creates and sends the deposit trasaction on L1.
Of course, it takes time for the transaction to actually be processed on L1. We are waiting for the receipt.
After the transaction is processed on L1 it needs to be picked up by an offchain service and relayed to L2 by the L1Oracle contract.
To show that the deposit actually happened we need to wait until the message is relayed. The waitForMessageStatus
function does this for us.
Once the message is relayed by the L1Oracle contract the messageStatus will be different from 0 implying that it will be ready to be finalized.
serviceBridge.finalizeDeposit(transactionReceipt)
finalizes the deposit settling the funds on L2.
withdrawETH
withdrawETH
For deposits it was enough to transfer 1 gwei to show that the L2 balance increases.
However, in the case of withdrawals the withdrawing account needs to pay on L1 for finalizing the message, which costs more than that.
By sending 0.01 ETH it is guaranteed that the withdrawal will actually increase the L1 ETH balance instead of decreasing it.
This is the initial withdrawal transaction on Specular.
Finalize the withdrawal and actually get back the 0.01 ETH.
main
main
A main
to run the setup followed by both operations.
Conclusion
You should now be able to write applications that use our SDK and bridge to transfer ETH between layer 1 and layer 2.
Last updated