Skip to content

Commit

Permalink
feat: added ability to swap token on and from ZetaChain
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Jun 27, 2024
1 parent e1a2ae4 commit 6adc2fa
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 22 deletions.
21 changes: 15 additions & 6 deletions omnichain/swap/contracts/Swap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,38 @@ contract Swap is zContract, OnlySystem {
params.to = recipient;
}

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

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

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

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

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

IZRC20(gasZRC20).approve(params.target, gasFee);
IZRC20(params.target).withdraw(params.to, outputAmount);
IZRC20(gasZRC20).approve(targetToken, gasFee);
IZRC20(targetToken).withdraw(recipient, outputAmount);
}
}
76 changes: 60 additions & 16 deletions omnichain/swap/contracts/SwapToAnyToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ contract SwapToAnyToken is zContract, OnlySystem {
bool withdraw;
}

receive() external payable {}

function onCrossChainCall(
zContext calldata context,
address zrc20,
Expand All @@ -37,49 +39,91 @@ contract SwapToAnyToken is zContract, OnlySystem {

if (context.chainID == BITCOIN) {
params.target = BytesHelperLib.bytesToAddress(message, 0);
params.to = abi.encodePacked(BytesHelperLib.bytesToAddress(message, 20));
params.to = abi.encodePacked(
BytesHelperLib.bytesToAddress(message, 20)
);
if (message.length >= 41) {
params.withdraw = BytesHelperLib.bytesToBool(message, 40);
}
} else {
(address targetToken, bytes memory recipient, bool withdrawFlag) = abi.decode(
message,
(address, bytes, bool)
);
(
address targetToken,
bytes memory recipient,
bool withdrawFlag
) = abi.decode(message, (address, bytes, bool));
params.target = targetToken;
params.to = recipient;
params.withdraw = withdrawFlag;
}

swapAndWithdraw(
zrc20,
amount,
params.target,
params.to,
params.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 {
IWETH9(params.target).transfer(address(uint160(bytes20(params.to))), outputAmount);
address wzeta = systemContract.wZetaContractAddress();
if (targetToken == wzeta) {
IWETH9(wzeta).withdraw(outputAmount);
address payable recipientAddress = payable(
address(uint160(bytes20(recipient)))
);
recipientAddress.transfer(outputAmount);
} else {
address recipientAddress = address(uint160(bytes20(recipient)));
IWETH9(targetToken).transfer(recipientAddress, outputAmount);
}
}
}

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);
}
}

0 comments on commit 6adc2fa

Please sign in to comment.