Developer Docs
Smart Contracts
Contract Logic
Swap

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

NameTypeDescription
_0contract ISwapRouterundefined

_swap

    function _swap(address baseToken_,address targetToken_,uint256 amount_) internal returns (uint256 amount)

Returns

NameTypeDescription
amountuint256Amount 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

NameTypeDescription
successboolIf 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

NameTypeDescription
successboolIf there enough allowance
   function _checkSpendAllowance(
        address baseToken_,
        address spender_,
        uint256 neededAllowance_
    ) internal view returns (bool) {
        return
            IERC20(baseToken_).allowance(address(this), spender_) >=
            neededAllowance_;
    }