From a38ef3655b47228c2cc949d917636a19d7e0dddc Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 22 Nov 2017 13:37:07 -0600 Subject: Remove even more asyncs --- .../src/contract_wrappers/ether_token_wrapper.ts | 16 +++++++++---- .../src/contract_wrappers/exchange_wrapper.ts | 6 ++--- .../token_transfer_proxy_wrapper.ts | 26 +++++++++++++--------- packages/0x.js/src/utils/order_validation_utils.ts | 7 +++--- packages/0x.js/test/0x.js_test.ts | 9 ++++---- packages/0x.js/test/ether_token_wrapper_test.ts | 2 +- packages/0x.js/test/token_wrapper_test.ts | 4 ++-- packages/tslint-config/tslint.json | 3 +++ 8 files changed, 44 insertions(+), 29 deletions(-) (limited to 'packages') 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 3cd2f0224..9867e9a23 100644 --- a/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/ether_token_wrapper.ts @@ -53,7 +53,7 @@ export class EtherTokenWrapper extends ContractWrapper { assert.isValidBaseUnitAmount('amountInWei', amountInWei); await assert.isSenderAddressAsync('withdrawer', withdrawer, this._web3Wrapper); - const wethContractAddress = await this.getContractAddressAsync(); + const wethContractAddress = this.getContractAddress(); const WETHBalanceInBaseUnits = await this._tokenWrapper.getBalanceAsync(wethContractAddress, withdrawer); assert.assert(WETHBalanceInBaseUnits.gte(amountInWei), ZeroExError.InsufficientWEthBalanceForWithdrawal); @@ -67,9 +67,17 @@ export class EtherTokenWrapper extends ContractWrapper { * Retrieves the Wrapped Ether token contract address * @return The Wrapped Ether token contract address */ - public async getContractAddressAsync(): Promise { - const wethContract = await this._getEtherTokenContractAsync(); - return wethContract.address; + public getContractAddress(): string { + const networkId = this._web3Wrapper.getNetworkId(); + if (_.isUndefined(this._contractAddressIfExists)) { + const contractAddress = artifacts.EtherTokenArtifact.networks[networkId].address; + if (_.isUndefined(contractAddress)) { + throw new Error(ZeroExError.ExchangeContractDoesNotExist); + } + return contractAddress; + } else { + return this._contractAddressIfExists; + } } private _invalidateContractInstance(): void { delete this._etherTokenContractIfExists; diff --git a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts index 22338d70f..604c53e3a 100644 --- a/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/exchange_wrapper.ts @@ -557,7 +557,7 @@ export class ExchangeWrapper extends ContractWrapper { if (shouldValidate) { const orderHash = utils.getOrderHashHex(order); const unavailableTakerTokenAmount = await this.getUnavailableTakerAmountAsync(orderHash); - await this._orderValidationUtils.validateCancelOrderThrowIfInvalidAsync( + this._orderValidationUtils.validateCancelOrderThrowIfInvalid( order, cancelTakerTokenAmount, unavailableTakerTokenAmount); } @@ -611,7 +611,7 @@ export class ExchangeWrapper extends ContractWrapper { for (const orderCancellationRequest of orderCancellationRequests) { const orderHash = utils.getOrderHashHex(orderCancellationRequest.order); const unavailableTakerTokenAmount = await this.getUnavailableTakerAmountAsync(orderHash); - await this._orderValidationUtils.validateCancelOrderThrowIfInvalidAsync( + this._orderValidationUtils.validateCancelOrderThrowIfInvalid( orderCancellationRequest.order, orderCancellationRequest.takerTokenCancelAmount, unavailableTakerTokenAmount, ); @@ -765,7 +765,7 @@ export class ExchangeWrapper extends ContractWrapper { assert.isValidBaseUnitAmount('cancelTakerTokenAmount', cancelTakerTokenAmount); const orderHash = utils.getOrderHashHex(order); const unavailableTakerTokenAmount = await this.getUnavailableTakerAmountAsync(orderHash); - await this._orderValidationUtils.validateCancelOrderThrowIfInvalidAsync( + this._orderValidationUtils.validateCancelOrderThrowIfInvalid( order, cancelTakerTokenAmount, unavailableTakerTokenAmount); } /** diff --git a/packages/0x.js/src/contract_wrappers/token_transfer_proxy_wrapper.ts b/packages/0x.js/src/contract_wrappers/token_transfer_proxy_wrapper.ts index f81845af9..6970d5433 100644 --- a/packages/0x.js/src/contract_wrappers/token_transfer_proxy_wrapper.ts +++ b/packages/0x.js/src/contract_wrappers/token_transfer_proxy_wrapper.ts @@ -2,17 +2,17 @@ import * as _ from 'lodash'; import {Web3Wrapper} from '../web3_wrapper'; import {ContractWrapper} from './contract_wrapper'; import {artifacts} from '../artifacts'; -import {TokenTransferProxyContract} from '../types'; +import {TokenTransferProxyContract, ZeroExError} from '../types'; /** * This class includes the functionality related to interacting with the TokenTransferProxy contract. */ export class TokenTransferProxyWrapper extends ContractWrapper { private _tokenTransferProxyContractIfExists?: TokenTransferProxyContract; - private _tokenTransferProxyContractAddressFetcher: () => Promise; - constructor(web3Wrapper: Web3Wrapper, tokenTransferProxyContractAddressFetcher: () => Promise) { + private _contractAddressIfExists?: string; + constructor(web3Wrapper: Web3Wrapper, contractAddressIfExists?: string) { super(web3Wrapper); - this._tokenTransferProxyContractAddressFetcher = tokenTransferProxyContractAddressFetcher; + this._contractAddressIfExists = contractAddressIfExists; } /** * Check if the Exchange contract address is authorized by the TokenTransferProxy contract. @@ -38,10 +38,17 @@ export class TokenTransferProxyWrapper extends ContractWrapper { * that the user-passed web3 provider is connected to. * @returns The Ethereum address of the TokenTransferProxy contract being used. */ - public async getContractAddressAsync(): Promise { - const proxyInstance = await this._getTokenTransferProxyContractAsync(); - const proxyAddress = proxyInstance.address; - return proxyAddress; + public getContractAddress(): string { + const networkId = this._web3Wrapper.getNetworkId(); + if (_.isUndefined(this._contractAddressIfExists)) { + const contractAddress = artifacts.TokenTransferProxyArtifact.networks[networkId].address; + if (_.isUndefined(contractAddress)) { + throw new Error(ZeroExError.ExchangeContractDoesNotExist); + } + return contractAddress; + } else { + return this._contractAddressIfExists; + } } private _invalidateContractInstance(): void { delete this._tokenTransferProxyContractIfExists; @@ -50,9 +57,8 @@ export class TokenTransferProxyWrapper extends ContractWrapper { if (!_.isUndefined(this._tokenTransferProxyContractIfExists)) { return this._tokenTransferProxyContractIfExists; } - const contractAddress = await this._tokenTransferProxyContractAddressFetcher(); const contractInstance = await this._instantiateContractIfExistsAsync( - artifacts.TokenTransferProxyArtifact, contractAddress, + artifacts.TokenTransferProxyArtifact, this._contractAddressIfExists, ); this._tokenTransferProxyContractIfExists = contractInstance as TokenTransferProxyContract; return this._tokenTransferProxyContractIfExists; diff --git a/packages/0x.js/src/utils/order_validation_utils.ts b/packages/0x.js/src/utils/order_validation_utils.ts index ed723e3d4..4822f66e2 100644 --- a/packages/0x.js/src/utils/order_validation_utils.ts +++ b/packages/0x.js/src/utils/order_validation_utils.ts @@ -92,10 +92,9 @@ export class OrderValidationUtils { throw new Error(ExchangeContractErrs.InsufficientRemainingFillAmount); } } - public async validateCancelOrderThrowIfInvalidAsync(order: Order, - cancelTakerTokenAmount: BigNumber, - unavailableTakerTokenAmount: BigNumber, - ): Promise { + public validateCancelOrderThrowIfInvalid( + order: Order, cancelTakerTokenAmount: BigNumber, unavailableTakerTokenAmount: BigNumber, + ): void { if (cancelTakerTokenAmount.eq(0)) { throw new Error(ExchangeContractErrs.OrderCancelAmountZero); } diff --git a/packages/0x.js/test/0x.js_test.ts b/packages/0x.js/test/0x.js_test.ts index 95a8b027d..ba7a92fe4 100644 --- a/packages/0x.js/test/0x.js_test.ts +++ b/packages/0x.js/test/0x.js_test.ts @@ -223,7 +223,7 @@ describe('ZeroEx library', () => { const tokens = await zeroEx.tokenRegistry.getTokensAsync(); const tokenUtils = new TokenUtils(tokens); const zrxTokenAddress = tokenUtils.getProtocolTokenOrThrow().address; - const proxyAddress = await zeroEx.proxy.getContractAddressAsync(); + const proxyAddress = zeroEx.proxy.getContractAddress(); const txHash = await zeroEx.token.setUnlimitedProxyAllowanceAsync(zrxTokenAddress, coinbase); const txReceiptWithDecodedLogs = await zeroEx.awaitTransactionMinedAsync(txHash); const log = txReceiptWithDecodedLogs.logs[0] as LogWithDecodedArgs; @@ -248,8 +248,7 @@ describe('ZeroEx library', () => { networkId: constants.TESTRPC_NETWORK_ID, }; const zeroExWithWrongEtherTokenAddress = new ZeroEx(web3.currentProvider, zeroExConfig); - return expect(zeroExWithWrongEtherTokenAddress.etherToken.getContractAddressAsync()) - .to.be.rejectedWith(ZeroExError.ContractDoesNotExist); + expect(zeroExWithWrongEtherTokenAddress.etherToken.getContractAddress()).to.be.equal(ZeroEx.NULL_ADDRESS); }); it('allows to specify token registry token contract address', async () => { const zeroExConfig = { @@ -257,8 +256,8 @@ describe('ZeroEx library', () => { networkId: constants.TESTRPC_NETWORK_ID, }; const zeroExWithWrongTokenRegistryAddress = new ZeroEx(web3.currentProvider, zeroExConfig); - return expect(zeroExWithWrongTokenRegistryAddress.tokenRegistry.getContractAddressAsync()) - .to.be.rejectedWith(ZeroExError.ContractDoesNotExist); + expect(zeroExWithWrongTokenRegistryAddress.tokenRegistry.getContractAddress()) + .to.be.equal(ZeroEx.NULL_ADDRESS); }); }); }); diff --git a/packages/0x.js/test/ether_token_wrapper_test.ts b/packages/0x.js/test/ether_token_wrapper_test.ts index d2408938c..b86e79054 100644 --- a/packages/0x.js/test/ether_token_wrapper_test.ts +++ b/packages/0x.js/test/ether_token_wrapper_test.ts @@ -36,7 +36,7 @@ describe('EtherTokenWrapper', () => { zeroEx = new ZeroEx(web3.currentProvider, zeroExConfig); userAddresses = await zeroEx.getAvailableAddressesAsync(); addressWithETH = userAddresses[0]; - wethContractAddress = await zeroEx.etherToken.getContractAddressAsync(); + wethContractAddress = zeroEx.etherToken.getContractAddress(); depositWeiAmount = (zeroEx as any)._web3Wrapper.toWei(new BigNumber(5)); decimalPlaces = 7; }); diff --git a/packages/0x.js/test/token_wrapper_test.ts b/packages/0x.js/test/token_wrapper_test.ts index 170dae6a6..b07fed045 100644 --- a/packages/0x.js/test/token_wrapper_test.ts +++ b/packages/0x.js/test/token_wrapper_test.ts @@ -436,10 +436,10 @@ describe('TokenWrapper', () => { toBlock: BlockParamLiteral.Latest, }; let txHash: string; - before(async () => { + before(() => { const token = tokens[0]; tokenAddress = token.address; - tokenTransferProxyAddress = await zeroEx.proxy.getContractAddressAsync(); + tokenTransferProxyAddress = zeroEx.proxy.getContractAddress(); }); it('should get logs with decoded args emitted by Approval', async () => { txHash = await zeroEx.token.setUnlimitedProxyAllowanceAsync(tokenAddress, coinbase); diff --git a/packages/tslint-config/tslint.json b/packages/tslint-config/tslint.json index 8b839f25a..a53f1b10e 100644 --- a/packages/tslint-config/tslint.json +++ b/packages/tslint-config/tslint.json @@ -5,6 +5,7 @@ ], "rules": { "arrow-parens": [true, "ban-single-arg-parens"], + "await-promise": true, "ordered-imports": false, "quotemark": [true, "single", "avoid-escape", "jsx-double"], "callable-types": true, @@ -21,10 +22,12 @@ "no-angle-bracket-type-assertion": true, "no-default-export": true, "no-empty-interface": false, + "no-floating-promises": true, "no-string-throw": true, "no-submodule-imports": false, "no-implicit-dependencies": [true, "dev"], "prefer-const": true, + "promise-function-async": true, "variable-name": [true, "ban-keywords", "allow-pascal-case" -- cgit v1.2.3