aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Browne <stephenalexbrowne@gmail.com>2018-06-02 07:09:49 +0800
committerAlex Browne <stephenalexbrowne@gmail.com>2018-06-02 07:09:49 +0800
commit448df1bb9c4011a385d5afa22f678786ba9d2c47 (patch)
treef8d8056e20be560beeade1fed0b351c28babf36f
parentbf6900fb2a991f2c252bc7f8594364925a6046e1 (diff)
downloaddexon-sol-tools-448df1bb9c4011a385d5afa22f678786ba9d2c47.tar.gz
dexon-sol-tools-448df1bb9c4011a385d5afa22f678786ba9d2c47.tar.zst
dexon-sol-tools-448df1bb9c4011a385d5afa22f678786ba9d2c47.zip
Make erc20_wrapper and erc721_wrapper serial
-rw-r--r--packages/contracts/src/utils/erc20_wrapper.ts45
-rw-r--r--packages/contracts/src/utils/erc721_wrapper.ts53
2 files changed, 50 insertions, 48 deletions
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<DummyERC20TokenContract[]> {
- 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<ERC20ProxyContract> {
@@ -53,42 +54,42 @@ export class ERC20Wrapper {
this._validateProxyContractExistsOrThrow();
const setBalancePromises: Array<Promise<string>> = [];
const setAllowancePromises: Array<Promise<string>> = [];
- _.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<ERC20BalancesByOwner> {
this._validateDummyTokenContractsExistOrThrow();
const balancesByOwner: ERC20BalancesByOwner = {};
+ const balances: BigNumber[] = [];
const balancePromises: Array<Promise<BigNumber>> = [];
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<DummyERC721TokenContract[]> {
- 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<ERC721ProxyContract> {
@@ -54,14 +55,15 @@ export class ERC721Wrapper {
const setBalancePromises: Array<Promise<string>> = [];
const setAllowancePromises: Array<Promise<string>> = [];
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<ERC721TokenIdsByOwner> {
this._validateDummyTokenContractsExistOrThrow();
this._validateBalancesAndAllowancesSetOrThrow();
const tokenIdsByOwner: ERC721TokenIdsByOwner = {};
+ const tokenOwnerAddresses: string[] = [];
const tokenOwnerPromises: Array<Promise<string>> = [];
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;