From 6463cda2045fe9caca3cb3908aa0c66f0bfe683a Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 19 Jan 2018 13:27:15 +0100 Subject: Remove truffle from tokenTransferProxy tests --- packages/contracts/test/ether_token.ts | 124 +++ packages/contracts/test/exchange/core.ts | 852 +++++++++++++++++++++ packages/contracts/test/exchange/helpers.ts | 167 ++++ packages/contracts/test/exchange/wrapper.ts | 348 +++++++++ .../contracts/test/multi_sig_with_time_lock.ts | 115 +++ ...i_sig_with_time_lock_except_remove_auth_addr.ts | 150 ++++ packages/contracts/test/token_registry.ts | 234 ++++++ .../contracts/test/token_transfer_proxy/auth.ts | 99 +++ .../test/token_transfer_proxy/transfer_from.ts | 94 +++ packages/contracts/test/ts/ether_token.ts | 124 --- packages/contracts/test/ts/exchange/core.ts | 852 --------------------- packages/contracts/test/ts/exchange/helpers.ts | 167 ---- packages/contracts/test/ts/exchange/wrapper.ts | 348 --------- .../contracts/test/ts/multi_sig_with_time_lock.ts | 115 --- ...i_sig_with_time_lock_except_remove_auth_addr.ts | 150 ---- packages/contracts/test/ts/token_registry.ts | 234 ------ .../contracts/test/ts/token_transfer_proxy/auth.ts | 99 --- .../test/ts/token_transfer_proxy/transfer_from.ts | 73 -- .../contracts/test/ts/unlimited_allowance_token.ts | 127 --- .../test/ts/unlimited_allowance_token_v2.ts | 141 ---- packages/contracts/test/ts/utils/chai_setup.ts | 13 - packages/contracts/test/ts/zrx_token.ts | 175 ----- .../contracts/test/unlimited_allowance_token.ts | 127 +++ .../contracts/test/unlimited_allowance_token_v2.ts | 141 ++++ packages/contracts/test/utils/chai_setup.ts | 13 + packages/contracts/test/zrx_token.ts | 175 +++++ 26 files changed, 2639 insertions(+), 2618 deletions(-) create mode 100644 packages/contracts/test/ether_token.ts create mode 100644 packages/contracts/test/exchange/core.ts create mode 100644 packages/contracts/test/exchange/helpers.ts create mode 100644 packages/contracts/test/exchange/wrapper.ts create mode 100644 packages/contracts/test/multi_sig_with_time_lock.ts create mode 100644 packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts create mode 100644 packages/contracts/test/token_registry.ts create mode 100644 packages/contracts/test/token_transfer_proxy/auth.ts create mode 100644 packages/contracts/test/token_transfer_proxy/transfer_from.ts delete mode 100644 packages/contracts/test/ts/ether_token.ts delete mode 100644 packages/contracts/test/ts/exchange/core.ts delete mode 100644 packages/contracts/test/ts/exchange/helpers.ts delete mode 100644 packages/contracts/test/ts/exchange/wrapper.ts delete mode 100644 packages/contracts/test/ts/multi_sig_with_time_lock.ts delete mode 100644 packages/contracts/test/ts/multi_sig_with_time_lock_except_remove_auth_addr.ts delete mode 100644 packages/contracts/test/ts/token_registry.ts delete mode 100644 packages/contracts/test/ts/token_transfer_proxy/auth.ts delete mode 100644 packages/contracts/test/ts/token_transfer_proxy/transfer_from.ts delete mode 100644 packages/contracts/test/ts/unlimited_allowance_token.ts delete mode 100644 packages/contracts/test/ts/unlimited_allowance_token_v2.ts delete mode 100644 packages/contracts/test/ts/utils/chai_setup.ts delete mode 100644 packages/contracts/test/ts/zrx_token.ts create mode 100644 packages/contracts/test/unlimited_allowance_token.ts create mode 100644 packages/contracts/test/unlimited_allowance_token_v2.ts create mode 100644 packages/contracts/test/utils/chai_setup.ts create mode 100644 packages/contracts/test/zrx_token.ts (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/ether_token.ts b/packages/contracts/test/ether_token.ts new file mode 100644 index 000000000..cce11812b --- /dev/null +++ b/packages/contracts/test/ether_token.ts @@ -0,0 +1,124 @@ +import { ZeroEx, ZeroExError } from '0x.js'; +import { BigNumber, promisify } from '@0xproject/utils'; +import * as chai from 'chai'; +import Web3 = require('web3'); + +import { Artifacts } from '../util/artifacts'; +import { constants } from '../util/constants'; + +import { chaiSetup } from './utils/chai_setup'; + +const { EtherToken } = new Artifacts(artifacts); + +chaiSetup.configure(); +const expect = chai.expect; + +// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle +// with type `any` to a variable of type `Web3`. +const web3: Web3 = (global as any).web3; + +contract('EtherToken', (accounts: string[]) => { + const account = accounts[0]; + const gasPrice = ZeroEx.toBaseUnitAmount(new BigNumber(20), 9); + let zeroEx: ZeroEx; + let etherTokenAddress: string; + + before(async () => { + etherTokenAddress = EtherToken.address; + zeroEx = new ZeroEx(web3.currentProvider, { + gasPrice, + networkId: constants.TESTRPC_NETWORK_ID, + }); + }); + + const sendTransactionAsync = promisify(web3.eth.sendTransaction); + const getEthBalanceAsync = async (owner: string) => { + const balanceStr = await promisify(web3.eth.getBalance)(owner); + const balance = new BigNumber(balanceStr); + return balance; + }; + + describe('deposit', () => { + it('should throw if caller attempts to deposit more Ether than caller balance', async () => { + const initEthBalance = await getEthBalanceAsync(account); + const ethToDeposit = initEthBalance.plus(1); + + return expect(zeroEx.etherToken.depositAsync(etherTokenAddress, ethToDeposit, account)).to.be.rejectedWith( + ZeroExError.InsufficientEthBalanceForDeposit, + ); + }); + + it('should convert deposited Ether to wrapped Ether tokens', async () => { + const initEthBalance = await getEthBalanceAsync(account); + const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); + + const ethToDeposit = new BigNumber(web3.toWei(1, 'ether')); + + const txHash = await zeroEx.etherToken.depositAsync(etherTokenAddress, ethToDeposit, account); + const receipt = await zeroEx.awaitTransactionMinedAsync(txHash); + + const ethSpentOnGas = gasPrice.times(receipt.gasUsed); + const finalEthBalance = await getEthBalanceAsync(account); + const finalEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); + + expect(finalEthBalance).to.be.bignumber.equal(initEthBalance.minus(ethToDeposit.plus(ethSpentOnGas))); + expect(finalEthTokenBalance).to.be.bignumber.equal(initEthTokenBalance.plus(ethToDeposit)); + }); + }); + + describe('withdraw', () => { + it('should throw if caller attempts to withdraw greater than caller balance', async () => { + const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); + const ethTokensToWithdraw = initEthTokenBalance.plus(1); + + return expect( + zeroEx.etherToken.withdrawAsync(etherTokenAddress, ethTokensToWithdraw, account), + ).to.be.rejectedWith(ZeroExError.InsufficientWEthBalanceForWithdrawal); + }); + + it('should convert ether tokens to ether with sufficient balance', async () => { + const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); + const initEthBalance = await getEthBalanceAsync(account); + const ethTokensToWithdraw = initEthTokenBalance; + expect(ethTokensToWithdraw).to.not.be.bignumber.equal(0); + const txHash = await zeroEx.etherToken.withdrawAsync(etherTokenAddress, ethTokensToWithdraw, account, { + gasLimit: constants.MAX_ETHERTOKEN_WITHDRAW_GAS, + }); + const receipt = await zeroEx.awaitTransactionMinedAsync(txHash); + + const ethSpentOnGas = gasPrice.times(receipt.gasUsed); + const finalEthBalance = await getEthBalanceAsync(account); + const finalEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); + + expect(finalEthBalance).to.be.bignumber.equal( + initEthBalance.plus(ethTokensToWithdraw.minus(ethSpentOnGas)), + ); + expect(finalEthTokenBalance).to.be.bignumber.equal(initEthTokenBalance.minus(ethTokensToWithdraw)); + }); + }); + + describe('fallback', () => { + it('should convert sent ether to ether tokens', async () => { + const initEthBalance = await getEthBalanceAsync(account); + const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); + + const ethToDeposit = ZeroEx.toBaseUnitAmount(new BigNumber(1), 18); + + const txHash = await sendTransactionAsync({ + from: account, + to: etherTokenAddress, + value: ethToDeposit, + gasPrice, + }); + + const receipt = await zeroEx.awaitTransactionMinedAsync(txHash); + + const ethSpentOnGas = gasPrice.times(receipt.gasUsed); + const finalEthBalance = await getEthBalanceAsync(account); + const finalEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); + + expect(finalEthBalance).to.be.bignumber.equal(initEthBalance.minus(ethToDeposit.plus(ethSpentOnGas))); + expect(finalEthTokenBalance).to.be.bignumber.equal(initEthTokenBalance.plus(ethToDeposit)); + }); + }); +}); diff --git a/packages/contracts/test/exchange/core.ts b/packages/contracts/test/exchange/core.ts new file mode 100644 index 000000000..0e94fcc34 --- /dev/null +++ b/packages/contracts/test/exchange/core.ts @@ -0,0 +1,852 @@ +import { ZeroEx } from '0x.js'; +import { BigNumber } from '@0xproject/utils'; +import * as chai from 'chai'; +import ethUtil = require('ethereumjs-util'); +import * as Web3 from 'web3'; + +import { Artifacts } from '../../util/artifacts'; +import { Balances } from '../../util/balances'; +import { constants } from '../../util/constants'; +import { crypto } from '../../util/crypto'; +import { ExchangeWrapper } from '../../util/exchange_wrapper'; +import { Order } from '../../util/order'; +import { OrderFactory } from '../../util/order_factory'; +import { BalancesByOwner, ContractInstance, ExchangeContractErrs } from '../../util/types'; +import { chaiSetup } from '../utils/chai_setup'; + +chaiSetup.configure(); +const expect = chai.expect; +const { Exchange, TokenTransferProxy, DummyToken, TokenRegistry, MaliciousToken } = new Artifacts(artifacts); + +// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle +// with type `any` to a variable of type `Web3`. +const web3: Web3 = (global as any).web3; + +contract('Exchange', (accounts: string[]) => { + const maker = accounts[0]; + const tokenOwner = accounts[0]; + const taker = accounts[1] || accounts[accounts.length - 1]; + const feeRecipient = accounts[2] || accounts[accounts.length - 1]; + + const INITIAL_BALANCE = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); + const INITIAL_ALLOWANCE = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); + + let rep: ContractInstance; + let dgd: ContractInstance; + let zrx: ContractInstance; + let exchange: ContractInstance; + let tokenRegistry: ContractInstance; + + let order: Order; + let balances: BalancesByOwner; + let exWrapper: ExchangeWrapper; + let dmyBalances: Balances; + let orderFactory: OrderFactory; + + let zeroEx: ZeroEx; + + before(async () => { + [tokenRegistry, exchange] = await Promise.all([TokenRegistry.deployed(), Exchange.deployed()]); + exWrapper = new ExchangeWrapper(exchange); + zeroEx = new ZeroEx(web3.currentProvider, { + exchangeContractAddress: exchange.address, + networkId: constants.TESTRPC_NETWORK_ID, + }); + + const [repAddress, dgdAddress, zrxAddress] = await Promise.all([ + tokenRegistry.getTokenAddressBySymbol('REP'), + tokenRegistry.getTokenAddressBySymbol('DGD'), + tokenRegistry.getTokenAddressBySymbol('ZRX'), + ]); + + const defaultOrderParams = { + exchangeContractAddress: Exchange.address, + maker, + feeRecipient, + makerToken: repAddress, + takerToken: dgdAddress, + makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), + takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), + makerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), + takerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), + }; + orderFactory = new OrderFactory(defaultOrderParams); + + [rep, dgd, zrx] = await Promise.all([ + DummyToken.at(repAddress), + DummyToken.at(dgdAddress), + DummyToken.at(zrxAddress), + ]); + dmyBalances = new Balances([rep, dgd, zrx], [maker, taker, feeRecipient]); + await Promise.all([ + rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: maker, + }), + rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: taker, + }), + rep.setBalance(maker, INITIAL_BALANCE, { from: tokenOwner }), + rep.setBalance(taker, INITIAL_BALANCE, { from: tokenOwner }), + dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: maker, + }), + dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: taker, + }), + dgd.setBalance(maker, INITIAL_BALANCE, { from: tokenOwner }), + dgd.setBalance(taker, INITIAL_BALANCE, { from: tokenOwner }), + zrx.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: maker, + }), + zrx.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: taker, + }), + zrx.setBalance(maker, INITIAL_BALANCE, { from: tokenOwner }), + zrx.setBalance(taker, INITIAL_BALANCE, { from: tokenOwner }), + ]); + }); + + describe('internal functions', () => { + it('should include transferViaTokenTransferProxy', () => { + expect(exchange.transferViaTokenTransferProxy).to.be.undefined(); + }); + + it('should include isTransferable', () => { + expect(exchange.isTransferable).to.be.undefined(); + }); + + it('should include getBalance', () => { + expect(exchange.getBalance).to.be.undefined(); + }); + + it('should include getAllowance', () => { + expect(exchange.getAllowance).to.be.undefined(); + }); + }); + + describe('fillOrder', () => { + beforeEach(async () => { + balances = await dmyBalances.getAsync(); + order = await orderFactory.newSignedOrderAsync(); + }); + + it('should create an unfillable order', async () => { + order = await orderFactory.newSignedOrderAsync({ + makerTokenAmount: new BigNumber(1001), + takerTokenAmount: new BigNumber(3), + }); + + const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); + expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); + + const fillTakerTokenAmount1 = new BigNumber(2); + await exWrapper.fillOrderAsync(order, taker, { + fillTakerTokenAmount: fillTakerTokenAmount1, + }); + + const filledTakerTokenAmountAfter1 = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); + expect(filledTakerTokenAmountAfter1).to.be.bignumber.equal(fillTakerTokenAmount1); + + const fillTakerTokenAmount2 = new BigNumber(1); + await exWrapper.fillOrderAsync(order, taker, { + fillTakerTokenAmount: fillTakerTokenAmount2, + }); + + const filledTakerTokenAmountAfter2 = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); + expect(filledTakerTokenAmountAfter2).to.be.bignumber.equal(filledTakerTokenAmountAfter1); + }); + + it('should transfer the correct amounts when makerTokenAmount === takerTokenAmount', async () => { + order = await orderFactory.newSignedOrderAsync({ + makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), + takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), + }); + + const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); + expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); + + const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); + await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); + + const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); + expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(fillTakerTokenAmount); + + const newBalances = await dmyBalances.getAsync(); + + const fillMakerTokenAmount = fillTakerTokenAmount + .times(order.params.makerTokenAmount) + .dividedToIntegerBy(order.params.takerTokenAmount); + const paidMakerFee = order.params.makerFee + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + const paidTakerFee = order.params.takerFee + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( + balances[maker][order.params.makerToken].minus(fillMakerTokenAmount), + ); + expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( + balances[maker][order.params.takerToken].add(fillTakerTokenAmount), + ); + expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( + balances[maker][zrx.address].minus(paidMakerFee), + ); + expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( + balances[taker][order.params.takerToken].minus(fillTakerTokenAmount), + ); + expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( + balances[taker][order.params.makerToken].add(fillMakerTokenAmount), + ); + expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( + balances[taker][zrx.address].minus(paidTakerFee), + ); + expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( + balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee)), + ); + }); + + it('should transfer the correct amounts when makerTokenAmount > takerTokenAmount', async () => { + order = await orderFactory.newSignedOrderAsync({ + makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), + takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), + }); + + const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); + expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); + + const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); + await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); + + const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); + expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(fillTakerTokenAmount); + + const newBalances = await dmyBalances.getAsync(); + + const fillMakerTokenAmount = fillTakerTokenAmount + .times(order.params.makerTokenAmount) + .dividedToIntegerBy(order.params.takerTokenAmount); + const paidMakerFee = order.params.makerFee + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + const paidTakerFee = order.params.takerFee + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( + balances[maker][order.params.makerToken].minus(fillMakerTokenAmount), + ); + expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( + balances[maker][order.params.takerToken].add(fillTakerTokenAmount), + ); + expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( + balances[maker][zrx.address].minus(paidMakerFee), + ); + expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( + balances[taker][order.params.takerToken].minus(fillTakerTokenAmount), + ); + expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( + balances[taker][order.params.makerToken].add(fillMakerTokenAmount), + ); + expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( + balances[taker][zrx.address].minus(paidTakerFee), + ); + expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( + balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee)), + ); + }); + + it('should transfer the correct amounts when makerTokenAmount < takerTokenAmount', async () => { + order = await orderFactory.newSignedOrderAsync({ + makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), + takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), + }); + + const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); + expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); + + const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); + await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); + + const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); + expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(fillTakerTokenAmount); + + const newBalances = await dmyBalances.getAsync(); + + const fillMakerTokenAmount = fillTakerTokenAmount + .times(order.params.makerTokenAmount) + .dividedToIntegerBy(order.params.takerTokenAmount); + const paidMakerFee = order.params.makerFee + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + const paidTakerFee = order.params.takerFee + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( + balances[maker][order.params.makerToken].minus(fillMakerTokenAmount), + ); + expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( + balances[maker][order.params.takerToken].add(fillTakerTokenAmount), + ); + expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( + balances[maker][zrx.address].minus(paidMakerFee), + ); + expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( + balances[taker][order.params.takerToken].minus(fillTakerTokenAmount), + ); + expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( + balances[taker][order.params.makerToken].add(fillMakerTokenAmount), + ); + expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( + balances[taker][zrx.address].minus(paidTakerFee), + ); + expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( + balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee)), + ); + }); + + it('should transfer the correct amounts when taker is specified and order is claimed by taker', async () => { + order = await orderFactory.newSignedOrderAsync({ + taker, + makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), + takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), + }); + + const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); + expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); + + const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); + await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); + + const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( + order.params.orderHashHex, + ); + const expectedFillAmountTAfter = fillTakerTokenAmount.add(filledTakerTokenAmountBefore); + expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(expectedFillAmountTAfter); + + const newBalances = await dmyBalances.getAsync(); + + const fillMakerTokenAmount = fillTakerTokenAmount + .times(order.params.makerTokenAmount) + .dividedToIntegerBy(order.params.takerTokenAmount); + const paidMakerFee = order.params.makerFee + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + const paidTakerFee = order.params.takerFee + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( + balances[maker][order.params.makerToken].minus(fillMakerTokenAmount), + ); + expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( + balances[maker][order.params.takerToken].add(fillTakerTokenAmount), + ); + expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( + balances[maker][zrx.address].minus(paidMakerFee), + ); + expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( + balances[taker][order.params.takerToken].minus(fillTakerTokenAmount), + ); + expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( + balances[taker][order.params.makerToken].add(fillMakerTokenAmount), + ); + expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( + balances[taker][zrx.address].minus(paidTakerFee), + ); + expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( + balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee)), + ); + }); + + it('should fill remaining value if fillTakerTokenAmount > remaining takerTokenAmount', async () => { + const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); + await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); + + const res = await exWrapper.fillOrderAsync(order, taker, { + fillTakerTokenAmount: order.params.takerTokenAmount, + }); + + expect(res.logs[0].args.filledTakerTokenAmount).to.be.bignumber.equal( + order.params.takerTokenAmount.minus(fillTakerTokenAmount), + ); + const newBalances = await dmyBalances.getAsync(); + + expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( + balances[maker][order.params.makerToken].minus(order.params.makerTokenAmount), + ); + expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( + balances[maker][order.params.takerToken].add(order.params.takerTokenAmount), + ); + expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( + balances[maker][zrx.address].minus(order.params.makerFee), + ); + expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( + balances[taker][order.params.takerToken].minus(order.params.takerTokenAmount), + ); + expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( + balances[taker][order.params.makerToken].add(order.params.makerTokenAmount), + ); + expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( + balances[taker][zrx.address].minus(order.params.takerFee), + ); + expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( + balances[feeRecipient][zrx.address].add(order.params.makerFee.add(order.params.takerFee)), + ); + }); + + it('should log 1 event with the correct arguments when order has a feeRecipient', async () => { + const divisor = 2; + const res = await exWrapper.fillOrderAsync(order, taker, { + fillTakerTokenAmount: order.params.takerTokenAmount.div(divisor), + }); + expect(res.logs).to.have.length(1); + + const logArgs = res.logs[0].args; + const expectedFilledMakerTokenAmount = order.params.makerTokenAmount.div(divisor); + const expectedFilledTakerTokenAmount = order.params.takerTokenAmount.div(divisor); + const expectedFeeMPaid = order.params.makerFee.div(divisor); + const expectedFeeTPaid = order.params.takerFee.div(divisor); + const tokensHashBuff = crypto.solSHA3([order.params.makerToken, order.params.takerToken]); + const expectedTokens = ethUtil.bufferToHex(tokensHashBuff); + + expect(order.params.maker).to.be.equal(logArgs.maker); + expect(taker).to.be.equal(logArgs.taker); + expect(order.params.feeRecipient).to.be.equal(logArgs.feeRecipient); + expect(order.params.makerToken).to.be.equal(logArgs.makerToken); + expect(order.params.takerToken).to.be.equal(logArgs.takerToken); + expect(expectedFilledMakerTokenAmount).to.be.bignumber.equal(logArgs.filledMakerTokenAmount); + expect(expectedFilledTakerTokenAmount).to.be.bignumber.equal(logArgs.filledTakerTokenAmount); + expect(expectedFeeMPaid).to.be.bignumber.equal(logArgs.paidMakerFee); + expect(expectedFeeTPaid).to.be.bignumber.equal(logArgs.paidTakerFee); + expect(expectedTokens).to.be.equal(logArgs.tokens); + expect(order.params.orderHashHex).to.be.equal(logArgs.orderHash); + }); + + it('should log 1 event with the correct arguments when order has no feeRecipient', async () => { + order = await orderFactory.newSignedOrderAsync({ + feeRecipient: ZeroEx.NULL_ADDRESS, + }); + const divisor = 2; + const res = await exWrapper.fillOrderAsync(order, taker, { + fillTakerTokenAmount: order.params.takerTokenAmount.div(divisor), + }); + expect(res.logs).to.have.length(1); + + const logArgs = res.logs[0].args; + const expectedFilledMakerTokenAmount = order.params.makerTokenAmount.div(divisor); + const expectedFilledTakerTokenAmount = order.params.takerTokenAmount.div(divisor); + const expectedFeeMPaid = new BigNumber(0); + const expectedFeeTPaid = new BigNumber(0); + const tokensHashBuff = crypto.solSHA3([order.params.makerToken, order.params.takerToken]); + const expectedTokens = ethUtil.bufferToHex(tokensHashBuff); + + expect(order.params.maker).to.be.equal(logArgs.maker); + expect(taker).to.be.equal(logArgs.taker); + expect(order.params.feeRecipient).to.be.equal(logArgs.feeRecipient); + expect(order.params.makerToken).to.be.equal(logArgs.makerToken); + expect(order.params.takerToken).to.be.equal(logArgs.takerToken); + expect(expectedFilledMakerTokenAmount).to.be.bignumber.equal(logArgs.filledMakerTokenAmount); + expect(expectedFilledTakerTokenAmount).to.be.bignumber.equal(logArgs.filledTakerTokenAmount); + expect(expectedFeeMPaid).to.be.bignumber.equal(logArgs.paidMakerFee); + expect(expectedFeeTPaid).to.be.bignumber.equal(logArgs.paidTakerFee); + expect(expectedTokens).to.be.equal(logArgs.tokens); + expect(order.params.orderHashHex).to.be.equal(logArgs.orderHash); + }); + + it('should throw when taker is specified and order is claimed by other', async () => { + order = await orderFactory.newSignedOrderAsync({ + taker: feeRecipient, + makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), + takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), + }); + + return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); + }); + + it('should throw if signature is invalid', async () => { + order = await orderFactory.newSignedOrderAsync({ + makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(10), 18), + }); + + order.params.r = ethUtil.bufferToHex(ethUtil.sha3('invalidR')); + order.params.s = ethUtil.bufferToHex(ethUtil.sha3('invalidS')); + return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); + }); + + it('should throw if makerTokenAmount is 0', async () => { + order = await orderFactory.newSignedOrderAsync({ + makerTokenAmount: new BigNumber(0), + }); + + return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); + }); + + it('should throw if takerTokenAmount is 0', async () => { + order = await orderFactory.newSignedOrderAsync({ + takerTokenAmount: new BigNumber(0), + }); + + return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); + }); + + it('should throw if fillTakerTokenAmount is 0', async () => { + order = await orderFactory.newSignedOrderAsync(); + + return expect( + exWrapper.fillOrderAsync(order, taker, { + fillTakerTokenAmount: new BigNumber(0), + }), + ).to.be.rejectedWith(constants.REVERT); + }); + + it('should not change balances if maker balances are too low to fill order and \ + shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { + order = await orderFactory.newSignedOrderAsync({ + makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), + }); + + await exWrapper.fillOrderAsync(order, taker); + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should throw if maker balances are too low to fill order and \ + shouldThrowOnInsufficientBalanceOrAllowance = true', async () => { + order = await orderFactory.newSignedOrderAsync({ + makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), + }); + + return expect( + exWrapper.fillOrderAsync(order, taker, { + shouldThrowOnInsufficientBalanceOrAllowance: true, + }), + ).to.be.rejectedWith(constants.REVERT); + }); + + it('should not change balances if taker balances are too low to fill order and \ + shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { + order = await orderFactory.newSignedOrderAsync({ + takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), + }); + + await exWrapper.fillOrderAsync(order, taker); + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should throw if taker balances are too low to fill order and \ + shouldThrowOnInsufficientBalanceOrAllowance = true', async () => { + order = await orderFactory.newSignedOrderAsync({ + takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), + }); + + return expect( + exWrapper.fillOrderAsync(order, taker, { + shouldThrowOnInsufficientBalanceOrAllowance: true, + }), + ).to.be.rejectedWith(constants.REVERT); + }); + + it('should not change balances if maker allowances are too low to fill order and \ + shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { + await rep.approve(TokenTransferProxy.address, 0, { from: maker }); + await exWrapper.fillOrderAsync(order, taker); + await rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: maker, + }); + + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should throw if maker allowances are too low to fill order and \ + shouldThrowOnInsufficientBalanceOrAllowance = true', async () => { + await rep.approve(TokenTransferProxy.address, 0, { from: maker }); + expect( + exWrapper.fillOrderAsync(order, taker, { + shouldThrowOnInsufficientBalanceOrAllowance: true, + }), + ).to.be.rejectedWith(constants.REVERT); + await rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: maker, + }); + }); + + it('should not change balances if taker allowances are too low to fill order and \ + shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { + await dgd.approve(TokenTransferProxy.address, 0, { from: taker }); + await exWrapper.fillOrderAsync(order, taker); + await dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: taker, + }); + + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should throw if taker allowances are too low to fill order and \ + shouldThrowOnInsufficientBalanceOrAllowance = true', async () => { + await dgd.approve(TokenTransferProxy.address, 0, { from: taker }); + expect( + exWrapper.fillOrderAsync(order, taker, { + shouldThrowOnInsufficientBalanceOrAllowance: true, + }), + ).to.be.rejectedWith(constants.REVERT); + await dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: taker, + }); + }); + + it('should not change balances if makerToken is ZRX, makerTokenAmount + makerFee > maker balance, \ + and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { + const makerZRXBalance = new BigNumber(balances[maker][zrx.address]); + order = await orderFactory.newSignedOrderAsync({ + makerToken: zrx.address, + makerTokenAmount: makerZRXBalance, + makerFee: new BigNumber(1), + }); + await exWrapper.fillOrderAsync(order, taker); + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should not change balances if makerToken is ZRX, makerTokenAmount + makerFee > maker allowance, \ + and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { + const makerZRXAllowance = await zrx.allowance(maker, TokenTransferProxy.address); + order = await orderFactory.newSignedOrderAsync({ + makerToken: zrx.address, + makerTokenAmount: new BigNumber(makerZRXAllowance), + makerFee: new BigNumber(1), + }); + await exWrapper.fillOrderAsync(order, taker); + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should not change balances if takerToken is ZRX, takerTokenAmount + takerFee > taker balance, \ + and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { + const takerZRXBalance = new BigNumber(balances[taker][zrx.address]); + order = await orderFactory.newSignedOrderAsync({ + takerToken: zrx.address, + takerTokenAmount: takerZRXBalance, + takerFee: new BigNumber(1), + }); + await exWrapper.fillOrderAsync(order, taker); + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should not change balances if takerToken is ZRX, takerTokenAmount + takerFee > taker allowance, \ + and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { + const takerZRXAllowance = await zrx.allowance(taker, TokenTransferProxy.address); + order = await orderFactory.newSignedOrderAsync({ + takerToken: zrx.address, + takerTokenAmount: new BigNumber(takerZRXAllowance), + takerFee: new BigNumber(1), + }); + await exWrapper.fillOrderAsync(order, taker); + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should throw if getBalance or getAllowance attempts to change state and \ + shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { + const maliciousToken = await MaliciousToken.new(); + await maliciousToken.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { from: taker }); + + order = await orderFactory.newSignedOrderAsync({ + takerToken: maliciousToken.address, + }); + + return expect( + exWrapper.fillOrderAsync(order, taker, { + shouldThrowOnInsufficientBalanceOrAllowance: false, + }), + ).to.be.rejectedWith(constants.REVERT); + }); + + it('should not change balances if an order is expired', async () => { + order = await orderFactory.newSignedOrderAsync({ + expirationTimestampInSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), + }); + await exWrapper.fillOrderAsync(order, taker); + + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should log an error event if an order is expired', async () => { + order = await orderFactory.newSignedOrderAsync({ + expirationTimestampInSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), + }); + + const res = await exWrapper.fillOrderAsync(order, taker); + expect(res.logs).to.have.length(1); + const errCode = res.logs[0].args.errorId.toNumber(); + expect(errCode).to.be.equal(ExchangeContractErrs.ERROR_ORDER_EXPIRED); + }); + + it('should log an error event if no value is filled', async () => { + await exWrapper.fillOrderAsync(order, taker); + + const res = await exWrapper.fillOrderAsync(order, taker); + expect(res.logs).to.have.length(1); + const errCode = res.logs[0].args.errorId.toNumber(); + expect(errCode).to.be.equal(ExchangeContractErrs.ERROR_ORDER_FULLY_FILLED_OR_CANCELLED); + }); + }); + + describe('cancelOrder', () => { + beforeEach(async () => { + balances = await dmyBalances.getAsync(); + order = await orderFactory.newSignedOrderAsync(); + }); + + it('should throw if not sent by maker', async () => { + return expect(exWrapper.cancelOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); + }); + + it('should throw if makerTokenAmount is 0', async () => { + order = await orderFactory.newSignedOrderAsync({ + makerTokenAmount: new BigNumber(0), + }); + + return expect(exWrapper.cancelOrderAsync(order, maker)).to.be.rejectedWith(constants.REVERT); + }); + + it('should throw if takerTokenAmount is 0', async () => { + order = await orderFactory.newSignedOrderAsync({ + takerTokenAmount: new BigNumber(0), + }); + + return expect(exWrapper.cancelOrderAsync(order, maker)).to.be.rejectedWith(constants.REVERT); + }); + + it('should throw if cancelTakerTokenAmount is 0', async () => { + order = await orderFactory.newSignedOrderAsync(); + + return expect( + exWrapper.cancelOrderAsync(order, maker, { + cancelTakerTokenAmount: new BigNumber(0), + }), + ).to.be.rejectedWith(constants.REVERT); + }); + + it('should be able to cancel a full order', async () => { + await exWrapper.cancelOrderAsync(order, maker); + await exWrapper.fillOrderAsync(order, taker, { + fillTakerTokenAmount: order.params.takerTokenAmount.div(2), + }); + + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should be able to cancel part of an order', async () => { + const cancelTakerTokenAmount = order.params.takerTokenAmount.div(2); + await exWrapper.cancelOrderAsync(order, maker, { + cancelTakerTokenAmount, + }); + + const res = await exWrapper.fillOrderAsync(order, taker, { + fillTakerTokenAmount: order.params.takerTokenAmount, + }); + expect(res.logs[0].args.filledTakerTokenAmount).to.be.bignumber.equal( + order.params.takerTokenAmount.minus(cancelTakerTokenAmount), + ); + + const newBalances = await dmyBalances.getAsync(); + const cancelMakerTokenAmount = cancelTakerTokenAmount + .times(order.params.makerTokenAmount) + .dividedToIntegerBy(order.params.takerTokenAmount); + const paidMakerFee = order.params.makerFee + .times(cancelMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + const paidTakerFee = order.params.takerFee + .times(cancelMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( + balances[maker][order.params.makerToken].minus(cancelMakerTokenAmount), + ); + expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( + balances[maker][order.params.takerToken].add(cancelTakerTokenAmount), + ); + expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( + balances[maker][zrx.address].minus(paidMakerFee), + ); + expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( + balances[taker][order.params.takerToken].minus(cancelTakerTokenAmount), + ); + expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( + balances[taker][order.params.makerToken].add(cancelMakerTokenAmount), + ); + expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( + balances[taker][zrx.address].minus(paidTakerFee), + ); + expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( + balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee)), + ); + }); + + it('should log 1 event with correct arguments', async () => { + const divisor = 2; + const res = await exWrapper.cancelOrderAsync(order, maker, { + cancelTakerTokenAmount: order.params.takerTokenAmount.div(divisor), + }); + expect(res.logs).to.have.length(1); + + const logArgs = res.logs[0].args; + const expectedCancelledMakerTokenAmount = order.params.makerTokenAmount.div(divisor); + const expectedCancelledTakerTokenAmount = order.params.takerTokenAmount.div(divisor); + const tokensHashBuff = crypto.solSHA3([order.params.makerToken, order.params.takerToken]); + const expectedTokens = ethUtil.bufferToHex(tokensHashBuff); + + expect(order.params.maker).to.be.equal(logArgs.maker); + expect(order.params.feeRecipient).to.be.equal(logArgs.feeRecipient); + expect(order.params.makerToken).to.be.equal(logArgs.makerToken); + expect(order.params.takerToken).to.be.equal(logArgs.takerToken); + expect(expectedCancelledMakerTokenAmount).to.be.bignumber.equal(logArgs.cancelledMakerTokenAmount); + expect(expectedCancelledTakerTokenAmount).to.be.bignumber.equal(logArgs.cancelledTakerTokenAmount); + expect(expectedTokens).to.be.equal(logArgs.tokens); + expect(order.params.orderHashHex).to.be.equal(logArgs.orderHash); + }); + + it('should not log events if no value is cancelled', async () => { + await exWrapper.cancelOrderAsync(order, maker); + + const res = await exWrapper.cancelOrderAsync(order, maker); + expect(res.logs).to.have.length(1); + const errCode = res.logs[0].args.errorId.toNumber(); + expect(errCode).to.be.equal(ExchangeContractErrs.ERROR_ORDER_FULLY_FILLED_OR_CANCELLED); + }); + + it('should not log events if order is expired', async () => { + order = await orderFactory.newSignedOrderAsync({ + expirationTimestampInSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), + }); + + const res = await exWrapper.cancelOrderAsync(order, maker); + expect(res.logs).to.have.length(1); + const errCode = res.logs[0].args.errorId.toNumber(); + expect(errCode).to.be.equal(ExchangeContractErrs.ERROR_ORDER_EXPIRED); + }); + }); +}); // tslint:disable-line:max-file-line-count diff --git a/packages/contracts/test/exchange/helpers.ts b/packages/contracts/test/exchange/helpers.ts new file mode 100644 index 000000000..830af44d1 --- /dev/null +++ b/packages/contracts/test/exchange/helpers.ts @@ -0,0 +1,167 @@ +import { ZeroEx } from '0x.js'; +import { BigNumber } from '@0xproject/utils'; +import * as chai from 'chai'; +import ethUtil = require('ethereumjs-util'); + +import { Artifacts } from '../../util/artifacts'; +import { ExchangeWrapper } from '../../util/exchange_wrapper'; +import { Order } from '../../util/order'; +import { OrderFactory } from '../../util/order_factory'; +import { chaiSetup } from '../utils/chai_setup'; + +chaiSetup.configure(); +const expect = chai.expect; + +const { Exchange, TokenRegistry } = new Artifacts(artifacts); + +contract('Exchange', (accounts: string[]) => { + const maker = accounts[0]; + const feeRecipient = accounts[1] || accounts[accounts.length - 1]; + + let order: Order; + let exchangeWrapper: ExchangeWrapper; + let orderFactory: OrderFactory; + + before(async () => { + const [tokenRegistry, exchange] = await Promise.all([TokenRegistry.deployed(), Exchange.deployed()]); + exchangeWrapper = new ExchangeWrapper(exchange); + const [repAddress, dgdAddress] = await Promise.all([ + tokenRegistry.getTokenAddressBySymbol('REP'), + tokenRegistry.getTokenAddressBySymbol('DGD'), + ]); + const defaultOrderParams = { + exchangeContractAddress: Exchange.address, + maker, + feeRecipient, + makerToken: repAddress, + takerToken: dgdAddress, + makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), + takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), + makerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), + takerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), + }; + orderFactory = new OrderFactory(defaultOrderParams); + }); + + beforeEach(async () => { + order = await orderFactory.newSignedOrderAsync(); + }); + + describe('getOrderHash', () => { + it('should output the correct orderHash', async () => { + const orderHashHex = await exchangeWrapper.getOrderHashAsync(order); + expect(order.params.orderHashHex).to.be.equal(orderHashHex); + }); + }); + + describe('isValidSignature', () => { + beforeEach(async () => { + order = await orderFactory.newSignedOrderAsync(); + }); + + it('should return true with a valid signature', async () => { + const success = await exchangeWrapper.isValidSignatureAsync(order); + const isValidSignature = order.isValidSignature(); + expect(isValidSignature).to.be.true(); + expect(success).to.be.true(); + }); + + it('should return false with an invalid signature', async () => { + order.params.r = ethUtil.bufferToHex(ethUtil.sha3('invalidR')); + order.params.s = ethUtil.bufferToHex(ethUtil.sha3('invalidS')); + const success = await exchangeWrapper.isValidSignatureAsync(order); + expect(order.isValidSignature()).to.be.false(); + expect(success).to.be.false(); + }); + }); + + describe('isRoundingError', () => { + it('should return false if there is a rounding error of 0.1%', async () => { + const numerator = new BigNumber(20); + const denominator = new BigNumber(999); + const target = new BigNumber(50); + // rounding error = ((20*50/999) - floor(20*50/999)) / (20*50/999) = 0.1% + const isRoundingError = await exchangeWrapper.isRoundingErrorAsync(numerator, denominator, target); + expect(isRoundingError).to.be.false(); + }); + + it('should return false if there is a rounding of 0.09%', async () => { + const numerator = new BigNumber(20); + const denominator = new BigNumber(9991); + const target = new BigNumber(500); + // rounding error = ((20*500/9991) - floor(20*500/9991)) / (20*500/9991) = 0.09% + const isRoundingError = await exchangeWrapper.isRoundingErrorAsync(numerator, denominator, target); + expect(isRoundingError).to.be.false(); + }); + + it('should return true if there is a rounding error of 0.11%', async () => { + const numerator = new BigNumber(20); + const denominator = new BigNumber(9989); + const target = new BigNumber(500); + // rounding error = ((20*500/9989) - floor(20*500/9989)) / (20*500/9989) = 0.011% + const isRoundingError = await exchangeWrapper.isRoundingErrorAsync(numerator, denominator, target); + expect(isRoundingError).to.be.true(); + }); + + it('should return true if there is a rounding error > 0.1%', async () => { + const numerator = new BigNumber(3); + const denominator = new BigNumber(7); + const target = new BigNumber(10); + // rounding error = ((3*10/7) - floor(3*10/7)) / (3*10/7) = 6.67% + const isRoundingError = await exchangeWrapper.isRoundingErrorAsync(numerator, denominator, target); + expect(isRoundingError).to.be.true(); + }); + + it('should return false when there is no rounding error', async () => { + const numerator = new BigNumber(1); + const denominator = new BigNumber(2); + const target = new BigNumber(10); + + const isRoundingError = await exchangeWrapper.isRoundingErrorAsync(numerator, denominator, target); + expect(isRoundingError).to.be.false(); + }); + + it('should return false when there is rounding error <= 0.1%', async () => { + // randomly generated numbers + const numerator = new BigNumber(76564); + const denominator = new BigNumber(676373677); + const target = new BigNumber(105762562); + // rounding error = ((76564*105762562/676373677) - floor(76564*105762562/676373677)) / + // (76564*105762562/676373677) = 0.0007% + const isRoundingError = await exchangeWrapper.isRoundingErrorAsync(numerator, denominator, target); + expect(isRoundingError).to.be.false(); + }); + }); + + describe('getPartialAmount', () => { + it('should return the numerator/denominator*target', async () => { + const numerator = new BigNumber(1); + const denominator = new BigNumber(2); + const target = new BigNumber(10); + + const partialAmount = await exchangeWrapper.getPartialAmountAsync(numerator, denominator, target); + const expectedPartialAmount = 5; + expect(partialAmount).to.be.bignumber.equal(expectedPartialAmount); + }); + + it('should round down', async () => { + const numerator = new BigNumber(2); + const denominator = new BigNumber(3); + const target = new BigNumber(10); + + const partialAmount = await exchangeWrapper.getPartialAmountAsync(numerator, denominator, target); + const expectedPartialAmount = 6; + expect(partialAmount).to.be.bignumber.equal(expectedPartialAmount); + }); + + it('should round .5 down', async () => { + const numerator = new BigNumber(1); + const denominator = new BigNumber(20); + const target = new BigNumber(10); + + const partialAmount = await exchangeWrapper.getPartialAmountAsync(numerator, denominator, target); + const expectedPartialAmount = 0; + expect(partialAmount).to.be.bignumber.equal(expectedPartialAmount); + }); + }); +}); diff --git a/packages/contracts/test/exchange/wrapper.ts b/packages/contracts/test/exchange/wrapper.ts new file mode 100644 index 000000000..91807fc37 --- /dev/null +++ b/packages/contracts/test/exchange/wrapper.ts @@ -0,0 +1,348 @@ +import { ZeroEx } from '0x.js'; +import { BigNumber } from '@0xproject/utils'; +import * as chai from 'chai'; +import * as _ from 'lodash'; + +import { Artifacts } from '../../util/artifacts'; +import { Balances } from '../../util/balances'; +import { constants } from '../../util/constants'; +import { ExchangeWrapper } from '../../util/exchange_wrapper'; +import { Order } from '../../util/order'; +import { OrderFactory } from '../../util/order_factory'; +import { BalancesByOwner, ContractInstance } from '../../util/types'; +import { chaiSetup } from '../utils/chai_setup'; + +chaiSetup.configure(); +const expect = chai.expect; +const { Exchange, TokenTransferProxy, DummyToken, TokenRegistry } = new Artifacts(artifacts); + +contract('Exchange', (accounts: string[]) => { + const maker = accounts[0]; + const tokenOwner = accounts[0]; + const taker = accounts[1] || accounts[accounts.length - 1]; + const feeRecipient = accounts[2] || accounts[accounts.length - 1]; + + const INIT_BAL = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); + const INIT_ALLOW = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); + + let rep: ContractInstance; + let dgd: ContractInstance; + let zrx: ContractInstance; + let exchange: ContractInstance; + let tokenRegistry: ContractInstance; + + let balances: BalancesByOwner; + + let exWrapper: ExchangeWrapper; + let dmyBalances: Balances; + let orderFactory: OrderFactory; + + before(async () => { + [tokenRegistry, exchange] = await Promise.all([TokenRegistry.deployed(), Exchange.deployed()]); + exWrapper = new ExchangeWrapper(exchange); + const [repAddress, dgdAddress, zrxAddress] = await Promise.all([ + tokenRegistry.getTokenAddressBySymbol('REP'), + tokenRegistry.getTokenAddressBySymbol('DGD'), + tokenRegistry.getTokenAddressBySymbol('ZRX'), + ]); + + const defaultOrderParams = { + exchangeContractAddress: Exchange.address, + maker, + feeRecipient, + makerToken: repAddress, + takerToken: dgdAddress, + makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), + takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), + makerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), + takerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), + }; + orderFactory = new OrderFactory(defaultOrderParams); + + [rep, dgd, zrx] = await Promise.all([ + DummyToken.at(repAddress), + DummyToken.at(dgdAddress), + DummyToken.at(zrxAddress), + ]); + dmyBalances = new Balances([rep, dgd, zrx], [maker, taker, feeRecipient]); + await Promise.all([ + rep.approve(TokenTransferProxy.address, INIT_ALLOW, { from: maker }), + rep.approve(TokenTransferProxy.address, INIT_ALLOW, { from: taker }), + rep.setBalance(maker, INIT_BAL, { from: tokenOwner }), + rep.setBalance(taker, INIT_BAL, { from: tokenOwner }), + dgd.approve(TokenTransferProxy.address, INIT_ALLOW, { from: maker }), + dgd.approve(TokenTransferProxy.address, INIT_ALLOW, { from: taker }), + dgd.setBalance(maker, INIT_BAL, { from: tokenOwner }), + dgd.setBalance(taker, INIT_BAL, { from: tokenOwner }), + zrx.approve(TokenTransferProxy.address, INIT_ALLOW, { from: maker }), + zrx.approve(TokenTransferProxy.address, INIT_ALLOW, { from: taker }), + zrx.setBalance(maker, INIT_BAL, { from: tokenOwner }), + zrx.setBalance(taker, INIT_BAL, { from: tokenOwner }), + ]); + }); + + describe('fillOrKillOrder', () => { + beforeEach(async () => { + balances = await dmyBalances.getAsync(); + }); + + it('should transfer the correct amounts', async () => { + const order = await orderFactory.newSignedOrderAsync({ + makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), + takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), + }); + const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); + await exWrapper.fillOrKillOrderAsync(order, taker, { + fillTakerTokenAmount, + }); + + const newBalances = await dmyBalances.getAsync(); + + const fillMakerTokenAmount = fillTakerTokenAmount + .times(order.params.makerTokenAmount) + .dividedToIntegerBy(order.params.takerTokenAmount); + const makerFee = order.params.makerFee + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + const takerFee = order.params.takerFee + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( + balances[maker][order.params.makerToken].minus(fillMakerTokenAmount), + ); + expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( + balances[maker][order.params.takerToken].add(fillTakerTokenAmount), + ); + expect(newBalances[maker][zrx.address]).to.be.bignumber.equal(balances[maker][zrx.address].minus(makerFee)); + expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( + balances[taker][order.params.takerToken].minus(fillTakerTokenAmount), + ); + expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( + balances[taker][order.params.makerToken].add(fillMakerTokenAmount), + ); + expect(newBalances[taker][zrx.address]).to.be.bignumber.equal(balances[taker][zrx.address].minus(takerFee)); + expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( + balances[feeRecipient][zrx.address].add(makerFee.add(takerFee)), + ); + }); + + it('should throw if an order is expired', async () => { + const order = await orderFactory.newSignedOrderAsync({ + expirationTimestampInSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), + }); + + return expect(exWrapper.fillOrKillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); + }); + + it('should throw if entire fillTakerTokenAmount not filled', async () => { + const order = await orderFactory.newSignedOrderAsync(); + + const from = taker; + await exWrapper.fillOrderAsync(order, from, { + fillTakerTokenAmount: order.params.takerTokenAmount.div(2), + }); + + return expect(exWrapper.fillOrKillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); + }); + }); + + describe('batch functions', () => { + let orders: Order[]; + beforeEach(async () => { + orders = await Promise.all([ + orderFactory.newSignedOrderAsync(), + orderFactory.newSignedOrderAsync(), + orderFactory.newSignedOrderAsync(), + ]); + balances = await dmyBalances.getAsync(); + }); + + describe('batchFillOrders', () => { + it('should transfer the correct amounts', async () => { + const fillTakerTokenAmounts: BigNumber[] = []; + const makerToken = rep.address; + const takerToken = dgd.address; + orders.forEach(order => { + const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); + const fillMakerTokenAmount = fillTakerTokenAmount + .times(order.params.makerTokenAmount) + .dividedToIntegerBy(order.params.takerTokenAmount); + const makerFee = order.params.makerFee + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + const takerFee = order.params.takerFee + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + fillTakerTokenAmounts.push(fillTakerTokenAmount); + balances[maker][makerToken] = balances[maker][makerToken].minus(fillMakerTokenAmount); + balances[maker][takerToken] = balances[maker][takerToken].add(fillTakerTokenAmount); + balances[maker][zrx.address] = balances[maker][zrx.address].minus(makerFee); + balances[taker][makerToken] = balances[taker][makerToken].add(fillMakerTokenAmount); + balances[taker][takerToken] = balances[taker][takerToken].minus(fillTakerTokenAmount); + balances[taker][zrx.address] = balances[taker][zrx.address].minus(takerFee); + balances[feeRecipient][zrx.address] = balances[feeRecipient][zrx.address].add( + makerFee.add(takerFee), + ); + }); + + await exWrapper.batchFillOrdersAsync(orders, taker, { + fillTakerTokenAmounts, + }); + + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + }); + + describe('batchFillOrKillOrders', () => { + it('should transfer the correct amounts', async () => { + const fillTakerTokenAmounts: BigNumber[] = []; + const makerToken = rep.address; + const takerToken = dgd.address; + orders.forEach(order => { + const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); + const fillMakerTokenAmount = fillTakerTokenAmount + .times(order.params.makerTokenAmount) + .dividedToIntegerBy(order.params.takerTokenAmount); + const makerFee = order.params.makerFee + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + const takerFee = order.params.takerFee + .times(fillMakerTokenAmount) + .dividedToIntegerBy(order.params.makerTokenAmount); + fillTakerTokenAmounts.push(fillTakerTokenAmount); + balances[maker][makerToken] = balances[maker][makerToken].minus(fillMakerTokenAmount); + balances[maker][takerToken] = balances[maker][takerToken].add(fillTakerTokenAmount); + balances[maker][zrx.address] = balances[maker][zrx.address].minus(makerFee); + balances[taker][makerToken] = balances[taker][makerToken].add(fillMakerTokenAmount); + balances[taker][takerToken] = balances[taker][takerToken].minus(fillTakerTokenAmount); + balances[taker][zrx.address] = balances[taker][zrx.address].minus(takerFee); + balances[feeRecipient][zrx.address] = balances[feeRecipient][zrx.address].add( + makerFee.add(takerFee), + ); + }); + + await exWrapper.batchFillOrKillOrdersAsync(orders, taker, { + fillTakerTokenAmounts, + }); + + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should throw if a single order does not fill the expected amount', async () => { + const fillTakerTokenAmounts: BigNumber[] = []; + orders.forEach(order => { + const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); + fillTakerTokenAmounts.push(fillTakerTokenAmount); + }); + + await exWrapper.fillOrKillOrderAsync(orders[0], taker); + + return expect( + exWrapper.batchFillOrKillOrdersAsync(orders, taker, { + fillTakerTokenAmounts, + }), + ).to.be.rejectedWith(constants.REVERT); + }); + }); + + describe('fillOrdersUpTo', () => { + it('should stop when the entire fillTakerTokenAmount is filled', async () => { + const fillTakerTokenAmount = orders[0].params.takerTokenAmount.plus( + orders[1].params.takerTokenAmount.div(2), + ); + await exWrapper.fillOrdersUpToAsync(orders, taker, { + fillTakerTokenAmount, + }); + + const newBalances = await dmyBalances.getAsync(); + + const fillMakerTokenAmount = orders[0].params.makerTokenAmount.add( + orders[1].params.makerTokenAmount.dividedToIntegerBy(2), + ); + const makerFee = orders[0].params.makerFee.add(orders[1].params.makerFee.dividedToIntegerBy(2)); + const takerFee = orders[0].params.takerFee.add(orders[1].params.takerFee.dividedToIntegerBy(2)); + expect(newBalances[maker][orders[0].params.makerToken]).to.be.bignumber.equal( + balances[maker][orders[0].params.makerToken].minus(fillMakerTokenAmount), + ); + expect(newBalances[maker][orders[0].params.takerToken]).to.be.bignumber.equal( + balances[maker][orders[0].params.takerToken].add(fillTakerTokenAmount), + ); + expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( + balances[maker][zrx.address].minus(makerFee), + ); + expect(newBalances[taker][orders[0].params.takerToken]).to.be.bignumber.equal( + balances[taker][orders[0].params.takerToken].minus(fillTakerTokenAmount), + ); + expect(newBalances[taker][orders[0].params.makerToken]).to.be.bignumber.equal( + balances[taker][orders[0].params.makerToken].add(fillMakerTokenAmount), + ); + expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( + balances[taker][zrx.address].minus(takerFee), + ); + expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( + balances[feeRecipient][zrx.address].add(makerFee.add(takerFee)), + ); + }); + + it('should fill all orders if cannot fill entire fillTakerTokenAmount', async () => { + const fillTakerTokenAmount = ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18); + orders.forEach(order => { + balances[maker][order.params.makerToken] = balances[maker][order.params.makerToken].minus( + order.params.makerTokenAmount, + ); + balances[maker][order.params.takerToken] = balances[maker][order.params.takerToken].add( + order.params.takerTokenAmount, + ); + balances[maker][zrx.address] = balances[maker][zrx.address].minus(order.params.makerFee); + balances[taker][order.params.makerToken] = balances[taker][order.params.makerToken].add( + order.params.makerTokenAmount, + ); + balances[taker][order.params.takerToken] = balances[taker][order.params.takerToken].minus( + order.params.takerTokenAmount, + ); + balances[taker][zrx.address] = balances[taker][zrx.address].minus(order.params.takerFee); + balances[feeRecipient][zrx.address] = balances[feeRecipient][zrx.address].add( + order.params.makerFee.add(order.params.takerFee), + ); + }); + await exWrapper.fillOrdersUpToAsync(orders, taker, { + fillTakerTokenAmount, + }); + + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should throw when an order does not use the same takerToken', async () => { + orders = await Promise.all([ + orderFactory.newSignedOrderAsync(), + orderFactory.newSignedOrderAsync({ takerToken: zrx.address }), + orderFactory.newSignedOrderAsync(), + ]); + + return expect( + exWrapper.fillOrdersUpToAsync(orders, taker, { + fillTakerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(1000), 18), + }), + ).to.be.rejectedWith(constants.REVERT); + }); + }); + + describe('batchCancelOrders', () => { + it('should be able to cancel multiple orders', async () => { + const cancelTakerTokenAmounts = _.map(orders, order => order.params.takerTokenAmount); + await exWrapper.batchCancelOrdersAsync(orders, maker, { + cancelTakerTokenAmounts, + }); + + await exWrapper.batchFillOrdersAsync(orders, taker, { + fillTakerTokenAmounts: cancelTakerTokenAmounts, + }); + const newBalances = await dmyBalances.getAsync(); + expect(balances).to.be.deep.equal(newBalances); + }); + }); + }); +}); diff --git a/packages/contracts/test/multi_sig_with_time_lock.ts b/packages/contracts/test/multi_sig_with_time_lock.ts new file mode 100644 index 000000000..5cd4dba3d --- /dev/null +++ b/packages/contracts/test/multi_sig_with_time_lock.ts @@ -0,0 +1,115 @@ +import { RPC } from '@0xproject/dev-utils'; +import { BigNumber, promisify } from '@0xproject/utils'; +import * as chai from 'chai'; +import Web3 = require('web3'); + +import * as multiSigWalletJSON from '../../build/contracts/MultiSigWalletWithTimeLock.json'; +import * as truffleConf from '../truffle.js'; +import { Artifacts } from '../util/artifacts'; +import { constants } from '../util/constants'; +import { MultiSigWrapper } from '../util/multi_sig_wrapper'; +import { ContractInstance } from '../util/types'; + +import { chaiSetup } from './utils/chai_setup'; + +const { MultiSigWalletWithTimeLock } = new Artifacts(artifacts); + +const MULTI_SIG_ABI = (multiSigWalletJSON as any).abi; +chaiSetup.configure(); +const expect = chai.expect; + +// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle +// with type `any` to a variable of type `Web3`. +const web3: Web3 = (global as any).web3; + +contract('MultiSigWalletWithTimeLock', (accounts: string[]) => { + const owners = [accounts[0], accounts[1]]; + const SECONDS_TIME_LOCKED = 10000; + + let multiSig: ContractInstance; + let multiSigWrapper: MultiSigWrapper; + let txId: number; + let initialSecondsTimeLocked: number; + let rpc: RPC; + + before(async () => { + multiSig = await MultiSigWalletWithTimeLock.deployed(); + multiSigWrapper = new MultiSigWrapper(multiSig); + + const secondsTimeLocked = await multiSig.secondsTimeLocked.call(); + initialSecondsTimeLocked = secondsTimeLocked.toNumber(); + const rpcUrl = `http://${truffleConf.networks.development.host}:${truffleConf.networks.development.port}`; + rpc = new RPC(rpcUrl); + }); + + describe('changeTimeLock', () => { + it('should throw when not called by wallet', async () => { + return expect(multiSig.changeTimeLock(SECONDS_TIME_LOCKED, { from: owners[0] })).to.be.rejectedWith( + constants.REVERT, + ); + }); + + it('should throw without enough confirmations', async () => { + const destination = multiSig.address; + const from = owners[0]; + const dataParams = { + name: 'changeTimeLock', + abi: MULTI_SIG_ABI, + args: [SECONDS_TIME_LOCKED], + }; + const subRes = await multiSigWrapper.submitTransactionAsync(destination, from, dataParams); + + txId = subRes.logs[0].args.transactionId.toNumber(); + return expect(multiSig.executeTransaction(txId)).to.be.rejectedWith(constants.REVERT); + }); + + it('should set confirmation time with enough confirmations', async () => { + const res = await multiSig.confirmTransaction(txId, { from: owners[1] }); + expect(res.logs).to.have.length(2); + const blockNum = await promisify(web3.eth.getBlockNumber)(); + const blockInfo = await promisify(web3.eth.getBlock)(blockNum); + const timestamp = new BigNumber(blockInfo.timestamp); + const confirmationTimeBigNum = new BigNumber(await multiSig.confirmationTimes.call(txId)); + + expect(timestamp).to.be.bignumber.equal(confirmationTimeBigNum); + }); + + it('should be executable with enough confirmations and secondsTimeLocked of 0', async () => { + expect(initialSecondsTimeLocked).to.be.equal(0); + + const res = await multiSig.executeTransaction(txId); + expect(res.logs).to.have.length(2); + + const secondsTimeLocked = new BigNumber(await multiSig.secondsTimeLocked.call()); + expect(secondsTimeLocked).to.be.bignumber.equal(SECONDS_TIME_LOCKED); + }); + + const newSecondsTimeLocked = 0; + it('should throw if it has enough confirmations but is not past the time lock', async () => { + const destination = multiSig.address; + const from = owners[0]; + const dataParams = { + name: 'changeTimeLock', + abi: MULTI_SIG_ABI, + args: [newSecondsTimeLocked], + }; + const subRes = await multiSigWrapper.submitTransactionAsync(destination, from, dataParams); + + txId = subRes.logs[0].args.transactionId.toNumber(); + const confRes = await multiSig.confirmTransaction(txId, { + from: owners[1], + }); + expect(confRes.logs).to.have.length(2); + + return expect(multiSig.executeTransaction(txId)).to.be.rejectedWith(constants.REVERT); + }); + + it('should execute if it has enough confirmations and is past the time lock', async () => { + await rpc.increaseTimeAsync(SECONDS_TIME_LOCKED); + await multiSig.executeTransaction(txId); + + const secondsTimeLocked = new BigNumber(await multiSig.secondsTimeLocked.call()); + expect(secondsTimeLocked).to.be.bignumber.equal(newSecondsTimeLocked); + }); + }); +}); diff --git a/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts b/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts new file mode 100644 index 000000000..82115418e --- /dev/null +++ b/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts @@ -0,0 +1,150 @@ +import * as chai from 'chai'; + +import * as tokenTransferProxyJSON from '../../build/contracts/TokenTransferProxy.json'; +import { Artifacts } from '../util/artifacts'; +import { constants } from '../util/constants'; +import { crypto } from '../util/crypto'; +import { MultiSigWrapper } from '../util/multi_sig_wrapper'; +import { ContractInstance, TransactionDataParams } from '../util/types'; + +import { chaiSetup } from './utils/chai_setup'; +const { TokenTransferProxy, MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress } = new Artifacts(artifacts); +const PROXY_ABI = (tokenTransferProxyJSON as any).abi; + +chaiSetup.configure(); +const expect = chai.expect; + +contract('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', (accounts: string[]) => { + const owners = [accounts[0], accounts[1]]; + const requiredApprovals = 2; + const SECONDS_TIME_LOCKED = 1000000; + + // initialize fake addresses + const authorizedAddress = `0x${crypto + .solSHA3([accounts[0]]) + .slice(0, 20) + .toString('hex')}`; + const unauthorizedAddress = `0x${crypto + .solSHA3([accounts[1]]) + .slice(0, 20) + .toString('hex')}`; + + let tokenTransferProxy: ContractInstance; + let multiSig: ContractInstance; + let multiSigWrapper: MultiSigWrapper; + + let validDestination: string; + + beforeEach(async () => { + const initialOwner = accounts[0]; + tokenTransferProxy = await TokenTransferProxy.new({ from: initialOwner }); + await tokenTransferProxy.addAuthorizedAddress(authorizedAddress, { + from: initialOwner, + }); + multiSig = await MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress.new( + owners, + requiredApprovals, + SECONDS_TIME_LOCKED, + tokenTransferProxy.address, + ); + await tokenTransferProxy.transferOwnership(multiSig.address, { + from: initialOwner, + }); + multiSigWrapper = new MultiSigWrapper(multiSig); + validDestination = tokenTransferProxy.address; + }); + + describe('isFunctionRemoveAuthorizedAddress', () => { + it('should throw if data is not for removeAuthorizedAddress', async () => { + const data = MultiSigWrapper.encodeFnArgs('addAuthorizedAddress', PROXY_ABI, [owners[0]]); + return expect(multiSig.isFunctionRemoveAuthorizedAddress.call(data)).to.be.rejectedWith(constants.REVERT); + }); + + it('should return true if data is for removeAuthorizedAddress', async () => { + const data = MultiSigWrapper.encodeFnArgs('removeAuthorizedAddress', PROXY_ABI, [owners[0]]); + const isFunctionRemoveAuthorizedAddress = await multiSig.isFunctionRemoveAuthorizedAddress.call(data); + expect(isFunctionRemoveAuthorizedAddress).to.be.true(); + }); + }); + + describe('executeRemoveAuthorizedAddress', () => { + it('should throw without the required confirmations', async () => { + const dataParams: TransactionDataParams = { + name: 'removeAuthorizedAddress', + abi: PROXY_ABI, + args: [authorizedAddress], + }; + const res = await multiSigWrapper.submitTransactionAsync(validDestination, owners[0], dataParams); + const txId = res.logs[0].args.transactionId.toString(); + + return expect(multiSig.executeRemoveAuthorizedAddress(txId)).to.be.rejectedWith(constants.REVERT); + }); + + it('should throw if tx destination is not the tokenTransferProxy', async () => { + const invalidTokenTransferProxy = await TokenTransferProxy.new(); + const invalidDestination = invalidTokenTransferProxy.address; + const dataParams: TransactionDataParams = { + name: 'removeAuthorizedAddress', + abi: PROXY_ABI, + args: [authorizedAddress], + }; + const res = await multiSigWrapper.submitTransactionAsync(invalidDestination, owners[0], dataParams); + const txId = res.logs[0].args.transactionId.toString(); + await multiSig.confirmTransaction(txId, { from: owners[1] }); + const isConfirmed = await multiSig.isConfirmed.call(txId); + expect(isConfirmed).to.be.true(); + + return expect(multiSig.executeRemoveAuthorizedAddress(txId)).to.be.rejectedWith(constants.REVERT); + }); + + it('should throw if tx data is not for removeAuthorizedAddress', async () => { + const dataParams: TransactionDataParams = { + name: 'addAuthorizedAddress', + abi: PROXY_ABI, + args: [unauthorizedAddress], + }; + const res = await multiSigWrapper.submitTransactionAsync(validDestination, owners[0], dataParams); + const txId = res.logs[0].args.transactionId.toString(); + await multiSig.confirmTransaction(txId, { from: owners[1] }); + const isConfirmed = await multiSig.isConfirmed.call(txId); + expect(isConfirmed).to.be.true(); + + return expect(multiSig.executeRemoveAuthorizedAddress(txId)).to.be.rejectedWith(constants.REVERT); + }); + + it('should execute removeAuthorizedAddress for valid tokenTransferProxy if fully confirmed', async () => { + const dataParams: TransactionDataParams = { + name: 'removeAuthorizedAddress', + abi: PROXY_ABI, + args: [authorizedAddress], + }; + const res = await multiSigWrapper.submitTransactionAsync(validDestination, owners[0], dataParams); + const txId = res.logs[0].args.transactionId.toString(); + await multiSig.confirmTransaction(txId, { from: owners[1] }); + const isConfirmed = await multiSig.isConfirmed.call(txId); + expect(isConfirmed).to.be.true(); + await multiSig.executeRemoveAuthorizedAddress(txId); + + const isAuthorized = await tokenTransferProxy.authorized.call(authorizedAddress); + expect(isAuthorized).to.be.false(); + }); + + it('should throw if already executed', async () => { + const dataParams: TransactionDataParams = { + name: 'removeAuthorizedAddress', + abi: PROXY_ABI, + args: [authorizedAddress], + }; + const res = await multiSigWrapper.submitTransactionAsync(validDestination, owners[0], dataParams); + const txId = res.logs[0].args.transactionId.toString(); + await multiSig.confirmTransaction(txId, { from: owners[1] }); + const isConfirmed = await multiSig.isConfirmed.call(txId); + expect(isConfirmed).to.be.true(); + await multiSig.executeRemoveAuthorizedAddress(txId); + const tx = await multiSig.transactions.call(txId); + const isExecuted = tx[3]; + expect(isExecuted).to.be.true(); + return expect(multiSig.executeRemoveAuthorizedAddress(txId)).to.be.rejectedWith(constants.REVERT); + }); + }); +}); diff --git a/packages/contracts/test/token_registry.ts b/packages/contracts/test/token_registry.ts new file mode 100644 index 000000000..9ca18202f --- /dev/null +++ b/packages/contracts/test/token_registry.ts @@ -0,0 +1,234 @@ +import { ZeroEx } from '0x.js'; +import * as chai from 'chai'; +import ethUtil = require('ethereumjs-util'); +import * as _ from 'lodash'; + +import { Artifacts } from '../util/artifacts'; +import { constants } from '../util/constants'; +import { TokenRegWrapper } from '../util/token_registry_wrapper'; +import { ContractInstance } from '../util/types'; + +import { chaiSetup } from './utils/chai_setup'; + +const { TokenRegistry } = new Artifacts(artifacts); +chaiSetup.configure(); +const expect = chai.expect; + +contract('TokenRegistry', (accounts: string[]) => { + const owner = accounts[0]; + const notOwner = accounts[1]; + + const tokenAddress1 = `0x${ethUtil.setLength(ethUtil.toBuffer('0x1'), 20, false).toString('hex')}`; + const tokenAddress2 = `0x${ethUtil.setLength(ethUtil.toBuffer('0x2'), 20, false).toString('hex')}`; + + const token1 = { + address: tokenAddress1, + name: 'testToken1', + symbol: 'TT1', + decimals: 18, + ipfsHash: `0x${ethUtil.sha3('ipfs1').toString('hex')}`, + swarmHash: `0x${ethUtil.sha3('swarm1').toString('hex')}`, + }; + + const token2 = { + address: tokenAddress2, + name: 'testToken2', + symbol: 'TT2', + decimals: 18, + ipfsHash: `0x${ethUtil.sha3('ipfs2').toString('hex')}`, + swarmHash: `0x${ethUtil.sha3('swarm2').toString('hex')}`, + }; + + const nullToken = { + address: ZeroEx.NULL_ADDRESS, + name: '', + symbol: '', + decimals: 0, + ipfsHash: constants.NULL_BYTES, + swarmHash: constants.NULL_BYTES, + }; + + let tokenReg: ContractInstance; + let tokenRegWrapper: TokenRegWrapper; + + beforeEach(async () => { + tokenReg = await TokenRegistry.new(); + tokenRegWrapper = new TokenRegWrapper(tokenReg); + }); + + describe('addToken', () => { + it('should throw when not called by owner', async () => { + return expect(tokenRegWrapper.addTokenAsync(token1, notOwner)).to.be.rejectedWith(constants.REVERT); + }); + + it('should add token metadata when called by owner', async () => { + await tokenRegWrapper.addTokenAsync(token1, owner); + const tokenData = await tokenRegWrapper.getTokenMetaDataAsync(token1.address); + expect(tokenData).to.be.deep.equal(token1); + }); + + it('should throw if token already exists', async () => { + await tokenRegWrapper.addTokenAsync(token1, owner); + + return expect(tokenRegWrapper.addTokenAsync(token1, owner)).to.be.rejectedWith(constants.REVERT); + }); + + it('should throw if token address is null', async () => { + return expect(tokenRegWrapper.addTokenAsync(nullToken, owner)).to.be.rejectedWith(constants.REVERT); + }); + + it('should throw if name already exists', async () => { + await tokenRegWrapper.addTokenAsync(token1, owner); + const duplicateNameToken = _.assign({}, token2, { name: token1.name }); + + return expect(tokenRegWrapper.addTokenAsync(duplicateNameToken, owner)).to.be.rejectedWith( + constants.REVERT, + ); + }); + + it('should throw if symbol already exists', async () => { + await tokenRegWrapper.addTokenAsync(token1, owner); + const duplicateSymbolToken = _.assign({}, token2, { + symbol: token1.symbol, + }); + + return expect(tokenRegWrapper.addTokenAsync(duplicateSymbolToken, owner)).to.be.rejectedWith( + constants.REVERT, + ); + }); + }); + + describe('after addToken', () => { + beforeEach(async () => { + await tokenRegWrapper.addTokenAsync(token1, owner); + }); + + describe('getTokenByName', () => { + it('should return token metadata when given the token name', async () => { + const tokenData = await tokenRegWrapper.getTokenByNameAsync(token1.name); + expect(tokenData).to.be.deep.equal(token1); + }); + }); + + describe('getTokenBySymbol', () => { + it('should return token metadata when given the token symbol', async () => { + const tokenData = await tokenRegWrapper.getTokenBySymbolAsync(token1.symbol); + expect(tokenData).to.be.deep.equal(token1); + }); + }); + + describe('setTokenName', () => { + it('should throw when not called by owner', async () => { + return expect( + tokenReg.setTokenName(token1.address, token2.name, { from: notOwner }), + ).to.be.rejectedWith(constants.REVERT); + }); + + it('should change the token name when called by owner', async () => { + const res = await tokenReg.setTokenName(token1.address, token2.name, { + from: owner, + }); + expect(res.logs).to.have.length(1); + const [newData, oldData] = await Promise.all([ + tokenRegWrapper.getTokenByNameAsync(token2.name), + tokenRegWrapper.getTokenByNameAsync(token1.name), + ]); + + const expectedNewData = _.assign({}, token1, { name: token2.name }); + const expectedOldData = nullToken; + expect(newData).to.be.deep.equal(expectedNewData); + expect(oldData).to.be.deep.equal(expectedOldData); + }); + + it('should throw if the name already exists', async () => { + await tokenRegWrapper.addTokenAsync(token2, owner); + + return expect(tokenReg.setTokenName(token1.address, token2.name, { from: owner })).to.be.rejectedWith( + constants.REVERT, + ); + }); + + it('should throw if token does not exist', async () => { + return expect( + tokenReg.setTokenName(nullToken.address, token2.name, { from: owner }), + ).to.be.rejectedWith(constants.REVERT); + }); + }); + + describe('setTokenSymbol', () => { + it('should throw when not called by owner', async () => { + return expect( + tokenReg.setTokenSymbol(token1.address, token2.symbol, { + from: notOwner, + }), + ).to.be.rejectedWith(constants.REVERT); + }); + + it('should change the token symbol when called by owner', async () => { + const res = await tokenReg.setTokenSymbol(token1.address, token2.symbol, { from: owner }); + expect(res.logs).to.have.length(1); + const [newData, oldData] = await Promise.all([ + tokenRegWrapper.getTokenBySymbolAsync(token2.symbol), + tokenRegWrapper.getTokenBySymbolAsync(token1.symbol), + ]); + + const expectedNewData = _.assign({}, token1, { symbol: token2.symbol }); + const expectedOldData = nullToken; + expect(newData).to.be.deep.equal(expectedNewData); + expect(oldData).to.be.deep.equal(expectedOldData); + }); + + it('should throw if the symbol already exists', async () => { + await tokenRegWrapper.addTokenAsync(token2, owner); + + return expect( + tokenReg.setTokenSymbol(token1.address, token2.symbol, { + from: owner, + }), + ).to.be.rejectedWith(constants.REVERT); + }); + + it('should throw if token does not exist', async () => { + return expect( + tokenReg.setTokenSymbol(nullToken.address, token2.symbol, { + from: owner, + }), + ).to.be.rejectedWith(constants.REVERT); + }); + }); + + describe('removeToken', () => { + it('should throw if not called by owner', async () => { + const index = 0; + return expect(tokenReg.removeToken(token1.address, index, { from: notOwner })).to.be.rejectedWith( + constants.REVERT, + ); + }); + + it('should remove token metadata when called by owner', async () => { + const index = 0; + const res = await tokenReg.removeToken(token1.address, index, { + from: owner, + }); + expect(res.logs).to.have.length(1); + const tokenData = await tokenRegWrapper.getTokenMetaDataAsync(token1.address); + expect(tokenData).to.be.deep.equal(nullToken); + }); + + it('should throw if token does not exist', async () => { + const index = 0; + return expect(tokenReg.removeToken(nullToken.address, index, { from: owner })).to.be.rejectedWith( + constants.REVERT, + ); + }); + + it('should throw if token at given index does not match address', async () => { + await tokenRegWrapper.addTokenAsync(token2, owner); + const incorrectIndex = 0; + return expect(tokenReg.removeToken(token2.address, incorrectIndex, { from: owner })).to.be.rejectedWith( + constants.REVERT, + ); + }); + }); + }); +}); diff --git a/packages/contracts/test/token_transfer_proxy/auth.ts b/packages/contracts/test/token_transfer_proxy/auth.ts new file mode 100644 index 000000000..aa0ad2fc5 --- /dev/null +++ b/packages/contracts/test/token_transfer_proxy/auth.ts @@ -0,0 +1,99 @@ +import * as chai from 'chai'; + +import { constants } from '../../util/constants'; +import { ContractInstance } from '../../util/types'; +import { chaiSetup } from '../utils/chai_setup'; + +chaiSetup.configure(); +const expect = chai.expect; +const TokenTransferProxy = artifacts.require('./db/TokenTransferProxy.sol'); + +contract('TokenTransferProxy', (accounts: string[]) => { + const owner = accounts[0]; + const notOwner = accounts[1]; + + let tokenTransferProxy: ContractInstance; + let authorized: string; + let notAuthorized = owner; + + before(async () => { + tokenTransferProxy = await TokenTransferProxy.deployed(); + }); + + describe('addAuthorizedAddress', () => { + it('should throw if not called by owner', async () => { + return expect(tokenTransferProxy.addAuthorizedAddress(notOwner, { from: notOwner })).to.be.rejectedWith( + constants.REVERT, + ); + }); + + it('should allow owner to add an authorized address', async () => { + await tokenTransferProxy.addAuthorizedAddress(notAuthorized, { + from: owner, + }); + authorized = notAuthorized; + notAuthorized = null; + const isAuthorized = await tokenTransferProxy.authorized.call(authorized); + expect(isAuthorized).to.be.true(); + }); + + it('should throw if owner attempts to authorize a duplicate address', async () => { + return expect(tokenTransferProxy.addAuthorizedAddress(authorized, { from: owner })).to.be.rejectedWith( + constants.REVERT, + ); + }); + }); + + describe('removeAuthorizedAddress', () => { + it('should throw if not called by owner', async () => { + return expect( + tokenTransferProxy.removeAuthorizedAddress(authorized, { + from: notOwner, + }), + ).to.be.rejectedWith(constants.REVERT); + }); + + it('should allow owner to remove an authorized address', async () => { + await tokenTransferProxy.removeAuthorizedAddress(authorized, { + from: owner, + }); + notAuthorized = authorized; + authorized = null; + + const isAuthorized = await tokenTransferProxy.authorized.call(notAuthorized); + expect(isAuthorized).to.be.false(); + }); + + it('should throw if owner attempts to remove an address that is not authorized', async () => { + return expect( + tokenTransferProxy.removeAuthorizedAddress(notAuthorized, { + from: owner, + }), + ).to.be.rejectedWith(constants.REVERT); + }); + }); + + describe('getAuthorizedAddresses', () => { + it('should return all authorized addresses', async () => { + const initial = await tokenTransferProxy.getAuthorizedAddresses(); + expect(initial).to.have.length(1); + await tokenTransferProxy.addAuthorizedAddress(notAuthorized, { + from: owner, + }); + + authorized = notAuthorized; + notAuthorized = null; + const afterAdd = await tokenTransferProxy.getAuthorizedAddresses(); + expect(afterAdd).to.have.length(2); + expect(afterAdd).to.include(authorized); + + await tokenTransferProxy.removeAuthorizedAddress(authorized, { + from: owner, + }); + notAuthorized = authorized; + authorized = null; + const afterRemove = await tokenTransferProxy.getAuthorizedAddresses(); + expect(afterRemove).to.have.length(1); + }); + }); +}); diff --git a/packages/contracts/test/token_transfer_proxy/transfer_from.ts b/packages/contracts/test/token_transfer_proxy/transfer_from.ts new file mode 100644 index 000000000..b5e1c7017 --- /dev/null +++ b/packages/contracts/test/token_transfer_proxy/transfer_from.ts @@ -0,0 +1,94 @@ +import { BlockchainLifecycle } from '@0xproject/dev-utils'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import * as chai from 'chai'; +import * as Web3 from 'web3'; + +import { Artifacts } from '../../util/artifacts'; +import { Balances } from '../../util/balances'; +import { constants } from '../../util/constants'; +import { ContractInstance } from '../../util/types'; +import { chaiSetup } from '../utils/chai_setup'; + +chaiSetup.configure(); +const expect = chai.expect; +const { TokenTransferProxy, DummyToken, TokenRegistry } = new Artifacts(artifacts); +// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle +// with type `any` to a variable of type `Web3`. +const web3: Web3 = (global as any).web3; +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); + +describe('TokenTransferProxy', () => { + const web3Wrapper = new Web3Wrapper(web3.currentProvider); + let accounts: string[]; + let owner: string; + let notAuthorized: string; + const config = { + networkId: constants.TESTRPC_NETWORK_ID, + }; + before(async () => { + accounts = await web3Wrapper.getAvailableAddressesAsync(); + owner = notAuthorized = accounts[0]; + }); + const INIT_BAL = 100000000; + const INIT_ALLOW = 100000000; + + let tokenTransferProxy: ContractInstance; + let tokenRegistry: ContractInstance; + let rep: ContractInstance; + let dmyBalances: Balances; + + before(async () => { + [tokenTransferProxy, tokenRegistry] = await Promise.all([ + TokenTransferProxy.deployed(), + TokenRegistry.deployed(), + ]); + const repAddress = await tokenRegistry.getTokenAddressBySymbol('REP'); + rep = DummyToken.at(repAddress); + + dmyBalances = new Balances([rep], [accounts[0], accounts[1]]); + await Promise.all([ + rep.approve(TokenTransferProxy.address, INIT_ALLOW, { + from: accounts[0], + }), + rep.setBalance(accounts[0], INIT_BAL, { from: owner }), + rep.approve(TokenTransferProxy.address, INIT_ALLOW, { + from: accounts[1], + }), + rep.setBalance(accounts[1], INIT_BAL, { from: owner }), + ]); + }); + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); + + describe('transferFrom', () => { + it('should throw when called by an unauthorized address', async () => { + expect( + tokenTransferProxy.transferFrom(rep.address, accounts[0], accounts[1], 1000, { from: notAuthorized }), + ).to.be.rejectedWith(constants.REVERT); + }); + + it('should allow an authorized address to transfer', async () => { + const balances = await dmyBalances.getAsync(); + + await tokenTransferProxy.addAuthorizedAddress(notAuthorized, { + from: owner, + }); + const transferAmt = 10000; + await tokenTransferProxy.transferFrom(rep.address, accounts[0], accounts[1], transferAmt, { + from: notAuthorized, + }); + + const newBalances = await dmyBalances.getAsync(); + expect(newBalances[accounts[0]][rep.address]).to.be.bignumber.equal( + balances[accounts[0]][rep.address].minus(transferAmt), + ); + expect(newBalances[accounts[1]][rep.address]).to.be.bignumber.equal( + balances[accounts[1]][rep.address].add(transferAmt), + ); + }); + }); +}); diff --git a/packages/contracts/test/ts/ether_token.ts b/packages/contracts/test/ts/ether_token.ts deleted file mode 100644 index f807cdaa3..000000000 --- a/packages/contracts/test/ts/ether_token.ts +++ /dev/null @@ -1,124 +0,0 @@ -import { ZeroEx, ZeroExError } from '0x.js'; -import { BigNumber, promisify } from '@0xproject/utils'; -import * as chai from 'chai'; -import Web3 = require('web3'); - -import { Artifacts } from '../../util/artifacts'; -import { constants } from '../../util/constants'; - -import { chaiSetup } from './utils/chai_setup'; - -const { EtherToken } = new Artifacts(artifacts); - -chaiSetup.configure(); -const expect = chai.expect; - -// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle -// with type `any` to a variable of type `Web3`. -const web3: Web3 = (global as any).web3; - -contract('EtherToken', (accounts: string[]) => { - const account = accounts[0]; - const gasPrice = ZeroEx.toBaseUnitAmount(new BigNumber(20), 9); - let zeroEx: ZeroEx; - let etherTokenAddress: string; - - before(async () => { - etherTokenAddress = EtherToken.address; - zeroEx = new ZeroEx(web3.currentProvider, { - gasPrice, - networkId: constants.TESTRPC_NETWORK_ID, - }); - }); - - const sendTransactionAsync = promisify(web3.eth.sendTransaction); - const getEthBalanceAsync = async (owner: string) => { - const balanceStr = await promisify(web3.eth.getBalance)(owner); - const balance = new BigNumber(balanceStr); - return balance; - }; - - describe('deposit', () => { - it('should throw if caller attempts to deposit more Ether than caller balance', async () => { - const initEthBalance = await getEthBalanceAsync(account); - const ethToDeposit = initEthBalance.plus(1); - - return expect(zeroEx.etherToken.depositAsync(etherTokenAddress, ethToDeposit, account)).to.be.rejectedWith( - ZeroExError.InsufficientEthBalanceForDeposit, - ); - }); - - it('should convert deposited Ether to wrapped Ether tokens', async () => { - const initEthBalance = await getEthBalanceAsync(account); - const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); - - const ethToDeposit = new BigNumber(web3.toWei(1, 'ether')); - - const txHash = await zeroEx.etherToken.depositAsync(etherTokenAddress, ethToDeposit, account); - const receipt = await zeroEx.awaitTransactionMinedAsync(txHash); - - const ethSpentOnGas = gasPrice.times(receipt.gasUsed); - const finalEthBalance = await getEthBalanceAsync(account); - const finalEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); - - expect(finalEthBalance).to.be.bignumber.equal(initEthBalance.minus(ethToDeposit.plus(ethSpentOnGas))); - expect(finalEthTokenBalance).to.be.bignumber.equal(initEthTokenBalance.plus(ethToDeposit)); - }); - }); - - describe('withdraw', () => { - it('should throw if caller attempts to withdraw greater than caller balance', async () => { - const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); - const ethTokensToWithdraw = initEthTokenBalance.plus(1); - - return expect( - zeroEx.etherToken.withdrawAsync(etherTokenAddress, ethTokensToWithdraw, account), - ).to.be.rejectedWith(ZeroExError.InsufficientWEthBalanceForWithdrawal); - }); - - it('should convert ether tokens to ether with sufficient balance', async () => { - const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); - const initEthBalance = await getEthBalanceAsync(account); - const ethTokensToWithdraw = initEthTokenBalance; - expect(ethTokensToWithdraw).to.not.be.bignumber.equal(0); - const txHash = await zeroEx.etherToken.withdrawAsync(etherTokenAddress, ethTokensToWithdraw, account, { - gasLimit: constants.MAX_ETHERTOKEN_WITHDRAW_GAS, - }); - const receipt = await zeroEx.awaitTransactionMinedAsync(txHash); - - const ethSpentOnGas = gasPrice.times(receipt.gasUsed); - const finalEthBalance = await getEthBalanceAsync(account); - const finalEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); - - expect(finalEthBalance).to.be.bignumber.equal( - initEthBalance.plus(ethTokensToWithdraw.minus(ethSpentOnGas)), - ); - expect(finalEthTokenBalance).to.be.bignumber.equal(initEthTokenBalance.minus(ethTokensToWithdraw)); - }); - }); - - describe('fallback', () => { - it('should convert sent ether to ether tokens', async () => { - const initEthBalance = await getEthBalanceAsync(account); - const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); - - const ethToDeposit = ZeroEx.toBaseUnitAmount(new BigNumber(1), 18); - - const txHash = await sendTransactionAsync({ - from: account, - to: etherTokenAddress, - value: ethToDeposit, - gasPrice, - }); - - const receipt = await zeroEx.awaitTransactionMinedAsync(txHash); - - const ethSpentOnGas = gasPrice.times(receipt.gasUsed); - const finalEthBalance = await getEthBalanceAsync(account); - const finalEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); - - expect(finalEthBalance).to.be.bignumber.equal(initEthBalance.minus(ethToDeposit.plus(ethSpentOnGas))); - expect(finalEthTokenBalance).to.be.bignumber.equal(initEthTokenBalance.plus(ethToDeposit)); - }); - }); -}); diff --git a/packages/contracts/test/ts/exchange/core.ts b/packages/contracts/test/ts/exchange/core.ts deleted file mode 100644 index 770ef0c43..000000000 --- a/packages/contracts/test/ts/exchange/core.ts +++ /dev/null @@ -1,852 +0,0 @@ -import { ZeroEx } from '0x.js'; -import { BigNumber } from '@0xproject/utils'; -import * as chai from 'chai'; -import ethUtil = require('ethereumjs-util'); -import * as Web3 from 'web3'; - -import { Artifacts } from '../../../util/artifacts'; -import { Balances } from '../../../util/balances'; -import { constants } from '../../../util/constants'; -import { crypto } from '../../../util/crypto'; -import { ExchangeWrapper } from '../../../util/exchange_wrapper'; -import { Order } from '../../../util/order'; -import { OrderFactory } from '../../../util/order_factory'; -import { BalancesByOwner, ContractInstance, ExchangeContractErrs } from '../../../util/types'; -import { chaiSetup } from '../utils/chai_setup'; - -chaiSetup.configure(); -const expect = chai.expect; -const { Exchange, TokenTransferProxy, DummyToken, TokenRegistry, MaliciousToken } = new Artifacts(artifacts); - -// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle -// with type `any` to a variable of type `Web3`. -const web3: Web3 = (global as any).web3; - -contract('Exchange', (accounts: string[]) => { - const maker = accounts[0]; - const tokenOwner = accounts[0]; - const taker = accounts[1] || accounts[accounts.length - 1]; - const feeRecipient = accounts[2] || accounts[accounts.length - 1]; - - const INITIAL_BALANCE = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); - const INITIAL_ALLOWANCE = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); - - let rep: ContractInstance; - let dgd: ContractInstance; - let zrx: ContractInstance; - let exchange: ContractInstance; - let tokenRegistry: ContractInstance; - - let order: Order; - let balances: BalancesByOwner; - let exWrapper: ExchangeWrapper; - let dmyBalances: Balances; - let orderFactory: OrderFactory; - - let zeroEx: ZeroEx; - - before(async () => { - [tokenRegistry, exchange] = await Promise.all([TokenRegistry.deployed(), Exchange.deployed()]); - exWrapper = new ExchangeWrapper(exchange); - zeroEx = new ZeroEx(web3.currentProvider, { - exchangeContractAddress: exchange.address, - networkId: constants.TESTRPC_NETWORK_ID, - }); - - const [repAddress, dgdAddress, zrxAddress] = await Promise.all([ - tokenRegistry.getTokenAddressBySymbol('REP'), - tokenRegistry.getTokenAddressBySymbol('DGD'), - tokenRegistry.getTokenAddressBySymbol('ZRX'), - ]); - - const defaultOrderParams = { - exchangeContractAddress: Exchange.address, - maker, - feeRecipient, - makerToken: repAddress, - takerToken: dgdAddress, - makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), - takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), - makerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), - takerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), - }; - orderFactory = new OrderFactory(defaultOrderParams); - - [rep, dgd, zrx] = await Promise.all([ - DummyToken.at(repAddress), - DummyToken.at(dgdAddress), - DummyToken.at(zrxAddress), - ]); - dmyBalances = new Balances([rep, dgd, zrx], [maker, taker, feeRecipient]); - await Promise.all([ - rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { - from: maker, - }), - rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { - from: taker, - }), - rep.setBalance(maker, INITIAL_BALANCE, { from: tokenOwner }), - rep.setBalance(taker, INITIAL_BALANCE, { from: tokenOwner }), - dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { - from: maker, - }), - dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { - from: taker, - }), - dgd.setBalance(maker, INITIAL_BALANCE, { from: tokenOwner }), - dgd.setBalance(taker, INITIAL_BALANCE, { from: tokenOwner }), - zrx.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { - from: maker, - }), - zrx.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { - from: taker, - }), - zrx.setBalance(maker, INITIAL_BALANCE, { from: tokenOwner }), - zrx.setBalance(taker, INITIAL_BALANCE, { from: tokenOwner }), - ]); - }); - - describe('internal functions', () => { - it('should include transferViaTokenTransferProxy', () => { - expect(exchange.transferViaTokenTransferProxy).to.be.undefined(); - }); - - it('should include isTransferable', () => { - expect(exchange.isTransferable).to.be.undefined(); - }); - - it('should include getBalance', () => { - expect(exchange.getBalance).to.be.undefined(); - }); - - it('should include getAllowance', () => { - expect(exchange.getAllowance).to.be.undefined(); - }); - }); - - describe('fillOrder', () => { - beforeEach(async () => { - balances = await dmyBalances.getAsync(); - order = await orderFactory.newSignedOrderAsync(); - }); - - it('should create an unfillable order', async () => { - order = await orderFactory.newSignedOrderAsync({ - makerTokenAmount: new BigNumber(1001), - takerTokenAmount: new BigNumber(3), - }); - - const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); - expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); - - const fillTakerTokenAmount1 = new BigNumber(2); - await exWrapper.fillOrderAsync(order, taker, { - fillTakerTokenAmount: fillTakerTokenAmount1, - }); - - const filledTakerTokenAmountAfter1 = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); - expect(filledTakerTokenAmountAfter1).to.be.bignumber.equal(fillTakerTokenAmount1); - - const fillTakerTokenAmount2 = new BigNumber(1); - await exWrapper.fillOrderAsync(order, taker, { - fillTakerTokenAmount: fillTakerTokenAmount2, - }); - - const filledTakerTokenAmountAfter2 = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); - expect(filledTakerTokenAmountAfter2).to.be.bignumber.equal(filledTakerTokenAmountAfter1); - }); - - it('should transfer the correct amounts when makerTokenAmount === takerTokenAmount', async () => { - order = await orderFactory.newSignedOrderAsync({ - makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), - takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), - }); - - const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); - expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); - - const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); - await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); - - const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); - expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(fillTakerTokenAmount); - - const newBalances = await dmyBalances.getAsync(); - - const fillMakerTokenAmount = fillTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); - const paidMakerFee = order.params.makerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - const paidTakerFee = order.params.takerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( - balances[maker][order.params.makerToken].minus(fillMakerTokenAmount), - ); - expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( - balances[maker][order.params.takerToken].add(fillTakerTokenAmount), - ); - expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( - balances[maker][zrx.address].minus(paidMakerFee), - ); - expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( - balances[taker][order.params.takerToken].minus(fillTakerTokenAmount), - ); - expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( - balances[taker][order.params.makerToken].add(fillMakerTokenAmount), - ); - expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( - balances[taker][zrx.address].minus(paidTakerFee), - ); - expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( - balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee)), - ); - }); - - it('should transfer the correct amounts when makerTokenAmount > takerTokenAmount', async () => { - order = await orderFactory.newSignedOrderAsync({ - makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), - takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), - }); - - const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); - expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); - - const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); - await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); - - const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); - expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(fillTakerTokenAmount); - - const newBalances = await dmyBalances.getAsync(); - - const fillMakerTokenAmount = fillTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); - const paidMakerFee = order.params.makerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - const paidTakerFee = order.params.takerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( - balances[maker][order.params.makerToken].minus(fillMakerTokenAmount), - ); - expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( - balances[maker][order.params.takerToken].add(fillTakerTokenAmount), - ); - expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( - balances[maker][zrx.address].minus(paidMakerFee), - ); - expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( - balances[taker][order.params.takerToken].minus(fillTakerTokenAmount), - ); - expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( - balances[taker][order.params.makerToken].add(fillMakerTokenAmount), - ); - expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( - balances[taker][zrx.address].minus(paidTakerFee), - ); - expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( - balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee)), - ); - }); - - it('should transfer the correct amounts when makerTokenAmount < takerTokenAmount', async () => { - order = await orderFactory.newSignedOrderAsync({ - makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), - takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), - }); - - const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); - expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); - - const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); - await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); - - const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); - expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(fillTakerTokenAmount); - - const newBalances = await dmyBalances.getAsync(); - - const fillMakerTokenAmount = fillTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); - const paidMakerFee = order.params.makerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - const paidTakerFee = order.params.takerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( - balances[maker][order.params.makerToken].minus(fillMakerTokenAmount), - ); - expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( - balances[maker][order.params.takerToken].add(fillTakerTokenAmount), - ); - expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( - balances[maker][zrx.address].minus(paidMakerFee), - ); - expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( - balances[taker][order.params.takerToken].minus(fillTakerTokenAmount), - ); - expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( - balances[taker][order.params.makerToken].add(fillMakerTokenAmount), - ); - expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( - balances[taker][zrx.address].minus(paidTakerFee), - ); - expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( - balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee)), - ); - }); - - it('should transfer the correct amounts when taker is specified and order is claimed by taker', async () => { - order = await orderFactory.newSignedOrderAsync({ - taker, - makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), - takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), - }); - - const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); - expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); - - const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); - await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); - - const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); - const expectedFillAmountTAfter = fillTakerTokenAmount.add(filledTakerTokenAmountBefore); - expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(expectedFillAmountTAfter); - - const newBalances = await dmyBalances.getAsync(); - - const fillMakerTokenAmount = fillTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); - const paidMakerFee = order.params.makerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - const paidTakerFee = order.params.takerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( - balances[maker][order.params.makerToken].minus(fillMakerTokenAmount), - ); - expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( - balances[maker][order.params.takerToken].add(fillTakerTokenAmount), - ); - expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( - balances[maker][zrx.address].minus(paidMakerFee), - ); - expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( - balances[taker][order.params.takerToken].minus(fillTakerTokenAmount), - ); - expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( - balances[taker][order.params.makerToken].add(fillMakerTokenAmount), - ); - expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( - balances[taker][zrx.address].minus(paidTakerFee), - ); - expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( - balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee)), - ); - }); - - it('should fill remaining value if fillTakerTokenAmount > remaining takerTokenAmount', async () => { - const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); - await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); - - const res = await exWrapper.fillOrderAsync(order, taker, { - fillTakerTokenAmount: order.params.takerTokenAmount, - }); - - expect(res.logs[0].args.filledTakerTokenAmount).to.be.bignumber.equal( - order.params.takerTokenAmount.minus(fillTakerTokenAmount), - ); - const newBalances = await dmyBalances.getAsync(); - - expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( - balances[maker][order.params.makerToken].minus(order.params.makerTokenAmount), - ); - expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( - balances[maker][order.params.takerToken].add(order.params.takerTokenAmount), - ); - expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( - balances[maker][zrx.address].minus(order.params.makerFee), - ); - expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( - balances[taker][order.params.takerToken].minus(order.params.takerTokenAmount), - ); - expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( - balances[taker][order.params.makerToken].add(order.params.makerTokenAmount), - ); - expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( - balances[taker][zrx.address].minus(order.params.takerFee), - ); - expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( - balances[feeRecipient][zrx.address].add(order.params.makerFee.add(order.params.takerFee)), - ); - }); - - it('should log 1 event with the correct arguments when order has a feeRecipient', async () => { - const divisor = 2; - const res = await exWrapper.fillOrderAsync(order, taker, { - fillTakerTokenAmount: order.params.takerTokenAmount.div(divisor), - }); - expect(res.logs).to.have.length(1); - - const logArgs = res.logs[0].args; - const expectedFilledMakerTokenAmount = order.params.makerTokenAmount.div(divisor); - const expectedFilledTakerTokenAmount = order.params.takerTokenAmount.div(divisor); - const expectedFeeMPaid = order.params.makerFee.div(divisor); - const expectedFeeTPaid = order.params.takerFee.div(divisor); - const tokensHashBuff = crypto.solSHA3([order.params.makerToken, order.params.takerToken]); - const expectedTokens = ethUtil.bufferToHex(tokensHashBuff); - - expect(order.params.maker).to.be.equal(logArgs.maker); - expect(taker).to.be.equal(logArgs.taker); - expect(order.params.feeRecipient).to.be.equal(logArgs.feeRecipient); - expect(order.params.makerToken).to.be.equal(logArgs.makerToken); - expect(order.params.takerToken).to.be.equal(logArgs.takerToken); - expect(expectedFilledMakerTokenAmount).to.be.bignumber.equal(logArgs.filledMakerTokenAmount); - expect(expectedFilledTakerTokenAmount).to.be.bignumber.equal(logArgs.filledTakerTokenAmount); - expect(expectedFeeMPaid).to.be.bignumber.equal(logArgs.paidMakerFee); - expect(expectedFeeTPaid).to.be.bignumber.equal(logArgs.paidTakerFee); - expect(expectedTokens).to.be.equal(logArgs.tokens); - expect(order.params.orderHashHex).to.be.equal(logArgs.orderHash); - }); - - it('should log 1 event with the correct arguments when order has no feeRecipient', async () => { - order = await orderFactory.newSignedOrderAsync({ - feeRecipient: ZeroEx.NULL_ADDRESS, - }); - const divisor = 2; - const res = await exWrapper.fillOrderAsync(order, taker, { - fillTakerTokenAmount: order.params.takerTokenAmount.div(divisor), - }); - expect(res.logs).to.have.length(1); - - const logArgs = res.logs[0].args; - const expectedFilledMakerTokenAmount = order.params.makerTokenAmount.div(divisor); - const expectedFilledTakerTokenAmount = order.params.takerTokenAmount.div(divisor); - const expectedFeeMPaid = new BigNumber(0); - const expectedFeeTPaid = new BigNumber(0); - const tokensHashBuff = crypto.solSHA3([order.params.makerToken, order.params.takerToken]); - const expectedTokens = ethUtil.bufferToHex(tokensHashBuff); - - expect(order.params.maker).to.be.equal(logArgs.maker); - expect(taker).to.be.equal(logArgs.taker); - expect(order.params.feeRecipient).to.be.equal(logArgs.feeRecipient); - expect(order.params.makerToken).to.be.equal(logArgs.makerToken); - expect(order.params.takerToken).to.be.equal(logArgs.takerToken); - expect(expectedFilledMakerTokenAmount).to.be.bignumber.equal(logArgs.filledMakerTokenAmount); - expect(expectedFilledTakerTokenAmount).to.be.bignumber.equal(logArgs.filledTakerTokenAmount); - expect(expectedFeeMPaid).to.be.bignumber.equal(logArgs.paidMakerFee); - expect(expectedFeeTPaid).to.be.bignumber.equal(logArgs.paidTakerFee); - expect(expectedTokens).to.be.equal(logArgs.tokens); - expect(order.params.orderHashHex).to.be.equal(logArgs.orderHash); - }); - - it('should throw when taker is specified and order is claimed by other', async () => { - order = await orderFactory.newSignedOrderAsync({ - taker: feeRecipient, - makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), - takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), - }); - - return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); - }); - - it('should throw if signature is invalid', async () => { - order = await orderFactory.newSignedOrderAsync({ - makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(10), 18), - }); - - order.params.r = ethUtil.bufferToHex(ethUtil.sha3('invalidR')); - order.params.s = ethUtil.bufferToHex(ethUtil.sha3('invalidS')); - return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); - }); - - it('should throw if makerTokenAmount is 0', async () => { - order = await orderFactory.newSignedOrderAsync({ - makerTokenAmount: new BigNumber(0), - }); - - return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); - }); - - it('should throw if takerTokenAmount is 0', async () => { - order = await orderFactory.newSignedOrderAsync({ - takerTokenAmount: new BigNumber(0), - }); - - return expect(exWrapper.fillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); - }); - - it('should throw if fillTakerTokenAmount is 0', async () => { - order = await orderFactory.newSignedOrderAsync(); - - return expect( - exWrapper.fillOrderAsync(order, taker, { - fillTakerTokenAmount: new BigNumber(0), - }), - ).to.be.rejectedWith(constants.REVERT); - }); - - it('should not change balances if maker balances are too low to fill order and \ - shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - order = await orderFactory.newSignedOrderAsync({ - makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), - }); - - await exWrapper.fillOrderAsync(order, taker); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); - }); - - it('should throw if maker balances are too low to fill order and \ - shouldThrowOnInsufficientBalanceOrAllowance = true', async () => { - order = await orderFactory.newSignedOrderAsync({ - makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), - }); - - return expect( - exWrapper.fillOrderAsync(order, taker, { - shouldThrowOnInsufficientBalanceOrAllowance: true, - }), - ).to.be.rejectedWith(constants.REVERT); - }); - - it('should not change balances if taker balances are too low to fill order and \ - shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - order = await orderFactory.newSignedOrderAsync({ - takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), - }); - - await exWrapper.fillOrderAsync(order, taker); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); - }); - - it('should throw if taker balances are too low to fill order and \ - shouldThrowOnInsufficientBalanceOrAllowance = true', async () => { - order = await orderFactory.newSignedOrderAsync({ - takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), - }); - - return expect( - exWrapper.fillOrderAsync(order, taker, { - shouldThrowOnInsufficientBalanceOrAllowance: true, - }), - ).to.be.rejectedWith(constants.REVERT); - }); - - it('should not change balances if maker allowances are too low to fill order and \ - shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - await rep.approve(TokenTransferProxy.address, 0, { from: maker }); - await exWrapper.fillOrderAsync(order, taker); - await rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { - from: maker, - }); - - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); - }); - - it('should throw if maker allowances are too low to fill order and \ - shouldThrowOnInsufficientBalanceOrAllowance = true', async () => { - await rep.approve(TokenTransferProxy.address, 0, { from: maker }); - expect( - exWrapper.fillOrderAsync(order, taker, { - shouldThrowOnInsufficientBalanceOrAllowance: true, - }), - ).to.be.rejectedWith(constants.REVERT); - await rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { - from: maker, - }); - }); - - it('should not change balances if taker allowances are too low to fill order and \ - shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - await dgd.approve(TokenTransferProxy.address, 0, { from: taker }); - await exWrapper.fillOrderAsync(order, taker); - await dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { - from: taker, - }); - - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); - }); - - it('should throw if taker allowances are too low to fill order and \ - shouldThrowOnInsufficientBalanceOrAllowance = true', async () => { - await dgd.approve(TokenTransferProxy.address, 0, { from: taker }); - expect( - exWrapper.fillOrderAsync(order, taker, { - shouldThrowOnInsufficientBalanceOrAllowance: true, - }), - ).to.be.rejectedWith(constants.REVERT); - await dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { - from: taker, - }); - }); - - it('should not change balances if makerToken is ZRX, makerTokenAmount + makerFee > maker balance, \ - and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - const makerZRXBalance = new BigNumber(balances[maker][zrx.address]); - order = await orderFactory.newSignedOrderAsync({ - makerToken: zrx.address, - makerTokenAmount: makerZRXBalance, - makerFee: new BigNumber(1), - }); - await exWrapper.fillOrderAsync(order, taker); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); - }); - - it('should not change balances if makerToken is ZRX, makerTokenAmount + makerFee > maker allowance, \ - and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - const makerZRXAllowance = await zrx.allowance(maker, TokenTransferProxy.address); - order = await orderFactory.newSignedOrderAsync({ - makerToken: zrx.address, - makerTokenAmount: new BigNumber(makerZRXAllowance), - makerFee: new BigNumber(1), - }); - await exWrapper.fillOrderAsync(order, taker); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); - }); - - it('should not change balances if takerToken is ZRX, takerTokenAmount + takerFee > taker balance, \ - and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - const takerZRXBalance = new BigNumber(balances[taker][zrx.address]); - order = await orderFactory.newSignedOrderAsync({ - takerToken: zrx.address, - takerTokenAmount: takerZRXBalance, - takerFee: new BigNumber(1), - }); - await exWrapper.fillOrderAsync(order, taker); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); - }); - - it('should not change balances if takerToken is ZRX, takerTokenAmount + takerFee > taker allowance, \ - and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - const takerZRXAllowance = await zrx.allowance(taker, TokenTransferProxy.address); - order = await orderFactory.newSignedOrderAsync({ - takerToken: zrx.address, - takerTokenAmount: new BigNumber(takerZRXAllowance), - takerFee: new BigNumber(1), - }); - await exWrapper.fillOrderAsync(order, taker); - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); - }); - - it('should throw if getBalance or getAllowance attempts to change state and \ - shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - const maliciousToken = await MaliciousToken.new(); - await maliciousToken.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { from: taker }); - - order = await orderFactory.newSignedOrderAsync({ - takerToken: maliciousToken.address, - }); - - return expect( - exWrapper.fillOrderAsync(order, taker, { - shouldThrowOnInsufficientBalanceOrAllowance: false, - }), - ).to.be.rejectedWith(constants.REVERT); - }); - - it('should not change balances if an order is expired', async () => { - order = await orderFactory.newSignedOrderAsync({ - expirationTimestampInSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), - }); - await exWrapper.fillOrderAsync(order, taker); - - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); - }); - - it('should log an error event if an order is expired', async () => { - order = await orderFactory.newSignedOrderAsync({ - expirationTimestampInSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), - }); - - const res = await exWrapper.fillOrderAsync(order, taker); - expect(res.logs).to.have.length(1); - const errCode = res.logs[0].args.errorId.toNumber(); - expect(errCode).to.be.equal(ExchangeContractErrs.ERROR_ORDER_EXPIRED); - }); - - it('should log an error event if no value is filled', async () => { - await exWrapper.fillOrderAsync(order, taker); - - const res = await exWrapper.fillOrderAsync(order, taker); - expect(res.logs).to.have.length(1); - const errCode = res.logs[0].args.errorId.toNumber(); - expect(errCode).to.be.equal(ExchangeContractErrs.ERROR_ORDER_FULLY_FILLED_OR_CANCELLED); - }); - }); - - describe('cancelOrder', () => { - beforeEach(async () => { - balances = await dmyBalances.getAsync(); - order = await orderFactory.newSignedOrderAsync(); - }); - - it('should throw if not sent by maker', async () => { - return expect(exWrapper.cancelOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); - }); - - it('should throw if makerTokenAmount is 0', async () => { - order = await orderFactory.newSignedOrderAsync({ - makerTokenAmount: new BigNumber(0), - }); - - return expect(exWrapper.cancelOrderAsync(order, maker)).to.be.rejectedWith(constants.REVERT); - }); - - it('should throw if takerTokenAmount is 0', async () => { - order = await orderFactory.newSignedOrderAsync({ - takerTokenAmount: new BigNumber(0), - }); - - return expect(exWrapper.cancelOrderAsync(order, maker)).to.be.rejectedWith(constants.REVERT); - }); - - it('should throw if cancelTakerTokenAmount is 0', async () => { - order = await orderFactory.newSignedOrderAsync(); - - return expect( - exWrapper.cancelOrderAsync(order, maker, { - cancelTakerTokenAmount: new BigNumber(0), - }), - ).to.be.rejectedWith(constants.REVERT); - }); - - it('should be able to cancel a full order', async () => { - await exWrapper.cancelOrderAsync(order, maker); - await exWrapper.fillOrderAsync(order, taker, { - fillTakerTokenAmount: order.params.takerTokenAmount.div(2), - }); - - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); - }); - - it('should be able to cancel part of an order', async () => { - const cancelTakerTokenAmount = order.params.takerTokenAmount.div(2); - await exWrapper.cancelOrderAsync(order, maker, { - cancelTakerTokenAmount, - }); - - const res = await exWrapper.fillOrderAsync(order, taker, { - fillTakerTokenAmount: order.params.takerTokenAmount, - }); - expect(res.logs[0].args.filledTakerTokenAmount).to.be.bignumber.equal( - order.params.takerTokenAmount.minus(cancelTakerTokenAmount), - ); - - const newBalances = await dmyBalances.getAsync(); - const cancelMakerTokenAmount = cancelTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); - const paidMakerFee = order.params.makerFee - .times(cancelMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - const paidTakerFee = order.params.takerFee - .times(cancelMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( - balances[maker][order.params.makerToken].minus(cancelMakerTokenAmount), - ); - expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( - balances[maker][order.params.takerToken].add(cancelTakerTokenAmount), - ); - expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( - balances[maker][zrx.address].minus(paidMakerFee), - ); - expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( - balances[taker][order.params.takerToken].minus(cancelTakerTokenAmount), - ); - expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( - balances[taker][order.params.makerToken].add(cancelMakerTokenAmount), - ); - expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( - balances[taker][zrx.address].minus(paidTakerFee), - ); - expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( - balances[feeRecipient][zrx.address].add(paidMakerFee.add(paidTakerFee)), - ); - }); - - it('should log 1 event with correct arguments', async () => { - const divisor = 2; - const res = await exWrapper.cancelOrderAsync(order, maker, { - cancelTakerTokenAmount: order.params.takerTokenAmount.div(divisor), - }); - expect(res.logs).to.have.length(1); - - const logArgs = res.logs[0].args; - const expectedCancelledMakerTokenAmount = order.params.makerTokenAmount.div(divisor); - const expectedCancelledTakerTokenAmount = order.params.takerTokenAmount.div(divisor); - const tokensHashBuff = crypto.solSHA3([order.params.makerToken, order.params.takerToken]); - const expectedTokens = ethUtil.bufferToHex(tokensHashBuff); - - expect(order.params.maker).to.be.equal(logArgs.maker); - expect(order.params.feeRecipient).to.be.equal(logArgs.feeRecipient); - expect(order.params.makerToken).to.be.equal(logArgs.makerToken); - expect(order.params.takerToken).to.be.equal(logArgs.takerToken); - expect(expectedCancelledMakerTokenAmount).to.be.bignumber.equal(logArgs.cancelledMakerTokenAmount); - expect(expectedCancelledTakerTokenAmount).to.be.bignumber.equal(logArgs.cancelledTakerTokenAmount); - expect(expectedTokens).to.be.equal(logArgs.tokens); - expect(order.params.orderHashHex).to.be.equal(logArgs.orderHash); - }); - - it('should not log events if no value is cancelled', async () => { - await exWrapper.cancelOrderAsync(order, maker); - - const res = await exWrapper.cancelOrderAsync(order, maker); - expect(res.logs).to.have.length(1); - const errCode = res.logs[0].args.errorId.toNumber(); - expect(errCode).to.be.equal(ExchangeContractErrs.ERROR_ORDER_FULLY_FILLED_OR_CANCELLED); - }); - - it('should not log events if order is expired', async () => { - order = await orderFactory.newSignedOrderAsync({ - expirationTimestampInSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), - }); - - const res = await exWrapper.cancelOrderAsync(order, maker); - expect(res.logs).to.have.length(1); - const errCode = res.logs[0].args.errorId.toNumber(); - expect(errCode).to.be.equal(ExchangeContractErrs.ERROR_ORDER_EXPIRED); - }); - }); -}); // tslint:disable-line:max-file-line-count diff --git a/packages/contracts/test/ts/exchange/helpers.ts b/packages/contracts/test/ts/exchange/helpers.ts deleted file mode 100644 index 95f68e419..000000000 --- a/packages/contracts/test/ts/exchange/helpers.ts +++ /dev/null @@ -1,167 +0,0 @@ -import { ZeroEx } from '0x.js'; -import { BigNumber } from '@0xproject/utils'; -import * as chai from 'chai'; -import ethUtil = require('ethereumjs-util'); - -import { Artifacts } from '../../../util/artifacts'; -import { ExchangeWrapper } from '../../../util/exchange_wrapper'; -import { Order } from '../../../util/order'; -import { OrderFactory } from '../../../util/order_factory'; -import { chaiSetup } from '../utils/chai_setup'; - -chaiSetup.configure(); -const expect = chai.expect; - -const { Exchange, TokenRegistry } = new Artifacts(artifacts); - -contract('Exchange', (accounts: string[]) => { - const maker = accounts[0]; - const feeRecipient = accounts[1] || accounts[accounts.length - 1]; - - let order: Order; - let exchangeWrapper: ExchangeWrapper; - let orderFactory: OrderFactory; - - before(async () => { - const [tokenRegistry, exchange] = await Promise.all([TokenRegistry.deployed(), Exchange.deployed()]); - exchangeWrapper = new ExchangeWrapper(exchange); - const [repAddress, dgdAddress] = await Promise.all([ - tokenRegistry.getTokenAddressBySymbol('REP'), - tokenRegistry.getTokenAddressBySymbol('DGD'), - ]); - const defaultOrderParams = { - exchangeContractAddress: Exchange.address, - maker, - feeRecipient, - makerToken: repAddress, - takerToken: dgdAddress, - makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), - takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), - makerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), - takerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), - }; - orderFactory = new OrderFactory(defaultOrderParams); - }); - - beforeEach(async () => { - order = await orderFactory.newSignedOrderAsync(); - }); - - describe('getOrderHash', () => { - it('should output the correct orderHash', async () => { - const orderHashHex = await exchangeWrapper.getOrderHashAsync(order); - expect(order.params.orderHashHex).to.be.equal(orderHashHex); - }); - }); - - describe('isValidSignature', () => { - beforeEach(async () => { - order = await orderFactory.newSignedOrderAsync(); - }); - - it('should return true with a valid signature', async () => { - const success = await exchangeWrapper.isValidSignatureAsync(order); - const isValidSignature = order.isValidSignature(); - expect(isValidSignature).to.be.true(); - expect(success).to.be.true(); - }); - - it('should return false with an invalid signature', async () => { - order.params.r = ethUtil.bufferToHex(ethUtil.sha3('invalidR')); - order.params.s = ethUtil.bufferToHex(ethUtil.sha3('invalidS')); - const success = await exchangeWrapper.isValidSignatureAsync(order); - expect(order.isValidSignature()).to.be.false(); - expect(success).to.be.false(); - }); - }); - - describe('isRoundingError', () => { - it('should return false if there is a rounding error of 0.1%', async () => { - const numerator = new BigNumber(20); - const denominator = new BigNumber(999); - const target = new BigNumber(50); - // rounding error = ((20*50/999) - floor(20*50/999)) / (20*50/999) = 0.1% - const isRoundingError = await exchangeWrapper.isRoundingErrorAsync(numerator, denominator, target); - expect(isRoundingError).to.be.false(); - }); - - it('should return false if there is a rounding of 0.09%', async () => { - const numerator = new BigNumber(20); - const denominator = new BigNumber(9991); - const target = new BigNumber(500); - // rounding error = ((20*500/9991) - floor(20*500/9991)) / (20*500/9991) = 0.09% - const isRoundingError = await exchangeWrapper.isRoundingErrorAsync(numerator, denominator, target); - expect(isRoundingError).to.be.false(); - }); - - it('should return true if there is a rounding error of 0.11%', async () => { - const numerator = new BigNumber(20); - const denominator = new BigNumber(9989); - const target = new BigNumber(500); - // rounding error = ((20*500/9989) - floor(20*500/9989)) / (20*500/9989) = 0.011% - const isRoundingError = await exchangeWrapper.isRoundingErrorAsync(numerator, denominator, target); - expect(isRoundingError).to.be.true(); - }); - - it('should return true if there is a rounding error > 0.1%', async () => { - const numerator = new BigNumber(3); - const denominator = new BigNumber(7); - const target = new BigNumber(10); - // rounding error = ((3*10/7) - floor(3*10/7)) / (3*10/7) = 6.67% - const isRoundingError = await exchangeWrapper.isRoundingErrorAsync(numerator, denominator, target); - expect(isRoundingError).to.be.true(); - }); - - it('should return false when there is no rounding error', async () => { - const numerator = new BigNumber(1); - const denominator = new BigNumber(2); - const target = new BigNumber(10); - - const isRoundingError = await exchangeWrapper.isRoundingErrorAsync(numerator, denominator, target); - expect(isRoundingError).to.be.false(); - }); - - it('should return false when there is rounding error <= 0.1%', async () => { - // randomly generated numbers - const numerator = new BigNumber(76564); - const denominator = new BigNumber(676373677); - const target = new BigNumber(105762562); - // rounding error = ((76564*105762562/676373677) - floor(76564*105762562/676373677)) / - // (76564*105762562/676373677) = 0.0007% - const isRoundingError = await exchangeWrapper.isRoundingErrorAsync(numerator, denominator, target); - expect(isRoundingError).to.be.false(); - }); - }); - - describe('getPartialAmount', () => { - it('should return the numerator/denominator*target', async () => { - const numerator = new BigNumber(1); - const denominator = new BigNumber(2); - const target = new BigNumber(10); - - const partialAmount = await exchangeWrapper.getPartialAmountAsync(numerator, denominator, target); - const expectedPartialAmount = 5; - expect(partialAmount).to.be.bignumber.equal(expectedPartialAmount); - }); - - it('should round down', async () => { - const numerator = new BigNumber(2); - const denominator = new BigNumber(3); - const target = new BigNumber(10); - - const partialAmount = await exchangeWrapper.getPartialAmountAsync(numerator, denominator, target); - const expectedPartialAmount = 6; - expect(partialAmount).to.be.bignumber.equal(expectedPartialAmount); - }); - - it('should round .5 down', async () => { - const numerator = new BigNumber(1); - const denominator = new BigNumber(20); - const target = new BigNumber(10); - - const partialAmount = await exchangeWrapper.getPartialAmountAsync(numerator, denominator, target); - const expectedPartialAmount = 0; - expect(partialAmount).to.be.bignumber.equal(expectedPartialAmount); - }); - }); -}); diff --git a/packages/contracts/test/ts/exchange/wrapper.ts b/packages/contracts/test/ts/exchange/wrapper.ts deleted file mode 100644 index e69e08bcf..000000000 --- a/packages/contracts/test/ts/exchange/wrapper.ts +++ /dev/null @@ -1,348 +0,0 @@ -import { ZeroEx } from '0x.js'; -import { BigNumber } from '@0xproject/utils'; -import * as chai from 'chai'; -import * as _ from 'lodash'; - -import { Artifacts } from '../../../util/artifacts'; -import { Balances } from '../../../util/balances'; -import { constants } from '../../../util/constants'; -import { ExchangeWrapper } from '../../../util/exchange_wrapper'; -import { Order } from '../../../util/order'; -import { OrderFactory } from '../../../util/order_factory'; -import { BalancesByOwner, ContractInstance } from '../../../util/types'; -import { chaiSetup } from '../utils/chai_setup'; - -chaiSetup.configure(); -const expect = chai.expect; -const { Exchange, TokenTransferProxy, DummyToken, TokenRegistry } = new Artifacts(artifacts); - -contract('Exchange', (accounts: string[]) => { - const maker = accounts[0]; - const tokenOwner = accounts[0]; - const taker = accounts[1] || accounts[accounts.length - 1]; - const feeRecipient = accounts[2] || accounts[accounts.length - 1]; - - const INIT_BAL = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); - const INIT_ALLOW = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); - - let rep: ContractInstance; - let dgd: ContractInstance; - let zrx: ContractInstance; - let exchange: ContractInstance; - let tokenRegistry: ContractInstance; - - let balances: BalancesByOwner; - - let exWrapper: ExchangeWrapper; - let dmyBalances: Balances; - let orderFactory: OrderFactory; - - before(async () => { - [tokenRegistry, exchange] = await Promise.all([TokenRegistry.deployed(), Exchange.deployed()]); - exWrapper = new ExchangeWrapper(exchange); - const [repAddress, dgdAddress, zrxAddress] = await Promise.all([ - tokenRegistry.getTokenAddressBySymbol('REP'), - tokenRegistry.getTokenAddressBySymbol('DGD'), - tokenRegistry.getTokenAddressBySymbol('ZRX'), - ]); - - const defaultOrderParams = { - exchangeContractAddress: Exchange.address, - maker, - feeRecipient, - makerToken: repAddress, - takerToken: dgdAddress, - makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), - takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), - makerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), - takerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), - }; - orderFactory = new OrderFactory(defaultOrderParams); - - [rep, dgd, zrx] = await Promise.all([ - DummyToken.at(repAddress), - DummyToken.at(dgdAddress), - DummyToken.at(zrxAddress), - ]); - dmyBalances = new Balances([rep, dgd, zrx], [maker, taker, feeRecipient]); - await Promise.all([ - rep.approve(TokenTransferProxy.address, INIT_ALLOW, { from: maker }), - rep.approve(TokenTransferProxy.address, INIT_ALLOW, { from: taker }), - rep.setBalance(maker, INIT_BAL, { from: tokenOwner }), - rep.setBalance(taker, INIT_BAL, { from: tokenOwner }), - dgd.approve(TokenTransferProxy.address, INIT_ALLOW, { from: maker }), - dgd.approve(TokenTransferProxy.address, INIT_ALLOW, { from: taker }), - dgd.setBalance(maker, INIT_BAL, { from: tokenOwner }), - dgd.setBalance(taker, INIT_BAL, { from: tokenOwner }), - zrx.approve(TokenTransferProxy.address, INIT_ALLOW, { from: maker }), - zrx.approve(TokenTransferProxy.address, INIT_ALLOW, { from: taker }), - zrx.setBalance(maker, INIT_BAL, { from: tokenOwner }), - zrx.setBalance(taker, INIT_BAL, { from: tokenOwner }), - ]); - }); - - describe('fillOrKillOrder', () => { - beforeEach(async () => { - balances = await dmyBalances.getAsync(); - }); - - it('should transfer the correct amounts', async () => { - const order = await orderFactory.newSignedOrderAsync({ - makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), - takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), - }); - const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); - await exWrapper.fillOrKillOrderAsync(order, taker, { - fillTakerTokenAmount, - }); - - const newBalances = await dmyBalances.getAsync(); - - const fillMakerTokenAmount = fillTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); - const makerFee = order.params.makerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - const takerFee = order.params.takerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( - balances[maker][order.params.makerToken].minus(fillMakerTokenAmount), - ); - expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( - balances[maker][order.params.takerToken].add(fillTakerTokenAmount), - ); - expect(newBalances[maker][zrx.address]).to.be.bignumber.equal(balances[maker][zrx.address].minus(makerFee)); - expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal( - balances[taker][order.params.takerToken].minus(fillTakerTokenAmount), - ); - expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( - balances[taker][order.params.makerToken].add(fillMakerTokenAmount), - ); - expect(newBalances[taker][zrx.address]).to.be.bignumber.equal(balances[taker][zrx.address].minus(takerFee)); - expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( - balances[feeRecipient][zrx.address].add(makerFee.add(takerFee)), - ); - }); - - it('should throw if an order is expired', async () => { - const order = await orderFactory.newSignedOrderAsync({ - expirationTimestampInSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), - }); - - return expect(exWrapper.fillOrKillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); - }); - - it('should throw if entire fillTakerTokenAmount not filled', async () => { - const order = await orderFactory.newSignedOrderAsync(); - - const from = taker; - await exWrapper.fillOrderAsync(order, from, { - fillTakerTokenAmount: order.params.takerTokenAmount.div(2), - }); - - return expect(exWrapper.fillOrKillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); - }); - }); - - describe('batch functions', () => { - let orders: Order[]; - beforeEach(async () => { - orders = await Promise.all([ - orderFactory.newSignedOrderAsync(), - orderFactory.newSignedOrderAsync(), - orderFactory.newSignedOrderAsync(), - ]); - balances = await dmyBalances.getAsync(); - }); - - describe('batchFillOrders', () => { - it('should transfer the correct amounts', async () => { - const fillTakerTokenAmounts: BigNumber[] = []; - const makerToken = rep.address; - const takerToken = dgd.address; - orders.forEach(order => { - const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); - const fillMakerTokenAmount = fillTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); - const makerFee = order.params.makerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - const takerFee = order.params.takerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - fillTakerTokenAmounts.push(fillTakerTokenAmount); - balances[maker][makerToken] = balances[maker][makerToken].minus(fillMakerTokenAmount); - balances[maker][takerToken] = balances[maker][takerToken].add(fillTakerTokenAmount); - balances[maker][zrx.address] = balances[maker][zrx.address].minus(makerFee); - balances[taker][makerToken] = balances[taker][makerToken].add(fillMakerTokenAmount); - balances[taker][takerToken] = balances[taker][takerToken].minus(fillTakerTokenAmount); - balances[taker][zrx.address] = balances[taker][zrx.address].minus(takerFee); - balances[feeRecipient][zrx.address] = balances[feeRecipient][zrx.address].add( - makerFee.add(takerFee), - ); - }); - - await exWrapper.batchFillOrdersAsync(orders, taker, { - fillTakerTokenAmounts, - }); - - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); - }); - }); - - describe('batchFillOrKillOrders', () => { - it('should transfer the correct amounts', async () => { - const fillTakerTokenAmounts: BigNumber[] = []; - const makerToken = rep.address; - const takerToken = dgd.address; - orders.forEach(order => { - const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); - const fillMakerTokenAmount = fillTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); - const makerFee = order.params.makerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - const takerFee = order.params.takerFee - .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - fillTakerTokenAmounts.push(fillTakerTokenAmount); - balances[maker][makerToken] = balances[maker][makerToken].minus(fillMakerTokenAmount); - balances[maker][takerToken] = balances[maker][takerToken].add(fillTakerTokenAmount); - balances[maker][zrx.address] = balances[maker][zrx.address].minus(makerFee); - balances[taker][makerToken] = balances[taker][makerToken].add(fillMakerTokenAmount); - balances[taker][takerToken] = balances[taker][takerToken].minus(fillTakerTokenAmount); - balances[taker][zrx.address] = balances[taker][zrx.address].minus(takerFee); - balances[feeRecipient][zrx.address] = balances[feeRecipient][zrx.address].add( - makerFee.add(takerFee), - ); - }); - - await exWrapper.batchFillOrKillOrdersAsync(orders, taker, { - fillTakerTokenAmounts, - }); - - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); - }); - - it('should throw if a single order does not fill the expected amount', async () => { - const fillTakerTokenAmounts: BigNumber[] = []; - orders.forEach(order => { - const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); - fillTakerTokenAmounts.push(fillTakerTokenAmount); - }); - - await exWrapper.fillOrKillOrderAsync(orders[0], taker); - - return expect( - exWrapper.batchFillOrKillOrdersAsync(orders, taker, { - fillTakerTokenAmounts, - }), - ).to.be.rejectedWith(constants.REVERT); - }); - }); - - describe('fillOrdersUpTo', () => { - it('should stop when the entire fillTakerTokenAmount is filled', async () => { - const fillTakerTokenAmount = orders[0].params.takerTokenAmount.plus( - orders[1].params.takerTokenAmount.div(2), - ); - await exWrapper.fillOrdersUpToAsync(orders, taker, { - fillTakerTokenAmount, - }); - - const newBalances = await dmyBalances.getAsync(); - - const fillMakerTokenAmount = orders[0].params.makerTokenAmount.add( - orders[1].params.makerTokenAmount.dividedToIntegerBy(2), - ); - const makerFee = orders[0].params.makerFee.add(orders[1].params.makerFee.dividedToIntegerBy(2)); - const takerFee = orders[0].params.takerFee.add(orders[1].params.takerFee.dividedToIntegerBy(2)); - expect(newBalances[maker][orders[0].params.makerToken]).to.be.bignumber.equal( - balances[maker][orders[0].params.makerToken].minus(fillMakerTokenAmount), - ); - expect(newBalances[maker][orders[0].params.takerToken]).to.be.bignumber.equal( - balances[maker][orders[0].params.takerToken].add(fillTakerTokenAmount), - ); - expect(newBalances[maker][zrx.address]).to.be.bignumber.equal( - balances[maker][zrx.address].minus(makerFee), - ); - expect(newBalances[taker][orders[0].params.takerToken]).to.be.bignumber.equal( - balances[taker][orders[0].params.takerToken].minus(fillTakerTokenAmount), - ); - expect(newBalances[taker][orders[0].params.makerToken]).to.be.bignumber.equal( - balances[taker][orders[0].params.makerToken].add(fillMakerTokenAmount), - ); - expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( - balances[taker][zrx.address].minus(takerFee), - ); - expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal( - balances[feeRecipient][zrx.address].add(makerFee.add(takerFee)), - ); - }); - - it('should fill all orders if cannot fill entire fillTakerTokenAmount', async () => { - const fillTakerTokenAmount = ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18); - orders.forEach(order => { - balances[maker][order.params.makerToken] = balances[maker][order.params.makerToken].minus( - order.params.makerTokenAmount, - ); - balances[maker][order.params.takerToken] = balances[maker][order.params.takerToken].add( - order.params.takerTokenAmount, - ); - balances[maker][zrx.address] = balances[maker][zrx.address].minus(order.params.makerFee); - balances[taker][order.params.makerToken] = balances[taker][order.params.makerToken].add( - order.params.makerTokenAmount, - ); - balances[taker][order.params.takerToken] = balances[taker][order.params.takerToken].minus( - order.params.takerTokenAmount, - ); - balances[taker][zrx.address] = balances[taker][zrx.address].minus(order.params.takerFee); - balances[feeRecipient][zrx.address] = balances[feeRecipient][zrx.address].add( - order.params.makerFee.add(order.params.takerFee), - ); - }); - await exWrapper.fillOrdersUpToAsync(orders, taker, { - fillTakerTokenAmount, - }); - - const newBalances = await dmyBalances.getAsync(); - expect(newBalances).to.be.deep.equal(balances); - }); - - it('should throw when an order does not use the same takerToken', async () => { - orders = await Promise.all([ - orderFactory.newSignedOrderAsync(), - orderFactory.newSignedOrderAsync({ takerToken: zrx.address }), - orderFactory.newSignedOrderAsync(), - ]); - - return expect( - exWrapper.fillOrdersUpToAsync(orders, taker, { - fillTakerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(1000), 18), - }), - ).to.be.rejectedWith(constants.REVERT); - }); - }); - - describe('batchCancelOrders', () => { - it('should be able to cancel multiple orders', async () => { - const cancelTakerTokenAmounts = _.map(orders, order => order.params.takerTokenAmount); - await exWrapper.batchCancelOrdersAsync(orders, maker, { - cancelTakerTokenAmounts, - }); - - await exWrapper.batchFillOrdersAsync(orders, taker, { - fillTakerTokenAmounts: cancelTakerTokenAmounts, - }); - const newBalances = await dmyBalances.getAsync(); - expect(balances).to.be.deep.equal(newBalances); - }); - }); - }); -}); diff --git a/packages/contracts/test/ts/multi_sig_with_time_lock.ts b/packages/contracts/test/ts/multi_sig_with_time_lock.ts deleted file mode 100644 index ea939a758..000000000 --- a/packages/contracts/test/ts/multi_sig_with_time_lock.ts +++ /dev/null @@ -1,115 +0,0 @@ -import { RPC } from '@0xproject/dev-utils'; -import { BigNumber, promisify } from '@0xproject/utils'; -import * as chai from 'chai'; -import Web3 = require('web3'); - -import * as multiSigWalletJSON from '../../build/contracts/MultiSigWalletWithTimeLock.json'; -import * as truffleConf from '../../truffle.js'; -import { Artifacts } from '../../util/artifacts'; -import { constants } from '../../util/constants'; -import { MultiSigWrapper } from '../../util/multi_sig_wrapper'; -import { ContractInstance } from '../../util/types'; - -import { chaiSetup } from './utils/chai_setup'; - -const { MultiSigWalletWithTimeLock } = new Artifacts(artifacts); - -const MULTI_SIG_ABI = (multiSigWalletJSON as any).abi; -chaiSetup.configure(); -const expect = chai.expect; - -// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle -// with type `any` to a variable of type `Web3`. -const web3: Web3 = (global as any).web3; - -contract('MultiSigWalletWithTimeLock', (accounts: string[]) => { - const owners = [accounts[0], accounts[1]]; - const SECONDS_TIME_LOCKED = 10000; - - let multiSig: ContractInstance; - let multiSigWrapper: MultiSigWrapper; - let txId: number; - let initialSecondsTimeLocked: number; - let rpc: RPC; - - before(async () => { - multiSig = await MultiSigWalletWithTimeLock.deployed(); - multiSigWrapper = new MultiSigWrapper(multiSig); - - const secondsTimeLocked = await multiSig.secondsTimeLocked.call(); - initialSecondsTimeLocked = secondsTimeLocked.toNumber(); - const rpcUrl = `http://${truffleConf.networks.development.host}:${truffleConf.networks.development.port}`; - rpc = new RPC(rpcUrl); - }); - - describe('changeTimeLock', () => { - it('should throw when not called by wallet', async () => { - return expect(multiSig.changeTimeLock(SECONDS_TIME_LOCKED, { from: owners[0] })).to.be.rejectedWith( - constants.REVERT, - ); - }); - - it('should throw without enough confirmations', async () => { - const destination = multiSig.address; - const from = owners[0]; - const dataParams = { - name: 'changeTimeLock', - abi: MULTI_SIG_ABI, - args: [SECONDS_TIME_LOCKED], - }; - const subRes = await multiSigWrapper.submitTransactionAsync(destination, from, dataParams); - - txId = subRes.logs[0].args.transactionId.toNumber(); - return expect(multiSig.executeTransaction(txId)).to.be.rejectedWith(constants.REVERT); - }); - - it('should set confirmation time with enough confirmations', async () => { - const res = await multiSig.confirmTransaction(txId, { from: owners[1] }); - expect(res.logs).to.have.length(2); - const blockNum = await promisify(web3.eth.getBlockNumber)(); - const blockInfo = await promisify(web3.eth.getBlock)(blockNum); - const timestamp = new BigNumber(blockInfo.timestamp); - const confirmationTimeBigNum = new BigNumber(await multiSig.confirmationTimes.call(txId)); - - expect(timestamp).to.be.bignumber.equal(confirmationTimeBigNum); - }); - - it('should be executable with enough confirmations and secondsTimeLocked of 0', async () => { - expect(initialSecondsTimeLocked).to.be.equal(0); - - const res = await multiSig.executeTransaction(txId); - expect(res.logs).to.have.length(2); - - const secondsTimeLocked = new BigNumber(await multiSig.secondsTimeLocked.call()); - expect(secondsTimeLocked).to.be.bignumber.equal(SECONDS_TIME_LOCKED); - }); - - const newSecondsTimeLocked = 0; - it('should throw if it has enough confirmations but is not past the time lock', async () => { - const destination = multiSig.address; - const from = owners[0]; - const dataParams = { - name: 'changeTimeLock', - abi: MULTI_SIG_ABI, - args: [newSecondsTimeLocked], - }; - const subRes = await multiSigWrapper.submitTransactionAsync(destination, from, dataParams); - - txId = subRes.logs[0].args.transactionId.toNumber(); - const confRes = await multiSig.confirmTransaction(txId, { - from: owners[1], - }); - expect(confRes.logs).to.have.length(2); - - return expect(multiSig.executeTransaction(txId)).to.be.rejectedWith(constants.REVERT); - }); - - it('should execute if it has enough confirmations and is past the time lock', async () => { - await rpc.increaseTimeAsync(SECONDS_TIME_LOCKED); - await multiSig.executeTransaction(txId); - - const secondsTimeLocked = new BigNumber(await multiSig.secondsTimeLocked.call()); - expect(secondsTimeLocked).to.be.bignumber.equal(newSecondsTimeLocked); - }); - }); -}); diff --git a/packages/contracts/test/ts/multi_sig_with_time_lock_except_remove_auth_addr.ts b/packages/contracts/test/ts/multi_sig_with_time_lock_except_remove_auth_addr.ts deleted file mode 100644 index 62aa625fe..000000000 --- a/packages/contracts/test/ts/multi_sig_with_time_lock_except_remove_auth_addr.ts +++ /dev/null @@ -1,150 +0,0 @@ -import * as chai from 'chai'; - -import * as tokenTransferProxyJSON from '../../build/contracts/TokenTransferProxy.json'; -import { Artifacts } from '../../util/artifacts'; -import { constants } from '../../util/constants'; -import { crypto } from '../../util/crypto'; -import { MultiSigWrapper } from '../../util/multi_sig_wrapper'; -import { ContractInstance, TransactionDataParams } from '../../util/types'; - -import { chaiSetup } from './utils/chai_setup'; -const { TokenTransferProxy, MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress } = new Artifacts(artifacts); -const PROXY_ABI = (tokenTransferProxyJSON as any).abi; - -chaiSetup.configure(); -const expect = chai.expect; - -contract('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', (accounts: string[]) => { - const owners = [accounts[0], accounts[1]]; - const requiredApprovals = 2; - const SECONDS_TIME_LOCKED = 1000000; - - // initialize fake addresses - const authorizedAddress = `0x${crypto - .solSHA3([accounts[0]]) - .slice(0, 20) - .toString('hex')}`; - const unauthorizedAddress = `0x${crypto - .solSHA3([accounts[1]]) - .slice(0, 20) - .toString('hex')}`; - - let tokenTransferProxy: ContractInstance; - let multiSig: ContractInstance; - let multiSigWrapper: MultiSigWrapper; - - let validDestination: string; - - beforeEach(async () => { - const initialOwner = accounts[0]; - tokenTransferProxy = await TokenTransferProxy.new({ from: initialOwner }); - await tokenTransferProxy.addAuthorizedAddress(authorizedAddress, { - from: initialOwner, - }); - multiSig = await MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress.new( - owners, - requiredApprovals, - SECONDS_TIME_LOCKED, - tokenTransferProxy.address, - ); - await tokenTransferProxy.transferOwnership(multiSig.address, { - from: initialOwner, - }); - multiSigWrapper = new MultiSigWrapper(multiSig); - validDestination = tokenTransferProxy.address; - }); - - describe('isFunctionRemoveAuthorizedAddress', () => { - it('should throw if data is not for removeAuthorizedAddress', async () => { - const data = MultiSigWrapper.encodeFnArgs('addAuthorizedAddress', PROXY_ABI, [owners[0]]); - return expect(multiSig.isFunctionRemoveAuthorizedAddress.call(data)).to.be.rejectedWith(constants.REVERT); - }); - - it('should return true if data is for removeAuthorizedAddress', async () => { - const data = MultiSigWrapper.encodeFnArgs('removeAuthorizedAddress', PROXY_ABI, [owners[0]]); - const isFunctionRemoveAuthorizedAddress = await multiSig.isFunctionRemoveAuthorizedAddress.call(data); - expect(isFunctionRemoveAuthorizedAddress).to.be.true(); - }); - }); - - describe('executeRemoveAuthorizedAddress', () => { - it('should throw without the required confirmations', async () => { - const dataParams: TransactionDataParams = { - name: 'removeAuthorizedAddress', - abi: PROXY_ABI, - args: [authorizedAddress], - }; - const res = await multiSigWrapper.submitTransactionAsync(validDestination, owners[0], dataParams); - const txId = res.logs[0].args.transactionId.toString(); - - return expect(multiSig.executeRemoveAuthorizedAddress(txId)).to.be.rejectedWith(constants.REVERT); - }); - - it('should throw if tx destination is not the tokenTransferProxy', async () => { - const invalidTokenTransferProxy = await TokenTransferProxy.new(); - const invalidDestination = invalidTokenTransferProxy.address; - const dataParams: TransactionDataParams = { - name: 'removeAuthorizedAddress', - abi: PROXY_ABI, - args: [authorizedAddress], - }; - const res = await multiSigWrapper.submitTransactionAsync(invalidDestination, owners[0], dataParams); - const txId = res.logs[0].args.transactionId.toString(); - await multiSig.confirmTransaction(txId, { from: owners[1] }); - const isConfirmed = await multiSig.isConfirmed.call(txId); - expect(isConfirmed).to.be.true(); - - return expect(multiSig.executeRemoveAuthorizedAddress(txId)).to.be.rejectedWith(constants.REVERT); - }); - - it('should throw if tx data is not for removeAuthorizedAddress', async () => { - const dataParams: TransactionDataParams = { - name: 'addAuthorizedAddress', - abi: PROXY_ABI, - args: [unauthorizedAddress], - }; - const res = await multiSigWrapper.submitTransactionAsync(validDestination, owners[0], dataParams); - const txId = res.logs[0].args.transactionId.toString(); - await multiSig.confirmTransaction(txId, { from: owners[1] }); - const isConfirmed = await multiSig.isConfirmed.call(txId); - expect(isConfirmed).to.be.true(); - - return expect(multiSig.executeRemoveAuthorizedAddress(txId)).to.be.rejectedWith(constants.REVERT); - }); - - it('should execute removeAuthorizedAddress for valid tokenTransferProxy if fully confirmed', async () => { - const dataParams: TransactionDataParams = { - name: 'removeAuthorizedAddress', - abi: PROXY_ABI, - args: [authorizedAddress], - }; - const res = await multiSigWrapper.submitTransactionAsync(validDestination, owners[0], dataParams); - const txId = res.logs[0].args.transactionId.toString(); - await multiSig.confirmTransaction(txId, { from: owners[1] }); - const isConfirmed = await multiSig.isConfirmed.call(txId); - expect(isConfirmed).to.be.true(); - await multiSig.executeRemoveAuthorizedAddress(txId); - - const isAuthorized = await tokenTransferProxy.authorized.call(authorizedAddress); - expect(isAuthorized).to.be.false(); - }); - - it('should throw if already executed', async () => { - const dataParams: TransactionDataParams = { - name: 'removeAuthorizedAddress', - abi: PROXY_ABI, - args: [authorizedAddress], - }; - const res = await multiSigWrapper.submitTransactionAsync(validDestination, owners[0], dataParams); - const txId = res.logs[0].args.transactionId.toString(); - await multiSig.confirmTransaction(txId, { from: owners[1] }); - const isConfirmed = await multiSig.isConfirmed.call(txId); - expect(isConfirmed).to.be.true(); - await multiSig.executeRemoveAuthorizedAddress(txId); - const tx = await multiSig.transactions.call(txId); - const isExecuted = tx[3]; - expect(isExecuted).to.be.true(); - return expect(multiSig.executeRemoveAuthorizedAddress(txId)).to.be.rejectedWith(constants.REVERT); - }); - }); -}); diff --git a/packages/contracts/test/ts/token_registry.ts b/packages/contracts/test/ts/token_registry.ts deleted file mode 100644 index d1c551565..000000000 --- a/packages/contracts/test/ts/token_registry.ts +++ /dev/null @@ -1,234 +0,0 @@ -import { ZeroEx } from '0x.js'; -import * as chai from 'chai'; -import ethUtil = require('ethereumjs-util'); -import * as _ from 'lodash'; - -import { Artifacts } from '../../util/artifacts'; -import { constants } from '../../util/constants'; -import { TokenRegWrapper } from '../../util/token_registry_wrapper'; -import { ContractInstance } from '../../util/types'; - -import { chaiSetup } from './utils/chai_setup'; - -const { TokenRegistry } = new Artifacts(artifacts); -chaiSetup.configure(); -const expect = chai.expect; - -contract('TokenRegistry', (accounts: string[]) => { - const owner = accounts[0]; - const notOwner = accounts[1]; - - const tokenAddress1 = `0x${ethUtil.setLength(ethUtil.toBuffer('0x1'), 20, false).toString('hex')}`; - const tokenAddress2 = `0x${ethUtil.setLength(ethUtil.toBuffer('0x2'), 20, false).toString('hex')}`; - - const token1 = { - address: tokenAddress1, - name: 'testToken1', - symbol: 'TT1', - decimals: 18, - ipfsHash: `0x${ethUtil.sha3('ipfs1').toString('hex')}`, - swarmHash: `0x${ethUtil.sha3('swarm1').toString('hex')}`, - }; - - const token2 = { - address: tokenAddress2, - name: 'testToken2', - symbol: 'TT2', - decimals: 18, - ipfsHash: `0x${ethUtil.sha3('ipfs2').toString('hex')}`, - swarmHash: `0x${ethUtil.sha3('swarm2').toString('hex')}`, - }; - - const nullToken = { - address: ZeroEx.NULL_ADDRESS, - name: '', - symbol: '', - decimals: 0, - ipfsHash: constants.NULL_BYTES, - swarmHash: constants.NULL_BYTES, - }; - - let tokenReg: ContractInstance; - let tokenRegWrapper: TokenRegWrapper; - - beforeEach(async () => { - tokenReg = await TokenRegistry.new(); - tokenRegWrapper = new TokenRegWrapper(tokenReg); - }); - - describe('addToken', () => { - it('should throw when not called by owner', async () => { - return expect(tokenRegWrapper.addTokenAsync(token1, notOwner)).to.be.rejectedWith(constants.REVERT); - }); - - it('should add token metadata when called by owner', async () => { - await tokenRegWrapper.addTokenAsync(token1, owner); - const tokenData = await tokenRegWrapper.getTokenMetaDataAsync(token1.address); - expect(tokenData).to.be.deep.equal(token1); - }); - - it('should throw if token already exists', async () => { - await tokenRegWrapper.addTokenAsync(token1, owner); - - return expect(tokenRegWrapper.addTokenAsync(token1, owner)).to.be.rejectedWith(constants.REVERT); - }); - - it('should throw if token address is null', async () => { - return expect(tokenRegWrapper.addTokenAsync(nullToken, owner)).to.be.rejectedWith(constants.REVERT); - }); - - it('should throw if name already exists', async () => { - await tokenRegWrapper.addTokenAsync(token1, owner); - const duplicateNameToken = _.assign({}, token2, { name: token1.name }); - - return expect(tokenRegWrapper.addTokenAsync(duplicateNameToken, owner)).to.be.rejectedWith( - constants.REVERT, - ); - }); - - it('should throw if symbol already exists', async () => { - await tokenRegWrapper.addTokenAsync(token1, owner); - const duplicateSymbolToken = _.assign({}, token2, { - symbol: token1.symbol, - }); - - return expect(tokenRegWrapper.addTokenAsync(duplicateSymbolToken, owner)).to.be.rejectedWith( - constants.REVERT, - ); - }); - }); - - describe('after addToken', () => { - beforeEach(async () => { - await tokenRegWrapper.addTokenAsync(token1, owner); - }); - - describe('getTokenByName', () => { - it('should return token metadata when given the token name', async () => { - const tokenData = await tokenRegWrapper.getTokenByNameAsync(token1.name); - expect(tokenData).to.be.deep.equal(token1); - }); - }); - - describe('getTokenBySymbol', () => { - it('should return token metadata when given the token symbol', async () => { - const tokenData = await tokenRegWrapper.getTokenBySymbolAsync(token1.symbol); - expect(tokenData).to.be.deep.equal(token1); - }); - }); - - describe('setTokenName', () => { - it('should throw when not called by owner', async () => { - return expect( - tokenReg.setTokenName(token1.address, token2.name, { from: notOwner }), - ).to.be.rejectedWith(constants.REVERT); - }); - - it('should change the token name when called by owner', async () => { - const res = await tokenReg.setTokenName(token1.address, token2.name, { - from: owner, - }); - expect(res.logs).to.have.length(1); - const [newData, oldData] = await Promise.all([ - tokenRegWrapper.getTokenByNameAsync(token2.name), - tokenRegWrapper.getTokenByNameAsync(token1.name), - ]); - - const expectedNewData = _.assign({}, token1, { name: token2.name }); - const expectedOldData = nullToken; - expect(newData).to.be.deep.equal(expectedNewData); - expect(oldData).to.be.deep.equal(expectedOldData); - }); - - it('should throw if the name already exists', async () => { - await tokenRegWrapper.addTokenAsync(token2, owner); - - return expect(tokenReg.setTokenName(token1.address, token2.name, { from: owner })).to.be.rejectedWith( - constants.REVERT, - ); - }); - - it('should throw if token does not exist', async () => { - return expect( - tokenReg.setTokenName(nullToken.address, token2.name, { from: owner }), - ).to.be.rejectedWith(constants.REVERT); - }); - }); - - describe('setTokenSymbol', () => { - it('should throw when not called by owner', async () => { - return expect( - tokenReg.setTokenSymbol(token1.address, token2.symbol, { - from: notOwner, - }), - ).to.be.rejectedWith(constants.REVERT); - }); - - it('should change the token symbol when called by owner', async () => { - const res = await tokenReg.setTokenSymbol(token1.address, token2.symbol, { from: owner }); - expect(res.logs).to.have.length(1); - const [newData, oldData] = await Promise.all([ - tokenRegWrapper.getTokenBySymbolAsync(token2.symbol), - tokenRegWrapper.getTokenBySymbolAsync(token1.symbol), - ]); - - const expectedNewData = _.assign({}, token1, { symbol: token2.symbol }); - const expectedOldData = nullToken; - expect(newData).to.be.deep.equal(expectedNewData); - expect(oldData).to.be.deep.equal(expectedOldData); - }); - - it('should throw if the symbol already exists', async () => { - await tokenRegWrapper.addTokenAsync(token2, owner); - - return expect( - tokenReg.setTokenSymbol(token1.address, token2.symbol, { - from: owner, - }), - ).to.be.rejectedWith(constants.REVERT); - }); - - it('should throw if token does not exist', async () => { - return expect( - tokenReg.setTokenSymbol(nullToken.address, token2.symbol, { - from: owner, - }), - ).to.be.rejectedWith(constants.REVERT); - }); - }); - - describe('removeToken', () => { - it('should throw if not called by owner', async () => { - const index = 0; - return expect(tokenReg.removeToken(token1.address, index, { from: notOwner })).to.be.rejectedWith( - constants.REVERT, - ); - }); - - it('should remove token metadata when called by owner', async () => { - const index = 0; - const res = await tokenReg.removeToken(token1.address, index, { - from: owner, - }); - expect(res.logs).to.have.length(1); - const tokenData = await tokenRegWrapper.getTokenMetaDataAsync(token1.address); - expect(tokenData).to.be.deep.equal(nullToken); - }); - - it('should throw if token does not exist', async () => { - const index = 0; - return expect(tokenReg.removeToken(nullToken.address, index, { from: owner })).to.be.rejectedWith( - constants.REVERT, - ); - }); - - it('should throw if token at given index does not match address', async () => { - await tokenRegWrapper.addTokenAsync(token2, owner); - const incorrectIndex = 0; - return expect(tokenReg.removeToken(token2.address, incorrectIndex, { from: owner })).to.be.rejectedWith( - constants.REVERT, - ); - }); - }); - }); -}); diff --git a/packages/contracts/test/ts/token_transfer_proxy/auth.ts b/packages/contracts/test/ts/token_transfer_proxy/auth.ts deleted file mode 100644 index 9ae0a8fc3..000000000 --- a/packages/contracts/test/ts/token_transfer_proxy/auth.ts +++ /dev/null @@ -1,99 +0,0 @@ -import * as chai from 'chai'; - -import { constants } from '../../../util/constants'; -import { ContractInstance } from '../../../util/types'; -import { chaiSetup } from '../utils/chai_setup'; - -chaiSetup.configure(); -const expect = chai.expect; -const TokenTransferProxy = artifacts.require('./db/TokenTransferProxy.sol'); - -contract('TokenTransferProxy', (accounts: string[]) => { - const owner = accounts[0]; - const notOwner = accounts[1]; - - let tokenTransferProxy: ContractInstance; - let authorized: string; - let notAuthorized = owner; - - before(async () => { - tokenTransferProxy = await TokenTransferProxy.deployed(); - }); - - describe('addAuthorizedAddress', () => { - it('should throw if not called by owner', async () => { - return expect(tokenTransferProxy.addAuthorizedAddress(notOwner, { from: notOwner })).to.be.rejectedWith( - constants.REVERT, - ); - }); - - it('should allow owner to add an authorized address', async () => { - await tokenTransferProxy.addAuthorizedAddress(notAuthorized, { - from: owner, - }); - authorized = notAuthorized; - notAuthorized = null; - const isAuthorized = await tokenTransferProxy.authorized.call(authorized); - expect(isAuthorized).to.be.true(); - }); - - it('should throw if owner attempts to authorize a duplicate address', async () => { - return expect(tokenTransferProxy.addAuthorizedAddress(authorized, { from: owner })).to.be.rejectedWith( - constants.REVERT, - ); - }); - }); - - describe('removeAuthorizedAddress', () => { - it('should throw if not called by owner', async () => { - return expect( - tokenTransferProxy.removeAuthorizedAddress(authorized, { - from: notOwner, - }), - ).to.be.rejectedWith(constants.REVERT); - }); - - it('should allow owner to remove an authorized address', async () => { - await tokenTransferProxy.removeAuthorizedAddress(authorized, { - from: owner, - }); - notAuthorized = authorized; - authorized = null; - - const isAuthorized = await tokenTransferProxy.authorized.call(notAuthorized); - expect(isAuthorized).to.be.false(); - }); - - it('should throw if owner attempts to remove an address that is not authorized', async () => { - return expect( - tokenTransferProxy.removeAuthorizedAddress(notAuthorized, { - from: owner, - }), - ).to.be.rejectedWith(constants.REVERT); - }); - }); - - describe('getAuthorizedAddresses', () => { - it('should return all authorized addresses', async () => { - const initial = await tokenTransferProxy.getAuthorizedAddresses(); - expect(initial).to.have.length(1); - await tokenTransferProxy.addAuthorizedAddress(notAuthorized, { - from: owner, - }); - - authorized = notAuthorized; - notAuthorized = null; - const afterAdd = await tokenTransferProxy.getAuthorizedAddresses(); - expect(afterAdd).to.have.length(2); - expect(afterAdd).to.include(authorized); - - await tokenTransferProxy.removeAuthorizedAddress(authorized, { - from: owner, - }); - notAuthorized = authorized; - authorized = null; - const afterRemove = await tokenTransferProxy.getAuthorizedAddresses(); - expect(afterRemove).to.have.length(1); - }); - }); -}); diff --git a/packages/contracts/test/ts/token_transfer_proxy/transfer_from.ts b/packages/contracts/test/ts/token_transfer_proxy/transfer_from.ts deleted file mode 100644 index e1aff6dae..000000000 --- a/packages/contracts/test/ts/token_transfer_proxy/transfer_from.ts +++ /dev/null @@ -1,73 +0,0 @@ -import * as chai from 'chai'; - -import { Artifacts } from '../../../util/artifacts'; -import { Balances } from '../../../util/balances'; -import { constants } from '../../../util/constants'; -import { ContractInstance } from '../../../util/types'; -import { chaiSetup } from '../utils/chai_setup'; - -chaiSetup.configure(); -const expect = chai.expect; -const { TokenTransferProxy, DummyToken, TokenRegistry } = new Artifacts(artifacts); - -contract('TokenTransferProxy', (accounts: string[]) => { - const INIT_BAL = 100000000; - const INIT_ALLOW = 100000000; - - const owner = accounts[0]; - const notAuthorized = owner; - - let tokenTransferProxy: ContractInstance; - let tokenRegistry: ContractInstance; - let rep: ContractInstance; - let dmyBalances: Balances; - - before(async () => { - [tokenTransferProxy, tokenRegistry] = await Promise.all([ - TokenTransferProxy.deployed(), - TokenRegistry.deployed(), - ]); - const repAddress = await tokenRegistry.getTokenAddressBySymbol('REP'); - rep = DummyToken.at(repAddress); - - dmyBalances = new Balances([rep], [accounts[0], accounts[1]]); - await Promise.all([ - rep.approve(TokenTransferProxy.address, INIT_ALLOW, { - from: accounts[0], - }), - rep.setBalance(accounts[0], INIT_BAL, { from: owner }), - rep.approve(TokenTransferProxy.address, INIT_ALLOW, { - from: accounts[1], - }), - rep.setBalance(accounts[1], INIT_BAL, { from: owner }), - ]); - }); - - describe('transferFrom', () => { - it('should throw when called by an unauthorized address', async () => { - expect( - tokenTransferProxy.transferFrom(rep.address, accounts[0], accounts[1], 1000, { from: notAuthorized }), - ).to.be.rejectedWith(constants.REVERT); - }); - - it('should allow an authorized address to transfer', async () => { - const balances = await dmyBalances.getAsync(); - - await tokenTransferProxy.addAuthorizedAddress(notAuthorized, { - from: owner, - }); - const transferAmt = 10000; - await tokenTransferProxy.transferFrom(rep.address, accounts[0], accounts[1], transferAmt, { - from: notAuthorized, - }); - - const newBalances = await dmyBalances.getAsync(); - expect(newBalances[accounts[0]][rep.address]).to.be.bignumber.equal( - balances[accounts[0]][rep.address].minus(transferAmt), - ); - expect(newBalances[accounts[1]][rep.address]).to.be.bignumber.equal( - balances[accounts[1]][rep.address].add(transferAmt), - ); - }); - }); -}); diff --git a/packages/contracts/test/ts/unlimited_allowance_token.ts b/packages/contracts/test/ts/unlimited_allowance_token.ts deleted file mode 100644 index c90a52095..000000000 --- a/packages/contracts/test/ts/unlimited_allowance_token.ts +++ /dev/null @@ -1,127 +0,0 @@ -import { ZeroEx } from '0x.js'; -import { BigNumber } from '@0xproject/utils'; -import * as chai from 'chai'; -import * as Web3 from 'web3'; - -import { Artifacts } from '../../util/artifacts'; -import { constants } from '../../util/constants'; -import { ContractInstance } from '../../util/types'; - -import { chaiSetup } from './utils/chai_setup'; - -const { DummyToken } = new Artifacts(artifacts); -const web3: Web3 = (global as any).web3; -chaiSetup.configure(); -const expect = chai.expect; - -contract('UnlimitedAllowanceToken', (accounts: string[]) => { - const config = { - networkId: constants.TESTRPC_NETWORK_ID, - }; - const zeroEx = new ZeroEx(web3.currentProvider, config); - const owner = accounts[0]; - const spender = accounts[1]; - - const MAX_MINT_VALUE = new BigNumber(100000000000000000000); - let tokenAddress: string; - let token: ContractInstance; - - beforeEach(async () => { - token = await DummyToken.new({ from: owner }); - await token.mint(MAX_MINT_VALUE, { from: owner }); - tokenAddress = token.address; - }); - - describe('transfer', () => { - it('should transfer balance from sender to receiver', async () => { - const receiver = spender; - const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = new BigNumber(1); - await zeroEx.token.transferAsync(tokenAddress, owner, receiver, amountToTransfer); - const finalOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const finalReceiverBalance = await zeroEx.token.getBalanceAsync(tokenAddress, receiver); - - const expectedFinalOwnerBalance = initOwnerBalance.minus(amountToTransfer); - const expectedFinalReceiverBalance = amountToTransfer; - expect(finalOwnerBalance).to.be.bignumber.equal(expectedFinalOwnerBalance); - expect(finalReceiverBalance).to.be.bignumber.equal(expectedFinalReceiverBalance); - }); - - it('should return true on a 0 value transfer', async () => { - const didReturnTrue = await token.transfer.call(spender, 0, { - from: owner, - }); - expect(didReturnTrue).to.be.true(); - }); - }); - - describe('transferFrom', () => { - it('should return false if owner has insufficient balance', async () => { - const ownerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = ownerBalance.plus(1); - await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, amountToTransfer); - const didReturnTrue = await token.transferFrom.call(owner, spender, amountToTransfer, { from: spender }); - expect(didReturnTrue).to.be.false(); - }); - - it('should return false if spender has insufficient allowance', async () => { - const ownerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = ownerBalance; - - const spenderAllowance = await zeroEx.token.getAllowanceAsync(tokenAddress, owner, spender); - const spenderAllowanceIsInsufficient = spenderAllowance.cmp(amountToTransfer) < 0; - expect(spenderAllowanceIsInsufficient).to.be.true(); - - const didReturnTrue = await token.transferFrom.call(owner, spender, amountToTransfer, { from: spender }); - expect(didReturnTrue).to.be.false(); - }); - - it('should return true on a 0 value transfer', async () => { - const amountToTransfer = 0; - const didReturnTrue = await token.transferFrom.call(owner, spender, amountToTransfer, { from: spender }); - expect(didReturnTrue).to.be.true(); - }); - - it('should not modify spender allowance if spender allowance is 2^256 - 1', async () => { - const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = initOwnerBalance; - const initSpenderAllowance = zeroEx.token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS; - await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, initSpenderAllowance); - await zeroEx.token.transferFromAsync(tokenAddress, owner, spender, spender, amountToTransfer, { - gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, - }); - - const newSpenderAllowance = await zeroEx.token.getAllowanceAsync(tokenAddress, owner, spender); - expect(initSpenderAllowance).to.be.bignumber.equal(newSpenderAllowance); - }); - - it('should transfer the correct balances if spender has sufficient allowance', async () => { - const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = initOwnerBalance; - const initSpenderAllowance = initOwnerBalance; - await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, initSpenderAllowance); - await zeroEx.token.transferFromAsync(tokenAddress, owner, spender, spender, amountToTransfer, { - gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, - }); - - const newOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const newSpenderBalance = await zeroEx.token.getBalanceAsync(tokenAddress, spender); - - expect(newOwnerBalance).to.be.bignumber.equal(0); - expect(newSpenderBalance).to.be.bignumber.equal(initOwnerBalance); - }); - - it('should modify allowance if spender has sufficient allowance less than 2^256 - 1', async () => { - const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = initOwnerBalance; - const initSpenderAllowance = initOwnerBalance; - await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, initSpenderAllowance); - await zeroEx.token.transferFromAsync(tokenAddress, owner, spender, spender, amountToTransfer, { - gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, - }); - - const newSpenderAllowance = await zeroEx.token.getAllowanceAsync(tokenAddress, owner, spender); - expect(newSpenderAllowance).to.be.bignumber.equal(0); - }); - }); -}); diff --git a/packages/contracts/test/ts/unlimited_allowance_token_v2.ts b/packages/contracts/test/ts/unlimited_allowance_token_v2.ts deleted file mode 100644 index 1b29a02ba..000000000 --- a/packages/contracts/test/ts/unlimited_allowance_token_v2.ts +++ /dev/null @@ -1,141 +0,0 @@ -import { ZeroEx } from '0x.js'; -import { BigNumber } from '@0xproject/utils'; -import * as chai from 'chai'; -import * as Web3 from 'web3'; - -import { Artifacts } from '../../util/artifacts'; -import { constants } from '../../util/constants'; -import { ContractInstance } from '../../util/types'; - -import { chaiSetup } from './utils/chai_setup'; - -const { DummyTokenV2 } = new Artifacts(artifacts); -const web3: Web3 = (global as any).web3; -chaiSetup.configure(); -const expect = chai.expect; - -contract('UnlimitedAllowanceTokenV2', (accounts: string[]) => { - const config = { - networkId: constants.TESTRPC_NETWORK_ID, - }; - const zeroEx = new ZeroEx(web3.currentProvider, config); - const owner = accounts[0]; - const spender = accounts[1]; - - const MAX_MINT_VALUE = new BigNumber(100000000000000000000); - let tokenAddress: string; - let token: ContractInstance; - - beforeEach(async () => { - token = await DummyTokenV2.new({ from: owner }); - await token.mint(MAX_MINT_VALUE, { from: owner }); - tokenAddress = token.address; - }); - - describe('transfer', () => { - it('should throw if owner has insufficient balance', async () => { - const ownerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = ownerBalance.plus(1); - return expect(token.transfer.call(spender, amountToTransfer, { from: owner })).to.be.rejectedWith( - constants.REVERT, - ); - }); - - it('should transfer balance from sender to receiver', async () => { - const receiver = spender; - const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = new BigNumber(1); - await zeroEx.token.transferAsync(tokenAddress, owner, receiver, amountToTransfer); - const finalOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const finalReceiverBalance = await zeroEx.token.getBalanceAsync(tokenAddress, receiver); - - const expectedFinalOwnerBalance = initOwnerBalance.minus(amountToTransfer); - const expectedFinalReceiverBalance = amountToTransfer; - expect(finalOwnerBalance).to.be.bignumber.equal(expectedFinalOwnerBalance); - expect(finalReceiverBalance).to.be.bignumber.equal(expectedFinalReceiverBalance); - }); - - it('should return true on a 0 value transfer', async () => { - const didReturnTrue = await token.transfer.call(spender, 0, { - from: owner, - }); - expect(didReturnTrue).to.be.true(); - }); - }); - - describe('transferFrom', () => { - it('should throw if owner has insufficient balance', async () => { - const ownerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = ownerBalance.plus(1); - await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, amountToTransfer); - return expect( - token.transferFrom.call(owner, spender, amountToTransfer, { - from: spender, - }), - ).to.be.rejectedWith(constants.REVERT); - }); - - it('should throw if spender has insufficient allowance', async () => { - const ownerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = ownerBalance; - - const spenderAllowance = await zeroEx.token.getAllowanceAsync(tokenAddress, owner, spender); - const spenderAllowanceIsInsufficient = spenderAllowance.cmp(amountToTransfer) < 0; - expect(spenderAllowanceIsInsufficient).to.be.true(); - - return expect( - token.transferFrom.call(owner, spender, amountToTransfer, { - from: spender, - }), - ).to.be.rejectedWith(constants.REVERT); - }); - - it('should return true on a 0 value transfer', async () => { - const amountToTransfer = 0; - const didReturnTrue = await token.transferFrom.call(owner, spender, amountToTransfer, { from: spender }); - expect(didReturnTrue).to.be.true(); - }); - - it('should not modify spender allowance if spender allowance is 2^256 - 1', async () => { - const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = initOwnerBalance; - const initSpenderAllowance = zeroEx.token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS; - await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, initSpenderAllowance); - await zeroEx.token.transferFromAsync(tokenAddress, owner, spender, spender, amountToTransfer, { - gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, - }); - - const newSpenderAllowance = await zeroEx.token.getAllowanceAsync(tokenAddress, owner, spender); - expect(initSpenderAllowance).to.be.bignumber.equal(newSpenderAllowance); - }); - - it('should transfer the correct balances if spender has sufficient allowance', async () => { - const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = initOwnerBalance; - const initSpenderAllowance = initOwnerBalance; - await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, initSpenderAllowance); - await zeroEx.token.transferFromAsync(tokenAddress, owner, spender, spender, amountToTransfer, { - gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, - }); - - const newOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const newSpenderBalance = await zeroEx.token.getBalanceAsync(tokenAddress, spender); - - expect(newOwnerBalance).to.be.bignumber.equal(0); - expect(newSpenderBalance).to.be.bignumber.equal(initOwnerBalance); - }); - - it('should modify allowance if spender has sufficient allowance less than 2^256 - 1', async () => { - const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = initOwnerBalance; - const initSpenderAllowance = initOwnerBalance; - await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, initSpenderAllowance); - await zeroEx.token.transferFromAsync(tokenAddress, owner, spender, spender, amountToTransfer, { - gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, - }); - - const newSpenderAllowance = await zeroEx.token.getAllowanceAsync(tokenAddress, owner, spender); - expect(newSpenderAllowance).to.be.bignumber.equal(0); - }); - }); -}); diff --git a/packages/contracts/test/ts/utils/chai_setup.ts b/packages/contracts/test/ts/utils/chai_setup.ts deleted file mode 100644 index 078edd309..000000000 --- a/packages/contracts/test/ts/utils/chai_setup.ts +++ /dev/null @@ -1,13 +0,0 @@ -import * as chai from 'chai'; -import chaiAsPromised = require('chai-as-promised'); -import ChaiBigNumber = require('chai-bignumber'); -import * as dirtyChai from 'dirty-chai'; - -export const chaiSetup = { - configure() { - chai.config.includeStack = true; - chai.use(ChaiBigNumber()); - chai.use(dirtyChai); - chai.use(chaiAsPromised); - }, -}; diff --git a/packages/contracts/test/ts/zrx_token.ts b/packages/contracts/test/ts/zrx_token.ts deleted file mode 100644 index 766c94c2a..000000000 --- a/packages/contracts/test/ts/zrx_token.ts +++ /dev/null @@ -1,175 +0,0 @@ -import { ZeroEx } from '0x.js'; -import { BigNumber } from '@0xproject/utils'; -import * as chai from 'chai'; -import Web3 = require('web3'); - -import { Artifacts } from '../../util/artifacts'; -import { constants } from '../../util/constants'; -import { ContractInstance } from '../../util/types'; - -import { chaiSetup } from './utils/chai_setup'; - -chaiSetup.configure(); -const expect = chai.expect; -const { Exchange, ZRXToken } = new Artifacts(artifacts); -const web3: Web3 = (global as any).web3; - -contract('ZRXToken', (accounts: string[]) => { - const owner = accounts[0]; - const spender = accounts[1]; - let zeroEx: ZeroEx; - - let MAX_UINT: BigNumber; - - let zrx: ContractInstance; - let zrxAddress: string; - - beforeEach(async () => { - zeroEx = new ZeroEx(web3.currentProvider, { - exchangeContractAddress: Exchange.address, - networkId: constants.TESTRPC_NETWORK_ID, - }); - zrx = await ZRXToken.new(); - zrxAddress = zrx.address; - MAX_UINT = zeroEx.token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS; - }); - - describe('constants', () => { - it('should have 18 decimals', async () => { - const decimals = new BigNumber(await zrx.decimals.call()); - 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.call()); - 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.call(); - const expectedName = '0x Protocol Token'; - expect(name).to.be.equal(expectedName); - }); - - it('should have the symbol ZRX', async () => { - const symbol = await zrx.symbol.call(); - const expectedSymbol = 'ZRX'; - expect(symbol).to.be.equal(expectedSymbol); - }); - }); - - 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.call()); - expect(totalSupply).to.be.bignumber.equal(ownerBalance); - }); - }); - - describe('transfer', () => { - it('should transfer balance from sender to receiver', async () => { - const receiver = spender; - const initOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); - const amountToTransfer = new BigNumber(1); - const txHash = await zeroEx.token.transferAsync(zrxAddress, owner, receiver, amountToTransfer); - await zeroEx.awaitTransactionMinedAsync(txHash); - const finalOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); - const finalReceiverBalance = await zeroEx.token.getBalanceAsync(zrxAddress, receiver); - - const expectedFinalOwnerBalance = initOwnerBalance.minus(amountToTransfer); - const expectedFinalReceiverBalance = amountToTransfer; - expect(finalOwnerBalance).to.be.bignumber.equal(expectedFinalOwnerBalance); - expect(finalReceiverBalance).to.be.bignumber.equal(expectedFinalReceiverBalance); - }); - - it('should return true on a 0 value transfer', async () => { - const didReturnTrue = await zrx.transfer.call(spender, 0, { - from: owner, - }); - expect(didReturnTrue).to.be.true(); - }); - }); - - describe('transferFrom', () => { - it('should return false if owner has insufficient balance', async () => { - const ownerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); - const amountToTransfer = ownerBalance.plus(1); - const txHash = await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, amountToTransfer, { - gasLimit: constants.MAX_TOKEN_APPROVE_GAS, - }); - await zeroEx.awaitTransactionMinedAsync(txHash); - const didReturnTrue = await zrx.transferFrom.call(owner, spender, amountToTransfer, { from: spender }); - expect(didReturnTrue).to.be.false(); - }); - - it('should return false if spender has insufficient allowance', async () => { - const ownerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); - const amountToTransfer = ownerBalance; - - const spenderAllowance = await zeroEx.token.getAllowanceAsync(zrxAddress, owner, spender); - const spenderAllowanceIsInsufficient = spenderAllowance.cmp(amountToTransfer) < 0; - expect(spenderAllowanceIsInsufficient).to.be.true(); - - const didReturnTrue = await zrx.transferFrom.call(owner, spender, amountToTransfer, { from: spender }); - expect(didReturnTrue).to.be.false(); - }); - - it('should return true on a 0 value transfer', async () => { - const amountToTransfer = 0; - const didReturnTrue = await zrx.transferFrom.call(owner, spender, amountToTransfer, { from: spender }); - expect(didReturnTrue).to.be.true(); - }); - - it('should not modify spender allowance if spender allowance is 2^256 - 1', async () => { - const initOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); - const amountToTransfer = initOwnerBalance; - const initSpenderAllowance = MAX_UINT; - let txHash = await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, initSpenderAllowance, { - gasLimit: constants.MAX_TOKEN_APPROVE_GAS, - }); - await zeroEx.awaitTransactionMinedAsync(txHash); - txHash = await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, { - gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, - }); - await zeroEx.awaitTransactionMinedAsync(txHash); - - const newSpenderAllowance = await zeroEx.token.getAllowanceAsync(zrxAddress, owner, spender); - expect(initSpenderAllowance).to.be.bignumber.equal(newSpenderAllowance); - }); - - it('should transfer the correct balances if spender has sufficient allowance', async () => { - const initOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); - const initSpenderBalance = await zeroEx.token.getBalanceAsync(zrxAddress, spender); - const amountToTransfer = initOwnerBalance; - const initSpenderAllowance = initOwnerBalance; - let txHash = await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, initSpenderAllowance); - await zeroEx.awaitTransactionMinedAsync(txHash); - txHash = await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, { - gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, - }); - await zeroEx.awaitTransactionMinedAsync(txHash); - - const newOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); - const newSpenderBalance = await zeroEx.token.getBalanceAsync(zrxAddress, spender); - - expect(newOwnerBalance).to.be.bignumber.equal(0); - expect(newSpenderBalance).to.be.bignumber.equal(initSpenderBalance.plus(initOwnerBalance)); - }); - - it('should modify allowance if spender has sufficient allowance less than 2^256 - 1', async () => { - const initOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); - const amountToTransfer = initOwnerBalance; - let txHash = await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, amountToTransfer); - await zeroEx.awaitTransactionMinedAsync(txHash); - txHash = await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, { - gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, - }); - await zeroEx.awaitTransactionMinedAsync(txHash); - - const newSpenderAllowance = await zeroEx.token.getAllowanceAsync(zrxAddress, owner, spender); - expect(newSpenderAllowance).to.be.bignumber.equal(0); - }); - }); -}); diff --git a/packages/contracts/test/unlimited_allowance_token.ts b/packages/contracts/test/unlimited_allowance_token.ts new file mode 100644 index 000000000..1b8abd510 --- /dev/null +++ b/packages/contracts/test/unlimited_allowance_token.ts @@ -0,0 +1,127 @@ +import { ZeroEx } from '0x.js'; +import { BigNumber } from '@0xproject/utils'; +import * as chai from 'chai'; +import * as Web3 from 'web3'; + +import { Artifacts } from '../util/artifacts'; +import { constants } from '../util/constants'; +import { ContractInstance } from '../util/types'; + +import { chaiSetup } from './utils/chai_setup'; + +const { DummyToken } = new Artifacts(artifacts); +const web3: Web3 = (global as any).web3; +chaiSetup.configure(); +const expect = chai.expect; + +contract('UnlimitedAllowanceToken', (accounts: string[]) => { + const config = { + networkId: constants.TESTRPC_NETWORK_ID, + }; + const zeroEx = new ZeroEx(web3.currentProvider, config); + const owner = accounts[0]; + const spender = accounts[1]; + + const MAX_MINT_VALUE = new BigNumber(100000000000000000000); + let tokenAddress: string; + let token: ContractInstance; + + beforeEach(async () => { + token = await DummyToken.new({ from: owner }); + await token.mint(MAX_MINT_VALUE, { from: owner }); + tokenAddress = token.address; + }); + + describe('transfer', () => { + it('should transfer balance from sender to receiver', async () => { + const receiver = spender; + const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); + const amountToTransfer = new BigNumber(1); + await zeroEx.token.transferAsync(tokenAddress, owner, receiver, amountToTransfer); + const finalOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); + const finalReceiverBalance = await zeroEx.token.getBalanceAsync(tokenAddress, receiver); + + const expectedFinalOwnerBalance = initOwnerBalance.minus(amountToTransfer); + const expectedFinalReceiverBalance = amountToTransfer; + expect(finalOwnerBalance).to.be.bignumber.equal(expectedFinalOwnerBalance); + expect(finalReceiverBalance).to.be.bignumber.equal(expectedFinalReceiverBalance); + }); + + it('should return true on a 0 value transfer', async () => { + const didReturnTrue = await token.transfer.call(spender, 0, { + from: owner, + }); + expect(didReturnTrue).to.be.true(); + }); + }); + + describe('transferFrom', () => { + it('should return false if owner has insufficient balance', async () => { + const ownerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); + const amountToTransfer = ownerBalance.plus(1); + await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, amountToTransfer); + const didReturnTrue = await token.transferFrom.call(owner, spender, amountToTransfer, { from: spender }); + expect(didReturnTrue).to.be.false(); + }); + + it('should return false if spender has insufficient allowance', async () => { + const ownerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); + const amountToTransfer = ownerBalance; + + const spenderAllowance = await zeroEx.token.getAllowanceAsync(tokenAddress, owner, spender); + const spenderAllowanceIsInsufficient = spenderAllowance.cmp(amountToTransfer) < 0; + expect(spenderAllowanceIsInsufficient).to.be.true(); + + const didReturnTrue = await token.transferFrom.call(owner, spender, amountToTransfer, { from: spender }); + expect(didReturnTrue).to.be.false(); + }); + + it('should return true on a 0 value transfer', async () => { + const amountToTransfer = 0; + const didReturnTrue = await token.transferFrom.call(owner, spender, amountToTransfer, { from: spender }); + expect(didReturnTrue).to.be.true(); + }); + + it('should not modify spender allowance if spender allowance is 2^256 - 1', async () => { + const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); + const amountToTransfer = initOwnerBalance; + const initSpenderAllowance = zeroEx.token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS; + await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, initSpenderAllowance); + await zeroEx.token.transferFromAsync(tokenAddress, owner, spender, spender, amountToTransfer, { + gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, + }); + + const newSpenderAllowance = await zeroEx.token.getAllowanceAsync(tokenAddress, owner, spender); + expect(initSpenderAllowance).to.be.bignumber.equal(newSpenderAllowance); + }); + + it('should transfer the correct balances if spender has sufficient allowance', async () => { + const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); + const amountToTransfer = initOwnerBalance; + const initSpenderAllowance = initOwnerBalance; + await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, initSpenderAllowance); + await zeroEx.token.transferFromAsync(tokenAddress, owner, spender, spender, amountToTransfer, { + gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, + }); + + const newOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); + const newSpenderBalance = await zeroEx.token.getBalanceAsync(tokenAddress, spender); + + expect(newOwnerBalance).to.be.bignumber.equal(0); + expect(newSpenderBalance).to.be.bignumber.equal(initOwnerBalance); + }); + + it('should modify allowance if spender has sufficient allowance less than 2^256 - 1', async () => { + const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); + const amountToTransfer = initOwnerBalance; + const initSpenderAllowance = initOwnerBalance; + await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, initSpenderAllowance); + await zeroEx.token.transferFromAsync(tokenAddress, owner, spender, spender, amountToTransfer, { + gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, + }); + + const newSpenderAllowance = await zeroEx.token.getAllowanceAsync(tokenAddress, owner, spender); + expect(newSpenderAllowance).to.be.bignumber.equal(0); + }); + }); +}); diff --git a/packages/contracts/test/unlimited_allowance_token_v2.ts b/packages/contracts/test/unlimited_allowance_token_v2.ts new file mode 100644 index 000000000..47927b913 --- /dev/null +++ b/packages/contracts/test/unlimited_allowance_token_v2.ts @@ -0,0 +1,141 @@ +import { ZeroEx } from '0x.js'; +import { BigNumber } from '@0xproject/utils'; +import * as chai from 'chai'; +import * as Web3 from 'web3'; + +import { Artifacts } from '../util/artifacts'; +import { constants } from '../util/constants'; +import { ContractInstance } from '../util/types'; + +import { chaiSetup } from './utils/chai_setup'; + +const { DummyTokenV2 } = new Artifacts(artifacts); +const web3: Web3 = (global as any).web3; +chaiSetup.configure(); +const expect = chai.expect; + +contract('UnlimitedAllowanceTokenV2', (accounts: string[]) => { + const config = { + networkId: constants.TESTRPC_NETWORK_ID, + }; + const zeroEx = new ZeroEx(web3.currentProvider, config); + const owner = accounts[0]; + const spender = accounts[1]; + + const MAX_MINT_VALUE = new BigNumber(100000000000000000000); + let tokenAddress: string; + let token: ContractInstance; + + beforeEach(async () => { + token = await DummyTokenV2.new({ from: owner }); + await token.mint(MAX_MINT_VALUE, { from: owner }); + tokenAddress = token.address; + }); + + describe('transfer', () => { + it('should throw if owner has insufficient balance', async () => { + const ownerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); + const amountToTransfer = ownerBalance.plus(1); + return expect(token.transfer.call(spender, amountToTransfer, { from: owner })).to.be.rejectedWith( + constants.REVERT, + ); + }); + + it('should transfer balance from sender to receiver', async () => { + const receiver = spender; + const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); + const amountToTransfer = new BigNumber(1); + await zeroEx.token.transferAsync(tokenAddress, owner, receiver, amountToTransfer); + const finalOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); + const finalReceiverBalance = await zeroEx.token.getBalanceAsync(tokenAddress, receiver); + + const expectedFinalOwnerBalance = initOwnerBalance.minus(amountToTransfer); + const expectedFinalReceiverBalance = amountToTransfer; + expect(finalOwnerBalance).to.be.bignumber.equal(expectedFinalOwnerBalance); + expect(finalReceiverBalance).to.be.bignumber.equal(expectedFinalReceiverBalance); + }); + + it('should return true on a 0 value transfer', async () => { + const didReturnTrue = await token.transfer.call(spender, 0, { + from: owner, + }); + expect(didReturnTrue).to.be.true(); + }); + }); + + describe('transferFrom', () => { + it('should throw if owner has insufficient balance', async () => { + const ownerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); + const amountToTransfer = ownerBalance.plus(1); + await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, amountToTransfer); + return expect( + token.transferFrom.call(owner, spender, amountToTransfer, { + from: spender, + }), + ).to.be.rejectedWith(constants.REVERT); + }); + + it('should throw if spender has insufficient allowance', async () => { + const ownerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); + const amountToTransfer = ownerBalance; + + const spenderAllowance = await zeroEx.token.getAllowanceAsync(tokenAddress, owner, spender); + const spenderAllowanceIsInsufficient = spenderAllowance.cmp(amountToTransfer) < 0; + expect(spenderAllowanceIsInsufficient).to.be.true(); + + return expect( + token.transferFrom.call(owner, spender, amountToTransfer, { + from: spender, + }), + ).to.be.rejectedWith(constants.REVERT); + }); + + it('should return true on a 0 value transfer', async () => { + const amountToTransfer = 0; + const didReturnTrue = await token.transferFrom.call(owner, spender, amountToTransfer, { from: spender }); + expect(didReturnTrue).to.be.true(); + }); + + it('should not modify spender allowance if spender allowance is 2^256 - 1', async () => { + const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); + const amountToTransfer = initOwnerBalance; + const initSpenderAllowance = zeroEx.token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS; + await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, initSpenderAllowance); + await zeroEx.token.transferFromAsync(tokenAddress, owner, spender, spender, amountToTransfer, { + gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, + }); + + const newSpenderAllowance = await zeroEx.token.getAllowanceAsync(tokenAddress, owner, spender); + expect(initSpenderAllowance).to.be.bignumber.equal(newSpenderAllowance); + }); + + it('should transfer the correct balances if spender has sufficient allowance', async () => { + const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); + const amountToTransfer = initOwnerBalance; + const initSpenderAllowance = initOwnerBalance; + await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, initSpenderAllowance); + await zeroEx.token.transferFromAsync(tokenAddress, owner, spender, spender, amountToTransfer, { + gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, + }); + + const newOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); + const newSpenderBalance = await zeroEx.token.getBalanceAsync(tokenAddress, spender); + + expect(newOwnerBalance).to.be.bignumber.equal(0); + expect(newSpenderBalance).to.be.bignumber.equal(initOwnerBalance); + }); + + it('should modify allowance if spender has sufficient allowance less than 2^256 - 1', async () => { + const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); + const amountToTransfer = initOwnerBalance; + const initSpenderAllowance = initOwnerBalance; + await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, initSpenderAllowance); + await zeroEx.token.transferFromAsync(tokenAddress, owner, spender, spender, amountToTransfer, { + gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, + }); + + const newSpenderAllowance = await zeroEx.token.getAllowanceAsync(tokenAddress, owner, spender); + expect(newSpenderAllowance).to.be.bignumber.equal(0); + }); + }); +}); diff --git a/packages/contracts/test/utils/chai_setup.ts b/packages/contracts/test/utils/chai_setup.ts new file mode 100644 index 000000000..078edd309 --- /dev/null +++ b/packages/contracts/test/utils/chai_setup.ts @@ -0,0 +1,13 @@ +import * as chai from 'chai'; +import chaiAsPromised = require('chai-as-promised'); +import ChaiBigNumber = require('chai-bignumber'); +import * as dirtyChai from 'dirty-chai'; + +export const chaiSetup = { + configure() { + chai.config.includeStack = true; + chai.use(ChaiBigNumber()); + chai.use(dirtyChai); + chai.use(chaiAsPromised); + }, +}; diff --git a/packages/contracts/test/zrx_token.ts b/packages/contracts/test/zrx_token.ts new file mode 100644 index 000000000..60ce5487e --- /dev/null +++ b/packages/contracts/test/zrx_token.ts @@ -0,0 +1,175 @@ +import { ZeroEx } from '0x.js'; +import { BigNumber } from '@0xproject/utils'; +import * as chai from 'chai'; +import Web3 = require('web3'); + +import { Artifacts } from '../util/artifacts'; +import { constants } from '../util/constants'; +import { ContractInstance } from '../util/types'; + +import { chaiSetup } from './utils/chai_setup'; + +chaiSetup.configure(); +const expect = chai.expect; +const { Exchange, ZRXToken } = new Artifacts(artifacts); +const web3: Web3 = (global as any).web3; + +contract('ZRXToken', (accounts: string[]) => { + const owner = accounts[0]; + const spender = accounts[1]; + let zeroEx: ZeroEx; + + let MAX_UINT: BigNumber; + + let zrx: ContractInstance; + let zrxAddress: string; + + beforeEach(async () => { + zeroEx = new ZeroEx(web3.currentProvider, { + exchangeContractAddress: Exchange.address, + networkId: constants.TESTRPC_NETWORK_ID, + }); + zrx = await ZRXToken.new(); + zrxAddress = zrx.address; + MAX_UINT = zeroEx.token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS; + }); + + describe('constants', () => { + it('should have 18 decimals', async () => { + const decimals = new BigNumber(await zrx.decimals.call()); + 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.call()); + 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.call(); + const expectedName = '0x Protocol Token'; + expect(name).to.be.equal(expectedName); + }); + + it('should have the symbol ZRX', async () => { + const symbol = await zrx.symbol.call(); + const expectedSymbol = 'ZRX'; + expect(symbol).to.be.equal(expectedSymbol); + }); + }); + + 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.call()); + expect(totalSupply).to.be.bignumber.equal(ownerBalance); + }); + }); + + describe('transfer', () => { + it('should transfer balance from sender to receiver', async () => { + const receiver = spender; + const initOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); + const amountToTransfer = new BigNumber(1); + const txHash = await zeroEx.token.transferAsync(zrxAddress, owner, receiver, amountToTransfer); + await zeroEx.awaitTransactionMinedAsync(txHash); + const finalOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); + const finalReceiverBalance = await zeroEx.token.getBalanceAsync(zrxAddress, receiver); + + const expectedFinalOwnerBalance = initOwnerBalance.minus(amountToTransfer); + const expectedFinalReceiverBalance = amountToTransfer; + expect(finalOwnerBalance).to.be.bignumber.equal(expectedFinalOwnerBalance); + expect(finalReceiverBalance).to.be.bignumber.equal(expectedFinalReceiverBalance); + }); + + it('should return true on a 0 value transfer', async () => { + const didReturnTrue = await zrx.transfer.call(spender, 0, { + from: owner, + }); + expect(didReturnTrue).to.be.true(); + }); + }); + + describe('transferFrom', () => { + it('should return false if owner has insufficient balance', async () => { + const ownerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); + const amountToTransfer = ownerBalance.plus(1); + const txHash = await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, amountToTransfer, { + gasLimit: constants.MAX_TOKEN_APPROVE_GAS, + }); + await zeroEx.awaitTransactionMinedAsync(txHash); + const didReturnTrue = await zrx.transferFrom.call(owner, spender, amountToTransfer, { from: spender }); + expect(didReturnTrue).to.be.false(); + }); + + it('should return false if spender has insufficient allowance', async () => { + const ownerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); + const amountToTransfer = ownerBalance; + + const spenderAllowance = await zeroEx.token.getAllowanceAsync(zrxAddress, owner, spender); + const spenderAllowanceIsInsufficient = spenderAllowance.cmp(amountToTransfer) < 0; + expect(spenderAllowanceIsInsufficient).to.be.true(); + + const didReturnTrue = await zrx.transferFrom.call(owner, spender, amountToTransfer, { from: spender }); + expect(didReturnTrue).to.be.false(); + }); + + it('should return true on a 0 value transfer', async () => { + const amountToTransfer = 0; + const didReturnTrue = await zrx.transferFrom.call(owner, spender, amountToTransfer, { from: spender }); + expect(didReturnTrue).to.be.true(); + }); + + it('should not modify spender allowance if spender allowance is 2^256 - 1', async () => { + const initOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); + const amountToTransfer = initOwnerBalance; + const initSpenderAllowance = MAX_UINT; + let txHash = await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, initSpenderAllowance, { + gasLimit: constants.MAX_TOKEN_APPROVE_GAS, + }); + await zeroEx.awaitTransactionMinedAsync(txHash); + txHash = await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, { + gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, + }); + await zeroEx.awaitTransactionMinedAsync(txHash); + + const newSpenderAllowance = await zeroEx.token.getAllowanceAsync(zrxAddress, owner, spender); + expect(initSpenderAllowance).to.be.bignumber.equal(newSpenderAllowance); + }); + + it('should transfer the correct balances if spender has sufficient allowance', async () => { + const initOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); + const initSpenderBalance = await zeroEx.token.getBalanceAsync(zrxAddress, spender); + const amountToTransfer = initOwnerBalance; + const initSpenderAllowance = initOwnerBalance; + let txHash = await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, initSpenderAllowance); + await zeroEx.awaitTransactionMinedAsync(txHash); + txHash = await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, { + gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, + }); + await zeroEx.awaitTransactionMinedAsync(txHash); + + const newOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); + const newSpenderBalance = await zeroEx.token.getBalanceAsync(zrxAddress, spender); + + expect(newOwnerBalance).to.be.bignumber.equal(0); + expect(newSpenderBalance).to.be.bignumber.equal(initSpenderBalance.plus(initOwnerBalance)); + }); + + it('should modify allowance if spender has sufficient allowance less than 2^256 - 1', async () => { + const initOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); + const amountToTransfer = initOwnerBalance; + let txHash = await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, amountToTransfer); + await zeroEx.awaitTransactionMinedAsync(txHash); + txHash = await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, { + gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, + }); + await zeroEx.awaitTransactionMinedAsync(txHash); + + const newSpenderAllowance = await zeroEx.token.getAllowanceAsync(zrxAddress, owner, spender); + expect(newSpenderAllowance).to.be.bignumber.equal(0); + }); + }); +}); -- cgit v1.2.3 From fca051c5656c29164d9ef05fe5124b98972b79cc Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 19 Jan 2018 13:49:10 +0100 Subject: Remove truffle from tokenTransferProxy tests --- .../contracts/test/token_transfer_proxy/auth.ts | 67 +++++++++++----------- .../test/token_transfer_proxy/transfer_from.ts | 9 +-- 2 files changed, 37 insertions(+), 39 deletions(-) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/token_transfer_proxy/auth.ts b/packages/contracts/test/token_transfer_proxy/auth.ts index aa0ad2fc5..414de9834 100644 --- a/packages/contracts/test/token_transfer_proxy/auth.ts +++ b/packages/contracts/test/token_transfer_proxy/auth.ts @@ -1,4 +1,7 @@ +import { BlockchainLifecycle } from '@0xproject/dev-utils'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; +import * as Web3 from 'web3'; import { constants } from '../../util/constants'; import { ContractInstance } from '../../util/types'; @@ -7,38 +10,44 @@ import { chaiSetup } from '../utils/chai_setup'; chaiSetup.configure(); const expect = chai.expect; const TokenTransferProxy = artifacts.require('./db/TokenTransferProxy.sol'); +// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle +// with type `any` to a variable of type `Web3`. +const web3: Web3 = (global as any).web3; +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); -contract('TokenTransferProxy', (accounts: string[]) => { - const owner = accounts[0]; - const notOwner = accounts[1]; - +describe('TokenTransferProxy', () => { + const web3Wrapper = new Web3Wrapper(web3.currentProvider); + let accounts: string[]; + let owner: string; + let notOwner: string; + let address: string; let tokenTransferProxy: ContractInstance; - let authorized: string; - let notAuthorized = owner; - before(async () => { + accounts = await web3Wrapper.getAvailableAddressesAsync(); + owner = address = accounts[0]; + notOwner = accounts[1]; tokenTransferProxy = await TokenTransferProxy.deployed(); }); - + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); describe('addAuthorizedAddress', () => { it('should throw if not called by owner', async () => { return expect(tokenTransferProxy.addAuthorizedAddress(notOwner, { from: notOwner })).to.be.rejectedWith( constants.REVERT, ); }); - it('should allow owner to add an authorized address', async () => { - await tokenTransferProxy.addAuthorizedAddress(notAuthorized, { - from: owner, - }); - authorized = notAuthorized; - notAuthorized = null; - const isAuthorized = await tokenTransferProxy.authorized.call(authorized); + await tokenTransferProxy.addAuthorizedAddress(address, { from: owner }); + const isAuthorized = await tokenTransferProxy.authorized.call(address); expect(isAuthorized).to.be.true(); }); - it('should throw if owner attempts to authorize a duplicate address', async () => { - return expect(tokenTransferProxy.addAuthorizedAddress(authorized, { from: owner })).to.be.rejectedWith( + await tokenTransferProxy.addAuthorizedAddress(address, { from: owner }); + return expect(tokenTransferProxy.addAuthorizedAddress(address, { from: owner })).to.be.rejectedWith( constants.REVERT, ); }); @@ -46,27 +55,26 @@ contract('TokenTransferProxy', (accounts: string[]) => { describe('removeAuthorizedAddress', () => { it('should throw if not called by owner', async () => { + await tokenTransferProxy.addAuthorizedAddress(address, { from: owner }); return expect( - tokenTransferProxy.removeAuthorizedAddress(authorized, { + tokenTransferProxy.removeAuthorizedAddress(address, { from: notOwner, }), ).to.be.rejectedWith(constants.REVERT); }); it('should allow owner to remove an authorized address', async () => { - await tokenTransferProxy.removeAuthorizedAddress(authorized, { + await tokenTransferProxy.addAuthorizedAddress(address, { from: owner }); + await tokenTransferProxy.removeAuthorizedAddress(address, { from: owner, }); - notAuthorized = authorized; - authorized = null; - - const isAuthorized = await tokenTransferProxy.authorized.call(notAuthorized); + const isAuthorized = await tokenTransferProxy.authorized.call(address); expect(isAuthorized).to.be.false(); }); it('should throw if owner attempts to remove an address that is not authorized', async () => { return expect( - tokenTransferProxy.removeAuthorizedAddress(notAuthorized, { + tokenTransferProxy.removeAuthorizedAddress(address, { from: owner, }), ).to.be.rejectedWith(constants.REVERT); @@ -77,21 +85,16 @@ contract('TokenTransferProxy', (accounts: string[]) => { it('should return all authorized addresses', async () => { const initial = await tokenTransferProxy.getAuthorizedAddresses(); expect(initial).to.have.length(1); - await tokenTransferProxy.addAuthorizedAddress(notAuthorized, { + await tokenTransferProxy.addAuthorizedAddress(address, { from: owner, }); - - authorized = notAuthorized; - notAuthorized = null; const afterAdd = await tokenTransferProxy.getAuthorizedAddresses(); expect(afterAdd).to.have.length(2); - expect(afterAdd).to.include(authorized); + expect(afterAdd).to.include(address); - await tokenTransferProxy.removeAuthorizedAddress(authorized, { + await tokenTransferProxy.removeAuthorizedAddress(address, { from: owner, }); - notAuthorized = authorized; - authorized = null; const afterRemove = await tokenTransferProxy.getAuthorizedAddresses(); expect(afterRemove).to.have.length(1); }); diff --git a/packages/contracts/test/token_transfer_proxy/transfer_from.ts b/packages/contracts/test/token_transfer_proxy/transfer_from.ts index b5e1c7017..1a5151309 100644 --- a/packages/contracts/test/token_transfer_proxy/transfer_from.ts +++ b/packages/contracts/test/token_transfer_proxy/transfer_from.ts @@ -22,13 +22,6 @@ describe('TokenTransferProxy', () => { let accounts: string[]; let owner: string; let notAuthorized: string; - const config = { - networkId: constants.TESTRPC_NETWORK_ID, - }; - before(async () => { - accounts = await web3Wrapper.getAvailableAddressesAsync(); - owner = notAuthorized = accounts[0]; - }); const INIT_BAL = 100000000; const INIT_ALLOW = 100000000; @@ -38,6 +31,8 @@ describe('TokenTransferProxy', () => { let dmyBalances: Balances; before(async () => { + accounts = await web3Wrapper.getAvailableAddressesAsync(); + owner = notAuthorized = accounts[0]; [tokenTransferProxy, tokenRegistry] = await Promise.all([ TokenTransferProxy.deployed(), TokenRegistry.deployed(), -- cgit v1.2.3 From 091ba473ff83ebd3239273b8e40671b9ccab5f47 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 19 Jan 2018 13:56:36 +0100 Subject: Remove truffle from Ether Token tests --- packages/contracts/test/ether_token.ts | 36 ++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/ether_token.ts b/packages/contracts/test/ether_token.ts index cce11812b..a5a97a60a 100644 --- a/packages/contracts/test/ether_token.ts +++ b/packages/contracts/test/ether_token.ts @@ -1,7 +1,9 @@ import { ZeroEx, ZeroExError } from '0x.js'; +import { BlockchainLifecycle } from '@0xproject/dev-utils'; import { BigNumber, promisify } from '@0xproject/utils'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; -import Web3 = require('web3'); +import * as Web3 from 'web3'; import { Artifacts } from '../util/artifacts'; import { constants } from '../util/constants'; @@ -16,28 +18,36 @@ const expect = chai.expect; // In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle // with type `any` to a variable of type `Web3`. const web3: Web3 = (global as any).web3; +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); -contract('EtherToken', (accounts: string[]) => { - const account = accounts[0]; +describe.only('EtherToken', () => { + const web3Wrapper = new Web3Wrapper(web3.currentProvider); + let accounts: string[]; + let account: string; const gasPrice = ZeroEx.toBaseUnitAmount(new BigNumber(20), 9); let zeroEx: ZeroEx; let etherTokenAddress: string; - + const sendTransactionAsync = promisify(web3.eth.sendTransaction); + const getEthBalanceAsync = async (owner: string) => { + const balanceStr = await promisify(web3.eth.getBalance)(owner); + const balance = new BigNumber(balanceStr); + return balance; + }; before(async () => { + accounts = await web3Wrapper.getAvailableAddressesAsync(); + account = accounts[0]; etherTokenAddress = EtherToken.address; zeroEx = new ZeroEx(web3.currentProvider, { gasPrice, networkId: constants.TESTRPC_NETWORK_ID, }); }); - - const sendTransactionAsync = promisify(web3.eth.sendTransaction); - const getEthBalanceAsync = async (owner: string) => { - const balanceStr = await promisify(web3.eth.getBalance)(owner); - const balance = new BigNumber(balanceStr); - return balance; - }; - + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); describe('deposit', () => { it('should throw if caller attempts to deposit more Ether than caller balance', async () => { const initEthBalance = await getEthBalanceAsync(account); @@ -77,6 +87,8 @@ contract('EtherToken', (accounts: string[]) => { }); it('should convert ether tokens to ether with sufficient balance', async () => { + const ethToDeposit = new BigNumber(web3.toWei(1, 'ether')); + await zeroEx.etherToken.depositAsync(etherTokenAddress, ethToDeposit, account); const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); const initEthBalance = await getEthBalanceAsync(account); const ethTokensToWithdraw = initEthTokenBalance; -- cgit v1.2.3 From eb881b9729374c3eedb4914e5293535e890296af Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 19 Jan 2018 14:07:00 +0100 Subject: Remove truffle from MultiSigWalletWithTimeLockExceptRemoveAuthAddr tests --- packages/contracts/test/ether_token.ts | 2 +- ...i_sig_with_time_lock_except_remove_auth_addr.ts | 44 +++++++++++++++------- 2 files changed, 32 insertions(+), 14 deletions(-) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/ether_token.ts b/packages/contracts/test/ether_token.ts index a5a97a60a..e46d93fe6 100644 --- a/packages/contracts/test/ether_token.ts +++ b/packages/contracts/test/ether_token.ts @@ -20,7 +20,7 @@ const expect = chai.expect; const web3: Web3 = (global as any).web3; const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); -describe.only('EtherToken', () => { +describe('EtherToken', () => { const web3Wrapper = new Web3Wrapper(web3.currentProvider); let accounts: string[]; let account: string; diff --git a/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts b/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts index 82115418e..27b885438 100644 --- a/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts +++ b/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts @@ -1,4 +1,7 @@ +import { BlockchainLifecycle } from '@0xproject/dev-utils'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; +import * as Web3 from 'web3'; import * as tokenTransferProxyJSON from '../../build/contracts/TokenTransferProxy.json'; import { Artifacts } from '../util/artifacts'; @@ -13,29 +16,38 @@ const PROXY_ABI = (tokenTransferProxyJSON as any).abi; chaiSetup.configure(); const expect = chai.expect; - -contract('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', (accounts: string[]) => { - const owners = [accounts[0], accounts[1]]; +// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle +// with type `any` to a variable of type `Web3`. +const web3: Web3 = (global as any).web3; +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); + +describe('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', () => { + const web3Wrapper = new Web3Wrapper(web3.currentProvider); + let accounts: string[]; + let owners: string[]; const requiredApprovals = 2; const SECONDS_TIME_LOCKED = 1000000; // initialize fake addresses - const authorizedAddress = `0x${crypto - .solSHA3([accounts[0]]) - .slice(0, 20) - .toString('hex')}`; - const unauthorizedAddress = `0x${crypto - .solSHA3([accounts[1]]) - .slice(0, 20) - .toString('hex')}`; + let authorizedAddress: string; + let unauthorizedAddress: string; let tokenTransferProxy: ContractInstance; let multiSig: ContractInstance; let multiSigWrapper: MultiSigWrapper; let validDestination: string; - - beforeEach(async () => { + before(async () => { + accounts = await web3Wrapper.getAvailableAddressesAsync(); + owners = [accounts[0], accounts[1]]; + authorizedAddress = `0x${crypto + .solSHA3([accounts[0]]) + .slice(0, 20) + .toString('hex')}`; + unauthorizedAddress = `0x${crypto + .solSHA3([accounts[1]]) + .slice(0, 20) + .toString('hex')}`; const initialOwner = accounts[0]; tokenTransferProxy = await TokenTransferProxy.new({ from: initialOwner }); await tokenTransferProxy.addAuthorizedAddress(authorizedAddress, { @@ -53,6 +65,12 @@ contract('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', (accounts: s multiSigWrapper = new MultiSigWrapper(multiSig); validDestination = tokenTransferProxy.address; }); + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); describe('isFunctionRemoveAuthorizedAddress', () => { it('should throw if data is not for removeAuthorizedAddress', async () => { -- cgit v1.2.3 From 850d32d60ca4f6eb20b8b626d93c4786937e9309 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 19 Jan 2018 14:11:59 +0100 Subject: Remove truffle from MultiSigWalletWithTimeLock tests --- packages/contracts/test/multi_sig_with_time_lock.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/multi_sig_with_time_lock.ts b/packages/contracts/test/multi_sig_with_time_lock.ts index 5cd4dba3d..d41aae38b 100644 --- a/packages/contracts/test/multi_sig_with_time_lock.ts +++ b/packages/contracts/test/multi_sig_with_time_lock.ts @@ -1,7 +1,8 @@ -import { RPC } from '@0xproject/dev-utils'; +import { BlockchainLifecycle, RPC } from '@0xproject/dev-utils'; import { BigNumber, promisify } from '@0xproject/utils'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; -import Web3 = require('web3'); +import * as Web3 from 'web3'; import * as multiSigWalletJSON from '../../build/contracts/MultiSigWalletWithTimeLock.json'; import * as truffleConf from '../truffle.js'; @@ -22,8 +23,13 @@ const expect = chai.expect; // with type `any` to a variable of type `Web3`. const web3: Web3 = (global as any).web3; -contract('MultiSigWalletWithTimeLock', (accounts: string[]) => { - const owners = [accounts[0], accounts[1]]; +describe('MultiSigWalletWithTimeLock', () => { + const web3Wrapper = new Web3Wrapper(web3.currentProvider); + let owners: string[]; + before(async () => { + const accounts = await web3Wrapper.getAvailableAddressesAsync(); + owners = [accounts[0], accounts[1]]; + }); const SECONDS_TIME_LOCKED = 10000; let multiSig: ContractInstance; -- cgit v1.2.3 From 661029f7ccf8e28e87faf85bb451298b3b066bfa Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 19 Jan 2018 14:15:44 +0100 Subject: Remove truffle from TokenRegistry tests --- packages/contracts/test/token_registry.ts | 40 +++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 12 deletions(-) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/token_registry.ts b/packages/contracts/test/token_registry.ts index 9ca18202f..1e01dcb6d 100644 --- a/packages/contracts/test/token_registry.ts +++ b/packages/contracts/test/token_registry.ts @@ -1,7 +1,10 @@ import { ZeroEx } from '0x.js'; +import { BlockchainLifecycle } from '@0xproject/dev-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 { Artifacts } from '../util/artifacts'; import { constants } from '../util/constants'; @@ -13,10 +16,31 @@ import { chaiSetup } from './utils/chai_setup'; const { TokenRegistry } = new Artifacts(artifacts); chaiSetup.configure(); const expect = chai.expect; - -contract('TokenRegistry', (accounts: string[]) => { - const owner = accounts[0]; - const notOwner = accounts[1]; +// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle +// with type `any` to a variable of type `Web3`. +const web3: Web3 = (global as any).web3; +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); + +describe('TokenRegistry', () => { + const web3Wrapper = new Web3Wrapper(web3.currentProvider); + let accounts: string[]; + let owner: string; + let notOwner: string; + let tokenReg: ContractInstance; + let tokenRegWrapper: TokenRegWrapper; + before(async () => { + accounts = await web3Wrapper.getAvailableAddressesAsync(); + owner = accounts[0]; + notOwner = accounts[1]; + tokenReg = await TokenRegistry.new(); + tokenRegWrapper = new TokenRegWrapper(tokenReg); + }); + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); const tokenAddress1 = `0x${ethUtil.setLength(ethUtil.toBuffer('0x1'), 20, false).toString('hex')}`; const tokenAddress2 = `0x${ethUtil.setLength(ethUtil.toBuffer('0x2'), 20, false).toString('hex')}`; @@ -48,14 +72,6 @@ contract('TokenRegistry', (accounts: string[]) => { swarmHash: constants.NULL_BYTES, }; - let tokenReg: ContractInstance; - let tokenRegWrapper: TokenRegWrapper; - - beforeEach(async () => { - tokenReg = await TokenRegistry.new(); - tokenRegWrapper = new TokenRegWrapper(tokenReg); - }); - describe('addToken', () => { it('should throw when not called by owner', async () => { return expect(tokenRegWrapper.addTokenAsync(token1, notOwner)).to.be.rejectedWith(constants.REVERT); -- cgit v1.2.3 From 20c88a46d9d87835bd9ddc30374309d615a9cf9f Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 19 Jan 2018 14:21:50 +0100 Subject: Remove truffle from UnlimitedAllowanceTokenV2 tests --- packages/contracts/test/ether_token.ts | 3 +-- ...i_sig_with_time_lock_except_remove_auth_addr.ts | 3 +-- packages/contracts/test/token_registry.ts | 3 +-- .../contracts/test/token_transfer_proxy/auth.ts | 3 +-- .../contracts/test/unlimited_allowance_token_v2.ts | 24 ++++++++++++++++------ 5 files changed, 22 insertions(+), 14 deletions(-) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/ether_token.ts b/packages/contracts/test/ether_token.ts index e46d93fe6..31c6c4ed6 100644 --- a/packages/contracts/test/ether_token.ts +++ b/packages/contracts/test/ether_token.ts @@ -22,7 +22,6 @@ const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); describe('EtherToken', () => { const web3Wrapper = new Web3Wrapper(web3.currentProvider); - let accounts: string[]; let account: string; const gasPrice = ZeroEx.toBaseUnitAmount(new BigNumber(20), 9); let zeroEx: ZeroEx; @@ -34,7 +33,7 @@ describe('EtherToken', () => { return balance; }; before(async () => { - accounts = await web3Wrapper.getAvailableAddressesAsync(); + const accounts = await web3Wrapper.getAvailableAddressesAsync(); account = accounts[0]; etherTokenAddress = EtherToken.address; zeroEx = new ZeroEx(web3.currentProvider, { diff --git a/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts b/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts index 27b885438..f8e8c1f8d 100644 --- a/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts +++ b/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts @@ -23,7 +23,6 @@ const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); describe('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', () => { const web3Wrapper = new Web3Wrapper(web3.currentProvider); - let accounts: string[]; let owners: string[]; const requiredApprovals = 2; const SECONDS_TIME_LOCKED = 1000000; @@ -38,7 +37,7 @@ describe('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', () => { let validDestination: string; before(async () => { - accounts = await web3Wrapper.getAvailableAddressesAsync(); + const accounts = await web3Wrapper.getAvailableAddressesAsync(); owners = [accounts[0], accounts[1]]; authorizedAddress = `0x${crypto .solSHA3([accounts[0]]) diff --git a/packages/contracts/test/token_registry.ts b/packages/contracts/test/token_registry.ts index 1e01dcb6d..bcb620e03 100644 --- a/packages/contracts/test/token_registry.ts +++ b/packages/contracts/test/token_registry.ts @@ -23,13 +23,12 @@ const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); describe('TokenRegistry', () => { const web3Wrapper = new Web3Wrapper(web3.currentProvider); - let accounts: string[]; let owner: string; let notOwner: string; let tokenReg: ContractInstance; let tokenRegWrapper: TokenRegWrapper; before(async () => { - accounts = await web3Wrapper.getAvailableAddressesAsync(); + const accounts = await web3Wrapper.getAvailableAddressesAsync(); owner = accounts[0]; notOwner = accounts[1]; tokenReg = await TokenRegistry.new(); diff --git a/packages/contracts/test/token_transfer_proxy/auth.ts b/packages/contracts/test/token_transfer_proxy/auth.ts index 414de9834..399ad2080 100644 --- a/packages/contracts/test/token_transfer_proxy/auth.ts +++ b/packages/contracts/test/token_transfer_proxy/auth.ts @@ -17,13 +17,12 @@ const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); describe('TokenTransferProxy', () => { const web3Wrapper = new Web3Wrapper(web3.currentProvider); - let accounts: string[]; let owner: string; let notOwner: string; let address: string; let tokenTransferProxy: ContractInstance; before(async () => { - accounts = await web3Wrapper.getAvailableAddressesAsync(); + const accounts = await web3Wrapper.getAvailableAddressesAsync(); owner = address = accounts[0]; notOwner = accounts[1]; tokenTransferProxy = await TokenTransferProxy.deployed(); diff --git a/packages/contracts/test/unlimited_allowance_token_v2.ts b/packages/contracts/test/unlimited_allowance_token_v2.ts index 47927b913..ebdc3ba11 100644 --- a/packages/contracts/test/unlimited_allowance_token_v2.ts +++ b/packages/contracts/test/unlimited_allowance_token_v2.ts @@ -1,5 +1,7 @@ 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'; @@ -10,28 +12,38 @@ import { ContractInstance } from '../util/types'; import { chaiSetup } from './utils/chai_setup'; const { DummyTokenV2 } = new Artifacts(artifacts); -const web3: Web3 = (global as any).web3; chaiSetup.configure(); const expect = chai.expect; +const web3: Web3 = (global as any).web3; +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); -contract('UnlimitedAllowanceTokenV2', (accounts: string[]) => { +describe('UnlimitedAllowanceTokenV2', () => { + const web3Wrapper = new Web3Wrapper(web3.currentProvider); const config = { networkId: constants.TESTRPC_NETWORK_ID, }; const zeroEx = new ZeroEx(web3.currentProvider, config); - const owner = accounts[0]; - const spender = accounts[1]; + let owner: string; + let spender: string; const MAX_MINT_VALUE = new BigNumber(100000000000000000000); let tokenAddress: string; let token: ContractInstance; - beforeEach(async () => { + before(async () => { + const accounts = await web3Wrapper.getAvailableAddressesAsync(); + owner = accounts[0]; + spender = accounts[1]; token = await DummyTokenV2.new({ from: owner }); await token.mint(MAX_MINT_VALUE, { from: owner }); tokenAddress = token.address; }); - + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); describe('transfer', () => { it('should throw if owner has insufficient balance', async () => { const ownerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); -- cgit v1.2.3 From 8269610a5c16cebda268c2497fae0adf692e4838 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 19 Jan 2018 14:25:18 +0100 Subject: Remove truffle from UnlimitedAllowanceToken tests --- .../contracts/test/multi_sig_with_time_lock.ts | 7 +++++++ .../contracts/test/unlimited_allowance_token.ts | 24 +++++++++++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/multi_sig_with_time_lock.ts b/packages/contracts/test/multi_sig_with_time_lock.ts index d41aae38b..03914786c 100644 --- a/packages/contracts/test/multi_sig_with_time_lock.ts +++ b/packages/contracts/test/multi_sig_with_time_lock.ts @@ -22,6 +22,7 @@ const expect = chai.expect; // In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle // with type `any` to a variable of type `Web3`. const web3: Web3 = (global as any).web3; +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); describe('MultiSigWalletWithTimeLock', () => { const web3Wrapper = new Web3Wrapper(web3.currentProvider); @@ -47,6 +48,12 @@ describe('MultiSigWalletWithTimeLock', () => { const rpcUrl = `http://${truffleConf.networks.development.host}:${truffleConf.networks.development.port}`; rpc = new RPC(rpcUrl); }); + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); describe('changeTimeLock', () => { it('should throw when not called by wallet', async () => { diff --git a/packages/contracts/test/unlimited_allowance_token.ts b/packages/contracts/test/unlimited_allowance_token.ts index 1b8abd510..ca8ce4c50 100644 --- a/packages/contracts/test/unlimited_allowance_token.ts +++ b/packages/contracts/test/unlimited_allowance_token.ts @@ -1,5 +1,7 @@ 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'; @@ -10,28 +12,40 @@ import { ContractInstance } from '../util/types'; import { chaiSetup } from './utils/chai_setup'; const { DummyToken } = new Artifacts(artifacts); +// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle +// with type `any` to a variable of type `Web3`. const web3: Web3 = (global as any).web3; chaiSetup.configure(); const expect = chai.expect; +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); -contract('UnlimitedAllowanceToken', (accounts: string[]) => { +describe('UnlimitedAllowanceToken', () => { + const web3Wrapper = new Web3Wrapper(web3.currentProvider); + let owner: string; + let spender: string; const config = { networkId: constants.TESTRPC_NETWORK_ID, }; const zeroEx = new ZeroEx(web3.currentProvider, config); - const owner = accounts[0]; - const spender = accounts[1]; const MAX_MINT_VALUE = new BigNumber(100000000000000000000); let tokenAddress: string; let token: ContractInstance; - beforeEach(async () => { + before(async () => { + const accounts = await web3Wrapper.getAvailableAddressesAsync(); + owner = accounts[0]; + spender = accounts[1]; token = await DummyToken.new({ from: owner }); await token.mint(MAX_MINT_VALUE, { from: owner }); tokenAddress = token.address; }); - + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); describe('transfer', () => { it('should transfer balance from sender to receiver', async () => { const receiver = spender; -- cgit v1.2.3 From d0fbea76d8790c56d07dcfe858e4969fe218cea1 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 19 Jan 2018 14:37:13 +0100 Subject: Remove truffle from ZRXToken tests --- packages/contracts/test/zrx_token.ts | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/zrx_token.ts b/packages/contracts/test/zrx_token.ts index 60ce5487e..fe251f53d 100644 --- a/packages/contracts/test/zrx_token.ts +++ b/packages/contracts/test/zrx_token.ts @@ -1,5 +1,7 @@ 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 Web3 = require('web3'); @@ -12,11 +14,15 @@ import { chaiSetup } from './utils/chai_setup'; chaiSetup.configure(); const expect = chai.expect; const { Exchange, ZRXToken } = new Artifacts(artifacts); +// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle +// with type `any` to a variable of type `Web3`. const web3: Web3 = (global as any).web3; +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); -contract('ZRXToken', (accounts: string[]) => { - const owner = accounts[0]; - const spender = accounts[1]; +describe('ZRXToken', () => { + const web3Wrapper = new Web3Wrapper(web3.currentProvider); + let owner: string; + let spender: string; let zeroEx: ZeroEx; let MAX_UINT: BigNumber; @@ -24,7 +30,10 @@ contract('ZRXToken', (accounts: string[]) => { let zrx: ContractInstance; let zrxAddress: string; - beforeEach(async () => { + before(async () => { + const accounts = await web3Wrapper.getAvailableAddressesAsync(); + owner = accounts[0]; + spender = accounts[1]; zeroEx = new ZeroEx(web3.currentProvider, { exchangeContractAddress: Exchange.address, networkId: constants.TESTRPC_NETWORK_ID, @@ -33,7 +42,12 @@ contract('ZRXToken', (accounts: string[]) => { zrxAddress = zrx.address; MAX_UINT = zeroEx.token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS; }); - + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); describe('constants', () => { it('should have 18 decimals', async () => { const decimals = new BigNumber(await zrx.decimals.call()); @@ -129,11 +143,9 @@ contract('ZRXToken', (accounts: string[]) => { let txHash = await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, initSpenderAllowance, { gasLimit: constants.MAX_TOKEN_APPROVE_GAS, }); - await zeroEx.awaitTransactionMinedAsync(txHash); txHash = await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, { gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, }); - await zeroEx.awaitTransactionMinedAsync(txHash); const newSpenderAllowance = await zeroEx.token.getAllowanceAsync(zrxAddress, owner, spender); expect(initSpenderAllowance).to.be.bignumber.equal(newSpenderAllowance); @@ -145,11 +157,9 @@ contract('ZRXToken', (accounts: string[]) => { const amountToTransfer = initOwnerBalance; const initSpenderAllowance = initOwnerBalance; let txHash = await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, initSpenderAllowance); - await zeroEx.awaitTransactionMinedAsync(txHash); txHash = await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, { gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, }); - await zeroEx.awaitTransactionMinedAsync(txHash); const newOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); const newSpenderBalance = await zeroEx.token.getBalanceAsync(zrxAddress, spender); @@ -162,11 +172,9 @@ contract('ZRXToken', (accounts: string[]) => { const initOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); const amountToTransfer = initOwnerBalance; let txHash = await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, amountToTransfer); - await zeroEx.awaitTransactionMinedAsync(txHash); txHash = await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, { gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, }); - await zeroEx.awaitTransactionMinedAsync(txHash); const newSpenderAllowance = await zeroEx.token.getAllowanceAsync(zrxAddress, owner, spender); expect(newSpenderAllowance).to.be.bignumber.equal(0); -- cgit v1.2.3 From f2b2b867866faa92256bc175e651e038001cef4d Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 19 Jan 2018 14:54:44 +0100 Subject: Remove truffle from Exchange tests --- packages/contracts/test/exchange/core.ts | 29 +++++++++++++++++++-------- packages/contracts/test/exchange/helpers.ts | 25 ++++++++++++++++++----- packages/contracts/test/exchange/wrapper.ts | 31 ++++++++++++++++++++++------- 3 files changed, 65 insertions(+), 20 deletions(-) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/exchange/core.ts b/packages/contracts/test/exchange/core.ts index 0e94fcc34..9a6b4dc83 100644 --- a/packages/contracts/test/exchange/core.ts +++ b/packages/contracts/test/exchange/core.ts @@ -1,5 +1,7 @@ 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 ethUtil = require('ethereumjs-util'); import * as Web3 from 'web3'; @@ -21,13 +23,14 @@ const { Exchange, TokenTransferProxy, DummyToken, TokenRegistry, MaliciousToken // In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle // with type `any` to a variable of type `Web3`. const web3: Web3 = (global as any).web3; - -contract('Exchange', (accounts: string[]) => { - const maker = accounts[0]; - const tokenOwner = accounts[0]; - const taker = accounts[1] || accounts[accounts.length - 1]; - const feeRecipient = accounts[2] || accounts[accounts.length - 1]; - +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); + +describe('Exchange', () => { + const web3Wrapper = new Web3Wrapper(web3.currentProvider); + let maker: string; + let tokenOwner: string; + let taker: string; + let feeRecipient: string; const INITIAL_BALANCE = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); const INITIAL_ALLOWANCE = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); @@ -46,6 +49,11 @@ contract('Exchange', (accounts: string[]) => { let zeroEx: ZeroEx; before(async () => { + const accounts = await web3Wrapper.getAvailableAddressesAsync(); + maker = accounts[0]; + tokenOwner = accounts[0]; + taker = accounts[1] || accounts[accounts.length - 1]; + feeRecipient = accounts[2] || accounts[accounts.length - 1]; [tokenRegistry, exchange] = await Promise.all([TokenRegistry.deployed(), Exchange.deployed()]); exWrapper = new ExchangeWrapper(exchange); zeroEx = new ZeroEx(web3.currentProvider, { @@ -105,7 +113,12 @@ contract('Exchange', (accounts: string[]) => { zrx.setBalance(taker, INITIAL_BALANCE, { from: tokenOwner }), ]); }); - + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); describe('internal functions', () => { it('should include transferViaTokenTransferProxy', () => { expect(exchange.transferViaTokenTransferProxy).to.be.undefined(); diff --git a/packages/contracts/test/exchange/helpers.ts b/packages/contracts/test/exchange/helpers.ts index 830af44d1..f6e11f087 100644 --- a/packages/contracts/test/exchange/helpers.ts +++ b/packages/contracts/test/exchange/helpers.ts @@ -1,9 +1,13 @@ 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 ethUtil = require('ethereumjs-util'); +import * as Web3 from 'web3'; import { Artifacts } from '../../util/artifacts'; +import { constants } from '../../util/constants'; import { ExchangeWrapper } from '../../util/exchange_wrapper'; import { Order } from '../../util/order'; import { OrderFactory } from '../../util/order_factory'; @@ -13,16 +17,24 @@ chaiSetup.configure(); const expect = chai.expect; const { Exchange, TokenRegistry } = new Artifacts(artifacts); +// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle +// with type `any` to a variable of type `Web3`. +const web3: Web3 = (global as any).web3; +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); -contract('Exchange', (accounts: string[]) => { - const maker = accounts[0]; - const feeRecipient = accounts[1] || accounts[accounts.length - 1]; +describe('Exchange', () => { + const web3Wrapper = new Web3Wrapper(web3.currentProvider); + let maker: string; + let feeRecipient: string; let order: Order; let exchangeWrapper: ExchangeWrapper; let orderFactory: OrderFactory; before(async () => { + const accounts = await web3Wrapper.getAvailableAddressesAsync(); + maker = accounts[0]; + feeRecipient = accounts[1] || accounts[accounts.length - 1]; const [tokenRegistry, exchange] = await Promise.all([TokenRegistry.deployed(), Exchange.deployed()]); exchangeWrapper = new ExchangeWrapper(exchange); const [repAddress, dgdAddress] = await Promise.all([ @@ -41,12 +53,15 @@ contract('Exchange', (accounts: string[]) => { takerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), }; orderFactory = new OrderFactory(defaultOrderParams); + order = await orderFactory.newSignedOrderAsync(); }); beforeEach(async () => { - order = await orderFactory.newSignedOrderAsync(); + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); }); - describe('getOrderHash', () => { it('should output the correct orderHash', async () => { const orderHashHex = await exchangeWrapper.getOrderHashAsync(order); diff --git a/packages/contracts/test/exchange/wrapper.ts b/packages/contracts/test/exchange/wrapper.ts index 91807fc37..22deec01c 100644 --- a/packages/contracts/test/exchange/wrapper.ts +++ b/packages/contracts/test/exchange/wrapper.ts @@ -1,7 +1,10 @@ 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 _ from 'lodash'; +import * as Web3 from 'web3'; import { Artifacts } from '../../util/artifacts'; import { Balances } from '../../util/balances'; @@ -15,12 +18,17 @@ import { chaiSetup } from '../utils/chai_setup'; chaiSetup.configure(); const expect = chai.expect; const { Exchange, TokenTransferProxy, DummyToken, TokenRegistry } = new Artifacts(artifacts); - -contract('Exchange', (accounts: string[]) => { - const maker = accounts[0]; - const tokenOwner = accounts[0]; - const taker = accounts[1] || accounts[accounts.length - 1]; - const feeRecipient = accounts[2] || accounts[accounts.length - 1]; +// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle +// with type `any` to a variable of type `Web3`. +const web3: Web3 = (global as any).web3; +const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); + +describe('Exchange', () => { + const web3Wrapper = new Web3Wrapper(web3.currentProvider); + let maker: string; + let tokenOwner: string; + let taker: string; + let feeRecipient: string; const INIT_BAL = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); const INIT_ALLOW = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); @@ -38,6 +46,10 @@ contract('Exchange', (accounts: string[]) => { let orderFactory: OrderFactory; before(async () => { + const accounts = await web3Wrapper.getAvailableAddressesAsync(); + tokenOwner = maker = accounts[0]; + taker = accounts[1] || accounts[accounts.length - 1]; + feeRecipient = accounts[2] || accounts[accounts.length - 1]; [tokenRegistry, exchange] = await Promise.all([TokenRegistry.deployed(), Exchange.deployed()]); exWrapper = new ExchangeWrapper(exchange); const [repAddress, dgdAddress, zrxAddress] = await Promise.all([ @@ -80,7 +92,12 @@ contract('Exchange', (accounts: string[]) => { zrx.setBalance(taker, INIT_BAL, { from: tokenOwner }), ]); }); - + beforeEach(async () => { + await blockchainLifecycle.startAsync(); + }); + afterEach(async () => { + await blockchainLifecycle.revertAsync(); + }); describe('fillOrKillOrder', () => { beforeEach(async () => { balances = await dmyBalances.getAsync(); -- cgit v1.2.3 From 709026bf1a49d468850b4ebed845c8598fa4fd75 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 19 Jan 2018 15:34:28 +0100 Subject: Refactor contracts tests to not use injected web3 instance --- packages/contracts/test/ether_token.ts | 9 +++------ packages/contracts/test/exchange/core.ts | 10 +++------- packages/contracts/test/exchange/helpers.ts | 10 +++------- packages/contracts/test/exchange/wrapper.ts | 9 +++------ packages/contracts/test/multi_sig_with_time_lock.ts | 16 +++++++--------- ...ti_sig_with_time_lock_except_remove_auth_addr.ts | 9 +++------ packages/contracts/test/token_registry.ts | 9 +++------ .../contracts/test/token_transfer_proxy/auth.ts | 9 +++------ .../test/token_transfer_proxy/transfer_from.ts | 9 +++------ .../contracts/test/unlimited_allowance_token.ts | 9 +++------ .../contracts/test/unlimited_allowance_token_v2.ts | 7 +++---- packages/contracts/test/zrx_token.ts | 21 +++++++++------------ 12 files changed, 46 insertions(+), 81 deletions(-) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/ether_token.ts b/packages/contracts/test/ether_token.ts index 31c6c4ed6..eadeeaa57 100644 --- a/packages/contracts/test/ether_token.ts +++ b/packages/contracts/test/ether_token.ts @@ -1,9 +1,8 @@ import { ZeroEx, ZeroExError } from '0x.js'; -import { BlockchainLifecycle } from '@0xproject/dev-utils'; +import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; import { BigNumber, promisify } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; -import * as Web3 from 'web3'; import { Artifacts } from '../util/artifacts'; import { constants } from '../util/constants'; @@ -15,10 +14,8 @@ const { EtherToken } = new Artifacts(artifacts); chaiSetup.configure(); const expect = chai.expect; -// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle -// with type `any` to a variable of type `Web3`. -const web3: Web3 = (global as any).web3; -const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); +const web3 = web3Factory.create(); +const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('EtherToken', () => { const web3Wrapper = new Web3Wrapper(web3.currentProvider); diff --git a/packages/contracts/test/exchange/core.ts b/packages/contracts/test/exchange/core.ts index 9a6b4dc83..4f55bc398 100644 --- a/packages/contracts/test/exchange/core.ts +++ b/packages/contracts/test/exchange/core.ts @@ -1,10 +1,9 @@ import { ZeroEx } from '0x.js'; -import { BlockchainLifecycle } from '@0xproject/dev-utils'; +import { BlockchainLifecycle, devConstants, web3Factory } 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 Web3 from 'web3'; import { Artifacts } from '../../util/artifacts'; import { Balances } from '../../util/balances'; @@ -19,11 +18,8 @@ import { chaiSetup } from '../utils/chai_setup'; chaiSetup.configure(); const expect = chai.expect; const { Exchange, TokenTransferProxy, DummyToken, TokenRegistry, MaliciousToken } = new Artifacts(artifacts); - -// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle -// with type `any` to a variable of type `Web3`. -const web3: Web3 = (global as any).web3; -const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); +const web3 = web3Factory.create(); +const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('Exchange', () => { const web3Wrapper = new Web3Wrapper(web3.currentProvider); diff --git a/packages/contracts/test/exchange/helpers.ts b/packages/contracts/test/exchange/helpers.ts index f6e11f087..7af9866d5 100644 --- a/packages/contracts/test/exchange/helpers.ts +++ b/packages/contracts/test/exchange/helpers.ts @@ -1,13 +1,11 @@ import { ZeroEx } from '0x.js'; -import { BlockchainLifecycle } from '@0xproject/dev-utils'; +import { BlockchainLifecycle, devConstants, web3Factory } 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 Web3 from 'web3'; import { Artifacts } from '../../util/artifacts'; -import { constants } from '../../util/constants'; import { ExchangeWrapper } from '../../util/exchange_wrapper'; import { Order } from '../../util/order'; import { OrderFactory } from '../../util/order_factory'; @@ -17,10 +15,8 @@ chaiSetup.configure(); const expect = chai.expect; const { Exchange, TokenRegistry } = new Artifacts(artifacts); -// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle -// with type `any` to a variable of type `Web3`. -const web3: Web3 = (global as any).web3; -const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); +const web3 = web3Factory.create(); +const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('Exchange', () => { const web3Wrapper = new Web3Wrapper(web3.currentProvider); diff --git a/packages/contracts/test/exchange/wrapper.ts b/packages/contracts/test/exchange/wrapper.ts index 22deec01c..93f9bf876 100644 --- a/packages/contracts/test/exchange/wrapper.ts +++ b/packages/contracts/test/exchange/wrapper.ts @@ -1,10 +1,9 @@ import { ZeroEx } from '0x.js'; -import { BlockchainLifecycle } from '@0xproject/dev-utils'; +import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; import * as _ from 'lodash'; -import * as Web3 from 'web3'; import { Artifacts } from '../../util/artifacts'; import { Balances } from '../../util/balances'; @@ -18,10 +17,8 @@ import { chaiSetup } from '../utils/chai_setup'; chaiSetup.configure(); const expect = chai.expect; const { Exchange, TokenTransferProxy, DummyToken, TokenRegistry } = new Artifacts(artifacts); -// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle -// with type `any` to a variable of type `Web3`. -const web3: Web3 = (global as any).web3; -const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); +const web3 = web3Factory.create(); +const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('Exchange', () => { const web3Wrapper = new Web3Wrapper(web3.currentProvider); diff --git a/packages/contracts/test/multi_sig_with_time_lock.ts b/packages/contracts/test/multi_sig_with_time_lock.ts index 03914786c..6590dccaa 100644 --- a/packages/contracts/test/multi_sig_with_time_lock.ts +++ b/packages/contracts/test/multi_sig_with_time_lock.ts @@ -1,8 +1,7 @@ -import { BlockchainLifecycle, RPC } from '@0xproject/dev-utils'; -import { BigNumber, promisify } from '@0xproject/utils'; +import { BlockchainLifecycle, devConstants, RPC, web3Factory } 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 * as multiSigWalletJSON from '../../build/contracts/MultiSigWalletWithTimeLock.json'; import * as truffleConf from '../truffle.js'; @@ -19,10 +18,8 @@ const MULTI_SIG_ABI = (multiSigWalletJSON as any).abi; chaiSetup.configure(); const expect = chai.expect; -// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle -// with type `any` to a variable of type `Web3`. -const web3: Web3 = (global as any).web3; -const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); +const web3 = web3Factory.create(); +const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('MultiSigWalletWithTimeLock', () => { const web3Wrapper = new Web3Wrapper(web3.currentProvider); @@ -79,8 +76,9 @@ describe('MultiSigWalletWithTimeLock', () => { it('should set confirmation time with enough confirmations', async () => { const res = await multiSig.confirmTransaction(txId, { from: owners[1] }); expect(res.logs).to.have.length(2); - const blockNum = await promisify(web3.eth.getBlockNumber)(); - const blockInfo = await promisify(web3.eth.getBlock)(blockNum); + + const blockNum = await web3Wrapper.getBlockNumberAsync(); + const blockInfo = await web3Wrapper.getBlockAsync(blockNum); const timestamp = new BigNumber(blockInfo.timestamp); const confirmationTimeBigNum = new BigNumber(await multiSig.confirmationTimes.call(txId)); diff --git a/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts b/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts index f8e8c1f8d..b10e260a2 100644 --- a/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts +++ b/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts @@ -1,7 +1,6 @@ -import { BlockchainLifecycle } from '@0xproject/dev-utils'; +import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; -import * as Web3 from 'web3'; import * as tokenTransferProxyJSON from '../../build/contracts/TokenTransferProxy.json'; import { Artifacts } from '../util/artifacts'; @@ -16,10 +15,8 @@ const PROXY_ABI = (tokenTransferProxyJSON as any).abi; chaiSetup.configure(); const expect = chai.expect; -// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle -// with type `any` to a variable of type `Web3`. -const web3: Web3 = (global as any).web3; -const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); +const web3 = web3Factory.create(); +const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', () => { const web3Wrapper = new Web3Wrapper(web3.currentProvider); diff --git a/packages/contracts/test/token_registry.ts b/packages/contracts/test/token_registry.ts index bcb620e03..cc78feba3 100644 --- a/packages/contracts/test/token_registry.ts +++ b/packages/contracts/test/token_registry.ts @@ -1,10 +1,9 @@ import { ZeroEx } from '0x.js'; -import { BlockchainLifecycle } from '@0xproject/dev-utils'; +import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-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 { Artifacts } from '../util/artifacts'; import { constants } from '../util/constants'; @@ -16,10 +15,8 @@ import { chaiSetup } from './utils/chai_setup'; const { TokenRegistry } = new Artifacts(artifacts); chaiSetup.configure(); const expect = chai.expect; -// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle -// with type `any` to a variable of type `Web3`. -const web3: Web3 = (global as any).web3; -const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); +const web3 = web3Factory.create(); +const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('TokenRegistry', () => { const web3Wrapper = new Web3Wrapper(web3.currentProvider); diff --git a/packages/contracts/test/token_transfer_proxy/auth.ts b/packages/contracts/test/token_transfer_proxy/auth.ts index 399ad2080..65455ed9e 100644 --- a/packages/contracts/test/token_transfer_proxy/auth.ts +++ b/packages/contracts/test/token_transfer_proxy/auth.ts @@ -1,7 +1,6 @@ -import { BlockchainLifecycle } from '@0xproject/dev-utils'; +import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; -import * as Web3 from 'web3'; import { constants } from '../../util/constants'; import { ContractInstance } from '../../util/types'; @@ -10,10 +9,8 @@ import { chaiSetup } from '../utils/chai_setup'; chaiSetup.configure(); const expect = chai.expect; const TokenTransferProxy = artifacts.require('./db/TokenTransferProxy.sol'); -// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle -// with type `any` to a variable of type `Web3`. -const web3: Web3 = (global as any).web3; -const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); +const web3 = web3Factory.create(); +const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('TokenTransferProxy', () => { const web3Wrapper = new Web3Wrapper(web3.currentProvider); diff --git a/packages/contracts/test/token_transfer_proxy/transfer_from.ts b/packages/contracts/test/token_transfer_proxy/transfer_from.ts index 1a5151309..d1245d3b2 100644 --- a/packages/contracts/test/token_transfer_proxy/transfer_from.ts +++ b/packages/contracts/test/token_transfer_proxy/transfer_from.ts @@ -1,7 +1,6 @@ -import { BlockchainLifecycle } from '@0xproject/dev-utils'; +import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; -import * as Web3 from 'web3'; import { Artifacts } from '../../util/artifacts'; import { Balances } from '../../util/balances'; @@ -12,10 +11,8 @@ import { chaiSetup } from '../utils/chai_setup'; chaiSetup.configure(); const expect = chai.expect; const { TokenTransferProxy, DummyToken, TokenRegistry } = new Artifacts(artifacts); -// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle -// with type `any` to a variable of type `Web3`. -const web3: Web3 = (global as any).web3; -const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); +const web3 = web3Factory.create(); +const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('TokenTransferProxy', () => { const web3Wrapper = new Web3Wrapper(web3.currentProvider); diff --git a/packages/contracts/test/unlimited_allowance_token.ts b/packages/contracts/test/unlimited_allowance_token.ts index ca8ce4c50..36f88c3d2 100644 --- a/packages/contracts/test/unlimited_allowance_token.ts +++ b/packages/contracts/test/unlimited_allowance_token.ts @@ -1,9 +1,8 @@ import { ZeroEx } from '0x.js'; -import { BlockchainLifecycle } from '@0xproject/dev-utils'; +import { BlockchainLifecycle, devConstants, web3Factory } 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 { Artifacts } from '../util/artifacts'; import { constants } from '../util/constants'; @@ -12,12 +11,10 @@ import { ContractInstance } from '../util/types'; import { chaiSetup } from './utils/chai_setup'; const { DummyToken } = new Artifacts(artifacts); -// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle -// with type `any` to a variable of type `Web3`. -const web3: Web3 = (global as any).web3; +const web3 = web3Factory.create(); chaiSetup.configure(); const expect = chai.expect; -const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); +const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('UnlimitedAllowanceToken', () => { const web3Wrapper = new Web3Wrapper(web3.currentProvider); diff --git a/packages/contracts/test/unlimited_allowance_token_v2.ts b/packages/contracts/test/unlimited_allowance_token_v2.ts index ebdc3ba11..3c4a8e941 100644 --- a/packages/contracts/test/unlimited_allowance_token_v2.ts +++ b/packages/contracts/test/unlimited_allowance_token_v2.ts @@ -1,9 +1,8 @@ import { ZeroEx } from '0x.js'; -import { BlockchainLifecycle } from '@0xproject/dev-utils'; +import { BlockchainLifecycle, devConstants, web3Factory } 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 { Artifacts } from '../util/artifacts'; import { constants } from '../util/constants'; @@ -14,8 +13,8 @@ import { chaiSetup } from './utils/chai_setup'; const { DummyTokenV2 } = new Artifacts(artifacts); chaiSetup.configure(); const expect = chai.expect; -const web3: Web3 = (global as any).web3; -const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); +const web3 = web3Factory.create(); +const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('UnlimitedAllowanceTokenV2', () => { const web3Wrapper = new Web3Wrapper(web3.currentProvider); diff --git a/packages/contracts/test/zrx_token.ts b/packages/contracts/test/zrx_token.ts index fe251f53d..573fb8d5a 100644 --- a/packages/contracts/test/zrx_token.ts +++ b/packages/contracts/test/zrx_token.ts @@ -1,9 +1,8 @@ import { ZeroEx } from '0x.js'; -import { BlockchainLifecycle } from '@0xproject/dev-utils'; +import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; -import Web3 = require('web3'); import { Artifacts } from '../util/artifacts'; import { constants } from '../util/constants'; @@ -14,10 +13,8 @@ import { chaiSetup } from './utils/chai_setup'; chaiSetup.configure(); const expect = chai.expect; const { Exchange, ZRXToken } = new Artifacts(artifacts); -// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle -// with type `any` to a variable of type `Web3`. -const web3: Web3 = (global as any).web3; -const blockchainLifecycle = new BlockchainLifecycle(constants.RPC_URL); +const web3 = web3Factory.create(); +const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('ZRXToken', () => { const web3Wrapper = new Web3Wrapper(web3.currentProvider); @@ -140,10 +137,10 @@ describe('ZRXToken', () => { const initOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); const amountToTransfer = initOwnerBalance; const initSpenderAllowance = MAX_UINT; - let txHash = await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, initSpenderAllowance, { + await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, initSpenderAllowance, { gasLimit: constants.MAX_TOKEN_APPROVE_GAS, }); - txHash = await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, { + await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, { gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, }); @@ -156,8 +153,8 @@ describe('ZRXToken', () => { const initSpenderBalance = await zeroEx.token.getBalanceAsync(zrxAddress, spender); const amountToTransfer = initOwnerBalance; const initSpenderAllowance = initOwnerBalance; - let txHash = await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, initSpenderAllowance); - txHash = await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, { + await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, initSpenderAllowance); + await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, { gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, }); @@ -171,8 +168,8 @@ describe('ZRXToken', () => { it('should modify allowance if spender has sufficient allowance less than 2^256 - 1', async () => { const initOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); const amountToTransfer = initOwnerBalance; - let txHash = await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, amountToTransfer); - txHash = await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, { + await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, amountToTransfer); + await zeroEx.token.transferFromAsync(zrxAddress, owner, spender, spender, amountToTransfer, { gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, }); -- cgit v1.2.3 From 387363283ca03ac1d6c9be5b7be2107790bbf79d Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Mon, 22 Jan 2018 21:53:32 +0100 Subject: Remove truffle from tests --- packages/contracts/test/ether_token.ts | 11 +- packages/contracts/test/exchange/core.ts | 123 ++++++------ packages/contracts/test/exchange/helpers.ts | 29 +-- packages/contracts/test/exchange/wrapper.ts | 60 +++--- .../contracts/test/multi_sig_with_time_lock.ts | 213 +++++++++++++-------- ...i_sig_with_time_lock_except_remove_auth_addr.ts | 88 +++++---- packages/contracts/test/token_registry.ts | 16 +- .../contracts/test/token_transfer_proxy/auth.ts | 20 +- .../test/token_transfer_proxy/transfer_from.ts | 24 +-- .../contracts/test/unlimited_allowance_token.ts | 11 +- .../contracts/test/unlimited_allowance_token_v2.ts | 11 +- packages/contracts/test/utils/deployer.ts | 16 ++ packages/contracts/test/zrx_token.ts | 28 ++- 13 files changed, 374 insertions(+), 276 deletions(-) create mode 100644 packages/contracts/test/utils/deployer.ts (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/ether_token.ts b/packages/contracts/test/ether_token.ts index eadeeaa57..d4e478099 100644 --- a/packages/contracts/test/ether_token.ts +++ b/packages/contracts/test/ether_token.ts @@ -4,21 +4,18 @@ import { BigNumber, promisify } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; -import { Artifacts } from '../util/artifacts'; import { constants } from '../util/constants'; import { chaiSetup } from './utils/chai_setup'; - -const { EtherToken } = new Artifacts(artifacts); +import { deployer } from './utils/deployer'; chaiSetup.configure(); const expect = chai.expect; - const web3 = web3Factory.create(); +const web3Wrapper = new Web3Wrapper(web3.currentProvider); const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('EtherToken', () => { - const web3Wrapper = new Web3Wrapper(web3.currentProvider); let account: string; const gasPrice = ZeroEx.toBaseUnitAmount(new BigNumber(20), 9); let zeroEx: ZeroEx; @@ -32,7 +29,9 @@ describe('EtherToken', () => { before(async () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); account = accounts[0]; - etherTokenAddress = EtherToken.address; + + const etherToken = await deployer.deployAsync('WETH9'); + etherTokenAddress = etherToken.address; zeroEx = new ZeroEx(web3.currentProvider, { gasPrice, networkId: constants.TESTRPC_NETWORK_ID, diff --git a/packages/contracts/test/exchange/core.ts b/packages/contracts/test/exchange/core.ts index 4f55bc398..5fe2a9452 100644 --- a/packages/contracts/test/exchange/core.ts +++ b/packages/contracts/test/exchange/core.ts @@ -1,28 +1,35 @@ -import { ZeroEx } from '0x.js'; +import { + LogCancelContractEventArgs, + LogErrorContractEventArgs, + LogFillContractEventArgs, + LogWithDecodedArgs, + TransactionReceiptWithDecodedLogs, + ZeroEx, +} from '0x.js'; import { BlockchainLifecycle, devConstants, web3Factory } 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 Web3 from 'web3'; -import { Artifacts } from '../../util/artifacts'; import { Balances } from '../../util/balances'; import { constants } from '../../util/constants'; import { crypto } from '../../util/crypto'; import { ExchangeWrapper } from '../../util/exchange_wrapper'; import { Order } from '../../util/order'; import { OrderFactory } from '../../util/order_factory'; -import { BalancesByOwner, ContractInstance, ExchangeContractErrs } from '../../util/types'; +import { BalancesByOwner, ExchangeContractErrs } from '../../util/types'; import { chaiSetup } from '../utils/chai_setup'; +import { deployer } from '../utils/deployer'; chaiSetup.configure(); const expect = chai.expect; -const { Exchange, TokenTransferProxy, DummyToken, TokenRegistry, MaliciousToken } = new Artifacts(artifacts); const web3 = web3Factory.create(); +const web3Wrapper = new Web3Wrapper(web3.currentProvider); const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('Exchange', () => { - const web3Wrapper = new Web3Wrapper(web3.currentProvider); let maker: string; let tokenOwner: string; let taker: string; @@ -30,11 +37,11 @@ describe('Exchange', () => { const INITIAL_BALANCE = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); const INITIAL_ALLOWANCE = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); - let rep: ContractInstance; - let dgd: ContractInstance; - let zrx: ContractInstance; - let exchange: ContractInstance; - let tokenRegistry: ContractInstance; + let rep: Web3.ContractInstance; + let dgd: Web3.ContractInstance; + let zrx: Web3.ContractInstance; + let exchange: Web3.ContractInstance; + let tokenTransferProxy: Web3.ContractInstance; let order: Order; let balances: BalancesByOwner; @@ -50,59 +57,54 @@ describe('Exchange', () => { tokenOwner = accounts[0]; taker = accounts[1] || accounts[accounts.length - 1]; feeRecipient = accounts[2] || accounts[accounts.length - 1]; - [tokenRegistry, exchange] = await Promise.all([TokenRegistry.deployed(), Exchange.deployed()]); - exWrapper = new ExchangeWrapper(exchange); + [rep, dgd, zrx] = await Promise.all([ + deployer.deployAsync('DummyToken'), + deployer.deployAsync('DummyToken'), + deployer.deployAsync('DummyToken'), + ]); + tokenTransferProxy = await deployer.deployAsync('TokenTransferProxy'); + exchange = await deployer.deployAsync('Exchange', [zrx.address, tokenTransferProxy.address]); + await tokenTransferProxy.addAuthorizedAddress(exchange.address, { from: accounts[0] }); zeroEx = new ZeroEx(web3.currentProvider, { exchangeContractAddress: exchange.address, networkId: constants.TESTRPC_NETWORK_ID, }); - - const [repAddress, dgdAddress, zrxAddress] = await Promise.all([ - tokenRegistry.getTokenAddressBySymbol('REP'), - tokenRegistry.getTokenAddressBySymbol('DGD'), - tokenRegistry.getTokenAddressBySymbol('ZRX'), - ]); + exWrapper = new ExchangeWrapper(exchange, zeroEx); const defaultOrderParams = { - exchangeContractAddress: Exchange.address, + exchangeContractAddress: exchange.address, maker, feeRecipient, - makerToken: repAddress, - takerToken: dgdAddress, + makerToken: rep.address, + takerToken: dgd.address, makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), makerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), takerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), }; - orderFactory = new OrderFactory(defaultOrderParams); - - [rep, dgd, zrx] = await Promise.all([ - DummyToken.at(repAddress), - DummyToken.at(dgdAddress), - DummyToken.at(zrxAddress), - ]); + orderFactory = new OrderFactory(web3Wrapper, defaultOrderParams); dmyBalances = new Balances([rep, dgd, zrx], [maker, taker, feeRecipient]); await Promise.all([ - rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + rep.approve(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: maker, }), - rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + rep.approve(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: taker, }), rep.setBalance(maker, INITIAL_BALANCE, { from: tokenOwner }), rep.setBalance(taker, INITIAL_BALANCE, { from: tokenOwner }), - dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + dgd.approve(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: maker, }), - dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + dgd.approve(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: taker, }), dgd.setBalance(maker, INITIAL_BALANCE, { from: tokenOwner }), dgd.setBalance(taker, INITIAL_BALANCE, { from: tokenOwner }), - zrx.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + zrx.approve(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: maker, }), - zrx.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + zrx.approve(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: taker, }), zrx.setBalance(maker, INITIAL_BALANCE, { from: tokenOwner }), @@ -392,8 +394,8 @@ describe('Exchange', () => { const res = await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount: order.params.takerTokenAmount, }); - - expect(res.logs[0].args.filledTakerTokenAmount).to.be.bignumber.equal( + const log = res.logs[0] as LogWithDecodedArgs; + expect(log.args.filledTakerTokenAmount).to.be.bignumber.equal( order.params.takerTokenAmount.minus(fillTakerTokenAmount), ); const newBalances = await dmyBalances.getAsync(); @@ -428,7 +430,7 @@ describe('Exchange', () => { }); expect(res.logs).to.have.length(1); - const logArgs = res.logs[0].args; + const logArgs = (res.logs[0] as LogWithDecodedArgs).args; const expectedFilledMakerTokenAmount = order.params.makerTokenAmount.div(divisor); const expectedFilledTakerTokenAmount = order.params.takerTokenAmount.div(divisor); const expectedFeeMPaid = order.params.makerFee.div(divisor); @@ -459,7 +461,7 @@ describe('Exchange', () => { }); expect(res.logs).to.have.length(1); - const logArgs = res.logs[0].args; + const logArgs = (res.logs[0] as LogWithDecodedArgs).args; const expectedFilledMakerTokenAmount = order.params.makerTokenAmount.div(divisor); const expectedFilledTakerTokenAmount = order.params.takerTokenAmount.div(divisor); const expectedFeeMPaid = new BigNumber(0); @@ -576,9 +578,9 @@ describe('Exchange', () => { it('should not change balances if maker allowances are too low to fill order and \ shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - await rep.approve(TokenTransferProxy.address, 0, { from: maker }); + await rep.approve(tokenTransferProxy.address, 0, { from: maker }); await exWrapper.fillOrderAsync(order, taker); - await rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + await rep.approve(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: maker, }); @@ -588,22 +590,22 @@ describe('Exchange', () => { it('should throw if maker allowances are too low to fill order and \ shouldThrowOnInsufficientBalanceOrAllowance = true', async () => { - await rep.approve(TokenTransferProxy.address, 0, { from: maker }); + await rep.approve(tokenTransferProxy.address, 0, { from: maker }); expect( exWrapper.fillOrderAsync(order, taker, { shouldThrowOnInsufficientBalanceOrAllowance: true, }), ).to.be.rejectedWith(constants.REVERT); - await rep.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + await rep.approve(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: maker, }); }); it('should not change balances if taker allowances are too low to fill order and \ shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - await dgd.approve(TokenTransferProxy.address, 0, { from: taker }); + await dgd.approve(tokenTransferProxy.address, 0, { from: taker }); await exWrapper.fillOrderAsync(order, taker); - await dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + await dgd.approve(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: taker, }); @@ -613,13 +615,13 @@ describe('Exchange', () => { it('should throw if taker allowances are too low to fill order and \ shouldThrowOnInsufficientBalanceOrAllowance = true', async () => { - await dgd.approve(TokenTransferProxy.address, 0, { from: taker }); + await dgd.approve(tokenTransferProxy.address, 0, { from: taker }); expect( exWrapper.fillOrderAsync(order, taker, { shouldThrowOnInsufficientBalanceOrAllowance: true, }), ).to.be.rejectedWith(constants.REVERT); - await dgd.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { + await dgd.approve(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: taker, }); }); @@ -639,7 +641,7 @@ describe('Exchange', () => { it('should not change balances if makerToken is ZRX, makerTokenAmount + makerFee > maker allowance, \ and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - const makerZRXAllowance = await zrx.allowance(maker, TokenTransferProxy.address); + const makerZRXAllowance = await zrx.allowance(maker, tokenTransferProxy.address); order = await orderFactory.newSignedOrderAsync({ makerToken: zrx.address, makerTokenAmount: new BigNumber(makerZRXAllowance), @@ -665,7 +667,7 @@ describe('Exchange', () => { it('should not change balances if takerToken is ZRX, takerTokenAmount + takerFee > taker allowance, \ and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - const takerZRXAllowance = await zrx.allowance(taker, TokenTransferProxy.address); + const takerZRXAllowance = await zrx.allowance(taker, tokenTransferProxy.address); order = await orderFactory.newSignedOrderAsync({ takerToken: zrx.address, takerTokenAmount: new BigNumber(takerZRXAllowance), @@ -676,10 +678,10 @@ describe('Exchange', () => { expect(newBalances).to.be.deep.equal(balances); }); - it('should throw if getBalance or getAllowance attempts to change state and \ + it.skip('should throw if getBalance or getAllowance attempts to change state and \ shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - const maliciousToken = await MaliciousToken.new(); - await maliciousToken.approve(TokenTransferProxy.address, INITIAL_ALLOWANCE, { from: taker }); + const maliciousToken = await deployer.deployAsync('MaliciousToken'); + await maliciousToken.approve(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: taker }); order = await orderFactory.newSignedOrderAsync({ takerToken: maliciousToken.address, @@ -709,16 +711,19 @@ describe('Exchange', () => { const res = await exWrapper.fillOrderAsync(order, taker); expect(res.logs).to.have.length(1); - const errCode = res.logs[0].args.errorId.toNumber(); + const log = res.logs[0] as LogWithDecodedArgs; + const errCode = log.args.errorId.toNumber(); expect(errCode).to.be.equal(ExchangeContractErrs.ERROR_ORDER_EXPIRED); }); it('should log an error event if no value is filled', async () => { + order = await orderFactory.newSignedOrderAsync({}); await exWrapper.fillOrderAsync(order, taker); const res = await exWrapper.fillOrderAsync(order, taker); expect(res.logs).to.have.length(1); - const errCode = res.logs[0].args.errorId.toNumber(); + const log = res.logs[0] as LogWithDecodedArgs; + const errCode = log.args.errorId.toNumber(); expect(errCode).to.be.equal(ExchangeContractErrs.ERROR_ORDER_FULLY_FILLED_OR_CANCELLED); }); }); @@ -778,7 +783,8 @@ describe('Exchange', () => { const res = await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount: order.params.takerTokenAmount, }); - expect(res.logs[0].args.filledTakerTokenAmount).to.be.bignumber.equal( + const log = res.logs[0] as LogWithDecodedArgs; + expect(log.args.filledTakerTokenAmount).to.be.bignumber.equal( order.params.takerTokenAmount.minus(cancelTakerTokenAmount), ); @@ -822,7 +828,8 @@ describe('Exchange', () => { }); expect(res.logs).to.have.length(1); - const logArgs = res.logs[0].args; + const log = res.logs[0] as LogWithDecodedArgs; + const logArgs = log.args; const expectedCancelledMakerTokenAmount = order.params.makerTokenAmount.div(divisor); const expectedCancelledTakerTokenAmount = order.params.takerTokenAmount.div(divisor); const tokensHashBuff = crypto.solSHA3([order.params.makerToken, order.params.takerToken]); @@ -843,7 +850,8 @@ describe('Exchange', () => { const res = await exWrapper.cancelOrderAsync(order, maker); expect(res.logs).to.have.length(1); - const errCode = res.logs[0].args.errorId.toNumber(); + const log = res.logs[0] as LogWithDecodedArgs; + const errCode = log.args.errorId.toNumber(); expect(errCode).to.be.equal(ExchangeContractErrs.ERROR_ORDER_FULLY_FILLED_OR_CANCELLED); }); @@ -854,7 +862,8 @@ describe('Exchange', () => { const res = await exWrapper.cancelOrderAsync(order, maker); expect(res.logs).to.have.length(1); - const errCode = res.logs[0].args.errorId.toNumber(); + const log = res.logs[0] as LogWithDecodedArgs; + const errCode = log.args.errorId.toNumber(); expect(errCode).to.be.equal(ExchangeContractErrs.ERROR_ORDER_EXPIRED); }); }); diff --git a/packages/contracts/test/exchange/helpers.ts b/packages/contracts/test/exchange/helpers.ts index 7af9866d5..965edeba8 100644 --- a/packages/contracts/test/exchange/helpers.ts +++ b/packages/contracts/test/exchange/helpers.ts @@ -5,21 +5,21 @@ import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; import ethUtil = require('ethereumjs-util'); -import { Artifacts } from '../../util/artifacts'; +import { constants } from '../../util/constants'; import { ExchangeWrapper } from '../../util/exchange_wrapper'; import { Order } from '../../util/order'; import { OrderFactory } from '../../util/order_factory'; import { chaiSetup } from '../utils/chai_setup'; +import { deployer } from '../utils/deployer'; chaiSetup.configure(); const expect = chai.expect; -const { Exchange, TokenRegistry } = new Artifacts(artifacts); const web3 = web3Factory.create(); +const web3Wrapper = new Web3Wrapper(web3.currentProvider); const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('Exchange', () => { - const web3Wrapper = new Web3Wrapper(web3.currentProvider); let maker: string; let feeRecipient: string; @@ -31,24 +31,29 @@ describe('Exchange', () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); maker = accounts[0]; feeRecipient = accounts[1] || accounts[accounts.length - 1]; - const [tokenRegistry, exchange] = await Promise.all([TokenRegistry.deployed(), Exchange.deployed()]); - exchangeWrapper = new ExchangeWrapper(exchange); - const [repAddress, dgdAddress] = await Promise.all([ - tokenRegistry.getTokenAddressBySymbol('REP'), - tokenRegistry.getTokenAddressBySymbol('DGD'), + const tokenRegistry = await deployer.deployAsync('TokenRegistry'); + const tokenTransferProxy = await deployer.deployAsync('TokenTransferProxy'); + const [rep, dgd, zrx] = await Promise.all([ + deployer.deployAsync('DummyToken'), + deployer.deployAsync('DummyToken'), + deployer.deployAsync('DummyToken'), ]); + const exchange = await deployer.deployAsync('Exchange', [zrx.address, tokenTransferProxy.address]); + await tokenTransferProxy.addAuthorizedAddress(exchange.address, { from: accounts[0] }); + const zeroEx = new ZeroEx(web3.currentProvider, { networkId: constants.TESTRPC_NETWORK_ID }); + exchangeWrapper = new ExchangeWrapper(exchange, zeroEx); const defaultOrderParams = { - exchangeContractAddress: Exchange.address, + exchangeContractAddress: exchange.address, maker, feeRecipient, - makerToken: repAddress, - takerToken: dgdAddress, + makerToken: rep.address, + takerToken: dgd.address, makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), makerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), takerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), }; - orderFactory = new OrderFactory(defaultOrderParams); + orderFactory = new OrderFactory(web3Wrapper, defaultOrderParams); order = await orderFactory.newSignedOrderAsync(); }); diff --git a/packages/contracts/test/exchange/wrapper.ts b/packages/contracts/test/exchange/wrapper.ts index 93f9bf876..2253f4cb7 100644 --- a/packages/contracts/test/exchange/wrapper.ts +++ b/packages/contracts/test/exchange/wrapper.ts @@ -4,24 +4,24 @@ import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; import * as _ from 'lodash'; +import * as Web3 from 'web3'; -import { Artifacts } from '../../util/artifacts'; import { Balances } from '../../util/balances'; import { constants } from '../../util/constants'; import { ExchangeWrapper } from '../../util/exchange_wrapper'; import { Order } from '../../util/order'; import { OrderFactory } from '../../util/order_factory'; -import { BalancesByOwner, ContractInstance } from '../../util/types'; +import { BalancesByOwner } from '../../util/types'; import { chaiSetup } from '../utils/chai_setup'; +import { deployer } from '../utils/deployer'; chaiSetup.configure(); const expect = chai.expect; -const { Exchange, TokenTransferProxy, DummyToken, TokenRegistry } = new Artifacts(artifacts); const web3 = web3Factory.create(); +const web3Wrapper = new Web3Wrapper(web3.currentProvider); const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('Exchange', () => { - const web3Wrapper = new Web3Wrapper(web3.currentProvider); let maker: string; let tokenOwner: string; let taker: string; @@ -30,11 +30,12 @@ describe('Exchange', () => { const INIT_BAL = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); const INIT_ALLOW = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); - let rep: ContractInstance; - let dgd: ContractInstance; - let zrx: ContractInstance; - let exchange: ContractInstance; - let tokenRegistry: ContractInstance; + let rep: Web3.ContractInstance; + let dgd: Web3.ContractInstance; + let zrx: Web3.ContractInstance; + let exchange: Web3.ContractInstance; + let tokenRegistry: Web3.ContractInstance; + let tokenTransferProxy: Web3.ContractInstance; let balances: BalancesByOwner; @@ -47,44 +48,43 @@ describe('Exchange', () => { tokenOwner = maker = accounts[0]; taker = accounts[1] || accounts[accounts.length - 1]; feeRecipient = accounts[2] || accounts[accounts.length - 1]; - [tokenRegistry, exchange] = await Promise.all([TokenRegistry.deployed(), Exchange.deployed()]); - exWrapper = new ExchangeWrapper(exchange); - const [repAddress, dgdAddress, zrxAddress] = await Promise.all([ - tokenRegistry.getTokenAddressBySymbol('REP'), - tokenRegistry.getTokenAddressBySymbol('DGD'), - tokenRegistry.getTokenAddressBySymbol('ZRX'), + [rep, dgd, zrx] = await Promise.all([ + deployer.deployAsync('DummyToken'), + deployer.deployAsync('DummyToken'), + deployer.deployAsync('DummyToken'), ]); + tokenRegistry = await deployer.deployAsync('TokenRegistry'); + tokenTransferProxy = await deployer.deployAsync('TokenTransferProxy'); + exchange = await deployer.deployAsync('Exchange', [zrx.address, tokenTransferProxy.address]); + await tokenTransferProxy.addAuthorizedAddress(exchange.address, { from: accounts[0] }); + const zeroEx = new ZeroEx(web3.currentProvider, { networkId: constants.TESTRPC_NETWORK_ID }); + exWrapper = new ExchangeWrapper(exchange, zeroEx); const defaultOrderParams = { - exchangeContractAddress: Exchange.address, + exchangeContractAddress: exchange.address, maker, feeRecipient, - makerToken: repAddress, - takerToken: dgdAddress, + makerToken: rep.address, + takerToken: dgd.address, makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), makerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), takerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18), }; - orderFactory = new OrderFactory(defaultOrderParams); - [rep, dgd, zrx] = await Promise.all([ - DummyToken.at(repAddress), - DummyToken.at(dgdAddress), - DummyToken.at(zrxAddress), - ]); + orderFactory = new OrderFactory(web3Wrapper, defaultOrderParams); dmyBalances = new Balances([rep, dgd, zrx], [maker, taker, feeRecipient]); await Promise.all([ - rep.approve(TokenTransferProxy.address, INIT_ALLOW, { from: maker }), - rep.approve(TokenTransferProxy.address, INIT_ALLOW, { from: taker }), + rep.approve(tokenTransferProxy.address, INIT_ALLOW, { from: maker }), + rep.approve(tokenTransferProxy.address, INIT_ALLOW, { from: taker }), rep.setBalance(maker, INIT_BAL, { from: tokenOwner }), rep.setBalance(taker, INIT_BAL, { from: tokenOwner }), - dgd.approve(TokenTransferProxy.address, INIT_ALLOW, { from: maker }), - dgd.approve(TokenTransferProxy.address, INIT_ALLOW, { from: taker }), + dgd.approve(tokenTransferProxy.address, INIT_ALLOW, { from: maker }), + dgd.approve(tokenTransferProxy.address, INIT_ALLOW, { from: taker }), dgd.setBalance(maker, INIT_BAL, { from: tokenOwner }), dgd.setBalance(taker, INIT_BAL, { from: tokenOwner }), - zrx.approve(TokenTransferProxy.address, INIT_ALLOW, { from: maker }), - zrx.approve(TokenTransferProxy.address, INIT_ALLOW, { from: taker }), + zrx.approve(tokenTransferProxy.address, INIT_ALLOW, { from: maker }), + zrx.approve(tokenTransferProxy.address, INIT_ALLOW, { from: taker }), zrx.setBalance(maker, INIT_BAL, { from: tokenOwner }), zrx.setBalance(taker, INIT_BAL, { from: tokenOwner }), ]); diff --git a/packages/contracts/test/multi_sig_with_time_lock.ts b/packages/contracts/test/multi_sig_with_time_lock.ts index 6590dccaa..ff8a01d14 100644 --- a/packages/contracts/test/multi_sig_with_time_lock.ts +++ b/packages/contracts/test/multi_sig_with_time_lock.ts @@ -1,49 +1,46 @@ +import { LogWithDecodedArgs, ZeroEx } from '0x.js'; import { BlockchainLifecycle, devConstants, RPC, web3Factory } from '@0xproject/dev-utils'; -import { BigNumber } from '@0xproject/utils'; +import { AbiDecoder, BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; +import * as Web3 from 'web3'; import * as multiSigWalletJSON from '../../build/contracts/MultiSigWalletWithTimeLock.json'; -import * as truffleConf from '../truffle.js'; -import { Artifacts } from '../util/artifacts'; +import { artifacts } from '../util/artifacts'; import { constants } from '../util/constants'; import { MultiSigWrapper } from '../util/multi_sig_wrapper'; -import { ContractInstance } from '../util/types'; +import { SubmissionContractEventArgs } from '../util/types'; import { chaiSetup } from './utils/chai_setup'; +import { deployer } from './utils/deployer'; -const { MultiSigWalletWithTimeLock } = new Artifacts(artifacts); - -const MULTI_SIG_ABI = (multiSigWalletJSON as any).abi; +const MULTI_SIG_ABI = artifacts.MultiSigWalletWithTimeLockArtifact.networks[constants.TESTRPC_NETWORK_ID].abi; chaiSetup.configure(); const expect = chai.expect; const web3 = web3Factory.create(); +const web3Wrapper = new Web3Wrapper(web3.currentProvider); const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); +const zeroEx = new ZeroEx(web3.currentProvider, { networkId: constants.TESTRPC_NETWORK_ID }); +const abiDecoder = new AbiDecoder([MULTI_SIG_ABI]); describe('MultiSigWalletWithTimeLock', () => { - const web3Wrapper = new Web3Wrapper(web3.currentProvider); let owners: string[]; before(async () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); owners = [accounts[0], accounts[1]]; }); + const SIGNATURES_REQUIRED = 2; const SECONDS_TIME_LOCKED = 10000; - let multiSig: ContractInstance; + let multiSig: Web3.ContractInstance; let multiSigWrapper: MultiSigWrapper; let txId: number; let initialSecondsTimeLocked: number; let rpc: RPC; before(async () => { - multiSig = await MultiSigWalletWithTimeLock.deployed(); - multiSigWrapper = new MultiSigWrapper(multiSig); - - const secondsTimeLocked = await multiSig.secondsTimeLocked.call(); - initialSecondsTimeLocked = secondsTimeLocked.toNumber(); - const rpcUrl = `http://${truffleConf.networks.development.host}:${truffleConf.networks.development.port}`; - rpc = new RPC(rpcUrl); + rpc = new RPC(devConstants.RPC_URL); }); beforeEach(async () => { await blockchainLifecycle.startAsync(); @@ -53,74 +50,138 @@ describe('MultiSigWalletWithTimeLock', () => { }); describe('changeTimeLock', () => { - it('should throw when not called by wallet', async () => { - return expect(multiSig.changeTimeLock(SECONDS_TIME_LOCKED, { from: owners[0] })).to.be.rejectedWith( - constants.REVERT, - ); - }); - - it('should throw without enough confirmations', async () => { - const destination = multiSig.address; - const from = owners[0]; - const dataParams = { - name: 'changeTimeLock', - abi: MULTI_SIG_ABI, - args: [SECONDS_TIME_LOCKED], - }; - const subRes = await multiSigWrapper.submitTransactionAsync(destination, from, dataParams); - - txId = subRes.logs[0].args.transactionId.toNumber(); - return expect(multiSig.executeTransaction(txId)).to.be.rejectedWith(constants.REVERT); - }); - - it('should set confirmation time with enough confirmations', async () => { - const res = await multiSig.confirmTransaction(txId, { from: owners[1] }); - expect(res.logs).to.have.length(2); - - const blockNum = await web3Wrapper.getBlockNumberAsync(); - const blockInfo = await web3Wrapper.getBlockAsync(blockNum); - const timestamp = new BigNumber(blockInfo.timestamp); - const confirmationTimeBigNum = new BigNumber(await multiSig.confirmationTimes.call(txId)); - - expect(timestamp).to.be.bignumber.equal(confirmationTimeBigNum); - }); + describe('initially non-time-locked', async () => { + before('deploy a walet', async () => { + multiSig = await deployer.deployAsync('MultiSigWalletWithTimeLock', [owners, SIGNATURES_REQUIRED, 0]); + multiSigWrapper = new MultiSigWrapper(multiSig); - it('should be executable with enough confirmations and secondsTimeLocked of 0', async () => { - expect(initialSecondsTimeLocked).to.be.equal(0); - - const res = await multiSig.executeTransaction(txId); - expect(res.logs).to.have.length(2); + const secondsTimeLocked = await multiSig.secondsTimeLocked(); + initialSecondsTimeLocked = secondsTimeLocked.toNumber(); + }); + it('should throw when not called by wallet', async () => { + return expect(multiSig.changeTimeLock(SECONDS_TIME_LOCKED, { from: owners[0] })).to.be.rejectedWith( + constants.REVERT, + ); + }); - const secondsTimeLocked = new BigNumber(await multiSig.secondsTimeLocked.call()); - expect(secondsTimeLocked).to.be.bignumber.equal(SECONDS_TIME_LOCKED); - }); + it('should throw without enough confirmations', async () => { + const destination = multiSig.address; + const from = owners[0]; + const dataParams = { + name: 'changeTimeLock', + abi: MULTI_SIG_ABI, + args: [SECONDS_TIME_LOCKED], + }; + const txHash = await multiSigWrapper.submitTransactionAsync(destination, from, dataParams); + const subRes = await zeroEx.awaitTransactionMinedAsync(txHash); + const log = abiDecoder.tryToDecodeLogOrNoop(subRes.logs[0]) as LogWithDecodedArgs< + SubmissionContractEventArgs + >; + + txId = log.args.transactionId.toNumber(); + return expect(multiSig.executeTransaction(txId, { from: owners[0] })).to.be.rejectedWith( + constants.REVERT, + ); + }); - const newSecondsTimeLocked = 0; - it('should throw if it has enough confirmations but is not past the time lock', async () => { - const destination = multiSig.address; - const from = owners[0]; - const dataParams = { - name: 'changeTimeLock', - abi: MULTI_SIG_ABI, - args: [newSecondsTimeLocked], - }; - const subRes = await multiSigWrapper.submitTransactionAsync(destination, from, dataParams); - - txId = subRes.logs[0].args.transactionId.toNumber(); - const confRes = await multiSig.confirmTransaction(txId, { - from: owners[1], + it('should set confirmation time with enough confirmations', async () => { + const destination = multiSig.address; + const from = owners[0]; + const dataParams = { + name: 'changeTimeLock', + abi: MULTI_SIG_ABI, + args: [SECONDS_TIME_LOCKED], + }; + let txHash = await multiSigWrapper.submitTransactionAsync(destination, from, dataParams); + const subRes = await zeroEx.awaitTransactionMinedAsync(txHash); + const log = abiDecoder.tryToDecodeLogOrNoop(subRes.logs[0]) as LogWithDecodedArgs< + SubmissionContractEventArgs + >; + + txId = log.args.transactionId.toNumber(); + txHash = await multiSig.confirmTransaction(txId, { from: owners[1] }); + const res = await zeroEx.awaitTransactionMinedAsync(txHash); + expect(res.logs).to.have.length(2); + + const blockNum = await web3Wrapper.getBlockNumberAsync(); + const blockInfo = await web3Wrapper.getBlockAsync(blockNum); + const timestamp = new BigNumber(blockInfo.timestamp); + const confirmationTimeBigNum = new BigNumber(await multiSig.confirmationTimes.call(txId)); + + expect(timestamp).to.be.bignumber.equal(confirmationTimeBigNum); }); - expect(confRes.logs).to.have.length(2); - return expect(multiSig.executeTransaction(txId)).to.be.rejectedWith(constants.REVERT); + it('should be executable with enough confirmations and secondsTimeLocked of 0', async () => { + const destination = multiSig.address; + const from = owners[0]; + const dataParams = { + name: 'changeTimeLock', + abi: MULTI_SIG_ABI, + args: [SECONDS_TIME_LOCKED], + }; + let txHash = await multiSigWrapper.submitTransactionAsync(destination, from, dataParams); + const subRes = await zeroEx.awaitTransactionMinedAsync(txHash); + const log = abiDecoder.tryToDecodeLogOrNoop(subRes.logs[0]) as LogWithDecodedArgs< + SubmissionContractEventArgs + >; + + txId = log.args.transactionId.toNumber(); + txHash = await multiSig.confirmTransaction(txId, { from: owners[1] }); + + expect(initialSecondsTimeLocked).to.be.equal(0); + + txHash = await multiSig.executeTransaction(txId, { from: owners[0] }); + const res = await zeroEx.awaitTransactionMinedAsync(txHash); + expect(res.logs).to.have.length(2); + + const secondsTimeLocked = new BigNumber(await multiSig.secondsTimeLocked.call()); + expect(secondsTimeLocked).to.be.bignumber.equal(SECONDS_TIME_LOCKED); + }); }); + describe('initially time-locked', async () => { + before('deploy a walet', async () => { + multiSig = await deployer.deployAsync('MultiSigWalletWithTimeLock', [ + owners, + SIGNATURES_REQUIRED, + SECONDS_TIME_LOCKED, + ]); + multiSigWrapper = new MultiSigWrapper(multiSig); + + const secondsTimeLocked = await multiSig.secondsTimeLocked(); + initialSecondsTimeLocked = secondsTimeLocked.toNumber(); + const destination = multiSig.address; + const from = owners[0]; + const dataParams = { + name: 'changeTimeLock', + abi: MULTI_SIG_ABI, + args: [newSecondsTimeLocked], + }; + let txHash = await multiSigWrapper.submitTransactionAsync(destination, from, dataParams); + const subRes = await zeroEx.awaitTransactionMinedAsync(txHash); + const log = abiDecoder.tryToDecodeLogOrNoop(subRes.logs[0]) as LogWithDecodedArgs< + SubmissionContractEventArgs + >; + txId = log.args.transactionId.toNumber(); + txHash = await multiSig.confirmTransaction(txId, { + from: owners[1], + }); + const confRes = await zeroEx.awaitTransactionMinedAsync(txHash); + expect(confRes.logs).to.have.length(2); + }); + const newSecondsTimeLocked = 0; + it('should throw if it has enough confirmations but is not past the time lock', async () => { + return expect(multiSig.executeTransaction(txId, { from: owners[0] })).to.be.rejectedWith( + constants.REVERT, + ); + }); - it('should execute if it has enough confirmations and is past the time lock', async () => { - await rpc.increaseTimeAsync(SECONDS_TIME_LOCKED); - await multiSig.executeTransaction(txId); + it('should execute if it has enough confirmations and is past the time lock', async () => { + await rpc.increaseTimeAsync(SECONDS_TIME_LOCKED); + await multiSig.executeTransaction(txId, { from: owners[0] }); - const secondsTimeLocked = new BigNumber(await multiSig.secondsTimeLocked.call()); - expect(secondsTimeLocked).to.be.bignumber.equal(newSecondsTimeLocked); + const secondsTimeLocked = new BigNumber(await multiSig.secondsTimeLocked.call()); + expect(secondsTimeLocked).to.be.bignumber.equal(newSecondsTimeLocked); + }); }); }); }); diff --git a/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts b/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts index b10e260a2..e8261b6f1 100644 --- a/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts +++ b/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts @@ -1,25 +1,32 @@ +import { LogWithDecodedArgs, ZeroEx } from '0x.js'; import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; +import { AbiDecoder } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; +import * as Web3 from 'web3'; -import * as tokenTransferProxyJSON from '../../build/contracts/TokenTransferProxy.json'; -import { Artifacts } from '../util/artifacts'; +import { artifacts } from '../util/artifacts'; import { constants } from '../util/constants'; import { crypto } from '../util/crypto'; import { MultiSigWrapper } from '../util/multi_sig_wrapper'; -import { ContractInstance, TransactionDataParams } from '../util/types'; +import { SubmissionContractEventArgs, TransactionDataParams } from '../util/types'; import { chaiSetup } from './utils/chai_setup'; -const { TokenTransferProxy, MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress } = new Artifacts(artifacts); -const PROXY_ABI = (tokenTransferProxyJSON as any).abi; +import { deployer } from './utils/deployer'; +const PROXY_ABI = artifacts.TokenTransferProxyArtifact.networks[constants.TESTRPC_NETWORK_ID].abi; +const MUTISIG_WALLET_WITH_TIME_LOCK_EXCEPT_REMOVE_AUTHORIZED_ADDRESS_ABI = + artifacts.MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddressArtifact.networks[constants.TESTRPC_NETWORK_ID] + .abi; chaiSetup.configure(); const expect = chai.expect; const web3 = web3Factory.create(); +const web3Wrapper = new Web3Wrapper(web3.currentProvider); const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); +const abiDecoder = new AbiDecoder([MUTISIG_WALLET_WITH_TIME_LOCK_EXCEPT_REMOVE_AUTHORIZED_ADDRESS_ABI]); describe('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', () => { - const web3Wrapper = new Web3Wrapper(web3.currentProvider); + const zeroEx = new ZeroEx(web3.currentProvider, { networkId: constants.TESTRPC_NETWORK_ID }); let owners: string[]; const requiredApprovals = 2; const SECONDS_TIME_LOCKED = 1000000; @@ -28,8 +35,8 @@ describe('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', () => { let authorizedAddress: string; let unauthorizedAddress: string; - let tokenTransferProxy: ContractInstance; - let multiSig: ContractInstance; + let tokenTransferProxy: Web3.ContractInstance; + let multiSig: Web3.ContractInstance; let multiSigWrapper: MultiSigWrapper; let validDestination: string; @@ -45,16 +52,16 @@ describe('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', () => { .slice(0, 20) .toString('hex')}`; const initialOwner = accounts[0]; - tokenTransferProxy = await TokenTransferProxy.new({ from: initialOwner }); + tokenTransferProxy = await deployer.deployAsync('TokenTransferProxy'); await tokenTransferProxy.addAuthorizedAddress(authorizedAddress, { from: initialOwner, }); - multiSig = await MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress.new( + multiSig = await deployer.deployAsync('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', [ owners, requiredApprovals, SECONDS_TIME_LOCKED, tokenTransferProxy.address, - ); + ]); await tokenTransferProxy.transferOwnership(multiSig.address, { from: initialOwner, }); @@ -88,27 +95,35 @@ describe('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', () => { abi: PROXY_ABI, args: [authorizedAddress], }; - const res = await multiSigWrapper.submitTransactionAsync(validDestination, owners[0], dataParams); - const txId = res.logs[0].args.transactionId.toString(); - - return expect(multiSig.executeRemoveAuthorizedAddress(txId)).to.be.rejectedWith(constants.REVERT); + const txHash = await multiSigWrapper.submitTransactionAsync(validDestination, owners[0], dataParams); + const res = await zeroEx.awaitTransactionMinedAsync(txHash); + const log = abiDecoder.tryToDecodeLogOrNoop(res.logs[0]) as LogWithDecodedArgs; + const txId = log.args.transactionId.toString(); + + return expect(multiSig.executeRemoveAuthorizedAddress(txId, { from: owners[1] })).to.be.rejectedWith( + constants.REVERT, + ); }); it('should throw if tx destination is not the tokenTransferProxy', async () => { - const invalidTokenTransferProxy = await TokenTransferProxy.new(); + const invalidTokenTransferProxy = await deployer.deployAsync('TokenTransferProxy'); const invalidDestination = invalidTokenTransferProxy.address; const dataParams: TransactionDataParams = { name: 'removeAuthorizedAddress', abi: PROXY_ABI, args: [authorizedAddress], }; - const res = await multiSigWrapper.submitTransactionAsync(invalidDestination, owners[0], dataParams); - const txId = res.logs[0].args.transactionId.toString(); + const txHash = await multiSigWrapper.submitTransactionAsync(invalidDestination, owners[0], dataParams); + const res = await zeroEx.awaitTransactionMinedAsync(txHash); + const log = abiDecoder.tryToDecodeLogOrNoop(res.logs[0]) as LogWithDecodedArgs; + const txId = log.args.transactionId.toString(); await multiSig.confirmTransaction(txId, { from: owners[1] }); const isConfirmed = await multiSig.isConfirmed.call(txId); expect(isConfirmed).to.be.true(); - return expect(multiSig.executeRemoveAuthorizedAddress(txId)).to.be.rejectedWith(constants.REVERT); + return expect(multiSig.executeRemoveAuthorizedAddress(txId, { from: owners[1] })).to.be.rejectedWith( + constants.REVERT, + ); }); it('should throw if tx data is not for removeAuthorizedAddress', async () => { @@ -117,13 +132,17 @@ describe('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', () => { abi: PROXY_ABI, args: [unauthorizedAddress], }; - const res = await multiSigWrapper.submitTransactionAsync(validDestination, owners[0], dataParams); - const txId = res.logs[0].args.transactionId.toString(); + const txHash = await multiSigWrapper.submitTransactionAsync(validDestination, owners[0], dataParams); + const res = await zeroEx.awaitTransactionMinedAsync(txHash); + const log = abiDecoder.tryToDecodeLogOrNoop(res.logs[0]) as LogWithDecodedArgs; + const txId = log.args.transactionId.toString(); await multiSig.confirmTransaction(txId, { from: owners[1] }); const isConfirmed = await multiSig.isConfirmed.call(txId); expect(isConfirmed).to.be.true(); - return expect(multiSig.executeRemoveAuthorizedAddress(txId)).to.be.rejectedWith(constants.REVERT); + return expect(multiSig.executeRemoveAuthorizedAddress(txId, { from: owners[1] })).to.be.rejectedWith( + constants.REVERT, + ); }); it('should execute removeAuthorizedAddress for valid tokenTransferProxy if fully confirmed', async () => { @@ -132,13 +151,14 @@ describe('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', () => { abi: PROXY_ABI, args: [authorizedAddress], }; - const res = await multiSigWrapper.submitTransactionAsync(validDestination, owners[0], dataParams); - const txId = res.logs[0].args.transactionId.toString(); + const txHash = await multiSigWrapper.submitTransactionAsync(validDestination, owners[0], dataParams); + const res = await zeroEx.awaitTransactionMinedAsync(txHash); + const log = abiDecoder.tryToDecodeLogOrNoop(res.logs[0]) as LogWithDecodedArgs; + const txId = log.args.transactionId.toString(); await multiSig.confirmTransaction(txId, { from: owners[1] }); const isConfirmed = await multiSig.isConfirmed.call(txId); expect(isConfirmed).to.be.true(); - await multiSig.executeRemoveAuthorizedAddress(txId); - + await multiSig.executeRemoveAuthorizedAddress(txId, { from: owners[1] }); const isAuthorized = await tokenTransferProxy.authorized.call(authorizedAddress); expect(isAuthorized).to.be.false(); }); @@ -149,16 +169,20 @@ describe('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', () => { abi: PROXY_ABI, args: [authorizedAddress], }; - const res = await multiSigWrapper.submitTransactionAsync(validDestination, owners[0], dataParams); - const txId = res.logs[0].args.transactionId.toString(); + const txHash = await multiSigWrapper.submitTransactionAsync(validDestination, owners[0], dataParams); + const res = await zeroEx.awaitTransactionMinedAsync(txHash); + const log = abiDecoder.tryToDecodeLogOrNoop(res.logs[0]) as LogWithDecodedArgs; + const txId = log.args.transactionId.toString(); await multiSig.confirmTransaction(txId, { from: owners[1] }); - const isConfirmed = await multiSig.isConfirmed.call(txId); + const isConfirmed = await multiSig.isConfirmed(txId); expect(isConfirmed).to.be.true(); - await multiSig.executeRemoveAuthorizedAddress(txId); - const tx = await multiSig.transactions.call(txId); + await multiSig.executeRemoveAuthorizedAddress(txId, { from: owners[1] }); + const tx = await multiSig.transactions(txId); const isExecuted = tx[3]; expect(isExecuted).to.be.true(); - return expect(multiSig.executeRemoveAuthorizedAddress(txId)).to.be.rejectedWith(constants.REVERT); + return expect(multiSig.executeRemoveAuthorizedAddress(txId, { from: owners[1] })).to.be.rejectedWith( + constants.REVERT, + ); }); }); }); diff --git a/packages/contracts/test/token_registry.ts b/packages/contracts/test/token_registry.ts index cc78feba3..1146353da 100644 --- a/packages/contracts/test/token_registry.ts +++ b/packages/contracts/test/token_registry.ts @@ -4,31 +4,30 @@ 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 { Artifacts } from '../util/artifacts'; import { constants } from '../util/constants'; import { TokenRegWrapper } from '../util/token_registry_wrapper'; -import { ContractInstance } from '../util/types'; import { chaiSetup } from './utils/chai_setup'; +import { deployer } from './utils/deployer'; -const { TokenRegistry } = new Artifacts(artifacts); chaiSetup.configure(); const expect = chai.expect; const web3 = web3Factory.create(); +const web3Wrapper = new Web3Wrapper(web3.currentProvider); const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('TokenRegistry', () => { - const web3Wrapper = new Web3Wrapper(web3.currentProvider); let owner: string; let notOwner: string; - let tokenReg: ContractInstance; + let tokenReg: Web3.ContractInstance; let tokenRegWrapper: TokenRegWrapper; before(async () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); owner = accounts[0]; notOwner = accounts[1]; - tokenReg = await TokenRegistry.new(); + tokenReg = await deployer.deployAsync('TokenRegistry'); tokenRegWrapper = new TokenRegWrapper(tokenReg); }); beforeEach(async () => { @@ -137,10 +136,9 @@ describe('TokenRegistry', () => { }); it('should change the token name when called by owner', async () => { - const res = await tokenReg.setTokenName(token1.address, token2.name, { + await tokenReg.setTokenName(token1.address, token2.name, { from: owner, }); - expect(res.logs).to.have.length(1); const [newData, oldData] = await Promise.all([ tokenRegWrapper.getTokenByNameAsync(token2.name), tokenRegWrapper.getTokenByNameAsync(token1.name), @@ -178,7 +176,6 @@ describe('TokenRegistry', () => { it('should change the token symbol when called by owner', async () => { const res = await tokenReg.setTokenSymbol(token1.address, token2.symbol, { from: owner }); - expect(res.logs).to.have.length(1); const [newData, oldData] = await Promise.all([ tokenRegWrapper.getTokenBySymbolAsync(token2.symbol), tokenRegWrapper.getTokenBySymbolAsync(token1.symbol), @@ -222,7 +219,6 @@ describe('TokenRegistry', () => { const res = await tokenReg.removeToken(token1.address, index, { from: owner, }); - expect(res.logs).to.have.length(1); const tokenData = await tokenRegWrapper.getTokenMetaDataAsync(token1.address); expect(tokenData).to.be.deep.equal(nullToken); }); diff --git a/packages/contracts/test/token_transfer_proxy/auth.ts b/packages/contracts/test/token_transfer_proxy/auth.ts index 65455ed9e..6b9767148 100644 --- a/packages/contracts/test/token_transfer_proxy/auth.ts +++ b/packages/contracts/test/token_transfer_proxy/auth.ts @@ -1,28 +1,28 @@ import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; +import * as Web3 from 'web3'; import { constants } from '../../util/constants'; -import { ContractInstance } from '../../util/types'; import { chaiSetup } from '../utils/chai_setup'; +import { deployer } from '../utils/deployer'; chaiSetup.configure(); const expect = chai.expect; -const TokenTransferProxy = artifacts.require('./db/TokenTransferProxy.sol'); const web3 = web3Factory.create(); +const web3Wrapper = new Web3Wrapper(web3.currentProvider); const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('TokenTransferProxy', () => { - const web3Wrapper = new Web3Wrapper(web3.currentProvider); let owner: string; let notOwner: string; let address: string; - let tokenTransferProxy: ContractInstance; + let tokenTransferProxy: Web3.ContractInstance; before(async () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); owner = address = accounts[0]; notOwner = accounts[1]; - tokenTransferProxy = await TokenTransferProxy.deployed(); + tokenTransferProxy = await deployer.deployAsync('TokenTransferProxy'); }); beforeEach(async () => { await blockchainLifecycle.startAsync(); @@ -38,7 +38,7 @@ describe('TokenTransferProxy', () => { }); it('should allow owner to add an authorized address', async () => { await tokenTransferProxy.addAuthorizedAddress(address, { from: owner }); - const isAuthorized = await tokenTransferProxy.authorized.call(address); + const isAuthorized = await tokenTransferProxy.authorized(address); expect(isAuthorized).to.be.true(); }); it('should throw if owner attempts to authorize a duplicate address', async () => { @@ -64,7 +64,7 @@ describe('TokenTransferProxy', () => { await tokenTransferProxy.removeAuthorizedAddress(address, { from: owner, }); - const isAuthorized = await tokenTransferProxy.authorized.call(address); + const isAuthorized = await tokenTransferProxy.authorized(address); expect(isAuthorized).to.be.false(); }); @@ -80,19 +80,19 @@ describe('TokenTransferProxy', () => { describe('getAuthorizedAddresses', () => { it('should return all authorized addresses', async () => { const initial = await tokenTransferProxy.getAuthorizedAddresses(); - expect(initial).to.have.length(1); + expect(initial).to.have.length(0); await tokenTransferProxy.addAuthorizedAddress(address, { from: owner, }); const afterAdd = await tokenTransferProxy.getAuthorizedAddresses(); - expect(afterAdd).to.have.length(2); + expect(afterAdd).to.have.length(1); expect(afterAdd).to.include(address); await tokenTransferProxy.removeAuthorizedAddress(address, { from: owner, }); const afterRemove = await tokenTransferProxy.getAuthorizedAddresses(); - expect(afterRemove).to.have.length(1); + expect(afterRemove).to.have.length(0); }); }); }); diff --git a/packages/contracts/test/token_transfer_proxy/transfer_from.ts b/packages/contracts/test/token_transfer_proxy/transfer_from.ts index d1245d3b2..deabf8fbc 100644 --- a/packages/contracts/test/token_transfer_proxy/transfer_from.ts +++ b/packages/contracts/test/token_transfer_proxy/transfer_from.ts @@ -1,49 +1,43 @@ import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; +import * as Web3 from 'web3'; -import { Artifacts } from '../../util/artifacts'; import { Balances } from '../../util/balances'; import { constants } from '../../util/constants'; -import { ContractInstance } from '../../util/types'; import { chaiSetup } from '../utils/chai_setup'; +import { deployer } from '../utils/deployer'; chaiSetup.configure(); const expect = chai.expect; -const { TokenTransferProxy, DummyToken, TokenRegistry } = new Artifacts(artifacts); const web3 = web3Factory.create(); +const web3Wrapper = new Web3Wrapper(web3.currentProvider); const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('TokenTransferProxy', () => { - const web3Wrapper = new Web3Wrapper(web3.currentProvider); let accounts: string[]; let owner: string; let notAuthorized: string; const INIT_BAL = 100000000; const INIT_ALLOW = 100000000; - let tokenTransferProxy: ContractInstance; - let tokenRegistry: ContractInstance; - let rep: ContractInstance; + let tokenTransferProxy: Web3.ContractInstance; + let rep: Web3.ContractInstance; let dmyBalances: Balances; before(async () => { accounts = await web3Wrapper.getAvailableAddressesAsync(); owner = notAuthorized = accounts[0]; - [tokenTransferProxy, tokenRegistry] = await Promise.all([ - TokenTransferProxy.deployed(), - TokenRegistry.deployed(), - ]); - const repAddress = await tokenRegistry.getTokenAddressBySymbol('REP'); - rep = DummyToken.at(repAddress); + tokenTransferProxy = await deployer.deployAsync('TokenTransferProxy'); + rep = await deployer.deployAsync('DummyToken'); dmyBalances = new Balances([rep], [accounts[0], accounts[1]]); await Promise.all([ - rep.approve(TokenTransferProxy.address, INIT_ALLOW, { + rep.approve(tokenTransferProxy.address, INIT_ALLOW, { from: accounts[0], }), rep.setBalance(accounts[0], INIT_BAL, { from: owner }), - rep.approve(TokenTransferProxy.address, INIT_ALLOW, { + rep.approve(tokenTransferProxy.address, INIT_ALLOW, { from: accounts[1], }), rep.setBalance(accounts[1], INIT_BAL, { from: owner }), diff --git a/packages/contracts/test/unlimited_allowance_token.ts b/packages/contracts/test/unlimited_allowance_token.ts index 36f88c3d2..b21bb8323 100644 --- a/packages/contracts/test/unlimited_allowance_token.ts +++ b/packages/contracts/test/unlimited_allowance_token.ts @@ -3,21 +3,20 @@ import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-u import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; +import * as Web3 from 'web3'; -import { Artifacts } from '../util/artifacts'; import { constants } from '../util/constants'; -import { ContractInstance } from '../util/types'; import { chaiSetup } from './utils/chai_setup'; +import { deployer } from './utils/deployer'; -const { DummyToken } = new Artifacts(artifacts); const web3 = web3Factory.create(); +const web3Wrapper = new Web3Wrapper(web3.currentProvider); chaiSetup.configure(); const expect = chai.expect; const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('UnlimitedAllowanceToken', () => { - const web3Wrapper = new Web3Wrapper(web3.currentProvider); let owner: string; let spender: string; const config = { @@ -27,13 +26,13 @@ describe('UnlimitedAllowanceToken', () => { const MAX_MINT_VALUE = new BigNumber(100000000000000000000); let tokenAddress: string; - let token: ContractInstance; + let token: Web3.ContractInstance; before(async () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); owner = accounts[0]; spender = accounts[1]; - token = await DummyToken.new({ from: owner }); + token = await deployer.deployAsync('DummyToken'); await token.mint(MAX_MINT_VALUE, { from: owner }); tokenAddress = token.address; }); diff --git a/packages/contracts/test/unlimited_allowance_token_v2.ts b/packages/contracts/test/unlimited_allowance_token_v2.ts index 3c4a8e941..d97259cd2 100644 --- a/packages/contracts/test/unlimited_allowance_token_v2.ts +++ b/packages/contracts/test/unlimited_allowance_token_v2.ts @@ -3,21 +3,20 @@ import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-u import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; +import * as Web3 from 'web3'; -import { Artifacts } from '../util/artifacts'; import { constants } from '../util/constants'; -import { ContractInstance } from '../util/types'; import { chaiSetup } from './utils/chai_setup'; +import { deployer } from './utils/deployer'; -const { DummyTokenV2 } = new Artifacts(artifacts); chaiSetup.configure(); const expect = chai.expect; const web3 = web3Factory.create(); +const web3Wrapper = new Web3Wrapper(web3.currentProvider); const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('UnlimitedAllowanceTokenV2', () => { - const web3Wrapper = new Web3Wrapper(web3.currentProvider); const config = { networkId: constants.TESTRPC_NETWORK_ID, }; @@ -27,13 +26,13 @@ describe('UnlimitedAllowanceTokenV2', () => { const MAX_MINT_VALUE = new BigNumber(100000000000000000000); let tokenAddress: string; - let token: ContractInstance; + let token: Web3.ContractInstance; before(async () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); owner = accounts[0]; spender = accounts[1]; - token = await DummyTokenV2.new({ from: owner }); + token = await deployer.deployAsync('DummyToken_v2'); await token.mint(MAX_MINT_VALUE, { from: owner }); tokenAddress = token.address; }); diff --git a/packages/contracts/test/utils/deployer.ts b/packages/contracts/test/utils/deployer.ts new file mode 100644 index 000000000..4c6eeff2b --- /dev/null +++ b/packages/contracts/test/utils/deployer.ts @@ -0,0 +1,16 @@ +import { Deployer } from '@0xproject/deployer'; +import { devConstants } from '@0xproject/dev-utils'; +import * as path from 'path'; + +import { constants } from '../../util/constants'; + +const deployerOpts = { + artifactsDir: `${path.resolve('build')}/artifacts`, + jsonrpcPort: devConstants.RPC_PORT, + networkId: constants.TESTRPC_NETWORK_ID, + defaults: { + gas: devConstants.GAS_ESTIMATE, + }, +}; + +export const deployer = new Deployer(deployerOpts); diff --git a/packages/contracts/test/zrx_token.ts b/packages/contracts/test/zrx_token.ts index 573fb8d5a..ffc3ec17f 100644 --- a/packages/contracts/test/zrx_token.ts +++ b/packages/contracts/test/zrx_token.ts @@ -3,28 +3,27 @@ import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-u import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; +import * as Web3 from 'web3'; -import { Artifacts } from '../util/artifacts'; import { constants } from '../util/constants'; -import { ContractInstance } from '../util/types'; import { chaiSetup } from './utils/chai_setup'; +import { deployer } from './utils/deployer'; chaiSetup.configure(); const expect = chai.expect; -const { Exchange, ZRXToken } = new Artifacts(artifacts); const web3 = web3Factory.create(); +const web3Wrapper = new Web3Wrapper(web3.currentProvider); const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); describe('ZRXToken', () => { - const web3Wrapper = new Web3Wrapper(web3.currentProvider); let owner: string; let spender: string; let zeroEx: ZeroEx; let MAX_UINT: BigNumber; - let zrx: ContractInstance; + let zrx: Web3.ContractInstance; let zrxAddress: string; before(async () => { @@ -32,10 +31,9 @@ describe('ZRXToken', () => { owner = accounts[0]; spender = accounts[1]; zeroEx = new ZeroEx(web3.currentProvider, { - exchangeContractAddress: Exchange.address, networkId: constants.TESTRPC_NETWORK_ID, }); - zrx = await ZRXToken.new(); + zrx = await deployer.deployAsync('ZRXToken'); zrxAddress = zrx.address; MAX_UINT = zeroEx.token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS; }); @@ -47,25 +45,25 @@ describe('ZRXToken', () => { }); describe('constants', () => { it('should have 18 decimals', async () => { - const decimals = new BigNumber(await zrx.decimals.call()); + const decimals = new BigNumber(await zrx.decimals()); 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.call()); + const totalSupply = new BigNumber(await zrx.totalSupply()); 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.call(); + const name = await zrx.name(); const expectedName = '0x Protocol Token'; expect(name).to.be.equal(expectedName); }); it('should have the symbol ZRX', async () => { - const symbol = await zrx.symbol.call(); + const symbol = await zrx.symbol(); const expectedSymbol = 'ZRX'; expect(symbol).to.be.equal(expectedSymbol); }); @@ -74,7 +72,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.call()); + const totalSupply = new BigNumber(await zrx.totalSupply()); expect(totalSupply).to.be.bignumber.equal(ownerBalance); }); }); @@ -84,8 +82,7 @@ describe('ZRXToken', () => { const receiver = spender; const initOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); const amountToTransfer = new BigNumber(1); - const txHash = await zeroEx.token.transferAsync(zrxAddress, owner, receiver, amountToTransfer); - await zeroEx.awaitTransactionMinedAsync(txHash); + await zeroEx.token.transferAsync(zrxAddress, owner, receiver, amountToTransfer); const finalOwnerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); const finalReceiverBalance = await zeroEx.token.getBalanceAsync(zrxAddress, receiver); @@ -107,10 +104,9 @@ describe('ZRXToken', () => { it('should return false if owner has insufficient balance', async () => { const ownerBalance = await zeroEx.token.getBalanceAsync(zrxAddress, owner); const amountToTransfer = ownerBalance.plus(1); - const txHash = await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, amountToTransfer, { + await zeroEx.token.setAllowanceAsync(zrxAddress, owner, spender, amountToTransfer, { gasLimit: constants.MAX_TOKEN_APPROVE_GAS, }); - await zeroEx.awaitTransactionMinedAsync(txHash); const didReturnTrue = await zrx.transferFrom.call(owner, spender, amountToTransfer, { from: spender }); expect(didReturnTrue).to.be.false(); }); -- cgit v1.2.3 From 1e9147b8bb45a251eefdead23573a9e8d68fc34b Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 25 Jan 2018 15:12:46 +0100 Subject: Normalize the dependencies --- packages/contracts/test/exchange/core.ts | 59 +++++++++++++------------------ packages/contracts/test/token_registry.ts | 4 +-- 2 files changed, 26 insertions(+), 37 deletions(-) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/exchange/core.ts b/packages/contracts/test/exchange/core.ts index 5fe2a9452..ac8c7229e 100644 --- a/packages/contracts/test/exchange/core.ts +++ b/packages/contracts/test/exchange/core.ts @@ -147,9 +147,8 @@ describe('Exchange', () => { takerTokenAmount: new BigNumber(3), }); - const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); + const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync(order.params + .orderHashHex as string); expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); const fillTakerTokenAmount1 = new BigNumber(2); @@ -157,9 +156,8 @@ describe('Exchange', () => { fillTakerTokenAmount: fillTakerTokenAmount1, }); - const filledTakerTokenAmountAfter1 = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); + const filledTakerTokenAmountAfter1 = await zeroEx.exchange.getFilledTakerAmountAsync(order.params + .orderHashHex as string); expect(filledTakerTokenAmountAfter1).to.be.bignumber.equal(fillTakerTokenAmount1); const fillTakerTokenAmount2 = new BigNumber(1); @@ -167,9 +165,8 @@ describe('Exchange', () => { fillTakerTokenAmount: fillTakerTokenAmount2, }); - const filledTakerTokenAmountAfter2 = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); + const filledTakerTokenAmountAfter2 = await zeroEx.exchange.getFilledTakerAmountAsync(order.params + .orderHashHex as string); expect(filledTakerTokenAmountAfter2).to.be.bignumber.equal(filledTakerTokenAmountAfter1); }); @@ -179,17 +176,15 @@ describe('Exchange', () => { takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), }); - const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); + const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync(order.params + .orderHashHex as string); expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); - const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); + const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync(order.params + .orderHashHex as string); expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(fillTakerTokenAmount); const newBalances = await dmyBalances.getAsync(); @@ -232,17 +227,15 @@ describe('Exchange', () => { takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18), }); - const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); + const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync(order.params + .orderHashHex as string); expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); - const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); + const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync(order.params + .orderHashHex as string); expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(fillTakerTokenAmount); const newBalances = await dmyBalances.getAsync(); @@ -285,17 +278,15 @@ describe('Exchange', () => { takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), }); - const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); + const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync(order.params + .orderHashHex as string); expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); - const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); + const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync(order.params + .orderHashHex as string); expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(fillTakerTokenAmount); const newBalances = await dmyBalances.getAsync(); @@ -339,17 +330,15 @@ describe('Exchange', () => { takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18), }); - const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); + const filledTakerTokenAmountBefore = await zeroEx.exchange.getFilledTakerAmountAsync(order.params + .orderHashHex as string); expect(filledTakerTokenAmountBefore).to.be.bignumber.equal(0); const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); await exWrapper.fillOrderAsync(order, taker, { fillTakerTokenAmount }); - const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync( - order.params.orderHashHex, - ); + const filledTakerTokenAmountAfter = await zeroEx.exchange.getFilledTakerAmountAsync(order.params + .orderHashHex as string); const expectedFillAmountTAfter = fillTakerTokenAmount.add(filledTakerTokenAmountBefore); expect(filledTakerTokenAmountAfter).to.be.bignumber.equal(expectedFillAmountTAfter); @@ -678,7 +667,7 @@ describe('Exchange', () => { expect(newBalances).to.be.deep.equal(balances); }); - it.skip('should throw if getBalance or getAllowance attempts to change state and \ + it('should throw if getBalance or getAllowance attempts to change state and \ shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { const maliciousToken = await deployer.deployAsync('MaliciousToken'); await maliciousToken.approve(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: taker }); @@ -691,7 +680,7 @@ describe('Exchange', () => { exWrapper.fillOrderAsync(order, taker, { shouldThrowOnInsufficientBalanceOrAllowance: false, }), - ).to.be.rejectedWith(constants.REVERT); + ).to.be.rejectedWith(constants.INVALID_OPCODE); }); it('should not change balances if an order is expired', async () => { diff --git a/packages/contracts/test/token_registry.ts b/packages/contracts/test/token_registry.ts index 1146353da..549b2f175 100644 --- a/packages/contracts/test/token_registry.ts +++ b/packages/contracts/test/token_registry.ts @@ -175,7 +175,7 @@ describe('TokenRegistry', () => { }); it('should change the token symbol when called by owner', async () => { - const res = await tokenReg.setTokenSymbol(token1.address, token2.symbol, { from: owner }); + await tokenReg.setTokenSymbol(token1.address, token2.symbol, { from: owner }); const [newData, oldData] = await Promise.all([ tokenRegWrapper.getTokenBySymbolAsync(token2.symbol), tokenRegWrapper.getTokenBySymbolAsync(token1.symbol), @@ -216,7 +216,7 @@ describe('TokenRegistry', () => { it('should remove token metadata when called by owner', async () => { const index = 0; - const res = await tokenReg.removeToken(token1.address, index, { + await tokenReg.removeToken(token1.address, index, { from: owner, }); const tokenData = await tokenRegWrapper.getTokenMetaDataAsync(token1.address); -- cgit v1.2.3 From 6f13d107c4ca90049249e44a90b45cac2b490762 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 26 Jan 2018 11:21:06 +0100 Subject: Remove promisified web3 functions from tests --- packages/contracts/test/ether_token.ts | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/ether_token.ts b/packages/contracts/test/ether_token.ts index d4e478099..fd7321280 100644 --- a/packages/contracts/test/ether_token.ts +++ b/packages/contracts/test/ether_token.ts @@ -20,12 +20,6 @@ describe('EtherToken', () => { const gasPrice = ZeroEx.toBaseUnitAmount(new BigNumber(20), 9); let zeroEx: ZeroEx; let etherTokenAddress: string; - const sendTransactionAsync = promisify(web3.eth.sendTransaction); - const getEthBalanceAsync = async (owner: string) => { - const balanceStr = await promisify(web3.eth.getBalance)(owner); - const balance = new BigNumber(balanceStr); - return balance; - }; before(async () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); account = accounts[0]; @@ -45,7 +39,7 @@ describe('EtherToken', () => { }); describe('deposit', () => { it('should throw if caller attempts to deposit more Ether than caller balance', async () => { - const initEthBalance = await getEthBalanceAsync(account); + const initEthBalance = await web3Wrapper.getBalanceInWeiAsync(account); const ethToDeposit = initEthBalance.plus(1); return expect(zeroEx.etherToken.depositAsync(etherTokenAddress, ethToDeposit, account)).to.be.rejectedWith( @@ -54,7 +48,7 @@ describe('EtherToken', () => { }); it('should convert deposited Ether to wrapped Ether tokens', async () => { - const initEthBalance = await getEthBalanceAsync(account); + const initEthBalance = await web3Wrapper.getBalanceInWeiAsync(account); const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); const ethToDeposit = new BigNumber(web3.toWei(1, 'ether')); @@ -63,7 +57,7 @@ describe('EtherToken', () => { const receipt = await zeroEx.awaitTransactionMinedAsync(txHash); const ethSpentOnGas = gasPrice.times(receipt.gasUsed); - const finalEthBalance = await getEthBalanceAsync(account); + const finalEthBalance = await web3Wrapper.getBalanceInWeiAsync(account); const finalEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); expect(finalEthBalance).to.be.bignumber.equal(initEthBalance.minus(ethToDeposit.plus(ethSpentOnGas))); @@ -85,7 +79,7 @@ describe('EtherToken', () => { const ethToDeposit = new BigNumber(web3.toWei(1, 'ether')); await zeroEx.etherToken.depositAsync(etherTokenAddress, ethToDeposit, account); const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); - const initEthBalance = await getEthBalanceAsync(account); + const initEthBalance = await web3Wrapper.getBalanceInWeiAsync(account); const ethTokensToWithdraw = initEthTokenBalance; expect(ethTokensToWithdraw).to.not.be.bignumber.equal(0); const txHash = await zeroEx.etherToken.withdrawAsync(etherTokenAddress, ethTokensToWithdraw, account, { @@ -94,7 +88,7 @@ describe('EtherToken', () => { const receipt = await zeroEx.awaitTransactionMinedAsync(txHash); const ethSpentOnGas = gasPrice.times(receipt.gasUsed); - const finalEthBalance = await getEthBalanceAsync(account); + const finalEthBalance = await web3Wrapper.getBalanceInWeiAsync(account); const finalEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); expect(finalEthBalance).to.be.bignumber.equal( @@ -106,12 +100,12 @@ describe('EtherToken', () => { describe('fallback', () => { it('should convert sent ether to ether tokens', async () => { - const initEthBalance = await getEthBalanceAsync(account); + const initEthBalance = await web3Wrapper.getBalanceInWeiAsync(account); const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); const ethToDeposit = ZeroEx.toBaseUnitAmount(new BigNumber(1), 18); - const txHash = await sendTransactionAsync({ + const txHash = await web3Wrapper.sendTransactionAsync({ from: account, to: etherTokenAddress, value: ethToDeposit, @@ -121,7 +115,7 @@ describe('EtherToken', () => { const receipt = await zeroEx.awaitTransactionMinedAsync(txHash); const ethSpentOnGas = gasPrice.times(receipt.gasUsed); - const finalEthBalance = await getEthBalanceAsync(account); + const finalEthBalance = await web3Wrapper.getBalanceInWeiAsync(account); const finalEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); expect(finalEthBalance).to.be.bignumber.equal(initEthBalance.minus(ethToDeposit.plus(ethSpentOnGas))); -- cgit v1.2.3 From 9d62e5fb6f0dc6dc451604f02fb4374a36fcac3c Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 26 Jan 2018 11:39:31 +0100 Subject: Use an enum for contract name --- packages/contracts/test/ether_token.ts | 3 ++- packages/contracts/test/exchange/core.ts | 14 +++++++------- packages/contracts/test/exchange/helpers.ts | 13 +++++++------ packages/contracts/test/exchange/wrapper.ts | 14 +++++++------- packages/contracts/test/multi_sig_with_time_lock.ts | 10 +++++++--- .../multi_sig_with_time_lock_except_remove_auth_addr.ts | 8 ++++---- packages/contracts/test/token_registry.ts | 3 ++- packages/contracts/test/token_transfer_proxy/auth.ts | 3 ++- .../contracts/test/token_transfer_proxy/transfer_from.ts | 5 +++-- packages/contracts/test/unlimited_allowance_token.ts | 3 ++- packages/contracts/test/unlimited_allowance_token_v2.ts | 3 ++- packages/contracts/test/zrx_token.ts | 3 ++- 12 files changed, 47 insertions(+), 35 deletions(-) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/ether_token.ts b/packages/contracts/test/ether_token.ts index fd7321280..2bcc0e2c5 100644 --- a/packages/contracts/test/ether_token.ts +++ b/packages/contracts/test/ether_token.ts @@ -5,6 +5,7 @@ import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; import { constants } from '../util/constants'; +import { ContractName } from '../util/types'; import { chaiSetup } from './utils/chai_setup'; import { deployer } from './utils/deployer'; @@ -24,7 +25,7 @@ describe('EtherToken', () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); account = accounts[0]; - const etherToken = await deployer.deployAsync('WETH9'); + const etherToken = await deployer.deployAsync(ContractName.EtherToken); etherTokenAddress = etherToken.address; zeroEx = new ZeroEx(web3.currentProvider, { gasPrice, diff --git a/packages/contracts/test/exchange/core.ts b/packages/contracts/test/exchange/core.ts index ac8c7229e..f2e330507 100644 --- a/packages/contracts/test/exchange/core.ts +++ b/packages/contracts/test/exchange/core.ts @@ -19,7 +19,7 @@ import { crypto } from '../../util/crypto'; import { ExchangeWrapper } from '../../util/exchange_wrapper'; import { Order } from '../../util/order'; import { OrderFactory } from '../../util/order_factory'; -import { BalancesByOwner, ExchangeContractErrs } from '../../util/types'; +import { BalancesByOwner, ContractName, ExchangeContractErrs } from '../../util/types'; import { chaiSetup } from '../utils/chai_setup'; import { deployer } from '../utils/deployer'; @@ -58,12 +58,12 @@ describe('Exchange', () => { taker = accounts[1] || accounts[accounts.length - 1]; feeRecipient = accounts[2] || accounts[accounts.length - 1]; [rep, dgd, zrx] = await Promise.all([ - deployer.deployAsync('DummyToken'), - deployer.deployAsync('DummyToken'), - deployer.deployAsync('DummyToken'), + deployer.deployAsync(ContractName.DummyToken), + deployer.deployAsync(ContractName.DummyToken), + deployer.deployAsync(ContractName.DummyToken), ]); - tokenTransferProxy = await deployer.deployAsync('TokenTransferProxy'); - exchange = await deployer.deployAsync('Exchange', [zrx.address, tokenTransferProxy.address]); + tokenTransferProxy = await deployer.deployAsync(ContractName.TokenTransferProxy); + exchange = await deployer.deployAsync(ContractName.Exchange, [zrx.address, tokenTransferProxy.address]); await tokenTransferProxy.addAuthorizedAddress(exchange.address, { from: accounts[0] }); zeroEx = new ZeroEx(web3.currentProvider, { exchangeContractAddress: exchange.address, @@ -669,7 +669,7 @@ describe('Exchange', () => { it('should throw if getBalance or getAllowance attempts to change state and \ shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - const maliciousToken = await deployer.deployAsync('MaliciousToken'); + const maliciousToken = await deployer.deployAsync(ContractName.MaliciousToken); await maliciousToken.approve(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: taker }); order = await orderFactory.newSignedOrderAsync({ diff --git a/packages/contracts/test/exchange/helpers.ts b/packages/contracts/test/exchange/helpers.ts index 965edeba8..a4707452e 100644 --- a/packages/contracts/test/exchange/helpers.ts +++ b/packages/contracts/test/exchange/helpers.ts @@ -9,6 +9,7 @@ import { constants } from '../../util/constants'; import { ExchangeWrapper } from '../../util/exchange_wrapper'; import { Order } from '../../util/order'; import { OrderFactory } from '../../util/order_factory'; +import { ContractName } from '../../util/types'; import { chaiSetup } from '../utils/chai_setup'; import { deployer } from '../utils/deployer'; @@ -31,14 +32,14 @@ describe('Exchange', () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); maker = accounts[0]; feeRecipient = accounts[1] || accounts[accounts.length - 1]; - const tokenRegistry = await deployer.deployAsync('TokenRegistry'); - const tokenTransferProxy = await deployer.deployAsync('TokenTransferProxy'); + const tokenRegistry = await deployer.deployAsync(ContractName.TokenRegistry); + const tokenTransferProxy = await deployer.deployAsync(ContractName.TokenTransferProxy); const [rep, dgd, zrx] = await Promise.all([ - deployer.deployAsync('DummyToken'), - deployer.deployAsync('DummyToken'), - deployer.deployAsync('DummyToken'), + deployer.deployAsync(ContractName.DummyToken), + deployer.deployAsync(ContractName.DummyToken), + deployer.deployAsync(ContractName.DummyToken), ]); - const exchange = await deployer.deployAsync('Exchange', [zrx.address, tokenTransferProxy.address]); + const exchange = await deployer.deployAsync(ContractName.Exchange, [zrx.address, tokenTransferProxy.address]); await tokenTransferProxy.addAuthorizedAddress(exchange.address, { from: accounts[0] }); const zeroEx = new ZeroEx(web3.currentProvider, { networkId: constants.TESTRPC_NETWORK_ID }); exchangeWrapper = new ExchangeWrapper(exchange, zeroEx); diff --git a/packages/contracts/test/exchange/wrapper.ts b/packages/contracts/test/exchange/wrapper.ts index 2253f4cb7..c749c016e 100644 --- a/packages/contracts/test/exchange/wrapper.ts +++ b/packages/contracts/test/exchange/wrapper.ts @@ -11,7 +11,7 @@ import { constants } from '../../util/constants'; import { ExchangeWrapper } from '../../util/exchange_wrapper'; import { Order } from '../../util/order'; import { OrderFactory } from '../../util/order_factory'; -import { BalancesByOwner } from '../../util/types'; +import { BalancesByOwner, ContractName } from '../../util/types'; import { chaiSetup } from '../utils/chai_setup'; import { deployer } from '../utils/deployer'; @@ -49,13 +49,13 @@ describe('Exchange', () => { taker = accounts[1] || accounts[accounts.length - 1]; feeRecipient = accounts[2] || accounts[accounts.length - 1]; [rep, dgd, zrx] = await Promise.all([ - deployer.deployAsync('DummyToken'), - deployer.deployAsync('DummyToken'), - deployer.deployAsync('DummyToken'), + deployer.deployAsync(ContractName.DummyToken), + deployer.deployAsync(ContractName.DummyToken), + deployer.deployAsync(ContractName.DummyToken), ]); - tokenRegistry = await deployer.deployAsync('TokenRegistry'); - tokenTransferProxy = await deployer.deployAsync('TokenTransferProxy'); - exchange = await deployer.deployAsync('Exchange', [zrx.address, tokenTransferProxy.address]); + tokenRegistry = await deployer.deployAsync(ContractName.TokenRegistry); + tokenTransferProxy = await deployer.deployAsync(ContractName.TokenTransferProxy); + exchange = await deployer.deployAsync(ContractName.Exchange, [zrx.address, tokenTransferProxy.address]); await tokenTransferProxy.addAuthorizedAddress(exchange.address, { from: accounts[0] }); const zeroEx = new ZeroEx(web3.currentProvider, { networkId: constants.TESTRPC_NETWORK_ID }); exWrapper = new ExchangeWrapper(exchange, zeroEx); diff --git a/packages/contracts/test/multi_sig_with_time_lock.ts b/packages/contracts/test/multi_sig_with_time_lock.ts index ff8a01d14..eee923020 100644 --- a/packages/contracts/test/multi_sig_with_time_lock.ts +++ b/packages/contracts/test/multi_sig_with_time_lock.ts @@ -9,7 +9,7 @@ import * as multiSigWalletJSON from '../../build/contracts/MultiSigWalletWithTim import { artifacts } from '../util/artifacts'; import { constants } from '../util/constants'; import { MultiSigWrapper } from '../util/multi_sig_wrapper'; -import { SubmissionContractEventArgs } from '../util/types'; +import { ContractName, SubmissionContractEventArgs } from '../util/types'; import { chaiSetup } from './utils/chai_setup'; import { deployer } from './utils/deployer'; @@ -52,7 +52,11 @@ describe('MultiSigWalletWithTimeLock', () => { describe('changeTimeLock', () => { describe('initially non-time-locked', async () => { before('deploy a walet', async () => { - multiSig = await deployer.deployAsync('MultiSigWalletWithTimeLock', [owners, SIGNATURES_REQUIRED, 0]); + multiSig = await deployer.deployAsync(ContractName.MultiSigWalletWithTimeLock, [ + owners, + SIGNATURES_REQUIRED, + 0, + ]); multiSigWrapper = new MultiSigWrapper(multiSig); const secondsTimeLocked = await multiSig.secondsTimeLocked(); @@ -140,7 +144,7 @@ describe('MultiSigWalletWithTimeLock', () => { }); describe('initially time-locked', async () => { before('deploy a walet', async () => { - multiSig = await deployer.deployAsync('MultiSigWalletWithTimeLock', [ + multiSig = await deployer.deployAsync(ContractName.MultiSigWalletWithTimeLock, [ owners, SIGNATURES_REQUIRED, SECONDS_TIME_LOCKED, diff --git a/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts b/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts index e8261b6f1..826815265 100644 --- a/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts +++ b/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts @@ -9,7 +9,7 @@ import { artifacts } from '../util/artifacts'; import { constants } from '../util/constants'; import { crypto } from '../util/crypto'; import { MultiSigWrapper } from '../util/multi_sig_wrapper'; -import { SubmissionContractEventArgs, TransactionDataParams } from '../util/types'; +import { ContractName, SubmissionContractEventArgs, TransactionDataParams } from '../util/types'; import { chaiSetup } from './utils/chai_setup'; import { deployer } from './utils/deployer'; @@ -52,11 +52,11 @@ describe('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', () => { .slice(0, 20) .toString('hex')}`; const initialOwner = accounts[0]; - tokenTransferProxy = await deployer.deployAsync('TokenTransferProxy'); + tokenTransferProxy = await deployer.deployAsync(ContractName.TokenTransferProxy); await tokenTransferProxy.addAuthorizedAddress(authorizedAddress, { from: initialOwner, }); - multiSig = await deployer.deployAsync('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', [ + multiSig = await deployer.deployAsync(ContractName.MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress, [ owners, requiredApprovals, SECONDS_TIME_LOCKED, @@ -106,7 +106,7 @@ describe('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', () => { }); it('should throw if tx destination is not the tokenTransferProxy', async () => { - const invalidTokenTransferProxy = await deployer.deployAsync('TokenTransferProxy'); + const invalidTokenTransferProxy = await deployer.deployAsync(ContractName.TokenTransferProxy); const invalidDestination = invalidTokenTransferProxy.address; const dataParams: TransactionDataParams = { name: 'removeAuthorizedAddress', diff --git a/packages/contracts/test/token_registry.ts b/packages/contracts/test/token_registry.ts index 549b2f175..f84a99e8f 100644 --- a/packages/contracts/test/token_registry.ts +++ b/packages/contracts/test/token_registry.ts @@ -8,6 +8,7 @@ import * as Web3 from 'web3'; import { constants } from '../util/constants'; import { TokenRegWrapper } from '../util/token_registry_wrapper'; +import { ContractName } from '../util/types'; import { chaiSetup } from './utils/chai_setup'; import { deployer } from './utils/deployer'; @@ -27,7 +28,7 @@ describe('TokenRegistry', () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); owner = accounts[0]; notOwner = accounts[1]; - tokenReg = await deployer.deployAsync('TokenRegistry'); + tokenReg = await deployer.deployAsync(ContractName.TokenRegistry); tokenRegWrapper = new TokenRegWrapper(tokenReg); }); beforeEach(async () => { diff --git a/packages/contracts/test/token_transfer_proxy/auth.ts b/packages/contracts/test/token_transfer_proxy/auth.ts index 6b9767148..52e7d3f02 100644 --- a/packages/contracts/test/token_transfer_proxy/auth.ts +++ b/packages/contracts/test/token_transfer_proxy/auth.ts @@ -4,6 +4,7 @@ import * as chai from 'chai'; import * as Web3 from 'web3'; import { constants } from '../../util/constants'; +import { ContractName } from '../../util/types'; import { chaiSetup } from '../utils/chai_setup'; import { deployer } from '../utils/deployer'; @@ -22,7 +23,7 @@ describe('TokenTransferProxy', () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); owner = address = accounts[0]; notOwner = accounts[1]; - tokenTransferProxy = await deployer.deployAsync('TokenTransferProxy'); + tokenTransferProxy = await deployer.deployAsync(ContractName.TokenTransferProxy); }); beforeEach(async () => { await blockchainLifecycle.startAsync(); diff --git a/packages/contracts/test/token_transfer_proxy/transfer_from.ts b/packages/contracts/test/token_transfer_proxy/transfer_from.ts index deabf8fbc..3e88d9432 100644 --- a/packages/contracts/test/token_transfer_proxy/transfer_from.ts +++ b/packages/contracts/test/token_transfer_proxy/transfer_from.ts @@ -5,6 +5,7 @@ import * as Web3 from 'web3'; import { Balances } from '../../util/balances'; import { constants } from '../../util/constants'; +import { ContractName } from '../../util/types'; import { chaiSetup } from '../utils/chai_setup'; import { deployer } from '../utils/deployer'; @@ -28,8 +29,8 @@ describe('TokenTransferProxy', () => { before(async () => { accounts = await web3Wrapper.getAvailableAddressesAsync(); owner = notAuthorized = accounts[0]; - tokenTransferProxy = await deployer.deployAsync('TokenTransferProxy'); - rep = await deployer.deployAsync('DummyToken'); + tokenTransferProxy = await deployer.deployAsync(ContractName.TokenTransferProxy); + rep = await deployer.deployAsync(ContractName.DummyToken); dmyBalances = new Balances([rep], [accounts[0], accounts[1]]); await Promise.all([ diff --git a/packages/contracts/test/unlimited_allowance_token.ts b/packages/contracts/test/unlimited_allowance_token.ts index b21bb8323..1c66c39b8 100644 --- a/packages/contracts/test/unlimited_allowance_token.ts +++ b/packages/contracts/test/unlimited_allowance_token.ts @@ -6,6 +6,7 @@ import * as chai from 'chai'; import * as Web3 from 'web3'; import { constants } from '../util/constants'; +import { ContractName } from '../util/types'; import { chaiSetup } from './utils/chai_setup'; import { deployer } from './utils/deployer'; @@ -32,7 +33,7 @@ describe('UnlimitedAllowanceToken', () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); owner = accounts[0]; spender = accounts[1]; - token = await deployer.deployAsync('DummyToken'); + token = await deployer.deployAsync(ContractName.DummyToken); await token.mint(MAX_MINT_VALUE, { from: owner }); tokenAddress = token.address; }); diff --git a/packages/contracts/test/unlimited_allowance_token_v2.ts b/packages/contracts/test/unlimited_allowance_token_v2.ts index d97259cd2..a36e44154 100644 --- a/packages/contracts/test/unlimited_allowance_token_v2.ts +++ b/packages/contracts/test/unlimited_allowance_token_v2.ts @@ -6,6 +6,7 @@ import * as chai from 'chai'; import * as Web3 from 'web3'; import { constants } from '../util/constants'; +import { ContractName } from '../util/types'; import { chaiSetup } from './utils/chai_setup'; import { deployer } from './utils/deployer'; @@ -32,7 +33,7 @@ describe('UnlimitedAllowanceTokenV2', () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); owner = accounts[0]; spender = accounts[1]; - token = await deployer.deployAsync('DummyToken_v2'); + token = await deployer.deployAsync(ContractName.DummyToken_v2); await token.mint(MAX_MINT_VALUE, { from: owner }); tokenAddress = token.address; }); diff --git a/packages/contracts/test/zrx_token.ts b/packages/contracts/test/zrx_token.ts index ffc3ec17f..2caef7da8 100644 --- a/packages/contracts/test/zrx_token.ts +++ b/packages/contracts/test/zrx_token.ts @@ -6,6 +6,7 @@ import * as chai from 'chai'; import * as Web3 from 'web3'; import { constants } from '../util/constants'; +import { ContractName } from '../util/types'; import { chaiSetup } from './utils/chai_setup'; import { deployer } from './utils/deployer'; @@ -33,7 +34,7 @@ describe('ZRXToken', () => { zeroEx = new ZeroEx(web3.currentProvider, { networkId: constants.TESTRPC_NETWORK_ID, }); - zrx = await deployer.deployAsync('ZRXToken'); + zrx = await deployer.deployAsync(ContractName.ZRXToken); zrxAddress = zrx.address; MAX_UINT = zeroEx.token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS; }); -- cgit v1.2.3 From 6205209fbbb5dbebbf225e1cef76c3e86117d356 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 30 Jan 2018 14:03:54 +0100 Subject: Make an RPC constructor param implicit --- packages/contracts/test/ether_token.ts | 2 +- packages/contracts/test/exchange/core.ts | 2 +- packages/contracts/test/exchange/helpers.ts | 2 +- packages/contracts/test/exchange/wrapper.ts | 2 +- packages/contracts/test/multi_sig_with_time_lock.ts | 6 +++--- .../test/multi_sig_with_time_lock_except_remove_auth_addr.ts | 2 +- packages/contracts/test/token_registry.ts | 2 +- packages/contracts/test/token_transfer_proxy/auth.ts | 2 +- packages/contracts/test/token_transfer_proxy/transfer_from.ts | 2 +- packages/contracts/test/unlimited_allowance_token.ts | 2 +- packages/contracts/test/unlimited_allowance_token_v2.ts | 2 +- packages/contracts/test/zrx_token.ts | 2 +- 12 files changed, 14 insertions(+), 14 deletions(-) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/ether_token.ts b/packages/contracts/test/ether_token.ts index 2bcc0e2c5..4c70534ee 100644 --- a/packages/contracts/test/ether_token.ts +++ b/packages/contracts/test/ether_token.ts @@ -14,7 +14,7 @@ chaiSetup.configure(); const expect = chai.expect; const web3 = web3Factory.create(); const web3Wrapper = new Web3Wrapper(web3.currentProvider); -const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); +const blockchainLifecycle = new BlockchainLifecycle(); describe('EtherToken', () => { let account: string; diff --git a/packages/contracts/test/exchange/core.ts b/packages/contracts/test/exchange/core.ts index f2e330507..0db4be830 100644 --- a/packages/contracts/test/exchange/core.ts +++ b/packages/contracts/test/exchange/core.ts @@ -27,7 +27,7 @@ chaiSetup.configure(); const expect = chai.expect; const web3 = web3Factory.create(); const web3Wrapper = new Web3Wrapper(web3.currentProvider); -const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); +const blockchainLifecycle = new BlockchainLifecycle(); describe('Exchange', () => { let maker: string; diff --git a/packages/contracts/test/exchange/helpers.ts b/packages/contracts/test/exchange/helpers.ts index a4707452e..047ab899a 100644 --- a/packages/contracts/test/exchange/helpers.ts +++ b/packages/contracts/test/exchange/helpers.ts @@ -18,7 +18,7 @@ const expect = chai.expect; const web3 = web3Factory.create(); const web3Wrapper = new Web3Wrapper(web3.currentProvider); -const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); +const blockchainLifecycle = new BlockchainLifecycle(); describe('Exchange', () => { let maker: string; diff --git a/packages/contracts/test/exchange/wrapper.ts b/packages/contracts/test/exchange/wrapper.ts index c749c016e..43cb622ac 100644 --- a/packages/contracts/test/exchange/wrapper.ts +++ b/packages/contracts/test/exchange/wrapper.ts @@ -19,7 +19,7 @@ chaiSetup.configure(); const expect = chai.expect; const web3 = web3Factory.create(); const web3Wrapper = new Web3Wrapper(web3.currentProvider); -const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); +const blockchainLifecycle = new BlockchainLifecycle(); describe('Exchange', () => { let maker: string; diff --git a/packages/contracts/test/multi_sig_with_time_lock.ts b/packages/contracts/test/multi_sig_with_time_lock.ts index eee923020..bd64be1ba 100644 --- a/packages/contracts/test/multi_sig_with_time_lock.ts +++ b/packages/contracts/test/multi_sig_with_time_lock.ts @@ -1,5 +1,5 @@ import { LogWithDecodedArgs, ZeroEx } from '0x.js'; -import { BlockchainLifecycle, devConstants, RPC, web3Factory } from '@0xproject/dev-utils'; +import { BlockchainLifecycle, RPC, web3Factory } from '@0xproject/dev-utils'; import { AbiDecoder, BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; import * as chai from 'chai'; @@ -20,7 +20,7 @@ const expect = chai.expect; const web3 = web3Factory.create(); const web3Wrapper = new Web3Wrapper(web3.currentProvider); -const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); +const blockchainLifecycle = new BlockchainLifecycle(); const zeroEx = new ZeroEx(web3.currentProvider, { networkId: constants.TESTRPC_NETWORK_ID }); const abiDecoder = new AbiDecoder([MULTI_SIG_ABI]); @@ -40,7 +40,7 @@ describe('MultiSigWalletWithTimeLock', () => { let rpc: RPC; before(async () => { - rpc = new RPC(devConstants.RPC_URL); + rpc = new RPC(); }); beforeEach(async () => { await blockchainLifecycle.startAsync(); diff --git a/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts b/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts index 826815265..06f30ffdc 100644 --- a/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts +++ b/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts @@ -22,7 +22,7 @@ chaiSetup.configure(); const expect = chai.expect; const web3 = web3Factory.create(); const web3Wrapper = new Web3Wrapper(web3.currentProvider); -const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); +const blockchainLifecycle = new BlockchainLifecycle(); const abiDecoder = new AbiDecoder([MUTISIG_WALLET_WITH_TIME_LOCK_EXCEPT_REMOVE_AUTHORIZED_ADDRESS_ABI]); describe('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', () => { diff --git a/packages/contracts/test/token_registry.ts b/packages/contracts/test/token_registry.ts index f84a99e8f..2a270dc2f 100644 --- a/packages/contracts/test/token_registry.ts +++ b/packages/contracts/test/token_registry.ts @@ -17,7 +17,7 @@ chaiSetup.configure(); const expect = chai.expect; const web3 = web3Factory.create(); const web3Wrapper = new Web3Wrapper(web3.currentProvider); -const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); +const blockchainLifecycle = new BlockchainLifecycle(); describe('TokenRegistry', () => { let owner: string; diff --git a/packages/contracts/test/token_transfer_proxy/auth.ts b/packages/contracts/test/token_transfer_proxy/auth.ts index 52e7d3f02..b2bfc6b65 100644 --- a/packages/contracts/test/token_transfer_proxy/auth.ts +++ b/packages/contracts/test/token_transfer_proxy/auth.ts @@ -12,7 +12,7 @@ chaiSetup.configure(); const expect = chai.expect; const web3 = web3Factory.create(); const web3Wrapper = new Web3Wrapper(web3.currentProvider); -const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); +const blockchainLifecycle = new BlockchainLifecycle(); describe('TokenTransferProxy', () => { let owner: string; diff --git a/packages/contracts/test/token_transfer_proxy/transfer_from.ts b/packages/contracts/test/token_transfer_proxy/transfer_from.ts index 3e88d9432..bd7adcfae 100644 --- a/packages/contracts/test/token_transfer_proxy/transfer_from.ts +++ b/packages/contracts/test/token_transfer_proxy/transfer_from.ts @@ -13,7 +13,7 @@ chaiSetup.configure(); const expect = chai.expect; const web3 = web3Factory.create(); const web3Wrapper = new Web3Wrapper(web3.currentProvider); -const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); +const blockchainLifecycle = new BlockchainLifecycle(); describe('TokenTransferProxy', () => { let accounts: string[]; diff --git a/packages/contracts/test/unlimited_allowance_token.ts b/packages/contracts/test/unlimited_allowance_token.ts index 1c66c39b8..7f4fef987 100644 --- a/packages/contracts/test/unlimited_allowance_token.ts +++ b/packages/contracts/test/unlimited_allowance_token.ts @@ -15,7 +15,7 @@ const web3 = web3Factory.create(); const web3Wrapper = new Web3Wrapper(web3.currentProvider); chaiSetup.configure(); const expect = chai.expect; -const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); +const blockchainLifecycle = new BlockchainLifecycle(); describe('UnlimitedAllowanceToken', () => { let owner: string; diff --git a/packages/contracts/test/unlimited_allowance_token_v2.ts b/packages/contracts/test/unlimited_allowance_token_v2.ts index a36e44154..440cc11ac 100644 --- a/packages/contracts/test/unlimited_allowance_token_v2.ts +++ b/packages/contracts/test/unlimited_allowance_token_v2.ts @@ -15,7 +15,7 @@ chaiSetup.configure(); const expect = chai.expect; const web3 = web3Factory.create(); const web3Wrapper = new Web3Wrapper(web3.currentProvider); -const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); +const blockchainLifecycle = new BlockchainLifecycle(); describe('UnlimitedAllowanceTokenV2', () => { const config = { diff --git a/packages/contracts/test/zrx_token.ts b/packages/contracts/test/zrx_token.ts index 2caef7da8..1844a67af 100644 --- a/packages/contracts/test/zrx_token.ts +++ b/packages/contracts/test/zrx_token.ts @@ -15,7 +15,7 @@ chaiSetup.configure(); const expect = chai.expect; const web3 = web3Factory.create(); const web3Wrapper = new Web3Wrapper(web3.currentProvider); -const blockchainLifecycle = new BlockchainLifecycle(devConstants.RPC_URL); +const blockchainLifecycle = new BlockchainLifecycle(); describe('ZRXToken', () => { let owner: string; -- cgit v1.2.3 From 5a4c0bff6a54f605040141b7b1a64c6a33a339b0 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 30 Jan 2018 14:06:43 +0100 Subject: Remove accounts magic from tests --- packages/contracts/test/exchange/core.ts | 4 +--- packages/contracts/test/exchange/helpers.ts | 3 +-- packages/contracts/test/exchange/wrapper.ts | 5 ++--- 3 files changed, 4 insertions(+), 8 deletions(-) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/exchange/core.ts b/packages/contracts/test/exchange/core.ts index 0db4be830..e9182dd1b 100644 --- a/packages/contracts/test/exchange/core.ts +++ b/packages/contracts/test/exchange/core.ts @@ -54,9 +54,7 @@ describe('Exchange', () => { before(async () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); maker = accounts[0]; - tokenOwner = accounts[0]; - taker = accounts[1] || accounts[accounts.length - 1]; - feeRecipient = accounts[2] || accounts[accounts.length - 1]; + [tokenOwner, taker, feeRecipient] = accounts; [rep, dgd, zrx] = await Promise.all([ deployer.deployAsync(ContractName.DummyToken), deployer.deployAsync(ContractName.DummyToken), diff --git a/packages/contracts/test/exchange/helpers.ts b/packages/contracts/test/exchange/helpers.ts index 047ab899a..5efce41a4 100644 --- a/packages/contracts/test/exchange/helpers.ts +++ b/packages/contracts/test/exchange/helpers.ts @@ -30,8 +30,7 @@ describe('Exchange', () => { before(async () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); - maker = accounts[0]; - feeRecipient = accounts[1] || accounts[accounts.length - 1]; + [maker, feeRecipient] = accounts; const tokenRegistry = await deployer.deployAsync(ContractName.TokenRegistry); const tokenTransferProxy = await deployer.deployAsync(ContractName.TokenTransferProxy); const [rep, dgd, zrx] = await Promise.all([ diff --git a/packages/contracts/test/exchange/wrapper.ts b/packages/contracts/test/exchange/wrapper.ts index 43cb622ac..acdf481a9 100644 --- a/packages/contracts/test/exchange/wrapper.ts +++ b/packages/contracts/test/exchange/wrapper.ts @@ -45,9 +45,8 @@ describe('Exchange', () => { before(async () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); - tokenOwner = maker = accounts[0]; - taker = accounts[1] || accounts[accounts.length - 1]; - feeRecipient = accounts[2] || accounts[accounts.length - 1]; + tokenOwner = accounts[0]; + [maker, taker, feeRecipient] = accounts; [rep, dgd, zrx] = await Promise.all([ deployer.deployAsync(ContractName.DummyToken), deployer.deployAsync(ContractName.DummyToken), -- cgit v1.2.3 From 52ad16b9200ff892b0880891e467e2b371bf8220 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Tue, 30 Jan 2018 14:09:49 +0100 Subject: Remove authorized address magic from tests --- .../test/multi_sig_with_time_lock_except_remove_auth_addr.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts b/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts index 06f30ffdc..f6b7c1d53 100644 --- a/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts +++ b/packages/contracts/test/multi_sig_with_time_lock_except_remove_auth_addr.ts @@ -43,14 +43,7 @@ describe('MultiSigWalletWithTimeLockExceptRemoveAuthorizedAddress', () => { before(async () => { const accounts = await web3Wrapper.getAvailableAddressesAsync(); owners = [accounts[0], accounts[1]]; - authorizedAddress = `0x${crypto - .solSHA3([accounts[0]]) - .slice(0, 20) - .toString('hex')}`; - unauthorizedAddress = `0x${crypto - .solSHA3([accounts[1]]) - .slice(0, 20) - .toString('hex')}`; + [authorizedAddress, unauthorizedAddress] = accounts; const initialOwner = accounts[0]; tokenTransferProxy = await deployer.deployAsync(ContractName.TokenTransferProxy); await tokenTransferProxy.addAuthorizedAddress(authorizedAddress, { -- cgit v1.2.3 From 2721252d6a82e83801c650ebad6a2cb4d7195bc1 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Fri, 19 Jan 2018 23:13:31 +0800 Subject: Get rid of suffixed contract versioning and replace it with a poor-mans package manager. Versions are stored locally, and are generated in a copy-on-write basis as required --- .../contracts/test/unlimited_allowance_token.ts | 26 +++- .../contracts/test/unlimited_allowance_token_v2.ts | 152 --------------------- 2 files changed, 20 insertions(+), 158 deletions(-) delete mode 100644 packages/contracts/test/unlimited_allowance_token_v2.ts (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/unlimited_allowance_token.ts b/packages/contracts/test/unlimited_allowance_token.ts index 7f4fef987..34d2ba33b 100644 --- a/packages/contracts/test/unlimited_allowance_token.ts +++ b/packages/contracts/test/unlimited_allowance_token.ts @@ -44,6 +44,14 @@ describe('UnlimitedAllowanceToken', () => { await blockchainLifecycle.revertAsync(); }); describe('transfer', () => { + it('should throw if owner has insufficient balance', async () => { + const ownerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); + const amountToTransfer = ownerBalance.plus(1); + return expect(token.transfer.call(spender, amountToTransfer, { from: owner })).to.be.rejectedWith( + constants.REVERT, + ); + }); + it('should transfer balance from sender to receiver', async () => { const receiver = spender; const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); @@ -67,15 +75,18 @@ describe('UnlimitedAllowanceToken', () => { }); describe('transferFrom', () => { - it('should return false if owner has insufficient balance', async () => { + it('should throw if owner has insufficient balance', async () => { const ownerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); const amountToTransfer = ownerBalance.plus(1); await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, amountToTransfer); - const didReturnTrue = await token.transferFrom.call(owner, spender, amountToTransfer, { from: spender }); - expect(didReturnTrue).to.be.false(); + return expect( + token.transferFrom.call(owner, spender, amountToTransfer, { + from: spender, + }), + ).to.be.rejectedWith(constants.REVERT); }); - it('should return false if spender has insufficient allowance', async () => { + it('should throw if spender has insufficient allowance', async () => { const ownerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); const amountToTransfer = ownerBalance; @@ -83,8 +94,11 @@ describe('UnlimitedAllowanceToken', () => { const spenderAllowanceIsInsufficient = spenderAllowance.cmp(amountToTransfer) < 0; expect(spenderAllowanceIsInsufficient).to.be.true(); - const didReturnTrue = await token.transferFrom.call(owner, spender, amountToTransfer, { from: spender }); - expect(didReturnTrue).to.be.false(); + return expect( + token.transferFrom.call(owner, spender, amountToTransfer, { + from: spender, + }), + ).to.be.rejectedWith(constants.REVERT); }); it('should return true on a 0 value transfer', async () => { diff --git a/packages/contracts/test/unlimited_allowance_token_v2.ts b/packages/contracts/test/unlimited_allowance_token_v2.ts deleted file mode 100644 index 440cc11ac..000000000 --- a/packages/contracts/test/unlimited_allowance_token_v2.ts +++ /dev/null @@ -1,152 +0,0 @@ -import { ZeroEx } from '0x.js'; -import { BlockchainLifecycle, devConstants, web3Factory } 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 { constants } from '../util/constants'; -import { ContractName } from '../util/types'; - -import { chaiSetup } from './utils/chai_setup'; -import { deployer } from './utils/deployer'; - -chaiSetup.configure(); -const expect = chai.expect; -const web3 = web3Factory.create(); -const web3Wrapper = new Web3Wrapper(web3.currentProvider); -const blockchainLifecycle = new BlockchainLifecycle(); - -describe('UnlimitedAllowanceTokenV2', () => { - const config = { - networkId: constants.TESTRPC_NETWORK_ID, - }; - const zeroEx = new ZeroEx(web3.currentProvider, config); - let owner: string; - let spender: string; - - const MAX_MINT_VALUE = new BigNumber(100000000000000000000); - let tokenAddress: string; - let token: Web3.ContractInstance; - - before(async () => { - const accounts = await web3Wrapper.getAvailableAddressesAsync(); - owner = accounts[0]; - spender = accounts[1]; - token = await deployer.deployAsync(ContractName.DummyToken_v2); - await token.mint(MAX_MINT_VALUE, { from: owner }); - tokenAddress = token.address; - }); - beforeEach(async () => { - await blockchainLifecycle.startAsync(); - }); - afterEach(async () => { - await blockchainLifecycle.revertAsync(); - }); - describe('transfer', () => { - it('should throw if owner has insufficient balance', async () => { - const ownerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = ownerBalance.plus(1); - return expect(token.transfer.call(spender, amountToTransfer, { from: owner })).to.be.rejectedWith( - constants.REVERT, - ); - }); - - it('should transfer balance from sender to receiver', async () => { - const receiver = spender; - const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = new BigNumber(1); - await zeroEx.token.transferAsync(tokenAddress, owner, receiver, amountToTransfer); - const finalOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const finalReceiverBalance = await zeroEx.token.getBalanceAsync(tokenAddress, receiver); - - const expectedFinalOwnerBalance = initOwnerBalance.minus(amountToTransfer); - const expectedFinalReceiverBalance = amountToTransfer; - expect(finalOwnerBalance).to.be.bignumber.equal(expectedFinalOwnerBalance); - expect(finalReceiverBalance).to.be.bignumber.equal(expectedFinalReceiverBalance); - }); - - it('should return true on a 0 value transfer', async () => { - const didReturnTrue = await token.transfer.call(spender, 0, { - from: owner, - }); - expect(didReturnTrue).to.be.true(); - }); - }); - - describe('transferFrom', () => { - it('should throw if owner has insufficient balance', async () => { - const ownerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = ownerBalance.plus(1); - await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, amountToTransfer); - return expect( - token.transferFrom.call(owner, spender, amountToTransfer, { - from: spender, - }), - ).to.be.rejectedWith(constants.REVERT); - }); - - it('should throw if spender has insufficient allowance', async () => { - const ownerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = ownerBalance; - - const spenderAllowance = await zeroEx.token.getAllowanceAsync(tokenAddress, owner, spender); - const spenderAllowanceIsInsufficient = spenderAllowance.cmp(amountToTransfer) < 0; - expect(spenderAllowanceIsInsufficient).to.be.true(); - - return expect( - token.transferFrom.call(owner, spender, amountToTransfer, { - from: spender, - }), - ).to.be.rejectedWith(constants.REVERT); - }); - - it('should return true on a 0 value transfer', async () => { - const amountToTransfer = 0; - const didReturnTrue = await token.transferFrom.call(owner, spender, amountToTransfer, { from: spender }); - expect(didReturnTrue).to.be.true(); - }); - - it('should not modify spender allowance if spender allowance is 2^256 - 1', async () => { - const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = initOwnerBalance; - const initSpenderAllowance = zeroEx.token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS; - await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, initSpenderAllowance); - await zeroEx.token.transferFromAsync(tokenAddress, owner, spender, spender, amountToTransfer, { - gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, - }); - - const newSpenderAllowance = await zeroEx.token.getAllowanceAsync(tokenAddress, owner, spender); - expect(initSpenderAllowance).to.be.bignumber.equal(newSpenderAllowance); - }); - - it('should transfer the correct balances if spender has sufficient allowance', async () => { - const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = initOwnerBalance; - const initSpenderAllowance = initOwnerBalance; - await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, initSpenderAllowance); - await zeroEx.token.transferFromAsync(tokenAddress, owner, spender, spender, amountToTransfer, { - gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, - }); - - const newOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const newSpenderBalance = await zeroEx.token.getBalanceAsync(tokenAddress, spender); - - expect(newOwnerBalance).to.be.bignumber.equal(0); - expect(newSpenderBalance).to.be.bignumber.equal(initOwnerBalance); - }); - - it('should modify allowance if spender has sufficient allowance less than 2^256 - 1', async () => { - const initOwnerBalance = await zeroEx.token.getBalanceAsync(tokenAddress, owner); - const amountToTransfer = initOwnerBalance; - const initSpenderAllowance = initOwnerBalance; - await zeroEx.token.setAllowanceAsync(tokenAddress, owner, spender, initSpenderAllowance); - await zeroEx.token.transferFromAsync(tokenAddress, owner, spender, spender, amountToTransfer, { - gasLimit: constants.MAX_TOKEN_TRANSFERFROM_GAS, - }); - - const newSpenderAllowance = await zeroEx.token.getAllowanceAsync(tokenAddress, owner, spender); - expect(newSpenderAllowance).to.be.bignumber.equal(0); - }); - }); -}); -- cgit v1.2.3 From d5d6079b673d0a6d311f349dba291da18c846d9e Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Thu, 1 Feb 2018 15:48:06 -0800 Subject: Rename directories --- packages/contracts/test/utils/deployer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/utils/deployer.ts b/packages/contracts/test/utils/deployer.ts index 4c6eeff2b..dc41e41e2 100644 --- a/packages/contracts/test/utils/deployer.ts +++ b/packages/contracts/test/utils/deployer.ts @@ -5,7 +5,7 @@ import * as path from 'path'; import { constants } from '../../util/constants'; const deployerOpts = { - artifactsDir: `${path.resolve('build')}/artifacts`, + artifactsDir: path.resolve('artifacts'), jsonrpcPort: devConstants.RPC_PORT, networkId: constants.TESTRPC_NETWORK_ID, defaults: { -- cgit v1.2.3 From 05aae368132a81ddb9fd6a04ac5b0ff1cbb24691 Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Thu, 1 Feb 2018 18:43:04 -0800 Subject: Update contract versions, fix tests --- packages/contracts/test/exchange/core.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/contracts/test') diff --git a/packages/contracts/test/exchange/core.ts b/packages/contracts/test/exchange/core.ts index e9182dd1b..10816d2d6 100644 --- a/packages/contracts/test/exchange/core.ts +++ b/packages/contracts/test/exchange/core.ts @@ -678,7 +678,7 @@ describe('Exchange', () => { exWrapper.fillOrderAsync(order, taker, { shouldThrowOnInsufficientBalanceOrAllowance: false, }), - ).to.be.rejectedWith(constants.INVALID_OPCODE); + ).to.be.rejectedWith(constants.REVERT); }); it('should not change balances if an order is expired', async () => { -- cgit v1.2.3