From a543dd27614f382193ee1601c9198497b1efc82b Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Tue, 25 Jun 2024 18:50:35 +0300 Subject: [PATCH] refactor(swap): use params struct (#183) --- omnichain/swap/contracts/Swap.sol | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/omnichain/swap/contracts/Swap.sol b/omnichain/swap/contracts/Swap.sol index ade9faf8..fa36a853 100644 --- a/omnichain/swap/contracts/Swap.sol +++ b/omnichain/swap/contracts/Swap.sol @@ -15,18 +15,22 @@ 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 { @@ -34,14 +38,17 @@ contract Swap is zContract, OnlySystem { 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, @@ -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); } }