In contract-wrappers, remove setProvider and add unsubscribeAll method.

This commit is contained in:
Alex Browne
2018-10-15 17:35:25 -07:00
parent e093864bff
commit 6f8e98e537
14 changed files with 35 additions and 73 deletions

View File

@@ -6,13 +6,31 @@
"note": "Add optional validation to the forwarder wrapper methods"
},
{
"note":
"Updated to use new modularized artifacts. Top-level `ContractWrappers` class has a new optional `contractAddresses` parameter. Contract addresses are no longer stored in artifacts and are instead loaded from the `@0xproject/contract-addresses` package. Most contract addresses are now defined at instantiation time and are available as properties (e.g., `exchangeWrapper.address`) instead of methods (e.g., `exchangeWrapper.getContractAddress()`). Some properties and methods have been renamed.",
"note": "Updated to use new modularized artifacts.",
"pr": 1105
},
{
"note": "Top-level `ContractWrappers` class has a new optional `contractAddresses` parameter.",
"pr": 1105
},
{
"note":
"Updated to use new modularized artifacts and the latest version of @0xproject/contract-wrappers",
"Default contract addresses are no longer stored in artifacts and are instead loaded from the `@0xproject/contract-addresses` package.",
"pr": 1105
},
{
"note":
"Most contract addresses are now defined at instantiation time and are available as properties (e.g., `exchangeWrapper.address`) instead of methods (e.g., `exchangeWrapper.getContractAddress()`).",
"pr": 1105
},
{
"note":
"Removed `setProvider` method in top-level `ContractWrapper` class and added new `unsubscribeAll` method.",
"pr": 1105
},
{
"note":
"Some properties and methods have been renamed. For example, some methods that previously could throw no longer can, and so their names have been updated accordingly.",
"pr": 1105
}
]

View File

