diff options
author | Leonid <logvinov.leon@gmail.com> | 2018-02-06 02:27:20 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-06 02:27:20 +0800 |
commit | 2c7db23022d429758dd01d8d63afdf50d19013ba (patch) | |
tree | d712f3b1ed773d22032e631001eb0f1414cbdbd7 /packages | |
parent | c7ad6ebad6ab65a4b1e4a2084e744c6ca2bc09b8 (diff) | |
parent | 5324dfa5a70a32f9cc2c51b8252a5310439d3a86 (diff) | |
download | dexon-sol-tools-2c7db23022d429758dd01d8d63afdf50d19013ba.tar.gz dexon-sol-tools-2c7db23022d429758dd01d8d63afdf50d19013ba.tar.zst dexon-sol-tools-2c7db23022d429758dd01d8d63afdf50d19013ba.zip |
Merge pull request #350 from 0xProject/fix/ether_token_address
Add zeroEx.etherToken.getContractAddressIfExists
Diffstat (limited to 'packages')
-rw-r--r-- | packages/0x.js/CHANGELOG.md | 6 | ||||
-rw-r--r-- | packages/0x.js/src/contract_wrappers/contract_wrapper.ts | 2 | ||||
-rw-r--r-- | packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts | 13 | ||||
-rw-r--r-- | packages/0x.js/test/ether_token_wrapper_test.ts | 15 |
4 files changed, 32 insertions, 4 deletions
diff --git a/packages/0x.js/CHANGELOG.md b/packages/0x.js/CHANGELOG.md index 5250402c6..06b11cba8 100644 --- a/packages/0x.js/CHANGELOG.md +++ b/packages/0x.js/CHANGELOG.md @@ -1,8 +1,12 @@ # CHANGELOG +## v0.x.0 - _TBD, 2018_ + + * Add `zeroEx.etherToken.getContractAddressIfExists` (#350) + ## v0.31.1 - _February 1, 2018_ - * Fix the bug causing order watcher to throw is makerToken === zrx (#357) + * Fix the bug causing order watcher to throw if makerToken === zrx (#357) ## v0.31.0 - _January 30, 2018_ diff --git a/packages/0x.js/src/contract_wrappers/contract_wrapper.ts b/packages/0x.js/src/contract_wrappers/contract_wrapper.ts index 2d5818618..d913e8d9b 100644 --- a/packages/0x.js/src/contract_wrappers/contract_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/contract_wrapper.ts @@ -32,7 +32,7 @@ const CONTRACT_NAME_TO_NOT_FOUND_ERROR: { export class ContractWrapper { protected _web3Wrapper: Web3Wrapper; - private _networkId: number; + protected _networkId: number; private _abiDecoder?: AbiDecoder; private _blockAndLogStreamerIfExists?: BlockAndLogStreamer; private _blockAndLogStreamIntervalIfExists?: NodeJS.Timer; diff --git a/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts b/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts index cbafcfe94..32c9ae6a9 100644 --- a/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts @@ -162,6 +162,19 @@ 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.EtherTokenArtifact.networks[this._networkId]; + const contractAddressIfExists = _.isUndefined(networkSpecificArtifact) + ? undefined + : networkSpecificArtifact.address; + return contractAddressIfExists; + } private _invalidateContractInstance(): void { this._unsubscribeAll(); this._etherTokenContractsByAddress = {}; diff --git a/packages/0x.js/test/ether_token_wrapper_test.ts b/packages/0x.js/test/ether_token_wrapper_test.ts index 9716abab8..da49ec467 100644 --- a/packages/0x.js/test/ether_token_wrapper_test.ts +++ b/packages/0x.js/test/ether_token_wrapper_test.ts @@ -17,7 +17,6 @@ import { ZeroEx, ZeroExError, } from '../src'; -import { artifacts } from '../src/artifacts'; import { DoneCallback } from '../src/types'; import { chaiSetup } from './utils/chai_setup'; @@ -60,7 +59,7 @@ describe('EtherTokenWrapper', () => { tokens = await zeroEx.tokenRegistry.getTokensAsync(); userAddresses = await zeroEx.getAvailableAddressesAsync(); addressWithETH = userAddresses[0]; - wethContractAddress = (zeroEx.etherToken as any)._getContractAddress(artifacts.EtherTokenArtifact); + wethContractAddress = zeroEx.etherToken.getContractAddressIfExists() as string; depositWeiAmount = (zeroEx as any)._web3Wrapper.toWei(new BigNumber(5)); decimalPlaces = 7; addressWithoutFunds = userAddresses[1]; @@ -71,6 +70,18 @@ describe('EtherTokenWrapper', () => { afterEach(async () => { await blockchainLifecycle.revertAsync(); }); + describe('#getContractAddressIfExists', async () => { + it('should return contract address if connected to a known network', () => { + const contractAddressIfExists = zeroEx.etherToken.getContractAddressIfExists(); + expect(contractAddressIfExists).to.not.be.undefined(); + }); + it('should return undefined if connected to an unknown network', () => { + const UNKNOWN_NETWORK_NETWORK_ID = 10; + const unknownNetworkZeroEx = new ZeroEx(web3.currentProvider, { networkId: UNKNOWN_NETWORK_NETWORK_ID }); + const contractAddressIfExists = unknownNetworkZeroEx.etherToken.getContractAddressIfExists(); + expect(contractAddressIfExists).to.be.undefined(); + }); + }); describe('#depositAsync', () => { it('should successfully deposit ETH and issue Wrapped ETH tokens', async () => { const preETHBalance = await (zeroEx as any)._web3Wrapper.getBalanceInWeiAsync(addressWithETH); |