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') 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') 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 +- .../test/asset_proxy_dispatcher/dispatcher.ts | 66 +-- .../test/asset_proxy_dispatcher/proxies.ts | 119 ++--- packages/contracts/test/exchange/core.ts | 425 +++++++-------- packages/contracts/test/exchange/helpers.ts | 41 +- packages/contracts/test/exchange/wrapper.ts | 585 ++++++++++----------- .../contracts/test/unlimited_allowance_token.ts | 10 +- 8 files changed, 587 insertions(+), 673 deletions(-) create mode 100644 packages/contracts/src/utils/address_utils.ts (limited to 'packages/contracts') 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; diff --git a/packages/contracts/test/asset_proxy_dispatcher/dispatcher.ts b/packages/contracts/test/asset_proxy_dispatcher/dispatcher.ts index 5cbe11e53..b5630d807 100644 --- a/packages/contracts/test/asset_proxy_dispatcher/dispatcher.ts +++ b/packages/contracts/test/asset_proxy_dispatcher/dispatcher.ts @@ -1,18 +1,17 @@ -import { LogWithDecodedArgs, TransactionReceiptWithDecodedLogs, ZeroEx } from '0x.js'; -import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; +import { ZeroEx } from '0x.js'; +import { BlockchainLifecycle } from '@0xproject/dev-utils'; import { BigNumber } from '@0xproject/utils'; -import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; import * as Web3 from 'web3'; import { AssetProxyDispatcherContract } from '../../src/contract_wrappers/generated/asset_proxy_dispatcher'; import { DummyERC20TokenContract } from '../../src/contract_wrappers/generated/dummy_e_r_c20_token'; -import { DummyERC721TokenContract } from '../../src/contract_wrappers/generated/dummy_e_r_c721_token'; import { ERC20ProxyContract } from '../../src/contract_wrappers/generated/e_r_c20_proxy'; import { ERC721ProxyContract } from '../../src/contract_wrappers/generated/e_r_c721_proxy'; import { assetProxyUtils } from '../../src/utils/asset_proxy_utils'; -import { Balances } from '../../src/utils/balances'; import { constants } from '../../src/utils/constants'; +import { ERC20Wrapper } from '../../src/utils/erc20_wrapper'; +import { ERC721Wrapper } from '../../src/utils/erc721_wrapper'; import { AssetProxyId, ContractName } from '../../src/utils/types'; import { chaiSetup } from '../utils/chai_setup'; import { deployer } from '../utils/deployer'; @@ -27,25 +26,32 @@ describe('AssetProxyDispatcher', () => { let notOwner: string; let notAuthorized: string; let exchangeAddress: string; - let tokenOwner: string; let makerAddress: string; let takerAddress: string; + let zrx: DummyERC20TokenContract; - let dmyBalances: Balances; - let assetProxyDispatcher: AssetProxyDispatcherContract; let erc20Proxy: ERC20ProxyContract; let erc721Proxy: ERC721ProxyContract; - const INITIAL_BALANCE = new BigNumber(10000); + let assetProxyDispatcher: AssetProxyDispatcherContract; + + let erc20Wrapper: ERC20Wrapper; + let erc721Wrapper: ERC721Wrapper; before(async () => { // Setup accounts & addresses const accounts = await web3Wrapper.getAvailableAddressesAsync(); - owner = tokenOwner = accounts[0]; - notOwner = notAuthorized = accounts[1]; - exchangeAddress = accounts[2]; - makerAddress = accounts[3]; - takerAddress = accounts[4]; - // Deploy Asset Proxy Dispatcher + const usedAddresses = ([owner, notOwner, exchangeAddress, makerAddress, takerAddress] = accounts); + notAuthorized = notOwner; + + erc20Wrapper = new ERC20Wrapper(deployer, provider, usedAddresses, owner); + erc721Wrapper = new ERC721Wrapper(deployer, provider, usedAddresses, owner); + + [zrx] = await erc20Wrapper.deployDummyERC20TokensAsync(); + erc20Proxy = await erc20Wrapper.deployERC20ProxyAsync(); + await erc20Wrapper.setBalancesAndAllowancesAsync(); + + erc721Proxy = await erc721Wrapper.deployERC721ProxyAsync(); + const assetProxyDispatcherInstance = await deployer.deployAsync(ContractName.AssetProxyDispatcher); assetProxyDispatcher = new AssetProxyDispatcherContract( assetProxyDispatcherInstance.abi, @@ -55,30 +61,12 @@ describe('AssetProxyDispatcher', () => { await assetProxyDispatcher.addAuthorizedAddress.sendTransactionAsync(exchangeAddress, { from: owner, }); - // Deploy ERC20 Proxy - const erc20ProxyInstance = await deployer.deployAsync(ContractName.ERC20Proxy); - erc20Proxy = new ERC20ProxyContract(erc20ProxyInstance.abi, erc20ProxyInstance.address, provider); await erc20Proxy.addAuthorizedAddress.sendTransactionAsync(assetProxyDispatcher.address, { from: owner, }); - // Deploy ERC721 Proxy - const erc721ProxyInstance = await deployer.deployAsync(ContractName.ERC721Proxy); - erc721Proxy = new ERC721ProxyContract(erc721ProxyInstance.abi, erc721ProxyInstance.address, provider); await erc721Proxy.addAuthorizedAddress.sendTransactionAsync(assetProxyDispatcher.address, { from: owner, }); - // Deploy zrx and set initial balances - const zrxInstance = await deployer.deployAsync(ContractName.DummyERC20Token, constants.DUMMY_TOKEN_ARGS); - zrx = new DummyERC20TokenContract(zrxInstance.abi, zrxInstance.address, provider); - await zrx.setBalance.sendTransactionAsync(makerAddress, INITIAL_BALANCE, { from: tokenOwner }); - await zrx.setBalance.sendTransactionAsync(takerAddress, INITIAL_BALANCE, { from: tokenOwner }); - dmyBalances = new Balances([zrx], [makerAddress, takerAddress]); - await zrx.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_BALANCE, { - from: takerAddress, - }); - await zrx.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_BALANCE, { - from: makerAddress, - }); }); beforeEach(async () => { await blockchainLifecycle.startAsync(); @@ -244,7 +232,7 @@ describe('AssetProxyDispatcher', () => { // Construct metadata for ERC20 proxy const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrx.address); // Perform a transfer from makerAddress to takerAddress - const balances = await dmyBalances.getAsync(); + const erc20Balances = await erc20Wrapper.getBalancesAsync(); const amount = new BigNumber(10); await assetProxyDispatcher.transferFrom.sendTransactionAsync( encodedProxyMetadata, @@ -254,12 +242,12 @@ describe('AssetProxyDispatcher', () => { { from: exchangeAddress }, ); // Verify transfer was successful - const newBalances = await dmyBalances.getAsync(); + const newBalances = await erc20Wrapper.getBalancesAsync(); expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - balances[makerAddress][zrx.address].minus(amount), + erc20Balances[makerAddress][zrx.address].minus(amount), ); expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - balances[takerAddress][zrx.address].add(amount), + erc20Balances[takerAddress][zrx.address].add(amount), ); }); @@ -267,7 +255,7 @@ describe('AssetProxyDispatcher', () => { // Construct metadata for ERC20 proxy const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrx.address); // Perform a transfer from makerAddress to takerAddress - const balances = await dmyBalances.getAsync(); + const erc20Balances = await erc20Wrapper.getBalancesAsync(); const amount = new BigNumber(10); return expect( assetProxyDispatcher.transferFrom.sendTransactionAsync( @@ -292,7 +280,7 @@ describe('AssetProxyDispatcher', () => { // Construct metadata for ERC20 proxy const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrx.address); // Perform a transfer from makerAddress to takerAddress - const balances = await dmyBalances.getAsync(); + const erc20Balances = await erc20Wrapper.getBalancesAsync(); const amount = new BigNumber(10); return expect( assetProxyDispatcher.transferFrom.sendTransactionAsync( diff --git a/packages/contracts/test/asset_proxy_dispatcher/proxies.ts b/packages/contracts/test/asset_proxy_dispatcher/proxies.ts index 9437fcbf0..830f40e13 100644 --- a/packages/contracts/test/asset_proxy_dispatcher/proxies.ts +++ b/packages/contracts/test/asset_proxy_dispatcher/proxies.ts @@ -1,7 +1,6 @@ -import { LogWithDecodedArgs, TransactionReceiptWithDecodedLogs, ZeroEx } from '0x.js'; -import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; +import { ZeroEx } from '0x.js'; +import { BlockchainLifecycle } from '@0xproject/dev-utils'; import { BigNumber } from '@0xproject/utils'; -import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; import * as Web3 from 'web3'; @@ -11,8 +10,9 @@ import { DummyERC721TokenContract } from '../../src/contract_wrappers/generated/ import { ERC20ProxyContract } from '../../src/contract_wrappers/generated/e_r_c20_proxy'; import { ERC721ProxyContract } from '../../src/contract_wrappers/generated/e_r_c721_proxy'; import { assetProxyUtils } from '../../src/utils/asset_proxy_utils'; -import { Balances } from '../../src/utils/balances'; import { constants } from '../../src/utils/constants'; +import { ERC20Wrapper } from '../../src/utils/erc20_wrapper'; +import { ERC721Wrapper } from '../../src/utils/erc721_wrapper'; import { AssetProxyId, ContractName } from '../../src/utils/types'; import { chaiSetup } from '../utils/chai_setup'; import { deployer } from '../utils/deployer'; @@ -26,63 +26,46 @@ describe('Asset Transfer Proxies', () => { let owner: string; let notAuthorized: string; let assetProxyDispatcherAddress: string; - let tokenOwner: string; let makerAddress: string; let takerAddress: string; + let zrx: DummyERC20TokenContract; let erc721Token: DummyERC721TokenContract; - let dmyBalances: Balances; let erc20Proxy: ERC20ProxyContract; let erc721Proxy: ERC721ProxyContract; - const makerAssetId = new BigNumber('0x1010101010101010101010101010101010101010101010101010101010101010'); - const testAddressPaddedWithZeros = '0x0000000000000000056000000000000000000010'; - const INITIAL_BALANCE = new BigNumber(10000); + + let erc20Wrapper: ERC20Wrapper; + let erc721Wrapper: ERC721Wrapper; + let erc721MakerTokenId: BigNumber; before(async () => { - // Setup accounts & addresses const accounts = await web3Wrapper.getAvailableAddressesAsync(); - owner = tokenOwner = accounts[0]; - notAuthorized = accounts[1]; - assetProxyDispatcherAddress = accounts[2]; - makerAddress = accounts[3]; - takerAddress = accounts[4]; - // Deploy ERC20 Proxy - const erc20ProxyInstance = await deployer.deployAsync(ContractName.ERC20Proxy); - erc20Proxy = new ERC20ProxyContract(erc20ProxyInstance.abi, erc20ProxyInstance.address, provider); + const usedAddresses = ([ + owner, + notAuthorized, + assetProxyDispatcherAddress, + makerAddress, + takerAddress, + ] = accounts); + + erc20Wrapper = new ERC20Wrapper(deployer, provider, usedAddresses, owner); + erc721Wrapper = new ERC721Wrapper(deployer, provider, usedAddresses, owner); + + [zrx] = await erc20Wrapper.deployDummyERC20TokensAsync(); + erc20Proxy = await erc20Wrapper.deployERC20ProxyAsync(); + await erc20Wrapper.setBalancesAndAllowancesAsync(); await erc20Proxy.addAuthorizedAddress.sendTransactionAsync(assetProxyDispatcherAddress, { from: owner, }); - // Deploy ERC721 Proxy - const erc721ProxyInstance = await deployer.deployAsync(ContractName.ERC721Proxy); - erc721Proxy = new ERC721ProxyContract(erc721ProxyInstance.abi, erc721ProxyInstance.address, provider); + + [erc721Token] = await erc721Wrapper.deployDummyERC721TokensAsync(); + erc721Proxy = await erc721Wrapper.deployERC721ProxyAsync(); + await erc721Wrapper.setBalancesAndAllowancesAsync(); + const erc721Balances = await erc721Wrapper.getBalancesAsync(); + erc721MakerTokenId = erc721Balances[makerAddress][erc721Token.address][0]; await erc721Proxy.addAuthorizedAddress.sendTransactionAsync(assetProxyDispatcherAddress, { from: owner, }); - // Deploy zrx and set initial balances - const zrxInstance = await deployer.deployAsync(ContractName.DummyERC20Token, constants.DUMMY_TOKEN_ARGS); - zrx = new DummyERC20TokenContract(zrxInstance.abi, zrxInstance.address, provider); - await zrx.setBalance.sendTransactionAsync(makerAddress, INITIAL_BALANCE, { from: tokenOwner }); - await zrx.setBalance.sendTransactionAsync(takerAddress, INITIAL_BALANCE, { from: tokenOwner }); - dmyBalances = new Balances([zrx], [makerAddress, takerAddress]); - await zrx.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_BALANCE, { - from: takerAddress, - }); - await zrx.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_BALANCE, { - from: makerAddress, - }); - // Deploy erc721Token and set initial balances - const erc721TokenInstance = await deployer.deployAsync( - ContractName.DummyERC721Token, - constants.DUMMY_ERC721TOKEN_ARGS, - ); - erc721Token = new DummyERC721TokenContract(erc721TokenInstance.abi, erc721TokenInstance.address, provider); - await erc721Token.setApprovalForAll.sendTransactionAsync(erc721Proxy.address, true, { - from: makerAddress, - }); - await erc721Token.setApprovalForAll.sendTransactionAsync(erc721Proxy.address, true, { - from: takerAddress, - }); - await erc721Token.mint.sendTransactionAsync(makerAddress, makerAssetId, { from: tokenOwner }); }); beforeEach(async () => { await blockchainLifecycle.startAsync(); @@ -95,7 +78,7 @@ describe('Asset Transfer Proxies', () => { // Construct metadata for ERC20 proxy const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrx.address); // Perform a transfer from makerAddress to takerAddress - const balances = await dmyBalances.getAsync(); + const erc20Balances = await erc20Wrapper.getBalancesAsync(); const amount = new BigNumber(10); await erc20Proxy.transferFrom.sendTransactionAsync( encodedProxyMetadata, @@ -105,12 +88,12 @@ describe('Asset Transfer Proxies', () => { { from: assetProxyDispatcherAddress }, ); // Verify transfer was successful - const newBalances = await dmyBalances.getAsync(); + const newBalances = await erc20Wrapper.getBalancesAsync(); expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - balances[makerAddress][zrx.address].minus(amount), + erc20Balances[makerAddress][zrx.address].minus(amount), ); expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - balances[takerAddress][zrx.address].add(amount), + erc20Balances[takerAddress][zrx.address].add(amount), ); }); @@ -118,7 +101,7 @@ describe('Asset Transfer Proxies', () => { // Construct metadata for ERC20 proxy const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrx.address); // Perform a transfer from makerAddress to takerAddress - const balances = await dmyBalances.getAsync(); + const erc20Balances = await erc20Wrapper.getBalancesAsync(); const amount = new BigNumber(0); await erc20Proxy.transferFrom.sendTransactionAsync( encodedProxyMetadata, @@ -128,9 +111,13 @@ describe('Asset Transfer Proxies', () => { { from: assetProxyDispatcherAddress }, ); // Verify transfer was successful - const newBalances = await dmyBalances.getAsync(); - expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal(balances[makerAddress][zrx.address]); - expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal(balances[takerAddress][zrx.address]); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrx.address], + ); + expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrx.address], + ); }); it('should throw if allowances are too low', async () => { @@ -170,12 +157,12 @@ describe('Asset Transfer Proxies', () => { describe('Transfer Proxy - ERC721', () => { it('should successfully transfer tokens', async () => { // Construct metadata for ERC721 proxy - const encodedProxyMetadata = assetProxyUtils.encodeERC721ProxyData(erc721Token.address, makerAssetId); + const encodedProxyMetadata = assetProxyUtils.encodeERC721ProxyData(erc721Token.address, erc721MakerTokenId); // Verify pre-condition - const ownerMakerAsset = await erc721Token.ownerOf.callAsync(makerAssetId); + const ownerMakerAsset = await erc721Token.ownerOf.callAsync(erc721MakerTokenId); expect(ownerMakerAsset).to.be.bignumber.equal(makerAddress); // Perform a transfer from makerAddress to takerAddress - const balances = await dmyBalances.getAsync(); + const erc20Balances = await erc20Wrapper.getBalancesAsync(); const amount = new BigNumber(1); await erc721Proxy.transferFrom.sendTransactionAsync( encodedProxyMetadata, @@ -185,18 +172,18 @@ describe('Asset Transfer Proxies', () => { { from: assetProxyDispatcherAddress }, ); // Verify transfer was successful - const newOwnerMakerAsset = await erc721Token.ownerOf.callAsync(makerAssetId); + const newOwnerMakerAsset = await erc721Token.ownerOf.callAsync(erc721MakerTokenId); expect(newOwnerMakerAsset).to.be.bignumber.equal(takerAddress); }); it('should throw if transferring 0 amount of a token', async () => { // Construct metadata for ERC721 proxy - const encodedProxyMetadata = assetProxyUtils.encodeERC721ProxyData(erc721Token.address, makerAssetId); + const encodedProxyMetadata = assetProxyUtils.encodeERC721ProxyData(erc721Token.address, erc721MakerTokenId); // Verify pre-condition - const ownerMakerAsset = await erc721Token.ownerOf.callAsync(makerAssetId); + const ownerMakerAsset = await erc721Token.ownerOf.callAsync(erc721MakerTokenId); expect(ownerMakerAsset).to.be.bignumber.equal(makerAddress); // Perform a transfer from makerAddress to takerAddress - const balances = await dmyBalances.getAsync(); + const erc20Balances = await erc20Wrapper.getBalancesAsync(); const amount = new BigNumber(0); return expect( erc721Proxy.transferFrom.sendTransactionAsync( @@ -211,12 +198,12 @@ describe('Asset Transfer Proxies', () => { it('should throw if transferring > 1 amount of a token', async () => { // Construct metadata for ERC721 proxy - const encodedProxyMetadata = assetProxyUtils.encodeERC721ProxyData(erc721Token.address, makerAssetId); + const encodedProxyMetadata = assetProxyUtils.encodeERC721ProxyData(erc721Token.address, erc721MakerTokenId); // Verify pre-condition - const ownerMakerAsset = await erc721Token.ownerOf.callAsync(makerAssetId); + const ownerMakerAsset = await erc721Token.ownerOf.callAsync(erc721MakerTokenId); expect(ownerMakerAsset).to.be.bignumber.equal(makerAddress); // Perform a transfer from makerAddress to takerAddress - const balances = await dmyBalances.getAsync(); + const erc20Balances = await erc20Wrapper.getBalancesAsync(); const amount = new BigNumber(500); return expect( erc721Proxy.transferFrom.sendTransactionAsync( @@ -231,7 +218,7 @@ describe('Asset Transfer Proxies', () => { it('should throw if allowances are too low', async () => { // Construct metadata for ERC721 proxy - const encodedProxyMetadata = assetProxyUtils.encodeERC721ProxyData(erc721Token.address, makerAssetId); + const encodedProxyMetadata = assetProxyUtils.encodeERC721ProxyData(erc721Token.address, erc721MakerTokenId); // Remove transfer approval for makerAddress. await erc721Token.setApprovalForAll.sendTransactionAsync(erc721Proxy.address, false, { from: makerAddress, @@ -247,7 +234,7 @@ describe('Asset Transfer Proxies', () => { it('should throw if requesting address is not authorized', async () => { // Construct metadata for ERC721 proxy - const encodedProxyMetadata = assetProxyUtils.encodeERC721ProxyData(erc721Token.address, makerAssetId); + const encodedProxyMetadata = assetProxyUtils.encodeERC721ProxyData(erc721Token.address, erc721MakerTokenId); // Perform a transfer from makerAddress to takerAddress const amount = new BigNumber(1); return expect( diff --git a/packages/contracts/test/exchange/core.ts b/packages/contracts/test/exchange/core.ts index 924dee951..9065c5e3f 100644 --- a/packages/contracts/test/exchange/core.ts +++ b/packages/contracts/test/exchange/core.ts @@ -1,11 +1,9 @@ -import { LogWithDecodedArgs, TransactionReceiptWithDecodedLogs, ZeroEx } from '0x.js'; -import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; +import { LogWithDecodedArgs, ZeroEx } from '0x.js'; +import { BlockchainLifecycle } from '@0xproject/dev-utils'; import { BigNumber } from '@0xproject/utils'; -import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; import ethUtil = require('ethereumjs-util'); import * as _ from 'lodash'; -import * as Web3 from 'web3'; import { AssetProxyDispatcherContract } from '../../src/contract_wrappers/generated/asset_proxy_dispatcher'; import { DummyERC20TokenContract } from '../../src/contract_wrappers/generated/dummy_e_r_c20_token'; @@ -19,18 +17,19 @@ import { FillContractEventArgs, } from '../../src/contract_wrappers/generated/exchange'; import { assetProxyUtils } from '../../src/utils/asset_proxy_utils'; -import { Balances } from '../../src/utils/balances'; import { constants } from '../../src/utils/constants'; import { crypto } from '../../src/utils/crypto'; +import { ERC20Wrapper } from '../../src/utils/erc20_wrapper'; +import { ERC721Wrapper } from '../../src/utils/erc721_wrapper'; import { ExchangeWrapper } from '../../src/utils/exchange_wrapper'; import { OrderFactory } from '../../src/utils/order_factory'; import { orderUtils } from '../../src/utils/order_utils'; import { AssetProxyId, - BalancesByOwner, ContractName, + ERC20BalancesByOwner, + ERC721BalancesByOwner, ExchangeContractErrs, - SignatureType, SignedOrder, } from '../../src/utils/types'; import { chaiSetup } from '../utils/chai_setup'; @@ -41,16 +40,14 @@ chaiSetup.configure(); const expect = chai.expect; const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); -describe('Exchange', () => { +describe('Exchange core', () => { let makerAddress: string; - let tokenOwner: string; + let owner: string; let takerAddress: string; let feeRecipientAddress: string; - const INITIAL_BALANCE = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); - const INITIAL_ALLOWANCE = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); - let rep: DummyERC20TokenContract; - let dgd: DummyERC20TokenContract; + let erc20TokenA: DummyERC20TokenContract; + let erc20TokenB: DummyERC20TokenContract; let zrx: DummyERC20TokenContract; let erc721Token: DummyERC721TokenContract; let exchange: ExchangeContract; @@ -59,20 +56,14 @@ describe('Exchange', () => { let erc721Proxy: ERC721ProxyContract; let signedOrder: SignedOrder; - let balances: BalancesByOwner; - let exWrapper: ExchangeWrapper; - let dmyBalances: Balances; + let erc20Balances: ERC20BalancesByOwner; + let exchangeWrapper: ExchangeWrapper; + let erc20Wrapper: ERC20Wrapper; + let erc721Wrapper: ERC721Wrapper; let orderFactory: OrderFactory; - const erc721MakerAssetIds = [ - new BigNumber('0x1010101010101010101010101010101010101010101010101010101010101010'), - new BigNumber('0x2020202020202020202020202020202020202020202020202020202020202020'), - ]; - - const erc721TakerAssetIds = [ - new BigNumber('0x3030303030303030303030303030303030303030303030303030303030303030'), - new BigNumber('0x4040404040404040404040404040404040404040404040404040404040404040'), - ]; + let erc721MakerAssetIds: BigNumber[]; + let erc721TakerAssetIds: BigNumber[]; let defaultMakerAssetAddress: string; let defaultTakerAssetAddress: string; @@ -81,32 +72,28 @@ describe('Exchange', () => { before(async () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); - makerAddress = accounts[0]; - [tokenOwner, takerAddress, feeRecipientAddress] = accounts; - const owner = tokenOwner; - const [repInstance, dgdInstance, zrxInstance, erc721TokenInstance] = await Promise.all([ - deployer.deployAsync(ContractName.DummyERC20Token, constants.DUMMY_TOKEN_ARGS), - deployer.deployAsync(ContractName.DummyERC20Token, constants.DUMMY_TOKEN_ARGS), - deployer.deployAsync(ContractName.DummyERC20Token, constants.DUMMY_TOKEN_ARGS), - deployer.deployAsync(ContractName.DummyERC721Token, constants.DUMMY_ERC721TOKEN_ARGS), - ]); - rep = new DummyERC20TokenContract(repInstance.abi, repInstance.address, provider); - dgd = new DummyERC20TokenContract(dgdInstance.abi, dgdInstance.address, provider); - zrx = new DummyERC20TokenContract(zrxInstance.abi, zrxInstance.address, provider); - erc721Token = new DummyERC721TokenContract(erc721TokenInstance.abi, erc721TokenInstance.address, provider); - // Deploy Asset Proxy Dispatcher + const usedAddresses = ([owner, makerAddress, takerAddress, feeRecipientAddress] = accounts); + + erc20Wrapper = new ERC20Wrapper(deployer, provider, usedAddresses, owner); + erc721Wrapper = new ERC721Wrapper(deployer, provider, usedAddresses, owner); + + [erc20TokenA, erc20TokenB, zrx] = await erc20Wrapper.deployDummyERC20TokensAsync(); + erc20Proxy = await erc20Wrapper.deployERC20ProxyAsync(); + await erc20Wrapper.setBalancesAndAllowancesAsync(); + + [erc721Token] = await erc721Wrapper.deployDummyERC721TokensAsync(); + erc721Proxy = await erc721Wrapper.deployERC721ProxyAsync(); + await erc721Wrapper.setBalancesAndAllowancesAsync(); + const erc721Balances = await erc721Wrapper.getBalancesAsync(); + erc721MakerAssetIds = erc721Balances[makerAddress][erc721Token.address]; + erc721TakerAssetIds = erc721Balances[takerAddress][erc721Token.address]; + const assetProxyDispatcherInstance = await deployer.deployAsync(ContractName.AssetProxyDispatcher); assetProxyDispatcher = new AssetProxyDispatcherContract( assetProxyDispatcherInstance.abi, assetProxyDispatcherInstance.address, provider, ); - // Deploy ERC20 Proxy - const erc20ProxyInstance = await deployer.deployAsync(ContractName.ERC20Proxy); - erc20Proxy = new ERC20ProxyContract(erc20ProxyInstance.abi, erc20ProxyInstance.address, provider); - await erc20Proxy.addAuthorizedAddress.sendTransactionAsync(assetProxyDispatcher.address, { - from: owner, - }); const prevERC20ProxyAddress = ZeroEx.NULL_ADDRESS; await assetProxyDispatcher.registerAssetProxy.sendTransactionAsync( AssetProxyId.ERC20, @@ -114,12 +101,6 @@ describe('Exchange', () => { prevERC20ProxyAddress, { from: owner }, ); - // Deploy ERC721 Proxy - const erc721ProxyInstance = await deployer.deployAsync(ContractName.ERC721Proxy); - erc721Proxy = new ERC721ProxyContract(erc721ProxyInstance.abi, erc721ProxyInstance.address, provider); - await erc721Proxy.addAuthorizedAddress.sendTransactionAsync(assetProxyDispatcher.address, { - from: owner, - }); const prevERC721ProxyAddress = ZeroEx.NULL_ADDRESS; await assetProxyDispatcher.registerAssetProxy.sendTransactionAsync( AssetProxyId.ERC721, @@ -127,7 +108,13 @@ describe('Exchange', () => { prevERC721ProxyAddress, { from: owner }, ); - // Deploy and configure Exchange + await erc20Proxy.addAuthorizedAddress.sendTransactionAsync(assetProxyDispatcher.address, { + from: owner, + }); + await erc721Proxy.addAuthorizedAddress.sendTransactionAsync(assetProxyDispatcher.address, { + from: owner, + }); + const exchangeInstance = await deployer.deployAsync(ContractName.Exchange, [ assetProxyDispatcher.address, assetProxyUtils.encodeERC20ProxyData(zrx.address), @@ -138,61 +125,21 @@ describe('Exchange', () => { exchangeContractAddress: exchange.address, networkId: constants.TESTRPC_NETWORK_ID, }); - exWrapper = new ExchangeWrapper(exchange, zeroEx); + exchangeWrapper = new ExchangeWrapper(exchange, zeroEx); - defaultMakerAssetAddress = rep.address; - defaultTakerAssetAddress = dgd.address; + defaultMakerAssetAddress = erc20TokenA.address; + defaultTakerAssetAddress = erc20TokenB.address; const defaultOrderParams = { + ...constants.STATIC_ORDER_PARAMS, exchangeAddress: exchange.address, makerAddress, feeRecipientAddress, - 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), makerAssetData: assetProxyUtils.encodeERC20ProxyData(defaultMakerAssetAddress), takerAssetData: assetProxyUtils.encodeERC20ProxyData(defaultTakerAssetAddress), }; - const privateKey = constants.TESTRPC_PRIVATE_KEYS[0]; + const privateKey = constants.TESTRPC_PRIVATE_KEYS[accounts.indexOf(makerAddress)]; orderFactory = new OrderFactory(privateKey, defaultOrderParams); - dmyBalances = new Balances([rep, dgd, zrx], [makerAddress, takerAddress, feeRecipientAddress]); - await Promise.all([ - rep.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_ALLOWANCE, { - from: makerAddress, - }), - rep.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_ALLOWANCE, { - from: takerAddress, - }), - rep.setBalance.sendTransactionAsync(makerAddress, INITIAL_BALANCE, { from: tokenOwner }), - rep.setBalance.sendTransactionAsync(takerAddress, INITIAL_BALANCE, { from: tokenOwner }), - dgd.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_ALLOWANCE, { - from: makerAddress, - }), - dgd.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_ALLOWANCE, { - from: takerAddress, - }), - dgd.setBalance.sendTransactionAsync(makerAddress, INITIAL_BALANCE, { from: tokenOwner }), - dgd.setBalance.sendTransactionAsync(takerAddress, INITIAL_BALANCE, { from: tokenOwner }), - zrx.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_ALLOWANCE, { - from: makerAddress, - }), - zrx.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_ALLOWANCE, { - from: takerAddress, - }), - zrx.setBalance.sendTransactionAsync(makerAddress, INITIAL_BALANCE, { from: tokenOwner }), - zrx.setBalance.sendTransactionAsync(takerAddress, INITIAL_BALANCE, { from: tokenOwner }), - erc721Token.setApprovalForAll.sendTransactionAsync(erc721Proxy.address, true, { - from: makerAddress, - }), - erc721Token.setApprovalForAll.sendTransactionAsync(erc721Proxy.address, true, { - from: takerAddress, - }), - erc721Token.mint.sendTransactionAsync(makerAddress, erc721MakerAssetIds[0], { from: tokenOwner }), - erc721Token.mint.sendTransactionAsync(makerAddress, erc721MakerAssetIds[1], { from: tokenOwner }), - erc721Token.mint.sendTransactionAsync(takerAddress, erc721TakerAssetIds[0], { from: tokenOwner }), - erc721Token.mint.sendTransactionAsync(takerAddress, erc721TakerAssetIds[1], { from: tokenOwner }), - ]); }); beforeEach(async () => { await blockchainLifecycle.startAsync(); @@ -208,7 +155,7 @@ describe('Exchange', () => { describe('fillOrder', () => { beforeEach(async () => { - balances = await dmyBalances.getAsync(); + erc20Balances = await erc20Wrapper.getBalancesAsync(); signedOrder = orderFactory.newSignedOrder(); }); @@ -218,27 +165,27 @@ describe('Exchange', () => { takerAssetAmount: new BigNumber(3), }); - const takerAssetFilledAmountBefore = await exWrapper.getTakerAssetFilledAmountAsync( + const takerAssetFilledAmountBefore = await exchangeWrapper.getTakerAssetFilledAmountAsync( orderUtils.getOrderHashHex(signedOrder), ); expect(takerAssetFilledAmountBefore).to.be.bignumber.equal(0); const fillTakerAssetAmount1 = new BigNumber(2); - await exWrapper.fillOrderAsync(signedOrder, takerAddress, { + await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount: fillTakerAssetAmount1, }); - const takerAssetFilledAmountAfter1 = await exWrapper.getTakerAssetFilledAmountAsync( + const takerAssetFilledAmountAfter1 = await exchangeWrapper.getTakerAssetFilledAmountAsync( orderUtils.getOrderHashHex(signedOrder), ); expect(takerAssetFilledAmountAfter1).to.be.bignumber.equal(fillTakerAssetAmount1); const fillTakerAssetAmount2 = new BigNumber(1); - await exWrapper.fillOrderAsync(signedOrder, takerAddress, { + await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount: fillTakerAssetAmount2, }); - const takerAssetFilledAmountAfter2 = await exWrapper.getTakerAssetFilledAmountAsync( + const takerAssetFilledAmountAfter2 = await exchangeWrapper.getTakerAssetFilledAmountAsync( orderUtils.getOrderHashHex(signedOrder), ); expect(takerAssetFilledAmountAfter2).to.be.bignumber.equal(takerAssetFilledAmountAfter1); @@ -250,20 +197,20 @@ describe('Exchange', () => { takerAssetAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), }); - const takerAssetFilledAmountBefore = await exWrapper.getTakerAssetFilledAmountAsync( + const takerAssetFilledAmountBefore = await exchangeWrapper.getTakerAssetFilledAmountAsync( orderUtils.getOrderHashHex(signedOrder), ); expect(takerAssetFilledAmountBefore).to.be.bignumber.equal(0); const takerAssetFillAmount = signedOrder.takerAssetAmount.div(2); - await exWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }); + await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }); - const makerAmountBoughtAfter = await exWrapper.getTakerAssetFilledAmountAsync( + const makerAmountBoughtAfter = await exchangeWrapper.getTakerAssetFilledAmountAsync( orderUtils.getOrderHashHex(signedOrder), ); expect(makerAmountBoughtAfter).to.be.bignumber.equal(takerAssetFillAmount); - const newBalances = await dmyBalances.getAsync(); + const newBalances = await erc20Wrapper.getBalancesAsync(); const makerAssetFilledAmount = takerAssetFillAmount .times(signedOrder.makerAssetAmount) @@ -275,25 +222,25 @@ describe('Exchange', () => { .times(makerAssetFilledAmount) .dividedToIntegerBy(signedOrder.makerAssetAmount); expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFilledAmount), + erc20Balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFilledAmount), ); expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), + erc20Balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), ); expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - balances[makerAddress][zrx.address].minus(makerFeePaid), + erc20Balances[makerAddress][zrx.address].minus(makerFeePaid), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), + erc20Balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), ); expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), + erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), ); expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - balances[takerAddress][zrx.address].minus(takerFeePaid), + erc20Balances[takerAddress][zrx.address].minus(takerFeePaid), ); expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - balances[feeRecipientAddress][zrx.address].add(makerFeePaid.add(takerFeePaid)), + erc20Balances[feeRecipientAddress][zrx.address].add(makerFeePaid.add(takerFeePaid)), ); }); @@ -303,20 +250,20 @@ describe('Exchange', () => { takerAssetAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), }); - const takerAssetFilledAmountBefore = await exWrapper.getTakerAssetFilledAmountAsync( + const takerAssetFilledAmountBefore = await exchangeWrapper.getTakerAssetFilledAmountAsync( orderUtils.getOrderHashHex(signedOrder), ); expect(takerAssetFilledAmountBefore).to.be.bignumber.equal(0); const takerAssetFillAmount = signedOrder.takerAssetAmount.div(2); - await exWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }); + await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }); - const makerAmountBoughtAfter = await exWrapper.getTakerAssetFilledAmountAsync( + const makerAmountBoughtAfter = await exchangeWrapper.getTakerAssetFilledAmountAsync( orderUtils.getOrderHashHex(signedOrder), ); expect(makerAmountBoughtAfter).to.be.bignumber.equal(takerAssetFillAmount); - const newBalances = await dmyBalances.getAsync(); + const newBalances = await erc20Wrapper.getBalancesAsync(); const makerAssetFilledAmount = takerAssetFillAmount .times(signedOrder.makerAssetAmount) @@ -328,25 +275,25 @@ describe('Exchange', () => { .times(makerAssetFilledAmount) .dividedToIntegerBy(signedOrder.makerAssetAmount); expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFilledAmount), + erc20Balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFilledAmount), ); expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), + erc20Balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), ); expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - balances[makerAddress][zrx.address].minus(makerFeePaid), + erc20Balances[makerAddress][zrx.address].minus(makerFeePaid), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), + erc20Balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), ); expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), + erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), ); expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - balances[takerAddress][zrx.address].minus(takerFeePaid), + erc20Balances[takerAddress][zrx.address].minus(takerFeePaid), ); expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - balances[feeRecipientAddress][zrx.address].add(makerFeePaid.add(takerFeePaid)), + erc20Balances[feeRecipientAddress][zrx.address].add(makerFeePaid.add(takerFeePaid)), ); }); @@ -356,20 +303,20 @@ describe('Exchange', () => { takerAssetAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), }); - const takerAssetFilledAmountBefore = await exWrapper.getTakerAssetFilledAmountAsync( + const takerAssetFilledAmountBefore = await exchangeWrapper.getTakerAssetFilledAmountAsync( orderUtils.getOrderHashHex(signedOrder), ); expect(takerAssetFilledAmountBefore).to.be.bignumber.equal(0); const takerAssetFillAmount = signedOrder.takerAssetAmount.div(2); - await exWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }); + await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }); - const makerAmountBoughtAfter = await exWrapper.getTakerAssetFilledAmountAsync( + const makerAmountBoughtAfter = await exchangeWrapper.getTakerAssetFilledAmountAsync( orderUtils.getOrderHashHex(signedOrder), ); expect(makerAmountBoughtAfter).to.be.bignumber.equal(takerAssetFillAmount); - const newBalances = await dmyBalances.getAsync(); + const newBalances = await erc20Wrapper.getBalancesAsync(); const makerAssetFilledAmount = takerAssetFillAmount .times(signedOrder.makerAssetAmount) @@ -381,25 +328,25 @@ describe('Exchange', () => { .times(makerAssetFilledAmount) .dividedToIntegerBy(signedOrder.makerAssetAmount); expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFilledAmount), + erc20Balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFilledAmount), ); expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), + erc20Balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), ); expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - balances[makerAddress][zrx.address].minus(makerFeePaid), + erc20Balances[makerAddress][zrx.address].minus(makerFeePaid), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), + erc20Balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), ); expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), + erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), ); expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - balances[takerAddress][zrx.address].minus(takerFeePaid), + erc20Balances[takerAddress][zrx.address].minus(takerFeePaid), ); expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - balances[feeRecipientAddress][zrx.address].add(makerFeePaid.add(takerFeePaid)), + erc20Balances[feeRecipientAddress][zrx.address].add(makerFeePaid.add(takerFeePaid)), ); }); @@ -410,21 +357,21 @@ describe('Exchange', () => { takerAssetAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), }); - const takerAssetFilledAmountBefore = await exWrapper.getTakerAssetFilledAmountAsync( + const takerAssetFilledAmountBefore = await exchangeWrapper.getTakerAssetFilledAmountAsync( orderUtils.getOrderHashHex(signedOrder), ); expect(takerAssetFilledAmountBefore).to.be.bignumber.equal(0); const takerAssetFillAmount = signedOrder.takerAssetAmount.div(2); - await exWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }); + await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }); - const makerAmountBoughtAfter = await exWrapper.getTakerAssetFilledAmountAsync( + const makerAmountBoughtAfter = await exchangeWrapper.getTakerAssetFilledAmountAsync( orderUtils.getOrderHashHex(signedOrder), ); const expectedMakerAmountBoughtAfter = takerAssetFillAmount.add(takerAssetFilledAmountBefore); expect(makerAmountBoughtAfter).to.be.bignumber.equal(expectedMakerAmountBoughtAfter); - const newBalances = await dmyBalances.getAsync(); + const newBalances = await erc20Wrapper.getBalancesAsync(); const makerAssetFilledAmount = takerAssetFillAmount .times(signedOrder.makerAssetAmount) @@ -436,67 +383,67 @@ describe('Exchange', () => { .times(makerAssetFilledAmount) .dividedToIntegerBy(signedOrder.makerAssetAmount); expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFilledAmount), + erc20Balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFilledAmount), ); expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), + erc20Balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), ); expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - balances[makerAddress][zrx.address].minus(makerFeePaid), + erc20Balances[makerAddress][zrx.address].minus(makerFeePaid), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), + erc20Balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), ); expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), + erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), ); expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - balances[takerAddress][zrx.address].minus(takerFeePaid), + erc20Balances[takerAddress][zrx.address].minus(takerFeePaid), ); expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - balances[feeRecipientAddress][zrx.address].add(makerFeePaid.add(takerFeePaid)), + erc20Balances[feeRecipientAddress][zrx.address].add(makerFeePaid.add(takerFeePaid)), ); }); it('should fill remaining value if takerAssetFillAmount > remaining takerAssetAmount', async () => { const takerAssetFillAmount = signedOrder.takerAssetAmount.div(2); - await exWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }); + await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }); - const res = await exWrapper.fillOrderAsync(signedOrder, takerAddress, { + const res = await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount: signedOrder.takerAssetAmount, }); const log = res.logs[0] as LogWithDecodedArgs; expect(log.args.takerAssetFilledAmount).to.be.bignumber.equal( signedOrder.takerAssetAmount.minus(takerAssetFillAmount), ); - const newBalances = await dmyBalances.getAsync(); + const newBalances = await erc20Wrapper.getBalancesAsync(); expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultMakerAssetAddress].minus(signedOrder.makerAssetAmount), + erc20Balances[makerAddress][defaultMakerAssetAddress].minus(signedOrder.makerAssetAmount), ); expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultTakerAssetAddress].add(signedOrder.takerAssetAmount), + erc20Balances[makerAddress][defaultTakerAssetAddress].add(signedOrder.takerAssetAmount), ); expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - balances[makerAddress][zrx.address].minus(signedOrder.makerFee), + erc20Balances[makerAddress][zrx.address].minus(signedOrder.makerFee), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultTakerAssetAddress].minus(signedOrder.takerAssetAmount), + erc20Balances[takerAddress][defaultTakerAssetAddress].minus(signedOrder.takerAssetAmount), ); expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultMakerAssetAddress].add(signedOrder.makerAssetAmount), + erc20Balances[takerAddress][defaultMakerAssetAddress].add(signedOrder.makerAssetAmount), ); expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - balances[takerAddress][zrx.address].minus(signedOrder.takerFee), + erc20Balances[takerAddress][zrx.address].minus(signedOrder.takerFee), ); expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - balances[feeRecipientAddress][zrx.address].add(signedOrder.makerFee.add(signedOrder.takerFee)), + erc20Balances[feeRecipientAddress][zrx.address].add(signedOrder.makerFee.add(signedOrder.takerFee)), ); }); it('should log 1 event with the correct arguments when order has a feeRecipient', async () => { const divisor = 2; - const res = await exWrapper.fillOrderAsync(signedOrder, takerAddress, { + const res = await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount: signedOrder.takerAssetAmount.div(divisor), }); expect(res.logs).to.have.length(1); @@ -525,7 +472,7 @@ describe('Exchange', () => { feeRecipientAddress: ZeroEx.NULL_ADDRESS, }); const divisor = 2; - const res = await exWrapper.fillOrderAsync(signedOrder, takerAddress, { + const res = await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount: signedOrder.takerAssetAmount.div(divisor), }); expect(res.logs).to.have.length(1); @@ -555,7 +502,9 @@ describe('Exchange', () => { makerAssetAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), takerAssetAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), }); - return expect(exWrapper.fillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith(constants.REVERT); + return expect(exchangeWrapper.fillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith( + constants.REVERT, + ); }); it('should throw if signature is invalid', async () => { @@ -569,7 +518,9 @@ describe('Exchange', () => { const invalidSigBuff = Buffer.concat([ethUtil.toBuffer(signatureTypeAndV), invalidR, invalidS]); const invalidSigHex = `0x${invalidSigBuff.toString('hex')}`; signedOrder.signature = invalidSigHex; - return expect(exWrapper.fillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith(constants.REVERT); + return expect(exchangeWrapper.fillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith( + constants.REVERT, + ); }); it('should throw if makerAssetAmount is 0', async () => { @@ -577,7 +528,9 @@ describe('Exchange', () => { makerAssetAmount: new BigNumber(0), }); - return expect(exWrapper.fillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith(constants.REVERT); + return expect(exchangeWrapper.fillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith( + constants.REVERT, + ); }); it('should throw if takerAssetAmount is 0', async () => { @@ -585,63 +538,69 @@ describe('Exchange', () => { takerAssetAmount: new BigNumber(0), }); - return expect(exWrapper.fillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith(constants.REVERT); + return expect(exchangeWrapper.fillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith( + constants.REVERT, + ); }); it('should throw if takerAssetFillAmount is 0', async () => { signedOrder = orderFactory.newSignedOrder(); return expect( - exWrapper.fillOrderAsync(signedOrder, takerAddress, { + exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount: new BigNumber(0), }), ).to.be.rejectedWith(constants.REVERT); }); - it('should throw if maker balances are too low to fill order', async () => { + it('should throw if maker erc20Balances are too low to fill order', async () => { signedOrder = orderFactory.newSignedOrder({ makerAssetAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), }); - return expect(exWrapper.fillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith(constants.REVERT); + return expect(exchangeWrapper.fillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith( + constants.REVERT, + ); }); - it('should throw if taker balances are too low to fill order', async () => { + it('should throw if taker erc20Balances are too low to fill order', async () => { signedOrder = orderFactory.newSignedOrder({ takerAssetAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), }); - return expect(exWrapper.fillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith(constants.REVERT); + return expect(exchangeWrapper.fillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith( + constants.REVERT, + ); }); it('should throw if maker allowances are too low to fill order', async () => { - await rep.approve.sendTransactionAsync(erc20Proxy.address, new BigNumber(0), { + await erc20TokenA.approve.sendTransactionAsync(erc20Proxy.address, new BigNumber(0), { from: makerAddress, }); - expect(exWrapper.fillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith(constants.REVERT); - await rep.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_ALLOWANCE, { + expect(exchangeWrapper.fillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith(constants.REVERT); + await erc20TokenA.approve.sendTransactionAsync(erc20Proxy.address, constants.INITIAL_ERC20_ALLOWANCE, { from: makerAddress, }); }); it('should throw if taker allowances are too low to fill order', async () => { - await dgd.approve.sendTransactionAsync(erc20Proxy.address, new BigNumber(0), { + await erc20TokenB.approve.sendTransactionAsync(erc20Proxy.address, new BigNumber(0), { from: takerAddress, }); - expect(exWrapper.fillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith(constants.REVERT); - await dgd.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_ALLOWANCE, { + expect(exchangeWrapper.fillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith(constants.REVERT); + await erc20TokenB.approve.sendTransactionAsync(erc20Proxy.address, constants.INITIAL_ERC20_ALLOWANCE, { from: takerAddress, }); }); - it('should not change balances if an order is expired', async () => { + it('should not change erc20Balances if an order is expired', async () => { signedOrder = orderFactory.newSignedOrder({ expirationTimeSeconds: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), }); - await exWrapper.fillOrderAsync(signedOrder, takerAddress); + await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances).to.be.deep.equal(erc20Balances); }); it('should log an error event if an order is expired', async () => { @@ -649,7 +608,7 @@ describe('Exchange', () => { expirationTimeSeconds: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), }); - const res = await exWrapper.fillOrderAsync(signedOrder, takerAddress); + const res = await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress); expect(res.logs).to.have.length(1); const log = res.logs[0] as LogWithDecodedArgs; const errCode = log.args.errorId; @@ -658,9 +617,9 @@ describe('Exchange', () => { it('should log an error event if no value is filled', async () => { signedOrder = orderFactory.newSignedOrder({}); - await exWrapper.fillOrderAsync(signedOrder, takerAddress); + await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress); - const res = await exWrapper.fillOrderAsync(signedOrder, takerAddress); + const res = await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress); expect(res.logs).to.have.length(1); const log = res.logs[0] as LogWithDecodedArgs; const errCode = log.args.errorId; @@ -670,12 +629,14 @@ describe('Exchange', () => { describe('cancelOrder', () => { beforeEach(async () => { - balances = await dmyBalances.getAsync(); + erc20Balances = await erc20Wrapper.getBalancesAsync(); signedOrder = orderFactory.newSignedOrder(); }); it('should throw if not sent by maker', async () => { - return expect(exWrapper.cancelOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith(constants.REVERT); + return expect(exchangeWrapper.cancelOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith( + constants.REVERT, + ); }); it('should throw if makerAssetAmount is 0', async () => { @@ -683,7 +644,9 @@ describe('Exchange', () => { makerAssetAmount: new BigNumber(0), }); - return expect(exWrapper.cancelOrderAsync(signedOrder, makerAddress)).to.be.rejectedWith(constants.REVERT); + return expect(exchangeWrapper.cancelOrderAsync(signedOrder, makerAddress)).to.be.rejectedWith( + constants.REVERT, + ); }); it('should throw if takerAssetAmount is 0', async () => { @@ -691,22 +654,24 @@ describe('Exchange', () => { takerAssetAmount: new BigNumber(0), }); - return expect(exWrapper.cancelOrderAsync(signedOrder, makerAddress)).to.be.rejectedWith(constants.REVERT); + return expect(exchangeWrapper.cancelOrderAsync(signedOrder, makerAddress)).to.be.rejectedWith( + constants.REVERT, + ); }); it('should be able to cancel a full order', async () => { - await exWrapper.cancelOrderAsync(signedOrder, makerAddress); - await exWrapper.fillOrderAsync(signedOrder, takerAddress, { + await exchangeWrapper.cancelOrderAsync(signedOrder, makerAddress); + await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount: signedOrder.takerAssetAmount.div(2), }); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances).to.be.deep.equal(erc20Balances); }); it('should log 1 event with correct arguments', async () => { const divisor = 2; - const res = await exWrapper.cancelOrderAsync(signedOrder, makerAddress); + const res = await exchangeWrapper.cancelOrderAsync(signedOrder, makerAddress); expect(res.logs).to.have.length(1); const log = res.logs[0] as LogWithDecodedArgs; @@ -720,9 +685,9 @@ describe('Exchange', () => { }); it('should log an error if already cancelled', async () => { - await exWrapper.cancelOrderAsync(signedOrder, makerAddress); + await exchangeWrapper.cancelOrderAsync(signedOrder, makerAddress); - const res = await exWrapper.cancelOrderAsync(signedOrder, makerAddress); + const res = await exchangeWrapper.cancelOrderAsync(signedOrder, makerAddress); expect(res.logs).to.have.length(1); const log = res.logs[0] as LogWithDecodedArgs; const errCode = log.args.errorId; @@ -734,7 +699,7 @@ describe('Exchange', () => { expirationTimeSeconds: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), }); - const res = await exWrapper.cancelOrderAsync(signedOrder, makerAddress); + const res = await exchangeWrapper.cancelOrderAsync(signedOrder, makerAddress); expect(res.logs).to.have.length(1); const log = res.logs[0] as LogWithDecodedArgs; const errCode = log.args.errorId; @@ -745,17 +710,17 @@ describe('Exchange', () => { describe('cancelOrdersUpTo', () => { it('should fail to set makerEpoch less than current makerEpoch', async () => { const makerEpoch = new BigNumber(1); - await exWrapper.cancelOrdersUpToAsync(makerEpoch, makerAddress); + await exchangeWrapper.cancelOrdersUpToAsync(makerEpoch, makerAddress); const lesserMakerEpoch = new BigNumber(0); - return expect(exWrapper.cancelOrdersUpToAsync(lesserMakerEpoch, makerAddress)).to.be.rejectedWith( + return expect(exchangeWrapper.cancelOrdersUpToAsync(lesserMakerEpoch, makerAddress)).to.be.rejectedWith( constants.REVERT, ); }); it('should fail to set makerEpoch equal to existing makerEpoch', async () => { const makerEpoch = new BigNumber(1); - await exWrapper.cancelOrdersUpToAsync(makerEpoch, makerAddress); - return expect(exWrapper.cancelOrdersUpToAsync(makerEpoch, makerAddress)).to.be.rejectedWith( + await exchangeWrapper.cancelOrdersUpToAsync(makerEpoch, makerAddress); + return expect(exchangeWrapper.cancelOrdersUpToAsync(makerEpoch, makerAddress)).to.be.rejectedWith( constants.REVERT, ); }); @@ -763,11 +728,11 @@ describe('Exchange', () => { it('should cancel only orders with a makerEpoch less than existing makerEpoch', async () => { // Cancel all transactions with a makerEpoch less than 1 const makerEpoch = new BigNumber(1); - await exWrapper.cancelOrdersUpToAsync(makerEpoch, makerAddress); + await exchangeWrapper.cancelOrdersUpToAsync(makerEpoch, makerAddress); // Create 3 orders with makerEpoch values: 0,1,2,3 // Since we cancelled with makerEpoch=1, orders with makerEpoch<=1 will not be processed - balances = await dmyBalances.getAsync(); + erc20Balances = await erc20Wrapper.getBalancesAsync(); const signedOrders = await Promise.all([ orderFactory.newSignedOrder({ makerAssetAmount: ZeroEx.toBaseUnitAmount(new BigNumber(9), 18), @@ -790,33 +755,33 @@ describe('Exchange', () => { salt: new BigNumber(3), }), ]); - await exWrapper.batchFillOrdersNoThrowAsync(signedOrders, takerAddress); + await exchangeWrapper.batchFillOrdersNoThrowAsync(signedOrders, takerAddress); - const newBalances = await dmyBalances.getAsync(); + const newBalances = await erc20Wrapper.getBalancesAsync(); const fillMakerAssetAmount = signedOrders[2].makerAssetAmount.add(signedOrders[3].makerAssetAmount); const fillTakerAssetAmount = signedOrders[2].takerAssetAmount.add(signedOrders[3].takerAssetAmount); const makerFee = signedOrders[2].makerFee.add(signedOrders[3].makerFee); const takerFee = signedOrders[2].takerFee.add(signedOrders[3].takerFee); expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultMakerAssetAddress].minus(fillMakerAssetAmount), + erc20Balances[makerAddress][defaultMakerAssetAddress].minus(fillMakerAssetAmount), ); expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultTakerAssetAddress].add(fillTakerAssetAmount), + erc20Balances[makerAddress][defaultTakerAssetAddress].add(fillTakerAssetAmount), ); expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - balances[makerAddress][zrx.address].minus(makerFee), + erc20Balances[makerAddress][zrx.address].minus(makerFee), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultTakerAssetAddress].minus(fillTakerAssetAmount), + erc20Balances[takerAddress][defaultTakerAssetAddress].minus(fillTakerAssetAmount), ); expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultMakerAssetAddress].add(fillMakerAssetAmount), + erc20Balances[takerAddress][defaultMakerAssetAddress].add(fillMakerAssetAmount), ); expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - balances[takerAddress][zrx.address].minus(takerFee), + erc20Balances[takerAddress][zrx.address].minus(takerFee), ); expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), + erc20Balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), ); }); }); @@ -839,7 +804,7 @@ describe('Exchange', () => { expect(initialOwnerTakerAsset).to.be.bignumber.equal(takerAddress); // Call Exchange const takerAssetFillAmount = signedOrder.takerAssetAmount; - const res = await exWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }); + const res = await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }); // Verify post-conditions const newOwnerMakerAsset = await erc721Token.ownerOf.callAsync(makerAssetId); expect(newOwnerMakerAsset).to.be.bignumber.equal(takerAddress); @@ -865,7 +830,7 @@ describe('Exchange', () => { // Call Exchange const takerAssetFillAmount = signedOrder.takerAssetAmount; return expect( - exWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }), + exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }), ).to.be.rejectedWith(constants.REVERT); }); @@ -887,7 +852,7 @@ describe('Exchange', () => { // Call Exchange const takerAssetFillAmount = signedOrder.takerAssetAmount; return expect( - exWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }), + exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }), ).to.be.rejectedWith(constants.REVERT); }); @@ -909,7 +874,7 @@ describe('Exchange', () => { // Call Exchange const takerAssetFillAmount = signedOrder.takerAssetAmount; return expect( - exWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }), + exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }), ).to.be.rejectedWith(constants.REVERT); }); @@ -931,7 +896,7 @@ describe('Exchange', () => { // Call Exchange const takerAssetFillAmount = signedOrder.takerAssetAmount; return expect( - exWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }), + exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }), ).to.be.rejectedWith(constants.REVERT); }); @@ -953,7 +918,7 @@ describe('Exchange', () => { // Call Exchange const takerAssetFillAmount = signedOrder.takerAssetAmount; return expect( - exWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }), + exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }), ).to.be.rejectedWith(constants.REVERT); }); @@ -970,28 +935,28 @@ describe('Exchange', () => { const initialOwnerMakerAsset = await erc721Token.ownerOf.callAsync(makerAssetId); expect(initialOwnerMakerAsset).to.be.bignumber.equal(makerAddress); // Call Exchange - balances = await dmyBalances.getAsync(); + erc20Balances = await erc20Wrapper.getBalancesAsync(); const takerAssetFillAmount = signedOrder.takerAssetAmount; - await exWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }); + await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }); // Verify ERC721 token was transferred from Maker to Taker const newOwnerMakerAsset = await erc721Token.ownerOf.callAsync(makerAssetId); expect(newOwnerMakerAsset).to.be.bignumber.equal(takerAddress); // Verify ERC20 tokens were transferred from Taker to Maker & fees were paid correctly - const newBalances = await dmyBalances.getAsync(); + const newBalances = await erc20Wrapper.getBalancesAsync(); expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), + erc20Balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), + erc20Balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), ); expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - balances[makerAddress][zrx.address].minus(signedOrder.makerFee), + erc20Balances[makerAddress][zrx.address].minus(signedOrder.makerFee), ); expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - balances[takerAddress][zrx.address].minus(signedOrder.takerFee), + erc20Balances[takerAddress][zrx.address].minus(signedOrder.takerFee), ); expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - balances[feeRecipientAddress][zrx.address].add(signedOrder.makerFee.add(signedOrder.takerFee)), + erc20Balances[feeRecipientAddress][zrx.address].add(signedOrder.makerFee.add(signedOrder.takerFee)), ); }); @@ -1008,28 +973,28 @@ describe('Exchange', () => { const initialOwnerTakerAsset = await erc721Token.ownerOf.callAsync(takerAssetId); expect(initialOwnerTakerAsset).to.be.bignumber.equal(takerAddress); // Call Exchange - balances = await dmyBalances.getAsync(); + erc20Balances = await erc20Wrapper.getBalancesAsync(); const takerAssetFillAmount = signedOrder.takerAssetAmount; - await exWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }); + await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount }); // Verify ERC721 token was transferred from Taker to Maker const newOwnerTakerAsset = await erc721Token.ownerOf.callAsync(takerAssetId); expect(newOwnerTakerAsset).to.be.bignumber.equal(makerAddress); // Verify ERC20 tokens were transferred from Maker to Taker & fees were paid correctly - const newBalances = await dmyBalances.getAsync(); + const newBalances = await erc20Wrapper.getBalancesAsync(); expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultMakerAssetAddress].add(signedOrder.makerAssetAmount), + erc20Balances[takerAddress][defaultMakerAssetAddress].add(signedOrder.makerAssetAmount), ); expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultMakerAssetAddress].minus(signedOrder.makerAssetAmount), + erc20Balances[makerAddress][defaultMakerAssetAddress].minus(signedOrder.makerAssetAmount), ); expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - balances[makerAddress][zrx.address].minus(signedOrder.makerFee), + erc20Balances[makerAddress][zrx.address].minus(signedOrder.makerFee), ); expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - balances[takerAddress][zrx.address].minus(signedOrder.takerFee), + erc20Balances[takerAddress][zrx.address].minus(signedOrder.takerFee), ); expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - balances[feeRecipientAddress][zrx.address].add(signedOrder.makerFee.add(signedOrder.takerFee)), + erc20Balances[feeRecipientAddress][zrx.address].add(signedOrder.makerFee.add(signedOrder.takerFee)), ); }); }); diff --git a/packages/contracts/test/exchange/helpers.ts b/packages/contracts/test/exchange/helpers.ts index 3c4431647..a5c2ca8d5 100644 --- a/packages/contracts/test/exchange/helpers.ts +++ b/packages/contracts/test/exchange/helpers.ts @@ -1,12 +1,11 @@ import { ZeroEx } from '0x.js'; -import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; +import { BlockchainLifecycle } from '@0xproject/dev-utils'; import { BigNumber } from '@0xproject/utils'; -import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; import ethUtil = require('ethereumjs-util'); -import { ERC20ProxyContract } from '../../src/contract_wrappers/generated/e_r_c20_proxy'; import { ExchangeContract } from '../../src/contract_wrappers/generated/exchange'; +import { addressUtils } from '../../src/utils/address_utils'; import { assetProxyUtils } from '../../src/utils/asset_proxy_utils'; import { constants } from '../../src/utils/constants'; import { ExchangeWrapper } from '../../src/utils/exchange_wrapper'; @@ -22,56 +21,42 @@ const expect = chai.expect; const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); -describe('Exchange', () => { - let makerAddress: string; - let feeRecipientAddress: string; - +describe('Exchange helpers', () => { let signedOrder: SignedOrder; let exchangeWrapper: ExchangeWrapper; let orderFactory: OrderFactory; before(async () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); - [makerAddress, feeRecipientAddress] = accounts; - const owner = accounts[0]; - const tokenRegistry = await deployer.deployAsync(ContractName.TokenRegistry); - const [rep, dgd, zrx] = await Promise.all([ - deployer.deployAsync(ContractName.DummyERC20Token, constants.DUMMY_TOKEN_ARGS), - deployer.deployAsync(ContractName.DummyERC20Token, constants.DUMMY_TOKEN_ARGS), - deployer.deployAsync(ContractName.DummyERC20Token, constants.DUMMY_TOKEN_ARGS), - ]); - const assetProxyDispatcher = await deployer.deployAsync(ContractName.AssetProxyDispatcher); - // Deploy and configure Exchange + const makerAddress = accounts[0]; const exchangeInstance = await deployer.deployAsync(ContractName.Exchange, [ - assetProxyDispatcher.address, + ZeroEx.NULL_ADDRESS, AssetProxyId.ERC20, ]); const exchange = new ExchangeContract(exchangeInstance.abi, exchangeInstance.address, provider); - await assetProxyDispatcher.addAuthorizedAddress.sendTransactionAsync(exchange.address, { from: owner }); const zeroEx = new ZeroEx(provider, { networkId: constants.TESTRPC_NETWORK_ID }); exchangeWrapper = new ExchangeWrapper(exchange, zeroEx); + const defaultOrderParams = { + ...constants.STATIC_ORDER_PARAMS, exchangeAddress: exchange.address, makerAddress, - feeRecipientAddress, - 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), - makerAssetData: assetProxyUtils.encodeERC20ProxyData(rep.address), - takerAssetData: assetProxyUtils.encodeERC20ProxyData(dgd.address), + feeRecipientAddress: addressUtils.generatePseudoRandomAddress(), + makerAssetData: assetProxyUtils.encodeERC20ProxyData(addressUtils.generatePseudoRandomAddress()), + takerAssetData: assetProxyUtils.encodeERC20ProxyData(addressUtils.generatePseudoRandomAddress()), }; - const privateKey = constants.TESTRPC_PRIVATE_KEYS[0]; + const privateKey = constants.TESTRPC_PRIVATE_KEYS[accounts.indexOf(makerAddress)]; orderFactory = new OrderFactory(privateKey, defaultOrderParams); - signedOrder = orderFactory.newSignedOrder(); }); beforeEach(async () => { await blockchainLifecycle.startAsync(); + signedOrder = orderFactory.newSignedOrder(); }); afterEach(async () => { await blockchainLifecycle.revertAsync(); }); + describe('getOrderHash', () => { it('should output the correct orderHash', async () => { const orderHashHex = await exchangeWrapper.getOrderHashAsync(signedOrder); diff --git a/packages/contracts/test/exchange/wrapper.ts b/packages/contracts/test/exchange/wrapper.ts index 88755ee03..60f47373e 100644 --- a/packages/contracts/test/exchange/wrapper.ts +++ b/packages/contracts/test/exchange/wrapper.ts @@ -14,11 +14,18 @@ import { ERC721ProxyContract } from '../../src/contract_wrappers/generated/e_r_c import { ExchangeContract } from '../../src/contract_wrappers/generated/exchange'; import { TokenRegistryContract } from '../../src/contract_wrappers/generated/token_registry'; import { assetProxyUtils } from '../../src/utils/asset_proxy_utils'; -import { Balances } from '../../src/utils/balances'; import { constants } from '../../src/utils/constants'; +import { ERC20Wrapper } from '../../src/utils/erc20_wrapper'; +import { ERC721Wrapper } from '../../src/utils/erc721_wrapper'; import { ExchangeWrapper } from '../../src/utils/exchange_wrapper'; import { OrderFactory } from '../../src/utils/order_factory'; -import { AssetProxyId, BalancesByOwner, ContractName, SignedOrder } from '../../src/utils/types'; +import { + AssetProxyId, + ContractName, + ERC20BalancesByOwner, + ERC721BalancesByOwner, + SignedOrder, +} from '../../src/utils/types'; import { chaiSetup } from '../utils/chai_setup'; import { deployer } from '../utils/deployer'; import { provider, web3Wrapper } from '../utils/web3_wrapper'; @@ -27,67 +34,59 @@ chaiSetup.configure(); const expect = chai.expect; const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); -describe('Exchange', () => { +describe('Exchange wrappers', () => { let makerAddress: string; - let tokenOwner: string; + let owner: string; let takerAddress: string; let feeRecipientAddress: string; - const INITIAL_BALANCE = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); - const INITIAL_ALLOWANCE = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); - - let rep: DummyERC20TokenContract; - let dgd: DummyERC20TokenContract; + let erc20TokenA: DummyERC20TokenContract; + let erc20TokenB: DummyERC20TokenContract; let zrx: DummyERC20TokenContract; let erc721Token: DummyERC721TokenContract; let exchange: ExchangeContract; - let tokenRegistry: TokenRegistryContract; let assetProxyDispatcher: AssetProxyDispatcherContract; let erc20Proxy: ERC20ProxyContract; let erc721Proxy: ERC721ProxyContract; - let balances: BalancesByOwner; - - let exWrapper: ExchangeWrapper; - let dmyBalances: Balances; + let exchangeWrapper: ExchangeWrapper; + let erc20Wrapper: ERC20Wrapper; + let erc721Wrapper: ERC721Wrapper; + let erc20Balances: ERC20BalancesByOwner; let orderFactory: OrderFactory; - const erc721MakerAssetId = new BigNumber('0x1010101010101010101010101010101010101010101010101010101010101010'); - const erc721TakerAssetId = new BigNumber('0x3030303030303030303030303030303030303030303030303030303030303030'); + let erc721MakerAssetId: BigNumber; + let erc721TakerAssetId: BigNumber; let defaultMakerAssetAddress: string; let defaultTakerAssetAddress: string; + let zeroEx: ZeroEx; + before(async () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); - tokenOwner = accounts[0]; - [makerAddress, takerAddress, feeRecipientAddress] = accounts; - const owner = tokenOwner; - const [repInstance, dgdInstance, zrxInstance, erc721TokenInstance] = await Promise.all([ - deployer.deployAsync(ContractName.DummyERC20Token, constants.DUMMY_TOKEN_ARGS), - deployer.deployAsync(ContractName.DummyERC20Token, constants.DUMMY_TOKEN_ARGS), - deployer.deployAsync(ContractName.DummyERC20Token, constants.DUMMY_TOKEN_ARGS), - deployer.deployAsync(ContractName.DummyERC721Token, constants.DUMMY_ERC721TOKEN_ARGS), - ]); - rep = new DummyERC20TokenContract(repInstance.abi, repInstance.address, provider); - dgd = new DummyERC20TokenContract(dgdInstance.abi, dgdInstance.address, provider); - zrx = new DummyERC20TokenContract(zrxInstance.abi, zrxInstance.address, provider); - erc721Token = new DummyERC721TokenContract(erc721TokenInstance.abi, erc721TokenInstance.address, provider); - const tokenRegistryInstance = await deployer.deployAsync(ContractName.TokenRegistry); - tokenRegistry = new TokenRegistryContract(tokenRegistryInstance.abi, tokenRegistryInstance.address, provider); - // Deploy Asset Proxy Dispatcher + const usedAddresses = ([owner, makerAddress, takerAddress, feeRecipientAddress] = accounts); + + erc20Wrapper = new ERC20Wrapper(deployer, provider, usedAddresses, owner); + erc721Wrapper = new ERC721Wrapper(deployer, provider, usedAddresses, owner); + + [erc20TokenA, erc20TokenB, zrx] = await erc20Wrapper.deployDummyERC20TokensAsync(); + erc20Proxy = await erc20Wrapper.deployERC20ProxyAsync(); + await erc20Wrapper.setBalancesAndAllowancesAsync(); + + [erc721Token] = await erc721Wrapper.deployDummyERC721TokensAsync(); + erc721Proxy = await erc721Wrapper.deployERC721ProxyAsync(); + await erc721Wrapper.setBalancesAndAllowancesAsync(); + const erc721Balances = await erc721Wrapper.getBalancesAsync(); + erc721MakerAssetId = erc721Balances[makerAddress][erc721Token.address][0]; + erc721TakerAssetId = erc721Balances[takerAddress][erc721Token.address][0]; + const assetProxyDispatcherInstance = await deployer.deployAsync(ContractName.AssetProxyDispatcher); assetProxyDispatcher = new AssetProxyDispatcherContract( assetProxyDispatcherInstance.abi, assetProxyDispatcherInstance.address, provider, ); - // Deploy ERC20 Proxy - const erc20ProxyInstance = await deployer.deployAsync(ContractName.ERC20Proxy); - erc20Proxy = new ERC20ProxyContract(erc20ProxyInstance.abi, erc20ProxyInstance.address, provider); - await erc20Proxy.addAuthorizedAddress.sendTransactionAsync(assetProxyDispatcher.address, { - from: owner, - }); const prevERC20ProxyAddress = ZeroEx.NULL_ADDRESS; await assetProxyDispatcher.registerAssetProxy.sendTransactionAsync( AssetProxyId.ERC20, @@ -95,12 +94,6 @@ describe('Exchange', () => { prevERC20ProxyAddress, { from: owner }, ); - // Deploy ERC721 Proxy - const erc721ProxyInstance = await deployer.deployAsync(ContractName.ERC721Proxy); - erc721Proxy = new ERC721ProxyContract(erc721ProxyInstance.abi, erc721ProxyInstance.address, provider); - await erc721Proxy.addAuthorizedAddress.sendTransactionAsync(assetProxyDispatcher.address, { - from: owner, - }); const prevERC721ProxyAddress = ZeroEx.NULL_ADDRESS; await assetProxyDispatcher.registerAssetProxy.sendTransactionAsync( AssetProxyId.ERC721, @@ -108,60 +101,42 @@ describe('Exchange', () => { prevERC721ProxyAddress, { from: owner }, ); - // Deploy and configure Exchange + await erc20Proxy.addAuthorizedAddress.sendTransactionAsync(assetProxyDispatcher.address, { + from: owner, + }); + await erc721Proxy.addAuthorizedAddress.sendTransactionAsync(assetProxyDispatcher.address, { + from: owner, + }); + const exchangeInstance = await deployer.deployAsync(ContractName.Exchange, [ assetProxyDispatcher.address, assetProxyUtils.encodeERC20ProxyData(zrx.address), ]); exchange = new ExchangeContract(exchangeInstance.abi, exchangeInstance.address, provider); await assetProxyDispatcher.addAuthorizedAddress.sendTransactionAsync(exchange.address, { from: owner }); + zeroEx = new ZeroEx(provider, { + exchangeContractAddress: exchange.address, + networkId: constants.TESTRPC_NETWORK_ID, + }); + exchangeWrapper = new ExchangeWrapper(exchange, zeroEx); - const zeroEx = new ZeroEx(provider, { networkId: constants.TESTRPC_NETWORK_ID }); - exWrapper = new ExchangeWrapper(exchange, zeroEx); + defaultMakerAssetAddress = erc20TokenA.address; + defaultTakerAssetAddress = erc20TokenB.address; - defaultMakerAssetAddress = rep.address; - defaultTakerAssetAddress = dgd.address; const defaultOrderParams = { + ...constants.STATIC_ORDER_PARAMS, exchangeAddress: exchange.address, makerAddress, feeRecipientAddress, - 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), makerAssetData: assetProxyUtils.encodeERC20ProxyData(defaultMakerAssetAddress), takerAssetData: assetProxyUtils.encodeERC20ProxyData(defaultTakerAssetAddress), }; - - const privateKey = constants.TESTRPC_PRIVATE_KEYS[0]; + const privateKey = constants.TESTRPC_PRIVATE_KEYS[accounts.indexOf(makerAddress)]; orderFactory = new OrderFactory(privateKey, defaultOrderParams); - dmyBalances = new Balances([rep, dgd, zrx], [makerAddress, takerAddress, feeRecipientAddress]); - await Promise.all([ - rep.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_ALLOWANCE, { from: makerAddress }), - rep.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_ALLOWANCE, { from: takerAddress }), - rep.setBalance.sendTransactionAsync(makerAddress, INITIAL_BALANCE, { from: tokenOwner }), - rep.setBalance.sendTransactionAsync(takerAddress, INITIAL_BALANCE, { from: tokenOwner }), - dgd.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_ALLOWANCE, { from: makerAddress }), - dgd.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_ALLOWANCE, { from: takerAddress }), - dgd.setBalance.sendTransactionAsync(makerAddress, INITIAL_BALANCE, { from: tokenOwner }), - dgd.setBalance.sendTransactionAsync(takerAddress, INITIAL_BALANCE, { from: tokenOwner }), - zrx.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_ALLOWANCE, { from: makerAddress }), - zrx.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_ALLOWANCE, { from: takerAddress }), - zrx.setBalance.sendTransactionAsync(makerAddress, INITIAL_BALANCE, { from: tokenOwner }), - zrx.setBalance.sendTransactionAsync(takerAddress, INITIAL_BALANCE, { from: tokenOwner }), - erc721Token.setApprovalForAll.sendTransactionAsync(erc721Proxy.address, true, { - from: makerAddress, - }), - erc721Token.setApprovalForAll.sendTransactionAsync(erc721Proxy.address, true, { - from: takerAddress, - }), - erc721Token.mint.sendTransactionAsync(makerAddress, erc721MakerAssetId, { from: tokenOwner }), - erc721Token.mint.sendTransactionAsync(takerAddress, erc721TakerAssetId, { from: tokenOwner }), - ]); }); beforeEach(async () => { await blockchainLifecycle.startAsync(); - balances = await dmyBalances.getAsync(); + erc20Balances = await erc20Wrapper.getBalancesAsync(); }); afterEach(async () => { await blockchainLifecycle.revertAsync(); @@ -173,11 +148,11 @@ describe('Exchange', () => { takerAssetAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), }); const takerAssetFillAmount = signedOrder.takerAssetAmount.div(2); - await exWrapper.fillOrKillOrderAsync(signedOrder, takerAddress, { + await exchangeWrapper.fillOrKillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount, }); - const newBalances = await dmyBalances.getAsync(); + const newBalances = await erc20Wrapper.getBalancesAsync(); const makerAssetFilledAmount = takerAssetFillAmount .times(signedOrder.makerAssetAmount) @@ -189,25 +164,25 @@ describe('Exchange', () => { .times(makerAssetFilledAmount) .dividedToIntegerBy(signedOrder.makerAssetAmount); expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFilledAmount), + erc20Balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFilledAmount), ); expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), + erc20Balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), ); expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - balances[makerAddress][zrx.address].minus(makerFee), + erc20Balances[makerAddress][zrx.address].minus(makerFee), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), + erc20Balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), ); expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), + erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), ); expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - balances[takerAddress][zrx.address].minus(takerFee), + erc20Balances[takerAddress][zrx.address].minus(takerFee), ); expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), + erc20Balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), ); }); @@ -216,7 +191,7 @@ describe('Exchange', () => { expirationTimeSeconds: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), }); - return expect(exWrapper.fillOrKillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith( + return expect(exchangeWrapper.fillOrKillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith( constants.REVERT, ); }); @@ -224,11 +199,11 @@ describe('Exchange', () => { it('should throw if entire takerAssetFillAmount not filled', async () => { const signedOrder = orderFactory.newSignedOrder(); - await exWrapper.fillOrderAsync(signedOrder, takerAddress, { + await exchangeWrapper.fillOrderAsync(signedOrder, takerAddress, { takerAssetFillAmount: signedOrder.takerAssetAmount.div(2), }); - return expect(exWrapper.fillOrKillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith( + return expect(exchangeWrapper.fillOrKillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith( constants.REVERT, ); }); @@ -241,11 +216,11 @@ describe('Exchange', () => { takerAssetAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), }); const takerAssetFillAmount = signedOrder.takerAssetAmount.div(2); - await exWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress, { + await exchangeWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress, { takerAssetFillAmount, }); - const newBalances = await dmyBalances.getAsync(); + const newBalances = await erc20Wrapper.getBalancesAsync(); const makerAssetFilledAmount = takerAssetFillAmount .times(signedOrder.makerAssetAmount) @@ -257,122 +232,122 @@ describe('Exchange', () => { .times(makerAssetFilledAmount) .dividedToIntegerBy(signedOrder.makerAssetAmount); expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFilledAmount), + erc20Balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFilledAmount), ); expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), + erc20Balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), ); expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - balances[makerAddress][zrx.address].minus(makerFee), + erc20Balances[makerAddress][zrx.address].minus(makerFee), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), + erc20Balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), ); expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), + erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), ); expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - balances[takerAddress][zrx.address].minus(takerFee), + erc20Balances[takerAddress][zrx.address].minus(takerFee), ); expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), + erc20Balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), ); }); - it('should not change balances if maker balances are too low to fill order', async () => { + it('should not change erc20Balances if maker erc20Balances are too low to fill order', async () => { const signedOrder = orderFactory.newSignedOrder({ makerAssetAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), }); - await exWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); + await exchangeWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances).to.be.deep.equal(erc20Balances); }); - it('should not change balances if taker balances are too low to fill order', async () => { + it('should not change erc20Balances if taker erc20Balances are too low to fill order', async () => { const signedOrder = orderFactory.newSignedOrder({ takerAssetAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), }); - await exWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); + await exchangeWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances).to.be.deep.equal(erc20Balances); }); - it('should not change balances if maker allowances are too low to fill order', async () => { + it('should not change erc20Balances if maker allowances are too low to fill order', async () => { const signedOrder = orderFactory.newSignedOrder(); - await rep.approve.sendTransactionAsync(erc20Proxy.address, new BigNumber(0), { + await erc20TokenA.approve.sendTransactionAsync(erc20Proxy.address, new BigNumber(0), { from: makerAddress, }); - await exWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); - await rep.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_ALLOWANCE, { + await exchangeWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); + await erc20TokenA.approve.sendTransactionAsync(erc20Proxy.address, constants.INITIAL_ERC20_ALLOWANCE, { from: makerAddress, }); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances).to.be.deep.equal(erc20Balances); }); - it('should not change balances if taker allowances are too low to fill order', async () => { + it('should not change erc20Balances if taker allowances are too low to fill order', async () => { const signedOrder = orderFactory.newSignedOrder(); - await dgd.approve.sendTransactionAsync(erc20Proxy.address, new BigNumber(0), { + await erc20TokenB.approve.sendTransactionAsync(erc20Proxy.address, new BigNumber(0), { from: takerAddress, }); - await exWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); - await dgd.approve.sendTransactionAsync(erc20Proxy.address, INITIAL_ALLOWANCE, { + await exchangeWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); + await erc20TokenB.approve.sendTransactionAsync(erc20Proxy.address, constants.INITIAL_ERC20_ALLOWANCE, { from: takerAddress, }); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances).to.be.deep.equal(erc20Balances); }); - it('should not change balances if makerAssetAddress is ZRX, makerAssetAmount + makerFee > maker balance', async () => { - const makerZRXBalance = new BigNumber(balances[makerAddress][zrx.address]); + it('should not change erc20Balances if makerAssetAddress is ZRX, makerAssetAmount + makerFee > maker balance', async () => { + const makerZRXBalance = new BigNumber(erc20Balances[makerAddress][zrx.address]); const signedOrder = orderFactory.newSignedOrder({ makerAssetAmount: makerZRXBalance, makerFee: new BigNumber(1), makerAssetData: assetProxyUtils.encodeERC20ProxyData(zrx.address), }); - await exWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); + await exchangeWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances).to.be.deep.equal(erc20Balances); }); - it('should not change balances if makerAssetAddress is ZRX, makerAssetAmount + makerFee > maker allowance', async () => { + it('should not change erc20Balances if makerAssetAddress is ZRX, makerAssetAmount + makerFee > maker allowance', async () => { const makerZRXAllowance = await zrx.allowance.callAsync(makerAddress, erc20Proxy.address); const signedOrder = orderFactory.newSignedOrder({ makerAssetAmount: new BigNumber(makerZRXAllowance), makerFee: new BigNumber(1), makerAssetData: assetProxyUtils.encodeERC20ProxyData(zrx.address), }); - await exWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); + await exchangeWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances).to.be.deep.equal(erc20Balances); }); - it('should not change balances if takerAssetAddress is ZRX, takerAssetAmount + takerFee > taker balance', async () => { - const takerZRXBalance = new BigNumber(balances[takerAddress][zrx.address]); + it('should not change erc20Balances if takerAssetAddress is ZRX, takerAssetAmount + takerFee > taker balance', async () => { + const takerZRXBalance = new BigNumber(erc20Balances[takerAddress][zrx.address]); const signedOrder = orderFactory.newSignedOrder({ takerAssetAmount: takerZRXBalance, takerFee: new BigNumber(1), takerAssetData: assetProxyUtils.encodeERC20ProxyData(zrx.address), }); - await exWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); + await exchangeWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances).to.be.deep.equal(erc20Balances); }); - it('should not change balances if takerAssetAddress is ZRX, takerAssetAmount + takerFee > taker allowance', async () => { + it('should not change erc20Balances if takerAssetAddress is ZRX, takerAssetAmount + takerFee > taker allowance', async () => { const takerZRXAllowance = await zrx.allowance.callAsync(takerAddress, erc20Proxy.address); const signedOrder = orderFactory.newSignedOrder({ takerAssetAmount: new BigNumber(takerZRXAllowance), takerFee: new BigNumber(1), takerAssetData: assetProxyUtils.encodeERC20ProxyData(zrx.address), }); - await exWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); + await exchangeWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances).to.be.deep.equal(erc20Balances); }); it('should successfully exchange ERC721 tokens', async () => { @@ -392,7 +367,7 @@ describe('Exchange', () => { expect(initialOwnerTakerAsset).to.be.bignumber.equal(takerAddress); // Call Exchange const takerAssetFillAmount = signedOrder.takerAssetAmount; - await exWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress, { takerAssetFillAmount }); + await exchangeWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress, { takerAssetFillAmount }); // Verify post-conditions const newOwnerMakerAsset = await erc721Token.ownerOf.callAsync(makerAssetId); expect(newOwnerMakerAsset).to.be.bignumber.equal(takerAddress); @@ -414,8 +389,8 @@ describe('Exchange', () => { describe('batchFillOrders', () => { it('should transfer the correct amounts', async () => { const takerAssetFillAmounts: BigNumber[] = []; - const makerAssetAddress = rep.address; - const takerAssetAddress = dgd.address; + const makerAssetAddress = erc20TokenA.address; + const takerAssetAddress = erc20TokenB.address; _.forEach(signedOrders, signedOrder => { const takerAssetFillAmount = signedOrder.takerAssetAmount.div(2); const makerAssetFilledAmount = takerAssetFillAmount @@ -428,39 +403,39 @@ describe('Exchange', () => { .times(makerAssetFilledAmount) .dividedToIntegerBy(signedOrder.makerAssetAmount); takerAssetFillAmounts.push(takerAssetFillAmount); - balances[makerAddress][makerAssetAddress] = balances[makerAddress][makerAssetAddress].minus( - makerAssetFilledAmount, - ); - balances[makerAddress][takerAssetAddress] = balances[makerAddress][takerAssetAddress].add( + erc20Balances[makerAddress][makerAssetAddress] = erc20Balances[makerAddress][ + makerAssetAddress + ].minus(makerAssetFilledAmount); + erc20Balances[makerAddress][takerAssetAddress] = erc20Balances[makerAddress][takerAssetAddress].add( takerAssetFillAmount, ); - balances[makerAddress][zrx.address] = balances[makerAddress][zrx.address].minus(makerFee); - balances[takerAddress][makerAssetAddress] = balances[takerAddress][makerAssetAddress].add( + erc20Balances[makerAddress][zrx.address] = erc20Balances[makerAddress][zrx.address].minus(makerFee); + erc20Balances[takerAddress][makerAssetAddress] = erc20Balances[takerAddress][makerAssetAddress].add( makerAssetFilledAmount, ); - balances[takerAddress][takerAssetAddress] = balances[takerAddress][takerAssetAddress].minus( - takerAssetFillAmount, - ); - balances[takerAddress][zrx.address] = balances[takerAddress][zrx.address].minus(takerFee); - balances[feeRecipientAddress][zrx.address] = balances[feeRecipientAddress][zrx.address].add( - makerFee.add(takerFee), - ); + erc20Balances[takerAddress][takerAssetAddress] = erc20Balances[takerAddress][ + takerAssetAddress + ].minus(takerAssetFillAmount); + erc20Balances[takerAddress][zrx.address] = erc20Balances[takerAddress][zrx.address].minus(takerFee); + erc20Balances[feeRecipientAddress][zrx.address] = erc20Balances[feeRecipientAddress][ + zrx.address + ].add(makerFee.add(takerFee)); }); - await exWrapper.batchFillOrdersAsync(signedOrders, takerAddress, { + await exchangeWrapper.batchFillOrdersAsync(signedOrders, takerAddress, { takerAssetFillAmounts, }); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances).to.be.deep.equal(erc20Balances); }); }); describe('batchFillOrKillOrders', () => { it('should transfer the correct amounts', async () => { const takerAssetFillAmounts: BigNumber[] = []; - const makerAssetAddress = rep.address; - const takerAssetAddress = dgd.address; + const makerAssetAddress = erc20TokenA.address; + const takerAssetAddress = erc20TokenB.address; _.forEach(signedOrders, signedOrder => { const takerAssetFillAmount = signedOrder.takerAssetAmount.div(2); const makerAssetFilledAmount = takerAssetFillAmount @@ -473,31 +448,31 @@ describe('Exchange', () => { .times(makerAssetFilledAmount) .dividedToIntegerBy(signedOrder.makerAssetAmount); takerAssetFillAmounts.push(takerAssetFillAmount); - balances[makerAddress][makerAssetAddress] = balances[makerAddress][makerAssetAddress].minus( - makerAssetFilledAmount, - ); - balances[makerAddress][takerAssetAddress] = balances[makerAddress][takerAssetAddress].add( + erc20Balances[makerAddress][makerAssetAddress] = erc20Balances[makerAddress][ + makerAssetAddress + ].minus(makerAssetFilledAmount); + erc20Balances[makerAddress][takerAssetAddress] = erc20Balances[makerAddress][takerAssetAddress].add( takerAssetFillAmount, ); - balances[makerAddress][zrx.address] = balances[makerAddress][zrx.address].minus(makerFee); - balances[takerAddress][makerAssetAddress] = balances[takerAddress][makerAssetAddress].add( + erc20Balances[makerAddress][zrx.address] = erc20Balances[makerAddress][zrx.address].minus(makerFee); + erc20Balances[takerAddress][makerAssetAddress] = erc20Balances[takerAddress][makerAssetAddress].add( makerAssetFilledAmount, ); - balances[takerAddress][takerAssetAddress] = balances[takerAddress][takerAssetAddress].minus( - takerAssetFillAmount, - ); - balances[takerAddress][zrx.address] = balances[takerAddress][zrx.address].minus(takerFee); - balances[feeRecipientAddress][zrx.address] = balances[feeRecipientAddress][zrx.address].add( - makerFee.add(takerFee), - ); + erc20Balances[takerAddress][takerAssetAddress] = erc20Balances[takerAddress][ + takerAssetAddress + ].minus(takerAssetFillAmount); + erc20Balances[takerAddress][zrx.address] = erc20Balances[takerAddress][zrx.address].minus(takerFee); + erc20Balances[feeRecipientAddress][zrx.address] = erc20Balances[feeRecipientAddress][ + zrx.address + ].add(makerFee.add(takerFee)); }); - await exWrapper.batchFillOrKillOrdersAsync(signedOrders, takerAddress, { + await exchangeWrapper.batchFillOrKillOrdersAsync(signedOrders, takerAddress, { takerAssetFillAmounts, }); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances).to.be.deep.equal(erc20Balances); }); it('should throw if a single signedOrder does not fill the expected amount', async () => { @@ -507,10 +482,10 @@ describe('Exchange', () => { takerAssetFillAmounts.push(takerAssetFillAmount); }); - await exWrapper.fillOrKillOrderAsync(signedOrders[0], takerAddress); + await exchangeWrapper.fillOrKillOrderAsync(signedOrders[0], takerAddress); return expect( - exWrapper.batchFillOrKillOrdersAsync(signedOrders, takerAddress, { + exchangeWrapper.batchFillOrKillOrdersAsync(signedOrders, takerAddress, { takerAssetFillAmounts, }), ).to.be.rejectedWith(constants.REVERT); @@ -520,8 +495,8 @@ describe('Exchange', () => { describe('batchFillOrdersNoThrow', async () => { it('should transfer the correct amounts', async () => { const takerAssetFillAmounts: BigNumber[] = []; - const makerAssetAddress = rep.address; - const takerAssetAddress = dgd.address; + const makerAssetAddress = erc20TokenA.address; + const takerAssetAddress = erc20TokenB.address; _.forEach(signedOrders, signedOrder => { const takerAssetFillAmount = signedOrder.takerAssetAmount.div(2); const makerAssetFilledAmount = takerAssetFillAmount @@ -534,37 +509,37 @@ describe('Exchange', () => { .times(makerAssetFilledAmount) .dividedToIntegerBy(signedOrder.makerAssetAmount); takerAssetFillAmounts.push(takerAssetFillAmount); - balances[makerAddress][makerAssetAddress] = balances[makerAddress][makerAssetAddress].minus( - makerAssetFilledAmount, - ); - balances[makerAddress][takerAssetAddress] = balances[makerAddress][takerAssetAddress].add( + erc20Balances[makerAddress][makerAssetAddress] = erc20Balances[makerAddress][ + makerAssetAddress + ].minus(makerAssetFilledAmount); + erc20Balances[makerAddress][takerAssetAddress] = erc20Balances[makerAddress][takerAssetAddress].add( takerAssetFillAmount, ); - balances[makerAddress][zrx.address] = balances[makerAddress][zrx.address].minus(makerFee); - balances[takerAddress][makerAssetAddress] = balances[takerAddress][makerAssetAddress].add( + erc20Balances[makerAddress][zrx.address] = erc20Balances[makerAddress][zrx.address].minus(makerFee); + erc20Balances[takerAddress][makerAssetAddress] = erc20Balances[takerAddress][makerAssetAddress].add( makerAssetFilledAmount, ); - balances[takerAddress][takerAssetAddress] = balances[takerAddress][takerAssetAddress].minus( - takerAssetFillAmount, - ); - balances[takerAddress][zrx.address] = balances[takerAddress][zrx.address].minus(takerFee); - balances[feeRecipientAddress][zrx.address] = balances[feeRecipientAddress][zrx.address].add( - makerFee.add(takerFee), - ); + erc20Balances[takerAddress][takerAssetAddress] = erc20Balances[takerAddress][ + takerAssetAddress + ].minus(takerAssetFillAmount); + erc20Balances[takerAddress][zrx.address] = erc20Balances[takerAddress][zrx.address].minus(takerFee); + erc20Balances[feeRecipientAddress][zrx.address] = erc20Balances[feeRecipientAddress][ + zrx.address + ].add(makerFee.add(takerFee)); }); - await exWrapper.batchFillOrdersNoThrowAsync(signedOrders, takerAddress, { + await exchangeWrapper.batchFillOrdersNoThrowAsync(signedOrders, takerAddress, { takerAssetFillAmounts, }); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances).to.be.deep.equal(erc20Balances); }); it('should not throw if an order is invalid and fill the remaining orders', async () => { const takerAssetFillAmounts: BigNumber[] = []; - const makerAssetAddress = rep.address; - const takerAssetAddress = dgd.address; + const makerAssetAddress = erc20TokenA.address; + const takerAssetAddress = erc20TokenB.address; const invalidOrder = { ...signedOrders[0], @@ -585,32 +560,32 @@ describe('Exchange', () => { .times(makerAssetFilledAmount) .dividedToIntegerBy(signedOrder.makerAssetAmount); takerAssetFillAmounts.push(takerAssetFillAmount); - balances[makerAddress][makerAssetAddress] = balances[makerAddress][makerAssetAddress].minus( - makerAssetFilledAmount, - ); - balances[makerAddress][takerAssetAddress] = balances[makerAddress][takerAssetAddress].add( + erc20Balances[makerAddress][makerAssetAddress] = erc20Balances[makerAddress][ + makerAssetAddress + ].minus(makerAssetFilledAmount); + erc20Balances[makerAddress][takerAssetAddress] = erc20Balances[makerAddress][takerAssetAddress].add( takerAssetFillAmount, ); - balances[makerAddress][zrx.address] = balances[makerAddress][zrx.address].minus(makerFee); - balances[takerAddress][makerAssetAddress] = balances[takerAddress][makerAssetAddress].add( + erc20Balances[makerAddress][zrx.address] = erc20Balances[makerAddress][zrx.address].minus(makerFee); + erc20Balances[takerAddress][makerAssetAddress] = erc20Balances[takerAddress][makerAssetAddress].add( makerAssetFilledAmount, ); - balances[takerAddress][takerAssetAddress] = balances[takerAddress][takerAssetAddress].minus( - takerAssetFillAmount, - ); - balances[takerAddress][zrx.address] = balances[takerAddress][zrx.address].minus(takerFee); - balances[feeRecipientAddress][zrx.address] = balances[feeRecipientAddress][zrx.address].add( - makerFee.add(takerFee), - ); + erc20Balances[takerAddress][takerAssetAddress] = erc20Balances[takerAddress][ + takerAssetAddress + ].minus(takerAssetFillAmount); + erc20Balances[takerAddress][zrx.address] = erc20Balances[takerAddress][zrx.address].minus(takerFee); + erc20Balances[feeRecipientAddress][zrx.address] = erc20Balances[feeRecipientAddress][ + zrx.address + ].add(makerFee.add(takerFee)); }); const newOrders = [invalidOrder, ...validOrders]; - await exWrapper.batchFillOrdersNoThrowAsync(newOrders, takerAddress, { + await exchangeWrapper.batchFillOrdersNoThrowAsync(newOrders, takerAddress, { takerAssetFillAmounts, }); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances).to.be.deep.equal(erc20Balances); }); }); @@ -619,11 +594,11 @@ describe('Exchange', () => { const takerAssetFillAmount = signedOrders[0].takerAssetAmount.plus( signedOrders[1].takerAssetAmount.div(2), ); - await exWrapper.marketSellOrdersAsync(signedOrders, takerAddress, { + await exchangeWrapper.marketSellOrdersAsync(signedOrders, takerAddress, { takerAssetFillAmount, }); - const newBalances = await dmyBalances.getAsync(); + const newBalances = await erc20Wrapper.getBalancesAsync(); const makerAssetFilledAmount = signedOrders[0].makerAssetAmount.add( signedOrders[1].makerAssetAmount.dividedToIntegerBy(2), @@ -631,59 +606,59 @@ describe('Exchange', () => { const makerFee = signedOrders[0].makerFee.add(signedOrders[1].makerFee.dividedToIntegerBy(2)); const takerFee = signedOrders[0].takerFee.add(signedOrders[1].takerFee.dividedToIntegerBy(2)); expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFilledAmount), + erc20Balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFilledAmount), ); expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), + erc20Balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), ); expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - balances[makerAddress][zrx.address].minus(makerFee), + erc20Balances[makerAddress][zrx.address].minus(makerFee), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), + erc20Balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), ); expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), + erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), ); expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - balances[takerAddress][zrx.address].minus(takerFee), + erc20Balances[takerAddress][zrx.address].minus(takerFee), ); expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), + erc20Balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), ); }); it('should fill all signedOrders if cannot fill entire takerAssetFillAmount', async () => { const takerAssetFillAmount = ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18); _.forEach(signedOrders, signedOrder => { - balances[makerAddress][defaultMakerAssetAddress] = balances[makerAddress][ + erc20Balances[makerAddress][defaultMakerAssetAddress] = erc20Balances[makerAddress][ defaultMakerAssetAddress ].minus(signedOrder.makerAssetAmount); - balances[makerAddress][defaultTakerAssetAddress] = balances[makerAddress][ + erc20Balances[makerAddress][defaultTakerAssetAddress] = erc20Balances[makerAddress][ defaultTakerAssetAddress ].add(signedOrder.takerAssetAmount); - balances[makerAddress][zrx.address] = balances[makerAddress][zrx.address].minus( + erc20Balances[makerAddress][zrx.address] = erc20Balances[makerAddress][zrx.address].minus( signedOrder.makerFee, ); - balances[takerAddress][defaultMakerAssetAddress] = balances[takerAddress][ + erc20Balances[takerAddress][defaultMakerAssetAddress] = erc20Balances[takerAddress][ defaultMakerAssetAddress ].add(signedOrder.makerAssetAmount); - balances[takerAddress][defaultTakerAssetAddress] = balances[takerAddress][ + erc20Balances[takerAddress][defaultTakerAssetAddress] = erc20Balances[takerAddress][ defaultTakerAssetAddress ].minus(signedOrder.takerAssetAmount); - balances[takerAddress][zrx.address] = balances[takerAddress][zrx.address].minus( + erc20Balances[takerAddress][zrx.address] = erc20Balances[takerAddress][zrx.address].minus( signedOrder.takerFee, ); - balances[feeRecipientAddress][zrx.address] = balances[feeRecipientAddress][zrx.address].add( - signedOrder.makerFee.add(signedOrder.takerFee), - ); + erc20Balances[feeRecipientAddress][zrx.address] = erc20Balances[feeRecipientAddress][ + zrx.address + ].add(signedOrder.makerFee.add(signedOrder.takerFee)); }); - await exWrapper.marketSellOrdersAsync(signedOrders, takerAddress, { + await exchangeWrapper.marketSellOrdersAsync(signedOrders, takerAddress, { takerAssetFillAmount, }); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances).to.be.deep.equal(erc20Balances); }); it('should throw when an signedOrder does not use the same takerAssetAddress', async () => { @@ -694,7 +669,7 @@ describe('Exchange', () => { ]; return expect( - exWrapper.marketSellOrdersAsync(signedOrders, takerAddress, { + exchangeWrapper.marketSellOrdersAsync(signedOrders, takerAddress, { takerAssetFillAmount: ZeroEx.toBaseUnitAmount(new BigNumber(1000), 18), }), ).to.be.rejectedWith(constants.REVERT); @@ -706,11 +681,11 @@ describe('Exchange', () => { const takerAssetFillAmount = signedOrders[0].takerAssetAmount.plus( signedOrders[1].takerAssetAmount.div(2), ); - await exWrapper.marketSellOrdersNoThrowAsync(signedOrders, takerAddress, { + await exchangeWrapper.marketSellOrdersNoThrowAsync(signedOrders, takerAddress, { takerAssetFillAmount, }); - const newBalances = await dmyBalances.getAsync(); + const newBalances = await erc20Wrapper.getBalancesAsync(); const makerAssetFilledAmount = signedOrders[0].makerAssetAmount.add( signedOrders[1].makerAssetAmount.dividedToIntegerBy(2), @@ -718,59 +693,59 @@ describe('Exchange', () => { const makerFee = signedOrders[0].makerFee.add(signedOrders[1].makerFee.dividedToIntegerBy(2)); const takerFee = signedOrders[0].takerFee.add(signedOrders[1].takerFee.dividedToIntegerBy(2)); expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFilledAmount), + erc20Balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFilledAmount), ); expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), + erc20Balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), ); expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - balances[makerAddress][zrx.address].minus(makerFee), + erc20Balances[makerAddress][zrx.address].minus(makerFee), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), + erc20Balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), ); expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), + erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), ); expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - balances[takerAddress][zrx.address].minus(takerFee), + erc20Balances[takerAddress][zrx.address].minus(takerFee), ); expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), + erc20Balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), ); }); it('should fill all signedOrders if cannot fill entire takerAssetFillAmount', async () => { const takerAssetFillAmount = ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18); _.forEach(signedOrders, signedOrder => { - balances[makerAddress][defaultMakerAssetAddress] = balances[makerAddress][ + erc20Balances[makerAddress][defaultMakerAssetAddress] = erc20Balances[makerAddress][ defaultMakerAssetAddress ].minus(signedOrder.makerAssetAmount); - balances[makerAddress][defaultTakerAssetAddress] = balances[makerAddress][ + erc20Balances[makerAddress][defaultTakerAssetAddress] = erc20Balances[makerAddress][ defaultTakerAssetAddress ].add(signedOrder.takerAssetAmount); - balances[makerAddress][zrx.address] = balances[makerAddress][zrx.address].minus( + erc20Balances[makerAddress][zrx.address] = erc20Balances[makerAddress][zrx.address].minus( signedOrder.makerFee, ); - balances[takerAddress][defaultMakerAssetAddress] = balances[takerAddress][ + erc20Balances[takerAddress][defaultMakerAssetAddress] = erc20Balances[takerAddress][ defaultMakerAssetAddress ].add(signedOrder.makerAssetAmount); - balances[takerAddress][defaultTakerAssetAddress] = balances[takerAddress][ + erc20Balances[takerAddress][defaultTakerAssetAddress] = erc20Balances[takerAddress][ defaultTakerAssetAddress ].minus(signedOrder.takerAssetAmount); - balances[takerAddress][zrx.address] = balances[takerAddress][zrx.address].minus( + erc20Balances[takerAddress][zrx.address] = erc20Balances[takerAddress][zrx.address].minus( signedOrder.takerFee, ); - balances[feeRecipientAddress][zrx.address] = balances[feeRecipientAddress][zrx.address].add( - signedOrder.makerFee.add(signedOrder.takerFee), - ); + erc20Balances[feeRecipientAddress][zrx.address] = erc20Balances[feeRecipientAddress][ + zrx.address + ].add(signedOrder.makerFee.add(signedOrder.takerFee)); }); - await exWrapper.marketSellOrdersNoThrowAsync(signedOrders, takerAddress, { + await exchangeWrapper.marketSellOrdersNoThrowAsync(signedOrders, takerAddress, { takerAssetFillAmount, }); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances).to.be.deep.equal(erc20Balances); }); it('should throw when a signedOrder does not use the same takerAssetAddress', async () => { @@ -781,7 +756,7 @@ describe('Exchange', () => { ]; return expect( - exWrapper.marketSellOrdersNoThrowAsync(signedOrders, takerAddress, { + exchangeWrapper.marketSellOrdersNoThrowAsync(signedOrders, takerAddress, { takerAssetFillAmount: ZeroEx.toBaseUnitAmount(new BigNumber(1000), 18), }), ).to.be.rejectedWith(constants.REVERT); @@ -793,11 +768,11 @@ describe('Exchange', () => { const makerAssetFillAmount = signedOrders[0].makerAssetAmount.plus( signedOrders[1].makerAssetAmount.div(2), ); - await exWrapper.marketBuyOrdersAsync(signedOrders, takerAddress, { + await exchangeWrapper.marketBuyOrdersAsync(signedOrders, takerAddress, { makerAssetFillAmount, }); - const newBalances = await dmyBalances.getAsync(); + const newBalances = await erc20Wrapper.getBalancesAsync(); const makerAmountBought = signedOrders[0].takerAssetAmount.add( signedOrders[1].takerAssetAmount.dividedToIntegerBy(2), @@ -805,59 +780,59 @@ describe('Exchange', () => { const makerFee = signedOrders[0].makerFee.add(signedOrders[1].makerFee.dividedToIntegerBy(2)); const takerFee = signedOrders[0].takerFee.add(signedOrders[1].takerFee.dividedToIntegerBy(2)); expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFillAmount), + erc20Balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFillAmount), ); expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultTakerAssetAddress].add(makerAmountBought), + erc20Balances[makerAddress][defaultTakerAssetAddress].add(makerAmountBought), ); expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - balances[makerAddress][zrx.address].minus(makerFee), + erc20Balances[makerAddress][zrx.address].minus(makerFee), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultTakerAssetAddress].minus(makerAmountBought), + erc20Balances[takerAddress][defaultTakerAssetAddress].minus(makerAmountBought), ); expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFillAmount), + erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFillAmount), ); expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - balances[takerAddress][zrx.address].minus(takerFee), + erc20Balances[takerAddress][zrx.address].minus(takerFee), ); expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), + erc20Balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), ); }); it('should fill all signedOrders if cannot fill entire makerAssetFillAmount', async () => { const makerAssetFillAmount = ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18); _.forEach(signedOrders, signedOrder => { - balances[makerAddress][defaultMakerAssetAddress] = balances[makerAddress][ + erc20Balances[makerAddress][defaultMakerAssetAddress] = erc20Balances[makerAddress][ defaultMakerAssetAddress ].minus(signedOrder.makerAssetAmount); - balances[makerAddress][defaultTakerAssetAddress] = balances[makerAddress][ + erc20Balances[makerAddress][defaultTakerAssetAddress] = erc20Balances[makerAddress][ defaultTakerAssetAddress ].add(signedOrder.takerAssetAmount); - balances[makerAddress][zrx.address] = balances[makerAddress][zrx.address].minus( + erc20Balances[makerAddress][zrx.address] = erc20Balances[makerAddress][zrx.address].minus( signedOrder.makerFee, ); - balances[takerAddress][defaultMakerAssetAddress] = balances[takerAddress][ + erc20Balances[takerAddress][defaultMakerAssetAddress] = erc20Balances[takerAddress][ defaultMakerAssetAddress ].add(signedOrder.makerAssetAmount); - balances[takerAddress][defaultTakerAssetAddress] = balances[takerAddress][ + erc20Balances[takerAddress][defaultTakerAssetAddress] = erc20Balances[takerAddress][ defaultTakerAssetAddress ].minus(signedOrder.takerAssetAmount); - balances[takerAddress][zrx.address] = balances[takerAddress][zrx.address].minus( + erc20Balances[takerAddress][zrx.address] = erc20Balances[takerAddress][zrx.address].minus( signedOrder.takerFee, ); - balances[feeRecipientAddress][zrx.address] = balances[feeRecipientAddress][zrx.address].add( - signedOrder.makerFee.add(signedOrder.takerFee), - ); + erc20Balances[feeRecipientAddress][zrx.address] = erc20Balances[feeRecipientAddress][ + zrx.address + ].add(signedOrder.makerFee.add(signedOrder.takerFee)); }); - await exWrapper.marketBuyOrdersAsync(signedOrders, takerAddress, { + await exchangeWrapper.marketBuyOrdersAsync(signedOrders, takerAddress, { makerAssetFillAmount, }); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances).to.be.deep.equal(erc20Balances); }); it('should throw when an signedOrder does not use the same makerAssetAddress', async () => { @@ -868,7 +843,7 @@ describe('Exchange', () => { ]; return expect( - exWrapper.marketBuyOrdersAsync(signedOrders, takerAddress, { + exchangeWrapper.marketBuyOrdersAsync(signedOrders, takerAddress, { makerAssetFillAmount: ZeroEx.toBaseUnitAmount(new BigNumber(1000), 18), }), ).to.be.rejectedWith(constants.REVERT); @@ -880,11 +855,11 @@ describe('Exchange', () => { const makerAssetFillAmount = signedOrders[0].makerAssetAmount.plus( signedOrders[1].makerAssetAmount.div(2), ); - await exWrapper.marketBuyOrdersNoThrowAsync(signedOrders, takerAddress, { + await exchangeWrapper.marketBuyOrdersNoThrowAsync(signedOrders, takerAddress, { makerAssetFillAmount, }); - const newBalances = await dmyBalances.getAsync(); + const newBalances = await erc20Wrapper.getBalancesAsync(); const makerAmountBought = signedOrders[0].takerAssetAmount.add( signedOrders[1].takerAssetAmount.dividedToIntegerBy(2), @@ -892,59 +867,59 @@ describe('Exchange', () => { const makerFee = signedOrders[0].makerFee.add(signedOrders[1].makerFee.dividedToIntegerBy(2)); const takerFee = signedOrders[0].takerFee.add(signedOrders[1].takerFee.dividedToIntegerBy(2)); expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFillAmount), + erc20Balances[makerAddress][defaultMakerAssetAddress].minus(makerAssetFillAmount), ); expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[makerAddress][defaultTakerAssetAddress].add(makerAmountBought), + erc20Balances[makerAddress][defaultTakerAssetAddress].add(makerAmountBought), ); expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - balances[makerAddress][zrx.address].minus(makerFee), + erc20Balances[makerAddress][zrx.address].minus(makerFee), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultTakerAssetAddress].minus(makerAmountBought), + erc20Balances[takerAddress][defaultTakerAssetAddress].minus(makerAmountBought), ); expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( - balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFillAmount), + erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFillAmount), ); expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - balances[takerAddress][zrx.address].minus(takerFee), + erc20Balances[takerAddress][zrx.address].minus(takerFee), ); expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), + erc20Balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), ); }); it('should fill all signedOrders if cannot fill entire takerAssetFillAmount', async () => { const takerAssetFillAmount = ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18); _.forEach(signedOrders, signedOrder => { - balances[makerAddress][defaultMakerAssetAddress] = balances[makerAddress][ + erc20Balances[makerAddress][defaultMakerAssetAddress] = erc20Balances[makerAddress][ defaultMakerAssetAddress ].minus(signedOrder.makerAssetAmount); - balances[makerAddress][defaultTakerAssetAddress] = balances[makerAddress][ + erc20Balances[makerAddress][defaultTakerAssetAddress] = erc20Balances[makerAddress][ defaultTakerAssetAddress ].add(signedOrder.takerAssetAmount); - balances[makerAddress][zrx.address] = balances[makerAddress][zrx.address].minus( + erc20Balances[makerAddress][zrx.address] = erc20Balances[makerAddress][zrx.address].minus( signedOrder.makerFee, ); - balances[takerAddress][defaultMakerAssetAddress] = balances[takerAddress][ + erc20Balances[takerAddress][defaultMakerAssetAddress] = erc20Balances[takerAddress][ defaultMakerAssetAddress ].add(signedOrder.makerAssetAmount); - balances[takerAddress][defaultTakerAssetAddress] = balances[takerAddress][ + erc20Balances[takerAddress][defaultTakerAssetAddress] = erc20Balances[takerAddress][ defaultTakerAssetAddress ].minus(signedOrder.takerAssetAmount); - balances[takerAddress][zrx.address] = balances[takerAddress][zrx.address].minus( + erc20Balances[takerAddress][zrx.address] = erc20Balances[takerAddress][zrx.address].minus( signedOrder.takerFee, ); - balances[feeRecipientAddress][zrx.address] = balances[feeRecipientAddress][zrx.address].add( - signedOrder.makerFee.add(signedOrder.takerFee), - ); + erc20Balances[feeRecipientAddress][zrx.address] = erc20Balances[feeRecipientAddress][ + zrx.address + ].add(signedOrder.makerFee.add(signedOrder.takerFee)); }); - await exWrapper.marketSellOrdersNoThrowAsync(signedOrders, takerAddress, { + await exchangeWrapper.marketSellOrdersNoThrowAsync(signedOrders, takerAddress, { takerAssetFillAmount, }); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(newBalances).to.be.deep.equal(erc20Balances); }); it('should throw when a signedOrder does not use the same makerAssetAddress', async () => { @@ -955,7 +930,7 @@ describe('Exchange', () => { ]; return expect( - exWrapper.marketBuyOrdersNoThrowAsync(signedOrders, takerAddress, { + exchangeWrapper.marketBuyOrdersNoThrowAsync(signedOrders, takerAddress, { makerAssetFillAmount: ZeroEx.toBaseUnitAmount(new BigNumber(1000), 18), }), ).to.be.rejectedWith(constants.REVERT); @@ -965,13 +940,13 @@ describe('Exchange', () => { describe('batchCancelOrders', () => { it('should be able to cancel multiple signedOrders', async () => { const takerAssetCancelAmounts = _.map(signedOrders, signedOrder => signedOrder.takerAssetAmount); - await exWrapper.batchCancelOrdersAsync(signedOrders, makerAddress); + await exchangeWrapper.batchCancelOrdersAsync(signedOrders, makerAddress); - await exWrapper.batchFillOrdersAsync(signedOrders, takerAddress, { + await exchangeWrapper.batchFillOrdersAsync(signedOrders, takerAddress, { takerAssetFillAmounts: takerAssetCancelAmounts, }); - const newBalances = await dmyBalances.getAsync(); - expect(balances).to.be.deep.equal(newBalances); + const newBalances = await erc20Wrapper.getBalancesAsync(); + expect(erc20Balances).to.be.deep.equal(newBalances); }); }); }); diff --git a/packages/contracts/test/unlimited_allowance_token.ts b/packages/contracts/test/unlimited_allowance_token.ts index f2284685f..f40708dbd 100644 --- a/packages/contracts/test/unlimited_allowance_token.ts +++ b/packages/contracts/test/unlimited_allowance_token.ts @@ -20,10 +20,9 @@ const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); describe('UnlimitedAllowanceToken', () => { let owner: string; let spender: string; - const config = { + const zeroEx = new ZeroEx(provider, { networkId: constants.TESTRPC_NETWORK_ID, - }; - const zeroEx = new ZeroEx(provider, config); + }); const MAX_MINT_VALUE = new BigNumber(100000000000000000000); let tokenAddress: string; @@ -33,7 +32,10 @@ describe('UnlimitedAllowanceToken', () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); owner = accounts[0]; spender = accounts[1]; - const tokenInstance = await deployer.deployAsync(ContractName.DummyERC20Token, constants.DUMMY_TOKEN_ARGS); + const tokenInstance = await deployer.deployAsync( + ContractName.DummyERC20Token, + constants.DUMMY_ERC20_TOKEN_ARGS, + ); token = new DummyERC20TokenContract(tokenInstance.abi, tokenInstance.address, provider); await token.mint.sendTransactionAsync(MAX_MINT_VALUE, { from: owner }); tokenAddress = token.address; -- 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') 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') 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 +- .../test/asset_proxy_dispatcher/dispatcher.ts | 22 +-- .../test/asset_proxy_dispatcher/proxies.ts | 36 ++-- packages/contracts/test/exchange/core.ts | 115 ++++++------ packages/contracts/test/exchange/wrapper.ts | 204 +++++++++++---------- packages/contracts/test/zrx_token.ts | 24 +-- 8 files changed, 308 insertions(+), 286 deletions(-) (limited to 'packages/contracts') 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[]; }; diff --git a/packages/contracts/test/asset_proxy_dispatcher/dispatcher.ts b/packages/contracts/test/asset_proxy_dispatcher/dispatcher.ts index b5630d807..6e0ec3dee 100644 --- a/packages/contracts/test/asset_proxy_dispatcher/dispatcher.ts +++ b/packages/contracts/test/asset_proxy_dispatcher/dispatcher.ts @@ -29,7 +29,7 @@ describe('AssetProxyDispatcher', () => { let makerAddress: string; let takerAddress: string; - let zrx: DummyERC20TokenContract; + let zrxToken: DummyERC20TokenContract; let erc20Proxy: ERC20ProxyContract; let erc721Proxy: ERC721ProxyContract; let assetProxyDispatcher: AssetProxyDispatcherContract; @@ -46,11 +46,11 @@ describe('AssetProxyDispatcher', () => { erc20Wrapper = new ERC20Wrapper(deployer, provider, usedAddresses, owner); erc721Wrapper = new ERC721Wrapper(deployer, provider, usedAddresses, owner); - [zrx] = await erc20Wrapper.deployDummyERC20TokensAsync(); - erc20Proxy = await erc20Wrapper.deployERC20ProxyAsync(); + [zrxToken] = await erc20Wrapper.deployDummyTokensAsync(); + erc20Proxy = await erc20Wrapper.deployProxyAsync(); await erc20Wrapper.setBalancesAndAllowancesAsync(); - erc721Proxy = await erc721Wrapper.deployERC721ProxyAsync(); + erc721Proxy = await erc721Wrapper.deployProxyAsync(); const assetProxyDispatcherInstance = await deployer.deployAsync(ContractName.AssetProxyDispatcher); assetProxyDispatcher = new AssetProxyDispatcherContract( @@ -230,7 +230,7 @@ describe('AssetProxyDispatcher', () => { { from: owner }, ); // Construct metadata for ERC20 proxy - const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrx.address); + const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrxToken.address); // Perform a transfer from makerAddress to takerAddress const erc20Balances = await erc20Wrapper.getBalancesAsync(); const amount = new BigNumber(10); @@ -243,17 +243,17 @@ describe('AssetProxyDispatcher', () => { ); // Verify transfer was successful const newBalances = await erc20Wrapper.getBalancesAsync(); - expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[makerAddress][zrx.address].minus(amount), + expect(newBalances[makerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrxToken.address].minus(amount), ); - expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[takerAddress][zrx.address].add(amount), + expect(newBalances[takerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrxToken.address].add(amount), ); }); it('should throw if dispatching to unregistered proxy', async () => { // Construct metadata for ERC20 proxy - const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrx.address); + const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrxToken.address); // Perform a transfer from makerAddress to takerAddress const erc20Balances = await erc20Wrapper.getBalancesAsync(); const amount = new BigNumber(10); @@ -278,7 +278,7 @@ describe('AssetProxyDispatcher', () => { { from: owner }, ); // Construct metadata for ERC20 proxy - const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrx.address); + const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrxToken.address); // Perform a transfer from makerAddress to takerAddress const erc20Balances = await erc20Wrapper.getBalancesAsync(); const amount = new BigNumber(10); diff --git a/packages/contracts/test/asset_proxy_dispatcher/proxies.ts b/packages/contracts/test/asset_proxy_dispatcher/proxies.ts index 830f40e13..14d96f023 100644 --- a/packages/contracts/test/asset_proxy_dispatcher/proxies.ts +++ b/packages/contracts/test/asset_proxy_dispatcher/proxies.ts @@ -29,7 +29,7 @@ describe('Asset Transfer Proxies', () => { let makerAddress: string; let takerAddress: string; - let zrx: DummyERC20TokenContract; + let zrxToken: DummyERC20TokenContract; let erc721Token: DummyERC721TokenContract; let erc20Proxy: ERC20ProxyContract; let erc721Proxy: ERC721ProxyContract; @@ -51,15 +51,15 @@ describe('Asset Transfer Proxies', () => { erc20Wrapper = new ERC20Wrapper(deployer, provider, usedAddresses, owner); erc721Wrapper = new ERC721Wrapper(deployer, provider, usedAddresses, owner); - [zrx] = await erc20Wrapper.deployDummyERC20TokensAsync(); - erc20Proxy = await erc20Wrapper.deployERC20ProxyAsync(); + [zrxToken] = await erc20Wrapper.deployDummyTokensAsync(); + erc20Proxy = await erc20Wrapper.deployProxyAsync(); await erc20Wrapper.setBalancesAndAllowancesAsync(); await erc20Proxy.addAuthorizedAddress.sendTransactionAsync(assetProxyDispatcherAddress, { from: owner, }); - [erc721Token] = await erc721Wrapper.deployDummyERC721TokensAsync(); - erc721Proxy = await erc721Wrapper.deployERC721ProxyAsync(); + [erc721Token] = await erc721Wrapper.deployDummyTokensAsync(); + erc721Proxy = await erc721Wrapper.deployProxyAsync(); await erc721Wrapper.setBalancesAndAllowancesAsync(); const erc721Balances = await erc721Wrapper.getBalancesAsync(); erc721MakerTokenId = erc721Balances[makerAddress][erc721Token.address][0]; @@ -76,7 +76,7 @@ describe('Asset Transfer Proxies', () => { describe('Transfer Proxy - ERC20', () => { it('should successfully transfer tokens', async () => { // Construct metadata for ERC20 proxy - const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrx.address); + const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrxToken.address); // Perform a transfer from makerAddress to takerAddress const erc20Balances = await erc20Wrapper.getBalancesAsync(); const amount = new BigNumber(10); @@ -89,17 +89,17 @@ describe('Asset Transfer Proxies', () => { ); // Verify transfer was successful const newBalances = await erc20Wrapper.getBalancesAsync(); - expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[makerAddress][zrx.address].minus(amount), + expect(newBalances[makerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrxToken.address].minus(amount), ); - expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[takerAddress][zrx.address].add(amount), + expect(newBalances[takerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrxToken.address].add(amount), ); }); it('should do nothing if transferring 0 amount of a token', async () => { // Construct metadata for ERC20 proxy - const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrx.address); + const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrxToken.address); // Perform a transfer from makerAddress to takerAddress const erc20Balances = await erc20Wrapper.getBalancesAsync(); const amount = new BigNumber(0); @@ -112,21 +112,21 @@ describe('Asset Transfer Proxies', () => { ); // Verify transfer was successful const newBalances = await erc20Wrapper.getBalancesAsync(); - expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[makerAddress][zrx.address], + expect(newBalances[makerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrxToken.address], ); - expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[takerAddress][zrx.address], + expect(newBalances[takerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrxToken.address], ); }); it('should throw if allowances are too low', async () => { // Construct metadata for ERC20 proxy - const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrx.address); + const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrxToken.address); // Create allowance less than transfer amount. Set allowance on proxy. const allowance = new BigNumber(0); const transferAmount = new BigNumber(10); - await zrx.approve.sendTransactionAsync(erc20Proxy.address, allowance, { + await zrxToken.approve.sendTransactionAsync(erc20Proxy.address, allowance, { from: makerAddress, }); // Perform a transfer; expect this to fail. @@ -143,7 +143,7 @@ describe('Asset Transfer Proxies', () => { it('should throw if requesting address is not authorized', async () => { // Construct metadata for ERC20 proxy - const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrx.address); + const encodedProxyMetadata = assetProxyUtils.encodeERC20ProxyData(zrxToken.address); // Perform a transfer from makerAddress to takerAddress const amount = new BigNumber(10); return expect( diff --git a/packages/contracts/test/exchange/core.ts b/packages/contracts/test/exchange/core.ts index 9065c5e3f..666486cc3 100644 --- a/packages/contracts/test/exchange/core.ts +++ b/packages/contracts/test/exchange/core.ts @@ -28,7 +28,6 @@ import { AssetProxyId, ContractName, ERC20BalancesByOwner, - ERC721BalancesByOwner, ExchangeContractErrs, SignedOrder, } from '../../src/utils/types'; @@ -48,7 +47,7 @@ describe('Exchange core', () => { let erc20TokenA: DummyERC20TokenContract; let erc20TokenB: DummyERC20TokenContract; - let zrx: DummyERC20TokenContract; + let zrxToken: DummyERC20TokenContract; let erc721Token: DummyERC721TokenContract; let exchange: ExchangeContract; let assetProxyDispatcher: AssetProxyDispatcherContract; @@ -77,12 +76,12 @@ describe('Exchange core', () => { erc20Wrapper = new ERC20Wrapper(deployer, provider, usedAddresses, owner); erc721Wrapper = new ERC721Wrapper(deployer, provider, usedAddresses, owner); - [erc20TokenA, erc20TokenB, zrx] = await erc20Wrapper.deployDummyERC20TokensAsync(); - erc20Proxy = await erc20Wrapper.deployERC20ProxyAsync(); + [erc20TokenA, erc20TokenB, zrxToken] = await erc20Wrapper.deployDummyTokensAsync(); + erc20Proxy = await erc20Wrapper.deployProxyAsync(); await erc20Wrapper.setBalancesAndAllowancesAsync(); - [erc721Token] = await erc721Wrapper.deployDummyERC721TokensAsync(); - erc721Proxy = await erc721Wrapper.deployERC721ProxyAsync(); + [erc721Token] = await erc721Wrapper.deployDummyTokensAsync(); + erc721Proxy = await erc721Wrapper.deployProxyAsync(); await erc721Wrapper.setBalancesAndAllowancesAsync(); const erc721Balances = await erc721Wrapper.getBalancesAsync(); erc721MakerAssetIds = erc721Balances[makerAddress][erc721Token.address]; @@ -117,7 +116,7 @@ describe('Exchange core', () => { const exchangeInstance = await deployer.deployAsync(ContractName.Exchange, [ assetProxyDispatcher.address, - assetProxyUtils.encodeERC20ProxyData(zrx.address), + assetProxyUtils.encodeERC20ProxyData(zrxToken.address), ]); exchange = new ExchangeContract(exchangeInstance.abi, exchangeInstance.address, provider); await assetProxyDispatcher.addAuthorizedAddress.sendTransactionAsync(exchange.address, { from: owner }); @@ -227,8 +226,8 @@ describe('Exchange core', () => { expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), ); - expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[makerAddress][zrx.address].minus(makerFeePaid), + expect(newBalances[makerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrxToken.address].minus(makerFeePaid), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), @@ -236,11 +235,11 @@ describe('Exchange core', () => { expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), ); - expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[takerAddress][zrx.address].minus(takerFeePaid), + expect(newBalances[takerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrxToken.address].minus(takerFeePaid), ); - expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[feeRecipientAddress][zrx.address].add(makerFeePaid.add(takerFeePaid)), + expect(newBalances[feeRecipientAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[feeRecipientAddress][zrxToken.address].add(makerFeePaid.add(takerFeePaid)), ); }); @@ -280,8 +279,8 @@ describe('Exchange core', () => { expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), ); - expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[makerAddress][zrx.address].minus(makerFeePaid), + expect(newBalances[makerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrxToken.address].minus(makerFeePaid), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), @@ -289,11 +288,11 @@ describe('Exchange core', () => { expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), ); - expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[takerAddress][zrx.address].minus(takerFeePaid), + expect(newBalances[takerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrxToken.address].minus(takerFeePaid), ); - expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[feeRecipientAddress][zrx.address].add(makerFeePaid.add(takerFeePaid)), + expect(newBalances[feeRecipientAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[feeRecipientAddress][zrxToken.address].add(makerFeePaid.add(takerFeePaid)), ); }); @@ -333,8 +332,8 @@ describe('Exchange core', () => { expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), ); - expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[makerAddress][zrx.address].minus(makerFeePaid), + expect(newBalances[makerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrxToken.address].minus(makerFeePaid), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), @@ -342,11 +341,11 @@ describe('Exchange core', () => { expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), ); - expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[takerAddress][zrx.address].minus(takerFeePaid), + expect(newBalances[takerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrxToken.address].minus(takerFeePaid), ); - expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[feeRecipientAddress][zrx.address].add(makerFeePaid.add(takerFeePaid)), + expect(newBalances[feeRecipientAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[feeRecipientAddress][zrxToken.address].add(makerFeePaid.add(takerFeePaid)), ); }); @@ -388,8 +387,8 @@ describe('Exchange core', () => { expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), ); - expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[makerAddress][zrx.address].minus(makerFeePaid), + expect(newBalances[makerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrxToken.address].minus(makerFeePaid), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), @@ -397,11 +396,11 @@ describe('Exchange core', () => { expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), ); - expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[takerAddress][zrx.address].minus(takerFeePaid), + expect(newBalances[takerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrxToken.address].minus(takerFeePaid), ); - expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[feeRecipientAddress][zrx.address].add(makerFeePaid.add(takerFeePaid)), + expect(newBalances[feeRecipientAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[feeRecipientAddress][zrxToken.address].add(makerFeePaid.add(takerFeePaid)), ); }); @@ -424,8 +423,8 @@ describe('Exchange core', () => { expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[makerAddress][defaultTakerAssetAddress].add(signedOrder.takerAssetAmount), ); - expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[makerAddress][zrx.address].minus(signedOrder.makerFee), + expect(newBalances[makerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrxToken.address].minus(signedOrder.makerFee), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultTakerAssetAddress].minus(signedOrder.takerAssetAmount), @@ -433,11 +432,13 @@ describe('Exchange core', () => { expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultMakerAssetAddress].add(signedOrder.makerAssetAmount), ); - expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[takerAddress][zrx.address].minus(signedOrder.takerFee), + expect(newBalances[takerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrxToken.address].minus(signedOrder.takerFee), ); - expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[feeRecipientAddress][zrx.address].add(signedOrder.makerFee.add(signedOrder.takerFee)), + expect(newBalances[feeRecipientAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[feeRecipientAddress][zrxToken.address].add( + signedOrder.makerFee.add(signedOrder.takerFee), + ), ); }); @@ -768,8 +769,8 @@ describe('Exchange core', () => { expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[makerAddress][defaultTakerAssetAddress].add(fillTakerAssetAmount), ); - expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[makerAddress][zrx.address].minus(makerFee), + expect(newBalances[makerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrxToken.address].minus(makerFee), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultTakerAssetAddress].minus(fillTakerAssetAmount), @@ -777,11 +778,11 @@ describe('Exchange core', () => { expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultMakerAssetAddress].add(fillMakerAssetAmount), ); - expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[takerAddress][zrx.address].minus(takerFee), + expect(newBalances[takerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrxToken.address].minus(takerFee), ); - expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), + expect(newBalances[feeRecipientAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[feeRecipientAddress][zrxToken.address].add(makerFee.add(takerFee)), ); }); }); @@ -949,14 +950,16 @@ describe('Exchange core', () => { expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), ); - expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[makerAddress][zrx.address].minus(signedOrder.makerFee), + expect(newBalances[makerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrxToken.address].minus(signedOrder.makerFee), ); - expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[takerAddress][zrx.address].minus(signedOrder.takerFee), + expect(newBalances[takerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrxToken.address].minus(signedOrder.takerFee), ); - expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[feeRecipientAddress][zrx.address].add(signedOrder.makerFee.add(signedOrder.takerFee)), + expect(newBalances[feeRecipientAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[feeRecipientAddress][zrxToken.address].add( + signedOrder.makerFee.add(signedOrder.takerFee), + ), ); }); @@ -987,14 +990,16 @@ describe('Exchange core', () => { expect(newBalances[makerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( erc20Balances[makerAddress][defaultMakerAssetAddress].minus(signedOrder.makerAssetAmount), ); - expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[makerAddress][zrx.address].minus(signedOrder.makerFee), + expect(newBalances[makerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrxToken.address].minus(signedOrder.makerFee), ); - expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[takerAddress][zrx.address].minus(signedOrder.takerFee), + expect(newBalances[takerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrxToken.address].minus(signedOrder.takerFee), ); - expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[feeRecipientAddress][zrx.address].add(signedOrder.makerFee.add(signedOrder.takerFee)), + expect(newBalances[feeRecipientAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[feeRecipientAddress][zrxToken.address].add( + signedOrder.makerFee.add(signedOrder.takerFee), + ), ); }); }); diff --git a/packages/contracts/test/exchange/wrapper.ts b/packages/contracts/test/exchange/wrapper.ts index 60f47373e..d2d82f977 100644 --- a/packages/contracts/test/exchange/wrapper.ts +++ b/packages/contracts/test/exchange/wrapper.ts @@ -19,13 +19,7 @@ import { ERC20Wrapper } from '../../src/utils/erc20_wrapper'; import { ERC721Wrapper } from '../../src/utils/erc721_wrapper'; import { ExchangeWrapper } from '../../src/utils/exchange_wrapper'; import { OrderFactory } from '../../src/utils/order_factory'; -import { - AssetProxyId, - ContractName, - ERC20BalancesByOwner, - ERC721BalancesByOwner, - SignedOrder, -} from '../../src/utils/types'; +import { AssetProxyId, ContractName, ERC20BalancesByOwner, SignedOrder } from '../../src/utils/types'; import { chaiSetup } from '../utils/chai_setup'; import { deployer } from '../utils/deployer'; import { provider, web3Wrapper } from '../utils/web3_wrapper'; @@ -42,7 +36,7 @@ describe('Exchange wrappers', () => { let erc20TokenA: DummyERC20TokenContract; let erc20TokenB: DummyERC20TokenContract; - let zrx: DummyERC20TokenContract; + let zrxToken: DummyERC20TokenContract; let erc721Token: DummyERC721TokenContract; let exchange: ExchangeContract; let assetProxyDispatcher: AssetProxyDispatcherContract; @@ -70,12 +64,12 @@ describe('Exchange wrappers', () => { erc20Wrapper = new ERC20Wrapper(deployer, provider, usedAddresses, owner); erc721Wrapper = new ERC721Wrapper(deployer, provider, usedAddresses, owner); - [erc20TokenA, erc20TokenB, zrx] = await erc20Wrapper.deployDummyERC20TokensAsync(); - erc20Proxy = await erc20Wrapper.deployERC20ProxyAsync(); + [erc20TokenA, erc20TokenB, zrxToken] = await erc20Wrapper.deployDummyTokensAsync(); + erc20Proxy = await erc20Wrapper.deployProxyAsync(); await erc20Wrapper.setBalancesAndAllowancesAsync(); - [erc721Token] = await erc721Wrapper.deployDummyERC721TokensAsync(); - erc721Proxy = await erc721Wrapper.deployERC721ProxyAsync(); + [erc721Token] = await erc721Wrapper.deployDummyTokensAsync(); + erc721Proxy = await erc721Wrapper.deployProxyAsync(); await erc721Wrapper.setBalancesAndAllowancesAsync(); const erc721Balances = await erc721Wrapper.getBalancesAsync(); erc721MakerAssetId = erc721Balances[makerAddress][erc721Token.address][0]; @@ -110,7 +104,7 @@ describe('Exchange wrappers', () => { const exchangeInstance = await deployer.deployAsync(ContractName.Exchange, [ assetProxyDispatcher.address, - assetProxyUtils.encodeERC20ProxyData(zrx.address), + assetProxyUtils.encodeERC20ProxyData(zrxToken.address), ]); exchange = new ExchangeContract(exchangeInstance.abi, exchangeInstance.address, provider); await assetProxyDispatcher.addAuthorizedAddress.sendTransactionAsync(exchange.address, { from: owner }); @@ -169,8 +163,8 @@ describe('Exchange wrappers', () => { expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), ); - expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[makerAddress][zrx.address].minus(makerFee), + expect(newBalances[makerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrxToken.address].minus(makerFee), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), @@ -178,11 +172,11 @@ describe('Exchange wrappers', () => { expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), ); - expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[takerAddress][zrx.address].minus(takerFee), + expect(newBalances[takerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrxToken.address].minus(takerFee), ); - expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), + expect(newBalances[feeRecipientAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[feeRecipientAddress][zrxToken.address].add(makerFee.add(takerFee)), ); }); @@ -237,8 +231,8 @@ describe('Exchange wrappers', () => { expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), ); - expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[makerAddress][zrx.address].minus(makerFee), + expect(newBalances[makerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrxToken.address].minus(makerFee), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), @@ -246,11 +240,11 @@ describe('Exchange wrappers', () => { expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), ); - expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[takerAddress][zrx.address].minus(takerFee), + expect(newBalances[takerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrxToken.address].minus(takerFee), ); - expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), + expect(newBalances[feeRecipientAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[feeRecipientAddress][zrxToken.address].add(makerFee.add(takerFee)), ); }); @@ -303,11 +297,11 @@ describe('Exchange wrappers', () => { }); it('should not change erc20Balances if makerAssetAddress is ZRX, makerAssetAmount + makerFee > maker balance', async () => { - const makerZRXBalance = new BigNumber(erc20Balances[makerAddress][zrx.address]); + const makerZRXBalance = new BigNumber(erc20Balances[makerAddress][zrxToken.address]); const signedOrder = orderFactory.newSignedOrder({ makerAssetAmount: makerZRXBalance, makerFee: new BigNumber(1), - makerAssetData: assetProxyUtils.encodeERC20ProxyData(zrx.address), + makerAssetData: assetProxyUtils.encodeERC20ProxyData(zrxToken.address), }); await exchangeWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); const newBalances = await erc20Wrapper.getBalancesAsync(); @@ -315,11 +309,11 @@ describe('Exchange wrappers', () => { }); it('should not change erc20Balances if makerAssetAddress is ZRX, makerAssetAmount + makerFee > maker allowance', async () => { - const makerZRXAllowance = await zrx.allowance.callAsync(makerAddress, erc20Proxy.address); + const makerZRXAllowance = await zrxToken.allowance.callAsync(makerAddress, erc20Proxy.address); const signedOrder = orderFactory.newSignedOrder({ makerAssetAmount: new BigNumber(makerZRXAllowance), makerFee: new BigNumber(1), - makerAssetData: assetProxyUtils.encodeERC20ProxyData(zrx.address), + makerAssetData: assetProxyUtils.encodeERC20ProxyData(zrxToken.address), }); await exchangeWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); const newBalances = await erc20Wrapper.getBalancesAsync(); @@ -327,11 +321,11 @@ describe('Exchange wrappers', () => { }); it('should not change erc20Balances if takerAssetAddress is ZRX, takerAssetAmount + takerFee > taker balance', async () => { - const takerZRXBalance = new BigNumber(erc20Balances[takerAddress][zrx.address]); + const takerZRXBalance = new BigNumber(erc20Balances[takerAddress][zrxToken.address]); const signedOrder = orderFactory.newSignedOrder({ takerAssetAmount: takerZRXBalance, takerFee: new BigNumber(1), - takerAssetData: assetProxyUtils.encodeERC20ProxyData(zrx.address), + takerAssetData: assetProxyUtils.encodeERC20ProxyData(zrxToken.address), }); await exchangeWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); const newBalances = await erc20Wrapper.getBalancesAsync(); @@ -339,11 +333,11 @@ describe('Exchange wrappers', () => { }); it('should not change erc20Balances if takerAssetAddress is ZRX, takerAssetAmount + takerFee > taker allowance', async () => { - const takerZRXAllowance = await zrx.allowance.callAsync(takerAddress, erc20Proxy.address); + const takerZRXAllowance = await zrxToken.allowance.callAsync(takerAddress, erc20Proxy.address); const signedOrder = orderFactory.newSignedOrder({ takerAssetAmount: new BigNumber(takerZRXAllowance), takerFee: new BigNumber(1), - takerAssetData: assetProxyUtils.encodeERC20ProxyData(zrx.address), + takerAssetData: assetProxyUtils.encodeERC20ProxyData(zrxToken.address), }); await exchangeWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); const newBalances = await erc20Wrapper.getBalancesAsync(); @@ -409,16 +403,20 @@ describe('Exchange wrappers', () => { erc20Balances[makerAddress][takerAssetAddress] = erc20Balances[makerAddress][takerAssetAddress].add( takerAssetFillAmount, ); - erc20Balances[makerAddress][zrx.address] = erc20Balances[makerAddress][zrx.address].minus(makerFee); + erc20Balances[makerAddress][zrxToken.address] = erc20Balances[makerAddress][zrxToken.address].minus( + makerFee, + ); erc20Balances[takerAddress][makerAssetAddress] = erc20Balances[takerAddress][makerAssetAddress].add( makerAssetFilledAmount, ); erc20Balances[takerAddress][takerAssetAddress] = erc20Balances[takerAddress][ takerAssetAddress ].minus(takerAssetFillAmount); - erc20Balances[takerAddress][zrx.address] = erc20Balances[takerAddress][zrx.address].minus(takerFee); - erc20Balances[feeRecipientAddress][zrx.address] = erc20Balances[feeRecipientAddress][ - zrx.address + erc20Balances[takerAddress][zrxToken.address] = erc20Balances[takerAddress][zrxToken.address].minus( + takerFee, + ); + erc20Balances[feeRecipientAddress][zrxToken.address] = erc20Balances[feeRecipientAddress][ + zrxToken.address ].add(makerFee.add(takerFee)); }); @@ -454,16 +452,20 @@ describe('Exchange wrappers', () => { erc20Balances[makerAddress][takerAssetAddress] = erc20Balances[makerAddress][takerAssetAddress].add( takerAssetFillAmount, ); - erc20Balances[makerAddress][zrx.address] = erc20Balances[makerAddress][zrx.address].minus(makerFee); + erc20Balances[makerAddress][zrxToken.address] = erc20Balances[makerAddress][zrxToken.address].minus( + makerFee, + ); erc20Balances[takerAddress][makerAssetAddress] = erc20Balances[takerAddress][makerAssetAddress].add( makerAssetFilledAmount, ); erc20Balances[takerAddress][takerAssetAddress] = erc20Balances[takerAddress][ takerAssetAddress ].minus(takerAssetFillAmount); - erc20Balances[takerAddress][zrx.address] = erc20Balances[takerAddress][zrx.address].minus(takerFee); - erc20Balances[feeRecipientAddress][zrx.address] = erc20Balances[feeRecipientAddress][ - zrx.address + erc20Balances[takerAddress][zrxToken.address] = erc20Balances[takerAddress][zrxToken.address].minus( + takerFee, + ); + erc20Balances[feeRecipientAddress][zrxToken.address] = erc20Balances[feeRecipientAddress][ + zrxToken.address ].add(makerFee.add(takerFee)); }); @@ -515,16 +517,20 @@ describe('Exchange wrappers', () => { erc20Balances[makerAddress][takerAssetAddress] = erc20Balances[makerAddress][takerAssetAddress].add( takerAssetFillAmount, ); - erc20Balances[makerAddress][zrx.address] = erc20Balances[makerAddress][zrx.address].minus(makerFee); + erc20Balances[makerAddress][zrxToken.address] = erc20Balances[makerAddress][zrxToken.address].minus( + makerFee, + ); erc20Balances[takerAddress][makerAssetAddress] = erc20Balances[takerAddress][makerAssetAddress].add( makerAssetFilledAmount, ); erc20Balances[takerAddress][takerAssetAddress] = erc20Balances[takerAddress][ takerAssetAddress ].minus(takerAssetFillAmount); - erc20Balances[takerAddress][zrx.address] = erc20Balances[takerAddress][zrx.address].minus(takerFee); - erc20Balances[feeRecipientAddress][zrx.address] = erc20Balances[feeRecipientAddress][ - zrx.address + erc20Balances[takerAddress][zrxToken.address] = erc20Balances[takerAddress][zrxToken.address].minus( + takerFee, + ); + erc20Balances[feeRecipientAddress][zrxToken.address] = erc20Balances[feeRecipientAddress][ + zrxToken.address ].add(makerFee.add(takerFee)); }); @@ -566,16 +572,20 @@ describe('Exchange wrappers', () => { erc20Balances[makerAddress][takerAssetAddress] = erc20Balances[makerAddress][takerAssetAddress].add( takerAssetFillAmount, ); - erc20Balances[makerAddress][zrx.address] = erc20Balances[makerAddress][zrx.address].minus(makerFee); + erc20Balances[makerAddress][zrxToken.address] = erc20Balances[makerAddress][zrxToken.address].minus( + makerFee, + ); erc20Balances[takerAddress][makerAssetAddress] = erc20Balances[takerAddress][makerAssetAddress].add( makerAssetFilledAmount, ); erc20Balances[takerAddress][takerAssetAddress] = erc20Balances[takerAddress][ takerAssetAddress ].minus(takerAssetFillAmount); - erc20Balances[takerAddress][zrx.address] = erc20Balances[takerAddress][zrx.address].minus(takerFee); - erc20Balances[feeRecipientAddress][zrx.address] = erc20Balances[feeRecipientAddress][ - zrx.address + erc20Balances[takerAddress][zrxToken.address] = erc20Balances[takerAddress][zrxToken.address].minus( + takerFee, + ); + erc20Balances[feeRecipientAddress][zrxToken.address] = erc20Balances[feeRecipientAddress][ + zrxToken.address ].add(makerFee.add(takerFee)); }); @@ -611,8 +621,8 @@ describe('Exchange wrappers', () => { expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), ); - expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[makerAddress][zrx.address].minus(makerFee), + expect(newBalances[makerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrxToken.address].minus(makerFee), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), @@ -620,11 +630,11 @@ describe('Exchange wrappers', () => { expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), ); - expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[takerAddress][zrx.address].minus(takerFee), + expect(newBalances[takerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrxToken.address].minus(takerFee), ); - expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), + expect(newBalances[feeRecipientAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[feeRecipientAddress][zrxToken.address].add(makerFee.add(takerFee)), ); }); @@ -637,7 +647,7 @@ describe('Exchange wrappers', () => { erc20Balances[makerAddress][defaultTakerAssetAddress] = erc20Balances[makerAddress][ defaultTakerAssetAddress ].add(signedOrder.takerAssetAmount); - erc20Balances[makerAddress][zrx.address] = erc20Balances[makerAddress][zrx.address].minus( + erc20Balances[makerAddress][zrxToken.address] = erc20Balances[makerAddress][zrxToken.address].minus( signedOrder.makerFee, ); erc20Balances[takerAddress][defaultMakerAssetAddress] = erc20Balances[takerAddress][ @@ -646,11 +656,11 @@ describe('Exchange wrappers', () => { erc20Balances[takerAddress][defaultTakerAssetAddress] = erc20Balances[takerAddress][ defaultTakerAssetAddress ].minus(signedOrder.takerAssetAmount); - erc20Balances[takerAddress][zrx.address] = erc20Balances[takerAddress][zrx.address].minus( + erc20Balances[takerAddress][zrxToken.address] = erc20Balances[takerAddress][zrxToken.address].minus( signedOrder.takerFee, ); - erc20Balances[feeRecipientAddress][zrx.address] = erc20Balances[feeRecipientAddress][ - zrx.address + erc20Balances[feeRecipientAddress][zrxToken.address] = erc20Balances[feeRecipientAddress][ + zrxToken.address ].add(signedOrder.makerFee.add(signedOrder.takerFee)); }); await exchangeWrapper.marketSellOrdersAsync(signedOrders, takerAddress, { @@ -664,7 +674,9 @@ describe('Exchange wrappers', () => { it('should throw when an signedOrder does not use the same takerAssetAddress', async () => { signedOrders = [ orderFactory.newSignedOrder(), - orderFactory.newSignedOrder({ takerAssetData: assetProxyUtils.encodeERC20ProxyData(zrx.address) }), + orderFactory.newSignedOrder({ + takerAssetData: assetProxyUtils.encodeERC20ProxyData(zrxToken.address), + }), orderFactory.newSignedOrder(), ]; @@ -698,8 +710,8 @@ describe('Exchange wrappers', () => { expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[makerAddress][defaultTakerAssetAddress].add(takerAssetFillAmount), ); - expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[makerAddress][zrx.address].minus(makerFee), + expect(newBalances[makerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrxToken.address].minus(makerFee), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultTakerAssetAddress].minus(takerAssetFillAmount), @@ -707,11 +719,11 @@ describe('Exchange wrappers', () => { expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFilledAmount), ); - expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[takerAddress][zrx.address].minus(takerFee), + expect(newBalances[takerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrxToken.address].minus(takerFee), ); - expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), + expect(newBalances[feeRecipientAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[feeRecipientAddress][zrxToken.address].add(makerFee.add(takerFee)), ); }); @@ -724,7 +736,7 @@ describe('Exchange wrappers', () => { erc20Balances[makerAddress][defaultTakerAssetAddress] = erc20Balances[makerAddress][ defaultTakerAssetAddress ].add(signedOrder.takerAssetAmount); - erc20Balances[makerAddress][zrx.address] = erc20Balances[makerAddress][zrx.address].minus( + erc20Balances[makerAddress][zrxToken.address] = erc20Balances[makerAddress][zrxToken.address].minus( signedOrder.makerFee, ); erc20Balances[takerAddress][defaultMakerAssetAddress] = erc20Balances[takerAddress][ @@ -733,11 +745,11 @@ describe('Exchange wrappers', () => { erc20Balances[takerAddress][defaultTakerAssetAddress] = erc20Balances[takerAddress][ defaultTakerAssetAddress ].minus(signedOrder.takerAssetAmount); - erc20Balances[takerAddress][zrx.address] = erc20Balances[takerAddress][zrx.address].minus( + erc20Balances[takerAddress][zrxToken.address] = erc20Balances[takerAddress][zrxToken.address].minus( signedOrder.takerFee, ); - erc20Balances[feeRecipientAddress][zrx.address] = erc20Balances[feeRecipientAddress][ - zrx.address + erc20Balances[feeRecipientAddress][zrxToken.address] = erc20Balances[feeRecipientAddress][ + zrxToken.address ].add(signedOrder.makerFee.add(signedOrder.takerFee)); }); await exchangeWrapper.marketSellOrdersNoThrowAsync(signedOrders, takerAddress, { @@ -751,7 +763,9 @@ describe('Exchange wrappers', () => { it('should throw when a signedOrder does not use the same takerAssetAddress', async () => { signedOrders = [ orderFactory.newSignedOrder(), - orderFactory.newSignedOrder({ takerAssetData: assetProxyUtils.encodeERC20ProxyData(zrx.address) }), + orderFactory.newSignedOrder({ + takerAssetData: assetProxyUtils.encodeERC20ProxyData(zrxToken.address), + }), orderFactory.newSignedOrder(), ]; @@ -785,8 +799,8 @@ describe('Exchange wrappers', () => { expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[makerAddress][defaultTakerAssetAddress].add(makerAmountBought), ); - expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[makerAddress][zrx.address].minus(makerFee), + expect(newBalances[makerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrxToken.address].minus(makerFee), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultTakerAssetAddress].minus(makerAmountBought), @@ -794,11 +808,11 @@ describe('Exchange wrappers', () => { expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFillAmount), ); - expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[takerAddress][zrx.address].minus(takerFee), + expect(newBalances[takerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrxToken.address].minus(takerFee), ); - expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), + expect(newBalances[feeRecipientAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[feeRecipientAddress][zrxToken.address].add(makerFee.add(takerFee)), ); }); @@ -811,7 +825,7 @@ describe('Exchange wrappers', () => { erc20Balances[makerAddress][defaultTakerAssetAddress] = erc20Balances[makerAddress][ defaultTakerAssetAddress ].add(signedOrder.takerAssetAmount); - erc20Balances[makerAddress][zrx.address] = erc20Balances[makerAddress][zrx.address].minus( + erc20Balances[makerAddress][zrxToken.address] = erc20Balances[makerAddress][zrxToken.address].minus( signedOrder.makerFee, ); erc20Balances[takerAddress][defaultMakerAssetAddress] = erc20Balances[takerAddress][ @@ -820,11 +834,11 @@ describe('Exchange wrappers', () => { erc20Balances[takerAddress][defaultTakerAssetAddress] = erc20Balances[takerAddress][ defaultTakerAssetAddress ].minus(signedOrder.takerAssetAmount); - erc20Balances[takerAddress][zrx.address] = erc20Balances[takerAddress][zrx.address].minus( + erc20Balances[takerAddress][zrxToken.address] = erc20Balances[takerAddress][zrxToken.address].minus( signedOrder.takerFee, ); - erc20Balances[feeRecipientAddress][zrx.address] = erc20Balances[feeRecipientAddress][ - zrx.address + erc20Balances[feeRecipientAddress][zrxToken.address] = erc20Balances[feeRecipientAddress][ + zrxToken.address ].add(signedOrder.makerFee.add(signedOrder.takerFee)); }); await exchangeWrapper.marketBuyOrdersAsync(signedOrders, takerAddress, { @@ -838,7 +852,9 @@ describe('Exchange wrappers', () => { it('should throw when an signedOrder does not use the same makerAssetAddress', async () => { signedOrders = [ orderFactory.newSignedOrder(), - orderFactory.newSignedOrder({ makerAssetData: assetProxyUtils.encodeERC20ProxyData(zrx.address) }), + orderFactory.newSignedOrder({ + makerAssetData: assetProxyUtils.encodeERC20ProxyData(zrxToken.address), + }), orderFactory.newSignedOrder(), ]; @@ -872,8 +888,8 @@ describe('Exchange wrappers', () => { expect(newBalances[makerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[makerAddress][defaultTakerAssetAddress].add(makerAmountBought), ); - expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[makerAddress][zrx.address].minus(makerFee), + expect(newBalances[makerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrxToken.address].minus(makerFee), ); expect(newBalances[takerAddress][defaultTakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultTakerAssetAddress].minus(makerAmountBought), @@ -881,11 +897,11 @@ describe('Exchange wrappers', () => { expect(newBalances[takerAddress][defaultMakerAssetAddress]).to.be.bignumber.equal( erc20Balances[takerAddress][defaultMakerAssetAddress].add(makerAssetFillAmount), ); - expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[takerAddress][zrx.address].minus(takerFee), + expect(newBalances[takerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrxToken.address].minus(takerFee), ); - expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( - erc20Balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), + expect(newBalances[feeRecipientAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[feeRecipientAddress][zrxToken.address].add(makerFee.add(takerFee)), ); }); @@ -898,7 +914,7 @@ describe('Exchange wrappers', () => { erc20Balances[makerAddress][defaultTakerAssetAddress] = erc20Balances[makerAddress][ defaultTakerAssetAddress ].add(signedOrder.takerAssetAmount); - erc20Balances[makerAddress][zrx.address] = erc20Balances[makerAddress][zrx.address].minus( + erc20Balances[makerAddress][zrxToken.address] = erc20Balances[makerAddress][zrxToken.address].minus( signedOrder.makerFee, ); erc20Balances[takerAddress][defaultMakerAssetAddress] = erc20Balances[takerAddress][ @@ -907,11 +923,11 @@ describe('Exchange wrappers', () => { erc20Balances[takerAddress][defaultTakerAssetAddress] = erc20Balances[takerAddress][ defaultTakerAssetAddress ].minus(signedOrder.takerAssetAmount); - erc20Balances[takerAddress][zrx.address] = erc20Balances[takerAddress][zrx.address].minus( + erc20Balances[takerAddress][zrxToken.address] = erc20Balances[takerAddress][zrxToken.address].minus( signedOrder.takerFee, ); - erc20Balances[feeRecipientAddress][zrx.address] = erc20Balances[feeRecipientAddress][ - zrx.address + erc20Balances[feeRecipientAddress][zrxToken.address] = erc20Balances[feeRecipientAddress][ + zrxToken.address ].add(signedOrder.makerFee.add(signedOrder.takerFee)); }); await exchangeWrapper.marketSellOrdersNoThrowAsync(signedOrders, takerAddress, { @@ -925,7 +941,9 @@ describe('Exchange wrappers', () => { it('should throw when a signedOrder does not use the same makerAssetAddress', async () => { signedOrders = [ orderFactory.newSignedOrder(), - orderFactory.newSignedOrder({ makerAssetData: assetProxyUtils.encodeERC20ProxyData(zrx.address) }), + orderFactory.newSignedOrder({ + makerAssetData: assetProxyUtils.encodeERC20ProxyData(zrxToken.address), + }), orderFactory.newSignedOrder(), ]; diff --git a/packages/contracts/test/zrx_token.ts b/packages/contracts/test/zrx_token.ts index 259b4310e..ddb7f0b35 100644 --- a/packages/contracts/test/zrx_token.ts +++ b/packages/contracts/test/zrx_token.ts @@ -24,7 +24,7 @@ describe('ZRXToken', () => { let MAX_UINT: BigNumber; - let zrx: ZRXTokenContract; + let zrxToken: ZRXTokenContract; let zrxAddress: string; before(async () => { @@ -35,8 +35,8 @@ describe('ZRXToken', () => { networkId: constants.TESTRPC_NETWORK_ID, }); const zrxInstance = await deployer.deployAsync(ContractName.ZRXToken); - zrx = new ZRXTokenContract(zrxInstance.abi, zrxInstance.address, provider); - zrxAddress = zrx.address; + zrxToken = new ZRXTokenContract(zrxInstance.abi, zrxInstance.address, provider); + zrxAddress = zrxToken.address; MAX_UINT = zeroEx.token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS; }); beforeEach(async () => { @@ -47,25 +47,25 @@ describe('ZRXToken', () => { }); describe('constants', () => { it('should have 18 decimals', async () => { - const decimals = new BigNumber(await zrx.decimals.callAsync()); + const decimals = new BigNumber(await zrxToken.decimals.callAsync()); const expectedDecimals = 18; expect(decimals).to.be.bignumber.equal(expectedDecimals); }); it('should have a total supply of 1 billion tokens', async () => { - const totalSupply = new BigNumber(await zrx.totalSupply.callAsync()); + const totalSupply = new BigNumber(await zrxToken.totalSupply.callAsync()); const expectedTotalSupply = 1000000000; expect(ZeroEx.toUnitAmount(totalSupply, 18)).to.be.bignumber.equal(expectedTotalSupply); }); it('should be named 0x Protocol Token', async () => { - const name = await zrx.name.callAsync(); + const name = await zrxToken.name.callAsync(); const expectedName = '0x Protocol Token'; expect(name).to.be.equal(expectedName); }); it('should have the symbol ZRX', async () => { - const symbol = await zrx.symbol.callAsync(); + const symbol = await zrxToken.symbol.callAsync(); const expectedSymbol = 'ZRX'; expect(symbol).to.be.equal(expectedSymbol); }); @@ -74,7 +74,7 @@ describe('ZRXToken', () => { describe('constructor', () => { it('should initialize owner balance to totalSupply', async () => { const ownerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); - const totalSupply = new BigNumber(await zrx.totalSupply.callAsync()); + const totalSupply = new BigNumber(await zrxToken.totalSupply.callAsync()); expect(totalSupply).to.be.bignumber.equal(ownerBalance); }); }); @@ -95,7 +95,7 @@ describe('ZRXToken', () => { }); it('should return true on a 0 value transfer', async () => { - const didReturnTrue = await zrx.transfer.callAsync(spender, new BigNumber(0), { + const didReturnTrue = await zrxToken.transfer.callAsync(spender, new BigNumber(0), { from: owner, }); expect(didReturnTrue).to.be.true(); @@ -109,7 +109,7 @@ describe('ZRXToken', () => { await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, amountToTransfer, { gasLimit: constants.MAX_TOKEN_APPROVE_GAS, }); - const didReturnTrue = await zrx.transferFrom.callAsync(owner, spender, amountToTransfer, { from: spender }); + const didReturnTrue = await zrxToken.transferFrom.callAsync(owner, spender, amountToTransfer, { from: spender }); expect(didReturnTrue).to.be.false(); }); @@ -121,13 +121,13 @@ describe('ZRXToken', () => { const spenderAllowanceIsInsufficient = spenderAllowance.cmp(amountToTransfer) < 0; expect(spenderAllowanceIsInsufficient).to.be.true(); - const didReturnTrue = await zrx.transferFrom.callAsync(owner, spender, amountToTransfer, { from: spender }); + const didReturnTrue = await zrxToken.transferFrom.callAsync(owner, spender, amountToTransfer, { from: spender }); expect(didReturnTrue).to.be.false(); }); it('should return true on a 0 value transfer', async () => { const amountToTransfer = new BigNumber(0); - const didReturnTrue = await zrx.transferFrom.callAsync(owner, spender, amountToTransfer, { from: spender }); + const didReturnTrue = await zrxToken.transferFrom.callAsync(owner, spender, amountToTransfer, { from: spender }); expect(didReturnTrue).to.be.true(); }); -- cgit v1.2.3