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

Issue calling setProvider #2786

Closed
eggplantzzz opened this issue May 6, 2019 · 13 comments
Closed

Issue calling setProvider #2786

eggplantzzz opened this issue May 6, 2019 · 13 comments
Labels
2.x 2.0 related issues Discussion

Comments

@eggplantzzz
Copy link
Contributor

When creating a Web3 instance and then attempting to call setProvider with a valid provider, a TypeError occurs.

Expected behavior

Calling setProvider should successfully set the provider without an error.

Actual behavior

A TypeError occurs (see error logs below).

Steps to reproduce the behavior

const Web3 = require("web3");      
const web3 = new Web3();
web3.setProvider(new Web3.providers.HttpProvider(someValidUrl));

Error Logs

TypeError: Cannot read property 'clearSubscriptions' of undefined
    at Web3.clearSubscriptions (/Users/name/projects/truffle/node_modules/web3-core/dist/web3-core.cjs.js:64:39)
    at Web3.setProvider (/Users/name/projects/truffle/node_modules/web3-core/dist/web3-core.cjs.js:44:14)
    at Web3.setProvider (/Users/name/projects/truffle/node_modules/web3/dist/web3.cjs.js:41:73)

Versions

  • web3.js: 1.0.0-beta.54
  • nodejs: 8.16.0
@nivida
Copy link
Contributor

nivida commented May 6, 2019

I've couldn't reproduce this issue with the provided example. Could you reference the complete code as GitHub repository or gist file?

@eggplantzzz
Copy link
Contributor Author

eggplantzzz commented May 7, 2019

So to give a slightly more verbose version of the above steps...so I start Ganache running at port 9545 and do the following:

const Web3 = require("web3");
const provider = new Web3.providers.HttpProvider("http://localhost:9545");
const web3 = new Web3();
web3.setProvider(provider);

It makes sense when looking at web3-core.cjs.js here. If a web3 instance does not have a provider it will throw when you try to set it because it calls clearSubscriptions in web3-core.cjs.js. This method checks this.currentProvider.clearSubscriptions, but this.currentProvider is undefined and will throw.

@nivida
Copy link
Contributor

nivida commented May 7, 2019

Is it not also possible to initiate Web3 with a non-existing URL?

const Web3 = require("web3");
const provider = new Web3.providers.HttpProvider("http://localhost:9545");
const web3 = new Web3('http://');
web3.setProvider(provider);

@eggplantzzz
Copy link
Contributor Author

eggplantzzz commented May 8, 2019

Well yes, I assume that would work...but isn't that a bit janky?

@nivida
Copy link
Contributor

nivida commented May 8, 2019

Yes, this is true. It is probably better if I update the setProvider method. I've checked the truffle-contract package and truffle does copy the methods from the Web3.js Contract class to a truffle related class. I think this could give some other problems. Is it possible for truffle to create a list and we could go through the list?

@eggplantzzz
Copy link
Contributor Author

Sure! What kind of a list would you like to compile exactly? Right now I am not too sure that I am completely aware of all of the problems that Truffle currently has with the newest versions of Web3.
I know there is some work that we need to do to adapt to the new provider standard for sure. I'll try to get as much of the work that I know needs to be done and then take another look.

@eggplantzzz
Copy link
Contributor Author

eggplantzzz commented May 9, 2019

I also know the "strings to BN" change is going to be a big deal for Truffle since that is a breaking change and we just released a major version not too long ago.
In other words, I don't think we can upgrade to the latest version without that string functionality. Maybe we could talk about some possible solutions?

@vilaleix
Copy link

Hello!
I'm having the same issue...

-web3@1.0.0-beta.55
-node v12.2.0
-truffle-contract@4.0.1

Any news??

@nivida
Copy link
Contributor

nivida commented May 10, 2019

Sure! What kind of a list would you like to compile exactly?

I think about a list where all the problems of truffle are listed would support the discussion :)

I know there is some work that we need to do to adapt to the new provider standard for sure.

The EIP-1193 isn't finalized and the CustomProvider fallback was created with the given interface from truffle. Was there anything missing in the given interface?

I think the biggest problem is that truffle does copy methods from the Web3 contract class to a custom truffle object with no relation to a Web3 API. Truffle could wrap these methods or it could use the ES6 proxy for providing the same truffle-contract API (adapter pattern).

I also know the "strings to BN" change is going to be a big deal for Truffle since that is a breaking change and we just released a major version not too long ago.

Is it possible to have deeper insights? (code references etc.)

Edit: Adapter Embark

@eggplantzzz
Copy link
Contributor Author

Let me talk with the Truffle team and see what information we can put together. To be honest I'm not sure how complete our knowledge is about what is currently incompatible with the latest versions of Web3.

@shikharfb
Copy link

shikharfb commented May 20, 2019

What do you suggest if I'm using Ganache as a provider?

var Web3 = require("web3");
var web3 = new Web3();
web3.setProvider(ganache.provider());

@FabioBonfiglio
Copy link

Hello,

I encountered the same issue. I'm only using Ganache as provider, running it on a server in my local network, and I was launching my node instance with this (not a Truffle project) :

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider("http://10.0.0.42:7545"));

Then I got the following error :

path/to/my/project/node_modules/web3-core/dist/web3-core.cjs.js:64
      if (typeof this.currentProvider.clearSubscriptions !== 'undefined' && this.currentProvider.subscriptions.length > 0) {
                                      ^

TypeError: Cannot read property 'clearSubscriptions' of undefined
...

But when I do it this way, it works :

const Web3 = require('web3');
const ethprovider = new Web3.providers.HttpProvider("http://10.0.0.42:7545");
const web3 = new Web3('http://');
web3.setProvider(ethprovider);

nodejs v10.15.3
web3js 1.0.0-beta.55

@nivida
Copy link
Contributor

nivida commented Oct 13, 2019

This got fixed in the 2.x branch and 1.x doesn't have that issue.

@nivida nivida closed this as completed Oct 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.x 2.0 related issues Discussion
Projects
None yet
Development

No branches or pull requests

5 participants