I have been trying to swap two tokens using Uniswap Routers. I keep getting:

Error: Returned error: VM execution error.

I have checked my code many times, have no idea what the issue could be.

async function getValueAtAddress(host, deployedContractAbi, deployedContractAddress) { 
const amountIn = web3.utils.toWei('0.01');
const web31 = new web3(host);  
const jackycoin = new web31.eth.Contract(deployedContractAbi, deployedContractAddress);
const bal = await jackycoin.methods.balanceOf('0xE6082a3FfdceBd999a34c32FDb2630a9EeaeCe6A').call();
const weth = new web31.eth.Contract(wethABI, WETH_ADDRESS);
const router = new web31.eth.Contract(importedRouterABI.abi, ROUTER_ADDRESS);

await weth.methods.deposit().call(); //{value: amountIn}
await weth.methods.approve(ROUTER_ADDRESS, amountIn).call();

const amountsOut = await router.methods.getAmountsOut(amountIn, [WETH_ADDRESS, JACKYCOIN_ADDRESS]).call();
const amountOutMin = web3.utils.toBN(amountsOut[1])

const balanceJackyCoinBefore = await jackycoin.methods.balanceOf('0xE6082a3FfdceBd999a34c32FDb2630a9EeaeCe6A').call();
const params = { 
  "amountIn": amountIn, 
  "amountOutMin": amountOutMin, 
  "to": '0xE6082a3FfdceBd999a34c32FDb2630a9EeaeCe6A',
  "deadline": Math.floor((Date.now() / 1000)) + 60 * 10
  // "value": BigNumber.from(0)

await router.methods.swapExactTokensForTokens(...Object.values(params)).call();


The full error trace:

Error: Returned error: VM execution error.
at Object.ErrorResponse (D:\OneDrive\Documents\Degree CS\Year 3\Individual Project\Website\TestingWeb\coursework\node_modules\web3-core-helpers\lib\errors.js:28:19)        
at D:\OneDrive\Documents\Degree CS\Year 3\Individual Project\Website\TestingWeb\coursework\node_modules\web3-core-requestmanager\lib\index.js:302:36
at XMLHttpRequest.request.onreadystatechange (D:\OneDrive\Documents\Degree CS\Year 3\Individual Project\Website\TestingWeb\coursework\node_modules\web3-providers-http\lib\index.js:98:13)
at XMLHttpRequestEventTarget.dispatchEvent (D:\OneDrive\Documents\Degree CS\Year 3\Individual Project\Website\TestingWeb\coursework\node_modules\xhr2-cookies\dist\xml-http-request-event-target.js:34:22)
at XMLHttpRequest._setReadyState (D:\OneDrive\Documents\Degree CS\Year 3\Individual Project\Website\TestingWeb\coursework\node_modules\xhr2-cookies\dist\xml-http-request.js:208:14)
at XMLHttpRequest._onHttpResponseEnd (D:\OneDrive\Documents\Degree CS\Year 3\Individual Project\Website\TestingWeb\coursework\node_modules\xhr2-cookies\dist\xml-http-request.js:318:14)
at IncomingMessage.<anonymous> (D:\OneDrive\Documents\Degree CS\Year 3\Individual Project\Website\TestingWeb\coursework\node_modules\xhr2-cookies\dist\xml-http-request.js:289:61)
at IncomingMessage.emit (node:events:402:35)
at endReadableNT (node:internal/streams/readable:1343:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
data: 'Reverted 0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000245472616e7366657248656c7065723a205452414e534645525f46524f4d5f4641494c454400000000000000000000000000000000000000000000000000000000'

←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m ←[0m←[35mcode←[0m ELIFECYCLE ←[0m←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m ←[0m←[35merrno←[0m 1 ←[0m←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m←[35m←[0m [email protected] start:node app.js ←[0m←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m←[35m←[0m Exit status 1 ←[0m←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m←[35m←[0m ←[0m←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m←[35m←[0m Failed at the [email protected] start script. ←[0m←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m←[35m←[0m This is probably not a problem with npm. There is likely additional logging output above. ←[0m ←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m←[35m←[0m A complete log of this run can be found in: ←[0m←[37;40mnpm←[0m ←[0m←[31;40mERR!←[0m←[35m←[0m C:\Users\jacky\AppData\Roaming\npm-cache\_logs\2022-03-13T12_36_51_984Z-debug.log ←[0m

The tokens used have all been deployed to the Kovan network and work with no issues.Does anybody know what the issue might be?

1 Answer 1


If you decode the string, it turns out to be "TransferHelper: TRANSFER_FROM_FAILED" which makes me think that you have not approved the router to spend your tokens. You need to approve the router before calling swap on it.

  • thank you @Meet for the help. Don't the lines: await weth.methods.deposit().call(); //{value: amountIn} and await weth.methods.approve(ROUTER_ADDRESS, amountIn).call(); Approve the router address? Would you suggest anything different?
    – Btadford
    Mar 14, 2022 at 10:24
  • @Btadford it does actually, didn't notice that, in that case I'd try and make sure there's enough balance for the swap requested amount i.e. amountIn
    – Meet
    Mar 14, 2022 at 11:35
  • I fetch this from Uniswap so I'm pretty sure its correct, have also tried many different alternations of it. So I'm not sure that is the issue. I am running this in my server.js using VueJS. Would that cause any issues? @Meet
    – Btadford
    Mar 15, 2022 at 10:11
  • @Btadford check await weth.methods.balanceOf(signer_address).call() and see if it is greater than 10 ** 16 assuming toWei('0.02') takes input in eth units
    – Meet
    Mar 15, 2022 at 14:28

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.