Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Uncaught TypeError: o.data.substring is not a function" calling a contract function reverting #4454

Closed
1 task done
nicos99 opened this issue Oct 11, 2021 · 12 comments
Closed
1 task done
Labels
1.x 1.0 related issues Bug Addressing a bug P3 Low severity bugs

Comments

@nicos99
Copy link
Contributor

nicos99 commented Oct 11, 2021

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

I get an "Uncaught TypeError: o.data.substring is not a function" In the console of my internet browser when I call() a contract function that revert due to an require not satified, using handleRevert = true and web3.js version >= 1.2.10 (ok until 1.2.9, still nok with 1.5.2).

For instance rocketContract.methods.SetSpeed(101).call() with this extract of the contract :

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

In addition I can not intercept the exception in my code.

Complete call stack with web3.js 1.5.2 :

Uncaught TypeError: o.data.substring is not a function
    o index.js:630
    n index.js:172
    onreadystatechange index.js:111
    send index.js:99
    send index.js:187
    h index.js:763
    n index.js:797
    _executeMethod index.js:945
    CallSetSpeed call-revert-bug-v2.1.html:121
    onclick call-revert-bug-v2.1.html:1

Expected Behavior

I can catch an exception and get the revert raison. Until version 1.2.9 we get Returned error: VM Exception while processing transaction: revert speed invalid using this code :

rocketContract.methods.SetSpeed(speed).call()
	.then(txt => {
		alert("Call SetSpeed() ok, send is likely to succeed");
	})
	.catch(err => {
		console.error(err);
    		alert("Call SetSpeed() error ! send is likely to failed\n" + err.message);
  	});

Steps to Reproduce

  1. run a local Ganache instance,
  2. copy this web page on a webserver: call-revert-bug-v2.1.html ,
  3. got to the webpage and open your browser's console (F12),
  4. clic on the "Init" button,
  5. set speed above 100 in the input field,
  6. clic on the "Call SetSpedd() !" button.

Web3.js Version

1.5.2

Environment

  • Operating System: Windows 10 Famille, version 20H2, build 19042.1237
  • Browser: Firefox 93.0 (64 bits)
  • Node.js Version: 7.5.3
  • Ganache CLI: v6.12.2 (ganache-core: 2.13.2)

Anything Else?

There is no issue using web3 as node library. You can find this test script testSpeed.js that demonstrates it (download the file into an empty folder, npm install web3@1.5.2 in the that folder and then mocha --timeout 999999 testSpeed having a live locale Ganache instance).

I discovered this bug months ago but I thought it was the #3742.

@nicos99 nicos99 added the Bug Addressing a bug label Oct 11, 2021
@nazarhussain
Copy link
Contributor

nazarhussain commented Oct 14, 2021

@nicos99 As I explained earlier in issue #3742 this error does not relate to web3. The standard implementation of Eth Client is geth and I tested the web3 with it for the similar contract you shared and it works fine. During that investigation I found the problem which was caused by hardhat provider.

Similarly this issue seems related to Ganache and not web3. We will try to investigate it further but that may take some time. I suggest to you as well to look into Ganache for probable issue.

@nazarhussain nazarhussain added Investigate P3 Low severity bugs labels Oct 14, 2021
@FanM
Copy link

FanM commented Oct 15, 2021

I hit the same error on Binance Test Chain. After some debugging, I found the o.data returned was somehow an object, not a string:
Screen Shot 2021-10-15 at 2 53 00 PM
I think maybe the code on https://github.com/ChainSafe/web3.js/blob/40974e9fe9a8f9075fcbb4c002430c6480d83031/packages/web3-core-method/src/index.js#L644 should check the type first.

@nicos99
Copy link
Contributor Author

nicos99 commented Oct 15, 2021

@nazarhussain I opened this ticket to explain precisely my problem (the #3742 having a short description) and to give a simpler code example (without relying on MetaMask as you suggested to me).

But you are right, the wrongs are shared because the bug does not appear when using Geth instead of Ganache (tested with 1.10.1-stable). So one might think that Ganache does not respect the protocol and Web3 (as the final package that embedded some third party libraries if I follow you) does not protect himself adequately.

I will probably update the description when I test with other clients (latest Ganache, Ropsten, Main net) to help determine the impact. Thank you for your expertise.

@raf202
Copy link

raf202 commented Oct 25, 2021

The problem is not Ganache, I'm getting the same problem with Polygon testnet (not using Ganache).
@FanM is probably right

@gitalvininfo
Copy link

@sigmaxf @FanM did you manage to solve this issue? Currently I'm having the same as yours.

@gitalvininfo
Copy link

I notice using old version of web3 and instantiating the smart contract in frontend fix the issue but I want to use the latest version of web3 and latest instantiation of smart contract.

@gitalvininfo
Copy link

I deployed my smart contract using the node of Moralis but still the error still exist.

@FanM
Copy link

FanM commented Nov 10, 2021

@sigmaxf @FanM did you manage to solve this issue? Currently I'm having the same as yours.

I added a type check to work around this.

@gitalvininfo
Copy link

@FanM Thanks
However I'm using a CDN version of web3 with a version of release candidate 1.6.1. Anyways I try to dig it up hoping I would fix this soon.

I look in your work around, clever solution. Thank you.

@gitalvininfo
Copy link

I think my issue for now is fixed, my issue in "o.data.substring is not a function" triggers in my ERC20 token contract(MyToken.sol). I change the order of loading of (MyToken.sol) in frontend as the first smart contract that loads and I don't know what happen but the error is gone. Previously (MyToken.sol) is loaded second before my other contract(TokenTimeLock.sol) in asynchronous way.

So that is my case.

@github-actions
Copy link

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.

@github-actions github-actions bot added the Stale Has not received enough activity label Jan 10, 2022
nicos99 added a commit to nicos99/web3.js that referenced this issue Mar 26, 2023
…)' to manage revert embedded error details got from some providers like MetaMask (related to issue web3#4454)
nicos99 added a commit to nicos99/web3.js that referenced this issue Mar 27, 2023
@nicos99
Copy link
Contributor Author

nicos99 commented Mar 27, 2023

here are two scenarios that show the structure of the data of the error object returned via MetaMask when revert, the basis of my work to correct:
RPC Error
RPC Error 2

nicos99 added a commit to nicos99/web3.js that referenced this issue Mar 28, 2023
…)' to manage revert embedded error details got from some providers like MetaMask (related to issue web3#4454)
nicos99 added a commit to nicos99/web3.js that referenced this issue Mar 28, 2023
@spacesailor24 spacesailor24 reopened this Mar 31, 2023
@spacesailor24 spacesailor24 added 1.x 1.0 related issues and removed Stale Has not received enough activity Investigate labels Mar 31, 2023
nicos99 added a commit to nicos99/web3.js that referenced this issue Apr 7, 2023
…sendTxCallback(err, result)' to better manage revert call reason from some providers like MetaMask (related to issue web3#4454)
@jdevcs jdevcs mentioned this issue May 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.x 1.0 related issues Bug Addressing a bug P3 Low severity bugs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants