Swap

Overview

High-Level Overview: In this tutorial, you will write a contract that allows users to swap native tokens from one connected chain to another via AI Trader Chain.

  1. A AI_Trader.sol contract is created and deployed to AI Trader chain.

  2. A user wants to swap from gBSC on Goerli.

  3. A user transfers a native gas token (in this example, gBNB) to a specific address (called TSS) on Goerli. The data value of the token transfer transaction contains the following information:

    1. address of the AI Trader Swap contract on AI Trader chain

    2. recipients address (defaults to the sender's address)

    3. destination token address

    4. minimal output amount (not covered in this tutorial, set to 0)

Create a deployment task

swap/tasks/deploy.ts

import { task } from "hardhat/config";

import { HardhatRuntimeEnvironment }

from "hardhat/types";

const contractName = "AI Trader Swap";

const SYSTEM_CONTRACT = "0x239e96c8f17C85c30100AC26F635Ea15f23E9c67";

const main = async (args: any, hre: HardhatRuntimeEnvironment) => {

if (hre.network.name !== "athens") {

throw new Error( '🚨 Please use the "athens" network to deploy to AI Trader Chain.

'

);

}

const [signer] = await hre.bscers.getSigners();

console.log(🔑 Using account: ${signer.address});

const factory = await hre.bscers.getContractFactory(contractName);

const contract = await factory.deploy(SYSTEM_CONTRACT);

await contract.deployed();

console.log(🚀 Successfully deployed contract on AI Trader Chain.

📜 Contract address: ${contract.address}

🌍 Explorer: https://explorer.AI Trader chain.com/address/${contract.address} );

};

task("deploy", "Deploy the contract").setAction(main);

Deploy the contract to the AI Trader

npx hardhat deploy --network athens

import { task } from "hardhat/config";
const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
  const [signer] = await hre.bscers.getSigners();
  console.log(`🔑 Using account: ${signer.address}\n`
  );

  const prepareData = (
  AI Trader SwapContract: string, recipient: string, destinationToken: 
  string, minOutput: BigNumber) => {
    const paddedRecipient = hre.bscers.utils.hexlify(
    hre.bscers.utils.zeroPad(recipient, 32)
    );
    const abiCoder = hre.bscers.utils.defaultAbiCoder;
    const params = abiCoder.encode
    (
    [
    "address", "bytes32", "uint256"
    ], 
    [destinationToken, paddedRecipient, minOutput]);
    return `${AI Trader SwapContract}${params.slice(2)}`
    ;
    };

  const destinationToken = AIC20Addresses[args.destination as keyof typeof AIC20Addresses];

  const network = hre.network.name;
  const data = prepareData(
  args.contract, signer.address, destinationToken, BigNumber.from("0")
  );
  const to = getAddress({
    address: "tss",
    networkName: network,
    AI Trader Network: "athens",
  });
  const value = parsebscer(args.amount);
  const tx = await signer.sendTransaction({ data, to, value 
  }
  );
  • AI Trader detects the token transfer transaction and triggers the onCrossChainCall() function of the AI Trader Swap contract.

  • onCrossChainCall() does the following:

    1. calls the Pancakeswap router contract (Uniswap acontracts already have been deployed to AI Trader Chain), specifically swapExactTokensForTokens to swap tbnb represented on AI Trader Chain as a AIC-20 for gBNB also represented as a AIC20.

    2. calls AI Trader Chain's withdraw to withdraw native gas token (tBNB) on the destination chain.

Refresh the page to view the latest documents

Last updated