3 - X-Talk Flow Cross Chain Contract
Last updated
Last updated
This guide will walk you through the basics of a demo template that simplifies cross-chain swaps.
You can find the contract's code in the l1x-contracts
directory, shown below:
The purpose of this demo template is to make cross-chain swaps less complicated for you. We will explain how it works in a step-by-step manner. Let's dive in!
1.1. Create an empty folder for this demonstration:
1.2. Change your current directory to the newly created folder:
2.1. Make sure you can access the l1x-forge
tool from your command line. If you don't have l1x-forge
installed, follow the instructions in Step 2: Checking the L1X Tool before proceeding.
2.2. Use the l1x-forge
command to create a new project workspace. We'll name it l1x_demo03_cross_chain_swap
and use the template l1x-cross-chain-swap
:
2.3. After running the command, you'll receive a confirmation message indicating the successful creation of the "l1x_demo02_erc20_cross_call" contract:
2.4. Your workspace will now be populated with content from the template.
3.1. Introduction to Devbox Shell and Workflow Commands
If you're new to the l1x-sde template workspace, it's a good idea to familiarize yourself with the basics of the devbox shell and workflow commands. This will help you get started more smoothly. You can review the following links for an overview:
3.2. Default Commands to Begin With
To start using the workspace, there are some default commands you can use:
Activate the Shell: To access the devbox shell, simply enter the following command in your terminal:
List Default Commands: To see a list of the built-in default commands available in the devbox shell, you can use the following command:
Initialize the Workspace: To prepare your workspace for building and testing, run:
Now you have all the necessary tools and packages to work with the demo template workspace.
Now, let's guide you through the process of building the smart contracts.
4.1 Compiling the Contract for l1x-vm
To compile the l1x_evm_cross_contract
contract, simply use this command:
Running this command will compile the contract for you and save the compiled files in the ./l1x-artifacts
folder automatically.
After executing the command, you'll discover the following files in the ./l1x-artifacts
folder:
That's it! We've successfully completed the steps for building smart contracts.
In this step, we will walk you through the process of configuring local development l1x-chain network.
5.1 Choose the Chain Type: To begin, select the "L1X_CFG_CHAIN_TYPE": "local_devnet"
option. Next, open a new terminal and activate the devbox shell:
5.2 Start the Devnode Services: While in the devbox shell, run the following command:
5.3 Wait for Services to Initialize: After executing the previous command, please wait for approximately 160 seconds. During this time, the services will start up and become operational. You will also observe the L1X node beginning to produce blocks.
5.4 Verify DevNet Service Status: To ensure that everything is running smoothly, use the following command to check the service status:
5.5 Troubleshooting: Occasionally, we might encounter a problem where the l1x-node-client
server and the cassandra
DB service do not sync properly, as indicated by the state shown in the image below.
To fix this issue and get your setup back on track, follow these simple steps:
These commands will help reset and restart the necessary components. Your setup should work smoothly after completing these steps.
6.1: Deploying and Creating a Contract Instance for source_registry.o
and xtalk_swap_flow.o
on l1x-vm
To deploy and create a contract instance for source_registry.o
and xtalk_swap_flow.o
on l1x-vm
, you can use the following command:
6.2: Understanding Traces
When deploying and creating contract instances, it's important to note the following information from traces, will be helpful for debugging:
The payload used for deploying and the deployed address for the source_registry.o
contract.
The payload used for creating an instance and the instance address for the source_registry.o
contract.
The payload used for deploying and the deployed address for the xtalk_swap_flow.o
contract.
The payload used for creating an instance and the instance address for the xtalk_swap_flow.o
contract.
6.3: Contract Address Registry
The addresses for deployed contracts and their instances are automatically updated in the configuration file l1x-conf/config-contract-address-registry.yaml. This registry keeps track of contract addresses.
6.4: Updating JSON Payload with New Instance Addresses
Update the following JSON payload with new instance addresses, before proceeding:
7.1: Updating the source_registry.o
Contract:
Update the configuration file for the payload.
Run the following command to execute the transaction:
Check the trace log for the following information, which indicates that the transaction has been successfully executed:
Run the following command to confirm the updates to the source registry transaction status:
To start L1X L2 monitoring services, follow each steps in a new devbox shell terminal:
8.1: Start the Event Listener "Leader" Service:
8.2: Start the Event Listener "Regular" Service:
8.3: Understanding Traces:
The trace prints show that the Leader node has discovered peers and begun fetching new blocks from the source Ethereum chain.
8.4: Launch the Signer Node Service:
The trace prints show that the Signer node has started fetching new blocks from the L1X L1 layer.
To start the ERC20 Token Swap, follow these steps:
9.1: Get Some Test Tokens for Your Wallet
Request some test tokens for your Metamask wallet from the L1X team.
9.2: Start the Token Swap on the Source Liquidity Pool Contract
Here's how you can do it:
Go to the Goerli Testnet Explorer and search for the LP contract at this address: 0xDa4140B906044aCFb1aF3b34C94A2803D90e96aA
Make sure you are on the correct page before proceeding with the InitiateSwap
transaction.
Connect your web3 wallet (we recommend using MetaMask) and select the account that has the test tokens from the L1X team.
Execute the InitiateSwap
transaction. In this demonstration, use the following arguments when making the InitiateSwap
transaction call:
Wait for the InitiateSwap
transaction to complete.
9.3: Confirm the Swap
To confirm the swap, follow these steps:
Find the transaction hash for the swap executed on the Destination Liquidity Pool deployed in Optimism Goerli Testnet. You can locate this in the transaction receipt, which is available in the trace prints of the Signer Node monitoring service.
Visit Goerli Optimism Testnet Explorer and search for this transaction hash to confirm that your swap was successful.
Open the contract at the address 0x44436a43330122a61a4877e51ba54084d5bd0ac6
and verify that the tokens have been successfully transferred to the destination address.
Congratulations! You've successfully completed the ERC20 Token Swap process. We hope this guide has been helpful in getting you through it smoothly. If you have any questions or run into any issues, feel free to reach out to our L1X team for assistance. Happy swapping!