@@ -143,21 +143,13 @@ export class ContractWrappers {
);
}
/**
* Sets a new web3 provider for contract-wrappers. Updating the provider will stop all
* subscriptions so you will need to re-subscribe to all events relevant to your app after this call.
* @param provider The Web3Provider you would like the contract-wrappers library to use from now on.
* @param networkId The id of the network your provider is connected to
* Unsubscribes from all subscriptions for all contracts.
*/
public setProvider(provider: Provider): void {
this._web3Wrapper.setProvider(provider);
(this.exchange as any)._invalidateContractInstances();
(this.erc20Token as any)._invalidateContractInstances();
(this.erc20Proxy as any)._invalidateContractInstance();
(this.erc721Token as any)._invalidateContractInstances();
(this.erc721Proxy as any)._invalidateContractInstance();
(this.etherToken as any)._invalidateContractInstance();
(this.forwarder as any)._invalidateContractInstance();
(this.orderValidator as any)._invalidateContractInstance();
public unsubscribeAll(): void {
this.exchange.unsubscribeAll();
this.erc20Token.unsubscribeAll();
this.erc721Token.unsubscribeAll();
this.etherToken.unsubscribeAll();
}
/**
* Get the provider instance currently used by contract-wrappers

View File

@@ -58,12 +58,6 @@ export class ERC20ProxyWrapper extends ContractWrapper {
const authorizedAddresses = await ERC20ProxyContractInstance.getAuthorizedAddresses.callAsync();
return authorizedAddresses;
}
// HACK: We don't want this method to be visible to the other units within that package but not to the end user.
// TS doesn't give that possibility and therefore we make it private and access it over an any cast. Because of that tslint sees it as unused.
// tslint:disable-next-line:no-unused-variable
private _invalidateContractInstance(): void {
delete this._erc20ProxyContractIfExists;
}
private _getERC20ProxyContract(): ERC20ProxyContract {
if (!_.isUndefined(this._erc20ProxyContractIfExists)) {
return this._erc20ProxyContractIfExists;

View File

@@ -422,13 +422,6 @@ export class ERC20TokenWrapper extends ContractWrapper {
);
return logs;
}
// HACK: We don't want this method to be visible to the other units within that package but not to the end user.
// TS doesn't give that possibility and therefore we make it private and access it over an any cast. Because of that tslint sees it as unused.
// tslint:disable-next-line:no-unused-variable
private _invalidateContractInstances(): void {
this.unsubscribeAll();
this._tokenContractsByAddress = {};
}
private async _getTokenContractAsync(tokenAddress: string): Promise<ERC20TokenContract> {
const normalizedTokenAddress = tokenAddress.toLowerCase();
let tokenContract = this._tokenContractsByAddress[normalizedTokenAddress];

View File

@@ -58,12 +58,6 @@ export class ERC721ProxyWrapper extends ContractWrapper {
const authorizedAddresses = await ERC721ProxyContractInstance.getAuthorizedAddresses.callAsync();
return authorizedAddresses;
}
// HACK: We don't want this method to be visible to the other units within that package but not to the end user.
// TS doesn't give that possibility and therefore we make it private and access it over an any cast. Because of that tslint sees it as unused.
// tslint:disable-next-line:no-unused-variable
private _invalidateContractInstance(): void {
delete this._erc721ProxyContractIfExists;
}
private _getERC721ProxyContract(): ERC721ProxyContract {
if (!_.isUndefined(this._erc721ProxyContractIfExists)) {
return this._erc721ProxyContractIfExists;

View File

@@ -449,13 +449,6 @@ export class ERC721TokenWrapper extends ContractWrapper {
);
return logs;
}
// HACK: We don't want this method to be visible to the other units within that package but not to the end user.
// TS doesn't give that possibility and therefore we make it private and access it over an any cast. Because of that tslint sees it as unused.
// tslint:disable-next-line:no-unused-variable
private _invalidateContractInstances(): void {
this.unsubscribeAll();
this._tokenContractsByAddress = {};
}
private async _getTokenContractAsync(tokenAddress: string): Promise<ERC721TokenContract> {
const normalizedTokenAddress = tokenAddress.toLowerCase();
let tokenContract = this._tokenContractsByAddress[normalizedTokenAddress];

View File

@@ -192,11 +192,6 @@ export class EtherTokenWrapper extends ContractWrapper {
public unsubscribeAll(): void {
super._unsubscribeAll();
}
// tslint:disable-next-line:no-unused-variable
private _invalidateContractInstance(): void {
this.unsubscribeAll();
this._etherTokenContractsByAddress = {};
}
private async _getEtherTokenContractAsync(etherTokenAddress: string): Promise<WETH9Contract> {
let etherTokenContract = this._etherTokenContractsByAddress[etherTokenAddress];
if (!_.isUndefined(etherTokenContract)) {

View File

@@ -1180,11 +1180,6 @@ export class ExchangeWrapper extends ContractWrapper {
const encoder = new TransactionEncoder(exchangeInstance);
return encoder;
}
// tslint:disable:no-unused-variable
private _invalidateContractInstances(): void {
this.unsubscribeAll();
delete this._exchangeContractIfExists;
}
// tslint:enable:no-unused-variable
private async _getExchangeContractAsync(): Promise<ExchangeContract> {
if (!_.isUndefined(this._exchangeContractIfExists)) {

View File

@@ -234,12 +234,6 @@ export class ForwarderWrapper extends ContractWrapper {
);
return txHash;
}
// HACK: We don't want this method to be visible to the other units within that package but not to the end user.
// TS doesn't give that possibility and therefore we make it private and access it over an any cast. Because of that tslint sees it as unused.
// tslint:disable-next-line:no-unused-variable
private _invalidateContractInstance(): void {
delete this._forwarderContractIfExists;
}
private async _getForwarderContractAsync(): Promise<ForwarderContract> {
if (!_.isUndefined(this._forwarderContractIfExists)) {
return this._forwarderContractIfExists;

View File

@@ -169,12 +169,6 @@ export class OrderValidatorWrapper extends ContractWrapper {
const result = await OrderValidatorContractInstance.getERC721TokenOwner.callAsync(tokenAddress, tokenId);
return result;
}
// HACK: We don't want this method to be visible to the other units within that package but not to the end user.
// TS doesn't give that possibility and therefore we make it private and access it over an any cast. Because of that tslint sees it as unused.
// tslint:disable-next-line:no-unused-variable
private _invalidateContractInstance(): void {
delete this._orderValidatorContractIfExists;
}
private async _getOrderValidatorContractAsync(): Promise<OrderValidatorContract> {
if (!_.isUndefined(this._orderValidatorContractIfExists)) {
return this._orderValidatorContractIfExists;

View File

@@ -533,7 +533,7 @@ describe('ERC20Wrapper', () => {
);
})().catch(done);
});
it('Outstanding subscriptions are cancelled when contractWrappers.setProvider called', (done: DoneCallback) => {
it('Outstanding subscriptions are cancelled when contractWrappers.unsubscribeAll called', (done: DoneCallback) => {
(async () => {
const callbackNeverToBeCalled = callbackErrorReporter.reportNodeCallbackErrors(done)(
(_logEvent: DecodedLogEvent<ERC20TokenApprovalEventArgs>) => {
@@ -547,7 +547,7 @@ describe('ERC20Wrapper', () => {
callbackNeverToBeCalled,
);
const callbackToBeCalled = callbackErrorReporter.reportNodeCallbackErrors(done)();
contractWrappers.setProvider(provider);
contractWrappers.unsubscribeAll();
contractWrappers.erc20Token.subscribe(
tokenAddress,
ERC20TokenEvents.Transfer,

View File

@@ -326,7 +326,7 @@ describe('ERC721Wrapper', () => {
);
})().catch(done);
});
it('Outstanding subscriptions are cancelled when contractWrappers.setProvider called', (done: DoneCallback) => {
it('Outstanding subscriptions are cancelled when contractWrappers.unsubscribeAll called', (done: DoneCallback) => {
(async () => {
const callbackNeverToBeCalled = callbackErrorReporter.reportNodeCallbackErrors(done)(
(logEvent: DecodedLogEvent<ERC721TokenApprovalEventArgs>) => {
@@ -340,7 +340,7 @@ describe('ERC721Wrapper', () => {
callbackNeverToBeCalled,
);
const callbackToBeCalled = callbackErrorReporter.reportNodeCallbackErrors(done)();
contractWrappers.setProvider(provider);
contractWrappers.unsubscribeAll();
contractWrappers.erc721Token.subscribe(
tokenAddress,
ERC721TokenEvents.Approval,

View File

@@ -283,7 +283,7 @@ describe('EtherTokenWrapper', () => {
await contractWrappers.etherToken.withdrawAsync(etherTokenAddress, withdrawalAmount, addressWithETH);
})().catch(done);
});
it('should cancel outstanding subscriptions when ZeroEx.setProvider is called', (done: DoneCallback) => {
it('should cancel outstanding subscriptions when contractWrappers.unsubscribeAll is called', (done: DoneCallback) => {
(async () => {
const callbackNeverToBeCalled = callbackErrorReporter.reportNodeCallbackErrors(done)(
(_logEvent: DecodedLogEvent<WETH9ApprovalEventArgs>) => {
@@ -297,7 +297,7 @@ describe('EtherTokenWrapper', () => {
callbackNeverToBeCalled,
);
const callbackToBeCalled = callbackErrorReporter.reportNodeCallbackErrors(done)();
contractWrappers.setProvider(provider);
contractWrappers.unsubscribeAll();
await contractWrappers.etherToken.depositAsync(etherTokenAddress, transferAmount, addressWithETH);
contractWrappers.etherToken.subscribe(
etherTokenAddress,

View File

@@ -410,7 +410,7 @@ describe('ExchangeWrapper', () => {
await contractWrappers.exchange.cancelOrderAsync(signedOrder);
})().catch(done);
});
it('Outstanding subscriptions are cancelled when contractWrappers.setProvider called', (done: DoneCallback) => {
it('Outstanding subscriptions are cancelled when contractWrappers.unsubscribeAll called', (done: DoneCallback) => {
(async () => {
const callbackNeverToBeCalled = callbackErrorReporter.reportNodeCallbackErrors(done)(
(logEvent: DecodedLogEvent<ExchangeFillEventArgs>) => {
@@ -419,7 +419,7 @@ describe('ExchangeWrapper', () => {
);
contractWrappers.exchange.subscribe(ExchangeEvents.Fill, indexFilterValues, callbackNeverToBeCalled);
contractWrappers.setProvider(provider);
contractWrappers.unsubscribeAll();
const callback = callbackErrorReporter.reportNodeCallbackErrors(done)(
(logEvent: DecodedLogEvent<ExchangeFillEventArgs>) => {