Remove even more asyncs

This commit is contained in:
Leonid Logvinov
2017-11-22 13:37:07 -06:00
parent c586d3e81d
commit a38ef3655b
8 changed files with 44 additions and 29 deletions

View File

@@ -53,7 +53,7 @@ export class EtherTokenWrapper extends ContractWrapper {
assert.isValidBaseUnitAmount('amountInWei', amountInWei); assert.isValidBaseUnitAmount('amountInWei', amountInWei);
await assert.isSenderAddressAsync('withdrawer', withdrawer, this._web3Wrapper); await assert.isSenderAddressAsync('withdrawer', withdrawer, this._web3Wrapper);
const wethContractAddress = await this.getContractAddressAsync(); const wethContractAddress = this.getContractAddress();
const WETHBalanceInBaseUnits = await this._tokenWrapper.getBalanceAsync(wethContractAddress, withdrawer); const WETHBalanceInBaseUnits = await this._tokenWrapper.getBalanceAsync(wethContractAddress, withdrawer);
assert.assert(WETHBalanceInBaseUnits.gte(amountInWei), ZeroExError.InsufficientWEthBalanceForWithdrawal); assert.assert(WETHBalanceInBaseUnits.gte(amountInWei), ZeroExError.InsufficientWEthBalanceForWithdrawal);
@@ -67,9 +67,17 @@ export class EtherTokenWrapper extends ContractWrapper {
* Retrieves the Wrapped Ether token contract address * Retrieves the Wrapped Ether token contract address
* @return The Wrapped Ether token contract address * @return The Wrapped Ether token contract address
*/ */
public async getContractAddressAsync(): Promise<string> { public getContractAddress(): string {
const wethContract = await this._getEtherTokenContractAsync(); const networkId = this._web3Wrapper.getNetworkId();
return wethContract.address; 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 { private _invalidateContractInstance(): void {
delete this._etherTokenContractIfExists; delete this._etherTokenContractIfExists;

View File

@@ -557,7 +557,7 @@ export class ExchangeWrapper extends ContractWrapper {
if (shouldValidate) { if (shouldValidate) {
const orderHash = utils.getOrderHashHex(order); const orderHash = utils.getOrderHashHex(order);
const unavailableTakerTokenAmount = await this.getUnavailableTakerAmountAsync(orderHash); const unavailableTakerTokenAmount = await this.getUnavailableTakerAmountAsync(orderHash);
await this._orderValidationUtils.validateCancelOrderThrowIfInvalidAsync( this._orderValidationUtils.validateCancelOrderThrowIfInvalid(
order, cancelTakerTokenAmount, unavailableTakerTokenAmount); order, cancelTakerTokenAmount, unavailableTakerTokenAmount);
} }
@@ -611,7 +611,7 @@ export class ExchangeWrapper extends ContractWrapper {
for (const orderCancellationRequest of orderCancellationRequests) { for (const orderCancellationRequest of orderCancellationRequests) {
const orderHash = utils.getOrderHashHex(orderCancellationRequest.order); const orderHash = utils.getOrderHashHex(orderCancellationRequest.order);
const unavailableTakerTokenAmount = await this.getUnavailableTakerAmountAsync(orderHash); const unavailableTakerTokenAmount = await this.getUnavailableTakerAmountAsync(orderHash);
await this._orderValidationUtils.validateCancelOrderThrowIfInvalidAsync( this._orderValidationUtils.validateCancelOrderThrowIfInvalid(
orderCancellationRequest.order, orderCancellationRequest.takerTokenCancelAmount, orderCancellationRequest.order, orderCancellationRequest.takerTokenCancelAmount,
unavailableTakerTokenAmount, unavailableTakerTokenAmount,
); );
@@ -765,7 +765,7 @@ export class ExchangeWrapper extends ContractWrapper {
assert.isValidBaseUnitAmount('cancelTakerTokenAmount', cancelTakerTokenAmount); assert.isValidBaseUnitAmount('cancelTakerTokenAmount', cancelTakerTokenAmount);
const orderHash = utils.getOrderHashHex(order); const orderHash = utils.getOrderHashHex(order);
const unavailableTakerTokenAmount = await this.getUnavailableTakerAmountAsync(orderHash); const unavailableTakerTokenAmount = await this.getUnavailableTakerAmountAsync(orderHash);
await this._orderValidationUtils.validateCancelOrderThrowIfInvalidAsync( this._orderValidationUtils.validateCancelOrderThrowIfInvalid(
order, cancelTakerTokenAmount, unavailableTakerTokenAmount); order, cancelTakerTokenAmount, unavailableTakerTokenAmount);
} }
/** /**

View File

@@ -2,17 +2,17 @@ import * as _ from 'lodash';
import {Web3Wrapper} from '../web3_wrapper'; import {Web3Wrapper} from '../web3_wrapper';
import {ContractWrapper} from './contract_wrapper'; import {ContractWrapper} from './contract_wrapper';
import {artifacts} from '../artifacts'; 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. * This class includes the functionality related to interacting with the TokenTransferProxy contract.
*/ */
export class TokenTransferProxyWrapper extends ContractWrapper { export class TokenTransferProxyWrapper extends ContractWrapper {
private _tokenTransferProxyContractIfExists?: TokenTransferProxyContract; private _tokenTransferProxyContractIfExists?: TokenTransferProxyContract;
private _tokenTransferProxyContractAddressFetcher: () => Promise<string>; private _contractAddressIfExists?: string;
constructor(web3Wrapper: Web3Wrapper, tokenTransferProxyContractAddressFetcher: () => Promise<string>) { constructor(web3Wrapper: Web3Wrapper, contractAddressIfExists?: string) {
super(web3Wrapper); super(web3Wrapper);
this._tokenTransferProxyContractAddressFetcher = tokenTransferProxyContractAddressFetcher; this._contractAddressIfExists = contractAddressIfExists;
} }
/** /**
* Check if the Exchange contract address is authorized by the TokenTransferProxy contract. * 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. * that the user-passed web3 provider is connected to.
* @returns The Ethereum address of the TokenTransferProxy contract being used. * @returns The Ethereum address of the TokenTransferProxy contract being used.
*/ */
public async getContractAddressAsync(): Promise<string> { public getContractAddress(): string {
const proxyInstance = await this._getTokenTransferProxyContractAsync(); const networkId = this._web3Wrapper.getNetworkId();
const proxyAddress = proxyInstance.address; if (_.isUndefined(this._contractAddressIfExists)) {
return proxyAddress; const contractAddress = artifacts.TokenTransferProxyArtifact.networks[networkId].address;
if (_.isUndefined(contractAddress)) {
throw new Error(ZeroExError.ExchangeContractDoesNotExist);
}
return contractAddress;
} else {
return this._contractAddressIfExists;
}
} }
private _invalidateContractInstance(): void { private _invalidateContractInstance(): void {
delete this._tokenTransferProxyContractIfExists; delete this._tokenTransferProxyContractIfExists;
@@ -50,9 +57,8 @@ export class TokenTransferProxyWrapper extends ContractWrapper {
if (!_.isUndefined(this._tokenTransferProxyContractIfExists)) { if (!_.isUndefined(this._tokenTransferProxyContractIfExists)) {
return this._tokenTransferProxyContractIfExists; return this._tokenTransferProxyContractIfExists;
} }
const contractAddress = await this._tokenTransferProxyContractAddressFetcher();
const contractInstance = await this._instantiateContractIfExistsAsync<TokenTransferProxyContract>( const contractInstance = await this._instantiateContractIfExistsAsync<TokenTransferProxyContract>(
artifacts.TokenTransferProxyArtifact, contractAddress, artifacts.TokenTransferProxyArtifact, this._contractAddressIfExists,
); );
this._tokenTransferProxyContractIfExists = contractInstance as TokenTransferProxyContract; this._tokenTransferProxyContractIfExists = contractInstance as TokenTransferProxyContract;
return this._tokenTransferProxyContractIfExists; return this._tokenTransferProxyContractIfExists;

View File

@@ -92,10 +92,9 @@ export class OrderValidationUtils {
throw new Error(ExchangeContractErrs.InsufficientRemainingFillAmount); throw new Error(ExchangeContractErrs.InsufficientRemainingFillAmount);
} }
} }
public async validateCancelOrderThrowIfInvalidAsync(order: Order, public validateCancelOrderThrowIfInvalid(
cancelTakerTokenAmount: BigNumber, order: Order, cancelTakerTokenAmount: BigNumber, unavailableTakerTokenAmount: BigNumber,
unavailableTakerTokenAmount: BigNumber, ): void {
): Promise<void> {
if (cancelTakerTokenAmount.eq(0)) { if (cancelTakerTokenAmount.eq(0)) {
throw new Error(ExchangeContractErrs.OrderCancelAmountZero); throw new Error(ExchangeContractErrs.OrderCancelAmountZero);
} }

View File

@@ -223,7 +223,7 @@ describe('ZeroEx library', () => {
const tokens = await zeroEx.tokenRegistry.getTokensAsync(); const tokens = await zeroEx.tokenRegistry.getTokensAsync();
const tokenUtils = new TokenUtils(tokens); const tokenUtils = new TokenUtils(tokens);
const zrxTokenAddress = tokenUtils.getProtocolTokenOrThrow().address; 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 txHash = await zeroEx.token.setUnlimitedProxyAllowanceAsync(zrxTokenAddress, coinbase);
const txReceiptWithDecodedLogs = await zeroEx.awaitTransactionMinedAsync(txHash); const txReceiptWithDecodedLogs = await zeroEx.awaitTransactionMinedAsync(txHash);
const log = txReceiptWithDecodedLogs.logs[0] as LogWithDecodedArgs<ApprovalContractEventArgs>; const log = txReceiptWithDecodedLogs.logs[0] as LogWithDecodedArgs<ApprovalContractEventArgs>;
@@ -248,8 +248,7 @@ describe('ZeroEx library', () => {
networkId: constants.TESTRPC_NETWORK_ID, networkId: constants.TESTRPC_NETWORK_ID,
}; };
const zeroExWithWrongEtherTokenAddress = new ZeroEx(web3.currentProvider, zeroExConfig); const zeroExWithWrongEtherTokenAddress = new ZeroEx(web3.currentProvider, zeroExConfig);
return expect(zeroExWithWrongEtherTokenAddress.etherToken.getContractAddressAsync()) expect(zeroExWithWrongEtherTokenAddress.etherToken.getContractAddress()).to.be.equal(ZeroEx.NULL_ADDRESS);
.to.be.rejectedWith(ZeroExError.ContractDoesNotExist);
}); });
it('allows to specify token registry token contract address', async () => { it('allows to specify token registry token contract address', async () => {
const zeroExConfig = { const zeroExConfig = {
@@ -257,8 +256,8 @@ describe('ZeroEx library', () => {
networkId: constants.TESTRPC_NETWORK_ID, networkId: constants.TESTRPC_NETWORK_ID,
}; };
const zeroExWithWrongTokenRegistryAddress = new ZeroEx(web3.currentProvider, zeroExConfig); const zeroExWithWrongTokenRegistryAddress = new ZeroEx(web3.currentProvider, zeroExConfig);
return expect(zeroExWithWrongTokenRegistryAddress.tokenRegistry.getContractAddressAsync()) expect(zeroExWithWrongTokenRegistryAddress.tokenRegistry.getContractAddress())
.to.be.rejectedWith(ZeroExError.ContractDoesNotExist); .to.be.equal(ZeroEx.NULL_ADDRESS);
}); });
}); });
}); });

View File

@@ -36,7 +36,7 @@ describe('EtherTokenWrapper', () => {
zeroEx = new ZeroEx(web3.currentProvider, zeroExConfig); zeroEx = new ZeroEx(web3.currentProvider, zeroExConfig);
userAddresses = await zeroEx.getAvailableAddressesAsync(); userAddresses = await zeroEx.getAvailableAddressesAsync();
addressWithETH = userAddresses[0]; addressWithETH = userAddresses[0];
wethContractAddress = await zeroEx.etherToken.getContractAddressAsync(); wethContractAddress = zeroEx.etherToken.getContractAddress();
depositWeiAmount = (zeroEx as any)._web3Wrapper.toWei(new BigNumber(5)); depositWeiAmount = (zeroEx as any)._web3Wrapper.toWei(new BigNumber(5));
decimalPlaces = 7; decimalPlaces = 7;
}); });

View File

@@ -436,10 +436,10 @@ describe('TokenWrapper', () => {
toBlock: BlockParamLiteral.Latest, toBlock: BlockParamLiteral.Latest,
}; };
let txHash: string; let txHash: string;
before(async () => { before(() => {
const token = tokens[0]; const token = tokens[0];
tokenAddress = token.address; tokenAddress = token.address;
tokenTransferProxyAddress = await zeroEx.proxy.getContractAddressAsync(); tokenTransferProxyAddress = zeroEx.proxy.getContractAddress();
}); });
it('should get logs with decoded args emitted by Approval', async () => { it('should get logs with decoded args emitted by Approval', async () => {
txHash = await zeroEx.token.setUnlimitedProxyAllowanceAsync(tokenAddress, coinbase); txHash = await zeroEx.token.setUnlimitedProxyAllowanceAsync(tokenAddress, coinbase);

View File

@@ -5,6 +5,7 @@
], ],
"rules": { "rules": {
"arrow-parens": [true, "ban-single-arg-parens"], "arrow-parens": [true, "ban-single-arg-parens"],
"await-promise": true,
"ordered-imports": false, "ordered-imports": false,
"quotemark": [true, "single", "avoid-escape", "jsx-double"], "quotemark": [true, "single", "avoid-escape", "jsx-double"],
"callable-types": true, "callable-types": true,
@@ -21,10 +22,12 @@
"no-angle-bracket-type-assertion": true, "no-angle-bracket-type-assertion": true,
"no-default-export": true, "no-default-export": true,
"no-empty-interface": false, "no-empty-interface": false,
"no-floating-promises": true,
"no-string-throw": true, "no-string-throw": true,
"no-submodule-imports": false, "no-submodule-imports": false,
"no-implicit-dependencies": [true, "dev"], "no-implicit-dependencies": [true, "dev"],
"prefer-const": true, "prefer-const": true,
"promise-function-async": true,
"variable-name": [true, "variable-name": [true,
"ban-keywords", "ban-keywords",
"allow-pascal-case" "allow-pascal-case"