Make tokenRegistry contract instantiation lazy and clear it on provider update
This commit is contained in:
parent
cb31b21d28
commit
a3ce892f85
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -26,8 +26,9 @@ describe('ZeroEx library', () => {
|
||||
(newProvider as any).zeroExTestId = 1;
|
||||
zeroEx.setProvider(newProvider);
|
||||
|
||||
// Check that exchangeContract instance removed after provider update
|
||||
// Check that contractInstances with old provider are removed after provider update
|
||||
expect((zeroEx.exchange as any).exchangeContractIfExists).to.be.an('undefined');
|
||||
expect((zeroEx.tokenRegistry as any).tokenRegistryContractIfExists).to.be.an('undefined');
|
||||
|
||||
// Check that all nested web3 instances return the updated provider
|
||||
const nestedWeb3WrapperProvider = (zeroEx as any).web3Wrapper.getCurrentProvider();
|
||||
|
Loading…
x
Reference in New Issue
Block a user