Skip to content

Commit

Permalink
add swap.ts back
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Aug 29, 2024
1 parent fea24c3 commit 99f848b
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions omnichain/swap/tasks/swap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { task } from "hardhat/config";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { parseEther } from "@ethersproject/units";
import { ethers } from "ethers";

const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const [signer] = await hre.ethers.getSigners();

if (!/zeta_(testnet|mainnet)/.test(hre.network.name)) {
throw new Error('🚨 Please use either "zeta_testnet" or "zeta_mainnet".');
}

const factory = await hre.ethers.getContractFactory("SwapToAnyToken");
const contract = factory.attach(args.contract);

const amount = parseEther(args.amount);
const inputToken = args.inputToken;
const targetToken = args.targetToken;
const recipient = ethers.utils.arrayify(args.recipient);
const withdraw = JSON.parse(args.withdraw);

const erc20Factory = await hre.ethers.getContractFactory("ERC20");
const inputTokenContract = erc20Factory.attach(args.inputToken);

const approval = await inputTokenContract.approve(args.contract, amount);
await approval.wait();

const tx = await contract.swap(
inputToken,
amount,
targetToken,
recipient,
withdraw
);

await tx.wait();
console.log(`Transaction hash: ${tx.hash}`);
};

task("swap", "Interact with the Swap contract from ZetaChain", main)
.addFlag("json", "Output JSON")
.addParam("contract", "Contract address")
.addParam("amount", "Token amount to send")
.addParam("inputToken", "Input token address")
.addParam("targetToken", "Target token address")
.addParam("recipient", "Recipient address")
.addParam("withdraw", "Withdraw flag (true/false)");

0 comments on commit 99f848b

Please sign in to comment.