From 99f848be74950c377b0799aef5e7c9d16edf6713 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Thu, 29 Aug 2024 18:26:11 +0900 Subject: [PATCH] add swap.ts back --- omnichain/swap/tasks/swap.ts | 47 ++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 omnichain/swap/tasks/swap.ts diff --git a/omnichain/swap/tasks/swap.ts b/omnichain/swap/tasks/swap.ts new file mode 100644 index 00000000..ff88f588 --- /dev/null +++ b/omnichain/swap/tasks/swap.ts @@ -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)");