diff options
-rw-r--r-- | src/0x.js.ts | 6 | ||||
-rw-r--r-- | src/web3_wrapper.ts | 3 | ||||
-rw-r--r-- | test/exchange_wrapper_test.ts | 33 | ||||
-rw-r--r-- | test/utils/fill_scenarios.ts | 12 |
4 files changed, 38 insertions, 16 deletions
diff --git a/src/0x.js.ts b/src/0x.js.ts index a613d5f48..6713ec885 100644 --- a/src/0x.js.ts +++ b/src/0x.js.ts @@ -124,6 +124,12 @@ export class ZeroEx { this.web3Wrapper.setDefaultAccount(account); } /** + * Gets default account for sending transactions. + */ + public getTransactionSenderAccount(): string { + return this.web3Wrapper.getDefaultAccount(); + } + /** * Computes the orderHash given the order parameters and returns it as a hex encoded string. */ public async getOrderHashHexAsync(order: Order): Promise<string> { diff --git a/src/web3_wrapper.ts b/src/web3_wrapper.ts index c1263222a..900c127d5 100644 --- a/src/web3_wrapper.ts +++ b/src/web3_wrapper.ts @@ -17,6 +17,9 @@ export class Web3Wrapper { public isAddress(address: string): boolean { return this.web3.isAddress(address); } + public getDefaultAccount(): string { + return this.web3.eth.defaultAccount; + } public setDefaultAccount(address: string): void { this.web3.eth.defaultAccount = address; } diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 77c78470b..716adb63a 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -108,16 +108,14 @@ describe('ExchangeWrapper', () => { let makerTokenAddress: string; let takerTokenAddress: string; let fillScenarios: FillScenarios; + let coinBase: string; + let makerAddress: string; let takerAddress: string; const fillTakerAmountInBaseUnits = new BigNumber(5); - const addressBySymbol: {[symbol: string]: string} = {}; const shouldCheckTransfer = false; before('fetch tokens', async () => { - takerAddress = userAddresses[1]; + [coinBase, makerAddress, takerAddress] = userAddresses; tokens = await zeroEx.tokenRegistry.getTokensAsync(); - _.forEach(tokens, token => { - addressBySymbol[token.symbol] = token.address; - }); const [makerToken, takerToken] = tokens; makerTokenAddress = makerToken.address; takerTokenAddress = takerToken.address; @@ -130,7 +128,7 @@ describe('ExchangeWrapper', () => { it('should throw when the fill amount is zero', async () => { const fillableAmount = new BigNumber(5); const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); const zeroFillAmount = new BigNumber(0); zeroEx.setTransactionSenderAccount(takerAddress); @@ -140,7 +138,7 @@ describe('ExchangeWrapper', () => { it('should throw when sender is not a taker', async () => { const fillableAmount = new BigNumber(5); const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer)) .to.be.rejectedWith(FillOrderValidationErrs.NOT_A_TAKER); @@ -149,7 +147,7 @@ describe('ExchangeWrapper', () => { const expirationInPast = new BigNumber(42); const fillableAmount = new BigNumber(5); const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, expirationInPast, + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, expirationInPast, ); zeroEx.setTransactionSenderAccount(takerAddress); expect(zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer)) @@ -158,7 +156,7 @@ describe('ExchangeWrapper', () => { it('should throw when taker balance is less than fill amount', async () => { const fillableAmount = new BigNumber(5); const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); zeroEx.setTransactionSenderAccount(takerAddress); const moreThanTheBalance = new BigNumber(6); @@ -170,18 +168,27 @@ describe('ExchangeWrapper', () => { it('should fill the valid order', async () => { const fillableAmount = new BigNumber(5); const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( - makerTokenAddress, takerTokenAddress, takerAddress, fillableAmount, + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); - zeroEx.setTransactionSenderAccount(takerAddress); + + expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress)) + .to.be.bignumber.equal(fillableAmount); + expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress)) + .to.be.bignumber.equal(0); expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress)) .to.be.bignumber.equal(0); expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) - .to.be.bignumber.equal(fillTakerAmountInBaseUnits); + .to.be.bignumber.equal(fillableAmount); + zeroEx.setTransactionSenderAccount(takerAddress); await zeroEx.exchange.fillOrderAsync(signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer); + expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress)) + .to.be.bignumber.equal(fillableAmount.minus(fillTakerAmountInBaseUnits)); + expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress)) + .to.be.bignumber.equal(fillTakerAmountInBaseUnits); expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress)) .to.be.bignumber.equal(fillTakerAmountInBaseUnits); expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) - .to.be.bignumber.equal(0); + .to.be.bignumber.equal(fillableAmount.minus(fillTakerAmountInBaseUnits)); }); }); }); diff --git a/test/utils/fill_scenarios.ts b/test/utils/fill_scenarios.ts index ac233ad50..26840bb0c 100644 --- a/test/utils/fill_scenarios.ts +++ b/test/utils/fill_scenarios.ts @@ -7,23 +7,29 @@ export class FillScenarios { private zeroEx: ZeroEx; private userAddresses: string[]; private tokens: Token[]; + private coinBase: string; constructor(zeroEx: ZeroEx, userAddresses: string[], tokens: Token[]) { this.zeroEx = zeroEx; this.userAddresses = userAddresses; this.tokens = tokens; + this.coinBase = userAddresses[0]; } public async createAFillableSignedOrderAsync(makerTokenAddress: string, takerTokenAddress: string, - takerAddress: string, fillableAmount: BigNumber.BigNumber, + makerAddress: string, takerAddress: string, + fillableAmount: BigNumber.BigNumber, expirationUnixTimestampSec?: BigNumber.BigNumber): Promise<SignedOrder> { - const [makerAddress] = this.userAddresses; + await this.zeroEx.token.transferAsync(makerTokenAddress, this.coinBase, makerAddress, fillableAmount); await this.zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, fillableAmount); - await this.zeroEx.token.transferAsync(takerTokenAddress, makerAddress, takerAddress, fillableAmount); + await this.zeroEx.token.transferAsync(takerTokenAddress, this.coinBase, takerAddress, fillableAmount); await this.zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, fillableAmount); + const transactionSenderAccount = this.zeroEx.getTransactionSenderAccount(); + this.zeroEx.setTransactionSenderAccount(makerAddress); const signedOrder = await orderFactory.createSignedOrderAsync(this.zeroEx, makerAddress, takerAddress, fillableAmount, makerTokenAddress, fillableAmount, takerTokenAddress, expirationUnixTimestampSec); + this.zeroEx.setTransactionSenderAccount(transactionSenderAccount); return signedOrder; } } |