aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-05-30 18:43:50 +0800
committerFabio Berger <me@fabioberger.com>2017-05-30 18:43:50 +0800
commita3ce892f85e7a8c1bb0e707f1e64f204a2ce29c6 (patch)
tree27bb9cbc2678b5342ce5064c4b57a70ebfbf2043 /src
parentcb31b21d28c20a0153199e9388202301e1385bfe (diff)
downloaddexon-0x-contracts-a3ce892f85e7a8c1bb0e707f1e64f204a2ce29c6.tar.gz
dexon-0x-contracts-a3ce892f85e7a8c1bb0e707f1e64f204a2ce29c6.tar.zst
dexon-0x-contracts-a3ce892f85e7a8c1bb0e707f1e64f204a2ce29c6.zip
Make tokenRegistry contract instantiation lazy and clear it on provider update
Diffstat (limited to 'src')
-rw-r--r--src/0x.js.ts1
-rw-r--r--src/contract_wrappers/token_registry_wrapper.ts18
2 files changed, 15 insertions, 4 deletions
diff --git a/src/0x.js.ts b/src/0x.js.ts
index 506ac4187..c621a15b6 100644
--- a/src/0x.js.ts
+++ b/src/0x.js.ts
@@ -143,6 +143,7 @@ export class ZeroEx {
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
diff --git a/src/contract_wrappers/token_registry_wrapper.ts b/src/contract_wrappers/token_registry_wrapper.ts
index 8dd79171e..69bad2bac 100644
--- a/src/contract_wrappers/token_registry_wrapper.ts
+++ b/src/contract_wrappers/token_registry_wrapper.ts
@@ -6,17 +6,20 @@ 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() {
+ delete this.tokenRegistryContractIfExists;
+ }
public async getTokensAsync(): Promise<Token[]> {
- const contractInstance = await this.instantiateContractIfExistsAsync((TokenRegistryArtifacts as any));
- const tokenRegistryContract = contractInstance as TokenRegistryContract;
+ await this.instantiateTokenRegistryContractIfDoesntExistAsync();
- const addresses = await tokenRegistryContract.getTokenAddresses.call();
+ const addresses = await this.tokenRegistryContractIfExists.getTokenAddresses.call();
const tokenMetadataPromises: Array<Promise<TokenMetadata>> = _.map(
addresses,
- (address: string) => (tokenRegistryContract.getTokenMetaData.call(address)),
+ (address: string) => (this.tokenRegistryContractIfExists.getTokenMetaData.call(address)),
);
const tokensMetadata = await Promise.all(tokenMetadataPromises);
const tokens = _.map(tokensMetadata, metadata => {
@@ -30,4 +33,11 @@ export class TokenRegistryWrapper extends ContractWrapper {
});
return tokens;
}
+ private async instantiateTokenRegistryContractIfDoesntExistAsync() {
+ if (!_.isUndefined(this.tokenRegistryContractIfExists)) {
+ return;
+ }
+ const contractInstance = await this.instantiateContractIfExistsAsync((TokenRegistryArtifacts as any));
+ this.tokenRegistryContractIfExists = contractInstance as TokenRegistryContract;
+ }
}