Skip to content

Commit

Permalink
feat: remove swapin function
Browse files Browse the repository at this point in the history
  • Loading branch information
alpanaca committed Jan 23, 2024
1 parent b426ace commit 86084d7
Show file tree
Hide file tree
Showing 7 changed files with 0 additions and 1,536 deletions.
142 changes: 0 additions & 142 deletions src/workers/PancakeV3Worker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ contract PancakeV3Worker is Initializable, Ownable2StepUpgradeable, ReentrancyGu
ERC20 _token0 = token0;
ERC20 _token1 = token1;

// SLOAD
ICommonV3PositionManager _nftPositionManager = nftPositionManager;
// Mint new position and stake it with masterchef
_token0.safeApprove(address(_nftPositionManager), _amountIn0);
Expand Down Expand Up @@ -247,147 +246,6 @@ contract PancakeV3Worker is Initializable, Ownable2StepUpgradeable, ReentrancyGu
emit LogIncreasePosition(_nftTokenId, msg.sender, _tickLower, _tickUpper, _amount0, _amount1);
}

function _prepareOptimalTokensForIncrease(
address _token0,
address _token1,
int24 _tickLower,
int24 _tickUpper,
uint256 _amountIn0,
uint256 _amountIn1
) internal returns (uint256 _amount0Desired, uint256 _amount1Desired) {
// Revert if not enough balance
if (ERC20(_token0).balanceOf(address(this)) < _amountIn0 || ERC20(_token1).balanceOf(address(this)) < _amountIn1) {
revert PancakeV3Worker_InvalidParams();
}
(, int24 _currTick,,,,,) = pool.slot0();
if (_tickLower <= _currTick && _currTick <= _tickUpper) {
(_amount0Desired, _amount1Desired) = _prepareOptimalTokensForIncreaseInRange(
address(_token0), address(_token1), _tickLower, _tickUpper, _amountIn0, _amountIn1
);
} else {
(_amount0Desired, _amount1Desired) = _prepareOptimalTokensForIncreaseOutOfRange(
address(_token0), address(_token1), _currTick, _tickLower, _tickUpper, _amountIn0, _amountIn1
);
}
}

function _prepareOptimalTokensForIncreaseInRange(
address _token0,
address _token1,
int24 _tickLower,
int24 _tickUpper,
uint256 _amountIn0,
uint256 _amountIn1
) internal returns (uint256 _optimalAmount0, uint256 _optimalAmount1) {
// Calculate zap in amount and direction.
(uint256 _amountSwap, uint256 _minAmountOut, bool _zeroForOne) = zapV3.calc(
IZapV3.CalcParams({
pool: address(pool),
amountIn0: _amountIn0,
amountIn1: _amountIn1,
tickLower: _tickLower,
tickUpper: _tickUpper
})
);

// Find out tokenIn and tokenOut
address _tokenIn;
address _tokenOut;
if (_zeroForOne) {
_tokenIn = address(_token0);
_tokenOut = address(_token1);
} else {
_tokenIn = address(_token1);
_tokenOut = address(_token0);
}

// Swap
ERC20(_tokenIn).safeApprove(address(router), _amountSwap);
uint256 _amountOut = router.exactInputSingle(
IPancakeV3Router.ExactInputSingleParams({
tokenIn: _tokenIn,
tokenOut: _tokenOut,
fee: poolFee,
recipient: address(this),
amountIn: _amountSwap,
amountOutMinimum: _minAmountOut,
sqrtPriceLimitX96: 0
})
);

if (_zeroForOne) {
_optimalAmount0 = _amountIn0 - _amountSwap;
_optimalAmount1 = _amountIn1 + _amountOut;
} else {
_optimalAmount0 = _amountIn0 + _amountOut;
_optimalAmount1 = _amountIn1 - _amountSwap;
}
}

function _prepareOptimalTokensForIncreaseOutOfRange(
address _token0,
address _token1,
int24 _currTick,
int24 _tickLower,
int24 _tickUpper,
uint256 _amountIn0,
uint256 _amountIn1
) internal returns (uint256 _optimalAmount0, uint256 _optimalAmount1) {
// If out of upper range (currTick > tickUpper), we swap token0 for token1
// and vice versa, to push price closer to range.
// We only want to swap until price move back in range so
// we will swap until price hit the first tick within range.
if (_currTick > _tickUpper) {
if (_amountIn0 > 0) {
uint256 _token0Before = ERC20(_token0).balanceOf(address(this));
// zero for one swap
ERC20(_token0).safeApprove(address(router), _amountIn0);
uint256 _amountOut = router.exactInputSingle(
IPancakeV3Router.ExactInputSingleParams({
tokenIn: _token0,
tokenOut: _token1,
fee: poolFee,
recipient: address(this),
amountIn: _amountIn0,
amountOutMinimum: 0,
sqrtPriceLimitX96: LibTickMath.getSqrtRatioAtTick(_tickUpper) - 1 // swap until passed upper tick
})
);
// Update optimal amount
_optimalAmount0 = _amountIn0 + ERC20(_token0).balanceOf(address(this)) - _token0Before;
_optimalAmount1 = _amountIn1 + _amountOut;
}
} else {
if (_amountIn1 > 0) {
uint256 _token1Before = ERC20(_token1).balanceOf(address(this));
// one for zero swap
ERC20(_token1).safeApprove(address(router), _amountIn1);
uint256 _amountOut = router.exactInputSingle(
IPancakeV3Router.ExactInputSingleParams({
tokenIn: _token1,
tokenOut: _token0,
fee: poolFee,
recipient: address(this),
amountIn: _amountIn1,
amountOutMinimum: 0,
sqrtPriceLimitX96: LibTickMath.getSqrtRatioAtTick(_tickLower) + 1 // swap until passed lower tick
})
);
// Update optimal amount
_optimalAmount0 = _amountIn0 + _amountOut;
_optimalAmount1 = _amountIn1 + ERC20(_token1).balanceOf(address(this)) - _token1Before;
}
}

// Also prepare in range if tick is back in range after swap
(, _currTick,,,,,) = pool.slot0();
if (_tickLower <= _currTick && _currTick <= _tickUpper) {
return _prepareOptimalTokensForIncreaseInRange(
_token0, _token1, _tickLower, _tickUpper, _optimalAmount0, _optimalAmount1
);
}
}

/// @dev Closing position (burning NFT) requires NFT to be empty (no tokens, rewards remain).
/// Executor should handle claiming rewards before closing position.
function closePosition() external nonReentrant onlyExecutorInScope {
Expand Down
Loading

0 comments on commit 86084d7

Please sign in to comment.