From 1f968fa4fdb260f647c06a4cf690792ff7bc45fd Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Sat, 21 Apr 2018 21:42:36 -0700 Subject: Add ERC20Wrapper --- packages/contracts/src/utils/balances.ts | 30 ------- packages/contracts/src/utils/constants.ts | 15 +++- packages/contracts/src/utils/erc20_wrapper.ts | 114 ++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 32 deletions(-) delete mode 100644 packages/contracts/src/utils/balances.ts create mode 100644 packages/contracts/src/utils/erc20_wrapper.ts (limited to 'packages/contracts/src') diff --git a/packages/contracts/src/utils/balances.ts b/packages/contracts/src/utils/balances.ts deleted file mode 100644 index 40a59e815..000000000 --- a/packages/contracts/src/utils/balances.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { BigNumber } from '@0xproject/utils'; -import * as _ from 'lodash'; -import * as Web3 from 'web3'; - -import { DummyERC20TokenContract } from '../contract_wrappers/generated/dummy_e_r_c20_token'; - -import { BalancesByOwner } from './types'; - -export class Balances { - private _tokenContractInstances: DummyERC20TokenContract[]; - private _ownerAddresses: string[]; - constructor(tokenContractInstances: DummyERC20TokenContract[], ownerAddresses: string[]) { - this._tokenContractInstances = tokenContractInstances; - this._ownerAddresses = ownerAddresses; - } - public async getAsync(): Promise { - const balancesByOwner: BalancesByOwner = {}; - for (const tokenContractInstance of this._tokenContractInstances) { - for (const ownerAddress of this._ownerAddresses) { - let balance = await tokenContractInstance.balanceOf.callAsync(ownerAddress); - balance = new BigNumber(balance); - if (_.isUndefined(balancesByOwner[ownerAddress])) { - balancesByOwner[ownerAddress] = {}; - } - balancesByOwner[ownerAddress][tokenContractInstance.address] = balance; - } - } - return balancesByOwner; - } -} diff --git a/packages/contracts/src/utils/constants.ts b/packages/contracts/src/utils/constants.ts index 49872fc59..9662a5c27 100644 --- a/packages/contracts/src/utils/constants.ts +++ b/packages/contracts/src/utils/constants.ts @@ -1,3 +1,5 @@ +import { ZeroEx } from '0x.js'; +import { BigNumber } from '@0xproject/utils'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; @@ -25,7 +27,16 @@ export const constants = { MAX_ETHERTOKEN_WITHDRAW_GAS: 43000, MAX_TOKEN_TRANSFERFROM_GAS: 80000, MAX_TOKEN_APPROVE_GAS: 60000, - DUMMY_TOKEN_ARGS: [DUMMY_TOKEN_NAME, DUMMY_TOKEN_SYMBOL, DUMMY_TOKEN_DECIMALS, DUMMY_TOKEN_TOTAL_SUPPLY], - DUMMY_ERC721TOKEN_ARGS: [DUMMY_TOKEN_NAME, DUMMY_TOKEN_SYMBOL], + DUMMY_ERC20_TOKEN_ARGS: [DUMMY_TOKEN_NAME, DUMMY_TOKEN_SYMBOL, DUMMY_TOKEN_DECIMALS, DUMMY_TOKEN_TOTAL_SUPPLY], + DUMMY_ERC721_TOKEN_ARGS: [DUMMY_TOKEN_NAME, DUMMY_TOKEN_SYMBOL], + NUM_ERC721_TOKENS_TO_MINT: 2, TESTRPC_PRIVATE_KEYS: _.map(TESTRPC_PRIVATE_KEYS_STRINGS, privateKeyString => ethUtil.toBuffer(privateKeyString)), + INITIAL_ERC20_BALANCE: ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18), + INITIAL_ERC20_ALLOWANCE: ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18), + STATIC_ORDER_PARAMS: { + makerAssetAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), + takerAssetAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), + makerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), + takerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), + }, }; diff --git a/packages/contracts/src/utils/erc20_wrapper.ts b/packages/contracts/src/utils/erc20_wrapper.ts new file mode 100644 index 000000000..950b20c99 --- /dev/null +++ b/packages/contracts/src/utils/erc20_wrapper.ts @@ -0,0 +1,114 @@ +import { Deployer } from '@0xproject/deployer'; +import { Provider } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; +import * as _ from 'lodash'; + +import { DummyERC20TokenContract } from '../contract_wrappers/generated/dummy_e_r_c20_token'; +import { ERC20ProxyContract } from '../contract_wrappers/generated/e_r_c20_proxy'; + +import { constants } from './constants'; +import { ContractName, ERC20BalancesByOwner } from './types'; + +export class ERC20Wrapper { + private _tokenOwnerAddresses: string[]; + private _contractOwnerAddress: string; + private _deployer: Deployer; + private _provider: Provider; + private _dummyERC20TokenContracts?: DummyERC20TokenContract[]; + private _erc20ProxyContract?: ERC20ProxyContract; + constructor(deployer: Deployer, provider: Provider, tokenOwnerAddresses: string[], contractOwnerAddress: string) { + this._deployer = deployer; + this._provider = provider; + this._tokenOwnerAddresses = tokenOwnerAddresses; + this._contractOwnerAddress = contractOwnerAddress; + } + public async deployDummyERC20TokensAsync(): Promise { + const tokenContractInstances = await Promise.all( + _.map(this._tokenOwnerAddresses, tokenOwnerAddress => + this._deployer.deployAsync(ContractName.DummyERC20Token, constants.DUMMY_ERC20_TOKEN_ARGS), + ), + ); + this._dummyERC20TokenContracts = _.map( + tokenContractInstances, + tokenContractInstance => + new DummyERC20TokenContract(tokenContractInstance.abi, tokenContractInstance.address, this._provider), + ); + return this._dummyERC20TokenContracts; + } + public async deployERC20ProxyAsync(): Promise { + const proxyContractInstance = await this._deployer.deployAsync(ContractName.ERC20Proxy); + this._erc20ProxyContract = new ERC20ProxyContract( + proxyContractInstance.abi, + proxyContractInstance.address, + this._provider, + ); + return this._erc20ProxyContract; + } + public async setBalancesAndAllowancesAsync() { + if (_.isUndefined(this._dummyERC20TokenContracts)) { + throw new Error('Dummy ERC20 tokens not yet deployed, please call "deployDummyERC20TokensAsync"'); + } + if (_.isUndefined(this._erc20ProxyContract)) { + throw new Error('ERC20 proxy contract not yet deployed, please call "deployERC20ProxyAsync"'); + } + const setBalancePromises: any[] = []; + const setAllowancePromises: any[] = []; + _.forEach(this._dummyERC20TokenContracts, dummyERC20TokenContract => { + _.forEach(this._tokenOwnerAddresses, tokenOwnerAddress => { + setBalancePromises.push( + dummyERC20TokenContract.setBalance.sendTransactionAsync( + tokenOwnerAddress, + constants.INITIAL_ERC20_BALANCE, + { from: this._contractOwnerAddress }, + ), + ); + setAllowancePromises.push( + dummyERC20TokenContract.approve.sendTransactionAsync( + (this._erc20ProxyContract as ERC20ProxyContract).address, + constants.INITIAL_ERC20_ALLOWANCE, + { from: tokenOwnerAddress }, + ), + ); + }); + }); + await Promise.all([...setBalancePromises, ...setAllowancePromises]); + } + public async getBalancesAsync(): Promise { + if (_.isUndefined(this._dummyERC20TokenContracts)) { + throw new Error('Dummy ERC20 tokens not yet deployed, please call "deployDummyTokensAsync"'); + } + const balancesByOwner: ERC20BalancesByOwner = {}; + const balancePromises: any[] = []; + const balanceInfo: Array<{ tokenOwnerAddress: string; tokenAddress: string }> = []; + _.forEach(this._dummyERC20TokenContracts, dummyERC20TokenContract => { + _.forEach(this._tokenOwnerAddresses, tokenOwnerAddress => { + balancePromises.push(dummyERC20TokenContract.balanceOf.callAsync(tokenOwnerAddress)); + balanceInfo.push({ + tokenOwnerAddress, + tokenAddress: dummyERC20TokenContract.address, + }); + }); + }); + const balances = await Promise.all(balancePromises); + _.forEach(balances, (balance, balanceIndex) => { + const tokenAddress = balanceInfo[balanceIndex].tokenAddress; + const tokenOwnerAddress = balanceInfo[balanceIndex].tokenOwnerAddress; + if (_.isUndefined(balancesByOwner[tokenOwnerAddress])) { + balancesByOwner[tokenOwnerAddress] = {}; + } + const wrappedBalance = new BigNumber(balance); + balancesByOwner[tokenOwnerAddress][tokenAddress] = wrappedBalance; + }); + return balancesByOwner; + } + public getTokenOwnerAddresses(): string[] { + return this._tokenOwnerAddresses; + } + public getTokenAddresses(): string[] { + const tokenAddresses = _.map( + this._dummyERC20TokenContracts, + dummyERC20TokenContract => dummyERC20TokenContract.address, + ); + return tokenAddresses; + } +} -- cgit v1.2.3 From 48b44342ab1642a78d0485e111019acfdbbfdfd5 Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Sun, 22 Apr 2018 15:17:55 -0700 Subject: Add ERC721 wrapper --- packages/contracts/src/utils/erc721_wrapper.ts | 154 +++++++++++++++++++++++++ packages/contracts/src/utils/types.ts | 27 ++--- 2 files changed, 161 insertions(+), 20 deletions(-) create mode 100644 packages/contracts/src/utils/erc721_wrapper.ts (limited to 'packages/contracts/src') diff --git a/packages/contracts/src/utils/erc721_wrapper.ts b/packages/contracts/src/utils/erc721_wrapper.ts new file mode 100644 index 000000000..5b79e5ddb --- /dev/null +++ b/packages/contracts/src/utils/erc721_wrapper.ts @@ -0,0 +1,154 @@ +import { ZeroEx } from '0x.js'; +import { Deployer } from '@0xproject/deployer'; +import { Provider } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; +import * as _ from 'lodash'; + +import { DummyERC721TokenContract } from '../contract_wrappers/generated/dummy_e_r_c721_token'; +import { ERC721ProxyContract } from '../contract_wrappers/generated/e_r_c721_proxy'; + +import { constants } from './constants'; +import { ContractName, ERC721BalancesByOwner } from './types'; + +export class ERC721Wrapper { + private _tokenOwnerAddresses: string[]; + private _contractOwnerAddress: string; + private _deployer: Deployer; + private _provider: Provider; + private _dummyERC721TokenContracts?: DummyERC721TokenContract[]; + private _erc721ProxyContract?: ERC721ProxyContract; + private _initialERC721BalancesByOwner?: ERC721BalancesByOwner; + constructor(deployer: Deployer, provider: Provider, tokenOwnerAddresses: string[], contractOwnerAddress: string) { + this._deployer = deployer; + this._provider = provider; + this._tokenOwnerAddresses = tokenOwnerAddresses; + this._contractOwnerAddress = contractOwnerAddress; + } + public async deployDummyERC721TokensAsync(): Promise { + const tokenContractInstances = await Promise.all( + _.map(this._tokenOwnerAddresses, tokenOwnerAddress => + this._deployer.deployAsync(ContractName.DummyERC721Token, constants.DUMMY_ERC721_TOKEN_ARGS), + ), + ); + this._dummyERC721TokenContracts = _.map( + tokenContractInstances, + tokenContractInstance => + new DummyERC721TokenContract(tokenContractInstance.abi, tokenContractInstance.address, this._provider), + ); + return this._dummyERC721TokenContracts; + } + public async deployERC721ProxyAsync(): Promise { + const proxyContractInstance = await this._deployer.deployAsync(ContractName.ERC721Proxy); + this._erc721ProxyContract = new ERC721ProxyContract( + proxyContractInstance.abi, + proxyContractInstance.address, + this._provider, + ); + return this._erc721ProxyContract; + } + public async setBalancesAndAllowancesAsync() { + if (_.isUndefined(this._dummyERC721TokenContracts)) { + throw new Error('Dummy ERC721 tokens not yet deployed, please call "deployDummyERC721TokensAsync"'); + } + if (_.isUndefined(this._erc721ProxyContract)) { + throw new Error('ERC721 proxy contract not yet deployed, please call "deployERC721ProxyAsync"'); + } + const setBalancePromises: any[] = []; + const setAllowancePromises: any[] = []; + this._initialERC721BalancesByOwner = {}; + _.forEach(this._dummyERC721TokenContracts, dummyERC721TokenContract => { + _.forEach(this._tokenOwnerAddresses, tokenOwnerAddress => { + _.forEach(_.range(constants.NUM_ERC721_TOKENS_TO_MINT), () => { + const tokenId = ZeroEx.generatePseudoRandomSalt(); + setBalancePromises.push( + dummyERC721TokenContract.mint.sendTransactionAsync(tokenOwnerAddress, tokenId, { + from: this._contractOwnerAddress, + }), + ); + if ( + _.isUndefined((this._initialERC721BalancesByOwner as ERC721BalancesByOwner)[tokenOwnerAddress]) + ) { + (this._initialERC721BalancesByOwner as ERC721BalancesByOwner)[tokenOwnerAddress] = { + [dummyERC721TokenContract.address]: [], + }; + } + if ( + _.isUndefined( + (this._initialERC721BalancesByOwner as ERC721BalancesByOwner)[tokenOwnerAddress][ + dummyERC721TokenContract.address + ], + ) + ) { + (this._initialERC721BalancesByOwner as ERC721BalancesByOwner)[tokenOwnerAddress][ + dummyERC721TokenContract.address + ] = []; + } + (this._initialERC721BalancesByOwner as ERC721BalancesByOwner)[tokenOwnerAddress][ + dummyERC721TokenContract.address + ].push(tokenId); + }); + const approval = true; + setAllowancePromises.push( + dummyERC721TokenContract.setApprovalForAll.sendTransactionAsync( + (this._erc721ProxyContract as ERC721ProxyContract).address, + approval, + { from: tokenOwnerAddress }, + ), + ); + }); + }); + await Promise.all([...setBalancePromises, ...setAllowancePromises]); + } + public async getBalancesAsync(): Promise { + if (_.isUndefined(this._dummyERC721TokenContracts)) { + throw new Error('Dummy ERC721 tokens not yet deployed, please call "deployDummyERC721TokensAsync"'); + } + if (_.isUndefined(this._initialERC721BalancesByOwner)) { + throw new Error( + 'Dummy ERC721 balances and allowances not yet set, please call "setBalancesAndAllowancesAsync"', + ); + } + const balancesByOwner: ERC721BalancesByOwner = {}; + const tokenOwnerPromises: any[] = []; + const tokenInfo: Array<{ tokenId: BigNumber; tokenAddress: string }> = []; + _.forEach(this._dummyERC721TokenContracts, dummyERC721TokenContract => { + _.forEach(this._tokenOwnerAddresses, tokenOwnerAddress => { + const initialTokenOwnerIds = (this._initialERC721BalancesByOwner as ERC721BalancesByOwner)[ + tokenOwnerAddress + ][dummyERC721TokenContract.address]; + _.forEach(initialTokenOwnerIds, tokenId => { + tokenOwnerPromises.push(dummyERC721TokenContract.ownerOf.callAsync(tokenId)); + tokenInfo.push({ + tokenId, + tokenAddress: dummyERC721TokenContract.address, + }); + }); + }); + }); + const tokenOwnerAddresses = await Promise.all(tokenOwnerPromises); + _.forEach(tokenOwnerAddresses, (tokenOwnerAddress, ownerIndex) => { + const tokenAddress = tokenInfo[ownerIndex].tokenAddress; + const tokenId = tokenInfo[ownerIndex].tokenId; + if (_.isUndefined(balancesByOwner[tokenOwnerAddress])) { + balancesByOwner[tokenOwnerAddress] = { + [tokenAddress]: [], + }; + } + if (_.isUndefined(balancesByOwner[tokenOwnerAddress][tokenAddress])) { + balancesByOwner[tokenOwnerAddress][tokenAddress] = []; + } + balancesByOwner[tokenOwnerAddress][tokenAddress].push(tokenId); + }); + return balancesByOwner; + } + public getTokenOwnerAddresses(): string[] { + return this._tokenOwnerAddresses; + } + public getTokenAddresses(): string[] { + const tokenAddresses = _.map( + this._dummyERC721TokenContracts, + dummyERC721TokenContract => dummyERC721TokenContract.address, + ); + return tokenAddresses; + } +} diff --git a/packages/contracts/src/utils/types.ts b/packages/contracts/src/utils/types.ts index 8bc9641fc..629187e95 100644 --- a/packages/contracts/src/utils/types.ts +++ b/packages/contracts/src/utils/types.ts @@ -1,12 +1,18 @@ import { AbiDefinition, ContractAbi } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; -export interface BalancesByOwner { +export interface ERC20BalancesByOwner { [ownerAddress: string]: { [tokenAddress: string]: BigNumber; }; } +export interface ERC721BalancesByOwner { + [ownerAddress: string]: { + [tokenAddress: string]: BigNumber[]; + }; +} + export interface SubmissionContractEventArgs { transactionId: BigNumber; } @@ -43,20 +49,6 @@ export enum AssetProxyId { ERC721, } -export interface DefaultOrderParams { - exchangeAddress: string; - makerAddress: string; - feeRecipientAddress: string; - makerAssetAddress: string; - takerAssetAddress: string; - makerAssetAmount: BigNumber; - takerAssetAmount: BigNumber; - makerFee: BigNumber; - takerFee: BigNumber; - makerAssetData: string; - takerAssetData: string; -} - export interface TransactionDataParams { name: string; abi: AbiDefinition[]; @@ -82,11 +74,6 @@ export interface Token { swarmHash: string; } -export interface TokenInfoByNetwork { - development: Token[]; - live: Token[]; -} - export enum ExchangeContractErrs { ERROR_ORDER_EXPIRED, ERROR_ORDER_FULLY_FILLED, -- cgit v1.2.3 From afb02b8bd16758f00d630c9ce6cf3bbc49ab307d Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Sun, 22 Apr 2018 15:18:55 -0700 Subject: Fix and cleanup tests --- packages/contracts/src/utils/address_utils.ts | 12 ++++++++++++ packages/contracts/src/utils/order_factory.ts | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 packages/contracts/src/utils/address_utils.ts (limited to 'packages/contracts/src') diff --git a/packages/contracts/src/utils/address_utils.ts b/packages/contracts/src/utils/address_utils.ts new file mode 100644 index 000000000..01a7a6fd4 --- /dev/null +++ b/packages/contracts/src/utils/address_utils.ts @@ -0,0 +1,12 @@ +import { ZeroEx } from '0x.js'; + +import { crypto } from './crypto'; + +export const addressUtils = { + generatePseudoRandomAddress(): string { + const randomBigNum = ZeroEx.generatePseudoRandomSalt(); + const randomBuff = crypto.solSHA3([randomBigNum]); + const randomAddress = `0x${randomBuff.slice(0, 20).toString('hex')}`; + return randomAddress; + }, +}; diff --git a/packages/contracts/src/utils/order_factory.ts b/packages/contracts/src/utils/order_factory.ts index bdf5f9abe..08853054d 100644 --- a/packages/contracts/src/utils/order_factory.ts +++ b/packages/contracts/src/utils/order_factory.ts @@ -4,7 +4,7 @@ import * as _ from 'lodash'; import { orderUtils } from './order_utils'; import { signingUtils } from './signing_utils'; -import { DefaultOrderParams, SignatureType, SignedOrder, UnsignedOrder } from './types'; +import { SignatureType, SignedOrder, UnsignedOrder } from './types'; export class OrderFactory { private _defaultOrderParams: Partial; -- cgit v1.2.3 From f427a736a39d716e5d3c796ffe7648a79469e6ec Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Sun, 22 Apr 2018 19:16:22 -0700 Subject: Remove type assertions --- packages/contracts/src/utils/erc721_wrapper.ts | 30 +++++++++++--------------- 1 file changed, 12 insertions(+), 18 deletions(-) (limited to 'packages/contracts/src') diff --git a/packages/contracts/src/utils/erc721_wrapper.ts b/packages/contracts/src/utils/erc721_wrapper.ts index 5b79e5ddb..906e564d0 100644 --- a/packages/contracts/src/utils/erc721_wrapper.ts +++ b/packages/contracts/src/utils/erc721_wrapper.ts @@ -17,7 +17,7 @@ export class ERC721Wrapper { private _provider: Provider; private _dummyERC721TokenContracts?: DummyERC721TokenContract[]; private _erc721ProxyContract?: ERC721ProxyContract; - private _initialERC721BalancesByOwner?: ERC721BalancesByOwner; + private _initialERC721BalancesByOwner: ERC721BalancesByOwner = {}; constructor(deployer: Deployer, provider: Provider, tokenOwnerAddresses: string[], contractOwnerAddress: string) { this._deployer = deployer; this._provider = provider; @@ -65,27 +65,21 @@ export class ERC721Wrapper { from: this._contractOwnerAddress, }), ); - if ( - _.isUndefined((this._initialERC721BalancesByOwner as ERC721BalancesByOwner)[tokenOwnerAddress]) - ) { - (this._initialERC721BalancesByOwner as ERC721BalancesByOwner)[tokenOwnerAddress] = { + if (_.isUndefined(this._initialERC721BalancesByOwner[tokenOwnerAddress])) { + this._initialERC721BalancesByOwner[tokenOwnerAddress] = { [dummyERC721TokenContract.address]: [], }; } if ( _.isUndefined( - (this._initialERC721BalancesByOwner as ERC721BalancesByOwner)[tokenOwnerAddress][ - dummyERC721TokenContract.address - ], + this._initialERC721BalancesByOwner[tokenOwnerAddress][dummyERC721TokenContract.address], ) ) { - (this._initialERC721BalancesByOwner as ERC721BalancesByOwner)[tokenOwnerAddress][ - dummyERC721TokenContract.address - ] = []; + this._initialERC721BalancesByOwner[tokenOwnerAddress][dummyERC721TokenContract.address] = []; } - (this._initialERC721BalancesByOwner as ERC721BalancesByOwner)[tokenOwnerAddress][ - dummyERC721TokenContract.address - ].push(tokenId); + this._initialERC721BalancesByOwner[tokenOwnerAddress][dummyERC721TokenContract.address].push( + tokenId, + ); }); const approval = true; setAllowancePromises.push( @@ -103,7 +97,7 @@ export class ERC721Wrapper { if (_.isUndefined(this._dummyERC721TokenContracts)) { throw new Error('Dummy ERC721 tokens not yet deployed, please call "deployDummyERC721TokensAsync"'); } - if (_.isUndefined(this._initialERC721BalancesByOwner)) { + if (_.keys(this._initialERC721BalancesByOwner).length === 0) { throw new Error( 'Dummy ERC721 balances and allowances not yet set, please call "setBalancesAndAllowancesAsync"', ); @@ -113,9 +107,9 @@ export class ERC721Wrapper { const tokenInfo: Array<{ tokenId: BigNumber; tokenAddress: string }> = []; _.forEach(this._dummyERC721TokenContracts, dummyERC721TokenContract => { _.forEach(this._tokenOwnerAddresses, tokenOwnerAddress => { - const initialTokenOwnerIds = (this._initialERC721BalancesByOwner as ERC721BalancesByOwner)[ - tokenOwnerAddress - ][dummyERC721TokenContract.address]; + const initialTokenOwnerIds = this._initialERC721BalancesByOwner[tokenOwnerAddress][ + dummyERC721TokenContract.address + ]; _.forEach(initialTokenOwnerIds, tokenId => { tokenOwnerPromises.push(dummyERC721TokenContract.ownerOf.callAsync(tokenId)); tokenInfo.push({ -- cgit v1.2.3 From 32e77753b518e0821d75c404b9dcd16af8f2cd8b Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Sun, 22 Apr 2018 19:30:04 -0700 Subject: Define numbers of dummy tokens to deploy in constants --- packages/contracts/src/utils/constants.ts | 2 ++ packages/contracts/src/utils/erc20_wrapper.ts | 2 +- packages/contracts/src/utils/erc721_wrapper.ts | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) (limited to 'packages/contracts/src') diff --git a/packages/contracts/src/utils/constants.ts b/packages/contracts/src/utils/constants.ts index 9662a5c27..2c9d7b272 100644 --- a/packages/contracts/src/utils/constants.ts +++ b/packages/contracts/src/utils/constants.ts @@ -29,6 +29,8 @@ export const constants = { MAX_TOKEN_APPROVE_GAS: 60000, DUMMY_ERC20_TOKEN_ARGS: [DUMMY_TOKEN_NAME, DUMMY_TOKEN_SYMBOL, DUMMY_TOKEN_DECIMALS, DUMMY_TOKEN_TOTAL_SUPPLY], DUMMY_ERC721_TOKEN_ARGS: [DUMMY_TOKEN_NAME, DUMMY_TOKEN_SYMBOL], + NUM_DUMMY_ERC20_TO_DEPLOY: 3, + NUM_DUMMY_ERC721_TO_DEPLOY: 1, NUM_ERC721_TOKENS_TO_MINT: 2, TESTRPC_PRIVATE_KEYS: _.map(TESTRPC_PRIVATE_KEYS_STRINGS, privateKeyString => ethUtil.toBuffer(privateKeyString)), INITIAL_ERC20_BALANCE: ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18), diff --git a/packages/contracts/src/utils/erc20_wrapper.ts b/packages/contracts/src/utils/erc20_wrapper.ts index 950b20c99..212a639ff 100644 --- a/packages/contracts/src/utils/erc20_wrapper.ts +++ b/packages/contracts/src/utils/erc20_wrapper.ts @@ -24,7 +24,7 @@ export class ERC20Wrapper { } public async deployDummyERC20TokensAsync(): Promise { const tokenContractInstances = await Promise.all( - _.map(this._tokenOwnerAddresses, tokenOwnerAddress => + _.map(_.range(constants.NUM_DUMMY_ERC20_TO_DEPLOY), () => this._deployer.deployAsync(ContractName.DummyERC20Token, constants.DUMMY_ERC20_TOKEN_ARGS), ), ); diff --git a/packages/contracts/src/utils/erc721_wrapper.ts b/packages/contracts/src/utils/erc721_wrapper.ts index 906e564d0..0c62fd5d9 100644 --- a/packages/contracts/src/utils/erc721_wrapper.ts +++ b/packages/contracts/src/utils/erc721_wrapper.ts @@ -26,7 +26,7 @@ export class ERC721Wrapper { } public async deployDummyERC721TokensAsync(): Promise { const tokenContractInstances = await Promise.all( - _.map(this._tokenOwnerAddresses, tokenOwnerAddress => + _.map(_.range(constants.NUM_DUMMY_ERC721_TO_DEPLOY), () => this._deployer.deployAsync(ContractName.DummyERC721Token, constants.DUMMY_ERC721_TOKEN_ARGS), ), ); -- cgit v1.2.3 From 27a44f0e77da67dd0b928945135cf28b93cbe87f Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Mon, 23 Apr 2018 23:23:01 -0700 Subject: Rename variables, create private validation methods in token wrappers --- packages/contracts/src/utils/erc20_wrapper.ts | 65 ++++++------- packages/contracts/src/utils/erc721_wrapper.ts | 126 ++++++++++++------------- packages/contracts/src/utils/types.ts | 2 +- 3 files changed, 96 insertions(+), 97 deletions(-) (limited to 'packages/contracts/src') diff --git a/packages/contracts/src/utils/erc20_wrapper.ts b/packages/contracts/src/utils/erc20_wrapper.ts index 212a639ff..c2effb617 100644 --- a/packages/contracts/src/utils/erc20_wrapper.ts +++ b/packages/contracts/src/utils/erc20_wrapper.ts @@ -14,57 +14,53 @@ export class ERC20Wrapper { private _contractOwnerAddress: string; private _deployer: Deployer; private _provider: Provider; - private _dummyERC20TokenContracts?: DummyERC20TokenContract[]; - private _erc20ProxyContract?: ERC20ProxyContract; + private _dummyTokenContracts?: DummyERC20TokenContract[]; + private _proxyContract?: ERC20ProxyContract; constructor(deployer: Deployer, provider: Provider, tokenOwnerAddresses: string[], contractOwnerAddress: string) { this._deployer = deployer; this._provider = provider; this._tokenOwnerAddresses = tokenOwnerAddresses; this._contractOwnerAddress = contractOwnerAddress; } - public async deployDummyERC20TokensAsync(): Promise { + public async deployDummyTokensAsync(): Promise { const tokenContractInstances = await Promise.all( - _.map(_.range(constants.NUM_DUMMY_ERC20_TO_DEPLOY), () => + _.times(constants.NUM_DUMMY_ERC20_TO_DEPLOY, () => this._deployer.deployAsync(ContractName.DummyERC20Token, constants.DUMMY_ERC20_TOKEN_ARGS), ), ); - this._dummyERC20TokenContracts = _.map( + this._dummyTokenContracts = _.map( tokenContractInstances, tokenContractInstance => new DummyERC20TokenContract(tokenContractInstance.abi, tokenContractInstance.address, this._provider), ); - return this._dummyERC20TokenContracts; + return this._dummyTokenContracts; } - public async deployERC20ProxyAsync(): Promise { + public async deployProxyAsync(): Promise { const proxyContractInstance = await this._deployer.deployAsync(ContractName.ERC20Proxy); - this._erc20ProxyContract = new ERC20ProxyContract( + this._proxyContract = new ERC20ProxyContract( proxyContractInstance.abi, proxyContractInstance.address, this._provider, ); - return this._erc20ProxyContract; + return this._proxyContract; } public async setBalancesAndAllowancesAsync() { - if (_.isUndefined(this._dummyERC20TokenContracts)) { - throw new Error('Dummy ERC20 tokens not yet deployed, please call "deployDummyERC20TokensAsync"'); - } - if (_.isUndefined(this._erc20ProxyContract)) { - throw new Error('ERC20 proxy contract not yet deployed, please call "deployERC20ProxyAsync"'); - } - const setBalancePromises: any[] = []; - const setAllowancePromises: any[] = []; - _.forEach(this._dummyERC20TokenContracts, dummyERC20TokenContract => { + this._validateDummyTokenContractsExistOrThrow(); + this._validateProxyContractExistsOrThrow(); + const setBalancePromises: Array> = []; + const setAllowancePromises: Array> = []; + _.forEach(this._dummyTokenContracts, dummyTokenContract => { _.forEach(this._tokenOwnerAddresses, tokenOwnerAddress => { setBalancePromises.push( - dummyERC20TokenContract.setBalance.sendTransactionAsync( + dummyTokenContract.setBalance.sendTransactionAsync( tokenOwnerAddress, constants.INITIAL_ERC20_BALANCE, { from: this._contractOwnerAddress }, ), ); setAllowancePromises.push( - dummyERC20TokenContract.approve.sendTransactionAsync( - (this._erc20ProxyContract as ERC20ProxyContract).address, + dummyTokenContract.approve.sendTransactionAsync( + (this._proxyContract as ERC20ProxyContract).address, constants.INITIAL_ERC20_ALLOWANCE, { from: tokenOwnerAddress }, ), @@ -74,18 +70,16 @@ export class ERC20Wrapper { await Promise.all([...setBalancePromises, ...setAllowancePromises]); } public async getBalancesAsync(): Promise { - if (_.isUndefined(this._dummyERC20TokenContracts)) { - throw new Error('Dummy ERC20 tokens not yet deployed, please call "deployDummyTokensAsync"'); - } + this._validateDummyTokenContractsExistOrThrow(); const balancesByOwner: ERC20BalancesByOwner = {}; - const balancePromises: any[] = []; + const balancePromises: Array> = []; const balanceInfo: Array<{ tokenOwnerAddress: string; tokenAddress: string }> = []; - _.forEach(this._dummyERC20TokenContracts, dummyERC20TokenContract => { + _.forEach(this._dummyTokenContracts, dummyTokenContract => { _.forEach(this._tokenOwnerAddresses, tokenOwnerAddress => { - balancePromises.push(dummyERC20TokenContract.balanceOf.callAsync(tokenOwnerAddress)); + balancePromises.push(dummyTokenContract.balanceOf.callAsync(tokenOwnerAddress)); balanceInfo.push({ tokenOwnerAddress, - tokenAddress: dummyERC20TokenContract.address, + tokenAddress: dummyTokenContract.address, }); }); }); @@ -105,10 +99,17 @@ export class ERC20Wrapper { return this._tokenOwnerAddresses; } public getTokenAddresses(): string[] { - const tokenAddresses = _.map( - this._dummyERC20TokenContracts, - dummyERC20TokenContract => dummyERC20TokenContract.address, - ); + const tokenAddresses = _.map(this._dummyTokenContracts, dummyTokenContract => dummyTokenContract.address); return tokenAddresses; } + private _validateDummyTokenContractsExistOrThrow() { + if (_.isUndefined(this._dummyTokenContracts)) { + throw new Error('Dummy ERC20 tokens not yet deployed, please call "deployDummyTokensAsync"'); + } + } + private _validateProxyContractExistsOrThrow() { + if (_.isUndefined(this._proxyContract)) { + throw new Error('ERC20 proxy contract not yet deployed, please call "deployProxyAsync"'); + } + } } diff --git a/packages/contracts/src/utils/erc721_wrapper.ts b/packages/contracts/src/utils/erc721_wrapper.ts index 0c62fd5d9..b78b76b51 100644 --- a/packages/contracts/src/utils/erc721_wrapper.ts +++ b/packages/contracts/src/utils/erc721_wrapper.ts @@ -8,84 +8,74 @@ import { DummyERC721TokenContract } from '../contract_wrappers/generated/dummy_e import { ERC721ProxyContract } from '../contract_wrappers/generated/e_r_c721_proxy'; import { constants } from './constants'; -import { ContractName, ERC721BalancesByOwner } from './types'; +import { ContractName, ERC721TokenIdsByOwner } from './types'; export class ERC721Wrapper { private _tokenOwnerAddresses: string[]; private _contractOwnerAddress: string; private _deployer: Deployer; private _provider: Provider; - private _dummyERC721TokenContracts?: DummyERC721TokenContract[]; - private _erc721ProxyContract?: ERC721ProxyContract; - private _initialERC721BalancesByOwner: ERC721BalancesByOwner = {}; + private _dummyTokenContracts?: DummyERC721TokenContract[]; + private _proxyContract?: ERC721ProxyContract; + private _initialTokenIdsByOwner: ERC721TokenIdsByOwner = {}; constructor(deployer: Deployer, provider: Provider, tokenOwnerAddresses: string[], contractOwnerAddress: string) { this._deployer = deployer; this._provider = provider; this._tokenOwnerAddresses = tokenOwnerAddresses; this._contractOwnerAddress = contractOwnerAddress; } - public async deployDummyERC721TokensAsync(): Promise { + public async deployDummyTokensAsync(): Promise { const tokenContractInstances = await Promise.all( - _.map(_.range(constants.NUM_DUMMY_ERC721_TO_DEPLOY), () => + _.times(constants.NUM_DUMMY_ERC721_TO_DEPLOY, () => this._deployer.deployAsync(ContractName.DummyERC721Token, constants.DUMMY_ERC721_TOKEN_ARGS), ), ); - this._dummyERC721TokenContracts = _.map( + this._dummyTokenContracts = _.map( tokenContractInstances, tokenContractInstance => new DummyERC721TokenContract(tokenContractInstance.abi, tokenContractInstance.address, this._provider), ); - return this._dummyERC721TokenContracts; + return this._dummyTokenContracts; } - public async deployERC721ProxyAsync(): Promise { + public async deployProxyAsync(): Promise { const proxyContractInstance = await this._deployer.deployAsync(ContractName.ERC721Proxy); - this._erc721ProxyContract = new ERC721ProxyContract( + this._proxyContract = new ERC721ProxyContract( proxyContractInstance.abi, proxyContractInstance.address, this._provider, ); - return this._erc721ProxyContract; + return this._proxyContract; } public async setBalancesAndAllowancesAsync() { - if (_.isUndefined(this._dummyERC721TokenContracts)) { - throw new Error('Dummy ERC721 tokens not yet deployed, please call "deployDummyERC721TokensAsync"'); - } - if (_.isUndefined(this._erc721ProxyContract)) { - throw new Error('ERC721 proxy contract not yet deployed, please call "deployERC721ProxyAsync"'); - } - const setBalancePromises: any[] = []; - const setAllowancePromises: any[] = []; - this._initialERC721BalancesByOwner = {}; - _.forEach(this._dummyERC721TokenContracts, dummyERC721TokenContract => { + this._validateDummyTokenContractsExistOrThrow(); + this._validateProxyContractExistsOrThrow(); + const setBalancePromises: Array> = []; + const setAllowancePromises: Array> = []; + this._initialTokenIdsByOwner = {}; + _.forEach(this._dummyTokenContracts, dummyTokenContract => { _.forEach(this._tokenOwnerAddresses, tokenOwnerAddress => { _.forEach(_.range(constants.NUM_ERC721_TOKENS_TO_MINT), () => { const tokenId = ZeroEx.generatePseudoRandomSalt(); setBalancePromises.push( - dummyERC721TokenContract.mint.sendTransactionAsync(tokenOwnerAddress, tokenId, { + dummyTokenContract.mint.sendTransactionAsync(tokenOwnerAddress, tokenId, { from: this._contractOwnerAddress, }), ); - if (_.isUndefined(this._initialERC721BalancesByOwner[tokenOwnerAddress])) { - this._initialERC721BalancesByOwner[tokenOwnerAddress] = { - [dummyERC721TokenContract.address]: [], + if (_.isUndefined(this._initialTokenIdsByOwner[tokenOwnerAddress])) { + this._initialTokenIdsByOwner[tokenOwnerAddress] = { + [dummyTokenContract.address]: [], }; } - if ( - _.isUndefined( - this._initialERC721BalancesByOwner[tokenOwnerAddress][dummyERC721TokenContract.address], - ) - ) { - this._initialERC721BalancesByOwner[tokenOwnerAddress][dummyERC721TokenContract.address] = []; + if (_.isUndefined(this._initialTokenIdsByOwner[tokenOwnerAddress][dummyTokenContract.address])) { + this._initialTokenIdsByOwner[tokenOwnerAddress][dummyTokenContract.address] = []; } - this._initialERC721BalancesByOwner[tokenOwnerAddress][dummyERC721TokenContract.address].push( - tokenId, - ); + this._initialTokenIdsByOwner[tokenOwnerAddress][dummyTokenContract.address].push(tokenId); }); - const approval = true; + const shouldApprove = true; setAllowancePromises.push( - dummyERC721TokenContract.setApprovalForAll.sendTransactionAsync( - (this._erc721ProxyContract as ERC721ProxyContract).address, - approval, + dummyTokenContract.setApprovalForAll.sendTransactionAsync( + (this._proxyContract as ERC721ProxyContract).address, + shouldApprove, { from: tokenOwnerAddress }, ), ); @@ -93,28 +83,22 @@ export class ERC721Wrapper { }); await Promise.all([...setBalancePromises, ...setAllowancePromises]); } - public async getBalancesAsync(): Promise { - if (_.isUndefined(this._dummyERC721TokenContracts)) { - throw new Error('Dummy ERC721 tokens not yet deployed, please call "deployDummyERC721TokensAsync"'); - } - if (_.keys(this._initialERC721BalancesByOwner).length === 0) { - throw new Error( - 'Dummy ERC721 balances and allowances not yet set, please call "setBalancesAndAllowancesAsync"', - ); - } - const balancesByOwner: ERC721BalancesByOwner = {}; - const tokenOwnerPromises: any[] = []; + public async getBalancesAsync(): Promise { + this._validateDummyTokenContractsExistOrThrow(); + this._validateBalancesAndAllowancesSetOrThrow(); + const tokenIdsByOwner: ERC721TokenIdsByOwner = {}; + const tokenOwnerPromises: Array> = []; const tokenInfo: Array<{ tokenId: BigNumber; tokenAddress: string }> = []; - _.forEach(this._dummyERC721TokenContracts, dummyERC721TokenContract => { + _.forEach(this._dummyTokenContracts, dummyTokenContract => { _.forEach(this._tokenOwnerAddresses, tokenOwnerAddress => { - const initialTokenOwnerIds = this._initialERC721BalancesByOwner[tokenOwnerAddress][ - dummyERC721TokenContract.address + const initialTokenOwnerIds = this._initialTokenIdsByOwner[tokenOwnerAddress][ + dummyTokenContract.address ]; _.forEach(initialTokenOwnerIds, tokenId => { - tokenOwnerPromises.push(dummyERC721TokenContract.ownerOf.callAsync(tokenId)); + tokenOwnerPromises.push(dummyTokenContract.ownerOf.callAsync(tokenId)); tokenInfo.push({ tokenId, - tokenAddress: dummyERC721TokenContract.address, + tokenAddress: dummyTokenContract.address, }); }); }); @@ -123,26 +107,40 @@ export class ERC721Wrapper { _.forEach(tokenOwnerAddresses, (tokenOwnerAddress, ownerIndex) => { const tokenAddress = tokenInfo[ownerIndex].tokenAddress; const tokenId = tokenInfo[ownerIndex].tokenId; - if (_.isUndefined(balancesByOwner[tokenOwnerAddress])) { - balancesByOwner[tokenOwnerAddress] = { + if (_.isUndefined(tokenIdsByOwner[tokenOwnerAddress])) { + tokenIdsByOwner[tokenOwnerAddress] = { [tokenAddress]: [], }; } - if (_.isUndefined(balancesByOwner[tokenOwnerAddress][tokenAddress])) { - balancesByOwner[tokenOwnerAddress][tokenAddress] = []; + if (_.isUndefined(tokenIdsByOwner[tokenOwnerAddress][tokenAddress])) { + tokenIdsByOwner[tokenOwnerAddress][tokenAddress] = []; } - balancesByOwner[tokenOwnerAddress][tokenAddress].push(tokenId); + tokenIdsByOwner[tokenOwnerAddress][tokenAddress].push(tokenId); }); - return balancesByOwner; + return tokenIdsByOwner; } public getTokenOwnerAddresses(): string[] { return this._tokenOwnerAddresses; } public getTokenAddresses(): string[] { - const tokenAddresses = _.map( - this._dummyERC721TokenContracts, - dummyERC721TokenContract => dummyERC721TokenContract.address, - ); + const tokenAddresses = _.map(this._dummyTokenContracts, dummyTokenContract => dummyTokenContract.address); return tokenAddresses; } + private _validateDummyTokenContractsExistOrThrow() { + if (_.isUndefined(this._dummyTokenContracts)) { + throw new Error('Dummy ERC721 tokens not yet deployed, please call "deployDummyTokensAsync"'); + } + } + private _validateProxyContractExistsOrThrow() { + if (_.isUndefined(this._proxyContract)) { + throw new Error('ERC721 proxy contract not yet deployed, please call "deployProxyAsync"'); + } + } + private _validateBalancesAndAllowancesSetOrThrow() { + if (_.keys(this._initialTokenIdsByOwner).length === 0) { + throw new Error( + 'Dummy ERC721 balances and allowances not yet set, please call "setBalancesAndAllowancesAsync"', + ); + } + } } diff --git a/packages/contracts/src/utils/types.ts b/packages/contracts/src/utils/types.ts index 629187e95..1e3cd5b9a 100644 --- a/packages/contracts/src/utils/types.ts +++ b/packages/contracts/src/utils/types.ts @@ -7,7 +7,7 @@ export interface ERC20BalancesByOwner { }; } -export interface ERC721BalancesByOwner { +export interface ERC721TokenIdsByOwner { [ownerAddress: string]: { [tokenAddress: string]: BigNumber[]; }; -- cgit v1.2.3