Build your first X-Talk Swap Contract

Step by Step guide and Repository link provided in Templates Section.

Building Blocks for X-Talk Swap

  1. 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.

  2. 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

  1. Create a New Directory (if you're starting fresh):

    mkdir evm_swap
    cd evm_swap
  2. Initialize a new NPM project:

    npm init -y

Step 2: Install Hardhat and Set Up the Project

  1. Install Hardhat:

    npm install --save-dev hardhat
  2. 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

  1. Install TypeScript-related dependencies:

  2. Install OpenZeppelin Contracts:

  3. Install Ethers and Hardhat Ethers (ensure compatibility):

Step 4: Configure TypeScript

Update tsconfig.json file in your project root with the following content:

Step 5: Write your smart contract

Create Swap.sol inside the contracts directory and paste your contract code there.

Step 6: Compile and Deploy Your Contracts

  1. Compile your project:

  1. Write deployment scripts or tests as needed, using the setup you've created. Example Provided below.

  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.

  1. Deployment Bash - Source

Deploy the Smart Contract on Source Chain

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.

  1. Deployment Bash - Destination

Similarly, deploy the same smart contract on the Destination Chain.

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

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.

  1. 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.

  2. Saving the event data that will store the event that is received.

  3. Payload Logic that will allow you to implement logic based on what the destination payload required.

  4. Payload Transformation that will be based on the destination runtime.

  5. Emitting the Event to be sent to the Destination.

X-Talk Swap Contract Components

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.

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.

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.

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.

Response will be as below

Default Wallet

After Importing your wallet, set it to be the default interaction account.

-- Check Endpoints in Interface Essentials for TestNet Faucet

Deploy your X-Talk Swap Contract with l1x-cli-beta

Response expected:

To initiate your smart contract, use this command:

Response expected:

The instance address is your contract address which you can use to call your functions and interact.

Register Source Chain Contract and X-Talk Swap Contract with X-Talk Node

X-Talk Nodes allow for data listening and send to the X-Talk Swap contract.

To register your addresses to the source registry, you need to call this:

Request Example:

To validate that your data has been registered to the registry:

Response Example:

You have successfully deployed your first Bridgeless Cross-Chain Token Swapping Application.

Initiate Swap

  1. Add Script: Add below listed initiateSwap.js at scripts folder of Souce Chain. Update all relevant details related to Source and Destination network.

    1. Ensure that your SOURCE_WALLET has SOURCE_TOKEN in it.

    2. Ensure that your DESTINATION_CONTRACT_ADDRESS has DESTINATION_TOKEN.

  1. Run Script on Source Network

Output looks like this

  1. Check the Source Network Transaction log

Use the TransactionHash received in above command as Initiate swap tx to check transaction log in the source network.

  1. Check the Destination Network Transaction log

Use the Destination X-Talk Gateway Contract Address from the table to check the transaction on Destination Network and then check the Transaction log.

X-TALK GATEWAY CONTRACT ADDRESS

Below table contains a list of contract address that serve as an authenticated entry to interact with X-Talk from respective client chain network.

Network
Contract Address

sepolia

0xf650162aF059734523E4Be23Ec5bAB9a5b878f57

bscTestnet

0x77d046D7d733672D44eA2Df53a1663b6CF453432

Last updated