> For the complete documentation index, see [llms.txt](https://ais-organization-2.gitbook.io/ai-trader/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ais-organization-2.gitbook.io/ai-trader/docs/swap.md).

# Swap

### Overview[​](https://www.zetachain.com/docs/developers/tutorials/swap/#overview) <a href="#overview" id="overview"></a>

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](/ai-trader/main-net.md).

1. A `AI_Trader.sol` contract is created and deployed to AI Trader chain.
2. A user wants to[ swap](https://aitchain.io/) from gBSC on Goerli.
3. A user [transfers](https://aitchain.io/) 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](/ai-trader/ait-token.md) address
   4. minimal output amount (not covered in this tutorial, set to 0)

### Create a deployment task <a href="#create-a-deployment-task" id="create-a-deployment-task"></a>

swap/tasks/deploy.ts

`import { task } from "hardhat/config";`&#x20;

`import { HardhatRuntimeEnvironment }`&#x20;

`from "hardhat/types";`

`const contractName = "AI Trader Swap";`&#x20;

`const SYSTEM_CONTRACT = "0x239e96c8f17C85c30100AC26F635Ea15f23E9c67";`

`const main = async (args: any, hre: HardhatRuntimeEnvironment) => {`&#x20;

`if (hre.network.name !== "athens") {`&#x20;

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

`'`&#x20;

`);`&#x20;

`}`

`const [signer] = await hre.bscers.getSigners();`&#x20;

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

`const factory = await hre.bscers.getContractFactory(contractName);`&#x20;

`const contract = await factory.deploy(SYSTEM_CONTRACT);`&#x20;

`await contract.deployed();`

`console.log(🚀 Successfully deployed contract on AI Trader Chain.`&#x20;

`📜 Contract address: ${contract.address}`&#x20;

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

`};`

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

#### Deploy the contract to the AI Trader <a href="#deploy-the-contract-to-the-zetachain-testnet" id="deploy-the-contract-to-the-zetachain-testnet"></a>

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