From 026ad1f9a10359723eed54f37c067ee2a61d8fcd Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Mon, 1 Oct 2018 18:22:42 -0700 Subject: Update contract-wrappers package to use new contracts package for generated files --- packages/contract-wrappers/src/artifacts.ts | 27 ----------- .../contract-wrappers/src/contract_wrappers.ts | 5 +- .../src/contract_wrappers/erc20_proxy_wrapper.ts | 9 ++-- .../src/contract_wrappers/erc20_token_wrapper.ts | 21 ++++----- .../src/contract_wrappers/erc721_proxy_wrapper.ts | 9 ++-- .../src/contract_wrappers/erc721_token_wrapper.ts | 21 ++++----- .../src/contract_wrappers/ether_token_wrapper.ts | 31 ++++++------- .../src/contract_wrappers/exchange_wrapper.ts | 21 ++++----- .../src/contract_wrappers/forwarder_wrapper.ts | 11 ++--- .../contract_wrappers/order_validator_wrapper.ts | 9 ++-- packages/contract-wrappers/src/index.ts | 54 ++++++++++------------ packages/contract-wrappers/src/types.ts | 18 +++++--- .../src/utils/transaction_encoder.ts | 10 ++-- 13 files changed, 104 insertions(+), 142 deletions(-) delete mode 100644 packages/contract-wrappers/src/artifacts.ts (limited to 'packages/contract-wrappers/src') diff --git a/packages/contract-wrappers/src/artifacts.ts b/packages/contract-wrappers/src/artifacts.ts deleted file mode 100644 index 925f34162..000000000 --- a/packages/contract-wrappers/src/artifacts.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { ContractArtifact } from 'ethereum-types'; - -import * as DummyERC20Token from './artifacts/DummyERC20Token.json'; -import * as DummyERC721Token from './artifacts/DummyERC721Token.json'; -import * as ERC20Proxy from './artifacts/ERC20Proxy.json'; -import * as ERC20Token from './artifacts/ERC20Token.json'; -import * as ERC721Proxy from './artifacts/ERC721Proxy.json'; -import * as ERC721Token from './artifacts/ERC721Token.json'; -import * as Exchange from './artifacts/Exchange.json'; -import * as Forwarder from './artifacts/Forwarder.json'; -import * as OrderValidator from './artifacts/OrderValidator.json'; -import * as EtherToken from './artifacts/WETH9.json'; -import * as ZRXToken from './artifacts/ZRXToken.json'; - -export const artifacts = { - ZRXToken: (ZRXToken as any) as ContractArtifact, - DummyERC20Token: (DummyERC20Token as any) as ContractArtifact, - DummyERC721Token: (DummyERC721Token as any) as ContractArtifact, - ERC20Token: (ERC20Token as any) as ContractArtifact, - ERC721Token: (ERC721Token as any) as ContractArtifact, - Exchange: (Exchange as any) as ContractArtifact, - EtherToken: (EtherToken as any) as ContractArtifact, - ERC20Proxy: (ERC20Proxy as any) as ContractArtifact, - ERC721Proxy: (ERC721Proxy as any) as ContractArtifact, - Forwarder: (Forwarder as any) as ContractArtifact, - OrderValidator: (OrderValidator as any) as ContractArtifact, -}; diff --git a/packages/contract-wrappers/src/contract_wrappers.ts b/packages/contract-wrappers/src/contract_wrappers.ts index 89402029b..b272fabda 100644 --- a/packages/contract-wrappers/src/contract_wrappers.ts +++ b/packages/contract-wrappers/src/contract_wrappers.ts @@ -1,10 +1,8 @@ +import { artifacts } from '@0xproject/contracts'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { Provider } from 'ethereum-types'; import * as _ from 'lodash'; -import { constants } from './utils/constants'; - -import { artifacts } from './artifacts'; import { ERC20ProxyWrapper } from './contract_wrappers/erc20_proxy_wrapper'; import { ERC20TokenWrapper } from './contract_wrappers/erc20_token_wrapper'; import { ERC721ProxyWrapper } from './contract_wrappers/erc721_proxy_wrapper'; @@ -18,6 +16,7 @@ import { contractWrappersPrivateNetworkConfigSchema } from './schemas/contract_w import { contractWrappersPublicNetworkConfigSchema } from './schemas/contract_wrappers_public_network_config_schema'; import { ContractWrappersConfig } from './types'; import { assert } from './utils/assert'; +import { constants } from './utils/constants'; /** * The ContractWrappers class contains smart contract wrappers helpful when building on 0x protocol. */ diff --git a/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts index ff027d78a..5900f0502 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts @@ -1,20 +1,19 @@ +import { artifacts, wrappers } from '@0xproject/contracts'; import { AssetProxyId } from '@0xproject/types'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { ContractAbi } from 'ethereum-types'; import * as _ from 'lodash'; -import { artifacts } from '../artifacts'; import { assert } from '../utils/assert'; import { ContractWrapper } from './contract_wrapper'; -import { ERC20ProxyContract } from './generated/erc20_proxy'; /** * This class includes the functionality related to interacting with the ERC20Proxy contract. */ export class ERC20ProxyWrapper extends ContractWrapper { public abi: ContractAbi = artifacts.ERC20Proxy.compilerOutput.abi; - private _erc20ProxyContractIfExists?: ERC20ProxyContract; + private _erc20ProxyContractIfExists?: wrappers.ERC20ProxyContract; private _contractAddressIfExists?: string; /** * Instantiate ERC20ProxyWrapper @@ -72,7 +71,7 @@ export class ERC20ProxyWrapper extends ContractWrapper { private _invalidateContractInstance(): void { delete this._erc20ProxyContractIfExists; } - private async _getERC20ProxyContractAsync(): Promise { + private async _getERC20ProxyContractAsync(): Promise { if (!_.isUndefined(this._erc20ProxyContractIfExists)) { return this._erc20ProxyContractIfExists; } @@ -80,7 +79,7 @@ export class ERC20ProxyWrapper extends ContractWrapper { artifacts.ERC20Proxy, this._contractAddressIfExists, ); - const contractInstance = new ERC20ProxyContract( + const contractInstance = new wrappers.ERC20ProxyContract( abi, address, this._web3Wrapper.getProvider(), diff --git a/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts index 4625cef6a..3be5bcaee 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts @@ -1,10 +1,10 @@ +import { artifacts, wrappers } from '@0xproject/contracts'; import { schemas } from '@0xproject/json-schemas'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { ContractAbi, LogWithDecodedArgs } from 'ethereum-types'; import * as _ from 'lodash'; -import { artifacts } from '../artifacts'; import { methodOptsSchema } from '../schemas/method_opts_schema'; import { txOptsSchema } from '../schemas/tx_opts_schema'; import { @@ -20,7 +20,6 @@ import { constants } from '../utils/constants'; import { ContractWrapper } from './contract_wrapper'; import { ERC20ProxyWrapper } from './erc20_proxy_wrapper'; -import { ERC20TokenContract, ERC20TokenEventArgs, ERC20TokenEvents } from './generated/erc20_token'; const removeUndefinedProperties = _.pickBy; @@ -32,7 +31,7 @@ const removeUndefinedProperties = _.pickBy; export class ERC20TokenWrapper extends ContractWrapper { public abi: ContractAbi = artifacts.ERC20Token.compilerOutput.abi; public UNLIMITED_ALLOWANCE_IN_BASE_UNITS = constants.UNLIMITED_ALLOWANCE_IN_BASE_UNITS; - private _tokenContractsByAddress: { [address: string]: ERC20TokenContract }; + private _tokenContractsByAddress: { [address: string]: wrappers.ERC20TokenContract }; private _erc20ProxyWrapper: ERC20ProxyWrapper; /** * Instantiate ERC20TokenWrapper @@ -357,15 +356,15 @@ export class ERC20TokenWrapper extends ContractWrapper { * @param isVerbose Enable verbose subscription warnings (e.g recoverable network issues encountered) * @return Subscription token used later to unsubscribe */ - public subscribe( + public subscribe( tokenAddress: string, - eventName: ERC20TokenEvents, + eventName: wrappers.ERC20TokenEvents, indexFilterValues: IndexedFilterValues, callback: EventCallback, isVerbose: boolean = false, ): string { assert.isETHAddressHex('tokenAddress', tokenAddress); - assert.doesBelongToStringEnum('eventName', eventName, ERC20TokenEvents); + assert.doesBelongToStringEnum('eventName', eventName, wrappers.ERC20TokenEvents); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); assert.isFunction('callback', callback); const normalizedTokenAddress = tokenAddress.toLowerCase(); @@ -402,14 +401,14 @@ export class ERC20TokenWrapper extends ContractWrapper { * the value is the value you are interested in. E.g `{_from: aUserAddressHex}` * @return Array of logs that match the parameters */ - public async getLogsAsync( + public async getLogsAsync( tokenAddress: string, - eventName: ERC20TokenEvents, + eventName: wrappers.ERC20TokenEvents, blockRange: BlockRange, indexFilterValues: IndexedFilterValues, ): Promise>> { assert.isETHAddressHex('tokenAddress', tokenAddress); - assert.doesBelongToStringEnum('eventName', eventName, ERC20TokenEvents); + assert.doesBelongToStringEnum('eventName', eventName, wrappers.ERC20TokenEvents); assert.doesConformToSchema('blockRange', blockRange, schemas.blockRangeSchema); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); const normalizedTokenAddress = tokenAddress.toLowerCase(); @@ -429,7 +428,7 @@ export class ERC20TokenWrapper extends ContractWrapper { this.unsubscribeAll(); this._tokenContractsByAddress = {}; } - private async _getTokenContractAsync(tokenAddress: string): Promise { + private async _getTokenContractAsync(tokenAddress: string): Promise { const normalizedTokenAddress = tokenAddress.toLowerCase(); let tokenContract = this._tokenContractsByAddress[normalizedTokenAddress]; if (!_.isUndefined(tokenContract)) { @@ -439,7 +438,7 @@ export class ERC20TokenWrapper extends ContractWrapper { artifacts.ERC20Token, normalizedTokenAddress, ); - const contractInstance = new ERC20TokenContract( + const contractInstance = new wrappers.ERC20TokenContract( abi, address, this._web3Wrapper.getProvider(), diff --git a/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts index 933c1dc27..6ba162213 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts @@ -1,20 +1,19 @@ +import { artifacts, wrappers } from '@0xproject/contracts'; import { AssetProxyId } from '@0xproject/types'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { ContractAbi } from 'ethereum-types'; import * as _ from 'lodash'; -import { artifacts } from '../artifacts'; import { assert } from '../utils/assert'; import { ContractWrapper } from './contract_wrapper'; -import { ERC721ProxyContract } from './generated/erc721_proxy'; /** * This class includes the functionality related to interacting with the ERC721Proxy contract. */ export class ERC721ProxyWrapper extends ContractWrapper { public abi: ContractAbi = artifacts.ERC20Proxy.compilerOutput.abi; - private _erc721ProxyContractIfExists?: ERC721ProxyContract; + private _erc721ProxyContractIfExists?: wrappers.ERC721ProxyContract; private _contractAddressIfExists?: string; /** * Instantiate ERC721ProxyWrapper @@ -72,7 +71,7 @@ export class ERC721ProxyWrapper extends ContractWrapper { private _invalidateContractInstance(): void { delete this._erc721ProxyContractIfExists; } - private async _getERC721ProxyContractAsync(): Promise { + private async _getERC721ProxyContractAsync(): Promise { if (!_.isUndefined(this._erc721ProxyContractIfExists)) { return this._erc721ProxyContractIfExists; } @@ -80,7 +79,7 @@ export class ERC721ProxyWrapper extends ContractWrapper { artifacts.ERC721Proxy, this._contractAddressIfExists, ); - const contractInstance = new ERC721ProxyContract( + const contractInstance = new wrappers.ERC721ProxyContract( abi, address, this._web3Wrapper.getProvider(), diff --git a/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts index 590dbbf74..23b335710 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts @@ -1,10 +1,10 @@ +import { artifacts, wrappers } from '@0xproject/contracts'; import { schemas } from '@0xproject/json-schemas'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { ContractAbi, LogWithDecodedArgs } from 'ethereum-types'; import * as _ from 'lodash'; -import { artifacts } from '../artifacts'; import { methodOptsSchema } from '../schemas/method_opts_schema'; import { txOptsSchema } from '../schemas/tx_opts_schema'; import { @@ -20,7 +20,6 @@ import { constants } from '../utils/constants'; import { ContractWrapper } from './contract_wrapper'; import { ERC721ProxyWrapper } from './erc721_proxy_wrapper'; -import { ERC721TokenContract, ERC721TokenEventArgs, ERC721TokenEvents } from './generated/erc721_token'; const removeUndefinedProperties = _.pickBy; @@ -31,7 +30,7 @@ const removeUndefinedProperties = _.pickBy; */ export class ERC721TokenWrapper extends ContractWrapper { public abi: ContractAbi = artifacts.ERC721Token.compilerOutput.abi; - private _tokenContractsByAddress: { [address: string]: ERC721TokenContract }; + private _tokenContractsByAddress: { [address: string]: wrappers.ERC721TokenContract }; private _erc721ProxyWrapper: ERC721ProxyWrapper; /** * Instantiate ERC721TokenWrapper @@ -384,15 +383,15 @@ export class ERC721TokenWrapper extends ContractWrapper { * @param isVerbose Enable verbose subscription warnings (e.g recoverable network issues encountered) * @return Subscription token used later to unsubscribe */ - public subscribe( + public subscribe( tokenAddress: string, - eventName: ERC721TokenEvents, + eventName: wrappers.ERC721TokenEvents, indexFilterValues: IndexedFilterValues, callback: EventCallback, isVerbose: boolean = false, ): string { assert.isETHAddressHex('tokenAddress', tokenAddress); - assert.doesBelongToStringEnum('eventName', eventName, ERC721TokenEvents); + assert.doesBelongToStringEnum('eventName', eventName, wrappers.ERC721TokenEvents); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); assert.isFunction('callback', callback); const normalizedTokenAddress = tokenAddress.toLowerCase(); @@ -429,14 +428,14 @@ export class ERC721TokenWrapper extends ContractWrapper { * the value is the value you are interested in. E.g `{_from: aUserAddressHex}` * @return Array of logs that match the parameters */ - public async getLogsAsync( + public async getLogsAsync( tokenAddress: string, - eventName: ERC721TokenEvents, + eventName: wrappers.ERC721TokenEvents, blockRange: BlockRange, indexFilterValues: IndexedFilterValues, ): Promise>> { assert.isETHAddressHex('tokenAddress', tokenAddress); - assert.doesBelongToStringEnum('eventName', eventName, ERC721TokenEvents); + assert.doesBelongToStringEnum('eventName', eventName, wrappers.ERC721TokenEvents); assert.doesConformToSchema('blockRange', blockRange, schemas.blockRangeSchema); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); const normalizedTokenAddress = tokenAddress.toLowerCase(); @@ -456,7 +455,7 @@ export class ERC721TokenWrapper extends ContractWrapper { this.unsubscribeAll(); this._tokenContractsByAddress = {}; } - private async _getTokenContractAsync(tokenAddress: string): Promise { + private async _getTokenContractAsync(tokenAddress: string): Promise { const normalizedTokenAddress = tokenAddress.toLowerCase(); let tokenContract = this._tokenContractsByAddress[normalizedTokenAddress]; if (!_.isUndefined(tokenContract)) { @@ -466,7 +465,7 @@ export class ERC721TokenWrapper extends ContractWrapper { artifacts.ERC721Token, normalizedTokenAddress, ); - const contractInstance = new ERC721TokenContract( + const contractInstance = new wrappers.ERC721TokenContract( abi, address, this._web3Wrapper.getProvider(), diff --git a/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts index 1ac01812e..30653adf2 100644 --- a/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts @@ -1,16 +1,15 @@ +import { artifacts, wrappers } from '@0xproject/contracts'; import { schemas } from '@0xproject/json-schemas'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { ContractAbi, LogWithDecodedArgs } from 'ethereum-types'; import * as _ from 'lodash'; -import { artifacts } from '../artifacts'; import { BlockRange, ContractWrappersError, EventCallback, IndexedFilterValues, TransactionOpts } from '../types'; import { assert } from '../utils/assert'; import { ContractWrapper } from './contract_wrapper'; import { ERC20TokenWrapper } from './erc20_token_wrapper'; -import { WETH9Contract, WETH9EventArgs, WETH9Events } from './generated/weth9'; const removeUndefinedProperties = _.pickBy; @@ -19,9 +18,9 @@ const removeUndefinedProperties = _.pickBy; * The caller can convert ETH into the equivalent number of wrapped ETH ERC20 tokens and back. */ export class EtherTokenWrapper extends ContractWrapper { - public abi: ContractAbi = artifacts.EtherToken.compilerOutput.abi; + public abi: ContractAbi = artifacts.WETH9.compilerOutput.abi; private _etherTokenContractsByAddress: { - [address: string]: WETH9Contract; + [address: string]: wrappers.WETH9Contract; } = {}; private _erc20TokenWrapper: ERC20TokenWrapper; /** @@ -126,15 +125,15 @@ export class EtherTokenWrapper extends ContractWrapper { * the value is the value you are interested in. E.g `{_owner: aUserAddressHex}` * @return Array of logs that match the parameters */ - public async getLogsAsync( + public async getLogsAsync( etherTokenAddress: string, - eventName: WETH9Events, + eventName: wrappers.WETH9Events, blockRange: BlockRange, indexFilterValues: IndexedFilterValues, ): Promise>> { assert.isETHAddressHex('etherTokenAddress', etherTokenAddress); const normalizedEtherTokenAddress = etherTokenAddress.toLowerCase(); - assert.doesBelongToStringEnum('eventName', eventName, WETH9Events); + assert.doesBelongToStringEnum('eventName', eventName, wrappers.WETH9Events); assert.doesConformToSchema('blockRange', blockRange, schemas.blockRangeSchema); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); const logs = await this._getLogsAsync( @@ -142,7 +141,7 @@ export class EtherTokenWrapper extends ContractWrapper { eventName, blockRange, indexFilterValues, - artifacts.EtherToken.compilerOutput.abi, + artifacts.WETH9.compilerOutput.abi, ); return logs; } @@ -156,23 +155,23 @@ export class EtherTokenWrapper extends ContractWrapper { * @param isVerbose Enable verbose subscription warnings (e.g recoverable network issues encountered) * @return Subscription token used later to unsubscribe */ - public subscribe( + public subscribe( etherTokenAddress: string, - eventName: WETH9Events, + eventName: wrappers.WETH9Events, indexFilterValues: IndexedFilterValues, callback: EventCallback, isVerbose: boolean = false, ): string { assert.isETHAddressHex('etherTokenAddress', etherTokenAddress); const normalizedEtherTokenAddress = etherTokenAddress.toLowerCase(); - assert.doesBelongToStringEnum('eventName', eventName, WETH9Events); + assert.doesBelongToStringEnum('eventName', eventName, wrappers.WETH9Events); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); assert.isFunction('callback', callback); const subscriptionToken = this._subscribe( normalizedEtherTokenAddress, eventName, indexFilterValues, - artifacts.EtherToken.compilerOutput.abi, + artifacts.WETH9.compilerOutput.abi, callback, isVerbose, ); @@ -199,7 +198,7 @@ export class EtherTokenWrapper extends ContractWrapper { * @returns The Ethereum address of the EtherToken contract or undefined. */ public getContractAddressIfExists(): string | undefined { - const networkSpecificArtifact = artifacts.EtherToken.networks[this._networkId]; + const networkSpecificArtifact = artifacts.WETH9.networks[this._networkId]; const contractAddressIfExists = _.isUndefined(networkSpecificArtifact) ? undefined : networkSpecificArtifact.address; @@ -210,16 +209,16 @@ export class EtherTokenWrapper extends ContractWrapper { this.unsubscribeAll(); this._etherTokenContractsByAddress = {}; } - private async _getEtherTokenContractAsync(etherTokenAddress: string): Promise { + private async _getEtherTokenContractAsync(etherTokenAddress: string): Promise { let etherTokenContract = this._etherTokenContractsByAddress[etherTokenAddress]; if (!_.isUndefined(etherTokenContract)) { return etherTokenContract; } const [abi, address] = await this._getContractAbiAndAddressFromArtifactsAsync( - artifacts.EtherToken, + artifacts.WETH9, etherTokenAddress, ); - const contractInstance = new WETH9Contract( + const contractInstance = new wrappers.WETH9Contract( abi, address, this._web3Wrapper.getProvider(), diff --git a/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts index dc9ffcbf7..613747720 100644 --- a/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts @@ -1,3 +1,4 @@ +import { artifacts, wrappers } from '@0xproject/contracts'; import { schemas } from '@0xproject/json-schemas'; import { assetDataUtils, @@ -11,7 +12,6 @@ import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { BlockParamLiteral, ContractAbi, LogWithDecodedArgs } from 'ethereum-types'; import * as _ from 'lodash'; -import { artifacts } from '../artifacts'; import { AssetBalanceAndProxyAllowanceFetcher } from '../fetchers/asset_balance_and_proxy_allowance_fetcher'; import { OrderFilledCancelledFetcher } from '../fetchers/order_filled_cancelled_fetcher'; import { methodOptsSchema } from '../schemas/method_opts_schema'; @@ -35,7 +35,6 @@ import { TransactionEncoder } from '../utils/transaction_encoder'; import { ContractWrapper } from './contract_wrapper'; import { ERC20TokenWrapper } from './erc20_token_wrapper'; import { ERC721TokenWrapper } from './erc721_token_wrapper'; -import { ExchangeContract, ExchangeEventArgs, ExchangeEvents } from './generated/exchange'; /** * This class includes all the functionality related to calling methods, sending transactions and subscribing to @@ -43,7 +42,7 @@ import { ExchangeContract, ExchangeEventArgs, ExchangeEvents } from './generated */ export class ExchangeWrapper extends ContractWrapper { public abi: ContractAbi = artifacts.Exchange.compilerOutput.abi; - private _exchangeContractIfExists?: ExchangeContract; + private _exchangeContractIfExists?: wrappers.ExchangeContract; private _erc721TokenWrapper: ERC721TokenWrapper; private _erc20TokenWrapper: ERC20TokenWrapper; private _contractAddressIfExists?: string; @@ -1042,13 +1041,13 @@ export class ExchangeWrapper extends ContractWrapper { * @param isVerbose Enable verbose subscription warnings (e.g recoverable network issues encountered) * @return Subscription token used later to unsubscribe */ - public subscribe( - eventName: ExchangeEvents, + public subscribe( + eventName: wrappers.ExchangeEvents, indexFilterValues: IndexedFilterValues, callback: EventCallback, isVerbose: boolean = false, ): string { - assert.doesBelongToStringEnum('eventName', eventName, ExchangeEvents); + assert.doesBelongToStringEnum('eventName', eventName, wrappers.ExchangeEvents); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); assert.isFunction('callback', callback); const exchangeContractAddress = this.getContractAddress(); @@ -1083,12 +1082,12 @@ export class ExchangeWrapper extends ContractWrapper { * the value is the value you are interested in. E.g `{_from: aUserAddressHex}` * @return Array of logs that match the parameters */ - public async getLogsAsync( - eventName: ExchangeEvents, + public async getLogsAsync( + eventName: wrappers.ExchangeEvents, blockRange: BlockRange, indexFilterValues: IndexedFilterValues, ): Promise>> { - assert.doesBelongToStringEnum('eventName', eventName, ExchangeEvents); + assert.doesBelongToStringEnum('eventName', eventName, wrappers.ExchangeEvents); assert.doesConformToSchema('blockRange', blockRange, schemas.blockRangeSchema); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); const exchangeContractAddress = this.getContractAddress(); @@ -1201,7 +1200,7 @@ export class ExchangeWrapper extends ContractWrapper { delete this._exchangeContractIfExists; } // tslint:enable:no-unused-variable - private async _getExchangeContractAsync(): Promise { + private async _getExchangeContractAsync(): Promise { if (!_.isUndefined(this._exchangeContractIfExists)) { return this._exchangeContractIfExists; } @@ -1209,7 +1208,7 @@ export class ExchangeWrapper extends ContractWrapper { artifacts.Exchange, this._contractAddressIfExists, ); - const contractInstance = new ExchangeContract( + const contractInstance = new wrappers.ExchangeContract( abi, address, this._web3Wrapper.getProvider(), diff --git a/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts index c19edf188..854a8381b 100644 --- a/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts @@ -1,3 +1,4 @@ +import { artifacts, wrappers } from '@0xproject/contracts'; import { schemas } from '@0xproject/json-schemas'; import { AssetProxyId, SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; @@ -5,7 +6,6 @@ import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { ContractAbi } from 'ethereum-types'; import * as _ from 'lodash'; -import { artifacts } from '../artifacts'; import { orderTxOptsSchema } from '../schemas/order_tx_opts_schema'; import { txOptsSchema } from '../schemas/tx_opts_schema'; import { OrderTransactionOpts } from '../types'; @@ -16,14 +16,13 @@ import { decorators } from '../utils/decorators'; import { utils } from '../utils/utils'; import { ContractWrapper } from './contract_wrapper'; -import { ForwarderContract } from './generated/forwarder'; /** * This class includes the functionality related to interacting with the Forwarder contract. */ export class ForwarderWrapper extends ContractWrapper { public abi: ContractAbi = artifacts.Forwarder.compilerOutput.abi; - private _forwarderContractIfExists?: ForwarderContract; + private _forwarderContractIfExists?: wrappers.ForwarderContract; private _contractAddressIfExists?: string; private _zrxContractAddressIfExists?: string; constructor( @@ -242,7 +241,7 @@ export class ForwarderWrapper extends ContractWrapper { * @return Address of Ether token */ public getEtherTokenAddress(): string { - const contractAddress = this._getContractAddress(artifacts.EtherToken); + const contractAddress = this._getContractAddress(artifacts.WETH9); return contractAddress; } // HACK: We don't want this method to be visible to the other units within that package but not to the end user. @@ -251,7 +250,7 @@ export class ForwarderWrapper extends ContractWrapper { private _invalidateContractInstance(): void { delete this._forwarderContractIfExists; } - private async _getForwarderContractAsync(): Promise { + private async _getForwarderContractAsync(): Promise { if (!_.isUndefined(this._forwarderContractIfExists)) { return this._forwarderContractIfExists; } @@ -259,7 +258,7 @@ export class ForwarderWrapper extends ContractWrapper { artifacts.Forwarder, this._contractAddressIfExists, ); - const contractInstance = new ForwarderContract( + const contractInstance = new wrappers.ForwarderContract( abi, address, this._web3Wrapper.getProvider(), diff --git a/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts index 1da88f624..02f9dd4de 100644 --- a/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts @@ -1,3 +1,4 @@ +import { artifacts, wrappers } from '@0xproject/contracts'; import { schemas } from '@0xproject/json-schemas'; import { SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; @@ -5,19 +6,17 @@ import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { ContractAbi } from 'ethereum-types'; import * as _ from 'lodash'; -import { artifacts } from '../artifacts'; import { BalanceAndAllowance, OrderAndTraderInfo, TraderInfo } from '../types'; import { assert } from '../utils/assert'; import { ContractWrapper } from './contract_wrapper'; -import { OrderValidatorContract } from './generated/order_validator'; /** * This class includes the functionality related to interacting with the OrderValidator contract. */ export class OrderValidatorWrapper extends ContractWrapper { public abi: ContractAbi = artifacts.OrderValidator.compilerOutput.abi; - private _orderValidatorContractIfExists?: OrderValidatorContract; + private _orderValidatorContractIfExists?: wrappers.OrderValidatorContract; /** * Instantiate OrderValidatorWrapper * @param web3Wrapper Web3Wrapper instance to use @@ -170,12 +169,12 @@ export class OrderValidatorWrapper extends ContractWrapper { private _invalidateContractInstance(): void { delete this._orderValidatorContractIfExists; } - private async _getOrderValidatorContractAsync(): Promise { + private async _getOrderValidatorContractAsync(): Promise { if (!_.isUndefined(this._orderValidatorContractIfExists)) { return this._orderValidatorContractIfExists; } const [abi, address] = await this._getContractAbiAndAddressFromArtifactsAsync(artifacts.OrderValidator); - const contractInstance = new OrderValidatorContract( + const contractInstance = new wrappers.OrderValidatorContract( abi, address, this._web3Wrapper.getProvider(), diff --git a/packages/contract-wrappers/src/index.ts b/packages/contract-wrappers/src/index.ts index e8a53170e..659394eb6 100644 --- a/packages/contract-wrappers/src/index.ts +++ b/packages/contract-wrappers/src/index.ts @@ -1,3 +1,5 @@ +import { wrappers } from '@0xproject/contracts'; + export { ContractWrappers } from './contract_wrappers'; export { ERC20TokenWrapper } from './contract_wrappers/erc20_token_wrapper'; export { ERC721TokenWrapper } from './contract_wrappers/erc721_token_wrapper'; @@ -54,39 +56,31 @@ export { StateMutability, } from 'ethereum-types'; -export { - WETH9Events, - WETH9WithdrawalEventArgs, - WETH9ApprovalEventArgs, - WETH9EventArgs, - WETH9DepositEventArgs, - WETH9TransferEventArgs, -} from './contract_wrappers/generated/weth9'; +export const WETH9Events = wrappers.WETH9Events; +export type WETH9WithdrawalEventArgs = wrappers.WETH9WithdrawalEventArgs; +export type WETH9ApprovalEventArgs = wrappers.WETH9ApprovalEventArgs; +export type WETH9EventArgs = wrappers.WETH9EventArgs; +export type WETH9DepositEventArgs = wrappers.WETH9DepositEventArgs; +export type WETH9TransferEventArgs = wrappers.WETH9TransferEventArgs; -export { - ERC20TokenTransferEventArgs, - ERC20TokenApprovalEventArgs, - ERC20TokenEvents, - ERC20TokenEventArgs, -} from './contract_wrappers/generated/erc20_token'; +export type ERC20TokenTransferEventArgs = wrappers.ERC20TokenTransferEventArgs; +export type ERC20TokenApprovalEventArgs = wrappers.ERC20TokenApprovalEventArgs; +export const ERC20TokenEvents = wrappers.ERC20TokenEvents; +export type ERC20TokenEventArgs = wrappers.ERC20TokenEventArgs; -export { - ERC721TokenApprovalEventArgs, - ERC721TokenApprovalForAllEventArgs, - ERC721TokenTransferEventArgs, - ERC721TokenEvents, - ERC721TokenEventArgs, -} from './contract_wrappers/generated/erc721_token'; +export type ERC721TokenApprovalEventArgs = wrappers.ERC721TokenApprovalEventArgs; +export type ERC721TokenApprovalForAllEventArgs = wrappers.ERC721TokenApprovalForAllEventArgs; +export type ERC721TokenTransferEventArgs = wrappers.ERC721TokenTransferEventArgs; +export const ERC721TokenEvents = wrappers.ERC721TokenEvents; +export type ERC721TokenEventArgs = wrappers.ERC721TokenEventArgs; -export { - ExchangeCancelUpToEventArgs, - ExchangeAssetProxyRegisteredEventArgs, - ExchangeSignatureValidatorApprovalEventArgs, - ExchangeFillEventArgs, - ExchangeCancelEventArgs, - ExchangeEventArgs, - ExchangeEvents, -} from './contract_wrappers/generated/exchange'; +export type ExchangeCancelUpToEventArgs = wrappers.ExchangeCancelUpToEventArgs; +export type ExchangeAssetProxyRegisteredEventArgs = wrappers.ExchangeAssetProxyRegisteredEventArgs; +export type ExchangeSignatureValidatorApprovalEventArgs = wrappers.ExchangeSignatureValidatorApprovalEventArgs; +export type ExchangeFillEventArgs = wrappers.ExchangeFillEventArgs; +export type ExchangeCancelEventArgs = wrappers.ExchangeCancelEventArgs; +export type ExchangeEventArgs = wrappers.ExchangeEventArgs; +export const ExchangeEvents = wrappers.ExchangeEvents; export { AbstractBalanceAndProxyAllowanceFetcher, AbstractOrderFilledCancelledFetcher } from '@0xproject/order-utils'; diff --git a/packages/contract-wrappers/src/types.ts b/packages/contract-wrappers/src/types.ts index e0b12b7c9..0ac5f05e4 100644 --- a/packages/contract-wrappers/src/types.ts +++ b/packages/contract-wrappers/src/types.ts @@ -1,13 +1,9 @@ +import { wrappers } from '@0xproject/contracts'; import { BigNumber } from '@0xproject/utils'; import { OrderState, SignedOrder } from '@0xproject/types'; import { BlockParam, ContractEventArg, DecodedLogArgs, LogEntryEvent, LogWithDecodedArgs } from 'ethereum-types'; -import { ERC20TokenEventArgs, ERC20TokenEvents } from './contract_wrappers/generated/erc20_token'; -import { ERC721TokenEventArgs, ERC721TokenEvents } from './contract_wrappers/generated/erc721_token'; -import { ExchangeEventArgs, ExchangeEvents } from './contract_wrappers/generated/exchange'; -import { WETH9EventArgs, WETH9Events } from './contract_wrappers/generated/weth9'; - export enum ExchangeWrapperError { AssetDataMismatch = 'ASSET_DATA_MISMATCH', } @@ -60,7 +56,11 @@ export interface ContractEvent { args: ContractEventArgs; } -export type ContractEventArgs = ExchangeEventArgs | ERC20TokenEventArgs | ERC721TokenEventArgs | WETH9EventArgs; +export type ContractEventArgs = + | wrappers.ExchangeEventArgs + | wrappers.ERC20TokenEventArgs + | wrappers.ERC721TokenEventArgs + | wrappers.WETH9EventArgs; // [address, name, symbol, decimals, ipfsHash, swarmHash] export type TokenMetadata = [string, string, string, number, string, string]; @@ -83,7 +83,11 @@ export interface TokenAddressBySymbol { [symbol: string]: string; } -export type ContractEvents = ERC20TokenEvents | ERC721TokenEvents | ExchangeEvents | WETH9Events; +export type ContractEvents = + | wrappers.ERC20TokenEvents + | wrappers.ERC721TokenEvents + | wrappers.ExchangeEvents + | wrappers.WETH9Events; export interface IndexedFilterValues { [index: string]: ContractEventArg; diff --git a/packages/contract-wrappers/src/utils/transaction_encoder.ts b/packages/contract-wrappers/src/utils/transaction_encoder.ts index 33086944b..f4c6de97f 100644 --- a/packages/contract-wrappers/src/utils/transaction_encoder.ts +++ b/packages/contract-wrappers/src/utils/transaction_encoder.ts @@ -1,11 +1,11 @@ +import { wrappers } from '@0xproject/contracts'; + import { schemas } from '@0xproject/json-schemas'; import { eip712Utils } from '@0xproject/order-utils'; import { Order, SignedOrder } from '@0xproject/types'; import { BigNumber, signTypedDataUtils } from '@0xproject/utils'; import _ = require('lodash'); -import { ExchangeContract } from '../contract_wrappers/generated/exchange'; - import { assert } from './assert'; /** @@ -14,8 +14,8 @@ import { assert } from './assert'; * can submit this to the blockchain. The Exchange context executes as if UserA had directly submitted this transaction. */ export class TransactionEncoder { - private readonly _exchangeInstance: ExchangeContract; - constructor(exchangeInstance: ExchangeContract) { + private readonly _exchangeInstance: wrappers.ExchangeContract; + constructor(exchangeInstance: wrappers.ExchangeContract) { this._exchangeInstance = exchangeInstance; } /** @@ -275,7 +275,7 @@ export class TransactionEncoder { ); return abiEncodedData; } - private _getExchangeContract(): ExchangeContract { + private _getExchangeContract(): wrappers.ExchangeContract { return this._exchangeInstance; } } -- cgit v1.2.3 From 3a7bb97ad1182eb9c718797bda8dca5eb5d7f9cd Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Wed, 3 Oct 2018 16:21:17 -0700 Subject: Remove artifacts from migrations package and update contract-wrappers accordingly --- .../contract-wrappers/src/contract_wrappers.ts | 56 +++++++----------- .../src/contract_wrappers/contract_wrapper.ts | 59 +----------------- .../src/contract_wrappers/erc20_proxy_wrapper.ts | 39 ++++-------- .../src/contract_wrappers/erc20_token_wrapper.ts | 28 ++++----- .../src/contract_wrappers/erc721_proxy_wrapper.ts | 39 ++++-------- .../src/contract_wrappers/erc721_token_wrapper.ts | 32 +++++----- .../src/contract_wrappers/ether_token_wrapper.ts | 37 ++++-------- .../src/contract_wrappers/exchange_wrapper.ts | 64 ++++++-------------- .../src/contract_wrappers/forwarder_wrapper.ts | 69 +++++----------------- .../contract_wrappers/order_validator_wrapper.ts | 16 ++--- packages/contract-wrappers/src/types.ts | 8 +-- 11 files changed, 129 insertions(+), 318 deletions(-) (limited to 'packages/contract-wrappers/src') diff --git a/packages/contract-wrappers/src/contract_wrappers.ts b/packages/contract-wrappers/src/contract_wrappers.ts index b272fabda..8e4693da6 100644 --- a/packages/contract-wrappers/src/contract_wrappers.ts +++ b/packages/contract-wrappers/src/contract_wrappers.ts @@ -83,46 +83,29 @@ export class ContractWrappers { const blockPollingIntervalMs = _.isUndefined(config.blockPollingIntervalMs) ? constants.DEFAULT_BLOCK_POLLING_INTERVAL : config.blockPollingIntervalMs; - this.erc20Proxy = new ERC20ProxyWrapper(this._web3Wrapper, config.networkId, config.erc20ProxyContractAddress); - this.erc721Proxy = new ERC721ProxyWrapper( - this._web3Wrapper, - config.networkId, - config.erc721ProxyContractAddress, - ); - this.erc20Token = new ERC20TokenWrapper( - this._web3Wrapper, - config.networkId, - this.erc20Proxy, - blockPollingIntervalMs, - ); - this.erc721Token = new ERC721TokenWrapper( - this._web3Wrapper, - config.networkId, - this.erc721Proxy, - blockPollingIntervalMs, - ); - this.etherToken = new EtherTokenWrapper( - this._web3Wrapper, - config.networkId, - this.erc20Token, - blockPollingIntervalMs, - ); + if (_.isUndefined(config.contractAddresses.erc20Proxy)) { + throw new Error('config.contractAddresses.erc20Proxy is required for testing'); + } + this.erc20Proxy = new ERC20ProxyWrapper(this._web3Wrapper, config.contractAddresses.erc20Proxy); + this.erc721Proxy = new ERC721ProxyWrapper(this._web3Wrapper, config.contractAddresses.erc721Proxy); + this.erc20Token = new ERC20TokenWrapper(this._web3Wrapper, this.erc20Proxy, blockPollingIntervalMs); + this.erc721Token = new ERC721TokenWrapper(this._web3Wrapper, this.erc721Proxy, blockPollingIntervalMs); + this.etherToken = new EtherTokenWrapper(this._web3Wrapper, this.erc20Token, blockPollingIntervalMs); this.exchange = new ExchangeWrapper( this._web3Wrapper, - config.networkId, this.erc20Token, this.erc721Token, - config.exchangeContractAddress, - config.zrxContractAddress, + config.contractAddresses.exchange, + config.contractAddresses.zrxToken, blockPollingIntervalMs, ); this.forwarder = new ForwarderWrapper( this._web3Wrapper, - config.networkId, - config.forwarderContractAddress, - config.zrxContractAddress, + config.contractAddresses.forwarder, + config.contractAddresses.zrxToken, + config.contractAddresses.etherToken, ); - this.orderValidator = new OrderValidatorWrapper(this._web3Wrapper, config.networkId); + this.orderValidator = new OrderValidatorWrapper(this._web3Wrapper, config.contractAddresses.orderValidator); } /** * Sets a new web3 provider for 0x.js. Updating the provider will stop all @@ -130,16 +113,17 @@ export class ContractWrappers { * @param provider The Web3Provider you would like the 0x.js library to use from now on. * @param networkId The id of the network your provider is connected to */ - public setProvider(provider: Provider, networkId: number): void { + public setProvider(provider: Provider): void { + // TODO(albrow): Make sure all contract wrappers are called below. this._web3Wrapper.setProvider(provider); (this.exchange as any)._invalidateContractInstances(); - (this.exchange as any)._setNetworkId(networkId); (this.erc20Token as any)._invalidateContractInstances(); - (this.erc20Token as any)._setNetworkId(networkId); (this.erc20Proxy as any)._invalidateContractInstance(); - (this.erc20Proxy as any)._setNetworkId(networkId); + (this.erc721Token as any)._invalidateContractInstances(); + (this.erc721Proxy as any)._invalidateContractInstance(); (this.etherToken as any)._invalidateContractInstance(); - (this.etherToken as any)._setNetworkId(networkId); + (this.forwarder as any)._invalidateContractInstance(); + (this.orderValidator as any)._invalidateContractInstance(); } /** * Get the provider instance currently used by 0x.js diff --git a/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts index f7a89e3be..aed9d44db 100644 --- a/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts @@ -3,7 +3,6 @@ import { marshaller, Web3Wrapper } from '@0xproject/web3-wrapper'; import { BlockParamLiteral, ContractAbi, - ContractArtifact, FilterObject, LogEntry, LogWithDecodedArgs, @@ -24,22 +23,9 @@ import { import { constants } from '../utils/constants'; import { filterUtils } from '../utils/filter_utils'; -const CONTRACT_NAME_TO_NOT_FOUND_ERROR: { - [contractName: string]: ContractWrappersError; -} = { - ZRX: ContractWrappersError.ZRXContractDoesNotExist, - EtherToken: ContractWrappersError.EtherTokenContractDoesNotExist, - ERC20Token: ContractWrappersError.ERC20TokenContractDoesNotExist, - ERC20Proxy: ContractWrappersError.ERC20ProxyContractDoesNotExist, - ERC721Token: ContractWrappersError.ERC721TokenContractDoesNotExist, - ERC721Proxy: ContractWrappersError.ERC721ProxyContractDoesNotExist, - Exchange: ContractWrappersError.ExchangeContractDoesNotExist, -}; - export abstract class ContractWrapper { public abstract abi: ContractAbi; protected _web3Wrapper: Web3Wrapper; - protected _networkId: number; private _blockAndLogStreamerIfExists: BlockAndLogStreamer | undefined; private _blockPollingIntervalMs: number; private _blockAndLogStreamIntervalIfExists?: NodeJS.Timer; @@ -56,9 +42,8 @@ export abstract class ContractWrapper { logUtils.warn(err); } } - constructor(web3Wrapper: Web3Wrapper, networkId: number, blockPollingIntervalMs?: number) { + constructor(web3Wrapper: Web3Wrapper, blockPollingIntervalMs?: number) { this._web3Wrapper = web3Wrapper; - this._networkId = networkId; this._blockPollingIntervalMs = _.isUndefined(blockPollingIntervalMs) ? constants.DEFAULT_BLOCK_POLLING_INTERVAL : blockPollingIntervalMs; @@ -124,40 +109,6 @@ export abstract class ContractWrapper { const logWithDecodedArgs = abiDecoder.tryToDecodeLogOrNoop(log); return logWithDecodedArgs; } - protected async _getContractAbiAndAddressFromArtifactsAsync( - artifact: ContractArtifact, - addressIfExists?: string, - ): Promise<[ContractAbi, string]> { - let contractAddress: string; - if (_.isUndefined(addressIfExists)) { - if (_.isUndefined(artifact.networks[this._networkId])) { - throw new Error(ContractWrappersError.ContractNotDeployedOnNetwork); - } - contractAddress = artifact.networks[this._networkId].address.toLowerCase(); - } else { - contractAddress = addressIfExists; - } - const doesContractExist = await this._web3Wrapper.doesContractExistAtAddressAsync(contractAddress); - if (!doesContractExist) { - throw new Error(CONTRACT_NAME_TO_NOT_FOUND_ERROR[artifact.contractName]); - } - const abiAndAddress: [ContractAbi, string] = [artifact.compilerOutput.abi, contractAddress]; - return abiAndAddress; - } - protected _getContractAddress(artifact: ContractArtifact, addressIfExists?: string): string { - if (_.isUndefined(addressIfExists)) { - if (_.isUndefined(artifact.networks[this._networkId])) { - throw new Error(ContractWrappersError.ContractNotDeployedOnNetwork); - } - const contractAddress = artifact.networks[this._networkId].address; - if (_.isUndefined(contractAddress)) { - throw new Error(CONTRACT_NAME_TO_NOT_FOUND_ERROR[artifact.contractName]); - } - return contractAddress; - } else { - return addressIfExists; - } - } private _onLogStateChanged(isRemoved: boolean, rawLog: RawLogEntry): void { const log: LogEntry = marshaller.unmarshalLog(rawLog); _.forEach(this._filters, (filter: FilterObject, filterToken: string) => { @@ -222,14 +173,6 @@ export abstract class ContractWrapper { }); return logs as RawLogEntry[]; } - // HACK: This should be a package-scoped method (which doesn't exist in TS) - // We don't want this method available in the public interface for all classes - // who inherit from ContractWrapper, and it is only used by the internal implementation - // of those higher classes. - // tslint:disable-next-line:no-unused-variable - private _setNetworkId(networkId: number): void { - this._networkId = networkId; - } private _stopBlockAndLogStream(): void { if (_.isUndefined(this._blockAndLogStreamerIfExists)) { throw new Error(ContractWrappersError.SubscriptionNotFound); diff --git a/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts index 5900f0502..31b3b6755 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts @@ -13,25 +13,25 @@ import { ContractWrapper } from './contract_wrapper'; */ export class ERC20ProxyWrapper extends ContractWrapper { public abi: ContractAbi = artifacts.ERC20Proxy.compilerOutput.abi; + public address: string; private _erc20ProxyContractIfExists?: wrappers.ERC20ProxyContract; - private _contractAddressIfExists?: string; /** * Instantiate ERC20ProxyWrapper * @param web3Wrapper Web3Wrapper instance to use - * @param networkId Desired networkId - * @param contractAddressIfExists The contract address to use. This is usually pulled from - * the artifacts but needs to be specified when using with your own custom testnet. + * @param address The address of the ERC20Proxy contract */ - constructor(web3Wrapper: Web3Wrapper, networkId: number, contractAddressIfExists?: string) { - super(web3Wrapper, networkId); - this._contractAddressIfExists = contractAddressIfExists; + // TODO(albrow): Make address optional and default to looking up the address + // based in a hard-coded mapping based on web3Wrapper network id. + constructor(web3Wrapper: Web3Wrapper, address: string) { + super(web3Wrapper); + this.address = address; } /** * Get the 4 bytes ID of this asset proxy * @return Proxy id */ public async getProxyIdAsync(): Promise { - const ERC20ProxyContractInstance = await this._getERC20ProxyContractAsync(); + const ERC20ProxyContractInstance = this._getERC20ProxyContract(); const proxyId = (await ERC20ProxyContractInstance.getProxyId.callAsync()) as AssetProxyId; return proxyId; } @@ -43,7 +43,7 @@ export class ERC20ProxyWrapper extends ContractWrapper { public async isAuthorizedAsync(exchangeContractAddress: string): Promise { assert.isETHAddressHex('exchangeContractAddress', exchangeContractAddress); const normalizedExchangeContractAddress = exchangeContractAddress.toLowerCase(); - const ERC20ProxyContractInstance = await this._getERC20ProxyContractAsync(); + const ERC20ProxyContractInstance = this._getERC20ProxyContract(); const isAuthorized = await ERC20ProxyContractInstance.authorized.callAsync(normalizedExchangeContractAddress); return isAuthorized; } @@ -52,36 +52,23 @@ export class ERC20ProxyWrapper extends ContractWrapper { * @return The list of authorized addresses. */ public async getAuthorizedAddressesAsync(): Promise { - const ERC20ProxyContractInstance = await this._getERC20ProxyContractAsync(); + const ERC20ProxyContractInstance = this._getERC20ProxyContract(); const authorizedAddresses = await ERC20ProxyContractInstance.getAuthorizedAddresses.callAsync(); return authorizedAddresses; } - /** - * Retrieves the Ethereum address of the ERC20Proxy contract deployed on the network - * that the user-passed web3 provider is connected to. - * @returns The Ethereum address of the ERC20Proxy contract being used. - */ - public getContractAddress(): string { - const contractAddress = this._getContractAddress(artifacts.ERC20Proxy, this._contractAddressIfExists); - return contractAddress; - } // 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 async _getERC20ProxyContractAsync(): Promise { + private _getERC20ProxyContract(): wrappers.ERC20ProxyContract { if (!_.isUndefined(this._erc20ProxyContractIfExists)) { return this._erc20ProxyContractIfExists; } - const [abi, address] = await this._getContractAbiAndAddressFromArtifactsAsync( - artifacts.ERC20Proxy, - this._contractAddressIfExists, - ); const contractInstance = new wrappers.ERC20ProxyContract( - abi, - address, + this.abi, + this.address, this._web3Wrapper.getProvider(), this._web3Wrapper.getContractDefaults(), ); diff --git a/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts index 3be5bcaee..53cda5d08 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts @@ -36,17 +36,11 @@ export class ERC20TokenWrapper extends ContractWrapper { /** * Instantiate ERC20TokenWrapper * @param web3Wrapper Web3Wrapper instance to use - * @param networkId Desired networkId * @param erc20ProxyWrapper The ERC20ProxyWrapper instance to use * @param blockPollingIntervalMs The block polling interval to use for active subscriptions */ - constructor( - web3Wrapper: Web3Wrapper, - networkId: number, - erc20ProxyWrapper: ERC20ProxyWrapper, - blockPollingIntervalMs?: number, - ) { - super(web3Wrapper, networkId, blockPollingIntervalMs); + constructor(web3Wrapper: Web3Wrapper, erc20ProxyWrapper: ERC20ProxyWrapper, blockPollingIntervalMs?: number) { + super(web3Wrapper, blockPollingIntervalMs); this._tokenContractsByAddress = {}; this._erc20ProxyWrapper = erc20ProxyWrapper; } @@ -188,7 +182,7 @@ export class ERC20TokenWrapper extends ContractWrapper { ownerAddress: string, methodOpts: MethodOpts = {}, ): Promise { - const proxyAddress = this._erc20ProxyWrapper.getContractAddress(); + const proxyAddress = this._erc20ProxyWrapper.address; const allowanceInBaseUnits = await this.getAllowanceAsync(tokenAddress, ownerAddress, proxyAddress, methodOpts); return allowanceInBaseUnits; } @@ -208,7 +202,7 @@ export class ERC20TokenWrapper extends ContractWrapper { amountInBaseUnits: BigNumber, txOpts: TransactionOpts = {}, ): Promise { - const proxyAddress = this._erc20ProxyWrapper.getContractAddress(); + const proxyAddress = this._erc20ProxyWrapper.address; const txHash = await this.setAllowanceAsync( tokenAddress, ownerAddress, @@ -434,13 +428,15 @@ export class ERC20TokenWrapper extends ContractWrapper { if (!_.isUndefined(tokenContract)) { return tokenContract; } - const [abi, address] = await this._getContractAbiAndAddressFromArtifactsAsync( - artifacts.ERC20Token, - normalizedTokenAddress, - ); + // TODO(albrow): Do we really still need this check? The default error + // looks okay to me. + const doesContractExist = await this._web3Wrapper.doesContractExistAtAddressAsync(tokenAddress); + if (!doesContractExist) { + throw new Error(ContractWrappersError.ERC20TokenContractDoesNotExist); + } const contractInstance = new wrappers.ERC20TokenContract( - abi, - address, + this.abi, + normalizedTokenAddress, this._web3Wrapper.getProvider(), this._web3Wrapper.getContractDefaults(), ); diff --git a/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts index 6ba162213..2472d512b 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts @@ -13,25 +13,25 @@ import { ContractWrapper } from './contract_wrapper'; */ export class ERC721ProxyWrapper extends ContractWrapper { public abi: ContractAbi = artifacts.ERC20Proxy.compilerOutput.abi; + public address: string; private _erc721ProxyContractIfExists?: wrappers.ERC721ProxyContract; - private _contractAddressIfExists?: string; /** * Instantiate ERC721ProxyWrapper * @param web3Wrapper Web3Wrapper instance to use - * @param networkId Desired networkId - * @param contractAddressIfExists The contract address to use. This is usually pulled from - * the artifacts but needs to be specified when using with your own custom testnet. + * @param address The address of the ERC721Proxy contract */ - constructor(web3Wrapper: Web3Wrapper, networkId: number, contractAddressIfExists?: string) { - super(web3Wrapper, networkId); - this._contractAddressIfExists = contractAddressIfExists; + // TODO(albrow): Make address optional and default to looking up the address + // based in a hard-coded mapping based on web3Wrapper network id. + constructor(web3Wrapper: Web3Wrapper, address: string) { + super(web3Wrapper); + this.address = address; } /** * Get the 4 bytes ID of this asset proxy * @return Proxy id */ public async getProxyIdAsync(): Promise { - const ERC721ProxyContractInstance = await this._getERC721ProxyContractAsync(); + const ERC721ProxyContractInstance = await this._getERC721ProxyContract(); const proxyId = (await ERC721ProxyContractInstance.getProxyId.callAsync()) as AssetProxyId; return proxyId; } @@ -43,7 +43,7 @@ export class ERC721ProxyWrapper extends ContractWrapper { public async isAuthorizedAsync(exchangeContractAddress: string): Promise { assert.isETHAddressHex('exchangeContractAddress', exchangeContractAddress); const normalizedExchangeContractAddress = exchangeContractAddress.toLowerCase(); - const ERC721ProxyContractInstance = await this._getERC721ProxyContractAsync(); + const ERC721ProxyContractInstance = await this._getERC721ProxyContract(); const isAuthorized = await ERC721ProxyContractInstance.authorized.callAsync(normalizedExchangeContractAddress); return isAuthorized; } @@ -52,36 +52,23 @@ export class ERC721ProxyWrapper extends ContractWrapper { * @return The list of authorized addresses. */ public async getAuthorizedAddressesAsync(): Promise { - const ERC721ProxyContractInstance = await this._getERC721ProxyContractAsync(); + const ERC721ProxyContractInstance = await this._getERC721ProxyContract(); const authorizedAddresses = await ERC721ProxyContractInstance.getAuthorizedAddresses.callAsync(); return authorizedAddresses; } - /** - * Retrieves the Ethereum address of the ERC721Proxy contract deployed on the network - * that the user-passed web3 provider is connected to. - * @returns The Ethereum address of the ERC721Proxy contract being used. - */ - public getContractAddress(): string { - const contractAddress = this._getContractAddress(artifacts.ERC721Proxy, this._contractAddressIfExists); - return contractAddress; - } // 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 async _getERC721ProxyContractAsync(): Promise { + private _getERC721ProxyContract(): wrappers.ERC721ProxyContract { if (!_.isUndefined(this._erc721ProxyContractIfExists)) { return this._erc721ProxyContractIfExists; } - const [abi, address] = await this._getContractAbiAndAddressFromArtifactsAsync( - artifacts.ERC721Proxy, - this._contractAddressIfExists, - ); const contractInstance = new wrappers.ERC721ProxyContract( - abi, - address, + this.abi, + this.address, this._web3Wrapper.getProvider(), this._web3Wrapper.getContractDefaults(), ); diff --git a/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts index 23b335710..7fcd74eaf 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts @@ -35,17 +35,11 @@ export class ERC721TokenWrapper extends ContractWrapper { /** * Instantiate ERC721TokenWrapper * @param web3Wrapper Web3Wrapper instance to use - * @param networkId Desired networkId * @param erc721ProxyWrapper The ERC721ProxyWrapper instance to use * @param blockPollingIntervalMs The block polling interval to use for active subscriptions */ - constructor( - web3Wrapper: Web3Wrapper, - networkId: number, - erc721ProxyWrapper: ERC721ProxyWrapper, - blockPollingIntervalMs?: number, - ) { - super(web3Wrapper, networkId, blockPollingIntervalMs); + constructor(web3Wrapper: Web3Wrapper, erc721ProxyWrapper: ERC721ProxyWrapper, blockPollingIntervalMs?: number) { + super(web3Wrapper, blockPollingIntervalMs); this._tokenContractsByAddress = {}; this._erc721ProxyWrapper = erc721ProxyWrapper; } @@ -149,7 +143,7 @@ export class ERC721TokenWrapper extends ContractWrapper { ownerAddress: string, methodOpts: MethodOpts = {}, ): Promise { - const proxyAddress = this._erc721ProxyWrapper.getContractAddress(); + const proxyAddress = this._erc721ProxyWrapper.address; const isProxyApprovedForAll = await this.isApprovedForAllAsync( tokenAddress, ownerAddress, @@ -198,7 +192,7 @@ export class ERC721TokenWrapper extends ContractWrapper { tokenId: BigNumber, methodOpts: MethodOpts = {}, ): Promise { - const proxyAddress = this._erc721ProxyWrapper.getContractAddress(); + const proxyAddress = this._erc721ProxyWrapper.address; const approvedAddress = await this.getApprovedIfExistsAsync(tokenAddress, tokenId, methodOpts); const isProxyApproved = approvedAddress === proxyAddress; return isProxyApproved; @@ -259,7 +253,7 @@ export class ERC721TokenWrapper extends ContractWrapper { isApproved: boolean, txOpts: TransactionOpts = {}, ): Promise { - const proxyAddress = this._erc721ProxyWrapper.getContractAddress(); + const proxyAddress = this._erc721ProxyWrapper.address; const txHash = await this.setApprovalForAllAsync(tokenAddress, ownerAddress, proxyAddress, isApproved, txOpts); return txHash; } @@ -317,7 +311,7 @@ export class ERC721TokenWrapper extends ContractWrapper { tokenId: BigNumber, txOpts: TransactionOpts = {}, ): Promise { - const proxyAddress = this._erc721ProxyWrapper.getContractAddress(); + const proxyAddress = this._erc721ProxyWrapper.address; const txHash = await this.setApprovalAsync(tokenAddress, proxyAddress, tokenId, txOpts); return txHash; } @@ -461,13 +455,15 @@ export class ERC721TokenWrapper extends ContractWrapper { if (!_.isUndefined(tokenContract)) { return tokenContract; } - const [abi, address] = await this._getContractAbiAndAddressFromArtifactsAsync( - artifacts.ERC721Token, - normalizedTokenAddress, - ); + // TODO(albrow): Do we really still need this check? The default error + // looks okay to me. + const doesContractExist = await this._web3Wrapper.doesContractExistAtAddressAsync(tokenAddress); + if (!doesContractExist) { + throw new Error(ContractWrappersError.ERC721TokenContractDoesNotExist); + } const contractInstance = new wrappers.ERC721TokenContract( - abi, - address, + this.abi, + normalizedTokenAddress, this._web3Wrapper.getProvider(), this._web3Wrapper.getContractDefaults(), ); diff --git a/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts index 30653adf2..526324abd 100644 --- a/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts @@ -30,13 +30,8 @@ export class EtherTokenWrapper extends ContractWrapper { * @param erc20TokenWrapper The ERC20TokenWrapper instance to use * @param blockPollingIntervalMs The block polling interval to use for active subscriptions */ - constructor( - web3Wrapper: Web3Wrapper, - networkId: number, - erc20TokenWrapper: ERC20TokenWrapper, - blockPollingIntervalMs?: number, - ) { - super(web3Wrapper, networkId, blockPollingIntervalMs); + constructor(web3Wrapper: Web3Wrapper, erc20TokenWrapper: ERC20TokenWrapper, blockPollingIntervalMs?: number) { + super(web3Wrapper, blockPollingIntervalMs); this._erc20TokenWrapper = erc20TokenWrapper; } /** @@ -191,19 +186,6 @@ export class EtherTokenWrapper extends ContractWrapper { public unsubscribeAll(): void { super._unsubscribeAll(); } - /** - * Retrieves the Ethereum address of the EtherToken contract deployed on the network - * that the user-passed web3 provider is connected to. If it's not Kovan, Ropsten, Rinkeby, Mainnet or TestRPC - * (networkId: 50), it will return undefined (e.g a private network). - * @returns The Ethereum address of the EtherToken contract or undefined. - */ - public getContractAddressIfExists(): string | undefined { - const networkSpecificArtifact = artifacts.WETH9.networks[this._networkId]; - const contractAddressIfExists = _.isUndefined(networkSpecificArtifact) - ? undefined - : networkSpecificArtifact.address; - return contractAddressIfExists; - } // tslint:disable-next-line:no-unused-variable private _invalidateContractInstance(): void { this.unsubscribeAll(); @@ -214,13 +196,16 @@ export class EtherTokenWrapper extends ContractWrapper { if (!_.isUndefined(etherTokenContract)) { return etherTokenContract; } - const [abi, address] = await this._getContractAbiAndAddressFromArtifactsAsync( - artifacts.WETH9, - etherTokenAddress, - ); + // TODO(albrow): Do we really still need this check? The default error + // looks okay to me. + // TODO(albrow): Should we normalize the token address here? + const doesContractExist = await this._web3Wrapper.doesContractExistAtAddressAsync(etherTokenAddress); + if (!doesContractExist) { + throw new Error(ContractWrappersError.EtherTokenContractDoesNotExist); + } const contractInstance = new wrappers.WETH9Contract( - abi, - address, + this.abi, + etherTokenAddress, this._web3Wrapper.getProvider(), this._web3Wrapper.getContractDefaults(), ); diff --git a/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts index 613747720..b65baee55 100644 --- a/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts @@ -24,7 +24,6 @@ import { IndexedFilterValues, MethodOpts, OrderInfo, - OrderStatus, OrderTransactionOpts, ValidateOrderFillableOpts, } from '../types'; @@ -42,35 +41,34 @@ import { ERC721TokenWrapper } from './erc721_token_wrapper'; */ export class ExchangeWrapper extends ContractWrapper { public abi: ContractAbi = artifacts.Exchange.compilerOutput.abi; + public address: string; + public zrxTokenAddress: string; private _exchangeContractIfExists?: wrappers.ExchangeContract; private _erc721TokenWrapper: ERC721TokenWrapper; private _erc20TokenWrapper: ERC20TokenWrapper; - private _contractAddressIfExists?: string; - private _zrxContractAddressIfExists?: string; /** * Instantiate ExchangeWrapper - * @param web3Wrapper Web3Wrapper instance to use - * @param networkId Desired networkId - * @param contractAddressIfExists The exchange contract address to use. This is usually pulled from - * the artifacts but needs to be specified when using with your own custom testnet. - * @param zrxContractAddressIfExists The ZRXToken contract address to use. This is usually pulled from - * the artifacts but needs to be specified when using with your own custom testnet. - * @param blockPollingIntervalMs The block polling interval to use for active subscriptions + * @param web3Wrapper Web3Wrapper instance to use. + * @param erc20TokenWrapper ERC20TokenWrapper instance to use. + * @param erc721TokenWrapper ERC721TokenWrapper instance to use. + * @param address The address of the Exchange contract. + * @param zrxTokenAddress The address of the ZRX Token contract. + * @param blockPollingIntervalMs The block polling interval to use for active subscriptions. */ constructor( web3Wrapper: Web3Wrapper, - networkId: number, erc20TokenWrapper: ERC20TokenWrapper, erc721TokenWrapper: ERC721TokenWrapper, - contractAddressIfExists?: string, - zrxContractAddressIfExists?: string, + // TODO(albrow): Make address optional? + address: string, + zrxTokenAddress: string, blockPollingIntervalMs?: number, ) { - super(web3Wrapper, networkId, blockPollingIntervalMs); + super(web3Wrapper, blockPollingIntervalMs); this._erc20TokenWrapper = erc20TokenWrapper; this._erc721TokenWrapper = erc721TokenWrapper; - this._contractAddressIfExists = contractAddressIfExists; - this._zrxContractAddressIfExists = zrxContractAddressIfExists; + this.address = address; + this.zrxTokenAddress = zrxTokenAddress; } /** * Retrieve the address of an asset proxy by signature. @@ -1050,9 +1048,8 @@ export class ExchangeWrapper extends ContractWrapper { assert.doesBelongToStringEnum('eventName', eventName, wrappers.ExchangeEvents); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); assert.isFunction('callback', callback); - const exchangeContractAddress = this.getContractAddress(); const subscriptionToken = this._subscribe( - exchangeContractAddress, + this.address, eventName, indexFilterValues, artifacts.Exchange.compilerOutput.abi, @@ -1090,9 +1087,8 @@ export class ExchangeWrapper extends ContractWrapper { assert.doesBelongToStringEnum('eventName', eventName, wrappers.ExchangeEvents); assert.doesConformToSchema('blockRange', blockRange, schemas.blockRangeSchema); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); - const exchangeContractAddress = this.getContractAddress(); const logs = await this._getLogsAsync( - exchangeContractAddress, + this.address, eventName, blockRange, indexFilterValues, @@ -1158,30 +1154,12 @@ export class ExchangeWrapper extends ContractWrapper { this.getZRXAssetData(), ); } - /** - * Retrieves the Ethereum address of the Exchange contract deployed on the network - * that the user-passed web3 provider is connected to. - * @returns The Ethereum address of the Exchange contract being used. - */ - public getContractAddress(): string { - const contractAddress = this._getContractAddress(artifacts.Exchange, this._contractAddressIfExists); - return contractAddress; - } - /** - * Returns the ZRX token address used by the exchange contract. - * @return Address of ZRX token - */ - public getZRXTokenAddress(): string { - const contractAddress = this._getContractAddress(artifacts.ZRXToken, this._zrxContractAddressIfExists); - return contractAddress; - } /** * Returns the ZRX asset data used by the exchange contract. * @return ZRX asset data */ public getZRXAssetData(): string { - const zrxTokenAddress = this.getZRXTokenAddress(); - const zrxAssetData = assetDataUtils.encodeERC20AssetData(zrxTokenAddress); + const zrxAssetData = assetDataUtils.encodeERC20AssetData(this.zrxTokenAddress); return zrxAssetData; } /** @@ -1204,13 +1182,9 @@ export class ExchangeWrapper extends ContractWrapper { if (!_.isUndefined(this._exchangeContractIfExists)) { return this._exchangeContractIfExists; } - const [abi, address] = await this._getContractAbiAndAddressFromArtifactsAsync( - artifacts.Exchange, - this._contractAddressIfExists, - ); const contractInstance = new wrappers.ExchangeContract( - abi, - address, + this.abi, + this.address, this._web3Wrapper.getProvider(), this._web3Wrapper.getContractDefaults(), ); diff --git a/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts index 854a8381b..cd8f9ece9 100644 --- a/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts @@ -22,18 +22,16 @@ import { ContractWrapper } from './contract_wrapper'; */ export class ForwarderWrapper extends ContractWrapper { public abi: ContractAbi = artifacts.Forwarder.compilerOutput.abi; + public address: string; + public zrxTokenAddress: string; + public etherTokenAddress: string; private _forwarderContractIfExists?: wrappers.ForwarderContract; - private _contractAddressIfExists?: string; - private _zrxContractAddressIfExists?: string; - constructor( - web3Wrapper: Web3Wrapper, - networkId: number, - contractAddressIfExists?: string, - zrxContractAddressIfExists?: string, - ) { - super(web3Wrapper, networkId); - this._contractAddressIfExists = contractAddressIfExists; - this._zrxContractAddressIfExists = zrxContractAddressIfExists; + // TODO(albrow): Make addresses optional? + constructor(web3Wrapper: Web3Wrapper, address: string, zrxTokenAddress: string, etherTokenAddress: string) { + super(web3Wrapper); + this.address = address; + this.zrxTokenAddress = zrxTokenAddress; + this.etherTokenAddress = etherTokenAddress; } /** * Purchases as much of orders' makerAssets as possible by selling up to 95% of transaction's ETH value. @@ -72,12 +70,8 @@ export class ForwarderWrapper extends ContractWrapper { assert.isETHAddressHex('feeRecipientAddress', feeRecipientAddress); assert.doesConformToSchema('orderTransactionOpts', orderTransactionOpts, orderTxOptsSchema, [txOptsSchema]); // other assertions - assert.ordersCanBeUsedForForwarderContract(signedOrders, this.getEtherTokenAddress()); - assert.feeOrdersCanBeUsedForForwarderContract( - signedFeeOrders, - this.getZRXTokenAddress(), - this.getEtherTokenAddress(), - ); + assert.ordersCanBeUsedForForwarderContract(signedOrders, this.etherTokenAddress); + assert.feeOrdersCanBeUsedForForwarderContract(signedFeeOrders, this.zrxTokenAddress, this.etherTokenAddress); // format feePercentage const formattedFeePercentage = utils.numberPercentageToEtherTokenAmountPercentage(feePercentage); // lowercase input addresses @@ -164,12 +158,8 @@ export class ForwarderWrapper extends ContractWrapper { assert.isETHAddressHex('feeRecipientAddress', feeRecipientAddress); assert.doesConformToSchema('orderTransactionOpts', orderTransactionOpts, orderTxOptsSchema, [txOptsSchema]); // other assertions - assert.ordersCanBeUsedForForwarderContract(signedOrders, this.getEtherTokenAddress()); - assert.feeOrdersCanBeUsedForForwarderContract( - signedFeeOrders, - this.getZRXTokenAddress(), - this.getEtherTokenAddress(), - ); + assert.ordersCanBeUsedForForwarderContract(signedOrders, this.etherTokenAddress); + assert.feeOrdersCanBeUsedForForwarderContract(signedFeeOrders, this.zrxTokenAddress, this.etherTokenAddress); // format feePercentage const formattedFeePercentage = utils.numberPercentageToEtherTokenAmountPercentage(feePercentage); // lowercase input addresses @@ -219,31 +209,6 @@ export class ForwarderWrapper extends ContractWrapper { ); return txHash; } - /** - * Retrieves the Ethereum address of the Forwarder contract deployed on the network - * that the user-passed web3 provider is connected to. - * @returns The Ethereum address of the Forwarder contract being used. - */ - public getContractAddress(): string { - const contractAddress = this._getContractAddress(artifacts.Forwarder, this._contractAddressIfExists); - return contractAddress; - } - /** - * Returns the ZRX token address used by the forwarder contract. - * @return Address of ZRX token - */ - public getZRXTokenAddress(): string { - const contractAddress = this._getContractAddress(artifacts.ZRXToken, this._zrxContractAddressIfExists); - return contractAddress; - } - /** - * Returns the Ether token address used by the forwarder contract. - * @return Address of Ether token - */ - public getEtherTokenAddress(): string { - const contractAddress = this._getContractAddress(artifacts.WETH9); - return contractAddress; - } // 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 @@ -254,13 +219,9 @@ export class ForwarderWrapper extends ContractWrapper { if (!_.isUndefined(this._forwarderContractIfExists)) { return this._forwarderContractIfExists; } - const [abi, address] = await this._getContractAbiAndAddressFromArtifactsAsync( - artifacts.Forwarder, - this._contractAddressIfExists, - ); const contractInstance = new wrappers.ForwarderContract( - abi, - address, + this.abi, + this.address, this._web3Wrapper.getProvider(), this._web3Wrapper.getContractDefaults(), ); diff --git a/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts index 02f9dd4de..23e0d42c1 100644 --- a/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts @@ -16,14 +16,17 @@ import { ContractWrapper } from './contract_wrapper'; */ export class OrderValidatorWrapper extends ContractWrapper { public abi: ContractAbi = artifacts.OrderValidator.compilerOutput.abi; + public address: string; private _orderValidatorContractIfExists?: wrappers.OrderValidatorContract; /** * Instantiate OrderValidatorWrapper - * @param web3Wrapper Web3Wrapper instance to use - * @param networkId Desired networkId + * @param web3Wrapper Web3Wrapper instance to use. + * @param address The address of the OrderValidator contract. */ - constructor(web3Wrapper: Web3Wrapper, networkId: number) { - super(web3Wrapper, networkId); + // TODO(albrow): Make address optional? + constructor(web3Wrapper: Web3Wrapper, address: string) { + super(web3Wrapper); + this.address = address; } /** * Get an object conforming to OrderAndTraderInfo containing on-chain information of the provided order and address @@ -173,10 +176,9 @@ export class OrderValidatorWrapper extends ContractWrapper { if (!_.isUndefined(this._orderValidatorContractIfExists)) { return this._orderValidatorContractIfExists; } - const [abi, address] = await this._getContractAbiAndAddressFromArtifactsAsync(artifacts.OrderValidator); const contractInstance = new wrappers.OrderValidatorContract( - abi, - address, + this.abi, + this.address, this._web3Wrapper.getProvider(), this._web3Wrapper.getContractDefaults(), ); diff --git a/packages/contract-wrappers/src/types.ts b/packages/contract-wrappers/src/types.ts index 0ac5f05e4..f882cc188 100644 --- a/packages/contract-wrappers/src/types.ts +++ b/packages/contract-wrappers/src/types.ts @@ -1,7 +1,7 @@ import { wrappers } from '@0xproject/contracts'; +import { ContractAddresses, OrderState, SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; -import { OrderState, SignedOrder } from '@0xproject/types'; import { BlockParam, ContractEventArg, DecodedLogArgs, LogEntryEvent, LogWithDecodedArgs } from 'ethereum-types'; export enum ExchangeWrapperError { @@ -120,11 +120,7 @@ export type SyncMethod = (...args: any[]) => any; export interface ContractWrappersConfig { networkId: number; gasPrice?: BigNumber; - exchangeContractAddress?: string; - zrxContractAddress?: string; - erc20ProxyContractAddress?: string; - erc721ProxyContractAddress?: string; - forwarderContractAddress?: string; + contractAddresses: ContractAddresses; blockPollingIntervalMs?: number; } -- cgit v1.2.3 From 2bd7b0f66bd28792281ba025cf005c666e7f767e Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Thu, 4 Oct 2018 14:40:58 -0700 Subject: update contract_wrappers to use new artifacts and abi-gen wrappers packages --- .../contract-wrappers/src/contract_wrappers.ts | 28 +++++++++--- .../src/contract_wrappers/erc20_proxy_wrapper.ts | 11 ++--- .../src/contract_wrappers/erc20_token_wrapper.ts | 27 ++++++------ .../src/contract_wrappers/erc721_proxy_wrapper.ts | 11 ++--- .../src/contract_wrappers/erc721_token_wrapper.ts | 27 ++++++------ .../src/contract_wrappers/ether_token_wrapper.ts | 27 ++++++------ .../src/contract_wrappers/exchange_wrapper.ts | 27 ++++++------ .../src/contract_wrappers/forwarder_wrapper.ts | 11 ++--- .../contract_wrappers/order_validator_wrapper.ts | 11 ++--- packages/contract-wrappers/src/globals.d.ts | 6 --- packages/contract-wrappers/src/index.ts | 51 ++++++++++------------ packages/contract-wrappers/src/types.ts | 23 +++++----- .../src/utils/transaction_encoder.ts | 8 ++-- 13 files changed, 142 insertions(+), 126 deletions(-) delete mode 100644 packages/contract-wrappers/src/globals.d.ts (limited to 'packages/contract-wrappers/src') diff --git a/packages/contract-wrappers/src/contract_wrappers.ts b/packages/contract-wrappers/src/contract_wrappers.ts index 8e4693da6..359307da4 100644 --- a/packages/contract-wrappers/src/contract_wrappers.ts +++ b/packages/contract-wrappers/src/contract_wrappers.ts @@ -1,4 +1,13 @@ -import { artifacts } from '@0xproject/contracts'; +import { + ERC20Proxy, + ERC20Token, + ERC721Proxy, + ERC721Token, + Exchange, + Forwarder, + OrderValidator, + WETH9, +} from '@0xproject/contract-artifacts'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { Provider } from 'ethereum-types'; import * as _ from 'lodash'; @@ -71,14 +80,22 @@ export class ContractWrappers { contractWrappersPrivateNetworkConfigSchema, contractWrappersPublicNetworkConfigSchema, ]); - const artifactJSONs = _.values(artifacts); - const abiArrays = _.map(artifactJSONs, artifact => artifact.compilerOutput.abi); const txDefaults = { gasPrice: config.gasPrice, }; this._web3Wrapper = new Web3Wrapper(provider, txDefaults); - _.forEach(abiArrays, abi => { - this._web3Wrapper.abiDecoder.addABI(abi); + const artifactsArray = [ + ERC20Proxy, + ERC20Token, + ERC721Proxy, + ERC721Token, + Exchange, + Forwarder, + OrderValidator, + WETH9, + ]; + _.forEach(artifactsArray, artifact => { + this._web3Wrapper.abiDecoder.addABI(artifact.compilerOutput.abi); }); const blockPollingIntervalMs = _.isUndefined(config.blockPollingIntervalMs) ? constants.DEFAULT_BLOCK_POLLING_INTERVAL @@ -114,7 +131,6 @@ export class ContractWrappers { * @param networkId The id of the network your provider is connected to */ public setProvider(provider: Provider): void { - // TODO(albrow): Make sure all contract wrappers are called below. this._web3Wrapper.setProvider(provider); (this.exchange as any)._invalidateContractInstances(); (this.erc20Token as any)._invalidateContractInstances(); diff --git a/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts index 31b3b6755..205a5ed10 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts @@ -1,4 +1,5 @@ -import { artifacts, wrappers } from '@0xproject/contracts'; +import { ERC20ProxyContract } from '@0xproject/abi-gen-wrappers'; +import { ERC20Proxy } from '@0xproject/contract-artifacts'; import { AssetProxyId } from '@0xproject/types'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { ContractAbi } from 'ethereum-types'; @@ -12,9 +13,9 @@ import { ContractWrapper } from './contract_wrapper'; * This class includes the functionality related to interacting with the ERC20Proxy contract. */ export class ERC20ProxyWrapper extends ContractWrapper { - public abi: ContractAbi = artifacts.ERC20Proxy.compilerOutput.abi; + public abi: ContractAbi = ERC20Proxy.compilerOutput.abi; public address: string; - private _erc20ProxyContractIfExists?: wrappers.ERC20ProxyContract; + private _erc20ProxyContractIfExists?: ERC20ProxyContract; /** * Instantiate ERC20ProxyWrapper * @param web3Wrapper Web3Wrapper instance to use @@ -62,11 +63,11 @@ export class ERC20ProxyWrapper extends ContractWrapper { private _invalidateContractInstance(): void { delete this._erc20ProxyContractIfExists; } - private _getERC20ProxyContract(): wrappers.ERC20ProxyContract { + private _getERC20ProxyContract(): ERC20ProxyContract { if (!_.isUndefined(this._erc20ProxyContractIfExists)) { return this._erc20ProxyContractIfExists; } - const contractInstance = new wrappers.ERC20ProxyContract( + const contractInstance = new ERC20ProxyContract( this.abi, this.address, this._web3Wrapper.getProvider(), diff --git a/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts index 53cda5d08..68928e71f 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts @@ -1,4 +1,5 @@ -import { artifacts, wrappers } from '@0xproject/contracts'; +import { ERC20TokenContract, ERC20TokenEventArgs, ERC20TokenEvents } from '@0xproject/abi-gen-wrappers'; +import { ERC20Token } from '@0xproject/contract-artifacts'; import { schemas } from '@0xproject/json-schemas'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; @@ -29,9 +30,9 @@ const removeUndefinedProperties = _.pickBy; * to the 0x ERC20 Proxy smart contract. */ export class ERC20TokenWrapper extends ContractWrapper { - public abi: ContractAbi = artifacts.ERC20Token.compilerOutput.abi; + public abi: ContractAbi = ERC20Token.compilerOutput.abi; public UNLIMITED_ALLOWANCE_IN_BASE_UNITS = constants.UNLIMITED_ALLOWANCE_IN_BASE_UNITS; - private _tokenContractsByAddress: { [address: string]: wrappers.ERC20TokenContract }; + private _tokenContractsByAddress: { [address: string]: ERC20TokenContract }; private _erc20ProxyWrapper: ERC20ProxyWrapper; /** * Instantiate ERC20TokenWrapper @@ -350,15 +351,15 @@ export class ERC20TokenWrapper extends ContractWrapper { * @param isVerbose Enable verbose subscription warnings (e.g recoverable network issues encountered) * @return Subscription token used later to unsubscribe */ - public subscribe( + public subscribe( tokenAddress: string, - eventName: wrappers.ERC20TokenEvents, + eventName: ERC20TokenEvents, indexFilterValues: IndexedFilterValues, callback: EventCallback, isVerbose: boolean = false, ): string { assert.isETHAddressHex('tokenAddress', tokenAddress); - assert.doesBelongToStringEnum('eventName', eventName, wrappers.ERC20TokenEvents); + assert.doesBelongToStringEnum('eventName', eventName, ERC20TokenEvents); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); assert.isFunction('callback', callback); const normalizedTokenAddress = tokenAddress.toLowerCase(); @@ -366,7 +367,7 @@ export class ERC20TokenWrapper extends ContractWrapper { normalizedTokenAddress, eventName, indexFilterValues, - artifacts.ERC20Token.compilerOutput.abi, + ERC20Token.compilerOutput.abi, callback, isVerbose, ); @@ -395,14 +396,14 @@ export class ERC20TokenWrapper extends ContractWrapper { * the value is the value you are interested in. E.g `{_from: aUserAddressHex}` * @return Array of logs that match the parameters */ - public async getLogsAsync( + public async getLogsAsync( tokenAddress: string, - eventName: wrappers.ERC20TokenEvents, + eventName: ERC20TokenEvents, blockRange: BlockRange, indexFilterValues: IndexedFilterValues, ): Promise>> { assert.isETHAddressHex('tokenAddress', tokenAddress); - assert.doesBelongToStringEnum('eventName', eventName, wrappers.ERC20TokenEvents); + assert.doesBelongToStringEnum('eventName', eventName, ERC20TokenEvents); assert.doesConformToSchema('blockRange', blockRange, schemas.blockRangeSchema); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); const normalizedTokenAddress = tokenAddress.toLowerCase(); @@ -411,7 +412,7 @@ export class ERC20TokenWrapper extends ContractWrapper { eventName, blockRange, indexFilterValues, - artifacts.ERC20Token.compilerOutput.abi, + ERC20Token.compilerOutput.abi, ); return logs; } @@ -422,7 +423,7 @@ export class ERC20TokenWrapper extends ContractWrapper { this.unsubscribeAll(); this._tokenContractsByAddress = {}; } - private async _getTokenContractAsync(tokenAddress: string): Promise { + private async _getTokenContractAsync(tokenAddress: string): Promise { const normalizedTokenAddress = tokenAddress.toLowerCase(); let tokenContract = this._tokenContractsByAddress[normalizedTokenAddress]; if (!_.isUndefined(tokenContract)) { @@ -434,7 +435,7 @@ export class ERC20TokenWrapper extends ContractWrapper { if (!doesContractExist) { throw new Error(ContractWrappersError.ERC20TokenContractDoesNotExist); } - const contractInstance = new wrappers.ERC20TokenContract( + const contractInstance = new ERC20TokenContract( this.abi, normalizedTokenAddress, this._web3Wrapper.getProvider(), diff --git a/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts index 2472d512b..963d5b40f 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts @@ -1,4 +1,5 @@ -import { artifacts, wrappers } from '@0xproject/contracts'; +import { ERC721ProxyContract } from '@0xproject/abi-gen-wrappers'; +import { ERC721Proxy } from '@0xproject/contract-artifacts'; import { AssetProxyId } from '@0xproject/types'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { ContractAbi } from 'ethereum-types'; @@ -12,9 +13,9 @@ import { ContractWrapper } from './contract_wrapper'; * This class includes the functionality related to interacting with the ERC721Proxy contract. */ export class ERC721ProxyWrapper extends ContractWrapper { - public abi: ContractAbi = artifacts.ERC20Proxy.compilerOutput.abi; + public abi: ContractAbi = ERC721Proxy.compilerOutput.abi; public address: string; - private _erc721ProxyContractIfExists?: wrappers.ERC721ProxyContract; + private _erc721ProxyContractIfExists?: ERC721ProxyContract; /** * Instantiate ERC721ProxyWrapper * @param web3Wrapper Web3Wrapper instance to use @@ -62,11 +63,11 @@ export class ERC721ProxyWrapper extends ContractWrapper { private _invalidateContractInstance(): void { delete this._erc721ProxyContractIfExists; } - private _getERC721ProxyContract(): wrappers.ERC721ProxyContract { + private _getERC721ProxyContract(): ERC721ProxyContract { if (!_.isUndefined(this._erc721ProxyContractIfExists)) { return this._erc721ProxyContractIfExists; } - const contractInstance = new wrappers.ERC721ProxyContract( + const contractInstance = new ERC721ProxyContract( this.abi, this.address, this._web3Wrapper.getProvider(), diff --git a/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts index 7fcd74eaf..3d58908d8 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts @@ -1,4 +1,5 @@ -import { artifacts, wrappers } from '@0xproject/contracts'; +import { ERC721TokenContract, ERC721TokenEventArgs, ERC721TokenEvents } from '@0xproject/abi-gen-wrappers'; +import { ERC721Token } from '@0xproject/contract-artifacts'; import { schemas } from '@0xproject/json-schemas'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; @@ -29,8 +30,8 @@ const removeUndefinedProperties = _.pickBy; * to the 0x ERC721 Proxy smart contract. */ export class ERC721TokenWrapper extends ContractWrapper { - public abi: ContractAbi = artifacts.ERC721Token.compilerOutput.abi; - private _tokenContractsByAddress: { [address: string]: wrappers.ERC721TokenContract }; + public abi: ContractAbi = ERC721Token.compilerOutput.abi; + private _tokenContractsByAddress: { [address: string]: ERC721TokenContract }; private _erc721ProxyWrapper: ERC721ProxyWrapper; /** * Instantiate ERC721TokenWrapper @@ -377,15 +378,15 @@ export class ERC721TokenWrapper extends ContractWrapper { * @param isVerbose Enable verbose subscription warnings (e.g recoverable network issues encountered) * @return Subscription token used later to unsubscribe */ - public subscribe( + public subscribe( tokenAddress: string, - eventName: wrappers.ERC721TokenEvents, + eventName: ERC721TokenEvents, indexFilterValues: IndexedFilterValues, callback: EventCallback, isVerbose: boolean = false, ): string { assert.isETHAddressHex('tokenAddress', tokenAddress); - assert.doesBelongToStringEnum('eventName', eventName, wrappers.ERC721TokenEvents); + assert.doesBelongToStringEnum('eventName', eventName, ERC721TokenEvents); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); assert.isFunction('callback', callback); const normalizedTokenAddress = tokenAddress.toLowerCase(); @@ -393,7 +394,7 @@ export class ERC721TokenWrapper extends ContractWrapper { normalizedTokenAddress, eventName, indexFilterValues, - artifacts.ERC721Token.compilerOutput.abi, + ERC721Token.compilerOutput.abi, callback, isVerbose, ); @@ -422,14 +423,14 @@ export class ERC721TokenWrapper extends ContractWrapper { * the value is the value you are interested in. E.g `{_from: aUserAddressHex}` * @return Array of logs that match the parameters */ - public async getLogsAsync( + public async getLogsAsync( tokenAddress: string, - eventName: wrappers.ERC721TokenEvents, + eventName: ERC721TokenEvents, blockRange: BlockRange, indexFilterValues: IndexedFilterValues, ): Promise>> { assert.isETHAddressHex('tokenAddress', tokenAddress); - assert.doesBelongToStringEnum('eventName', eventName, wrappers.ERC721TokenEvents); + assert.doesBelongToStringEnum('eventName', eventName, ERC721TokenEvents); assert.doesConformToSchema('blockRange', blockRange, schemas.blockRangeSchema); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); const normalizedTokenAddress = tokenAddress.toLowerCase(); @@ -438,7 +439,7 @@ export class ERC721TokenWrapper extends ContractWrapper { eventName, blockRange, indexFilterValues, - artifacts.ERC721Token.compilerOutput.abi, + ERC721Token.compilerOutput.abi, ); return logs; } @@ -449,7 +450,7 @@ export class ERC721TokenWrapper extends ContractWrapper { this.unsubscribeAll(); this._tokenContractsByAddress = {}; } - private async _getTokenContractAsync(tokenAddress: string): Promise { + private async _getTokenContractAsync(tokenAddress: string): Promise { const normalizedTokenAddress = tokenAddress.toLowerCase(); let tokenContract = this._tokenContractsByAddress[normalizedTokenAddress]; if (!_.isUndefined(tokenContract)) { @@ -461,7 +462,7 @@ export class ERC721TokenWrapper extends ContractWrapper { if (!doesContractExist) { throw new Error(ContractWrappersError.ERC721TokenContractDoesNotExist); } - const contractInstance = new wrappers.ERC721TokenContract( + const contractInstance = new ERC721TokenContract( this.abi, normalizedTokenAddress, this._web3Wrapper.getProvider(), diff --git a/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts index 526324abd..a6f2fd81e 100644 --- a/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts @@ -1,4 +1,5 @@ -import { artifacts, wrappers } from '@0xproject/contracts'; +import { WETH9Contract, WETH9EventArgs, WETH9Events } from '@0xproject/abi-gen-wrappers'; +import { WETH9 } from '@0xproject/contract-artifacts'; import { schemas } from '@0xproject/json-schemas'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; @@ -18,9 +19,9 @@ const removeUndefinedProperties = _.pickBy; * The caller can convert ETH into the equivalent number of wrapped ETH ERC20 tokens and back. */ export class EtherTokenWrapper extends ContractWrapper { - public abi: ContractAbi = artifacts.WETH9.compilerOutput.abi; + public abi: ContractAbi = WETH9.compilerOutput.abi; private _etherTokenContractsByAddress: { - [address: string]: wrappers.WETH9Contract; + [address: string]: WETH9Contract; } = {}; private _erc20TokenWrapper: ERC20TokenWrapper; /** @@ -120,15 +121,15 @@ export class EtherTokenWrapper extends ContractWrapper { * the value is the value you are interested in. E.g `{_owner: aUserAddressHex}` * @return Array of logs that match the parameters */ - public async getLogsAsync( + public async getLogsAsync( etherTokenAddress: string, - eventName: wrappers.WETH9Events, + eventName: WETH9Events, blockRange: BlockRange, indexFilterValues: IndexedFilterValues, ): Promise>> { assert.isETHAddressHex('etherTokenAddress', etherTokenAddress); const normalizedEtherTokenAddress = etherTokenAddress.toLowerCase(); - assert.doesBelongToStringEnum('eventName', eventName, wrappers.WETH9Events); + assert.doesBelongToStringEnum('eventName', eventName, WETH9Events); assert.doesConformToSchema('blockRange', blockRange, schemas.blockRangeSchema); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); const logs = await this._getLogsAsync( @@ -136,7 +137,7 @@ export class EtherTokenWrapper extends ContractWrapper { eventName, blockRange, indexFilterValues, - artifacts.WETH9.compilerOutput.abi, + WETH9.compilerOutput.abi, ); return logs; } @@ -150,23 +151,23 @@ export class EtherTokenWrapper extends ContractWrapper { * @param isVerbose Enable verbose subscription warnings (e.g recoverable network issues encountered) * @return Subscription token used later to unsubscribe */ - public subscribe( + public subscribe( etherTokenAddress: string, - eventName: wrappers.WETH9Events, + eventName: WETH9Events, indexFilterValues: IndexedFilterValues, callback: EventCallback, isVerbose: boolean = false, ): string { assert.isETHAddressHex('etherTokenAddress', etherTokenAddress); const normalizedEtherTokenAddress = etherTokenAddress.toLowerCase(); - assert.doesBelongToStringEnum('eventName', eventName, wrappers.WETH9Events); + assert.doesBelongToStringEnum('eventName', eventName, WETH9Events); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); assert.isFunction('callback', callback); const subscriptionToken = this._subscribe( normalizedEtherTokenAddress, eventName, indexFilterValues, - artifacts.WETH9.compilerOutput.abi, + WETH9.compilerOutput.abi, callback, isVerbose, ); @@ -191,7 +192,7 @@ export class EtherTokenWrapper extends ContractWrapper { this.unsubscribeAll(); this._etherTokenContractsByAddress = {}; } - private async _getEtherTokenContractAsync(etherTokenAddress: string): Promise { + private async _getEtherTokenContractAsync(etherTokenAddress: string): Promise { let etherTokenContract = this._etherTokenContractsByAddress[etherTokenAddress]; if (!_.isUndefined(etherTokenContract)) { return etherTokenContract; @@ -203,7 +204,7 @@ export class EtherTokenWrapper extends ContractWrapper { if (!doesContractExist) { throw new Error(ContractWrappersError.EtherTokenContractDoesNotExist); } - const contractInstance = new wrappers.WETH9Contract( + const contractInstance = new WETH9Contract( this.abi, etherTokenAddress, this._web3Wrapper.getProvider(), diff --git a/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts index b65baee55..6b3694bc5 100644 --- a/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts @@ -1,4 +1,5 @@ -import { artifacts, wrappers } from '@0xproject/contracts'; +import { ExchangeContract, ExchangeEventArgs, ExchangeEvents } from '@0xproject/abi-gen-wrappers'; +import { Exchange } from '@0xproject/contract-artifacts'; import { schemas } from '@0xproject/json-schemas'; import { assetDataUtils, @@ -40,10 +41,10 @@ import { ERC721TokenWrapper } from './erc721_token_wrapper'; * events of the 0x V2 Exchange smart contract. */ export class ExchangeWrapper extends ContractWrapper { - public abi: ContractAbi = artifacts.Exchange.compilerOutput.abi; + public abi: ContractAbi = Exchange.compilerOutput.abi; public address: string; public zrxTokenAddress: string; - private _exchangeContractIfExists?: wrappers.ExchangeContract; + private _exchangeContractIfExists?: ExchangeContract; private _erc721TokenWrapper: ERC721TokenWrapper; private _erc20TokenWrapper: ERC20TokenWrapper; /** @@ -1039,20 +1040,20 @@ export class ExchangeWrapper extends ContractWrapper { * @param isVerbose Enable verbose subscription warnings (e.g recoverable network issues encountered) * @return Subscription token used later to unsubscribe */ - public subscribe( - eventName: wrappers.ExchangeEvents, + public subscribe( + eventName: ExchangeEvents, indexFilterValues: IndexedFilterValues, callback: EventCallback, isVerbose: boolean = false, ): string { - assert.doesBelongToStringEnum('eventName', eventName, wrappers.ExchangeEvents); + assert.doesBelongToStringEnum('eventName', eventName, ExchangeEvents); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); assert.isFunction('callback', callback); const subscriptionToken = this._subscribe( this.address, eventName, indexFilterValues, - artifacts.Exchange.compilerOutput.abi, + Exchange.compilerOutput.abi, callback, isVerbose, ); @@ -1079,12 +1080,12 @@ export class ExchangeWrapper extends ContractWrapper { * the value is the value you are interested in. E.g `{_from: aUserAddressHex}` * @return Array of logs that match the parameters */ - public async getLogsAsync( - eventName: wrappers.ExchangeEvents, + public async getLogsAsync( + eventName: ExchangeEvents, blockRange: BlockRange, indexFilterValues: IndexedFilterValues, ): Promise>> { - assert.doesBelongToStringEnum('eventName', eventName, wrappers.ExchangeEvents); + assert.doesBelongToStringEnum('eventName', eventName, ExchangeEvents); assert.doesConformToSchema('blockRange', blockRange, schemas.blockRangeSchema); assert.doesConformToSchema('indexFilterValues', indexFilterValues, schemas.indexFilterValuesSchema); const logs = await this._getLogsAsync( @@ -1092,7 +1093,7 @@ export class ExchangeWrapper extends ContractWrapper { eventName, blockRange, indexFilterValues, - artifacts.Exchange.compilerOutput.abi, + Exchange.compilerOutput.abi, ); return logs; } @@ -1178,11 +1179,11 @@ export class ExchangeWrapper extends ContractWrapper { delete this._exchangeContractIfExists; } // tslint:enable:no-unused-variable - private async _getExchangeContractAsync(): Promise { + private async _getExchangeContractAsync(): Promise { if (!_.isUndefined(this._exchangeContractIfExists)) { return this._exchangeContractIfExists; } - const contractInstance = new wrappers.ExchangeContract( + const contractInstance = new ExchangeContract( this.abi, this.address, this._web3Wrapper.getProvider(), diff --git a/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts index cd8f9ece9..22fc916e2 100644 --- a/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts @@ -1,4 +1,5 @@ -import { artifacts, wrappers } from '@0xproject/contracts'; +import { ForwarderContract } from '@0xproject/abi-gen-wrappers'; +import { Forwarder } from '@0xproject/contract-artifacts'; import { schemas } from '@0xproject/json-schemas'; import { AssetProxyId, SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; @@ -21,11 +22,11 @@ import { ContractWrapper } from './contract_wrapper'; * This class includes the functionality related to interacting with the Forwarder contract. */ export class ForwarderWrapper extends ContractWrapper { - public abi: ContractAbi = artifacts.Forwarder.compilerOutput.abi; + public abi: ContractAbi = Forwarder.compilerOutput.abi; public address: string; public zrxTokenAddress: string; public etherTokenAddress: string; - private _forwarderContractIfExists?: wrappers.ForwarderContract; + private _forwarderContractIfExists?: ForwarderContract; // TODO(albrow): Make addresses optional? constructor(web3Wrapper: Web3Wrapper, address: string, zrxTokenAddress: string, etherTokenAddress: string) { super(web3Wrapper); @@ -215,11 +216,11 @@ export class ForwarderWrapper extends ContractWrapper { private _invalidateContractInstance(): void { delete this._forwarderContractIfExists; } - private async _getForwarderContractAsync(): Promise { + private async _getForwarderContractAsync(): Promise { if (!_.isUndefined(this._forwarderContractIfExists)) { return this._forwarderContractIfExists; } - const contractInstance = new wrappers.ForwarderContract( + const contractInstance = new ForwarderContract( this.abi, this.address, this._web3Wrapper.getProvider(), diff --git a/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts index 23e0d42c1..cb67d57f7 100644 --- a/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts @@ -1,4 +1,5 @@ -import { artifacts, wrappers } from '@0xproject/contracts'; +import { OrderValidatorContract } from '@0xproject/abi-gen-wrappers'; +import { OrderValidator } from '@0xproject/contract-artifacts'; import { schemas } from '@0xproject/json-schemas'; import { SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; @@ -15,9 +16,9 @@ import { ContractWrapper } from './contract_wrapper'; * This class includes the functionality related to interacting with the OrderValidator contract. */ export class OrderValidatorWrapper extends ContractWrapper { - public abi: ContractAbi = artifacts.OrderValidator.compilerOutput.abi; + public abi: ContractAbi = OrderValidator.compilerOutput.abi; public address: string; - private _orderValidatorContractIfExists?: wrappers.OrderValidatorContract; + private _orderValidatorContractIfExists?: OrderValidatorContract; /** * Instantiate OrderValidatorWrapper * @param web3Wrapper Web3Wrapper instance to use. @@ -172,11 +173,11 @@ export class OrderValidatorWrapper extends ContractWrapper { private _invalidateContractInstance(): void { delete this._orderValidatorContractIfExists; } - private async _getOrderValidatorContractAsync(): Promise { + private async _getOrderValidatorContractAsync(): Promise { if (!_.isUndefined(this._orderValidatorContractIfExists)) { return this._orderValidatorContractIfExists; } - const contractInstance = new wrappers.OrderValidatorContract( + const contractInstance = new OrderValidatorContract( this.abi, this.address, this._web3Wrapper.getProvider(), diff --git a/packages/contract-wrappers/src/globals.d.ts b/packages/contract-wrappers/src/globals.d.ts deleted file mode 100644 index 94e63a32d..000000000 --- a/packages/contract-wrappers/src/globals.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -declare module '*.json' { - const json: any; - /* tslint:disable */ - export default json; - /* tslint:enable */ -} diff --git a/packages/contract-wrappers/src/index.ts b/packages/contract-wrappers/src/index.ts index 659394eb6..50dfe88be 100644 --- a/packages/contract-wrappers/src/index.ts +++ b/packages/contract-wrappers/src/index.ts @@ -1,4 +1,27 @@ -import { wrappers } from '@0xproject/contracts'; +export { + WETH9Events, + WETH9WithdrawalEventArgs, + WETH9ApprovalEventArgs, + WETH9EventArgs, + WETH9DepositEventArgs, + WETH9TransferEventArgs, + ERC20TokenTransferEventArgs, + ERC20TokenApprovalEventArgs, + ERC20TokenEvents, + ERC20TokenEventArgs, + ERC721TokenApprovalEventArgs, + ERC721TokenApprovalForAllEventArgs, + ERC721TokenTransferEventArgs, + ERC721TokenEvents, + ERC721TokenEventArgs, + ExchangeCancelUpToEventArgs, + ExchangeAssetProxyRegisteredEventArgs, + ExchangeSignatureValidatorApprovalEventArgs, + ExchangeFillEventArgs, + ExchangeCancelEventArgs, + ExchangeEventArgs, + ExchangeEvents, +} from '@0xproject/abi-gen-wrappers'; export { ContractWrappers } from './contract_wrappers'; export { ERC20TokenWrapper } from './contract_wrappers/erc20_token_wrapper'; @@ -56,32 +79,6 @@ export { StateMutability, } from 'ethereum-types'; -export const WETH9Events = wrappers.WETH9Events; -export type WETH9WithdrawalEventArgs = wrappers.WETH9WithdrawalEventArgs; -export type WETH9ApprovalEventArgs = wrappers.WETH9ApprovalEventArgs; -export type WETH9EventArgs = wrappers.WETH9EventArgs; -export type WETH9DepositEventArgs = wrappers.WETH9DepositEventArgs; -export type WETH9TransferEventArgs = wrappers.WETH9TransferEventArgs; - -export type ERC20TokenTransferEventArgs = wrappers.ERC20TokenTransferEventArgs; -export type ERC20TokenApprovalEventArgs = wrappers.ERC20TokenApprovalEventArgs; -export const ERC20TokenEvents = wrappers.ERC20TokenEvents; -export type ERC20TokenEventArgs = wrappers.ERC20TokenEventArgs; - -export type ERC721TokenApprovalEventArgs = wrappers.ERC721TokenApprovalEventArgs; -export type ERC721TokenApprovalForAllEventArgs = wrappers.ERC721TokenApprovalForAllEventArgs; -export type ERC721TokenTransferEventArgs = wrappers.ERC721TokenTransferEventArgs; -export const ERC721TokenEvents = wrappers.ERC721TokenEvents; -export type ERC721TokenEventArgs = wrappers.ERC721TokenEventArgs; - -export type ExchangeCancelUpToEventArgs = wrappers.ExchangeCancelUpToEventArgs; -export type ExchangeAssetProxyRegisteredEventArgs = wrappers.ExchangeAssetProxyRegisteredEventArgs; -export type ExchangeSignatureValidatorApprovalEventArgs = wrappers.ExchangeSignatureValidatorApprovalEventArgs; -export type ExchangeFillEventArgs = wrappers.ExchangeFillEventArgs; -export type ExchangeCancelEventArgs = wrappers.ExchangeCancelEventArgs; -export type ExchangeEventArgs = wrappers.ExchangeEventArgs; -export const ExchangeEvents = wrappers.ExchangeEvents; - export { AbstractBalanceAndProxyAllowanceFetcher, AbstractOrderFilledCancelledFetcher } from '@0xproject/order-utils'; export { AssetBalanceAndProxyAllowanceFetcher } from './fetchers/asset_balance_and_proxy_allowance_fetcher'; diff --git a/packages/contract-wrappers/src/types.ts b/packages/contract-wrappers/src/types.ts index f882cc188..b06a6b914 100644 --- a/packages/contract-wrappers/src/types.ts +++ b/packages/contract-wrappers/src/types.ts @@ -1,4 +1,13 @@ -import { wrappers } from '@0xproject/contracts'; +import { + ERC20TokenEventArgs, + ERC20TokenEvents, + ERC721TokenEventArgs, + ERC721TokenEvents, + ExchangeEventArgs, + ExchangeEvents, + WETH9EventArgs, + WETH9Events, +} from '@0xproject/abi-gen-wrappers'; import { ContractAddresses, OrderState, SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; @@ -56,11 +65,7 @@ export interface ContractEvent { args: ContractEventArgs; } -export type ContractEventArgs = - | wrappers.ExchangeEventArgs - | wrappers.ERC20TokenEventArgs - | wrappers.ERC721TokenEventArgs - | wrappers.WETH9EventArgs; +export type ContractEventArgs = ExchangeEventArgs | ERC20TokenEventArgs | ERC721TokenEventArgs | WETH9EventArgs; // [address, name, symbol, decimals, ipfsHash, swarmHash] export type TokenMetadata = [string, string, string, number, string, string]; @@ -83,11 +88,7 @@ export interface TokenAddressBySymbol { [symbol: string]: string; } -export type ContractEvents = - | wrappers.ERC20TokenEvents - | wrappers.ERC721TokenEvents - | wrappers.ExchangeEvents - | wrappers.WETH9Events; +export type ContractEvents = ERC20TokenEvents | ERC721TokenEvents | ExchangeEvents | WETH9Events; export interface IndexedFilterValues { [index: string]: ContractEventArg; diff --git a/packages/contract-wrappers/src/utils/transaction_encoder.ts b/packages/contract-wrappers/src/utils/transaction_encoder.ts index f4c6de97f..d179fa07e 100644 --- a/packages/contract-wrappers/src/utils/transaction_encoder.ts +++ b/packages/contract-wrappers/src/utils/transaction_encoder.ts @@ -1,4 +1,4 @@ -import { wrappers } from '@0xproject/contracts'; +import { ExchangeContract } from '@0xproject/abi-gen-wrappers'; import { schemas } from '@0xproject/json-schemas'; import { eip712Utils } from '@0xproject/order-utils'; @@ -14,8 +14,8 @@ import { assert } from './assert'; * can submit this to the blockchain. The Exchange context executes as if UserA had directly submitted this transaction. */ export class TransactionEncoder { - private readonly _exchangeInstance: wrappers.ExchangeContract; - constructor(exchangeInstance: wrappers.ExchangeContract) { + private readonly _exchangeInstance: ExchangeContract; + constructor(exchangeInstance: ExchangeContract) { this._exchangeInstance = exchangeInstance; } /** @@ -275,7 +275,7 @@ export class TransactionEncoder { ); return abiEncodedData; } - private _getExchangeContract(): wrappers.ExchangeContract { + private _getExchangeContract(): ExchangeContract { return this._exchangeInstance; } } -- cgit v1.2.3 From 1e9ea09f087c7b3120e758d931a88812b655da08 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Tue, 9 Oct 2018 23:10:33 -0700 Subject: Introduce new contract-addresses package and use it everywhere --- .../contract-wrappers/src/contract_wrappers.ts | 50 ++++++++++++++++------ .../src/contract_wrappers/contract_wrapper.ts | 8 +++- .../src/contract_wrappers/erc20_proxy_wrapper.ts | 13 +++--- .../src/contract_wrappers/erc20_token_wrapper.ts | 10 ++++- .../src/contract_wrappers/erc721_proxy_wrapper.ts | 13 +++--- .../src/contract_wrappers/erc721_token_wrapper.ts | 10 ++++- .../src/contract_wrappers/ether_token_wrapper.ts | 9 +++- .../src/contract_wrappers/exchange_wrapper.ts | 23 ++++++---- .../src/contract_wrappers/forwarder_wrapper.ts | 36 +++++++++++++--- .../contract_wrappers/order_validator_wrapper.ts | 12 +++--- packages/contract-wrappers/src/types.ts | 12 ++---- 11 files changed, 136 insertions(+), 60 deletions(-) (limited to 'packages/contract-wrappers/src') diff --git a/packages/contract-wrappers/src/contract_wrappers.ts b/packages/contract-wrappers/src/contract_wrappers.ts index 359307da4..25acf823d 100644 --- a/packages/contract-wrappers/src/contract_wrappers.ts +++ b/packages/contract-wrappers/src/contract_wrappers.ts @@ -1,3 +1,4 @@ +import { getContractAddressesForNetwork } from '@0xproject/contract-addresses'; import { ERC20Proxy, ERC20Token, @@ -100,29 +101,50 @@ export class ContractWrappers { const blockPollingIntervalMs = _.isUndefined(config.blockPollingIntervalMs) ? constants.DEFAULT_BLOCK_POLLING_INTERVAL : config.blockPollingIntervalMs; - if (_.isUndefined(config.contractAddresses.erc20Proxy)) { - throw new Error('config.contractAddresses.erc20Proxy is required for testing'); - } - this.erc20Proxy = new ERC20ProxyWrapper(this._web3Wrapper, config.contractAddresses.erc20Proxy); - this.erc721Proxy = new ERC721ProxyWrapper(this._web3Wrapper, config.contractAddresses.erc721Proxy); - this.erc20Token = new ERC20TokenWrapper(this._web3Wrapper, this.erc20Proxy, blockPollingIntervalMs); - this.erc721Token = new ERC721TokenWrapper(this._web3Wrapper, this.erc721Proxy, blockPollingIntervalMs); - this.etherToken = new EtherTokenWrapper(this._web3Wrapper, this.erc20Token, blockPollingIntervalMs); + const contractAddresses = _.isUndefined(config.contractAddresses) + ? getContractAddressesForNetwork(config.networkId) + : config.contractAddresses; + this.erc20Proxy = new ERC20ProxyWrapper(this._web3Wrapper, config.networkId, contractAddresses.erc20Proxy); + this.erc721Proxy = new ERC721ProxyWrapper(this._web3Wrapper, config.networkId, contractAddresses.erc721Proxy); + this.erc20Token = new ERC20TokenWrapper( + this._web3Wrapper, + config.networkId, + this.erc20Proxy, + blockPollingIntervalMs, + ); + this.erc721Token = new ERC721TokenWrapper( + this._web3Wrapper, + config.networkId, + this.erc721Proxy, + blockPollingIntervalMs, + ); + this.etherToken = new EtherTokenWrapper( + this._web3Wrapper, + config.networkId, + this.erc20Token, + blockPollingIntervalMs, + ); this.exchange = new ExchangeWrapper( this._web3Wrapper, + config.networkId, this.erc20Token, this.erc721Token, - config.contractAddresses.exchange, - config.contractAddresses.zrxToken, + contractAddresses.exchange, + contractAddresses.zrxToken, blockPollingIntervalMs, ); this.forwarder = new ForwarderWrapper( this._web3Wrapper, - config.contractAddresses.forwarder, - config.contractAddresses.zrxToken, - config.contractAddresses.etherToken, + config.networkId, + contractAddresses.forwarder, + contractAddresses.zrxToken, + contractAddresses.etherToken, + ); + this.orderValidator = new OrderValidatorWrapper( + this._web3Wrapper, + config.networkId, + contractAddresses.orderValidator, ); - this.orderValidator = new OrderValidatorWrapper(this._web3Wrapper, config.contractAddresses.orderValidator); } /** * Sets a new web3 provider for 0x.js. Updating the provider will stop all diff --git a/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts index aed9d44db..30095e002 100644 --- a/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts @@ -1,3 +1,4 @@ +import { ContractAddresses, getContractAddressesForNetwork } from '@0xproject/contract-addresses'; import { AbiDecoder, intervalUtils, logUtils } from '@0xproject/utils'; import { marshaller, Web3Wrapper } from '@0xproject/web3-wrapper'; import { @@ -25,6 +26,7 @@ import { filterUtils } from '../utils/filter_utils'; export abstract class ContractWrapper { public abstract abi: ContractAbi; + protected _networkId: number; protected _web3Wrapper: Web3Wrapper; private _blockAndLogStreamerIfExists: BlockAndLogStreamer | undefined; private _blockPollingIntervalMs: number; @@ -42,8 +44,9 @@ export abstract class ContractWrapper { logUtils.warn(err); } } - constructor(web3Wrapper: Web3Wrapper, blockPollingIntervalMs?: number) { + constructor(web3Wrapper: Web3Wrapper, networkId: number, blockPollingIntervalMs?: number) { this._web3Wrapper = web3Wrapper; + this._networkId = networkId; this._blockPollingIntervalMs = _.isUndefined(blockPollingIntervalMs) ? constants.DEFAULT_BLOCK_POLLING_INTERVAL : blockPollingIntervalMs; @@ -109,6 +112,9 @@ export abstract class ContractWrapper { const logWithDecodedArgs = abiDecoder.tryToDecodeLogOrNoop(log); return logWithDecodedArgs; } + protected _getDefaultContractAddresses(): ContractAddresses { + return getContractAddressesForNetwork(this._networkId); + } private _onLogStateChanged(isRemoved: boolean, rawLog: RawLogEntry): void { const log: LogEntry = marshaller.unmarshalLog(rawLog); _.forEach(this._filters, (filter: FilterObject, filterToken: string) => { diff --git a/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts index 205a5ed10..605646256 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts @@ -19,13 +19,14 @@ export class ERC20ProxyWrapper extends ContractWrapper { /** * Instantiate ERC20ProxyWrapper * @param web3Wrapper Web3Wrapper instance to use - * @param address The address of the ERC20Proxy contract + * @param networkId Desired networkId + * @param address (Optional) The address of the ERC20Proxy contract. If + * undefined, will default to the known address corresponding to the + * networkId. */ - // TODO(albrow): Make address optional and default to looking up the address - // based in a hard-coded mapping based on web3Wrapper network id. - constructor(web3Wrapper: Web3Wrapper, address: string) { - super(web3Wrapper); - this.address = address; + constructor(web3Wrapper: Web3Wrapper, networkId: number, address?: string) { + super(web3Wrapper, networkId); + this.address = _.isUndefined(address) ? this._getDefaultContractAddresses().erc20Proxy : address; } /** * Get the 4 bytes ID of this asset proxy diff --git a/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts index 68928e71f..2db0165bc 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts @@ -37,11 +37,17 @@ export class ERC20TokenWrapper extends ContractWrapper { /** * Instantiate ERC20TokenWrapper * @param web3Wrapper Web3Wrapper instance to use + * @param networkId Desired networkId * @param erc20ProxyWrapper The ERC20ProxyWrapper instance to use * @param blockPollingIntervalMs The block polling interval to use for active subscriptions */ - constructor(web3Wrapper: Web3Wrapper, erc20ProxyWrapper: ERC20ProxyWrapper, blockPollingIntervalMs?: number) { - super(web3Wrapper, blockPollingIntervalMs); + constructor( + web3Wrapper: Web3Wrapper, + networkId: number, + erc20ProxyWrapper: ERC20ProxyWrapper, + blockPollingIntervalMs?: number, + ) { + super(web3Wrapper, networkId, blockPollingIntervalMs); this._tokenContractsByAddress = {}; this._erc20ProxyWrapper = erc20ProxyWrapper; } diff --git a/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts index 963d5b40f..b1b026a3a 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts @@ -19,13 +19,14 @@ export class ERC721ProxyWrapper extends ContractWrapper { /** * Instantiate ERC721ProxyWrapper * @param web3Wrapper Web3Wrapper instance to use - * @param address The address of the ERC721Proxy contract + * @param networkId Desired networkId + * @param address (Optional) The address of the ERC721Proxy contract. If + * undefined, will default to the known address corresponding to the + * networkId. */ - // TODO(albrow): Make address optional and default to looking up the address - // based in a hard-coded mapping based on web3Wrapper network id. - constructor(web3Wrapper: Web3Wrapper, address: string) { - super(web3Wrapper); - this.address = address; + constructor(web3Wrapper: Web3Wrapper, networkId: number, address?: string) { + super(web3Wrapper, networkId); + this.address = _.isUndefined(address) ? this._getDefaultContractAddresses().erc721Proxy : address; } /** * Get the 4 bytes ID of this asset proxy diff --git a/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts index 3d58908d8..b18692964 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts @@ -36,11 +36,17 @@ export class ERC721TokenWrapper extends ContractWrapper { /** * Instantiate ERC721TokenWrapper * @param web3Wrapper Web3Wrapper instance to use + * @param networkId Desired networkId * @param erc721ProxyWrapper The ERC721ProxyWrapper instance to use * @param blockPollingIntervalMs The block polling interval to use for active subscriptions */ - constructor(web3Wrapper: Web3Wrapper, erc721ProxyWrapper: ERC721ProxyWrapper, blockPollingIntervalMs?: number) { - super(web3Wrapper, blockPollingIntervalMs); + constructor( + web3Wrapper: Web3Wrapper, + networkId: number, + erc721ProxyWrapper: ERC721ProxyWrapper, + blockPollingIntervalMs?: number, + ) { + super(web3Wrapper, networkId, blockPollingIntervalMs); this._tokenContractsByAddress = {}; this._erc721ProxyWrapper = erc721ProxyWrapper; } diff --git a/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts index a6f2fd81e..2586401bc 100644 --- a/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts @@ -31,8 +31,13 @@ export class EtherTokenWrapper extends ContractWrapper { * @param erc20TokenWrapper The ERC20TokenWrapper instance to use * @param blockPollingIntervalMs The block polling interval to use for active subscriptions */ - constructor(web3Wrapper: Web3Wrapper, erc20TokenWrapper: ERC20TokenWrapper, blockPollingIntervalMs?: number) { - super(web3Wrapper, blockPollingIntervalMs); + constructor( + web3Wrapper: Web3Wrapper, + networkId: number, + erc20TokenWrapper: ERC20TokenWrapper, + blockPollingIntervalMs?: number, + ) { + super(web3Wrapper, networkId, blockPollingIntervalMs); this._erc20TokenWrapper = erc20TokenWrapper; } /** diff --git a/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts index 6b3694bc5..b185b7e0c 100644 --- a/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts @@ -50,26 +50,33 @@ export class ExchangeWrapper extends ContractWrapper { /** * Instantiate ExchangeWrapper * @param web3Wrapper Web3Wrapper instance to use. + * @param networkId Desired networkId. * @param erc20TokenWrapper ERC20TokenWrapper instance to use. * @param erc721TokenWrapper ERC721TokenWrapper instance to use. - * @param address The address of the Exchange contract. - * @param zrxTokenAddress The address of the ZRX Token contract. + * @param address (Optional) The address of the Exchange contract. If + * undefined, will default to the known address corresponding to the + * networkId. + * @param zrxTokenAddress (Optional) The address of the ZRXToken contract. + * If undefined, will default to the known address corresponding to the + * networkId. * @param blockPollingIntervalMs The block polling interval to use for active subscriptions. */ constructor( web3Wrapper: Web3Wrapper, + networkId: number, erc20TokenWrapper: ERC20TokenWrapper, erc721TokenWrapper: ERC721TokenWrapper, - // TODO(albrow): Make address optional? - address: string, - zrxTokenAddress: string, + address?: string, + zrxTokenAddress?: string, blockPollingIntervalMs?: number, ) { - super(web3Wrapper, blockPollingIntervalMs); + super(web3Wrapper, networkId, blockPollingIntervalMs); this._erc20TokenWrapper = erc20TokenWrapper; this._erc721TokenWrapper = erc721TokenWrapper; - this.address = address; - this.zrxTokenAddress = zrxTokenAddress; + this.address = _.isUndefined(address) ? this._getDefaultContractAddresses().exchange : address; + this.zrxTokenAddress = _.isUndefined(zrxTokenAddress) + ? this._getDefaultContractAddresses().zrxToken + : zrxTokenAddress; } /** * Retrieve the address of an asset proxy by signature. diff --git a/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts index 22fc916e2..b72a77e8b 100644 --- a/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts @@ -27,12 +27,36 @@ export class ForwarderWrapper extends ContractWrapper { public zrxTokenAddress: string; public etherTokenAddress: string; private _forwarderContractIfExists?: ForwarderContract; - // TODO(albrow): Make addresses optional? - constructor(web3Wrapper: Web3Wrapper, address: string, zrxTokenAddress: string, etherTokenAddress: string) { - super(web3Wrapper); - this.address = address; - this.zrxTokenAddress = zrxTokenAddress; - this.etherTokenAddress = etherTokenAddress; + + /** + * Instantiate ForwarderWrapper + * @param web3Wrapper Web3Wrapper instance to use. + * @param networkId Desired networkId. + * @param address (Optional) The address of the Exchange contract. If + * undefined, will default to the known address corresponding to the + * networkId. + * @param zrxTokenAddress (Optional) The address of the ZRXToken contract. + * If undefined, will default to the known address corresponding to the + * networkId. + * @param etherTokenAddress (Optional) The address of a WETH (Ether token) + * contract. If undefined, will default to the known address corresponding + * to the networkId. + */ + constructor( + web3Wrapper: Web3Wrapper, + networkId: number, + address?: string, + zrxTokenAddress?: string, + etherTokenAddress?: string, + ) { + super(web3Wrapper, networkId); + this.address = _.isUndefined(address) ? this._getDefaultContractAddresses().exchange : address; + this.zrxTokenAddress = _.isUndefined(zrxTokenAddress) + ? this._getDefaultContractAddresses().zrxToken + : zrxTokenAddress; + this.etherTokenAddress = _.isUndefined(etherTokenAddress) + ? this._getDefaultContractAddresses().etherToken + : etherTokenAddress; } /** * Purchases as much of orders' makerAssets as possible by selling up to 95% of transaction's ETH value. diff --git a/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts index cb67d57f7..b7bdfb149 100644 --- a/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts @@ -22,12 +22,14 @@ export class OrderValidatorWrapper extends ContractWrapper { /** * Instantiate OrderValidatorWrapper * @param web3Wrapper Web3Wrapper instance to use. - * @param address The address of the OrderValidator contract. + * @param networkId Desired networkId. + * @param address (Optional) The address of the OrderValidator contract. If + * undefined, will default to the known address corresponding to the + * networkId. */ - // TODO(albrow): Make address optional? - constructor(web3Wrapper: Web3Wrapper, address: string) { - super(web3Wrapper); - this.address = address; + constructor(web3Wrapper: Web3Wrapper, networkId: number, address?: string) { + super(web3Wrapper, networkId); + this.address = _.isUndefined(address) ? this._getDefaultContractAddresses().exchange : address; } /** * Get an object conforming to OrderAndTraderInfo containing on-chain information of the provided order and address diff --git a/packages/contract-wrappers/src/types.ts b/packages/contract-wrappers/src/types.ts index b06a6b914..e70adb991 100644 --- a/packages/contract-wrappers/src/types.ts +++ b/packages/contract-wrappers/src/types.ts @@ -8,7 +8,8 @@ import { WETH9EventArgs, WETH9Events, } from '@0xproject/abi-gen-wrappers'; -import { ContractAddresses, OrderState, SignedOrder } from '@0xproject/types'; +import { ContractAddresses } from '@0xproject/contract-addresses'; +import { OrderState, SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import { BlockParam, ContractEventArg, DecodedLogArgs, LogEntryEvent, LogWithDecodedArgs } from 'ethereum-types'; @@ -110,18 +111,13 @@ export type SyncMethod = (...args: any[]) => any; /** * networkId: The id of the underlying ethereum network your provider is connected to. (1-mainnet, 3-ropsten, 4-rinkeby, 42-kovan, 50-testrpc) * gasPrice: Gas price to use with every transaction - * exchangeContractAddress: The address of an exchange contract to use - * zrxContractAddress: The address of the ZRX contract to use - * erc20ProxyContractAddress: The address of the erc20 token transfer proxy contract to use - * erc721ProxyContractAddress: The address of the erc721 token transfer proxy contract to use - * forwarderContractAddress: The address of the forwarder contract to use - * orderWatcherConfig: All the configs related to the orderWatcher + * contractAddresses: The address of all contracts to use. Defaults to the known addresses based on networkId. * blockPollingIntervalMs: The interval to use for block polling in event watching methods (defaults to 1000) */ export interface ContractWrappersConfig { networkId: number; gasPrice?: BigNumber; - contractAddresses: ContractAddresses; + contractAddresses?: ContractAddresses; blockPollingIntervalMs?: number; } -- cgit v1.2.3 From 974ec23ecd2a445523e0550c0a7f69329d6959f1 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Wed, 10 Oct 2018 16:04:37 -0700 Subject: Apply various PR feedback --- packages/contract-wrappers/src/contract_wrappers.ts | 8 ++++---- .../src/contract_wrappers/contract_wrapper.ts | 1 + .../src/contract_wrappers/erc20_proxy_wrapper.ts | 5 ++--- .../src/contract_wrappers/erc721_proxy_wrapper.ts | 5 ++--- .../src/contract_wrappers/exchange_wrapper.ts | 7 +++---- .../src/contract_wrappers/forwarder_wrapper.ts | 11 +++++------ .../src/contract_wrappers/order_validator_wrapper.ts | 5 ++--- 7 files changed, 19 insertions(+), 23 deletions(-) (limited to 'packages/contract-wrappers/src') diff --git a/packages/contract-wrappers/src/contract_wrappers.ts b/packages/contract-wrappers/src/contract_wrappers.ts index 25acf823d..34eb1f303 100644 --- a/packages/contract-wrappers/src/contract_wrappers.ts +++ b/packages/contract-wrappers/src/contract_wrappers.ts @@ -70,7 +70,7 @@ export class ContractWrappers { private readonly _web3Wrapper: Web3Wrapper; /** * Instantiates a new ContractWrappers instance. - * @param provider The Provider instance you would like the 0x.js library to use for interacting with + * @param provider The Provider instance you would like the contract-wrappers library to use for interacting with * the Ethereum network. * @param config The configuration object. Look up the type for the description. * @return An instance of the ContractWrappers class. @@ -147,9 +147,9 @@ export class ContractWrappers { ); } /** - * Sets a new web3 provider for 0x.js. Updating the provider will stop all + * 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 0x.js library to use from now on. + * @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 */ public setProvider(provider: Provider): void { @@ -164,7 +164,7 @@ export class ContractWrappers { (this.orderValidator as any)._invalidateContractInstance(); } /** - * Get the provider instance currently used by 0x.js + * Get the provider instance currently used by contract-wrappers * @return Web3 provider instance */ public getProvider(): Provider { diff --git a/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts index 30095e002..3d37e446c 100644 --- a/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts @@ -113,6 +113,7 @@ export abstract class ContractWrapper { return logWithDecodedArgs; } protected _getDefaultContractAddresses(): ContractAddresses { + // TODO(albrow): Figure out better error handling here. return getContractAddressesForNetwork(this._networkId); } private _onLogStateChanged(isRemoved: boolean, rawLog: RawLogEntry): void { diff --git a/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts index 605646256..459019877 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts @@ -20,9 +20,8 @@ export class ERC20ProxyWrapper extends ContractWrapper { * Instantiate ERC20ProxyWrapper * @param web3Wrapper Web3Wrapper instance to use * @param networkId Desired networkId - * @param address (Optional) The address of the ERC20Proxy contract. If - * undefined, will default to the known address corresponding to the - * networkId. + * @param address The address of the ERC20Proxy contract. If undefined, will + * default to the known address corresponding to the networkId. */ constructor(web3Wrapper: Web3Wrapper, networkId: number, address?: string) { super(web3Wrapper, networkId); diff --git a/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts index b1b026a3a..9cf56dec1 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts @@ -20,9 +20,8 @@ export class ERC721ProxyWrapper extends ContractWrapper { * Instantiate ERC721ProxyWrapper * @param web3Wrapper Web3Wrapper instance to use * @param networkId Desired networkId - * @param address (Optional) The address of the ERC721Proxy contract. If - * undefined, will default to the known address corresponding to the - * networkId. + * @param address The address of the ERC721Proxy contract. If undefined, + * will default to the known address corresponding to the networkId. */ constructor(web3Wrapper: Web3Wrapper, networkId: number, address?: string) { super(web3Wrapper, networkId); diff --git a/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts index b185b7e0c..67c0351e2 100644 --- a/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts @@ -53,12 +53,11 @@ export class ExchangeWrapper extends ContractWrapper { * @param networkId Desired networkId. * @param erc20TokenWrapper ERC20TokenWrapper instance to use. * @param erc721TokenWrapper ERC721TokenWrapper instance to use. - * @param address (Optional) The address of the Exchange contract. If + * @param address The address of the Exchange contract. If undefined, will + * default to the known address corresponding to the networkId. + * @param zrxTokenAddress The address of the ZRXToken contract. If * undefined, will default to the known address corresponding to the * networkId. - * @param zrxTokenAddress (Optional) The address of the ZRXToken contract. - * If undefined, will default to the known address corresponding to the - * networkId. * @param blockPollingIntervalMs The block polling interval to use for active subscriptions. */ constructor( diff --git a/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts index b72a77e8b..fe4d0cbda 100644 --- a/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts @@ -32,15 +32,14 @@ export class ForwarderWrapper extends ContractWrapper { * Instantiate ForwarderWrapper * @param web3Wrapper Web3Wrapper instance to use. * @param networkId Desired networkId. - * @param address (Optional) The address of the Exchange contract. If + * @param address The address of the Exchange contract. If undefined, will + * default to the known address corresponding to the networkId. + * @param zrxTokenAddress The address of the ZRXToken contract. If * undefined, will default to the known address corresponding to the * networkId. - * @param zrxTokenAddress (Optional) The address of the ZRXToken contract. - * If undefined, will default to the known address corresponding to the + * @param etherTokenAddress The address of a WETH (Ether token) contract. If + * undefined, will default to the known address corresponding to the * networkId. - * @param etherTokenAddress (Optional) The address of a WETH (Ether token) - * contract. If undefined, will default to the known address corresponding - * to the networkId. */ constructor( web3Wrapper: Web3Wrapper, diff --git a/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts index b7bdfb149..16179447b 100644 --- a/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts @@ -23,9 +23,8 @@ export class OrderValidatorWrapper extends ContractWrapper { * Instantiate OrderValidatorWrapper * @param web3Wrapper Web3Wrapper instance to use. * @param networkId Desired networkId. - * @param address (Optional) The address of the OrderValidator contract. If - * undefined, will default to the known address corresponding to the - * networkId. + * @param address The address of the OrderValidator contract. If undefined, + * will default to the known address corresponding to the networkId. */ constructor(web3Wrapper: Web3Wrapper, networkId: number, address?: string) { super(web3Wrapper, networkId); -- cgit v1.2.3 From c83dec22c9e70cec2b75c9e5051f37124baa2761 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Wed, 10 Oct 2018 16:52:35 -0700 Subject: Improve error handling for unknown network ids in contract-wrappers --- packages/contract-wrappers/src/contract_wrappers.ts | 4 +++- .../src/contract_wrappers/contract_wrapper.ts | 6 +----- .../src/contract_wrappers/erc20_proxy_wrapper.ts | 3 ++- .../src/contract_wrappers/erc721_proxy_wrapper.ts | 3 ++- .../src/contract_wrappers/exchange_wrapper.ts | 5 +++-- .../src/contract_wrappers/forwarder_wrapper.ts | 8 ++++---- .../src/contract_wrappers/order_validator_wrapper.ts | 3 ++- packages/contract-wrappers/src/utils/contract_addresses.ts | 13 +++++++++++++ 8 files changed, 30 insertions(+), 15 deletions(-) create mode 100644 packages/contract-wrappers/src/utils/contract_addresses.ts (limited to 'packages/contract-wrappers/src') diff --git a/packages/contract-wrappers/src/contract_wrappers.ts b/packages/contract-wrappers/src/contract_wrappers.ts index 34eb1f303..8cb322912 100644 --- a/packages/contract-wrappers/src/contract_wrappers.ts +++ b/packages/contract-wrappers/src/contract_wrappers.ts @@ -27,6 +27,8 @@ import { contractWrappersPublicNetworkConfigSchema } from './schemas/contract_wr import { ContractWrappersConfig } from './types'; import { assert } from './utils/assert'; import { constants } from './utils/constants'; +import { _getDefaultContractAddresses } from './utils/contract_addresses'; + /** * The ContractWrappers class contains smart contract wrappers helpful when building on 0x protocol. */ @@ -102,7 +104,7 @@ export class ContractWrappers { ? constants.DEFAULT_BLOCK_POLLING_INTERVAL : config.blockPollingIntervalMs; const contractAddresses = _.isUndefined(config.contractAddresses) - ? getContractAddressesForNetwork(config.networkId) + ? _getDefaultContractAddresses(config.networkId) : config.contractAddresses; this.erc20Proxy = new ERC20ProxyWrapper(this._web3Wrapper, config.networkId, contractAddresses.erc20Proxy); this.erc721Proxy = new ERC721ProxyWrapper(this._web3Wrapper, config.networkId, contractAddresses.erc721Proxy); diff --git a/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts index 3d37e446c..72f5aa629 100644 --- a/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts @@ -1,4 +1,4 @@ -import { ContractAddresses, getContractAddressesForNetwork } from '@0xproject/contract-addresses'; +import { ContractAddresses, getContractAddressesForNetwork, NetworkId } from '@0xproject/contract-addresses'; import { AbiDecoder, intervalUtils, logUtils } from '@0xproject/utils'; import { marshaller, Web3Wrapper } from '@0xproject/web3-wrapper'; import { @@ -112,10 +112,6 @@ export abstract class ContractWrapper { const logWithDecodedArgs = abiDecoder.tryToDecodeLogOrNoop(log); return logWithDecodedArgs; } - protected _getDefaultContractAddresses(): ContractAddresses { - // TODO(albrow): Figure out better error handling here. - return getContractAddressesForNetwork(this._networkId); - } private _onLogStateChanged(isRemoved: boolean, rawLog: RawLogEntry): void { const log: LogEntry = marshaller.unmarshalLog(rawLog); _.forEach(this._filters, (filter: FilterObject, filterToken: string) => { diff --git a/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts index 459019877..369e27e19 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts @@ -6,6 +6,7 @@ import { ContractAbi } from 'ethereum-types'; import * as _ from 'lodash'; import { assert } from '../utils/assert'; +import { _getDefaultContractAddresses } from '../utils/contract_addresses'; import { ContractWrapper } from './contract_wrapper'; @@ -25,7 +26,7 @@ export class ERC20ProxyWrapper extends ContractWrapper { */ constructor(web3Wrapper: Web3Wrapper, networkId: number, address?: string) { super(web3Wrapper, networkId); - this.address = _.isUndefined(address) ? this._getDefaultContractAddresses().erc20Proxy : address; + this.address = _.isUndefined(address) ? _getDefaultContractAddresses(networkId).erc20Proxy : address; } /** * Get the 4 bytes ID of this asset proxy diff --git a/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts index 9cf56dec1..0c61d939f 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts @@ -6,6 +6,7 @@ import { ContractAbi } from 'ethereum-types'; import * as _ from 'lodash'; import { assert } from '../utils/assert'; +import { _getDefaultContractAddresses } from '../utils/contract_addresses'; import { ContractWrapper } from './contract_wrapper'; @@ -25,7 +26,7 @@ export class ERC721ProxyWrapper extends ContractWrapper { */ constructor(web3Wrapper: Web3Wrapper, networkId: number, address?: string) { super(web3Wrapper, networkId); - this.address = _.isUndefined(address) ? this._getDefaultContractAddresses().erc721Proxy : address; + this.address = _.isUndefined(address) ? _getDefaultContractAddresses(networkId).erc721Proxy : address; } /** * Get the 4 bytes ID of this asset proxy diff --git a/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts index 67c0351e2..a9809e0e7 100644 --- a/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts @@ -29,6 +29,7 @@ import { ValidateOrderFillableOpts, } from '../types'; import { assert } from '../utils/assert'; +import { _getDefaultContractAddresses } from '../utils/contract_addresses'; import { decorators } from '../utils/decorators'; import { TransactionEncoder } from '../utils/transaction_encoder'; @@ -72,9 +73,9 @@ export class ExchangeWrapper extends ContractWrapper { super(web3Wrapper, networkId, blockPollingIntervalMs); this._erc20TokenWrapper = erc20TokenWrapper; this._erc721TokenWrapper = erc721TokenWrapper; - this.address = _.isUndefined(address) ? this._getDefaultContractAddresses().exchange : address; + this.address = _.isUndefined(address) ? _getDefaultContractAddresses(networkId).exchange : address; this.zrxTokenAddress = _.isUndefined(zrxTokenAddress) - ? this._getDefaultContractAddresses().zrxToken + ? _getDefaultContractAddresses(networkId).zrxToken : zrxTokenAddress; } /** diff --git a/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts index fe4d0cbda..fd7a9a362 100644 --- a/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts @@ -7,12 +7,12 @@ import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { ContractAbi } from 'ethereum-types'; import * as _ from 'lodash'; -import { orderTxOptsSchema } from '../schemas/order_tx_opts_schema'; import { txOptsSchema } from '../schemas/tx_opts_schema'; import { OrderTransactionOpts } from '../types'; import { assert } from '../utils/assert'; import { calldataOptimizationUtils } from '../utils/calldata_optimization_utils'; import { constants } from '../utils/constants'; +import { _getDefaultContractAddresses } from '../utils/contract_addresses'; import { decorators } from '../utils/decorators'; import { utils } from '../utils/utils'; @@ -49,12 +49,12 @@ export class ForwarderWrapper extends ContractWrapper { etherTokenAddress?: string, ) { super(web3Wrapper, networkId); - this.address = _.isUndefined(address) ? this._getDefaultContractAddresses().exchange : address; + this.address = _.isUndefined(address) ? _getDefaultContractAddresses(networkId).exchange : address; this.zrxTokenAddress = _.isUndefined(zrxTokenAddress) - ? this._getDefaultContractAddresses().zrxToken + ? _getDefaultContractAddresses(networkId).zrxToken : zrxTokenAddress; this.etherTokenAddress = _.isUndefined(etherTokenAddress) - ? this._getDefaultContractAddresses().etherToken + ? _getDefaultContractAddresses(networkId).etherToken : etherTokenAddress; } /** diff --git a/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts index 16179447b..4b6196eff 100644 --- a/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts @@ -9,6 +9,7 @@ import * as _ from 'lodash'; import { BalanceAndAllowance, OrderAndTraderInfo, TraderInfo } from '../types'; import { assert } from '../utils/assert'; +import { _getDefaultContractAddresses } from '../utils/contract_addresses'; import { ContractWrapper } from './contract_wrapper'; @@ -28,7 +29,7 @@ export class OrderValidatorWrapper extends ContractWrapper { */ constructor(web3Wrapper: Web3Wrapper, networkId: number, address?: string) { super(web3Wrapper, networkId); - this.address = _.isUndefined(address) ? this._getDefaultContractAddresses().exchange : address; + this.address = _.isUndefined(address) ? _getDefaultContractAddresses(networkId).exchange : address; } /** * Get an object conforming to OrderAndTraderInfo containing on-chain information of the provided order and address diff --git a/packages/contract-wrappers/src/utils/contract_addresses.ts b/packages/contract-wrappers/src/utils/contract_addresses.ts new file mode 100644 index 000000000..c32907c24 --- /dev/null +++ b/packages/contract-wrappers/src/utils/contract_addresses.ts @@ -0,0 +1,13 @@ +import { ContractAddresses, getContractAddressesForNetwork, NetworkId } from '@0xproject/contract-addresses'; +import * as _ from 'lodash'; + +// Returns the default contract addresses for the given networkId or throws with +// a context-specific error message if the networkId is not recognized. +export function _getDefaultContractAddresses(networkId: number): ContractAddresses { + if (!(networkId in NetworkId)) { + throw new Error( + `No default contract addresses found for the given network id (${networkId}). If you want to use ContractWrappers on this network, you must manually pass in the contract address(es) to the constructor.`, + ); + } + return getContractAddressesForNetwork(networkId); +} -- cgit v1.2.3 From 003ab1e5b311bfedcffce07f03035e42736a47af Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Fri, 12 Oct 2018 15:10:18 -0700 Subject: Fix linter errors in contract-wrappers --- packages/contract-wrappers/src/contract_wrappers.ts | 1 - packages/contract-wrappers/src/utils/contract_addresses.ts | 6 ++++-- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'packages/contract-wrappers/src') diff --git a/packages/contract-wrappers/src/contract_wrappers.ts b/packages/contract-wrappers/src/contract_wrappers.ts index 8cb322912..ebca2e99d 100644 --- a/packages/contract-wrappers/src/contract_wrappers.ts +++ b/packages/contract-wrappers/src/contract_wrappers.ts @@ -1,4 +1,3 @@ -import { getContractAddressesForNetwork } from '@0xproject/contract-addresses'; import { ERC20Proxy, ERC20Token, diff --git a/packages/contract-wrappers/src/utils/contract_addresses.ts b/packages/contract-wrappers/src/utils/contract_addresses.ts index c32907c24..cd0d31308 100644 --- a/packages/contract-wrappers/src/utils/contract_addresses.ts +++ b/packages/contract-wrappers/src/utils/contract_addresses.ts @@ -1,8 +1,10 @@ import { ContractAddresses, getContractAddressesForNetwork, NetworkId } from '@0xproject/contract-addresses'; import * as _ from 'lodash'; -// Returns the default contract addresses for the given networkId or throws with -// a context-specific error message if the networkId is not recognized. +/** + * Returns the default contract addresses for the given networkId or throws with + * a context-specific error message if the networkId is not recognized. + */ export function _getDefaultContractAddresses(networkId: number): ContractAddresses { if (!(networkId in NetworkId)) { throw new Error( -- cgit v1.2.3 From f0e483798339b24b599c408949265cca54d7e58b Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Mon, 15 Oct 2018 13:30:12 -0700 Subject: Fix failing doc generation tests --- packages/contract-wrappers/src/index.ts | 2 ++ 1 file changed, 2 insertions(+) (limited to 'packages/contract-wrappers/src') diff --git a/packages/contract-wrappers/src/index.ts b/packages/contract-wrappers/src/index.ts index 50dfe88be..f24828153 100644 --- a/packages/contract-wrappers/src/index.ts +++ b/packages/contract-wrappers/src/index.ts @@ -1,3 +1,5 @@ +export { ContractAddresses } from '@0xproject/contract-addresses'; + export { WETH9Events, WETH9WithdrawalEventArgs, -- cgit v1.2.3 From 00db096d2e387b51db627fbb6c0f41f5e8f67a9c Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Mon, 15 Oct 2018 13:44:21 -0700 Subject: Fix bug introduced during rebase --- packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts | 1 + 1 file changed, 1 insertion(+) (limited to 'packages/contract-wrappers/src') diff --git a/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts index fd7a9a362..93dcd6d66 100644 --- a/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts @@ -7,6 +7,7 @@ import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { ContractAbi } from 'ethereum-types'; import * as _ from 'lodash'; +import { orderTxOptsSchema } from '../schemas/order_tx_opts_schema'; import { txOptsSchema } from '../schemas/tx_opts_schema'; import { OrderTransactionOpts } from '../types'; import { assert } from '../utils/assert'; -- cgit v1.2.3 From 38b146c395429f860f57fa1865e01175eee39e62 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Mon, 15 Oct 2018 14:22:48 -0700 Subject: Add OrThrow suffix to getContractAddressesForNetwork --- packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts | 1 - packages/contract-wrappers/src/utils/contract_addresses.ts | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'packages/contract-wrappers/src') diff --git a/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts index 72f5aa629..e03e4188b 100644 --- a/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/contract_wrapper.ts @@ -1,4 +1,3 @@ -import { ContractAddresses, getContractAddressesForNetwork, NetworkId } from '@0xproject/contract-addresses'; import { AbiDecoder, intervalUtils, logUtils } from '@0xproject/utils'; import { marshaller, Web3Wrapper } from '@0xproject/web3-wrapper'; import { diff --git a/packages/contract-wrappers/src/utils/contract_addresses.ts b/packages/contract-wrappers/src/utils/contract_addresses.ts index cd0d31308..98967c52d 100644 --- a/packages/contract-wrappers/src/utils/contract_addresses.ts +++ b/packages/contract-wrappers/src/utils/contract_addresses.ts @@ -1,4 +1,4 @@ -import { ContractAddresses, getContractAddressesForNetwork, NetworkId } from '@0xproject/contract-addresses'; +import { ContractAddresses, getContractAddressesForNetworkOrThrow, NetworkId } from '@0xproject/contract-addresses'; import * as _ from 'lodash'; /** @@ -11,5 +11,5 @@ export function _getDefaultContractAddresses(networkId: number): ContractAddress `No default contract addresses found for the given network id (${networkId}). If you want to use ContractWrappers on this network, you must manually pass in the contract address(es) to the constructor.`, ); } - return getContractAddressesForNetwork(networkId); + return getContractAddressesForNetworkOrThrow(networkId); } -- cgit v1.2.3 From e093864bff4b9e6191d6ed4d7fe8ce9c60c5c367 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Mon, 15 Oct 2018 17:00:48 -0700 Subject: Update json-schemas for contract-wrappers --- .../contract-wrappers/src/contract_wrappers.ts | 7 +--- .../src/schemas/contract_wrappers_config_schema.ts | 22 ++++++++++- ...tract_wrappers_private_network_config_schema.ts | 36 ------------------ ...ntract_wrappers_public_network_config_schema.ts | 44 ---------------------- 4 files changed, 22 insertions(+), 87 deletions(-) delete mode 100644 packages/contract-wrappers/src/schemas/contract_wrappers_private_network_config_schema.ts delete mode 100644 packages/contract-wrappers/src/schemas/contract_wrappers_public_network_config_schema.ts (limited to 'packages/contract-wrappers/src') diff --git a/packages/contract-wrappers/src/contract_wrappers.ts b/packages/contract-wrappers/src/contract_wrappers.ts index ebca2e99d..9802e685d 100644 --- a/packages/contract-wrappers/src/contract_wrappers.ts +++ b/packages/contract-wrappers/src/contract_wrappers.ts @@ -21,8 +21,6 @@ import { ExchangeWrapper } from './contract_wrappers/exchange_wrapper'; import { ForwarderWrapper } from './contract_wrappers/forwarder_wrapper'; import { OrderValidatorWrapper } from './contract_wrappers/order_validator_wrapper'; import { ContractWrappersConfigSchema } from './schemas/contract_wrappers_config_schema'; -import { contractWrappersPrivateNetworkConfigSchema } from './schemas/contract_wrappers_private_network_config_schema'; -import { contractWrappersPublicNetworkConfigSchema } from './schemas/contract_wrappers_public_network_config_schema'; import { ContractWrappersConfig } from './types'; import { assert } from './utils/assert'; import { constants } from './utils/constants'; @@ -78,10 +76,7 @@ export class ContractWrappers { */ constructor(provider: Provider, config: ContractWrappersConfig) { assert.isWeb3Provider('provider', provider); - assert.doesConformToSchema('config', config, ContractWrappersConfigSchema, [ - contractWrappersPrivateNetworkConfigSchema, - contractWrappersPublicNetworkConfigSchema, - ]); + assert.doesConformToSchema('config', config, ContractWrappersConfigSchema); const txDefaults = { gasPrice: config.gasPrice, }; diff --git a/packages/contract-wrappers/src/schemas/contract_wrappers_config_schema.ts b/packages/contract-wrappers/src/schemas/contract_wrappers_config_schema.ts index ac248b2d4..ae1ce668c 100644 --- a/packages/contract-wrappers/src/schemas/contract_wrappers_config_schema.ts +++ b/packages/contract-wrappers/src/schemas/contract_wrappers_config_schema.ts @@ -1,5 +1,25 @@ export const ContractWrappersConfigSchema = { id: '/ContractWrappersConfig', - oneOf: [{ $ref: '/ZeroExContractPrivateNetworkConfig' }, { $ref: '/ZeroExContractPublicNetworkConfig' }], + properties: { + networkId: { + type: 'number', + }, + gasPrice: { $ref: '/numberSchema' }, + contractAddresses: { + type: 'object', + properties: { + erc20Proxy: { $ref: '/addressSchema' }, + erc721Proxy: { $ref: '/addressSchema' }, + zrxToken: { $ref: '/addressSchema' }, + etherToken: { $ref: '/addressSchema' }, + exchange: { $ref: '/addressSchema' }, + assetProxyOwner: { $ref: '/addressSchema' }, + forwarder: { $ref: '/addressSchema' }, + orderValidator: { $ref: '/addressSchema' }, + }, + }, + blockPollingIntervalMs: { type: 'number' }, + }, type: 'object', + required: ['networkId'], }; diff --git a/packages/contract-wrappers/src/schemas/contract_wrappers_private_network_config_schema.ts b/packages/contract-wrappers/src/schemas/contract_wrappers_private_network_config_schema.ts deleted file mode 100644 index 904690ae7..000000000 --- a/packages/contract-wrappers/src/schemas/contract_wrappers_private_network_config_schema.ts +++ /dev/null @@ -1,36 +0,0 @@ -export const contractWrappersPrivateNetworkConfigSchema = { - id: '/ZeroExContractPrivateNetworkConfig', - properties: { - networkId: { - type: 'number', - minimum: 1, - }, - gasPrice: { $ref: '/numberSchema' }, - zrxContractAddress: { $ref: '/addressSchema' }, - exchangeContractAddress: { $ref: '/addressSchema' }, - erc20ProxyContractAddress: { $ref: '/addressSchema' }, - erc721ProxyContractAddress: { $ref: '/addressSchema' }, - blockPollingIntervalMs: { type: 'number' }, - orderWatcherConfig: { - type: 'object', - properties: { - pollingIntervalMs: { - type: 'number', - minimum: 0, - }, - numConfirmations: { - type: 'number', - minimum: 0, - }, - }, - }, - }, - type: 'object', - required: [ - 'networkId', - 'zrxContractAddress', - 'exchangeContractAddress', - 'erc20ProxyContractAddress', - 'erc721ProxyContractAddress', - ], -}; diff --git a/packages/contract-wrappers/src/schemas/contract_wrappers_public_network_config_schema.ts b/packages/contract-wrappers/src/schemas/contract_wrappers_public_network_config_schema.ts deleted file mode 100644 index 5cd008ae0..000000000 --- a/packages/contract-wrappers/src/schemas/contract_wrappers_public_network_config_schema.ts +++ /dev/null @@ -1,44 +0,0 @@ -const networkNameToId: { [networkName: string]: number } = { - mainnet: 1, - ropsten: 3, - rinkeby: 4, - kovan: 42, - ganache: 50, -}; - -export const contractWrappersPublicNetworkConfigSchema = { - id: '/ZeroExContractPublicNetworkConfig', - properties: { - networkId: { - type: 'number', - enum: [ - networkNameToId.mainnet, - networkNameToId.kovan, - networkNameToId.ropsten, - networkNameToId.rinkeby, - networkNameToId.ganache, - ], - }, - gasPrice: { $ref: '/numberSchema' }, - zrxContractAddress: { $ref: '/addressSchema' }, - exchangeContractAddress: { $ref: '/addressSchema' }, - erc20ProxyContractAddress: { $ref: '/addressSchema' }, - erc721ProxyContractAddress: { $ref: '/addressSchema' }, - blockPollingIntervalMs: { type: 'number' }, - orderWatcherConfig: { - type: 'object', - properties: { - pollingIntervalMs: { - type: 'number', - minimum: 0, - }, - numConfirmations: { - type: 'number', - minimum: 0, - }, - }, - }, - }, - type: 'object', - required: ['networkId'], -}; -- cgit v1.2.3 From 6f8e98e53727a1efc08800017cf2bde38884c982 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Mon, 15 Oct 2018 17:35:25 -0700 Subject: In contract-wrappers, remove setProvider and add unsubscribeAll method. --- packages/contract-wrappers/src/contract_wrappers.ts | 20 ++++++-------------- .../src/contract_wrappers/erc20_proxy_wrapper.ts | 6 ------ .../src/contract_wrappers/erc20_token_wrapper.ts | 7 ------- .../src/contract_wrappers/erc721_proxy_wrapper.ts | 6 ------ .../src/contract_wrappers/erc721_token_wrapper.ts | 7 ------- .../src/contract_wrappers/ether_token_wrapper.ts | 5 ----- .../src/contract_wrappers/exchange_wrapper.ts | 5 ----- .../src/contract_wrappers/forwarder_wrapper.ts | 6 ------ .../src/contract_wrappers/order_validator_wrapper.ts | 6 ------ 9 files changed, 6 insertions(+), 62 deletions(-) (limited to 'packages/contract-wrappers/src') diff --git a/packages/contract-wrappers/src/contract_wrappers.ts b/packages/contract-wrappers/src/contract_wrappers.ts index 9802e685d..36381c623 100644 --- a/packages/contract-wrappers/src/contract_wrappers.ts +++ b/packages/contract-wrappers/src/contract_wrappers.ts @@ -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 diff --git a/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts index 369e27e19..e59b2c20b 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc20_proxy_wrapper.ts @@ -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; diff --git a/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts index 2db0165bc..97043bd19 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts @@ -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 { const normalizedTokenAddress = tokenAddress.toLowerCase(); let tokenContract = this._tokenContractsByAddress[normalizedTokenAddress]; diff --git a/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts index 0c61d939f..adee3e3ab 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc721_proxy_wrapper.ts @@ -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; diff --git a/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts index b18692964..a59801b41 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts @@ -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 { const normalizedTokenAddress = tokenAddress.toLowerCase(); let tokenContract = this._tokenContractsByAddress[normalizedTokenAddress]; diff --git a/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts index 2586401bc..8bb27369f 100644 --- a/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts @@ -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 { let etherTokenContract = this._etherTokenContractsByAddress[etherTokenAddress]; if (!_.isUndefined(etherTokenContract)) { diff --git a/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts index a9809e0e7..9063e0e40 100644 --- a/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/exchange_wrapper.ts @@ -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 { if (!_.isUndefined(this._exchangeContractIfExists)) { diff --git a/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts index 93dcd6d66..b6fc071ea 100644 --- a/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/forwarder_wrapper.ts @@ -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 { if (!_.isUndefined(this._forwarderContractIfExists)) { return this._forwarderContractIfExists; diff --git a/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts index 4b6196eff..2ae1a158b 100644 --- a/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/order_validator_wrapper.ts @@ -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 { if (!_.isUndefined(this._orderValidatorContractIfExists)) { return this._orderValidatorContractIfExists; -- cgit v1.2.3 From 5bdfad9b415fc183102d5f5a7f3a08782921f003 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Tue, 16 Oct 2018 00:11:58 -0700 Subject: Remove ContractNotFound errors in contract-wrappers --- .../contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts | 6 ------ .../src/contract_wrappers/erc721_token_wrapper.ts | 6 ------ .../contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts | 7 ------- packages/contract-wrappers/src/types.ts | 7 ------- 4 files changed, 26 deletions(-) (limited to 'packages/contract-wrappers/src') diff --git a/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts index 97043bd19..192eaf0a7 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc20_token_wrapper.ts @@ -428,12 +428,6 @@ export class ERC20TokenWrapper extends ContractWrapper { if (!_.isUndefined(tokenContract)) { return tokenContract; } - // TODO(albrow): Do we really still need this check? The default error - // looks okay to me. - const doesContractExist = await this._web3Wrapper.doesContractExistAtAddressAsync(tokenAddress); - if (!doesContractExist) { - throw new Error(ContractWrappersError.ERC20TokenContractDoesNotExist); - } const contractInstance = new ERC20TokenContract( this.abi, normalizedTokenAddress, diff --git a/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts index a59801b41..d92d7087c 100644 --- a/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/erc721_token_wrapper.ts @@ -455,12 +455,6 @@ export class ERC721TokenWrapper extends ContractWrapper { if (!_.isUndefined(tokenContract)) { return tokenContract; } - // TODO(albrow): Do we really still need this check? The default error - // looks okay to me. - const doesContractExist = await this._web3Wrapper.doesContractExistAtAddressAsync(tokenAddress); - if (!doesContractExist) { - throw new Error(ContractWrappersError.ERC721TokenContractDoesNotExist); - } const contractInstance = new ERC721TokenContract( this.abi, normalizedTokenAddress, diff --git a/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts b/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts index 8bb27369f..d5775208e 100644 --- a/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts +++ b/packages/contract-wrappers/src/contract_wrappers/ether_token_wrapper.ts @@ -197,13 +197,6 @@ export class EtherTokenWrapper extends ContractWrapper { if (!_.isUndefined(etherTokenContract)) { return etherTokenContract; } - // TODO(albrow): Do we really still need this check? The default error - // looks okay to me. - // TODO(albrow): Should we normalize the token address here? - const doesContractExist = await this._web3Wrapper.doesContractExistAtAddressAsync(etherTokenAddress); - if (!doesContractExist) { - throw new Error(ContractWrappersError.EtherTokenContractDoesNotExist); - } const contractInstance = new WETH9Contract( this.abi, etherTokenAddress, diff --git a/packages/contract-wrappers/src/types.ts b/packages/contract-wrappers/src/types.ts index e70adb991..895a47cc1 100644 --- a/packages/contract-wrappers/src/types.ts +++ b/packages/contract-wrappers/src/types.ts @@ -19,13 +19,6 @@ export enum ExchangeWrapperError { } export enum ContractWrappersError { - ExchangeContractDoesNotExist = 'EXCHANGE_CONTRACT_DOES_NOT_EXIST', - ZRXContractDoesNotExist = 'ZRX_CONTRACT_DOES_NOT_EXIST', - EtherTokenContractDoesNotExist = 'ETHER_TOKEN_CONTRACT_DOES_NOT_EXIST', - ERC20ProxyContractDoesNotExist = 'ERC20_PROXY_CONTRACT_DOES_NOT_EXIST', - ERC721ProxyContractDoesNotExist = 'ERC721_PROXY_CONTRACT_DOES_NOT_EXIST', - ERC20TokenContractDoesNotExist = 'ERC20_TOKEN_CONTRACT_DOES_NOT_EXIST', - ERC721TokenContractDoesNotExist = 'ERC721_TOKEN_CONTRACT_DOES_NOT_EXIST', ContractNotDeployedOnNetwork = 'CONTRACT_NOT_DEPLOYED_ON_NETWORK', InsufficientAllowanceForTransfer = 'INSUFFICIENT_ALLOWANCE_FOR_TRANSFER', InsufficientBalanceForTransfer = 'INSUFFICIENT_BALANCE_FOR_TRANSFER', -- cgit v1.2.3