-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Conversation
web3.version = {}; | ||
web3.version.api = version.version; | ||
web3.eth = {}; | ||
function Web3(provider) { |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()
.
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 |
Of course we would to this in 2 (or more) separate steps |
@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. |
How would an example look like for the strict params? Would you simply throw an error if they don't match? |
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;
}
};
};
}); |
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.