Build your first X-Talk Swap Solana to EVM Contract and X-Talk Swap EVM to Solana Contract

Step by Step guide with detailed process. In this guide we will use Solana devnet, Avalanche mainnet and L1X TestNet to deploy X-Talk Flow Contract.

Building Blocks for X-Talk Swap: Solana-EVM and EVM-Solana

  1. Smart Contracts on Solana that will be the endpoints for swapping tokens. This is runtime agnostic.

  2. L1X X-Talk Solana-EVM Flow Contract Deployment and Initialisation to facilitate token swapping

  3. L1X X-Talk EVM-Solana Flow Contract Deployment and Initialisation to facilitate token swapping

  4. Smart Contracts on EVM-compatible chain (Avalanche in this example) that will be the endpoints for swapping tokens. This is runtime agnostic.

X-Talk Process

This page is divided into 4 parts, for the ease of understanding, as listed below

  1. Basic Steps: Common for both Solana-EVM and EVM-Solana

  2. Solana to EVM Token Swapping

  3. EVM to Solana Token Swapping

  4. X-Talk Gateway Contract Address

I. Basic Steps

This section is categorised into Pre-Requisites, Set up Solana Project and Set up Avalanche Project.

Step 1: Pre-Requisites

Ensure Pre-Requisites for Solana and L1X are installed.

Step 2: Set up Solana Project

Open a new terminal to create Solana project

Step 1: Initialize a New Project

  1. Create a New Directory

  1. Initialize a New Anchor Project

Step 2: Write your Solana smart contract

In this, below listed smart contracts are to be created at /programs/cross_chain_swap/src/

  1. lib.rs

  2. swap_accounts.rs

  3. instructions.rs

  4. events.rs

  5. errors.rs

  6. utils.rs

  1. Paste your contract code at /programs/cross_chain_swap/src/lib.rs

Update declare_id! with YOUR_SOLANA_PROGRAM_ID

  1. Paste your contract code at /programs/cross_chain_swap/src/swap_accounts.rs

  1. Paste your contract code at /programs/cross_chain_swap/src/instructions.rs

  1. Paste your contract code at /programs/cross_chain_swap/src/events.rs

  1. Paste your contract code at /programs/cross_chain_swap/src/errors.rs

  1. Paste your contract code at /programs/cross_chain_swap/src/utils.rs

Step 3: Update Cargo.toml

Ensure that you update at programs/cross_chain_swap/Cargo.toml

Step 4: Install Dependencies

Step 5: Compile the Smart Contract

Step 6: Display the list of Key Pairs

Output is YOUR_SOLANA_PROGRAM_ID. Save it as it is used later at various instances.

Step 7: Declare PROGRAM_ID

Goto program/cross_chain_swap/src/lib.rs and update declare_id! with YOUR_SOLANA_PROGRAM_ID.

Below is the snapshot for your reference

Step 8: Configure Scripts

Create scripts folder and add below scripts to it.

  1. Script to Initiate Contract

  1. Configure Anchor.toml

  • Update swap with YOUR_SOLANA_PROGRAM_ID

  • Update wallet with YOUR_KEYPAIR_PATH

  • Set path for initialize.ts script

Step 9: Compile, Deploy and Run Solana Program

  1. Compile the Solana Program

  1. Deploy the Solana Program

  1. Run the Solana Program

Output contains YOUR_SOLANA_STATE_ACCOUNT, YOUR_SOLANA_PROGRAM_TOKEN_ACCOUNT and YOUR_SOLANA_TRANSACTION_HASH

Save it as it is used later.

Step 3: Set up Avalanche Project

You can use Hardhat to compile, deploy and interact with Avalanche contracts. Note, this example is on Avalanche Mainnet.

Step 1: Initialize a New Project

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

  2. Initialize a new NPM project:

Step 2: Install Hardhat and Set Up the Project

  1. Install Hardhat:

  2. Set up the Hardhat project: Run the setup command and choose to create a JavaScript project:

    When prompted, select to create a JavaScript project. Follow the prompts to add a .gitignore and install the project's dependencies.

Step 3: Install Necessary Plugins and Dependencies

  1. Install the Hardhat Toolbox plugin

Step 4: Write your EVM Smart Contract

Create your smart contract file as contracts/SwapV2Contract.sol

Step 5: Scripts and Configuration Settings

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

  1. Sample Hardhat Configuration file

Step 6: Contract Deployment

Output you get YOUR_AVAX_CONTRACT_ADDRESS. Save it for now.

II. Solana to EVM Swap

This section is dedicated to swap a token from Solana to any EVM-compatible chain, Avalanche in this example.

Step 1: Implementing L1X X-Talk Solana-EVM Flow Contract

Flow Contracts are at the core of cross-chain application development. They enable logic programmability, reducing integration and maintenance overhead and enable scalability.

