Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Add support of multiple providers #317

Closed
wants to merge 1 commit into from
Closed

Conversation

asinyagin
Copy link
Contributor

Hey,

I'd like to add support of multiple providers to web3. I was asking about workaround for it in #297. I've created this PR to discuss the idea, it changes only eth.getBalance(), so now the method can work through multiple providers.

I'll add comments to the diff to mark the main points.

web3.version = {};
web3.version.api = version.version;
web3.eth = {};
function Web3(provider) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a constructor for web3 object. The object stores links to requestManager and namespaces like eth (eth1 in this case).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you need to instantiate Web3?
So you would like to have multiple instances, with different providers running in the same app?

web3 is designed to be an object like the chrome or later mist object. It gives "native" functions and the dapp shouldn't even care how it connects.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, our ide works with 2 and more JSON RPC in parallel.

Okay, I see the problem. Yeah, that could be unnecessary complication. But if you think it's useful to work with multiple providers, you could get rid of RequestManager singleton and current global web3 and define global object like window.web3 = new Web3().

@debris
Copy link
Contributor

debris commented Oct 6, 2015

I like this pr a lot. I was thinking about this kind of refactor for some time 👍

And after we finish that, I would like to refactor even further. I would like to have strongly typed input params. I even did a small demo in typescript. We could have similar types to these. It would also solve issues similar to #322. What do you think about this? @frozeman @asinyagin

@debris
Copy link
Contributor

debris commented Oct 6, 2015

Of course we would to this in 2 (or more) separate steps

@asinyagin
Copy link
Contributor Author

@debris nice! Looking forward to test the version with multiple providers. And I agree it's a good idea to validate args. I have such validators to check params before giving them to web3.

@frozeman
Copy link
Contributor

frozeman commented Oct 6, 2015

How would an example look like for the strict params? Would you simply throw an error if they don't match?

@asinyagin
Copy link
Contributor Author

I'm not sure what's a good way in general, but in my case validator (for intN from Solidity) returns an array with errors:

define(function() {
    return function(type) {
        var size = parseInt(type.substr(3));
        return {
            validate: function(value) {
                var errors = [];
                if (!value.match(/^-?\d+$/))
                    errors.push('int must contain only sign and digits.');
                else {
                    var max = new BigNumber(2).pow(size).minus(1),
                        val = new BigNumber(value, 10).abs();
                    if (!val.lessThan(max))
                        errors.push(type + ' must be less than 2^' + size + '.');
                }
                return errors;
            }
        };
    };
});

@debris debris mentioned this pull request Oct 8, 2015
@debris debris closed this Oct 19, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants