Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Jun 24, 2024
1 parent c99b922 commit cb65765
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 14 deletions.
60 changes: 46 additions & 14 deletions omnichain/swap/contracts/SwapToAnyToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,41 +56,73 @@ contract SwapToAnyToken is zContract, OnlySystem {
params.withdraw = withdrawFlag;
}

swapAndWithdraw(
zrc20,
amount,
params.target,
params.to,
params.withdraw
);
}

function swap(
address inputToken,
uint256 amount,
address targetToken,
bytes memory recipient,
bool withdraw
) public {
IZRC20(inputToken).transferFrom(msg.sender, address(this), amount);

swapAndWithdraw(inputToken, amount, targetToken, recipient, withdraw);
}

function swapAndWithdraw(
address inputToken,
uint256 amount,
address targetToken,
bytes memory recipient,
bool withdraw
) internal {
uint256 outputAmount;
uint256 inputForGas;
address gasZRC20;
uint256 gasFee;

if (params.withdraw) {
(gasZRC20, gasFee) = IZRC20(params.target).withdrawGasFee();
if (withdraw) {
(gasZRC20, gasFee) = IZRC20(targetToken).withdrawGasFee();

inputForGas = SwapHelperLib.swapTokensForExactTokens(
systemContract,
zrc20,
inputToken,
gasFee,
gasZRC20,
amount
);
}

uint256 outputAmount = SwapHelperLib.swapExactTokensForTokens(
outputAmount = SwapHelperLib.swapExactTokensForTokens(
systemContract,
zrc20,
params.withdraw ? amount - inputForGas : amount,
params.target,
inputToken,
withdraw ? amount - inputForGas : amount,
targetToken,
0
);

if (params.withdraw) {
IZRC20(gasZRC20).approve(params.target, gasFee);
IZRC20(params.target).withdraw(params.to, outputAmount);
if (withdraw) {
IZRC20(gasZRC20).approve(targetToken, gasFee);
IZRC20(targetToken).withdraw(recipient, outputAmount);
} else {
address recipient = address(uint160(bytes20(params.to)));
address wzeta = systemContract.wZetaContractAddress();
if (params.target == wzeta) {
if (targetToken == wzeta) {
IWETH9(wzeta).withdraw(outputAmount);
payable(recipient).transfer(outputAmount);
address payable recipientAddress = payable(
address(uint160(bytes20(recipient)))
);
recipientAddress.transfer(outputAmount);
} else {
IWETH9(params.target).transfer(recipient, outputAmount);
address recipientAddress = address(uint160(bytes20(recipient)));
IWETH9(targetToken).transfer(recipientAddress, outputAmount);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions omnichain/swap/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "./tasks/interact";
import "./tasks/deploy";
import "./tasks/swap";
import "@nomicfoundation/hardhat-toolbox";
import "@zetachain/toolkit/tasks";

Expand Down
44 changes: 44 additions & 0 deletions omnichain/swap/tasks/swap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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();

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", "Sends a message from one chain to another.", 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)")
.setAction(main);

0 comments on commit cb65765

Please sign in to comment.