diff --git a/src/ConveyorRouterV1.sol b/src/ConveyorRouterV1.sol index 6909621..4899f29 100644 --- a/src/ConveyorRouterV1.sol +++ b/src/ConveyorRouterV1.sol @@ -9,9 +9,7 @@ import {ConveyorSwapCallbacks} from "./callbacks/ConveyorSwapCallbacks.sol"; import {IConveyorRouterV1} from "./interfaces/IConveyorRouterV1.sol"; interface IConveyorMulticall { - function executeMulticall( - ConveyorRouterV1.SwapAggregatorMulticall calldata multicall - ) external; + function executeMulticall(ConveyorRouterV1.SwapAggregatorMulticall calldata multicall) external; } /// @title ConveyorRouterV1 @@ -119,11 +117,7 @@ contract ConveyorRouterV1 is IConveyorRouterV1 { SwapAggregatorMulticall calldata genericMulticall ) public payable { ///@notice Transfer tokenIn from msg.sender to tokenInDestination address. - IERC20(swapData.tokenIn).safeTransferFrom( - msg.sender, - genericMulticall.tokenInDestination, - swapData.amountIn - ); + IERC20(swapData.tokenIn).safeTransferFrom(msg.sender, genericMulticall.tokenInDestination, swapData.amountIn); ///@notice Get tokenOut balance of msg.sender. uint256 balanceBefore = IERC20(swapData.tokenOut).balanceOf(msg.sender); @@ -131,27 +125,19 @@ contract ConveyorRouterV1 is IConveyorRouterV1 { uint256 tokenOutAmountRequired = balanceBefore + swapData.amountOutMin; ///@notice Execute Multicall. - IConveyorMulticall(CONVEYOR_MULTICALL).executeMulticall( - genericMulticall - ); + IConveyorMulticall(CONVEYOR_MULTICALL).executeMulticall(genericMulticall); uint256 balanceAfter = IERC20(swapData.tokenOut).balanceOf(msg.sender); ///@notice Check if tokenOut balance of msg.sender is sufficient. if (balanceAfter < tokenOutAmountRequired) { - revert InsufficientOutputAmount( - tokenOutAmountRequired - balanceAfter, - swapData.amountOutMin - ); + revert InsufficientOutputAmount(tokenOutAmountRequired - balanceAfter, swapData.amountOutMin); } if (swapData.affiliate & 0x1 != 0x0) { address affiliate = affiliates[swapData.affiliate >> 0x1]; if (affiliate == address(0)) { revert AffiliateDoesNotExist(); } - _safeTransferETH( - affiliate, - ConveyorMath.mul64U(AFFILIATE_PERCENT, msg.value) - ); + _safeTransferETH(affiliate, ConveyorMath.mul64U(AFFILIATE_PERCENT, msg.value)); } ///@dev First bit of referrer is used to check if referrer exists if (swapData.referrer & 0x1 != 0x0) { @@ -159,10 +145,7 @@ contract ConveyorRouterV1 is IConveyorRouterV1 { if (referrer == address(0)) { revert ReferrerDoesNotExist(); } - _safeTransferETH( - referrer, - ConveyorMath.mul64U(REFERRAL_PERCENT, msg.value) - ); + _safeTransferETH(referrer, ConveyorMath.mul64U(REFERRAL_PERCENT, msg.value)); } } @@ -184,10 +167,7 @@ contract ConveyorRouterV1 is IConveyorRouterV1 { _depositEth(amountIn, WETH); ///@notice Transfer WETH from WETH to tokenInDestination address. - IERC20(WETH).transfer( - swapAggregatorMulticall.tokenInDestination, - amountIn - ); + IERC20(WETH).transfer(swapAggregatorMulticall.tokenInDestination, amountIn); ///@notice Get tokenOut balance of msg.sender. uint256 balanceBefore = IERC20(swapData.tokenOut).balanceOf(msg.sender); @@ -196,29 +176,21 @@ contract ConveyorRouterV1 is IConveyorRouterV1 { uint256 tokenOutAmountRequired = balanceBefore + swapData.amountOutMin; ///@notice Execute Multicall. - IConveyorMulticall(CONVEYOR_MULTICALL).executeMulticall( - swapAggregatorMulticall - ); + IConveyorMulticall(CONVEYOR_MULTICALL).executeMulticall(swapAggregatorMulticall); ///@notice Get tokenOut balance of msg.sender after multicall execution. uint256 balanceAfter = IERC20(swapData.tokenOut).balanceOf(msg.sender); ///@notice Revert if tokenOut balance of msg.sender is insufficient. if (balanceAfter < tokenOutAmountRequired) { - revert InsufficientOutputAmount( - tokenOutAmountRequired - balanceAfter, - swapData.amountOutMin - ); + revert InsufficientOutputAmount(tokenOutAmountRequired - balanceAfter, swapData.amountOutMin); } if (swapData.affiliate & 0x1 != 0x0) { address affiliate = affiliates[swapData.affiliate >> 0x1]; if (affiliate == address(0)) { revert AffiliateDoesNotExist(); } - _safeTransferETH( - affiliate, - ConveyorMath.mul64U(AFFILIATE_PERCENT, swapData.protocolFee) - ); + _safeTransferETH(affiliate, ConveyorMath.mul64U(AFFILIATE_PERCENT, swapData.protocolFee)); } ///@dev First bit of referrer is used to check if referrer exists if (swapData.referrer & 0x1 != 0x0) { @@ -226,10 +198,7 @@ contract ConveyorRouterV1 is IConveyorRouterV1 { if (referrer == address(0)) { revert ReferrerDoesNotExist(); } - _safeTransferETH( - referrer, - ConveyorMath.mul64U(REFERRAL_PERCENT, swapData.protocolFee) - ); + _safeTransferETH(referrer, ConveyorMath.mul64U(REFERRAL_PERCENT, swapData.protocolFee)); } } @@ -244,9 +213,7 @@ contract ConveyorRouterV1 is IConveyorRouterV1 { if (swapAggregatorMulticall.tokenInDestination != address(0)) { ///@notice Transfer tokenIn from msg.sender to tokenInDestination address. IERC20(swapData.tokenIn).safeTransferFrom( - msg.sender, - swapAggregatorMulticall.tokenInDestination, - swapData.amountIn + msg.sender, swapAggregatorMulticall.tokenInDestination, swapData.amountIn ); } ///@notice Get ETH balance of msg.sender. @@ -256,9 +223,7 @@ contract ConveyorRouterV1 is IConveyorRouterV1 { uint256 amountOutRequired = balanceBefore + swapData.amountOutMin; ///@notice Execute Multicall. - IConveyorMulticall(CONVEYOR_MULTICALL).executeMulticall( - swapAggregatorMulticall - ); + IConveyorMulticall(CONVEYOR_MULTICALL).executeMulticall(swapAggregatorMulticall); ///@notice Get WETH balance of this contract. uint256 balanceWeth = IERC20(WETH).balanceOf(address(this)); @@ -271,20 +236,14 @@ contract ConveyorRouterV1 is IConveyorRouterV1 { ///@notice Revert if Eth balance of the caller is insufficient. if (msg.sender.balance < amountOutRequired) { - revert InsufficientOutputAmount( - amountOutRequired - msg.sender.balance, - swapData.amountOutMin - ); + revert InsufficientOutputAmount(amountOutRequired - msg.sender.balance, swapData.amountOutMin); } if (swapData.affiliate & 0x1 != 0x0) { address affiliate = affiliates[swapData.affiliate >> 0x1]; if (affiliate == address(0)) { revert AffiliateDoesNotExist(); } - _safeTransferETH( - affiliate, - ConveyorMath.mul64U(AFFILIATE_PERCENT, msg.value) - ); + _safeTransferETH(affiliate, ConveyorMath.mul64U(AFFILIATE_PERCENT, msg.value)); } ///@dev First bit of referrer is used to check if referrer exists if (swapData.referrer & 0x1 != 0x0) { @@ -292,10 +251,7 @@ contract ConveyorRouterV1 is IConveyorRouterV1 { if (referrer == address(0)) { revert ReferrerDoesNotExist(); } - _safeTransferETH( - referrer, - ConveyorMath.mul64U(REFERRAL_PERCENT, msg.value) - ); + _safeTransferETH(referrer, ConveyorMath.mul64U(REFERRAL_PERCENT, msg.value)); } } @@ -366,24 +322,19 @@ contract ConveyorRouterV1 is IConveyorRouterV1 { function _withdrawEth(uint256 amount, address weth) internal { /// @solidity memory-safe-assembly assembly { - mstore( - 0x0, - shl(224, 0x2e1a7d4d) /* keccak256("withdraw(uint256)") */ - ) + mstore(0x0, shl(224, 0x2e1a7d4d) /* keccak256("withdraw(uint256)") */ ) mstore(4, amount) if iszero( call( - gas() /* gas */, - weth /* to */, - 0 /* value */, - 0 /* in */, - 68 /* in size */, - 0 /* out */, + gas(), /* gas */ + weth, /* to */ + 0, /* value */ + 0, /* in */ + 68, /* in size */ + 0, /* out */ 0 /* out size */ ) - ) { - revert("Native Token Withdraw failed", amount) - } + ) { revert("Native Token Withdraw failed", amount) } } } @@ -394,17 +345,15 @@ contract ConveyorRouterV1 is IConveyorRouterV1 { mstore(0x0, shl(224, 0xd0e30db0)) /* keccak256("deposit()") */ if iszero( call( - gas() /* gas */, - weth /* to */, - amount /* value */, - 0 /* in */, - 0 /* in size */, - 0 /* out */, + gas(), /* gas */ + weth, /* to */ + amount, /* value */ + 0, /* in */ + 0, /* in size */ + 0, /* out */ 0 /* out size */ ) - ) { - revert("Native token deposit failed", amount) - } + ) { revert("Native token deposit failed", amount) } } } @@ -435,21 +384,11 @@ contract ConveyorRouterV1 is IConveyorRouterV1 { } ///@notice Function to upgrade the ConveyorMulticall contract. - function upgradeMulticall( - bytes memory bytecode, - bytes32 salt - ) external payable onlyOwner returns (address) { + function upgradeMulticall(bytes memory bytecode, bytes32 salt) external payable onlyOwner returns (address) { assembly { - let addr := create2( - callvalue(), - add(bytecode, 0x20), - mload(bytecode), - salt - ) - - if iszero(extcodesize(addr)) { - revert(0, 0) - } + let addr := create2(callvalue(), add(bytecode, 0x20), mload(bytecode), salt) + + if iszero(extcodesize(addr)) { revert(0, 0) } sstore(CONVEYOR_MULTICALL.slot, addr) } @@ -464,10 +403,7 @@ contract ConveyorRouterV1 is IConveyorRouterV1 { affiliateIndex[affiliateAddress] = tempAffiliateNonce; unchecked { tempAffiliateNonce++; - require( - tempAffiliateNonce < type(uint16).max >> 0x1, - "Affiliate nonce overflow" - ); + require(tempAffiliateNonce < type(uint16).max >> 0x1, "Affiliate nonce overflow"); affiliateNonce = tempAffiliateNonce; } } @@ -483,10 +419,7 @@ contract ConveyorRouterV1 is IConveyorRouterV1 { unchecked { tempReferrerNonce++; - require( - tempReferrerNonce < type(uint16).max >> 0x1, - "Referrer nonce overflow" - ); + require(tempReferrerNonce < type(uint16).max >> 0x1, "Referrer nonce overflow"); referrerNonce = tempReferrerNonce; } } @@ -501,30 +434,14 @@ contract ConveyorRouterV1 is IConveyorRouterV1 { contract ConveyorMulticall is IConveyorMulticall, ConveyorSwapCallbacks { constructor() {} - function executeMulticall( - ConveyorRouterV1.SwapAggregatorMulticall calldata multicall - ) external { - for (uint256 i = 0; i < multicall.calls.length; ) { + function executeMulticall(ConveyorRouterV1.SwapAggregatorMulticall calldata multicall) external { + for (uint256 i = 0; i < multicall.calls.length;) { address target = multicall.calls[i].target; bytes calldata callData = multicall.calls[i].callData; assembly ("memory-safe") { let freeMemoryPointer := mload(0x40) - calldatacopy( - freeMemoryPointer, - callData.offset, - callData.length - ) - if iszero( - call( - gas(), - target, - 0, - freeMemoryPointer, - callData.length, - 0, - 0 - ) - ) { + calldatacopy(freeMemoryPointer, callData.offset, callData.length) + if iszero(call(gas(), target, 0, freeMemoryPointer, callData.length, 0, 0)) { returndatacopy(0, 0, returndatasize()) revert(0, returndatasize()) }