Skip to content

Commit

Permalink
refactor(swap): use params struct (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored Jun 25, 2024
1 parent cb04b9b commit a543dd2
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions omnichain/swap/contracts/Swap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,40 @@ contract Swap is zContract, OnlySystem {
systemContract = SystemContract(systemContractAddress);
}

struct Params {
address target;
bytes to;
}

function onCrossChainCall(
zContext calldata context,
address zrc20,
uint256 amount,
bytes calldata message
) external virtual override onlySystem(systemContract) {
address targetTokenAddress;
bytes memory recipientAddress;
Params memory params = Params({target: address(0), to: bytes("")});

if (context.chainID == BITCOIN) {
targetTokenAddress = BytesHelperLib.bytesToAddress(message, 0);
recipientAddress = abi.encodePacked(
params.target = BytesHelperLib.bytesToAddress(message, 0);
params.to = abi.encodePacked(
BytesHelperLib.bytesToAddress(message, 20)
);
} else {
(address targetToken, bytes memory recipient) = abi.decode(
message,
(address, bytes)
);
targetTokenAddress = targetToken;
recipientAddress = recipient;
params.target = targetToken;
params.to = recipient;
}

(address gasZRC20, uint256 gasFee) = IZRC20(targetTokenAddress)
.withdrawGasFee();
uint256 inputForGas;
address gasZRC20;
uint256 gasFee;

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

uint256 inputForGas = SwapHelperLib.swapTokensForExactTokens(
inputForGas = SwapHelperLib.swapTokensForExactTokens(
systemContract,
zrc20,
gasFee,
Expand All @@ -53,11 +60,11 @@ contract Swap is zContract, OnlySystem {
systemContract,
zrc20,
amount - inputForGas,
targetTokenAddress,
params.target,
0
);

IZRC20(gasZRC20).approve(targetTokenAddress, gasFee);
IZRC20(targetTokenAddress).withdraw(recipientAddress, outputAmount);
IZRC20(gasZRC20).approve(params.target, gasFee);
IZRC20(params.target).withdraw(params.to, outputAmount);
}
}

0 comments on commit a543dd2

Please sign in to comment.