Skip to content

Commit

Permalink
added SwapToAnyToken
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Sep 4, 2024
1 parent 3e8e411 commit 19664f2
Showing 1 changed file with 42 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
pragma solidity 0.8.26;

import "@zetachain/protocol-contracts/contracts/zevm/SystemContract.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/zContract.sol";
import "@zetachain/toolkit/contracts/SwapHelperLib.sol";
import "@zetachain/toolkit/contracts/BytesHelperLib.sol";
import "./shared/SystemContract.sol";
import "./shared/SwapHelperLib.sol";
import "./shared/BytesHelperLib.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./shared/libraries/UniswapV2Library.sol";

import {RevertContext, RevertOptions} from "@zetachain/protocol-contracts/contracts/Revert.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/UniversalContract.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/IGatewayZEVM.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/IWZETA.sol";
import "@zetachain/toolkit/contracts/OnlySystem.sol";

contract SwapToAnyToken is zContract, OnlySystem {
contract SwapToAnyToken is UniversalContract {
SystemContract public systemContract;

uint256 constant BITCOIN = 18332;
address constant gatewayAddress =
0xA51c1fc2f0D1a1b8494Ed1FE312d7C3a78Ed91C0;

constructor(address systemContractAddress) {
systemContract = SystemContract(systemContractAddress);
Expand All @@ -28,7 +33,7 @@ contract SwapToAnyToken is zContract, OnlySystem {
address zrc20,
uint256 amount,
bytes calldata message
) external virtual override onlySystem(systemContract) {
) external virtual override {
Params memory params = Params({
target: address(0),
to: bytes(""),
Expand All @@ -37,15 +42,18 @@ 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;
Expand All @@ -54,23 +62,29 @@ contract SwapToAnyToken is zContract, OnlySystem {
uint256 inputForGas;
address gasZRC20;
uint256 gasFee;
uint256 swapAmount;

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

inputForGas = SwapHelperLib.swapTokensForExactTokens(
systemContract,
zrc20,
gasFee,
gasZRC20,
amount
);
if (gasZRC20 == zrc20) {
swapAmount = amount - gasFee;
} else {
inputForGas = SwapHelperLib.swapTokensForExactTokens(
systemContract,
zrc20,
gasFee,
gasZRC20,
amount
);
swapAmount = amount - inputForGas;
}
}

uint256 outputAmount = SwapHelperLib.swapExactTokensForTokens(
systemContract,
zrc20,
params.withdraw ? amount - inputForGas : amount,
params.withdraw ? swapAmount : amount,
params.target,
0
);
Expand All @@ -79,7 +93,12 @@ contract SwapToAnyToken is zContract, OnlySystem {
IZRC20(gasZRC20).approve(params.target, gasFee);
IZRC20(params.target).withdraw(params.to, outputAmount);
} else {
IWETH9(params.target).transfer(address(uint160(bytes20(params.to))), outputAmount);
IWETH9(params.target).transfer(
address(uint160(bytes20(params.to))),
outputAmount
);
}
}

function onRevert(RevertContext calldata revertContext) external override {}
}

0 comments on commit 19664f2

Please sign in to comment.