chore: merge main

This commit is contained in:
royvardhan
2025-02-26 23:26:27 +05:30
parent 0be69c9aea
commit 7b0dd5872c
4 changed files with 89 additions and 79 deletions

View File

@@ -140,27 +140,51 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
uint256 nTokens,
address receiver,
bytes calldata swaps
) external payable whenNotPaused nonReentrant returns (uint256 amountOut) {
) public payable whenNotPaused nonReentrant returns (uint256 amountOut) {
if (receiver == address(0)) {
revert TychoRouter__AddressZero();
}
// Assume funds already in our router.
// Assume funds are already in the router.
if (wrapEth) {
_wrapETH(amountIn);
tokenIn = address(_weth);
}
return _executeSwap(
amountIn,
tokenIn,
tokenOut,
minAmountOut,
unwrapEth,
nTokens,
receiver,
swaps
);
uint256 initialBalance = tokenIn == address(0)
? address(this).balance
: IERC20(tokenIn).balanceOf(address(this));
amountOut = _swap(amountIn, nTokens, swaps);
uint256 currentBalance = tokenIn == address(0)
? address(this).balance
: IERC20(tokenIn).balanceOf(address(this));
uint256 amountConsumed = initialBalance - currentBalance;
if (amountConsumed < amountIn) {
uint256 leftoverAmount = amountIn - amountConsumed;
revert TychoRouter__AmountInNotFullySpent(leftoverAmount);
}
if (fee > 0) {
uint256 feeAmount = (amountOut * fee) / 10000;
amountOut -= feeAmount;
IERC20(tokenOut).safeTransfer(feeReceiver, feeAmount);
}
if (minAmountOut > 0 && amountOut < minAmountOut) {
revert TychoRouter__NegativeSlippage(amountOut, minAmountOut);
}
if (unwrapEth) {
_unwrapETH(amountOut);
}
if (tokenOut == address(0)) {
Address.sendValue(payable(receiver), amountOut);
} else {
IERC20(tokenOut).safeTransfer(receiver, amountOut);
}
}
/**
@@ -202,16 +226,9 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
IAllowanceTransfer.PermitSingle calldata permitSingle,
bytes calldata signature,
bytes calldata swaps
) external payable whenNotPaused nonReentrant returns (uint256 amountOut) {
if (receiver == address(0)) {
revert TychoRouter__AddressZero();
}
) external payable whenNotPaused returns (uint256 amountOut) {
// For native ETH, assume funds already in our router. Else, transfer and handle approval.
if (wrapEth) {
_wrapETH(amountIn);
tokenIn = address(_weth);
} else if (tokenIn != address(0)) {
if (tokenIn != address(0)) {
permit2.permit(msg.sender, permitSingle, signature);
permit2.transferFrom(
msg.sender,
@@ -221,11 +238,12 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
);
}
return _executeSwap(
return swap(
amountIn,
tokenIn,
tokenOut,
minAmountOut,
wrapEth,
unwrapEth,
nTokens,
receiver,
@@ -233,61 +251,6 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
);
}
/**
* @notice Internal implementation of the core swap logic shared between swap() and swapPermit2().
*
* @notice This function centralizes the swap execution logic.
* @notice For detailed documentation on parameters and behavior, see the documentation for
* swap() and swapPermit2() functions.
*
*/
function _executeSwap(
uint256 amountIn,
address tokenIn,
address tokenOut,
uint256 minAmountOut,
bool unwrapEth,
uint256 nTokens,
address receiver,
bytes calldata swaps
) internal returns (uint256 amountOut) {
uint256 initialBalance = tokenIn == address(0)
? address(this).balance
: IERC20(tokenIn).balanceOf(address(this));
amountOut = _swap(amountIn, nTokens, swaps);
uint256 currentBalance = tokenIn == address(0)
? address(this).balance
: IERC20(tokenIn).balanceOf(address(this));
uint256 amountConsumed = initialBalance - currentBalance;
if (amountConsumed < amountIn) {
uint256 leftoverAmount = amountIn - amountConsumed;
revert TychoRouter__AmountInNotFullySpent(leftoverAmount);
}
if (fee > 0) {
uint256 feeAmount = (amountOut * fee) / 10000;
amountOut -= feeAmount;
IERC20(tokenOut).safeTransfer(feeReceiver, feeAmount);
}
if (minAmountOut > 0 && amountOut < minAmountOut) {
revert TychoRouter__NegativeSlippage(amountOut, minAmountOut);
}
if (unwrapEth) {
_unwrapETH(amountOut);
}
if (tokenOut == address(0)) {
Address.sendValue(payable(receiver), amountOut);
} else {
IERC20(tokenOut).safeTransfer(receiver, amountOut);
}
}
/**
* @dev Executes sequential swaps as defined by the provided swap graph.
*