diff options
author | Leonid <logvinov.leon@gmail.com> | 2017-05-30 20:31:37 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-30 20:31:37 +0800 |
commit | b0436a4f679fb5ecb2e13cc9af18cb114d71ed63 (patch) | |
tree | 8c7b600fb2ead36e828d6acfbd0d47e3e7fb78d9 /src | |
parent | 911ab437b8f9371f70e835f680d799b7c62fb140 (diff) | |
parent | 3522f94ff6cde1aad83299def7308025da9432d5 (diff) | |
download | dexon-sol-tools-b0436a4f679fb5ecb2e13cc9af18cb114d71ed63.tar dexon-sol-tools-b0436a4f679fb5ecb2e13cc9af18cb114d71ed63.tar.gz dexon-sol-tools-b0436a4f679fb5ecb2e13cc9af18cb114d71ed63.tar.bz2 dexon-sol-tools-b0436a4f679fb5ecb2e13cc9af18cb114d71ed63.tar.lz dexon-sol-tools-b0436a4f679fb5ecb2e13cc9af18cb114d71ed63.tar.xz dexon-sol-tools-b0436a4f679fb5ecb2e13cc9af18cb114d71ed63.tar.zst dexon-sol-tools-b0436a4f679fb5ecb2e13cc9af18cb114d71ed63.zip |
Merge pull request #25 from 0xProject/dontReinstantiateContractInstances
Add zeroEx.setProvider and clear contractInstances created with old provider
Diffstat (limited to 'src')
-rw-r--r-- | src/0x.js.ts | 9 | ||||
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 17 | ||||
-rw-r--r-- | src/contract_wrappers/token_registry_wrapper.ts | 15 | ||||
-rw-r--r-- | src/web3_wrapper.ts | 3 |
4 files changed, 39 insertions, 5 deletions
diff --git a/src/0x.js.ts b/src/0x.js.ts index f3da005f8..4c67027fc 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -137,6 +137,15 @@ 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 instantiated with the old provider. + */ + public setProvider(provider: Web3.Provider) { + this.web3Wrapper.setProvider(provider); + this.exchange.invalidateContractInstance(); + this.tokenRegistry.invalidateContractInstance(); + } + /** * 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..3f6eb0dab 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -7,9 +7,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 invalidateContractInstance(): void { + delete this.exchangeContractIfExists; + } public async isValidSignatureAsync(dataHex: string, ecSignature: ECSignature, signerAddressHex: string): Promise<boolean> { assert.isHexString('dataHex', dataHex); @@ -19,10 +23,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; + const exchangeContract = await this.getExchangeContractAsync(); - const isValidSignature = await exchangeInstance.isValidSignature.call( + const isValidSignature = await exchangeContract.isValidSignature.call( signerAddressHex, dataHex, ecSignature.v, @@ -34,4 +37,12 @@ export class ExchangeWrapper extends ContractWrapper { ); return isValidSignature; } + private async getExchangeContractAsync(): Promise<ExchangeContract> { + if (!_.isUndefined(this.exchangeContractIfExists)) { + return this.exchangeContractIfExists; + } + const contractInstance = await this.instantiateContractIfExistsAsync((ExchangeArtifacts as any)); + this.exchangeContractIfExists = contractInstance as ExchangeContract; + return this.exchangeContractIfExists; + } } diff --git a/src/contract_wrappers/token_registry_wrapper.ts b/src/contract_wrappers/token_registry_wrapper.ts index 8dd79171e..86bea1c5d 100644 --- a/src/contract_wrappers/token_registry_wrapper.ts +++ b/src/contract_wrappers/token_registry_wrapper.ts @@ -6,12 +6,15 @@ import {ContractWrapper} from './contract_wrapper'; import * as TokenRegistryArtifacts from '../artifacts/TokenRegistry.json'; export class TokenRegistryWrapper extends ContractWrapper { + private tokenRegistryContractIfExists?: TokenRegistryContract; constructor(web3Wrapper: Web3Wrapper) { super(web3Wrapper); } + public invalidateContractInstance(): void { + delete this.tokenRegistryContractIfExists; + } public async getTokensAsync(): Promise<Token[]> { - const contractInstance = await this.instantiateContractIfExistsAsync((TokenRegistryArtifacts as any)); - const tokenRegistryContract = contractInstance as TokenRegistryContract; + const tokenRegistryContract = await this.getTokenRegistryContractAsync(); const addresses = await tokenRegistryContract.getTokenAddresses.call(); const tokenMetadataPromises: Array<Promise<TokenMetadata>> = _.map( @@ -30,4 +33,12 @@ export class TokenRegistryWrapper extends ContractWrapper { }); return tokens; } + private async getTokenRegistryContractAsync(): Promise<TokenRegistryContract> { + if (!_.isUndefined(this.tokenRegistryContractIfExists)) { + return this.tokenRegistryContractIfExists; + } + const contractInstance = await this.instantiateContractIfExistsAsync((TokenRegistryArtifacts as any)); + this.tokenRegistryContractIfExists = contractInstance as TokenRegistryContract; + return this.tokenRegistryContractIfExists; + } } 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); } |