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

Simpler provider parameter handling on construction of a Web3 Module #2321

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions docs/include_package-core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ Parameters
----------

1. ``Object|String`` - ``provider``: a valid provider
2. ``Net`` - ``net``: (optional) the node.js Net package. This is only required for the IPC provider.

-------
Returns
Expand All @@ -254,10 +253,8 @@ Example
web3.setProvider(new Web3.providers.WebsocketProvider('ws://localhost:8546'));

// Using the IPC provider in node.js
const net = require('net');
const web3 = new Web3('/Users/myuser/Library/Ethereum/geth.ipc', net); // mac os path
import net from 'net';

// or
const web3 = new Web3(new Web3.providers.IpcProvider('/Users/myuser/Library/Ethereum/geth.ipc', net)); // mac os path
// on windows the path is: '\\\\.\\pipe\\geth.ipc'
// on linux the path is: '/users/myuser/.ethereum/geth.ipc'
Expand Down Expand Up @@ -291,17 +288,16 @@ Example

.. code-block:: javascript

const Web3 = require('web3');
import Web3 from 'web3';

// use the given Provider, e.g in Mist, or instantiate a new websocket provider
const web3 = new Web3(Web3.givenProvider || 'ws://localhost:8546');
// or
const web3 = new Web3(Web3.givenProvider || new Web3.providers.WebsocketProvider('ws://localhost:8546'));

// Using the IPC provider in node.js
const net = require('net');
import net from 'net';

const web3 = new Web3('/Users/myuser/Library/Ethereum/geth.ipc', net); // mac os path
// or
const web3 = new Web3(new Web3.providers.IpcProvider('/Users/myuser/Library/Ethereum/geth.ipc', net)); // mac os path
// on windows the path is: '\\\\.\\pipe\\geth.ipc'
// on linux the path is: '/users/myuser/.ethereum/geth.ipc'
Expand Down
7 changes: 1 addition & 6 deletions docs/web3-eth-abi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@ This will be used for function calls to the EVM (Ethereum Virtual Machine).


// or using the web3 umbrella package


import {Web3} from 'web3';

const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546', options);
// -> web3.eth.abi




------------------------------------------------------------------------------


Expand Down Expand Up @@ -191,6 +186,7 @@ Example
}
);
> "0x000000000000000000000000000000000000000000000000000000000000002a0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000004e"

------------------------------------------------------------------------------

encodeParameters
Expand Down Expand Up @@ -475,7 +471,6 @@ Example

------------------------------------------------------------------------------


decodeLog
=====================

Expand Down
17 changes: 5 additions & 12 deletions docs/web3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ Web3
Web3.modules
=====================

Static property of the Web3 class

.. code-block:: javascript

Web3.modules

Will return an object with the classes of all major sub modules, to be able to instantiate them manually.

-------
Expand All @@ -52,11 +46,11 @@ Example

Web3.modules
> {
Eth: Eth function(provider, options?, net?),
Net: Net function(provider, options?, net?),
Personal: Personal function(provider, options?, net?),
Shh: Shh function(provider, options?, net?),
Bzz: Bzz function(provider, options?, net?),
Eth: Eth function(provider, options?),
Net: Net function(provider, options?),
Personal: Personal function(provider, options?),
Shh: Shh function(provider, options?),
Bzz: Bzz function(provider, options?),
}


Expand Down Expand Up @@ -96,7 +90,6 @@ Example

------------------------------------------------------------------------------


utils
=====================

Expand Down
8 changes: 5 additions & 3 deletions packages/web3-core/src/AbstractWeb3Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,18 +259,20 @@ export default class AbstractWeb3Module {
}

