diff options
author | Fabio Berger <me@fabioberger.com> | 2017-09-26 17:01:33 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-09-26 17:01:33 +0800 |
commit | 5e92ca039c593028694a1453b39e55c127e96ba5 (patch) | |
tree | b026864d4df60352af7a47d801ea5573d2a136cd /src/utils/order_validation_utils.ts | |
parent | 0a19a7e8d165a3df33e862d761492c9b4382ed24 (diff) | |
download | dexon-0x-contracts-5e92ca039c593028694a1453b39e55c127e96ba5.tar dexon-0x-contracts-5e92ca039c593028694a1453b39e55c127e96ba5.tar.gz dexon-0x-contracts-5e92ca039c593028694a1453b39e55c127e96ba5.tar.bz2 dexon-0x-contracts-5e92ca039c593028694a1453b39e55c127e96ba5.tar.lz dexon-0x-contracts-5e92ca039c593028694a1453b39e55c127e96ba5.tar.xz dexon-0x-contracts-5e92ca039c593028694a1453b39e55c127e96ba5.tar.zst dexon-0x-contracts-5e92ca039c593028694a1453b39e55c127e96ba5.zip |
Add validateOrderFillableThrowIfNotFillableAsync to public methods in order to validate orders in an orderbook without a specific taker in mind
Diffstat (limited to 'src/utils/order_validation_utils.ts')
-rw-r--r-- | src/utils/order_validation_utils.ts | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/utils/order_validation_utils.ts b/src/utils/order_validation_utils.ts index 815cd0115..27fc72dca 100644 --- a/src/utils/order_validation_utils.ts +++ b/src/utils/order_validation_utils.ts @@ -1,3 +1,4 @@ +import * as _ from 'lodash'; import {ExchangeContractErrs, SignedOrder, Order, ZeroExError} from '../types'; import {ZeroEx} from '../0x'; import {TokenWrapper} from '../contract_wrappers/token_wrapper'; @@ -12,6 +13,26 @@ export class OrderValidationUtils { this.tokenWrapper = tokenWrapper; this.exchangeWrapper = exchangeWrapper; } + public async validateOrderFillableThrowIfNotFillableAsync( + signedOrder: SignedOrder, zrxTokenAddress: string, expectedFillTakerTokenAmount?: BigNumber.BigNumber, + ): Promise<void> { + const orderHash = utils.getOrderHashHex(signedOrder); + const unavailableTakerTokenAmount = await this.exchangeWrapper.getUnavailableTakerAmountAsync(orderHash); + if (signedOrder.makerTokenAmount.eq(unavailableTakerTokenAmount)) { + throw new Error(ExchangeContractErrs.OrderRemainingFillAmountZero); + } + const currentUnixTimestampSec = utils.getCurrentUnixTimestamp(); + if (signedOrder.expirationUnixTimestampSec.lessThan(currentUnixTimestampSec)) { + throw new Error(ExchangeContractErrs.OrderFillExpired); + } + let fillTakerTokenAmount = signedOrder.takerTokenAmount.minus(unavailableTakerTokenAmount); + if (!_.isUndefined(expectedFillTakerTokenAmount)) { + fillTakerTokenAmount = expectedFillTakerTokenAmount; + } + await this.validateFillOrderMakerBalancesAllowancesThrowIfInvalidAsync( + signedOrder, fillTakerTokenAmount, zrxTokenAddress, + ); + } public async validateFillOrderThrowIfInvalidAsync(signedOrder: SignedOrder, fillTakerTokenAmount: BigNumber.BigNumber, takerAddress: string, |