Skip to content

Commit

Permalink
chore: fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOsiris committed Oct 20, 2023
1 parent 0dc187a commit 5c036b7
Showing 1 changed file with 41 additions and 124 deletions.
165 changes: 41 additions & 124 deletions src/ConveyorRouterV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -119,50 +117,35 @@ 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);
///@notice Calculate tokenOut amount required.
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) {
address referrer = referrers[swapData.referrer >> 0x1];
if (referrer == address(0)) {
revert ReferrerDoesNotExist();
}
_safeTransferETH(
referrer,
ConveyorMath.mul64U(REFERRAL_PERCENT, msg.value)
);
_safeTransferETH(referrer, ConveyorMath.mul64U(REFERRAL_PERCENT, msg.value));
}
}

Expand All @@ -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);
Expand All @@ -196,40 +176,29 @@ 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) {
address referrer = referrers[swapData.referrer >> 0x1];
if (referrer == address(0)) {
revert ReferrerDoesNotExist();
}
_safeTransferETH(
referrer,
ConveyorMath.mul64U(REFERRAL_PERCENT, swapData.protocolFee)
);
_safeTransferETH(referrer, ConveyorMath.mul64U(REFERRAL_PERCENT, swapData.protocolFee));
}
}

Expand All @@ -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.
Expand All @@ -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));
Expand All @@ -271,31 +236,22 @@ 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) {
address referrer = referrers[swapData.referrer >> 0x1];
if (referrer == address(0)) {
revert ReferrerDoesNotExist();
}
_safeTransferETH(
referrer,
ConveyorMath.mul64U(REFERRAL_PERCENT, msg.value)
);
_safeTransferETH(referrer, ConveyorMath.mul64U(REFERRAL_PERCENT, msg.value));
}
}

Expand Down Expand Up @@ -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) }
}
}

Expand All @@ -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) }
}
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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())
}
Expand Down

0 comments on commit 5c036b7

Please sign in to comment.