0

I have created a test project for the purpose of this question for testing a simple swap using Uniswap Router. My aim is to simply test the transaction before it is actually sent, so I'm trying to use the .callStatic() method

For the sake of this question I created a simple repo, you can install it with npm i, replace the Infura key and put any random private key you like and then just run node index.js https://github.com/niko534/callStaticTest

If you cloned the repo and tried to run it, you will see that the transaction is executing well, you will receive the recipe as you should!

My problem comes when I'm trying to replace the same transaction with .callStatic, on line 78 replace

    const tx = await router.swapExactETHForTokens(
        amountOutMin,
        [weth.address, floki.address],
        addresses.recipient,
        deadline,
        {
            value: amountIn,
            gasLimit: 1000000
        }
    );

with:

    const tx = await router.callStatic.swapExactETHForTokens(
        amountOutMin,
        [weth.address, floki.address],
        addresses.recipient,
        deadline,
        {
            value: amountIn,
            gasLimit: 1000000
        }
    );

I'm getting an error:

Error: missing revert data in call exception (error={"reason":"processing response error","code":"SERVER_ERROR","body":"{\"id\":46,\"jsonrpc\":\"2.0\",\"error\":{\"message\":\"VM Exception while processing transaction: revert\",\"code\":-32000,\"data\":{\"0x95d2182e8a13b89b535b2b61e5f6b1bc56ef3f9505773d353cb64116d38cdb96\":{\"error\":\"revert\",\"program_counter\":9406,\"return\":\"0x\"},\"stack\":\"c: VM Exception while processing transaction: revert\\n    at Function.c.fromResults (C:\\\\Users\\\\USER\\\\Desktop\\\\callStaticTest\\\\node_modules\\\\ganache-cli\\\\build\\\\ganache-core.node.cli.js:4:192416)\\n    at C:\\\\Users\\\\USER\\\\Desktop\\\\callStaticTest\\\\node_modules\\\\ganache-cli\\\\build\\\\ganache-core.node.cli.js:42:50402\",\"name\":\"c\"}}}","error":{"code":-32000,"data":{"0x95d2182e8a13b89b535b2b61e5f6b1bc56ef3f9505773d353cb64116d38cdb96":{"error":"revert","program_counter":9406,"return":"0x"},"stack":"c: VM Exception while processing transaction: revert\n    at Function.c.fromResults (C:\\Users\\USER\\Desktop\\callStaticTest\\node_modules\\ganache-cli\\build\\ganache-core.node.cli.js:4:192416)\n    at C:\\Users\\USER\\Desktop\\callStaticTest\\node_modules\\ganache-cli\\build\\ganache-core.node.cli.js:42:50402","name":"c"}},"requestBody":"{\"method\":\"eth_call\",\"params\":[{\"gas\":\"0xf4240\",\"value\":\"0x8ac7230489e80000\",\"from\":\"0x33850a0605cc68b139ba15dd68fa4a2623d3e992\",\"to\":\"0x7a250d5630b4cf539739df2c5dacb4c659f2488d\",\"data\":\"0x7ff36ab50000000000000000000000000000000000000000000000000441e05289161917000000000000000000000000000000000000000000000000000000000000008000000000000000000000000033850a0605cc68b139ba15dd68fa4a2623d3e9920000000000000000000000000000000000000000000000000000000061cbc8410000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000043f11c02439e2736800433b4594994bd43cd066d\"},\"latest\"],\"id\":46,\"jsonrpc\":\"2.0\"}","requestMethod":"POST","url":"http://localhost:8545"}, data="0x", code=CALL_EXCEPTION, version=providers/5.5.1)
    at Logger.makeError (C:\Users\USER\Desktop\callStaticTest\node_modules\@ethersproject\logger\src.ts\index.ts:225:28)
    at Logger.throwError (C:\Users\USER\Desktop\callStaticTest\node_modules\@ethersproject\logger\src.ts\index.ts:237:20)
    at checkError (C:\Users\USER\Desktop\callStaticTest\node_modules\@ethersproject\providers\src.ts\json-rpc-provider.ts:34:16)        
    at JsonRpcProvider.<anonymous> (C:\Users\USER\Desktop\callStaticTest\node_modules\@ethersproject\providers\src.ts\json-rpc-provider.ts:562:20)
    at step (C:\Users\USER\Desktop\callStaticTest\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:48:23)
    at Object.throw (C:\Users\USER\Desktop\callStaticTest\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:29:53)
    at rejected (C:\Users\USER\Desktop\callStaticTest\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:21:65)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

this is the implementation for the router function I'm trying to interact with:

    function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
        external
        virtual
        override
        payable
        ensure(deadline)
        returns (uint[] memory amounts)
    {
        require(path[0] == WETH, 'UniswapV2Router: INVALID_PATH');
        amounts = UniswapV2Library.getAmountsOut(factory, msg.value, path);
        require(amounts[amounts.length - 1] >= amountOutMin, 'UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT');
        IWETH(WETH).deposit{value: amounts[0]}();
        assert(IWETH(WETH).transfer(UniswapV2Library.pairFor(factory, path[0], path[1]), amounts[0]));
        _swap(amounts, path, to);
    }

link to source code: https://etherscan.io/address/0x7a250d5630b4cf539739df2c5dacb4c659f2488d#code

I have tried to run this code with a different contract and If I try to call a simple approve on the floki erc20 contract for example, it works! Any ideas on what the reason could be for that error?

5
  • Hmmm weird, if I run the query directly against infura it works, so it seems to be an issue with the ganache fork.
    – Richard
    Dec 29, 2021 at 6:25
  • Yep, that's what I thought, any suggestion? (other then switching to hardhat xD) Dec 29, 2021 at 7:22
  • I played around with it for 1h without any result :/ maybe it is worth opening an issue on the ganache repo for this, as it seems very unexpected. Edit: I tried using a local db with Ganache and also using the Ganache provider directly instead of the server, but neither made a change. I am assuming that it triggers an assert somewhere, but not sure why. If you set the amount to 0 you can see that it can trigger the method successfully, as it reverts with a proper error message, but it cannot fully perform the call on it, neither via ethers nor a direct rpc call.
    – Richard
    Dec 29, 2021 at 7:24
  • If I use ETH testnet , will 'callstatic' work ?
    – Jack
    May 25, 2022 at 6:13
  • Ethers.js's callStatic uses eth_call internally. And it should work with any eth testnet. May 25, 2022 at 12:47

1 Answer 1

2

It seems like it was a problem with the ganache-CLI fork updating to [email protected], solved the issue.

Release notes and usage: https://github.com/trufflesuite/ganache/releases/tag/ganache%407.0.0-beta.2

link to dissociation: https://github.com/trufflesuite/ganache/discussions/1933

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.