Step by Step guide and Repository link provided in Templates Section.
Building Blocks for X-Talk Swap
Smart Contracts on Source Chain and Destination Chain (example ethereum, binance chain and solana) that will be the endpoints for swapping tokens. This is runtime agnostic.
L1X X-Talk Swap Contract that includes Source Chain Contract Registration Initialisation and X-Talk Swap Contract Deployment and Initialisation to facilitate token swapping.
Implementing EVM Smart Contract on Source/Destination Chain
Implement below evm-compatible smart contarct on source and destination chain. Once the contract is deployed, use the source chain contract address during registration on X-Talk node.
Step 1: Initialize a New Project
Create a New Directory (if you're starting fresh):
mkdirevm_swapcdevm_swap
Initialize a new NPM project:
npminit-y
Step 2: Install Hardhat and Set Up the Project
Install Hardhat:
npminstall--save-devhardhat
Set up the Hardhat project: Run the setup command and choose to create a TypeScript project:
npxhardhatinit
When prompted, select to create a TypeScript project. Follow the prompts to add a .gitignore and install the project's dependencies.
Step 3: Install Necessary Plugins and Dependencies
Write deployment scripts or tests as needed, using the setup you've created. Example Provided below.
//scripts/deploy.jsconsthre=require("hardhat");asyncfunctionmain() {const [deployer] =awaithre.ethers.getSigners();console.log("Deploying contracts with the account:",deployer.address );// getBalance is a method on the provider, not the signerconsole.log("Account balance:", (awaitdeployer.provider.getBalance(deployer.address)).toString());constSimpleSwap=awaithre.ethers.getContractFactory("Swap");constSimpleSwapContract=awaitSimpleSwap.deploy([deployer.address]);awaitSimpleSwapContract.waitForDeployment();console.log("SimpleSwap contract deployed to:",awaitSimpleSwapContract.getAddress()); }main().then(() =>process.exit(0)).catch((error) => {console.error(error);process.exit(1); });
Sample Hardhat Config JS File: Configure your Hardhat project by editing hardhat.config.js. Add your network in this file with relevant details. Ensure it looks like this.
Save this DESTINATION_CONTRACT_ADDRESS to be used while initiating swap.
Implementing L1X X-Talk Swap Contract
X-Talk Swap Contract is at the core of token swapping and liquidity management across different chains.
Create a new L1X project that provides the necessary files to get started
cargol1xcreatextalk_swap
Use the provided code below to implement the cross-chain swap logic. The X-Talk Swap Contract consists of fundamental building blocks which are re-usable across all development standards with X-Talk for token swapping.
Payload Definition that is being received. This could be deterministic or a generic vector of bytes. You can be deterministic or make this logic programmable.
Saving the event data that will store the event that is received.
Payload Logic that will allow you to implement logic based on what the destination payload required.
Payload Transformation that will be based on the destination runtime.
Swap Level Functions: manage the core processes of initiating and executing swap contracts between different blockchain networks. They ensure transparent and secure asset exchange by defining swap terms, obtaining mutual consent, facilitating asset transfer, and providing status updates throughout the transaction.
Registration Level Functions: deal with registration and management of participants in the X-Talk swap platform. By enabling registration, a trusted environment is established while allowing participants to maintain control over their engagement.
Oracle Service Level Functions: allow integrating external data sources into the swap contracts to ensure accuracy and reliability. They facilitate the retrieval, validation, and utilization of real-time data, such as exchange rates or asset prices, enhancing the integrity and efficiency of swap transactions.
The Fundamental Building Blocks of X-Talk Swap require the below building blocks in X-Talk Swap Contract.
Data Structures: Define messages and events used within the contract.
Event Handling: Parse, log, and handle blockchain events.
State Management: Load and save the contract's state to maintain event records.
Message Parsing: Convert blockchain event logs into usable data formats.
Event Emission and Logging: Emit structured logs for cross-chain messaging.
Security: Ensure that only valid and expected data is processed.
Ensure you have all the necessary packages, libraries and dependencies in your cargo.toml file as below.
[package]name ="swap-example"version ="0.1.0"edition ="2021"[lib]crate-type = ["cdylib"][dependencies]borsh = { version ="0.9", features = ["const-generics"] }l1x-sdk ="0.3.1"serde = { version ="1", features = ["derive"] }serde_json ="1"base64 ="*"ethers ="2.0.11"getrandom = { version ="0.2.10", features = ["js"] }hex ="0.4"log ="0.4.20"
To deploy the X-Talk Swap contract, you can use this command on your project level. After successfully building, you will find the "project_name.o" file in target/l1x/release folder. This is the object file which will be deployed.
cargol1xbuild
To deploy your contract, use an existing L1X Account with L1X for Mainnet and L1X TestNet for TestNet.
Create Wallet
Create your own wallet to generate a new keypair.
l1x-cli-betawalletcreate
Import Wallet
Import your existing wallet using the l1x-cli-beta tool, by providing a private key. If you don't have one, Create Wallet.
SOURCE_REGISTRY_INSTANCE_ADDRESS => This is provided
source_contract_address => Without 0x, This is the contract address you want to listen to
TOPIC_OF_YOUR_EVENT => Topic of the event
YOUR_FLOW_CONTRACT_ADDRESS => The initiated X-TalK Swap Contract Address