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):
mkdir evm_swap
cd evm_swap
Initialize a new NPM project:
npm init -y
Step 2: Install Hardhat and Set Up the Project
Install Hardhat:
npm install --save-dev hardhat
Set up the Hardhat project: Run the setup command and choose to create a TypeScript project:
npx hardhat init
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.js
const hre = require("hardhat");
async function main() {
const [deployer] = await hre.ethers.getSigners();
console.log(
"Deploying contracts with the account:",
deployer.address
);
// getBalance is a method on the provider, not the signer
console.log("Account balance:", (await deployer.provider.getBalance(deployer.address)).toString());
const SimpleSwap = await hre.ethers.getContractFactory("Swap");
const SimpleSwapContract = await SimpleSwap.deploy([deployer.address]);
await SimpleSwapContract.waitForDeployment();
console.log("SimpleSwap contract deployed to:", await SimpleSwapContract.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.
npx hardhat run scripts/deploy.js --network SOURCE_NETWORK
Save this SOURCE_CONTRACT_ADDRESS to be used while initiating swap.
Also, Use this SOURCE_CONTRACT_ADDRESS (without 0x) to register on X-Talk Node.
Deployment Bash - Destination
Similarly, deploy the same smart contract on the Destination Chain.
npx hardhat run scripts/deploy.js --network DESTINATION_NETWORK
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
cargo l1x create xtalk_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.
cargo l1x build
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-beta wallet create
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