diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-11-22 06:50:14 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-11-24 05:13:36 +0800 |
commit | 63dc606a9c08dad5e0aae09b86178fcd3e78e867 (patch) | |
tree | 1b82eb0d3da14ab22d614d69338623086214c3db /packages | |
parent | 131236305bff6cdf85a7edb7e3c61c50ffbb1956 (diff) | |
download | dexon-sol-tools-63dc606a9c08dad5e0aae09b86178fcd3e78e867.tar dexon-sol-tools-63dc606a9c08dad5e0aae09b86178fcd3e78e867.tar.gz dexon-sol-tools-63dc606a9c08dad5e0aae09b86178fcd3e78e867.tar.bz2 dexon-sol-tools-63dc606a9c08dad5e0aae09b86178fcd3e78e867.tar.lz dexon-sol-tools-63dc606a9c08dad5e0aae09b86178fcd3e78e867.tar.xz dexon-sol-tools-63dc606a9c08dad5e0aae09b86178fcd3e78e867.tar.zst dexon-sol-tools-63dc606a9c08dad5e0aae09b86178fcd3e78e867.zip |
Make getZRXTokenAddress non async
Diffstat (limited to 'packages')
-rw-r--r-- | packages/0x.js/src/artifacts.ts | 2 | ||||
-rw-r--r-- | packages/0x.js/src/contract_wrappers/exchange_wrapper.ts | 32 | ||||
-rw-r--r-- | packages/0x.js/src/contract_wrappers/token_wrapper.ts | 4 | ||||
-rw-r--r-- | packages/0x.js/src/order_watcher/order_state_watcher.ts | 4 | ||||
-rw-r--r-- | packages/0x.js/src/types.ts | 3 | ||||
-rw-r--r-- | packages/0x.js/src/utils/order_state_utils.ts | 2 | ||||
-rw-r--r-- | packages/0x.js/test/artifacts_test.ts | 9 | ||||
-rw-r--r-- | packages/0x.js/test/event_watcher_test.ts | 2 | ||||
-rw-r--r-- | packages/0x.js/test/exchange_wrapper_test.ts | 4 | ||||
-rw-r--r-- | packages/0x.js/test/utils/constants.ts | 2 | ||||
-rw-r--r-- | packages/0x.js/test/web3_wrapper_test.ts | 32 |
11 files changed, 40 insertions, 56 deletions
diff --git a/packages/0x.js/src/artifacts.ts b/packages/0x.js/src/artifacts.ts index 447f9880a..93b11a959 100644 --- a/packages/0x.js/src/artifacts.ts +++ b/packages/0x.js/src/artifacts.ts @@ -1,4 +1,5 @@ import {Artifact} from './types'; +import * as ZRXArtifact from './artifacts/ZRX.json'; import * as TokenArtifact from './artifacts/Token.json'; import * as ExchangeArtifact from './artifacts/Exchange.json'; import * as EtherTokenArtifact from './artifacts/EtherToken.json'; @@ -6,6 +7,7 @@ import * as TokenRegistryArtifact from './artifacts/TokenRegistry.json'; import * as TokenTransferProxyArtifact from './artifacts/TokenTransferProxy.json'; export const artifacts = { + ZRXArtifact: ZRXArtifact as any as Artifact, TokenArtifact: TokenArtifact as any as Artifact, ExchangeArtifact: ExchangeArtifact as any as Artifact, EtherTokenArtifact: EtherTokenArtifact as any as Artifact, diff --git a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts index 3e631b73e..96b642a90 100644 --- a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts @@ -63,6 +63,7 @@ export class ExchangeWrapper extends ContractWrapper { [ExchangeContractErrCodes.ERROR_FILL_BALANCE_ALLOWANCE]: ExchangeContractErrs.FillBalanceAllowanceError, }; private _contractAddressIfExists?: string; + private _zrxContractAddressIfExists?: string; private static _getOrderAddressesAndValues(order: Order): [OrderAddresses, OrderValues] { const orderAddresses: OrderAddresses = [ order.maker, @@ -177,7 +178,7 @@ export class ExchangeWrapper extends ContractWrapper { SHOULD_VALIDATE_BY_DEFAULT : orderTransactionOpts.shouldValidate; if (shouldValidate) { - const zrxTokenAddress = await this.getZRXTokenAddressAsync(); + const zrxTokenAddress = this.getZRXTokenAddress(); const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper); await this._orderValidationUtils.validateFillOrderThrowIfInvalidAsync( exchangeTradeEmulator, signedOrder, fillTakerTokenAmount, takerAddress, zrxTokenAddress); @@ -249,7 +250,7 @@ export class ExchangeWrapper extends ContractWrapper { SHOULD_VALIDATE_BY_DEFAULT : orderTransactionOpts.shouldValidate; if (shouldValidate) { - const zrxTokenAddress = await this.getZRXTokenAddressAsync(); + const zrxTokenAddress = this.getZRXTokenAddress(); const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper); for (const signedOrder of signedOrders) { await this._orderValidationUtils.validateFillOrderThrowIfInvalidAsync( @@ -339,7 +340,7 @@ export class ExchangeWrapper extends ContractWrapper { SHOULD_VALIDATE_BY_DEFAULT : orderTransactionOpts.shouldValidate; if (shouldValidate) { - const zrxTokenAddress = await this.getZRXTokenAddressAsync(); + const zrxTokenAddress = this.getZRXTokenAddress(); const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper); for (const orderFillRequest of orderFillRequests) { await this._orderValidationUtils.validateFillOrderThrowIfInvalidAsync( @@ -419,7 +420,7 @@ export class ExchangeWrapper extends ContractWrapper { SHOULD_VALIDATE_BY_DEFAULT : orderTransactionOpts.shouldValidate; if (shouldValidate) { - const zrxTokenAddress = await this.getZRXTokenAddressAsync(); + const zrxTokenAddress = this.getZRXTokenAddress(); const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper); await this._orderValidationUtils.validateFillOrKillOrderThrowIfInvalidAsync( exchangeTradeEmulator, signedOrder, fillTakerTokenAmount, takerAddress, zrxTokenAddress); @@ -483,7 +484,7 @@ export class ExchangeWrapper extends ContractWrapper { SHOULD_VALIDATE_BY_DEFAULT : orderTransactionOpts.shouldValidate; if (shouldValidate) { - const zrxTokenAddress = await this.getZRXTokenAddressAsync(); + const zrxTokenAddress = this.getZRXTokenAddress(); const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper); for (const orderFillRequest of orderFillRequests) { await this._orderValidationUtils.validateFillOrKillOrderThrowIfInvalidAsync( @@ -719,7 +720,7 @@ export class ExchangeWrapper extends ContractWrapper { signedOrder: SignedOrder, opts?: ValidateOrderFillableOpts, ): Promise<void> { assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema); - const zrxTokenAddress = await this.getZRXTokenAddressAsync(); + const zrxTokenAddress = this.getZRXTokenAddress(); const expectedFillTakerTokenAmount = !_.isUndefined(opts) ? opts.expectedFillTakerTokenAmount : undefined; const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper); await this._orderValidationUtils.validateOrderFillableOrThrowAsync( @@ -740,7 +741,7 @@ export class ExchangeWrapper extends ContractWrapper { assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema); assert.isValidBaseUnitAmount('fillTakerTokenAmount', fillTakerTokenAmount); await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper); - const zrxTokenAddress = await this.getZRXTokenAddressAsync(); + const zrxTokenAddress = this.getZRXTokenAddress(); const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper); await this._orderValidationUtils.validateFillOrderThrowIfInvalidAsync( exchangeTradeEmulator, signedOrder, fillTakerTokenAmount, takerAddress, zrxTokenAddress); @@ -774,7 +775,7 @@ export class ExchangeWrapper extends ContractWrapper { assert.doesConformToSchema('signedOrder', signedOrder, schemas.signedOrderSchema); assert.isValidBaseUnitAmount('fillTakerTokenAmount', fillTakerTokenAmount); await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper); - const zrxTokenAddress = await this.getZRXTokenAddressAsync(); + const zrxTokenAddress = this.getZRXTokenAddress(); const exchangeTradeEmulator = new ExchangeTransferSimulator(this._tokenWrapper); await this._orderValidationUtils.validateFillOrKillOrderThrowIfInvalidAsync( exchangeTradeEmulator, signedOrder, fillTakerTokenAmount, takerAddress, zrxTokenAddress); @@ -819,10 +820,17 @@ export class ExchangeWrapper extends ContractWrapper { * Returns the ZRX token address used by the exchange contract. * @return Address of ZRX token */ - public async getZRXTokenAddressAsync(): Promise<string> { - const exchangeInstance = await this._getExchangeContractAsync(); - const ZRXtokenAddress = await exchangeInstance.ZRX_TOKEN_CONTRACT.callAsync(); - return ZRXtokenAddress; + public getZRXTokenAddress(): string { + const networkId = this._web3Wrapper.getNetworkId(); + if (_.isUndefined(this._zrxContractAddressIfExists)) { + const zrxTokenAddress = artifacts.ZRXArtifact.networks[networkId].address; + if (_.isUndefined(zrxTokenAddress)) { + throw new Error(ZeroExError.ZRXContractDoesNotExist); + } + return zrxTokenAddress; + } else { + return this._zrxContractAddressIfExists; + } } private async _invalidateContractInstancesAsync(): Promise<void> { this.unsubscribeAll(); diff --git a/packages/0x.js/src/contract_wrappers/token_wrapper.ts b/packages/0x.js/src/contract_wrappers/token_wrapper.ts index 4b89d3cfc..5fd3a2a5d 100644 --- a/packages/0x.js/src/contract_wrappers/token_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/token_wrapper.ts @@ -76,8 +76,8 @@ export class TokenWrapper extends ContractWrapper { // Hack: for some reason default estimated gas amount causes `base fee exceeds gas limit` exception // on testrpc. Probably related to https://github.com/ethereumjs/testrpc/issues/294 // TODO: Debug issue in testrpc and submit a PR, then remove this hack - const networkIdIfExists = await this._web3Wrapper.getNetworkIdIfExistsAsync(); - const gas = networkIdIfExists === constants.TESTRPC_NETWORK_ID ? ALLOWANCE_TO_ZERO_GAS_AMOUNT : undefined; + const networkId = this._web3Wrapper.getNetworkId(); + const gas = networkId === constants.TESTRPC_NETWORK_ID ? ALLOWANCE_TO_ZERO_GAS_AMOUNT : undefined; const txHash = await tokenContract.approve.sendTransactionAsync(spenderAddress, amountInBaseUnits, { from: ownerAddress, gas, diff --git a/packages/0x.js/src/order_watcher/order_state_watcher.ts b/packages/0x.js/src/order_watcher/order_state_watcher.ts index 44a41669d..870f42650 100644 --- a/packages/0x.js/src/order_watcher/order_state_watcher.ts +++ b/packages/0x.js/src/order_watcher/order_state_watcher.ts @@ -116,7 +116,7 @@ export class OrderStateWatcher { delete this._orderByOrderHash[orderHash]; delete this._orderStateByOrderHashCache[orderHash]; const exchange = (this._orderFilledCancelledLazyStore as any).exchange as ExchangeWrapper; - const zrxTokenAddress = await exchange.getZRXTokenAddressAsync(); + const zrxTokenAddress = exchange.getZRXTokenAddress(); this.removeFromDependentOrderHashes(signedOrder.maker, zrxTokenAddress, orderHash); this.removeFromDependentOrderHashes(signedOrder.maker, signedOrder.makerTokenAddress, orderHash); this._expirationWatcher.removeOrder(orderHash); @@ -263,7 +263,7 @@ export class OrderStateWatcher { } this._dependentOrderHashes[signedOrder.maker][signedOrder.makerTokenAddress].add(orderHash); const exchange = (this._orderFilledCancelledLazyStore as any).exchange as ExchangeWrapper; - const zrxTokenAddress = await exchange.getZRXTokenAddressAsync(); + const zrxTokenAddress = exchange.getZRXTokenAddress(); if (_.isUndefined(this._dependentOrderHashes[signedOrder.maker][zrxTokenAddress])) { this._dependentOrderHashes[signedOrder.maker][zrxTokenAddress] = new Set(); } diff --git a/packages/0x.js/src/types.ts b/packages/0x.js/src/types.ts index d34bb02b6..313540edb 100644 --- a/packages/0x.js/src/types.ts +++ b/packages/0x.js/src/types.ts @@ -4,6 +4,7 @@ import BigNumber from 'bignumber.js'; export enum ZeroExError { ContractDoesNotExist = 'CONTRACT_DOES_NOT_EXIST', ExchangeContractDoesNotExist = 'EXCHANGE_CONTRACT_DOES_NOT_EXIST', + ZRXContractDoesNotExist = 'ZRX_CONTRACT_DOES_NOT_EXIST', UnhandledError = 'UNHANDLED_ERROR', UserHasNoAssociatedAddress = 'USER_HAS_NO_ASSOCIATED_ADDRESSES', InvalidSignature = 'INVALID_SIGNATURE', @@ -441,7 +442,7 @@ export interface TransactionReceiptWithDecodedLogs extends TransactionReceipt { } export interface Artifact { - abi: any; + abi: Web3.ContractAbi; networks: {[networkId: number]: { address: string; }}; diff --git a/packages/0x.js/src/utils/order_state_utils.ts b/packages/0x.js/src/utils/order_state_utils.ts index 1d8f02a18..891849424 100644 --- a/packages/0x.js/src/utils/order_state_utils.ts +++ b/packages/0x.js/src/utils/order_state_utils.ts @@ -55,7 +55,7 @@ export class OrderStateUtils { // because JS doesn't support async constructors. // Moreover - it's cached under the hood so it's equivalent to an async constructor. const exchange = (this.orderFilledCancelledLazyStore as any).exchange as ExchangeWrapper; - const zrxTokenAddress = await exchange.getZRXTokenAddressAsync(); + const zrxTokenAddress = exchange.getZRXTokenAddress(); const orderHash = ZeroEx.getOrderHashHex(signedOrder); const makerBalance = await this.balanceAndProxyAllowanceLazyStore.getBalanceAsync( signedOrder.makerTokenAddress, signedOrder.maker, diff --git a/packages/0x.js/test/artifacts_test.ts b/packages/0x.js/test/artifacts_test.ts index 65feb306e..1c36fb358 100644 --- a/packages/0x.js/test/artifacts_test.ts +++ b/packages/0x.js/test/artifacts_test.ts @@ -12,15 +12,15 @@ const expect = chai.expect; const TIMEOUT = 10000; describe('Artifacts', () => { - const config = { - networkId: constants.TESTRPC_NETWORK_ID, - }; describe('contracts are deployed on kovan', () => { const kovanRpcUrl = constants.KOVAN_RPC_URL; const packageJSONContent = fs.readFileSync('package.json', 'utf-8'); const packageJSON = JSON.parse(packageJSONContent); const mnemonic = packageJSON.config.mnemonic; const web3Provider = new HDWalletProvider(mnemonic, kovanRpcUrl); + const config = { + networkId: constants.KOVAN_NETWORK_ID, + }; const zeroEx = new ZeroEx(web3Provider, config); it('token registry contract is deployed', async () => { await (zeroEx.tokenRegistry as any)._getTokenRegistryContractAsync(); @@ -38,6 +38,9 @@ describe('Artifacts', () => { const packageJSON = JSON.parse(packageJSONContent); const mnemonic = packageJSON.config.mnemonic; const web3Provider = new HDWalletProvider(mnemonic, ropstenRpcUrl); + const config = { + networkId: constants.ROPSTEN_NETWORK_ID, + }; const zeroEx = new ZeroEx(web3Provider, config); it('token registry contract is deployed', async () => { await (zeroEx.tokenRegistry as any)._getTokenRegistryContractAsync(); diff --git a/packages/0x.js/test/event_watcher_test.ts b/packages/0x.js/test/event_watcher_test.ts index 0b0d6d885..fc52d522a 100644 --- a/packages/0x.js/test/event_watcher_test.ts +++ b/packages/0x.js/test/event_watcher_test.ts @@ -58,7 +58,7 @@ describe('EventWatcher', () => { before(async () => { web3 = web3Factory.create(); const pollingIntervalMs = 10; - web3Wrapper = new Web3Wrapper(web3.currentProvider); + web3Wrapper = new Web3Wrapper(web3.currentProvider, constants.TESTRPC_NETWORK_ID); eventWatcher = new EventWatcher(web3Wrapper, pollingIntervalMs); }); afterEach(() => { diff --git a/packages/0x.js/test/exchange_wrapper_test.ts b/packages/0x.js/test/exchange_wrapper_test.ts index 773829b4e..5d1ff8f50 100644 --- a/packages/0x.js/test/exchange_wrapper_test.ts +++ b/packages/0x.js/test/exchange_wrapper_test.ts @@ -744,8 +744,8 @@ describe('ExchangeWrapper', () => { }); }); describe('#getZRXTokenAddressAsync', () => { - it('gets the same token as is in token registry', async () => { - const zrxAddress = await zeroEx.exchange.getZRXTokenAddressAsync(); + it('gets the same token as is in token registry', () => { + const zrxAddress = zeroEx.exchange.getZRXTokenAddress(); const zrxToken = tokenUtils.getProtocolTokenOrThrow(); expect(zrxAddress).to.equal(zrxToken.address); }); diff --git a/packages/0x.js/test/utils/constants.ts b/packages/0x.js/test/utils/constants.ts index 5992c226e..212abf4d6 100644 --- a/packages/0x.js/test/utils/constants.ts +++ b/packages/0x.js/test/utils/constants.ts @@ -2,6 +2,8 @@ export const constants = { NULL_ADDRESS: '0x0000000000000000000000000000000000000000', RPC_HOST: 'localhost', RPC_PORT: 8545, + ROPSTEN_NETWORK_ID: 3, + KOVAN_NETWORK_ID: 42, TESTRPC_NETWORK_ID: 50, KOVAN_RPC_URL: 'https://kovan.infura.io', ROPSTEN_RPC_URL: 'https://ropsten.infura.io', diff --git a/packages/0x.js/test/web3_wrapper_test.ts b/packages/0x.js/test/web3_wrapper_test.ts deleted file mode 100644 index ccbf336ab..000000000 --- a/packages/0x.js/test/web3_wrapper_test.ts +++ /dev/null @@ -1,32 +0,0 @@ -import * as chai from 'chai'; -import {web3Factory} from './utils/web3_factory'; -import {ZeroEx} from '../src/'; -import {Web3Wrapper} from '../src/web3_wrapper'; -import {constants} from './utils/constants'; - -chai.config.includeStack = true; -const expect = chai.expect; - -describe('Web3Wrapper', () => { - const web3Provider = web3Factory.create().currentProvider; - const config = { - networkId: constants.TESTRPC_NETWORK_ID, - }; - describe('#getNetworkIdIfExistsAsync', () => { - it('caches network id requests', async () => { - const web3Wrapper = (new ZeroEx(web3Provider, config) as any)._web3Wrapper as Web3Wrapper; - expect((web3Wrapper as any).networkIdIfExists).to.be.undefined(); - const networkIdIfExists = await web3Wrapper.getNetworkIdIfExistsAsync(); - expect((web3Wrapper as any).networkIdIfExists).to.be.equal(constants.TESTRPC_NETWORK_ID); - }); - it('invalidates network id cache on setProvider call', async () => { - const web3Wrapper = (new ZeroEx(web3Provider, config) as any)._web3Wrapper as Web3Wrapper; - expect((web3Wrapper as any).networkIdIfExists).to.be.undefined(); - const networkIdIfExists = await web3Wrapper.getNetworkIdIfExistsAsync(); - expect((web3Wrapper as any).networkIdIfExists).to.be.equal(constants.TESTRPC_NETWORK_ID); - const newProvider = web3Factory.create().currentProvider; - web3Wrapper.setProvider(newProvider); - expect((web3Wrapper as any).networkIdIfExists).to.be.undefined(); - }); - }); -}); |