Bridging ERC-20 tokens 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 addresses of the ERC-20 token on L1 and L2.
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.
erc20ABI
erc20ABI
A fragment of the ABI with the functions we need to call directly.
This is balanceOf
from the ERC-20 standard, used to get the balance of an address.
This is faucet
, a function supported by the L1 contract, which gives the caller a thousand tokens. Technically speaking we should have two ABIs, because the L2 contract does not have faucet
, but that would be a needless complication in this case when we can just avoid trying to call it.
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.
This tutorial teaches you how to use the Specular SDK to transfer ERC-20 tokens between Layer 1 (Sepolia) and Layer 2 (Specular Testnet).
Warning: The standard bridge does not support certain ERC-20 configurations:
Create the ServiceBridge
object that we use to transfer assets.
The ERC20 contracts, one per layer.
depositERC20
depositERC20
This function shows how to deposit ETH from Ethereum to Specular.
ERC20
tokens are divided into $10^18$ basic units, same as ETH divided into wei.
To show that the deposit actually happened we show before and after balances.
To enable the bridge to transfer ERC-20 tokens, it needs to get an allowance first. The reason to use the SDK here is that it looks up the bridge address for us. While most ERC-20 tokens go through the standard bridge, a few require custom business logic that has to be written into the bridge itself. In those cases there is a custom bridge contract that needs to get the allowance.
Wait until the allowance transaction is processed and then report the time it took and the hash.
serviceBridge.depositERC20()
creates and sends the deposit trasaction on L1.
Of course, it takes time for the transaction to actually be processed on L1.
After the transaction is processed on L1 it needs to be picked up by an offchain service and relayed to L2. To show that the deposit actually happened we need to wait until the message is relayed. The getDepositStatus
function does this for us.
Once the messageStatus
is ready the transaction can be finalized and finally settled on L2.
withdrawERC20
withdrawERC20
This function shows how to withdraw ERC-20 from Specular to Sepolia. The withdrawal process has these stages:
Submit the withdrawal transaction on Specular Testnet
Wait until the state root with the withdrawal is published (and the status changes to
ready
).Finalize to cause the actual withdrawal on L1 using
serviceBridge.finalizeMessage()
.
We want users to see their balances, and how long the withdrawal is taking.
This is the initial withdrawal transaction Specular.
Finalize the withdrawal and get back the token to L1.
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 ERC-20 assets between layer 1 and layer 2.
Last updated