Create a new L1X project that provides the necessary files to get started

Use the provided code below to implement the cross-chain swap Solana to EVM logic.

Your L1X Solana-EVM Flow consist of below listed smart contracts that are to be created at /programs/swap_from_solana/src/

  1. lib.rs

  2. solana.rs

  3. types.rs

Create src/lib.rs

Create src/solana.rs

Create src/types.rs

The Fundamental Building Blocks of X-Talk require the below building blocks in X-Talk 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 flow 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 on TestNet.

Create Wallet

Create your own wallet to generate a new keypair.

Import Wallet

Import you existing wallet using the l1x-cli-beta tool, by providing a private key. If you don't have one, Create Wallet.

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

Step 2: Register Solana Contract and X-Talk Solana-EVM Flow Contract with X-Talk Node

X-Talk Nodes allow for data listening and send to the X-Talk Solana-EVM Flow contract.

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

Request Example:

Step 3: Native Token Swap Solana - EVM

Step A: Implementing Script to Initiate Native Token Swap from Solana to Avalanche

Step 1: Add Script to Initiate Swap

In your solana project, inside scripts folder add init_swap.ts script.

  • Update walletKeypair with YOUR_KEYPAIR_PATH

  • Update other relevant network details in the script.

Step 2: Update script path in Anchor.toml

Add the path for init_swap.ts script in Anchor.toml

Step B: Initiate Swap from Solana to Avalanche

To verify that sawp is triggered at YOUR_AVAX_CONTRACT_ADDRESS, goto Avalanche explorer, check for the transaction with X-Talk Gateway for Avalanche mentioned in the table.

Step 4: ERC20 Token Swap Solana - EVM

Step A: Implementing Script to Initiate ERC20 Token Swap from Solana to Avalanche

Step 1: Add Script to Initiate ERC20 Swap

In your solana project, inside scripts folder add init_swap_erc20.ts script.

  • Update walletKeypair with YOUR_KEYPAIR_PATH

  • Update other relevant network details in the script.

Step 2: Update script path in Anchor.toml

Add the path for init_swap_erc20.ts script in Anchor.toml

Step B: Initiate ERC20 Swap from Solana to Avalanche

To verify that sawp is triggered at YOUR_AVAX_CONTRACT_ADDRESS, goto Avalanche explorer, check for the transaction with X-Talk Gateway for Avalanche mentioned in the table.

III. EVM to Solana Swap

This section is dedicated to send a token from any EVM-compatible chain (Avalanche in this example) to Solana.

Step 1: Implementing L1X X-Talk EVM-Solana Flow Contract

Flow Contracts are at the core of cross-chain application development.

Create a new L1X project that provides the necessary files to get started

Use the provided code below to implement the EVM to Solana swap cross-chain logic.

Your L1X EVM-Solana Flow consist of below listed smart contracts that are to be created at /programs/swap_from_evm/src/

  1. lib.rs

  2. solana.rs

  3. types.rs

Create src/lib.rs

Create src/solana.rs

Create src/types.rs

The Fundamental Building Blocks of X-Talk require the below building blocks in X-Talk 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 flow 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 TestNet.

Create Wallet

Create your own wallet to generate a new keypair.

Import Wallet

Import you existing wallet using the l1x-cli-beta tool, by providing a private key. If you don't have one, Create Wallet.

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

Step 2: Register Avalanche Contract and X-Talk EVM-Solana Flow Contract with X-Talk Node

X-Talk Nodes allow for data listening and send to the X-Talk EVM-Solana Flow contract.

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

Request Example:

Step 3: Native Token Swap EVM - Solana

Step A: Implementing Script to Swap Native from Avalanche to Solana

Step 1: Add Script to Swap Native

In your Avalanche project, inside scripts folder add swapNative.ts script.

  • Update relevant network details

Step B: Send Native Token from Avalanche to Solana

To verify that event is received at YOUR_SOLANA_PROGRAM_ID, go to Solana explorer, check for the transaction with X-Talk Gateway for Solana mentioned in the table.

Step 4: ERC20 Token Swap EVM - Solana

Step A: Implementing Script to Swap ERC20 Token from Avalanche to Solana

Step 1: Add Script to Swap ERC20 Token

In your Avalanche project, inside scripts folder add swapERC20.ts script.

  • Update relevant network details

Step B: Send ERC20 Token from Avalanche to Solana

To verify that event is received at YOUR_SOLANA_PROGRAM_ID, go to Solana explorer, check for the transaction with X-Talk Gateway for Solana mentioned in the table.

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

AVALANCHE (Mainnnet)

0xf650162aF059734523E4Be23Ec5bAB9a5b878f57

SOLANA (Devnet)

GsU9N7gPtkMCSMwcEsyvj4sHcdoptHRyVwstbKiKmXeU

Last updated