diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-12-07 01:55:09 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-12-07 01:55:09 +0800 |
commit | f1b267cc9fe7f6e5566dc2535b064b92aef92df1 (patch) | |
tree | c07a9dd32c5b959d33587ce2d38098dfe397badb /packages/0x.js/src/contract_wrappers/contract_wrapper.ts | |
parent | 598f1dd2d8a89b0e3ab04b2917138997031bafc6 (diff) | |
download | dexon-0x-contracts-f1b267cc9fe7f6e5566dc2535b064b92aef92df1.tar dexon-0x-contracts-f1b267cc9fe7f6e5566dc2535b064b92aef92df1.tar.gz dexon-0x-contracts-f1b267cc9fe7f6e5566dc2535b064b92aef92df1.tar.bz2 dexon-0x-contracts-f1b267cc9fe7f6e5566dc2535b064b92aef92df1.tar.lz dexon-0x-contracts-f1b267cc9fe7f6e5566dc2535b064b92aef92df1.tar.xz dexon-0x-contracts-f1b267cc9fe7f6e5566dc2535b064b92aef92df1.tar.zst dexon-0x-contracts-f1b267cc9fe7f6e5566dc2535b064b92aef92df1.zip |
Refactor web3Wrapper to a separate package
Diffstat (limited to 'packages/0x.js/src/contract_wrappers/contract_wrapper.ts')
-rw-r--r-- | packages/0x.js/src/contract_wrappers/contract_wrapper.ts | 31 |
1 files changed, 27 insertions, 4 deletions
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<Web3.ContractInstance> { - 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)) { |