diff options
-rw-r--r-- | src/0x.js.ts | 8 | ||||
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 17 | ||||
-rw-r--r-- | src/types.ts | 3 | ||||
-rw-r--r-- | src/web3_wrapper.ts | 3 | ||||
-rw-r--r-- | test/0x.js_test.ts | 23 | ||||
-rw-r--r-- | test/utils/web3_factory.ts | 10 |
6 files changed, 58 insertions, 6 deletions
diff --git a/src/0x.js.ts b/src/0x.js.ts index f3da005f8..3dfdfbfcd 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -137,6 +137,14 @@ export class ZeroEx { this.tokenRegistry = new TokenRegistryWrapper(this.web3Wrapper); } /** + * Sets a new provider for the web3 instance used by 0x.js internally and invalidates any instantiated + * contract instances created with the old provider. + */ + public setProvider(provider: Web3.Provider) { + this.web3Wrapper.setProvider(provider); + this.exchange.invalidateExchangeContract(); + } + /** * Signs an orderHash and returns it's elliptic curve signature * This method currently supports TestRPC, Geth and Parity above and below V1.6.6 */ diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index f0f153c2b..a7abbb078 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -1,4 +1,5 @@ import * as _ from 'lodash'; +import * as Web3 from 'web3'; import {Web3Wrapper} from '../web3_wrapper'; import {ECSignature, ZeroExError, ExchangeContract} from '../types'; import {assert} from '../utils/assert'; @@ -7,9 +8,13 @@ import * as ExchangeArtifacts from '../artifacts/Exchange.json'; import {ecSignatureSchema} from '../schemas/ec_signature_schema'; export class ExchangeWrapper extends ContractWrapper { + private exchangeContractIfExists: ExchangeContract; constructor(web3Wrapper: Web3Wrapper) { super(web3Wrapper); } + public invalidateExchangeContract() { + delete this.exchangeContractIfExists; + } public async isValidSignatureAsync(dataHex: string, ecSignature: ECSignature, signerAddressHex: string): Promise<boolean> { assert.isHexString('dataHex', dataHex); @@ -19,10 +24,9 @@ export class ExchangeWrapper extends ContractWrapper { const senderAddressIfExists = await this.web3Wrapper.getSenderAddressIfExistsAsync(); assert.assert(!_.isUndefined(senderAddressIfExists), ZeroExError.USER_HAS_NO_ASSOCIATED_ADDRESSES); - const contractInstance = await this.instantiateContractIfExistsAsync((ExchangeArtifacts as any)); - const exchangeInstance = contractInstance as ExchangeContract; + await this.instantiateExchangeContractIfDoesntExistAsync(); - const isValidSignature = await exchangeInstance.isValidSignature.call( + const isValidSignature = await this.exchangeContractIfExists.isValidSignature.call( signerAddressHex, dataHex, ecSignature.v, @@ -34,4 +38,11 @@ export class ExchangeWrapper extends ContractWrapper { ); return isValidSignature; } + private async instantiateExchangeContractIfDoesntExistAsync() { + if (!_.isUndefined(this.exchangeContractIfExists)) { + return; + } + const contractInstance = await this.instantiateContractIfExistsAsync((ExchangeArtifacts as any)); + this.exchangeContractIfExists = contractInstance as ExchangeContract; + } } diff --git a/src/types.ts b/src/types.ts index 6fce95706..f17beafd2 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,5 @@ import * as _ from 'lodash'; +import * as Web3 from 'web3'; // Utility function to create a K:V from a list of strings // Adapted from: https://basarat.gitbooks.io/typescript/content/docs/types/literal-types.html @@ -28,6 +29,8 @@ export interface ECSignature { export interface ExchangeContract { isValidSignature: any; + currentProvider: Web3.Provider; + setProvider: (provider: Web3.Provider) => void; } export interface TokenRegistryContract { diff --git a/src/web3_wrapper.ts b/src/web3_wrapper.ts index a915a89e8..e65f29b56 100644 --- a/src/web3_wrapper.ts +++ b/src/web3_wrapper.ts @@ -9,6 +9,9 @@ export class Web3Wrapper { this.web3 = new Web3(); this.web3.setProvider(web3.currentProvider); } + public setProvider(provider: Web3.Provider) { + this.web3.setProvider(provider); + } public isAddress(address: string): boolean { return this.web3.isAddress(address); } diff --git a/test/0x.js_test.ts b/test/0x.js_test.ts index c45c70991..1e76ae71a 100644 --- a/test/0x.js_test.ts +++ b/test/0x.js_test.ts @@ -4,6 +4,7 @@ import 'mocha'; import * as BigNumber from 'bignumber.js'; import ChaiBigNumber = require('chai-bignumber'); import * as Sinon from 'sinon'; +import ProviderEngine = require('web3-provider-engine'); import {ZeroEx} from '../src/0x.js'; import {constants} from './utils/constants'; import {web3Factory} from './utils/web3_factory'; @@ -13,6 +14,28 @@ chai.use(ChaiBigNumber()); const expect = chai.expect; describe('ZeroEx library', () => { + describe('#setProvider', () => { + it('overrides the provider in the nested web3 instance and invalidates contractInstances', async () => { + const web3 = web3Factory.create(); + const zeroEx = new ZeroEx(web3); + // Instantiate the exchangeContract instance with the current provider + await (zeroEx.exchange as any).instantiateExchangeContractIfDoesntExistAsync(); + + const newProvider = web3Factory.getRpcProvider(); + // Add property to newProvider so that we can differentiate it from old provider + (newProvider as any).zeroExTestId = 1; + zeroEx.setProvider(newProvider); + + // Check that exchangeContract instance removed after provider update + expect((zeroEx.exchange as any).exchangeContractIfExists).to.be.an('undefined'); + + // Check that all nested web3 instances return the updated provider + const nestedWeb3WrapperProvider = (zeroEx as any).web3Wrapper.getCurrentProvider(); + expect((nestedWeb3WrapperProvider as any).zeroExTestId).to.be.a('number'); + const contractWrapperWeb3WrapperProvider = zeroEx.exchange.web3Wrapper.getCurrentProvider(); + expect((nestedWeb3WrapperProvider as any).zeroExTestId).to.be.a('number'); + }); + }); describe('#getOrderHash', () => { const expectedOrderHash = '0x103a5e97dab5dbeb8f385636f86a7d1e458a7ccbe1bd194727f0b2f85ab116c7'; it('defaults takerAddress to NULL address', () => { diff --git a/test/utils/web3_factory.ts b/test/utils/web3_factory.ts index 493fbc2df..ffdc0e4cf 100644 --- a/test/utils/web3_factory.ts +++ b/test/utils/web3_factory.ts @@ -10,14 +10,18 @@ import {constants} from './constants'; export const web3Factory = { create(): Web3 { + const provider = this.getRpcProvider(); + const web3 = new Web3(); + web3.setProvider(provider); + return web3; + }, + getRpcProvider(): Web3.Provider { const provider = new ProviderEngine(); const rpcUrl = `http://${constants.RPC_HOST}:${constants.RPC_PORT}`; provider.addProvider(new RpcSubprovider({ rpcUrl, })); provider.start(); - const web3 = new Web3(); - web3.setProvider(provider); - return web3; + return provider; }, }; |