From f1b267cc9fe7f6e5566dc2535b064b92aef92df1 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 6 Dec 2017 20:55:09 +0300 Subject: Refactor web3Wrapper to a separate package --- .../src/contract_wrappers/contract_wrapper.ts | 31 +++++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'packages/0x.js/src/contract_wrappers/contract_wrapper.ts') diff --git a/packages/0x.js/src/contract_wrappers/contract_wrapper.ts b/packages/0x.js/src/contract_wrappers/contract_wrapper.ts index 5e5a38f8c..0b6fc031a 100644 --- a/packages/0x.js/src/contract_wrappers/contract_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/contract_wrapper.ts @@ -1,3 +1,4 @@ +import {Web3Wrapper} from '@0xproject/web3-wrapper'; import {Block, BlockAndLogStreamer} from 'ethereumjs-blockstream'; import * as _ from 'lodash'; import * as Web3 from 'web3'; @@ -19,7 +20,15 @@ import {AbiDecoder} from '../utils/abi_decoder'; import {constants} from '../utils/constants'; import {filterUtils} from '../utils/filter_utils'; import {intervalUtils} from '../utils/interval_utils'; -import {Web3Wrapper} from '../web3_wrapper'; + +const CONTRACT_NAME_TO_NOT_FOUND_ERROR: {[contractName: string]: ZeroExError} = { + ZRX: ZeroExError.ZRXContractDoesNotExist, + EtherToken: ZeroExError.EtherTokenContractDoesNotExist, + Token: ZeroExError.TokenContractDoesNotExist, + TokenRegistry: ZeroExError.TokenRegistryContractDoesNotExist, + TokenTransferProxy: ZeroExError.TokenTransferProxyContractDoesNotExist, + Exchange: ZeroExError.ExchangeContractDoesNotExist, +}; export class ContractWrapper { protected _web3Wrapper: Web3Wrapper; @@ -93,10 +102,24 @@ export class ContractWrapper { protected async _instantiateContractIfExistsAsync( artifact: Artifact, addressIfExists?: string, ): Promise { - const web3ContractInstance = await this._web3Wrapper.getContractInstanceFromArtifactAsync( - artifact, addressIfExists, + let contractAddress: string; + if (_.isUndefined(addressIfExists)) { + const networkId = this._web3Wrapper.getNetworkId(); + if (_.isUndefined(artifact.networks[networkId])) { + throw new Error(ZeroExError.ContractNotDeployedOnNetwork); + } + contractAddress = artifact.networks[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.contract_name]); + } + const contractInstance = this._web3Wrapper.getContractInstance( + artifact.abi, contractAddress, ); - return web3ContractInstance; + return contractInstance; } protected _getContractAddress(artifact: Artifact, addressIfExists?: string): string { if (_.isUndefined(addressIfExists)) { -- cgit v1.2.3 From b362e2c28e9cafa7335bced17ec61fba93b018e6 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 8 Dec 2017 12:51:46 +0300 Subject: Refactor networkId out of web3Wrapper --- packages/0x.js/src/contract_wrappers/contract_wrapper.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'packages/0x.js/src/contract_wrappers/contract_wrapper.ts') diff --git a/packages/0x.js/src/contract_wrappers/contract_wrapper.ts b/packages/0x.js/src/contract_wrappers/contract_wrapper.ts index 0b6fc031a..d56b8632d 100644 --- a/packages/0x.js/src/contract_wrappers/contract_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/contract_wrapper.ts @@ -32,6 +32,7 @@ const CONTRACT_NAME_TO_NOT_FOUND_ERROR: {[contractName: string]: ZeroExError} = export class ContractWrapper { protected _web3Wrapper: Web3Wrapper; + private _networkId: number; private _abiDecoder?: AbiDecoder; private _blockAndLogStreamer: BlockAndLogStreamer|undefined; private _blockAndLogStreamInterval: NodeJS.Timer; @@ -39,8 +40,9 @@ export class ContractWrapper { private _filterCallbacks: {[filterToken: string]: EventCallback}; private _onLogAddedSubscriptionToken: string|undefined; private _onLogRemovedSubscriptionToken: string|undefined; - constructor(web3Wrapper: Web3Wrapper, abiDecoder?: AbiDecoder) { + constructor(web3Wrapper: Web3Wrapper, networkId: number, abiDecoder?: AbiDecoder) { this._web3Wrapper = web3Wrapper; + this._networkId = networkId; this._abiDecoder = abiDecoder; this._filters = {}; this._filterCallbacks = {}; @@ -104,11 +106,10 @@ export class ContractWrapper { ): Promise { let contractAddress: string; if (_.isUndefined(addressIfExists)) { - const networkId = this._web3Wrapper.getNetworkId(); - if (_.isUndefined(artifact.networks[networkId])) { + if (_.isUndefined(artifact.networks[this._networkId])) { throw new Error(ZeroExError.ContractNotDeployedOnNetwork); } - contractAddress = artifact.networks[networkId].address.toLowerCase(); + contractAddress = artifact.networks[this._networkId].address.toLowerCase(); } else { contractAddress = addressIfExists; } @@ -123,8 +124,7 @@ export class ContractWrapper { } protected _getContractAddress(artifact: Artifact, addressIfExists?: string): string { if (_.isUndefined(addressIfExists)) { - const networkId = this._web3Wrapper.getNetworkId(); - const contractAddress = artifact.networks[networkId].address; + const contractAddress = artifact.networks[this._networkId].address; if (_.isUndefined(contractAddress)) { throw new Error(ZeroExError.ExchangeContractDoesNotExist); } -- cgit v1.2.3