web3 / web3.js

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.
https://web3js.org/
Other
19.38k stars 4.97k forks source link

Uncaught TypeError: err.data.substring is not a function #3742

Closed gabmontes closed 3 years ago

gabmontes commented 4 years ago

The following line throws an Uncaught TypeError:

https://github.com/ethereum/web3.js/blob/4615f2d35c455bd81cb04f0ab8d8d2c0b140e33a/packages/web3-core-method/src/index.js#L630

The error is only thrown when handleRevert is activated in the contract and the transaction reverts. In that case err.data is an object with this format: {name, stack, 0x...: {error, program_counter, return} }. If handleRevert is false, then err.data is a string and it is properly parsed.

Using Geth 1.9, ganache-cli@6.8.2 and web3@1.3.0.

spacesailor24 commented 4 years ago

Hi there, the bug you're describing makes sense, do you have example code we can use to debug?

Thank you for reporting this!

github-actions[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

danielspk commented 3 years ago

Sorry but this bug was not fixed

nicos99 commented 3 years ago

The bug seems to have been introduced on version 1.2.10 and still exist on 1.3.3. You can find here some web code to reproduce the issue.

TbLtzk commented 3 years ago

Seeing the same issue. In our case it occurs when we provide an rpc url that

  1. has a geth client running
  2. does not expose rpc interface
ivarcontessotto commented 3 years ago

Encountered this issue as well and had to turn the 'handleRevert' property on the web3 provider to false like @gabmontes meantioned. I would like to use the revert reason feature but this error crashes the whole application and I was unable to catch it with a try-catch around 'await myMethod.send(...)'.

github-actions[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

gabmontes commented 3 years ago

Hey bot, do not close this. The bug is not resolved yet.

anycryptodeveloper commented 3 years ago

The bug is not resolved yet.

rayyan808 commented 3 years ago

Hello, encountering the same bug on latest web3, specifically in the transaction callback when calling a uint256 value from the contract:

 submitVote = async (e) => {
     e.preventDefault();
     if(this.state.proofGenerated && this.state.proof !== null && this.state.candidateID !== null){
       let result = await Election.methods
       .publicKey().call({from: this.state.accountList[this.state.account], gas: 40000}).catch((err) => { console.log("Error calling publicKey from contract: " + err) });
       const json = JSONbig.parse(result);
       const publicKey = new paillierBigint.PublicKey(json.n, json.g);
       let voteValue = publicKey.encrypt(this.state.candidateID);
       console.log("voteValue: " + voteValue + "publicKey: " + publicKey + "")
         await Election.methods
          .submitVote(this.state.proof.proof, this.state.proof.inputs, voteValue)
          .send({ from: this.state.accountList[this.state.account], gas: 400000 }, (receipt) => {
            /* Callback from transaction*/
            console.log(receipt);
          }).catch((err) => { console.log("Error apply function submitVote: " + err); });

          console.log("End of Conduct\n");
        }
    }

Uncaught TypeError: err.data.substring is not a function at sendTxCallback (index.js:510) at Object.callback (index.js:303) at index.js:114 at Array.forEach () at WebsocketProvider.push../node_modules/web3-providers-ws/lib/index.js.WebsocketProvider._onMessage (index.js:102)

dougbtv commented 3 years ago

Just another data point, I wound up encountering this while developing in truffle, and I simplified a contract method to:

    function faker(bytes3 _fake) public view returns(bytes3) {
        return (_fake);
    }

And then I try to execute that function and I get...

truffle(develop)> let instance = await MyContract.deployed()
undefined
truffle(develop)> const paintdata = await instance.faker([1,2,3])
Uncaught TypeError: param.substring is not a function
    at evalmachine.<anonymous>:1:18
    at evalmachine.<anonymous>:2:49
    at sigintHandlersWrap (vm.js:273:12)
    at Script.runInContext (vm.js:140:14)
    at runScript (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/console.js:270:1)
    at Console.interpret (/usr/local/lib/node_modules/truffle/build/webpack:/packages/core/lib/console.js:285:1)
    at bound (domain.js:413:15)
    at REPLServer.runBound [as eval] (domain.js:424:12)
    at REPLServer.onLine (repl.js:817:10)
    at REPLServer.emit (events.js:315:20)
    at REPLServer.EventEmitter.emit (domain.js:467:12)
    at REPLServer.Interface._onLine (readline.js:337:10)
    at REPLServer.Interface._line (readline.js:666:8)
    at REPLServer.Interface._ttyWrite (readline.js:1010:14) {
  hijackedStack: 'TypeError: param.substring is not a function\n' +
    '    at ABICoder.formatParam (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/web3-eth-abi/lib/index.js:239:1)\n' +
    '    at /usr/local/lib/node_modules/truffle/build/webpack:/node_modules/web3-eth-abi/lib/index.js:100:1\n' +
    '    at Array.map (<anonymous>)\n' +
    '    at ABICoder.encodeParameters (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/web3-eth-abi/lib/index.js:94:1)\n' +
    '    at /usr/local/lib/node_modules/truffle/build/webpack:/node_modules/web3-eth/node_modules/web3-eth-contract/lib/index.js:439:1\n' +
    '    at Array.map (<anonymous>)\n' +
    '    at Object._encodeMethodABI (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/web3-eth/node_modules/web3-eth-contract/lib/index.js:438:6)\n' +
    '    at Object._processExecuteArguments (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/web3-eth/node_modules/web3-eth-contract/lib/index.js:701:1)\n' +
    '    at Object._executeMethod (/usr/local/lib/node_modules/truffle/build/webpack:/node_modules/web3-eth/node_modules/web3-eth-contract/lib/index.js:720:1)\n' +
    '    at /usr/local/lib/node_modules/truffle/build/webpack:/packages/contract/lib/execute.js:143:1\n' +
    '    at processTicksAndRejections (internal/process/task_queues.js:93:5)'
}
rayyan808 commented 3 years ago

Bug still persists

weidaix commented 3 years ago

Bug still present. Any ETA on resolution?

musnit commented 3 years ago

bump

Mereep commented 3 years ago

Getting this error when running truffle test (web3@1.3.4).

itseasy21 commented 3 years ago

Bump! Getting this error.

Log:

TypeError: param.substring is not a function
    at ABICoder.formatParam (D:\Blockchain-WorkSpace\smart-contract-test\node_modules\web3-eth-abi\lib\index.js:248:35)
    at D:\Blockchain-WorkSpace\smart-contract-test\node_modules\web3-eth-abi\lib\index.js:99:22
    at Array.map (<anonymous>)
    at ABICoder.encodeParameters (D:\Blockchain-WorkSpace\smart-contract-test\node_modules\web3-eth-abi\lib\index.js:93:21)
    at D:\Blockchain-WorkSpace\smart-contract-test\node_modules\web3-eth-contract\lib\index.js:438:20
    at Array.map (<anonymous>)
    at Object._encodeMethodABI (D:\Blockchain-WorkSpace\smart-contract-test\node_modules\web3-eth-contract\lib\index.js:437:8)
    at mintNFT (D:\Blockchain-WorkSpace\smart-contract-test\scripts\mint-nft.js:23:71)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
bitsandtea commented 3 years ago

bump

nazarhussain commented 3 years ago

I am able to reproduce the error with following hardhat script and running against ganache with npx hardhat --network localhost test

contract CallRevert{
    uint speed;

    function Start(uint _speedPercent) public {
        console.log("Start called:", _speedPercent);
        require(_speedPercent <= 100, "speed invalid");
        speed = _speedPercent;
    }
}
const CallRevert = artifacts.require("CallRevert");

describe("Call Error on Revert", function () {
  let accounts;
  let contract; 

  before(async function () {
    accounts = await web3.eth.getAccounts();
    const c = await CallRevert.new();
    contract = new web3.eth.Contract(c.abi, c.address);
  });

  describe("Call Revert", function () {
    it("should throw error when called", async function () {
      contract.handleRevert = true;
      await expect(contract.methods.Start(102).call()).to.be.rejectedWith('speed invalid');
    });

    it("should throw error when sent", async function () {
      contract.handleRevert = true;
      await expect(contract.methods.Start(102).send({from: accounts[0]})).to.be.rejectedWith('speed invalid');
    });
  });
});

The actual error object that appears with Ganahe is.

 {
    stack: 'ProviderError: VM Exception while processing transaction: revert speed invalid\n' +
      '    at HttpProvider.request (/...../)\n' +
      '    at GanacheGasMultiplierProvider.request (/...../)',
    name: 'ProviderError'
  }
nazarhussain commented 3 years ago

The actual response that is sent by the client is:

<     "id": 14,
 <     "jsonrpc": "2.0",
 <     "error": {
 <       "message": "VM Exception while processing transaction: revert speed invalid",
 <       "code": -32000,
 <       "data": {
 <         "0x7d38bce5a39ee329c3f8552f7dc835c7b388d713f8cc033abf08a71a40bae9f9": {
 <           "error": "revert",
 <           "program_counter": 261,
 <           "return": "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d737065656420696e76616c696400000000000000000000000000000000000000",
 <           "reason": "speed invalid"
 <         },
 <         "stack": "c: VM Exception while processing transaction: revert speed invalid\n    at Function.c.fromResults (/Users/nazar/.asdf/installs/nodejs/14.17.5/.npm/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:4:192416)\n    at /Users/nazar/.asdf/installs/nodejs/14.17.5/.npm/lib/node_modules/ganache-cli/build/ganache-core.node.cli.js:42:50402",
 <         "name": "c"
 <       }
 <     }
 <   }
nazarhussain commented 3 years ago

The further investigation leads to the fact that latest version of web3@1.5.2 does not include this bug rather it must be in some dependant libraries as reported above for redhat https://github.com/nomiclabs/hardhat/issues/1910

The following scripts confirms that if we use web3 directly with geth latest version this error can't be reproduced.

Start the geth with following command.

geth --datadir test-chain-dir --dev --http --rpc.txfeecap 0 --allow-insecure-unlock

And use following contract and compile it.

//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

contract CallRevert{
    uint speed;

    function Start(uint _speedPercent) public {
        require(_speedPercent <= 100, "speed invalid");
        speed = _speedPercent;
    }
}

Run following script.

const Web3 = require("web3");
const web3 = new Web3(Web3.givenProvider || "http://localhost:8545");
const CallRevert = require("../artifacts/contracts/CallRevert.sol/CallRevert.json");

(async () => {
  console.log("%%%%% WEB3: ", web3.version);
  const account = web3.eth.accounts.create("my-pass-phrase");
  web3.eth.accounts.wallet.add(account);

  const accounts = await web3.eth.getAccounts();

  await web3.eth.sendTransaction({
    from: accounts[0],
    to: account.address,
    value: web3.utils.toWei("1234", "ether"),
  });

  console.log(
    "%%%%% account balance: ",
    await web3.eth.getBalance(account.address)
  );

  const contract = new web3.eth.Contract(CallRevert.abi, {
    data: CallRevert.bytecode,
  });

  const contractInstance = await contract
    .deploy({
      arguments: [],
    })
    .send({
      from: account.address,
      gas: 1500000,
      gasPrice: "30000000000000",
    });

  console.log("%%%%% contract address: ", contractInstance.options.address);
  contractInstance.handleRevert = true;
  await contractInstance.methods.Start(102).call();
})().catch((e) => {
  console.error("%%%%% catches error: ", e);
});

It will produce following output.

%%% web3:  1.5.2
%%% account balance:  1234000000000000000000
%%% contract address:  0x291b490f1a38d2b3Eed82d2aC1F34d2abd4c819C
%%% catches error:  Error: Your request got reverted with the following reason string: speed invalid

The above output confirms that web3 works fine directly with geth. So problem lies in some other provider or wrapper.

nazarhussain commented 3 years ago

@gabmontes I am closing this issues after above verification. Feel free to open it if you get more traceable data or you can reproduce it with latest version of web3 and geth directly without any third-party library or provider.

nicos99 commented 3 years ago

@nazarhussain thanks for the investigative work but I still see an issue (Uncaught TypeError: o.data.substring is not a function) in context explained in my comment of January 25, even with web3 1.5.2. I will probably open a new bug since the symptom is close but the cause may be different due to the context (for me : ganache <-> metamask & FireFox <-> webpage using web3.min.js)

nazarhussain commented 3 years ago

@nicos99 I am pretty confident that problem is somewhere between Genache or Metamask provider. Try your code directly connecting with the local running node. If you can find more insights and share some script without external providers then feel free to re-open this issue.

nicos99 commented 3 years ago

@nazarhussain ok, I will. However note that it is enough to only change the web3 version to 1.2.9 (with <script src="https://cdn.jsdelivr.net/npm/web3@1.2.9/dist/web3.min.js">) for the js script to catch correctly the revert exception

jesmith0 commented 3 years ago

Same issue here:

Uncaught TypeError: err.data.substring is not a function
      at sendTxCallback (node_modules/web3-core-method/lib/index.js:510:43)
      at Object.callback (node_modules/web3-core-requestmanager/lib/index.js:288:20)
      at /app/node_modules/web3-providers-ws/lib/index.js:114:45
      at Array.forEach (<anonymous>)
      at WebsocketProvider._onMessage (node_modules/web3-providers-ws/lib/index.js:102:69)
      at W3CWebSocket._dispatchEvent [as dispatchEvent] (node_modules/yaeti/lib/EventTarget.js:115:12)
      at W3CWebSocket.onMessage (node_modules/websocket/lib/W3CWebSocket.js:234:14)
      at WebSocketConnection.<anonymous> (node_modules/websocket/lib/W3CWebSocket.js:205:19)
      at WebSocketConnection.processFrame (node_modules/websocket/lib/WebSocketConnection.js:554:26)
      at /app/node_modules/websocket/lib/WebSocketConnection.js:323:40
      at processTicksAndRejections (internal/process/task_queues.js:79:11)

Environment: Ganache CLI v6.12.2 (ganache-core: 2.13.2) web3@1.30

No issues when using web3@1.2.9

ntudor11 commented 2 years ago

Bump, still no fix yet the issue has been closed.

/node_modules/web3-core-method/lib/index.js:518
                    reasonData = err.data.substring(10);
                                          ^
TypeError: err.data.substring is not a function

Code at the specified file & line:

else if (err && err.data) {
   reasonData = err.data.substring(10);
}

Where reasonData seems to be an object, not a string.

Web3 version: 1.6.1

tabakhian commented 2 years ago

iam using "https://bsc-dataseed.binance.org/" and get the error on 1.7.0

   Uncaught TypeError: err.data.substring is not a function
at sendTxCallback (index.js?7789:518:1)
at cb (util.js?3022:689:1)
at callbackifyOnRejected (util.js?3022:666:1)
at eval (process.js?4362:5:1)

when i set handleRevert to true metamask goes crazy and make errors every second right after i send the contract method MetaMask - RPC Error: Internal JSON-RPC error.

jspruance commented 2 years ago

I am experiencing the same behavior. I see this is now closed - what was the resolution? Thanks.