Swap
We use a custom SWAP contract to interact with the chosen DEX - Uniswap.
We are currently using Uniswap V3.
Functions
SWAP_ROUTER
function SWAP_ROUTER() external view returns (contract ISwapRouter)Returns
| Name | Type | Description |
|---|---|---|
| _0 | contract ISwapRouter | undefined |
_swap
function _swap(address baseToken_,address targetToken_,uint256 amount_) internal returns (uint256 amount)Returns
| Name | Type | Description |
|---|---|---|
| amount | uint256 | Amount retunred from the swap |
function _swap(
address baseToken_,
address targetToken_,
uint256 amount_
) internal returns (uint256 amount) {
// The call to `exactInputSingle` executes the swap.
if (targetToken_ == address(0)) {
// Swap tokens for WETH then convert to ETH
amount = SWAP_ROUTER.exactInputSingle(
ISwapRouter.ExactInputSingleParams({
tokenIn: baseToken_,
tokenOut: SWAP_ROUTER.WETH9(),
fee: _poolFee,
recipient: address(this),
amountIn: amount_,
amountOutMinimum: 0,
sqrtPriceLimitX96: 0
})
);
IWETH9(SWAP_ROUTER.WETH9()).withdraw(amount);
return amount;
} else
return
SWAP_ROUTER.exactInputSingle(
ISwapRouter.ExactInputSingleParams({
tokenIn: baseToken_,
tokenOut: targetToken_,
fee: _poolFee,
recipient: address(this),
amountIn: amount_,
amountOutMinimum: 0,
sqrtPriceLimitX96: 0
})
);
}_approveSwapSpend
function _approveSwapSpend(address baseToken_,uint256 amount_) internal returns (bool success)Returns
| Name | Type | Description |
|---|---|---|
| success | bool | If the approval was successfull. |
function _approveSwapSpend(
address baseToken_,
uint256 amount_
) internal returns (bool success) {
success = _checkSendAllowance(
baseToken_,
address(SWAP_ROUTER),
amount_
);
if (!success) {
success = IERC20(baseToken_).approve(address(SWAP_ROUTER), amount_);
require(success, "Swap Allowance Set Failed");
}
}_checkSpendAllowance
function _checkSpendAllowance(address baseToken_,address spender_,uint256 neededAllowance_) internal view returns (bool)Returns
| Name | Type | Description |
|---|---|---|
| success | bool | If there enough allowance |
function _checkSpendAllowance(
address baseToken_,
address spender_,
uint256 neededAllowance_
) internal view returns (bool) {
return
IERC20(baseToken_).allowance(address(this), spender_) >=
neededAllowance_;
}