From 448df1bb9c4011a385d5afa22f678786ba9d2c47 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Fri, 1 Jun 2018 16:09:49 -0700 Subject: Make erc20_wrapper and erc721_wrapper serial --- packages/contracts/src/utils/erc20_wrapper.ts | 45 +++++++++++----------- packages/contracts/src/utils/erc721_wrapper.ts | 53 +++++++++++++------------- 2 files changed, 50 insertions(+), 48 deletions(-) (limited to 'packages/contracts/src/utils') diff --git a/packages/contracts/src/utils/erc20_wrapper.ts b/packages/contracts/src/utils/erc20_wrapper.ts index 0f45fb1e6..f58fa94fe 100644 --- a/packages/contracts/src/utils/erc20_wrapper.ts +++ b/packages/contracts/src/utils/erc20_wrapper.ts @@ -16,18 +16,19 @@ export class ERC20Wrapper { private _contractOwnerAddress: string; private _web3Wrapper: Web3Wrapper; private _provider: Provider; - private _dummyTokenContracts?: DummyERC20TokenContract[]; + private _dummyTokenContracts: DummyERC20TokenContract[]; private _proxyContract?: ERC20ProxyContract; constructor(provider: Provider, tokenOwnerAddresses: string[], contractOwnerAddress: string) { + this._dummyTokenContracts = []; this._web3Wrapper = new Web3Wrapper(provider); this._provider = provider; this._tokenOwnerAddresses = tokenOwnerAddresses; this._contractOwnerAddress = contractOwnerAddress; } public async deployDummyTokensAsync(): Promise { - this._dummyTokenContracts = await Promise.all( - _.times(constants.NUM_DUMMY_ERC20_TO_DEPLOY, async () => - DummyERC20TokenContract.deployFrom0xArtifactAsync( + for (let i = 0; i < constants.NUM_DUMMY_ERC20_TO_DEPLOY; i++) { + this._dummyTokenContracts.push( + await DummyERC20TokenContract.deployFrom0xArtifactAsync( artifacts.DummyERC20Token, this._provider, txDefaults, @@ -36,8 +37,8 @@ export class ERC20Wrapper { constants.DUMMY_TOKEN_DECIMALS, constants.DUMMY_TOKEN_TOTAL_SUPPLY, ), - ), - ); + ); + } return this._dummyTokenContracts; } public async deployProxyAsync(): Promise { @@ -53,42 +54,42 @@ export class ERC20Wrapper { this._validateProxyContractExistsOrThrow(); const setBalancePromises: Array> = []; const setAllowancePromises: Array> = []; - _.forEach(this._dummyTokenContracts, dummyTokenContract => { - _.forEach(this._tokenOwnerAddresses, tokenOwnerAddress => { - setBalancePromises.push( - dummyTokenContract.setBalance.sendTransactionAsync( + for (const dummyTokenContract of this._dummyTokenContracts) { + for (const tokenOwnerAddress of this._tokenOwnerAddresses) { + await this._web3Wrapper.awaitTransactionSuccessAsync( + await dummyTokenContract.setBalance.sendTransactionAsync( tokenOwnerAddress, constants.INITIAL_ERC20_BALANCE, { from: this._contractOwnerAddress }, ), + constants.AWAIT_TRANSACTION_MINED_MS, ); - setAllowancePromises.push( - dummyTokenContract.approve.sendTransactionAsync( + await this._web3Wrapper.awaitTransactionSuccessAsync( + await dummyTokenContract.approve.sendTransactionAsync( (this._proxyContract as ERC20ProxyContract).address, constants.INITIAL_ERC20_ALLOWANCE, { from: tokenOwnerAddress }, ), + constants.AWAIT_TRANSACTION_MINED_MS, ); - }); - }); - const txHashes = await Promise.all([...setBalancePromises, ...setAllowancePromises]); - await Promise.all(_.map(txHashes, async txHash => this._web3Wrapper.awaitTransactionSuccessAsync(txHash))); + } + } } public async getBalancesAsync(): Promise { this._validateDummyTokenContractsExistOrThrow(); const balancesByOwner: ERC20BalancesByOwner = {}; + const balances: BigNumber[] = []; const balancePromises: Array> = []; const balanceInfo: Array<{ tokenOwnerAddress: string; tokenAddress: string }> = []; - _.forEach(this._dummyTokenContracts, dummyTokenContract => { - _.forEach(this._tokenOwnerAddresses, tokenOwnerAddress => { - balancePromises.push(dummyTokenContract.balanceOf.callAsync(tokenOwnerAddress)); + for (const dummyTokenContract of this._dummyTokenContracts) { + for (const tokenOwnerAddress of this._tokenOwnerAddresses) { + balances.push(await dummyTokenContract.balanceOf.callAsync(tokenOwnerAddress)); balanceInfo.push({ tokenOwnerAddress, tokenAddress: dummyTokenContract.address, }); - }); - }); - const balances = await Promise.all(balancePromises); + } + } _.forEach(balances, (balance, balanceIndex) => { const tokenAddress = balanceInfo[balanceIndex].tokenAddress; const tokenOwnerAddress = balanceInfo[balanceIndex].tokenOwnerAddress; diff --git a/packages/contracts/src/utils/erc721_wrapper.ts b/packages/contracts/src/utils/erc721_wrapper.ts index 11a012602..d5c278de1 100644 --- a/packages/contracts/src/utils/erc721_wrapper.ts +++ b/packages/contracts/src/utils/erc721_wrapper.ts @@ -17,27 +17,28 @@ export class ERC721Wrapper { private _contractOwnerAddress: string; private _web3Wrapper: Web3Wrapper; private _provider: Provider; - private _dummyTokenContracts?: DummyERC721TokenContract[]; + private _dummyTokenContracts: DummyERC721TokenContract[]; private _proxyContract?: ERC721ProxyContract; private _initialTokenIdsByOwner: ERC721TokenIdsByOwner = {}; constructor(provider: Provider, tokenOwnerAddresses: string[], contractOwnerAddress: string) { this._web3Wrapper = new Web3Wrapper(provider); this._provider = provider; + this._dummyTokenContracts = []; this._tokenOwnerAddresses = tokenOwnerAddresses; this._contractOwnerAddress = contractOwnerAddress; } public async deployDummyTokensAsync(): Promise { - this._dummyTokenContracts = await Promise.all( - _.times(constants.NUM_DUMMY_ERC721_TO_DEPLOY, async () => - DummyERC721TokenContract.deployFrom0xArtifactAsync( + for (let i = 0; i < constants.NUM_DUMMY_ERC721_TO_DEPLOY; i++) { + this._dummyTokenContracts.push( + await DummyERC721TokenContract.deployFrom0xArtifactAsync( artifacts.DummyERC721Token, this._provider, txDefaults, constants.DUMMY_TOKEN_NAME, constants.DUMMY_TOKEN_SYMBOL, ), - ), - ); + ); + } return this._dummyTokenContracts; } public async deployProxyAsync(): Promise { @@ -54,14 +55,15 @@ export class ERC721Wrapper { const setBalancePromises: Array> = []; const setAllowancePromises: Array> = []; this._initialTokenIdsByOwner = {}; - _.forEach(this._dummyTokenContracts, dummyTokenContract => { - _.forEach(this._tokenOwnerAddresses, tokenOwnerAddress => { - _.forEach(_.range(constants.NUM_ERC721_TOKENS_TO_MINT), () => { + for (const dummyTokenContract of this._dummyTokenContracts) { + for (const tokenOwnerAddress of this._tokenOwnerAddresses) { + for (let i = 0; i < constants.NUM_ERC721_TOKENS_TO_MINT; i++) { const tokenId = generatePseudoRandomSalt(); - setBalancePromises.push( - dummyTokenContract.mint.sendTransactionAsync(tokenOwnerAddress, tokenId, { + await this._web3Wrapper.awaitTransactionSuccessAsync( + await dummyTokenContract.mint.sendTransactionAsync(tokenOwnerAddress, tokenId, { from: this._contractOwnerAddress, }), + constants.AWAIT_TRANSACTION_MINED_MS, ); if (_.isUndefined(this._initialTokenIdsByOwner[tokenOwnerAddress])) { this._initialTokenIdsByOwner[tokenOwnerAddress] = { @@ -72,41 +74,40 @@ export class ERC721Wrapper { this._initialTokenIdsByOwner[tokenOwnerAddress][dummyTokenContract.address] = []; } this._initialTokenIdsByOwner[tokenOwnerAddress][dummyTokenContract.address].push(tokenId); - }); + } const shouldApprove = true; - setAllowancePromises.push( - dummyTokenContract.setApprovalForAll.sendTransactionAsync( + await this._web3Wrapper.awaitTransactionSuccessAsync( + await dummyTokenContract.setApprovalForAll.sendTransactionAsync( (this._proxyContract as ERC721ProxyContract).address, shouldApprove, { from: tokenOwnerAddress }, ), + constants.AWAIT_TRANSACTION_MINED_MS, ); - }); - }); - const txHashes = await Promise.all([...setBalancePromises, ...setAllowancePromises]); - await Promise.all(_.map(txHashes, async txHash => this._web3Wrapper.awaitTransactionSuccessAsync(txHash))); + } + } } public async getBalancesAsync(): Promise { this._validateDummyTokenContractsExistOrThrow(); this._validateBalancesAndAllowancesSetOrThrow(); const tokenIdsByOwner: ERC721TokenIdsByOwner = {}; + const tokenOwnerAddresses: string[] = []; const tokenOwnerPromises: Array> = []; const tokenInfo: Array<{ tokenId: BigNumber; tokenAddress: string }> = []; - _.forEach(this._dummyTokenContracts, dummyTokenContract => { - _.forEach(this._tokenOwnerAddresses, tokenOwnerAddress => { + for (const dummyTokenContract of this._dummyTokenContracts) { + for (const tokenOwnerAddress of this._tokenOwnerAddresses) { const initialTokenOwnerIds = this._initialTokenIdsByOwner[tokenOwnerAddress][ dummyTokenContract.address ]; - _.forEach(initialTokenOwnerIds, tokenId => { - tokenOwnerPromises.push(dummyTokenContract.ownerOf.callAsync(tokenId)); + for (const tokenId of initialTokenOwnerIds) { + tokenOwnerAddresses.push(await dummyTokenContract.ownerOf.callAsync(tokenId)); tokenInfo.push({ tokenId, tokenAddress: dummyTokenContract.address, }); - }); - }); - }); - const tokenOwnerAddresses = await Promise.all(tokenOwnerPromises); + } + } + } _.forEach(tokenOwnerAddresses, (tokenOwnerAddress, ownerIndex) => { const tokenAddress = tokenInfo[ownerIndex].tokenAddress; const tokenId = tokenInfo[ownerIndex].tokenId; -- cgit From e4a8b175225108c66778372166e52b7d5a18333f Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Fri, 1 Jun 2018 16:17:27 -0700 Subject: Remove unused promises array --- packages/contracts/src/utils/erc20_wrapper.ts | 3 --- packages/contracts/src/utils/erc721_wrapper.ts | 3 --- 2 files changed, 6 deletions(-) (limited to 'packages/contracts/src/utils') diff --git a/packages/contracts/src/utils/erc20_wrapper.ts b/packages/contracts/src/utils/erc20_wrapper.ts index f58fa94fe..dceeceeea 100644 --- a/packages/contracts/src/utils/erc20_wrapper.ts +++ b/packages/contracts/src/utils/erc20_wrapper.ts @@ -52,8 +52,6 @@ export class ERC20Wrapper { public async setBalancesAndAllowancesAsync(): Promise { this._validateDummyTokenContractsExistOrThrow(); this._validateProxyContractExistsOrThrow(); - const setBalancePromises: Array> = []; - const setAllowancePromises: Array> = []; for (const dummyTokenContract of this._dummyTokenContracts) { for (const tokenOwnerAddress of this._tokenOwnerAddresses) { await this._web3Wrapper.awaitTransactionSuccessAsync( @@ -79,7 +77,6 @@ export class ERC20Wrapper { this._validateDummyTokenContractsExistOrThrow(); const balancesByOwner: ERC20BalancesByOwner = {}; const balances: BigNumber[] = []; - const balancePromises: Array> = []; const balanceInfo: Array<{ tokenOwnerAddress: string; tokenAddress: string }> = []; for (const dummyTokenContract of this._dummyTokenContracts) { for (const tokenOwnerAddress of this._tokenOwnerAddresses) { diff --git a/packages/contracts/src/utils/erc721_wrapper.ts b/packages/contracts/src/utils/erc721_wrapper.ts index d5c278de1..13fdf630e 100644 --- a/packages/contracts/src/utils/erc721_wrapper.ts +++ b/packages/contracts/src/utils/erc721_wrapper.ts @@ -52,8 +52,6 @@ export class ERC721Wrapper { public async setBalancesAndAllowancesAsync(): Promise { this._validateDummyTokenContractsExistOrThrow(); this._validateProxyContractExistsOrThrow(); - const setBalancePromises: Array> = []; - const setAllowancePromises: Array> = []; this._initialTokenIdsByOwner = {}; for (const dummyTokenContract of this._dummyTokenContracts) { for (const tokenOwnerAddress of this._tokenOwnerAddresses) { @@ -92,7 +90,6 @@ export class ERC721Wrapper { this._validateBalancesAndAllowancesSetOrThrow(); const tokenIdsByOwner: ERC721TokenIdsByOwner = {}; const tokenOwnerAddresses: string[] = []; - const tokenOwnerPromises: Array> = []; const tokenInfo: Array<{ tokenId: BigNumber; tokenAddress: string }> = []; for (const dummyTokenContract of this._dummyTokenContracts) { for (const tokenOwnerAddress of this._tokenOwnerAddresses) { -- cgit From fb3860757ca882e2b01a71673217a5f543087066 Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Fri, 1 Jun 2018 16:40:38 -0700 Subject: Set contract expiration time to a constant 10 minutes --- packages/contracts/src/utils/order_factory.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'packages/contracts/src/utils') diff --git a/packages/contracts/src/utils/order_factory.ts b/packages/contracts/src/utils/order_factory.ts index f704c26ec..e60f25605 100644 --- a/packages/contracts/src/utils/order_factory.ts +++ b/packages/contracts/src/utils/order_factory.ts @@ -19,7 +19,8 @@ export class OrderFactory { customOrderParams: Partial = {}, signatureType: SignatureType = SignatureType.Ecrecover, ): SignedOrder { - const randomExpiration = new BigNumber(Math.floor((Date.now() + Math.random() * 100000000000) / 1000)); + const tenMinutes = 10 * 60 * 1000; + const randomExpiration = new BigNumber(Date.now() + tenMinutes); const order = ({ senderAddress: constants.NULL_ADDRESS, expirationTimeSeconds: randomExpiration, -- cgit