ethereum / solidity

Solidity, the Smart Contract Programming Language
https://soliditylang.org
GNU General Public License v3.0
23.35k stars 5.77k forks source link

Invalid import: #14047

Closed codeWithDevMA closed 1 year ago

codeWithDevMA commented 1 year ago

import "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol"; Invalid import: trying to use an unsupported protocolhardhat(406) "devDependencies": { "@nomicfoundation/hardhat-toolbox": "^2.0.2", "@nomiclabs/hardhat-ethers": "^2.2.2", "@nomiclabs/hardhat-waffle": "^2.0.1", "chai": "^4.3.7", "ethereum-waffle": "^3.4.0", "ethers": "^5.7.2", "hardhat": "^2.13.0" }, "dependencies": { "@typechain/ethers-v5": "^10.2.0", "@typechain/hardhat": "^6.1.5", "@uniswap/v3-core": "^1.0.1", "@uniswap/v3-periphery": "^1.4.3", "chai-as-promised": "^7.1.1" } // SPDX-License-Identifier: GPL-2.0-or-later pragma solidity =0.8.19; pragma abicoder v2;

import "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol";

interface IERC20 { function balanceOf(address account) external view returns (uint256);

function transfer(address recipient, uint256 amount)
    external
    returns (bool);

function approve(address spender, uint256 amount) external returns (bool);

}

contract SingleSwap { address public constant routerAddress = 0xE592427A0AEce92De3Edee1F18E0157C05861564; ISwapRouter public immutable swapRouter = ISwapRouter(routerAddress);

address public constant LINK = 0x326C977E6efc84E512bB9C30f76E30c160eD06FB;
address public constant WETH = 0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6;

IERC20 public linkToken = IERC20(LINK);

// For this example, we will set the pool fee to 0.3%.
uint24 public constant poolFee = 3000;

constructor() {}

function swapExactInputSingle(uint256 amountIn)
    external
    returns (uint256 amountOut)
{
    linkToken.approve(address(swapRouter), amountIn);

    ISwapRouter.ExactInputSingleParams memory params = ISwapRouter
        .ExactInputSingleParams({
            tokenIn: LINK,
            tokenOut: WETH,
            fee: poolFee,
            recipient: address(this),
            deadline: block.timestamp,
            amountIn: amountIn,
            amountOutMinimum: 0,
            sqrtPriceLimitX96: 0
        });

    amountOut = swapRouter.exactInputSingle(params);
}

function swapExactOutputSingle(uint256 amountOut, uint256 amountInMaximum)
    external
    returns (uint256 amountIn)
{
    linkToken.approve(address(swapRouter), amountInMaximum);

    ISwapRouter.ExactOutputSingleParams memory params = ISwapRouter
        .ExactOutputSingleParams({
            tokenIn: LINK,
            tokenOut: WETH,
            fee: poolFee,
            recipient: address(this),
            deadline: block.timestamp,
            amountOut: amountOut,
            amountInMaximum: amountInMaximum,
            sqrtPriceLimitX96: 0
        });

    amountIn = swapRouter.exactOutputSingle(params);

    if (amountIn < amountInMaximum) {
        linkToken.approve(address(swapRouter), 0);
        linkToken.transfer(address(this), amountInMaximum - amountIn);
    }
}

}

r0qs commented 1 year ago

@ghazali-web as far as I could see, your issue is not a solidity problem but rather a wrong use of it or of hardhat.

I suggest that you take a look at our documentation and the documentation of the tools that you are using, i.e. Hardhat. A better place to post such issues would be the Ethereum stack exchange.

Please, feel free to reopen the issue in case I misunderstood something and you indeed found an issue in the Solidity compiler. Also, if you reopen it, please make sure you properly describe what is the problem, and use the markdown format appropriately for the code snippet. So it will be easier to understand the issue.