最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Transaction reverted: function returned an unexpected amount of data while testing with ether.js (uniswapv2router.s

programmeradmin4浏览0评论

im testing UNISWAP_V2_ROUTER.swapExactTokensForTokens using ether.js and this line: await swapInstances.connect(accounts[0]).swap(tokenIn, tokenOut, amountIn, amountOutMin, to); cause this error : Transaction reverted: function returned an unexpected amount of data.

why?

unit test :

 it("should be able to swap tokens", async function () {
            accounts = await ethers.getSigners()
            to = await accounts[1].getAddress();
            const Swap = await ethers.getContractFactory("Swap", accounts[0]);
            const swapInstances = await Swap.deploy();
            const LocandaToken = await ethers.getContractFactory("LocandaToken", accounts[0]); //ERC20
            const locandaToken = await LocandaToken.deploy();
            const RubiconPoolToken = await ethers.getContractFactory("RubiconPoolToken", accounts[1]); //ERC20
            const rubiconPoolToken = await RubiconPoolToken.deploy();
            tokenIn = locandaToken.address;
            tokenOut = rubiconPoolToken.address;

            await locandaToken.connect(accounts[0]).transfer(swapInstances.address, amountIn);
            await rubiconPoolToken.connect(accounts[1]).transfer(swapInstances.address, amountIn);

            
            const ethBalance = await ethers.provider.getBalance(accounts[0].address);
            console.log("eth balance" + ethBalance);

            await locandaToken.connect(accounts[0]).approve(swapInstances.address, amountIn)
            const test = await swapInstances.connect(accounts[0]).swap(tokenIn, tokenOut, amountIn, amountOutMin, to);
        })

swap function :

function swap(
        address _tokenIn,
        address _tokenOut,
        uint256 _amountIn,
        uint256 _amountOutMin,
        address _to // address where sending the tokenout
    ) external {
        IERC20(_tokenIn).transferFrom(msg.sender, address(this), _amountIn); // transfer from user wallet to this contract
        IERC20(_tokenIn).approve(UNISWAP_V2_ROUTER, _amountIn); // aprove the router to spend _tokenin
        address[] memory path; //represents the path/flow of the swap

        if (_tokenIn == WETH || _tokenOut == WETH) {
            path = new address[](2);
            path[0] = _tokenIn;
            path[1] = _tokenOut;
        } else {
            path = new address[](3);
            path[0] = _tokenIn;
            path[1] = WETH;
            path[2] = _tokenOut;
        }

        IUniswapV2Router(UNISWAP_V2_ROUTER).swapExactTokensForTokens(
            _amountIn,
            _amountOutMin,
            path,
            _to,
            block.timestamp
        );

     
    }

im testing UNISWAP_V2_ROUTER.swapExactTokensForTokens using ether.js and this line: await swapInstances.connect(accounts[0]).swap(tokenIn, tokenOut, amountIn, amountOutMin, to); cause this error : Transaction reverted: function returned an unexpected amount of data.

why?

unit test :

 it("should be able to swap tokens", async function () {
            accounts = await ethers.getSigners()
            to = await accounts[1].getAddress();
            const Swap = await ethers.getContractFactory("Swap", accounts[0]);
            const swapInstances = await Swap.deploy();
            const LocandaToken = await ethers.getContractFactory("LocandaToken", accounts[0]); //ERC20
            const locandaToken = await LocandaToken.deploy();
            const RubiconPoolToken = await ethers.getContractFactory("RubiconPoolToken", accounts[1]); //ERC20
            const rubiconPoolToken = await RubiconPoolToken.deploy();
            tokenIn = locandaToken.address;
            tokenOut = rubiconPoolToken.address;

            await locandaToken.connect(accounts[0]).transfer(swapInstances.address, amountIn);
            await rubiconPoolToken.connect(accounts[1]).transfer(swapInstances.address, amountIn);

            
            const ethBalance = await ethers.provider.getBalance(accounts[0].address);
            console.log("eth balance" + ethBalance);

            await locandaToken.connect(accounts[0]).approve(swapInstances.address, amountIn)
            const test = await swapInstances.connect(accounts[0]).swap(tokenIn, tokenOut, amountIn, amountOutMin, to);
        })

swap function :

function swap(
        address _tokenIn,
        address _tokenOut,
        uint256 _amountIn,
        uint256 _amountOutMin,
        address _to // address where sending the tokenout
    ) external {
        IERC20(_tokenIn).transferFrom(msg.sender, address(this), _amountIn); // transfer from user wallet to this contract
        IERC20(_tokenIn).approve(UNISWAP_V2_ROUTER, _amountIn); // aprove the router to spend _tokenin
        address[] memory path; //represents the path/flow of the swap

        if (_tokenIn == WETH || _tokenOut == WETH) {
            path = new address[](2);
            path[0] = _tokenIn;
            path[1] = _tokenOut;
        } else {
            path = new address[](3);
            path[0] = _tokenIn;
            path[1] = WETH;
            path[2] = _tokenOut;
        }

        IUniswapV2Router(UNISWAP_V2_ROUTER).swapExactTokensForTokens(
            _amountIn,
            _amountOutMin,
            path,
            _to,
            block.timestamp
        );

     
    }
Share Improve this question asked Aug 17, 2022 at 22:42 MattMatt 631 silver badge6 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

I also got this error when I used Router1 Address instead of Router2 address.

After use correct router address, it's solved.

You are deploying two new tokens. Hence, there are no uniswap pools available for those tokens yet.

I think you need to create a corresponding pair calling the uniswapV2Factory and then add liquidity: https://docs.uniswap/protocol/V2/reference/smart-contracts/factory#createpair

Second, where are you setting your amountOutMin?

third, Why are you transferring tokens to your swap contract?

await locandaToken.connect(accounts[0]).transfer(swapInstances.address, amountIn);

await rubiconPoolToken.connect(accounts[1]).transfer(swapInstances.address, amountIn);

This error may be happened with IERC20 interface that you're using. For example, transfer and transferFrom functions return (bool) in IERC20 interface generally.

function transfer(address to, uint256 amount) external returns (bool);
function transferFrom(address from, address to, uint256 amount) external returns (bool);

But the tokens or external routers you're interacting might be having interfaces with non-returning transfer or transferFrom functions. Considering USDT token, transfer and transferFrom functions don't have any return value, but if you're calling these functions with your custom IERC20 tranfer and transferFrom functions with bool return value, then you probably will get this error "Transaction reverted: function returned an unexpected amount of data".

发布评论

评论列表(0)

  1. 暂无评论