diff options
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 18 | ||||
-rw-r--r-- | test/exchange_wrapper_test.ts | 25 |
2 files changed, 39 insertions, 4 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index e2ac07b55..3fb187de2 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -120,9 +120,9 @@ export class ExchangeWrapper extends ContractWrapper { ); this.throwErrorLogsAsErrors(response.logs); } - private async validateFillOrderAsync(signedOrder: SignedOrder, fillAmount: BigNumber.BigNumber, + private async validateFillOrderAsync(signedOrder: SignedOrder, fillTakerAmountInBaseUnits: BigNumber.BigNumber, senderAddress: string) { - if (fillAmount.eq(0)) { + if (fillTakerAmountInBaseUnits.eq(0)) { throw new Error(FillOrderValidationErrs.FILL_AMOUNT_IS_ZERO); } if (signedOrder.taker !== constants.NULL_ADDRESS && signedOrder.taker !== senderAddress) { @@ -138,12 +138,22 @@ export class ExchangeWrapper extends ContractWrapper { signedOrder.maker); const takerAllowance = await this.tokenWrapper.getProxyAllowanceAsync(signedOrder.takerTokenAddress, senderAddress); - if (fillAmount.greaterThan(takerBalance)) { + // How many taker tokens would you get for 1 maker token; + const exchangeRate = signedOrder.takerTokenAmount.div(signedOrder.makerTokenAmount); + const fillMakerAmountInBaseUnits = fillTakerAmountInBaseUnits.div(exchangeRate); + + if (fillTakerAmountInBaseUnits.greaterThan(takerBalance)) { throw new Error(FillOrderValidationErrs.NOT_ENOUGH_TAKER_BALANCE); } - if (fillAmount.greaterThan(takerAllowance)) { + if (fillTakerAmountInBaseUnits.greaterThan(takerAllowance)) { throw new Error(FillOrderValidationErrs.NOT_ENOUGH_TAKER_ALLOWANCE); } + if (fillMakerAmountInBaseUnits.greaterThan(makerBalance)) { + throw new Error(FillOrderValidationErrs.NOT_ENOUGH_MAKER_BALANCE); + } + if (fillMakerAmountInBaseUnits.greaterThan(makerAllowance)) { + throw new Error(FillOrderValidationErrs.NOT_ENOUGH_MAKER_ALLOWANCE); + } } private throwErrorLogsAsErrors(logs: ContractEvent[]): void { const errEvent = _.find(logs, {event: 'LogError'}); diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 0c1f552b6..66ba167cc 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -180,6 +180,31 @@ describe('ExchangeWrapper', () => { signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_TAKER_ALLOWANCE); }); + it('should throw when maker balance is less than maker fill amount', async () => { + const fillableAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, + ); + const lackingMakerBalance = new BigNumber(3); + await zeroEx.token.transferAsync(makerTokenAddress, makerAddress, coinBase, lackingMakerBalance); + zeroEx.setTransactionSenderAccount(takerAddress); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_BALANCE); + }); + it('should throw when maker allowance is less than maker fill amount', async () => { + const fillableAmount = new BigNumber(5); + const signedOrder = await fillScenarios.createAFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, + ); + const newAllowanceWhichIsLessThanFillAmount = fillTakerAmountInBaseUnits.minus(1); + await zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, + newAllowanceWhichIsLessThanFillAmount); + zeroEx.setTransactionSenderAccount(takerAddress); + return expect(zeroEx.exchange.fillOrderAsync( + signedOrder, fillTakerAmountInBaseUnits, shouldCheckTransfer, + )).to.be.rejectedWith(FillOrderValidationErrs.NOT_ENOUGH_MAKER_ALLOWANCE); + }); }); describe('successful fills', () => { it('should fill the valid order', async () => { |