From c42877327e59f983a83c7221f1bec5b31addf5a7 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 18:52:56 +0200 Subject: Change test name --- test/exchange_wrapper_test.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 2746c9e46..4847d10cf 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -154,15 +154,14 @@ describe('ExchangeWrapper', () => { expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); }); - it('should throw when not enough balance', async () => { + it('should throw when taker balance is less than fill amount', async () => { const makerAmount = 10; const takerAmount = 10; const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); zeroEx.setDefaultAccount(taker); const moreThanTheBalance = new BigNumber(6); - const checkTransfer = true; - expect(zeroEx.exchange.fillOrderAsync(signedOrder, moreThanTheBalance, checkTransfer)) + expect(zeroEx.exchange.fillOrderAsync(signedOrder, moreThanTheBalance, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_BALANCE); }); }); -- cgit From a1be87058536f5ab10c68c570c1b9994defe52b1 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 1 Jun 2017 19:47:22 +0200 Subject: Create a FillsScenario utils module and use it in the fillOrder tests --- src/0x.js.ts | 2 +- src/contract_wrappers/exchange_wrapper.ts | 10 +++-- src/types.ts | 4 ++ test/exchange_wrapper_test.ts | 64 +++++++++++++------------------ test/utils/fill_scenarios.ts | 29 ++++++++++++++ 5 files changed, 67 insertions(+), 42 deletions(-) create mode 100644 test/utils/fill_scenarios.ts diff --git a/src/0x.js.ts b/src/0x.js.ts index a95bc3384..a613d5f48 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -120,7 +120,7 @@ export class ZeroEx { /** * Sets default account for sending transactions. */ - public setDefaultAccount(account: string): void { + public setTransactionSenderAccount(account: string): void { this.web3Wrapper.setDefaultAccount(account); } /** diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index dc2c95d4e..eace55f4d 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -74,10 +74,10 @@ export class ExchangeWrapper extends ContractWrapper { assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer); const senderAddress = await this.web3Wrapper.getSenderAddressOrThrowAsync(); - const exchangeInstance = await this.getExchangeContractAsync(); - await this.validateFillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, senderAddress); + const exchangeInstance = await this.getExchangeContractAsync(); + const orderAddresses: OrderAddresses = [ signedOrder.maker, signedOrder.taker, @@ -120,7 +120,8 @@ export class ExchangeWrapper extends ContractWrapper { ); this.throwErrorLogsAsErrors(response.logs); } - private async validateFillOrderAsync(signedOrder: SignedOrder, fillAmount: BigNumber.BigNumber, senderAddress: string) { + private async validateFillOrderAsync(signedOrder: SignedOrder, fillAmount: BigNumber.BigNumber, + senderAddress: string) { if (fillAmount.eq(0)) { throw new Error(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO); } @@ -130,7 +131,8 @@ export class ExchangeWrapper extends ContractWrapper { if (signedOrder.expirationUnixTimestampSec.lessThan(Date.now() / 1000)) { throw new Error(FillOrderValidationErrs.EXPIRED); } - const makerBalance = await this.tokenWrapper.getBalanceAsync(signedOrder.makerTokenAddress, signedOrder.maker); + const makerBalance = await this.tokenWrapper.getBalanceAsync(signedOrder.makerTokenAddress, + signedOrder.maker); const takerBalance = await this.tokenWrapper.getBalanceAsync(signedOrder.takerTokenAddress, senderAddress); const makerAllowance = await this.tokenWrapper.getProxyAllowanceAsync(signedOrder.makerTokenAddress, signedOrder.maker); diff --git a/src/types.ts b/src/types.ts index ad5d8fb17..f80f98dc4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -138,3 +138,7 @@ export interface TxOpts { from: string; gas?: number; } + +export interface TokenAddressBySymbol { + [symbol: string]: string; +} diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 4847d10cf..ad6e5496e 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -11,6 +11,7 @@ import {ZeroEx} from '../src/0x.js'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import {orderFactory} from './utils/order_factory'; import {FillOrderValidationErrs, Token} from '../src/types'; +import {FillScenarios} from './utils/fill_scenarios'; chai.use(dirtyChai); chai.use(ChaiBigNumber()); @@ -104,62 +105,52 @@ describe('ExchangeWrapper', () => { }); describe('#fillOrderAsync', () => { let tokens: Token[]; + let fillScenarios: FillScenarios; + let takerAddress: string; const fillTakerAmountInBaseUnits = new BigNumber(5); - let maker: string; - let taker: string; const addressBySymbol: {[symbol: string]: string} = {}; const shouldCheckTransfer = false; before('fetch tokens', async () => { + takerAddress = userAddresses[1]; tokens = await zeroEx.tokenRegistry.getTokensAsync(); _.forEach(tokens, token => { addressBySymbol[token.symbol] = token.address; }); - }); - beforeEach('setup', async () => { - maker = userAddresses[0]; - taker = userAddresses[1]; - await zeroEx.token.setProxyAllowanceAsync(addressBySymbol.MLN, maker, new BigNumber(5)); - await zeroEx.token.transferAsync(addressBySymbol.GNT, maker, taker, new BigNumber(5)); - await zeroEx.token.setProxyAllowanceAsync(addressBySymbol.GNT, taker, new BigNumber(5)); + fillScenarios = new FillScenarios(zeroEx, userAddresses, tokens); }); afterEach('reset default account', () => { - zeroEx.setDefaultAccount(userAddresses[0]); + zeroEx.setTransactionSenderAccount(userAddresses[0]); }); describe('failed fills', () => { it('should throw when the fill amount is zero', async () => { - const makerAmount = 5; - const takerAmount = 5; - const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, - makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); + const fillableAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, fillableAmount); const zeroFillAmount = new BigNumber(0); - zeroEx.setDefaultAccount(taker); + zeroEx.setTransactionSenderAccount(takerAddress); expect(zeroEx.exchange.fillOrderAsync(signedOrder, zeroFillAmount, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO); }); it('should throw when sender is not a taker', async () => { - const makerAmount = 5; - const takerAmount = 5; - const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, - makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); + const fillableAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, fillableAmount); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); }); it('should throw when order is expired', async () => { - const timestampInThePast = new BigNumber(42); - const makerAmount = 5; - const takerAmount = 5; - const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, - makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT, timestampInThePast); - zeroEx.setDefaultAccount(taker); + const expirationInPast = new BigNumber(42); + const fillableAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, + fillableAmount, + expirationInPast); + zeroEx.setTransactionSenderAccount(takerAddress); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); }); it('should throw when taker balance is less than fill amount', async () => { - const makerAmount = 10; - const takerAmount = 10; - const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, - makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); - zeroEx.setDefaultAccount(taker); + const fillableAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, + fillableAmount); + zeroEx.setTransactionSenderAccount(takerAddress); const moreThanTheBalance = new BigNumber(6); expect(zeroEx.exchange.fillOrderAsync(signedOrder, moreThanTheBalance, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_BALANCE); @@ -167,15 +158,14 @@ describe('ExchangeWrapper', () => { }); describe('successful fills', () => { it('should fill the valid order', async () => { - const makerAmount = 5; - const takerAmount = 5; - const signedOrder = await orderFactory.createSignedOrderAsync(zeroEx, maker, taker, - makerAmount, addressBySymbol.MLN, takerAmount, addressBySymbol.GNT); - zeroEx.setDefaultAccount(taker); + const fillableAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, + fillableAmount); + zeroEx.setTransactionSenderAccount(takerAddress); await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer); - expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, taker)) + expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, takerAddress)) .to.be.bignumber.equal(fillTakerAmountInBaseUnits); - expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, taker)) + expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, takerAddress)) .to.be.bignumber.equal(0); }); }); diff --git a/test/utils/fill_scenarios.ts b/test/utils/fill_scenarios.ts new file mode 100644 index 000000000..8447c8c15 --- /dev/null +++ b/test/utils/fill_scenarios.ts @@ -0,0 +1,29 @@ +import * as BigNumber from 'bignumber.js'; +import {ZeroEx} from '../../src/0x.js'; +import {Token, SignedOrder} from '../../src/types'; +import {orderFactory} from '../utils/order_factory'; + +export class FillScenarios { + private zeroEx: ZeroEx; + private userAddresses: string[]; + private tokens: Token[]; + constructor(zeroEx: ZeroEx, userAddresses: string[], tokens: Token[]) { + this.zeroEx = zeroEx; + this.userAddresses = userAddresses; + this.tokens = tokens; + } + public async createAFillableSignedOrderAsync(takerAddress: string, fillableAmount: BigNumber.BigNumber, + expirationUnixTimestampSec?: BigNumber.BigNumber): + Promise { + const [makerAddress] = this.userAddresses; + const [makerToken, takerToken] = this.tokens; + await this.zeroEx.token.setProxyAllowanceAsync(makerToken.address, makerAddress, fillableAmount); + await this.zeroEx.token.transferAsync(takerToken.address, makerAddress, takerAddress, fillableAmount); + await this.zeroEx.token.setProxyAllowanceAsync(takerToken.address, takerAddress, fillableAmount); + + const signedOrder = await orderFactory.createSignedOrderAsync(this.zeroEx, makerAddress, + takerAddress, fillableAmount, makerToken.address, fillableAmount, takerToken.address, + expirationUnixTimestampSec); + return signedOrder; + } +} -- cgit From 511fa537ad3d2a669d5d680efb26012ca1b9c062 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 20:04:12 +0200 Subject: Work --- test/exchange_wrapper_test.ts | 33 ++++++++++++++++++++++++--------- test/utils/fill_scenarios.ts | 12 ++++++------ 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index ad6e5496e..debc8cced 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -105,6 +105,8 @@ describe('ExchangeWrapper', () => { }); describe('#fillOrderAsync', () => { let tokens: Token[]; + let makerTokenAddress: string; + let takerTokenAddress: string; let fillScenarios: FillScenarios; let takerAddress: string; const fillTakerAmountInBaseUnits = new BigNumber(5); @@ -116,6 +118,9 @@ describe('ExchangeWrapper', () => { _.forEach(tokens, token => { addressBySymbol[token.symbol] = token.address; }); + const [makerToken, takerToken] = tokens; + makerTokenAddress = makerToken.address; + takerTokenAddress = takerToken.address; fillScenarios = new FillScenarios(zeroEx, userAddresses, tokens); }); afterEach('reset default account', () => { @@ -124,7 +129,9 @@ describe('ExchangeWrapper', () => { describe('failed fills', () => { it('should throw when the fill amount is zero', async () => { const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, fillableAmount); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, + ); const zeroFillAmount = new BigNumber(0); zeroEx.setTransactionSenderAccount(takerAddress); expect(zeroEx.exchange.fillOrderAsync(signedOrder, zeroFillAmount, shouldCheckTransfer)) @@ -132,24 +139,27 @@ describe('ExchangeWrapper', () => { }); it('should throw when sender is not a taker', async () => { const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, fillableAmount); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, + ); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); }); it('should throw when order is expired', async () => { const expirationInPast = new BigNumber(42); const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, - fillableAmount, - expirationInPast); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, expirationInPast, + ); zeroEx.setTransactionSenderAccount(takerAddress); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.EXPIRED); }); it('should throw when taker balance is less than fill amount', async () => { const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, - fillableAmount); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, + ); zeroEx.setTransactionSenderAccount(takerAddress); const moreThanTheBalance = new BigNumber(6); expect(zeroEx.exchange.fillOrderAsync(signedOrder, moreThanTheBalance, shouldCheckTransfer)) @@ -159,9 +169,14 @@ describe('ExchangeWrapper', () => { describe('successful fills', () => { it('should fill the valid order', async () => { const fillableAmount = new BigNumber(5); - const signedOrder = await fillScenarios.createAFillableSignedOrderAsync(takerAddress, - fillableAmount); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, + ); zeroEx.setTransactionSenderAccount(takerAddress); + expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress)) + .to.be.bignumber.equal(0); + expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) + .to.be.bignumber.equal(fillTakerAmountInBaseUnits); await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer); expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, takerAddress)) .to.be.bignumber.equal(fillTakerAmountInBaseUnits); diff --git a/test/utils/fill_scenarios.ts b/test/utils/fill_scenarios.ts index 8447c8c15..ac233ad50 100644 --- a/test/utils/fill_scenarios.ts +++ b/test/utils/fill_scenarios.ts @@ -12,17 +12,17 @@ export class FillScenarios { this.userAddresses = userAddresses; this.tokens = tokens; } - public async createAFillableSignedOrderAsync(takerAddress: string, fillableAmount: BigNumber.BigNumber, + public async createAFillableSignedOrderAsync(makerTokenAddress: string, takerTokenAddress: string, + takerAddress: string, fillableAmount: BigNumber.BigNumber, expirationUnixTimestampSec?: BigNumber.BigNumber): Promise { const [makerAddress] = this.userAddresses; - const [makerToken, takerToken] = this.tokens; - await this.zeroEx.token.setProxyAllowanceAsync(makerToken.address, makerAddress, fillableAmount); - await this.zeroEx.token.transferAsync(takerToken.address, makerAddress, takerAddress, fillableAmount); - await this.zeroEx.token.setProxyAllowanceAsync(takerToken.address, takerAddress, fillableAmount); + await this.zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, fillableAmount); + await this.zeroEx.token.transferAsync(takerTokenAddress, makerAddress, takerAddress, fillableAmount); + await this.zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, fillableAmount); const signedOrder = await orderFactory.createSignedOrderAsync(this.zeroEx, makerAddress, - takerAddress, fillableAmount, makerToken.address, fillableAmount, takerToken.address, + takerAddress, fillableAmount, makerTokenAddress, fillableAmount, takerTokenAddress, expirationUnixTimestampSec); return signedOrder; } -- cgit From f663a15c9b000d639bd0d58d0c177e6de9d47346 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 1 Jun 2017 21:12:13 +0200 Subject: Fix tests --- test/exchange_wrapper_test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index debc8cced..77c78470b 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -178,9 +178,9 @@ describe('ExchangeWrapper', () => { expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) .to.be.bignumber.equal(fillTakerAmountInBaseUnits); await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer); - expect(await zeroEx.token.getBalanceAsync(addressBySymbol.MLN, takerAddress)) + expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress)) .to.be.bignumber.equal(fillTakerAmountInBaseUnits); - expect(await zeroEx.token.getBalanceAsync(addressBySymbol.GNT, takerAddress)) + expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) .to.be.bignumber.equal(0); }); }); -- cgit