/**
* TODO: Remove the net parameter. Developers should pass a IpcProvider instance if they would like to connect to a
* local node.
*
* Sets the currentProvider and provider property
*
* @method setProvider
*
* @param {EthereumProvider|HttpProvider|WebsocketProvider|IpcProvider|String} provider
* @param {Net} net
*
* @returns {Boolean|Error}
*/
setProvider(provider, net) {
setProvider(provider) {
if (!this.isSameProvider(provider)) {
const resolvedProvider = this.providerResolver.resolve(provider, net);
const resolvedProvider = this.providerResolver.resolve(provider);
this.clearSubscriptions();
this._currentProvider = resolvedProvider;

Expand Down
6 changes: 3 additions & 3 deletions packages/web3-core/tests/src/AbstractWeb3ModuleTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ describe('AbstractWeb3ModuleTest', () => {

expect(providerResolverMock.resolve).toHaveBeenNthCalledWith(1, 'WS');

expect(providerResolverMock.resolve).toHaveBeenNthCalledWith(2, 'SOCKET_PROVIDER', undefined);
expect(providerResolverMock.resolve).toHaveBeenNthCalledWith(2, 'SOCKET_PROVIDER');

expect(abstractWeb3Module.currentProvider).toEqual(providerMock);
});
Expand All @@ -201,7 +201,7 @@ describe('AbstractWeb3ModuleTest', () => {

expect(providerResolverMock.resolve).toHaveBeenNthCalledWith(1, 'WS');

expect(providerResolverMock.resolve).toHaveBeenNthCalledWith(2, 'SOCKET_PROVIDER', undefined);
expect(providerResolverMock.resolve).toHaveBeenNthCalledWith(2, 'SOCKET_PROVIDER');

expect(providerMock.clearSubscriptions).toHaveBeenCalled();

Expand All @@ -224,7 +224,7 @@ describe('AbstractWeb3ModuleTest', () => {
abstractWeb3Module.setProvider(provider);
}).toThrow('Invalid provider');

expect(providerResolverMock.resolve).toHaveBeenCalledWith(provider, undefined);
expect(providerResolverMock.resolve).toHaveBeenCalledWith(provider);
});

it('calls setProvider and returns false because of the equal host', () => {
Expand Down
5 changes: 2 additions & 3 deletions packages/web3-eth-ens/src/Ens.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,11 @@ export default class Ens extends AbstractWeb3Module {
* @method setProvider
*
* @param {HttpProvider|WebsocketProvider|IpcProvider|EthereumProvider|String} provider
* @param {Net} net
*
* @returns {Boolean}
*/
setProvider(provider, net) {
return !!(super.setProvider(provider, net) && this.registry.setProvider(provider, net));
setProvider(provider) {
return !!(super.setProvider(provider) && this.registry.setProvider(provider));
}

/**
Expand Down
7 changes: 3 additions & 4 deletions packages/web3-eth-ens/src/contracts/Registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,15 @@ export default class Registry extends AbstractContract {
* @method setProvider
*
* @param {HttpProvider|WebsocketProvider|IpcProvider|EthereumProvider|String} provider
* @param {Net} net
*
* @returns {Boolean}
*/
setProvider(provider, net) {
setProvider(provider) {
if (this.resolverContract) {
return !!(super.setProvider(provider, net) && this.resolverContract.setProvider(provider, net));
return !!(super.setProvider(provider) && this.resolverContract.setProvider(provider));
}

return super.setProvider(provider, net);
return super.setProvider(provider);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/web3-eth-personal/src/Personal.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {AbstractWeb3Module} from 'web3-core';
export default class Personal extends AbstractWeb3Module {
/**
* TODO: Add missing documentation for getAccounts, lockAccount and sendTransaction!
*
* @param {EthereumProvider|HttpProvider|WebsocketProvider|IpcProvider|String} provider
* @param {ProvidersModuleFactory} providersModuleFactory
* @param {MethodModuleFactory} methodModuleFactory
Expand All @@ -51,12 +52,11 @@ export default class Personal extends AbstractWeb3Module {
* @method setProvider
*
* @param {Object|String} provider
* @param {Net} net
*
* @returns {Boolean}
*/
setProvider(provider, net) {
return !!(super.setProvider(provider, net) && this.net.setProvider(provider, net));
setProvider(provider) {
return !!(super.setProvider(provider) && this.net.setProvider(provider));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-eth-personal/tests/src/PersonalTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ describe('PersonalTest', () => {
networkMock.setProvider = jest.fn();
networkMock.setProvider.mockReturnValueOnce(true);

expect(personal.setProvider('provider', 'net')).toEqual(true);
expect(personal.setProvider('provider')).toEqual(true);

expect(networkMock.setProvider).toHaveBeenCalledWith('provider', 'net');
expect(networkMock.setProvider).toHaveBeenCalledWith('provider');
});

it('sets the defaultGasPrice property', () => {
Expand Down
13 changes: 6 additions & 7 deletions packages/web3-eth/src/Eth.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,20 +353,19 @@ export default class Eth extends AbstractWeb3Module {
* This is required for updating the provider also in the sub packages and objects related to Eth.
*
* @param {Object|String} provider
* @param {Net} net
*
* @returns {Boolean}
*/
setProvider(provider, net) {
setProvider(provider) {
const setContractProviders = this.initiatedContracts.every((contract) => {
return contract.setProvider(provider, net);
return contract.setProvider(provider);
});

return (
this.net.setProvider(provider, net) &&
this.personal.setProvider(provider, net) &&
this.accounts.setProvider(provider, net) &&
super.setProvider(provider, net) &&
this.net.setProvider(provider) &&
this.personal.setProvider(provider) &&
this.accounts.setProvider(provider) &&
super.setProvider(provider) &&
setContractProviders
);
}
Expand Down
10 changes: 5 additions & 5 deletions packages/web3-eth/tests/src/EthTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,14 @@ describe('EthTest', () => {

accountsMock.setProvider.mockReturnValueOnce(true);

expect(eth.setProvider('provider', 'net')).toEqual(true);
expect(eth.setProvider('provider')).toEqual(true);

expect(eth.initiatedContracts[0].setProvider).toHaveBeenCalledWith('provider', 'net');
expect(eth.initiatedContracts[0].setProvider).toHaveBeenCalledWith('provider');

expect(networkMock.setProvider).toHaveBeenCalledWith('provider', 'net');
expect(networkMock.setProvider).toHaveBeenCalledWith('provider');

expect(personalMock.setProvider).toHaveBeenCalledWith('provider', 'net');
expect(personalMock.setProvider).toHaveBeenCalledWith('provider');

expect(accountsMock.setProvider).toHaveBeenCalledWith('provider', 'net');
expect(accountsMock.setProvider).toHaveBeenCalledWith('provider');
});
});
8 changes: 1 addition & 7 deletions packages/web3-providers/src/resolvers/ProviderResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ export default class ProviderResolver {
* @method resolve
*
* @param {EthereumProvider|HttpProvider|WebsocketProvider|IpcProvider|String} provider
* @param {Net} net
*
* @returns {EthereumProvider|HttpProvider|WebsocketProvider|IpcProvider|Error}
*/
resolve(provider, net) {
resolve(provider) {
if (typeof provider === 'string') {
// HTTP
if (/^http(s)?:\/\//i.test(provider)) {
Expand All @@ -65,11 +64,6 @@ export default class ProviderResolver {
if (/^ws(s)?:\/\//i.test(provider)) {
return this.providersModuleFactory.createWebsocketProvider(provider);
}

// IPC
if (provider && isObject(net) && isFunction(net.connect)) {
return this.providersModuleFactory.createIpcProvider(provider, net);
}
}

if (typeof global.mist !== 'undefined' && provider.constructor.name === 'EthereumProvider') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,6 @@ describe('ProviderResolverTest', () => {
expect(providersModuleFactoryMock.createWebsocketProvider).toHaveBeenCalledWith('ws://127.0.0.1:8545');
});

it('calls resolve with Ipc path and net object', () => {
new IpcProvider({}, '/path/to/the/socket');
const ipcProviderMock = IpcProvider.mock.instances[0];

const net = {connect: () => {}};

providersModuleFactory.createIpcProvider.mockReturnValueOnce(ipcProviderMock);

expect(providerResolver.resolve('/path/to/the/socket', net)).toBeInstanceOf(IpcProvider);

expect(providersModuleFactoryMock.createIpcProvider).toHaveBeenCalledWith('/path/to/the/socket', net);
});

it('calls resolve with the HttpProvider', () => {
new HttpProvider('host', {}, providersModuleFactoryMock);
const httpProviderMock = HttpProvider.mock.instances[0];
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-providers/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class JsonRpcMapper {
export class ProviderResolver {
constructor(providersPackageFactory: ProvidersModuleFactory);

resolve(provider: provider, net: net.Socket): provider;
resolve(provider: provider): provider;
}

export class JsonRpcResponseValidator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* @date 2018
*/

import * as net from "net";
import {ProviderResolver, ProvidersModuleFactory, HttpProvider} from 'web3-providers';

const options = {
Expand All @@ -37,4 +36,4 @@ const providersModuleFactory = new ProvidersModuleFactory();
const providerResolver = new ProviderResolver(providersModuleFactory);

// $ExpectType provider
providerResolver.resolve(provider, new net.Socket());
providerResolver.resolve(provider);
5 changes: 2 additions & 3 deletions packages/web3-shh/src/Shh.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,11 @@ export default class Shh extends AbstractWeb3Module {
* This is required for updating the provider also in the sub package Net.
*
* @param {Object|String} provider
* @param {Net} net
*
* @returns {Boolean}
*/
setProvider(provider, net) {
return super.setProvider(provider, net) && this.net.setProvider(provider, net);
setProvider(provider) {
return super.setProvider(provider) && this.net.setProvider(provider);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/web3-shh/tests/src/ShhTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,18 @@ describe('ShhTest', () => {
networkMock.setProvider = jest.fn();
networkMock.setProvider.mockReturnValueOnce(true);

expect(shh.setProvider('provider', 'net')).toEqual(true);
expect(shh.setProvider('provider')).toEqual(true);

expect(networkMock.setProvider).toHaveBeenCalledWith('provider', 'net');
expect(networkMock.setProvider).toHaveBeenCalledWith('provider');
});

it('calls setProvider and returns false', () => {
networkMock.setProvider = jest.fn();
networkMock.setProvider.mockReturnValueOnce(false);

expect(shh.setProvider('provider', 'net')).toEqual(false);
expect(shh.setProvider('provider')).toEqual(false);

expect(networkMock.setProvider).toHaveBeenCalledWith('provider', 'net');
expect(networkMock.setProvider).toHaveBeenCalledWith('provider');
});

it('sets the defaultGasPrice property', () => {
Expand Down
Loading