diff options
Diffstat (limited to 'packages/contract-wrappers/src')
11 files changed, 136 insertions, 60 deletions
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<Block, Log> | 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<ArgsType extends ContractEventArgs>(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; } |