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.
A
AI_Trader.sol
contract is created and deployed to AI Trader chain.A user wants to swap from gBSC on Goerli.
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:address of the AI Trader Swap contract on AI Trader chain
recipients address (defaults to the sender's address)
destination token address
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
AI Trader detects the token transfer transaction and triggers the
onCrossChainCall()
function of the AI Trader Swap contract.onCrossChainCall()
does the following: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.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