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

supportsSubscriptions added to providers #2632

Merged
merged 9 commits into from
Apr 3, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export default class AbstractMethodFactory {
getTimeout(moduleInstance) {
let timeout = moduleInstance.transactionBlockTimeout;

if (!moduleInstance.currentProvider.SOCKET_MESSAGE) {
if (!moduleInstance.currentProvider.supportsSubscriptions()) {
timeout = moduleInstance.transactionPollingTimeout;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export default class TransactionObserver {
* @returns {Boolean}
*/
isSocketBasedProvider() {
return !!this.provider.SOCKET_MESSAGE;
return !!this.provider.supportsSubscriptions();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,17 @@ describe('AbstractMethodFactoryTest', () => {
it('calls createMethod and returns a object of type AbstractObservedTransactionMethod', () => {
new AbstractWeb3Module();
const moduleInstanceMock = AbstractWeb3Module.mock.instances[0];
moduleInstanceMock.currentProvider = {constructor: {name: 'HttpProvider'}};
moduleInstanceMock.currentProvider = {supportsSubscriptions: jest.fn()};
moduleInstanceMock.currentProvider.supportsSubscriptions.mockReturnValueOnce(false);

expect(abstractMethodFactory.hasMethod('sendObserved')).toEqual(true);

const observedMethod = abstractMethodFactory.createMethod('sendObserved', moduleInstanceMock);

expect(observedMethod).toBeInstanceOf(AbstractObservedTransactionMethod);

expect(moduleInstanceMock.currentProvider.supportsSubscriptions).toHaveBeenCalled();

expect(GetTransactionReceiptMethod).toHaveBeenCalledTimes(1);

expect(AbstractObservedTransactionMethod).toHaveBeenCalledTimes(1);
Expand All @@ -101,14 +104,17 @@ describe('AbstractMethodFactoryTest', () => {
it('calls createMethod and returns a object of type EthSendTransactionMethod', () => {
new AbstractWeb3Module();
const moduleInstanceMock = AbstractWeb3Module.mock.instances[0];
moduleInstanceMock.currentProvider = {constructor: {name: 'HttpProvider'}};
moduleInstanceMock.currentProvider = {supportsSubscriptions: jest.fn()};
moduleInstanceMock.currentProvider.supportsSubscriptions.mockReturnValueOnce(false);

expect(abstractMethodFactory.hasMethod('sendEthObserved')).toEqual(true);

const observedMethod = abstractMethodFactory.createMethod('sendEthObserved', moduleInstanceMock);

expect(observedMethod).toBeInstanceOf(EthSendTransactionMethod);

expect(moduleInstanceMock.currentProvider.supportsSubscriptions).toHaveBeenCalled();

expect(GetTransactionReceiptMethod).toHaveBeenCalledTimes(1);

expect(GetBlockByNumberMethod).toHaveBeenCalledTimes(1);
Expand All @@ -125,14 +131,17 @@ describe('AbstractMethodFactoryTest', () => {
it('calls createMethod with a socket provider and returns a object of type AbstractObservedTransactionMethod', () => {
new AbstractWeb3Module();
const moduleInstanceMock = AbstractWeb3Module.mock.instances[0];
moduleInstanceMock.currentProvider = {constructor: {name: 'WebsocketProvider'}};
moduleInstanceMock.currentProvider = {supportsSubscriptions: jest.fn()};
moduleInstanceMock.currentProvider.supportsSubscriptions.mockReturnValueOnce(true);

expect(abstractMethodFactory.hasMethod('sendObserved')).toEqual(true);

const observedMethod = abstractMethodFactory.createMethod('sendObserved', moduleInstanceMock);

expect(observedMethod).toBeInstanceOf(AbstractObservedTransactionMethod);

expect(moduleInstanceMock.currentProvider.supportsSubscriptions).toHaveBeenCalled();

expect(GetTransactionReceiptMethod).toHaveBeenCalledTimes(1);

expect(AbstractObservedTransactionMethod).toHaveBeenCalledTimes(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('TransactionObserverTest', () => {
newHeadsSubscriptionMock;

beforeEach(() => {
providerMock = {constructor: {name: 'socket'}};
providerMock = {supportsSubscriptions: jest.fn()};

new GetTransactionReceiptMethod();
getTransactionReceiptMethodMock = GetTransactionReceiptMethod.mock.instances[0];
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('TransactionObserverTest', () => {
it('calls observe with a socket provider and returns a transaction receipt', (done) => {
transactionObserver.blockConfirmations = 2;

providerMock.SOCKET_MESSAGE = true;
providerMock.supportsSubscriptions.mockReturnValueOnce(true);

const blockHeadOne = {
number: 0
Expand Down Expand Up @@ -102,6 +102,8 @@ describe('TransactionObserverTest', () => {
},
() => {},
() => {
expect(providerMock.supportsSubscriptions).toHaveBeenCalled();

expect(newHeadsSubscriptionMock.unsubscribe).toHaveBeenCalled();

done();
Expand All @@ -113,7 +115,7 @@ describe('TransactionObserverTest', () => {
transactionObserver.blockConfirmations = 2;
transactionObserver.timeout = 1;

providerMock.SOCKET_MESSAGE = true;
providerMock.supportsSubscriptions.mockReturnValueOnce(true);

const blockHeadOne = {
number: 0
Expand All @@ -140,6 +142,8 @@ describe('TransactionObserverTest', () => {
)
);

expect(providerMock.supportsSubscriptions).toHaveBeenCalled();

expect(error.receipt).toEqual(receipt);

expect(error.confirmations).toEqual(1);
Expand All @@ -156,7 +160,7 @@ describe('TransactionObserverTest', () => {
it('calls observe with a socket provider and the newHeads subscription returns a error', (done) => {
transactionObserver.blockConfirmations = 2;

providerMock.SOCKET_MESSAGE = true;
providerMock.supportsSubscriptions.mockReturnValueOnce(true);

newHeadsSubscriptionMock.subscribe = jest.fn((callback) => {
callback(true, false);
Expand All @@ -167,6 +171,8 @@ describe('TransactionObserverTest', () => {
(error) => {
expect(error.error).toEqual(true);

expect(providerMock.supportsSubscriptions).toHaveBeenCalled();

expect(error.receipt).toEqual(false);

expect(error.confirmations).toEqual(0);
Expand All @@ -181,6 +187,8 @@ describe('TransactionObserverTest', () => {
it('calls observe with a http provider and returns a transaction receipt', (done) => {
transactionObserver.blockConfirmations = 2;

providerMock.supportsSubscriptions.mockReturnValueOnce(false);

const receipt = {blockNumber: '0xa'};
const blockOne = {number: '0xa', hash: '0x0'};
const blockTwo = {number: '0xc', parentHash: '0x0'};
Expand Down Expand Up @@ -210,6 +218,8 @@ describe('TransactionObserverTest', () => {
() => {
expect(getTransactionReceiptMethodMock.execute).toHaveBeenCalledTimes(2);

expect(providerMock.supportsSubscriptions).toHaveBeenCalled();

expect(getTransactionReceiptMethodMock.parameters).toEqual(['transactionHash']);

expect(getBlockByNumberMethodMock.parameters).toEqual(['0xb']);
Expand All @@ -223,6 +233,8 @@ describe('TransactionObserverTest', () => {
transactionObserver.blockConfirmations = 2;
transactionObserver.timeout = 1;

providerMock.supportsSubscriptions.mockReturnValueOnce(false);

const receipt = {blockNumber: '0xa'};
const blockOne = {number: '0xa', hash: '0x0'};

Expand All @@ -245,6 +257,8 @@ describe('TransactionObserverTest', () => {
expect(error.confirmations).toEqual(1);
expect(error.confirmationChecks).toEqual(1);

expect(providerMock.supportsSubscriptions).toHaveBeenCalled();

expect(getTransactionReceiptMethodMock.execute).toHaveBeenCalledTimes(1);

expect(getTransactionReceiptMethodMock.parameters).toEqual(['transactionHash']);
Expand All @@ -258,6 +272,8 @@ describe('TransactionObserverTest', () => {
transactionObserver.blockConfirmations = 2;
transactionObserver.timeout = 1;

providerMock.supportsSubscriptions.mockReturnValueOnce(false);

getTransactionReceiptMethodMock.execute = jest.fn(() => {
return Promise.reject(new Error('ERROR'));
});
Expand All @@ -270,6 +286,8 @@ describe('TransactionObserverTest', () => {
expect(error.confirmations).toEqual(0);
expect(error.confirmationChecks).toEqual(0);

expect(providerMock.supportsSubscriptions).toHaveBeenCalled();

expect(getTransactionReceiptMethodMock.execute).toHaveBeenCalledTimes(1);

expect(getTransactionReceiptMethodMock.parameters).toEqual(['transactionHash']);
Expand Down
7 changes: 4 additions & 3 deletions packages/web3-core/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import BN = require('bn.js');
import {AbstractMethodFactory} from 'web3-core-method';
import {
BatchRequest,
EthereumProvider,
Web3EthereumProvider,
HttpProvider,
HttpProviderOptions,
IpcProvider,
provider,
WebsocketProvider,
CustomProvider,
WebsocketProviderOptions
} from 'web3-providers';

Expand All @@ -48,8 +49,8 @@ export class AbstractWeb3Module {
defaultGas: number;
static readonly providers: Providers;
defaultAccount: string | null;
readonly currentProvider: EthereumProvider | HttpProvider | IpcProvider | WebsocketProvider;
readonly givenProvider: object | null;
readonly currentProvider: Web3EthereumProvider | HttpProvider | IpcProvider | WebsocketProvider | CustomProvider;
readonly givenProvider: any;

setProvider(provider: provider, net?: net.Socket): boolean;

Expand Down
4 changes: 2 additions & 2 deletions packages/web3-core/types/tests/abstract-web3-module-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ abstractWeb3Module.defaultGas;
// $ExpectType Providers
AbstractWeb3Module.providers;

// $ExpectType object | null
// $ExpectType any
abstractWeb3Module.givenProvider;

// $ExpectType string | null
abstractWeb3Module.defaultAccount;

// $ExpectType HttpProvider | IpcProvider | WebsocketProvider | EthereumProvider
// $ExpectType HttpProvider | IpcProvider | WebsocketProvider | Web3EthereumProvider | CustomProvider
abstractWeb3Module.currentProvider;

// $ExpectType boolean
Expand Down
10 changes: 3 additions & 7 deletions packages/web3-eth-contract/src/factories/MethodFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,11 @@ export default class MethodFactory {
* @returns {SendContractMethod}
*/
createSendContractMethod(contract) {
const transactionObserver = this.createTransactionObserver(contract);

return new SendContractMethod(
this.utils,
this.formatters,
contract,
transactionObserver,
this.createTransactionObserver(contract),
new ChainIdMethod(this.utils, this.formatters, contract),
new GetTransactionCountMethod(this.utils, this.formatters, contract),
this.contractModuleFactory.createAllEventsLogDecoder(),
Expand All @@ -177,13 +175,11 @@ export default class MethodFactory {
* @returns {ContractDeployMethod}
*/
createContractDeployMethod(contract) {
const transactionObserver = this.createTransactionObserver(contract);

return new ContractDeployMethod(
this.utils,
this.formatters,
contract,
transactionObserver,
this.createTransactionObserver(contract),
new ChainIdMethod(this.utils, this.formatters, contract),
new GetTransactionCountMethod(this.utils, this.formatters, contract)
);
Expand Down Expand Up @@ -214,7 +210,7 @@ export default class MethodFactory {
getTimeout(contract) {
let timeout = contract.transactionBlockTimeout;

if (!contract.currentProvider.SOCKET_MESSAGE) {
if (!contract.currentProvider.supportsSubscriptions()) {
timeout = contract.transactionPollingTimeout;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('MethodFactoryTest', () => {
let methodFactory, contractModuleFactoryMock, abiCoderMock, contract;

beforeEach(() => {
contract = {currentProvider: {SOCKET_MESSAGE: false}};
contract = {currentProvider: {supportsSubscriptions: jest.fn()}};

new ContractModuleFactory({}, {}, {});
contractModuleFactoryMock = ContractModuleFactory.mock.instances[0];
Expand All @@ -56,18 +56,26 @@ describe('MethodFactoryTest', () => {
});

it('calls createMethodByRequestType with requestType send', () => {
contract.currentProvider.supportsSubscriptions.mockReturnValueOnce(false);

expect(methodFactory.createMethodByRequestType({}, contract, 'send')).toBeInstanceOf(SendContractMethod);

expect(SendContractMethod).toHaveBeenCalled();

expect(ChainIdMethod).toHaveBeenCalledWith(Utils, formatters, contract);

expect(GetTransactionCountMethod).toHaveBeenCalledWith(Utils, formatters, contract);

expect(contract.currentProvider.supportsSubscriptions).toHaveBeenCalled();
});

it('calls createMethodByRequestType with requestType send and a socketProvider', () => {
it('calls createMethodByRequestType with requestType send and a socket based provider', () => {
contract.currentProvider.supportsSubscriptions.mockReturnValueOnce(true);

expect(methodFactory.createMethodByRequestType({}, contract, 'send')).toBeInstanceOf(SendContractMethod);

expect(contract.currentProvider.supportsSubscriptions).toHaveBeenCalled();

expect(SendContractMethod).toHaveBeenCalled();

expect(ChainIdMethod).toHaveBeenCalledWith(Utils, formatters, contract);
Expand All @@ -82,10 +90,14 @@ describe('MethodFactoryTest', () => {
});

it('calls createMethodByRequestType with requestType contract-deployment', () => {
contract.currentProvider.supportsSubscriptions.mockReturnValueOnce(false);

expect(methodFactory.createMethodByRequestType({}, contract, 'contract-deployment')).toBeInstanceOf(
ContractDeployMethod
);

expect(contract.currentProvider.supportsSubscriptions).toHaveBeenCalled();

expect(ContractDeployMethod).toHaveBeenCalled();

expect(ChainIdMethod).toHaveBeenCalledWith(Utils, formatters, contract);
Expand Down Expand Up @@ -134,8 +146,12 @@ describe('MethodFactoryTest', () => {
});

it('calls createSendContractMethod and returns SendContractMethod object', () => {
contract.currentProvider.supportsSubscriptions.mockReturnValueOnce(false);

expect(methodFactory.createSendContractMethod(contract)).toBeInstanceOf(SendContractMethod);

expect(contract.currentProvider.supportsSubscriptions).toHaveBeenCalled();

expect(contractModuleFactoryMock.createAllEventsLogDecoder).toHaveBeenCalled();

expect(SendContractMethod).toHaveBeenCalled();
Expand All @@ -146,8 +162,12 @@ describe('MethodFactoryTest', () => {
});

it('calls createContractDeployMethod and returns ContractDeployMethod object', () => {
contract.currentProvider.supportsSubscriptions.mockReturnValueOnce(false);

expect(methodFactory.createContractDeployMethod(contract)).toBeInstanceOf(ContractDeployMethod);

expect(contract.currentProvider.supportsSubscriptions).toHaveBeenCalled();

expect(ContractDeployMethod).toHaveBeenCalled();

expect(ChainIdMethod).toHaveBeenCalledWith(Utils, formatters, contract);
Expand All @@ -162,10 +182,12 @@ describe('MethodFactoryTest', () => {
});

it('calls createSendContractMethod with a socket based provider and returns SendContractMethod object', () => {
const contract = {currentProvider: {SOCKET_MESSAGE: true}};
contract.currentProvider.supportsSubscriptions.mockReturnValueOnce(true);

expect(methodFactory.createSendContractMethod(contract)).toBeInstanceOf(SendContractMethod);

expect(contract.currentProvider.supportsSubscriptions).toHaveBeenCalled();

expect(contractModuleFactoryMock.createAllEventsLogDecoder).toHaveBeenCalled();

expect(SendContractMethod).toHaveBeenCalled();
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-eth/types/tests/eth.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ eth.subscribe('pendingTransactions', null, (error: Error, transactionHash: strin
// $ExpectType Providers
Eth.providers;

// $ExpectType object | null
// $ExpectType any
eth.givenProvider;

// $ExpectType BatchRequest
Expand All @@ -83,7 +83,7 @@ eth.defaultAccount;
// $ExpectType string | number
eth.defaultBlock;

// $ExpectType HttpProvider | IpcProvider | WebsocketProvider | EthereumProvider
// $ExpectType HttpProvider | IpcProvider | WebsocketProvider | Web3EthereumProvider | CustomProvider
eth.currentProvider;

// $ExpectType Promise<string>
Expand Down
Loading