diff options
author | Fabio Berger <me@fabioberger.com> | 2018-06-25 19:51:00 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-06-25 19:51:00 +0800 |
commit | ad67a6add54cc31f4b888dbbfe70fed75b743745 (patch) | |
tree | cfa4e8a74397c77b6ab5bf7a4e88fb53fa2a8000 /packages/order-utils | |
parent | 1353723b58715faf3dd10b173323dab015e2dd5b (diff) | |
download | dexon-sol-tools-ad67a6add54cc31f4b888dbbfe70fed75b743745.tar dexon-sol-tools-ad67a6add54cc31f4b888dbbfe70fed75b743745.tar.gz dexon-sol-tools-ad67a6add54cc31f4b888dbbfe70fed75b743745.tar.bz2 dexon-sol-tools-ad67a6add54cc31f4b888dbbfe70fed75b743745.tar.lz dexon-sol-tools-ad67a6add54cc31f4b888dbbfe70fed75b743745.tar.xz dexon-sol-tools-ad67a6add54cc31f4b888dbbfe70fed75b743745.tar.zst dexon-sol-tools-ad67a6add54cc31f4b888dbbfe70fed75b743745.zip |
Check revert reasons in declarative and combinatorial tests
Diffstat (limited to 'packages/order-utils')
-rw-r--r-- | packages/order-utils/src/order_validation_utils.ts | 113 |
1 files changed, 59 insertions, 54 deletions
diff --git a/packages/order-utils/src/order_validation_utils.ts b/packages/order-utils/src/order_validation_utils.ts index da5b37456..d2449b183 100644 --- a/packages/order-utils/src/order_validation_utils.ts +++ b/packages/order-utils/src/order_validation_utils.ts @@ -1,4 +1,5 @@ -import { ExchangeContractErrs, Order, SignedOrder } from '@0xproject/types'; +import { Order, SignedOrder } from '@0xproject/types'; +import { RevertReasons } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import { Provider } from 'ethereum-types'; import * as _ from 'lodash'; @@ -54,66 +55,70 @@ export class OrderValidationUtils { senderAddress: string, zrxAssetData: string, ): Promise<void> { - const fillMakerTokenAmount = utils.getPartialAmount( - fillTakerAssetAmount, - signedOrder.takerAssetAmount, - signedOrder.makerAssetAmount, - ); - await exchangeTradeEmulator.transferFromAsync( - signedOrder.makerAssetData, - signedOrder.makerAddress, - senderAddress, - fillMakerTokenAmount, - TradeSide.Maker, - TransferType.Trade, - ); - await exchangeTradeEmulator.transferFromAsync( - signedOrder.takerAssetData, - senderAddress, - signedOrder.makerAddress, - fillTakerAssetAmount, - TradeSide.Taker, - TransferType.Trade, - ); - const makerFeeAmount = utils.getPartialAmount( - fillTakerAssetAmount, - signedOrder.takerAssetAmount, - signedOrder.makerFee, - ); - await exchangeTradeEmulator.transferFromAsync( - zrxAssetData, - signedOrder.makerAddress, - signedOrder.feeRecipientAddress, - makerFeeAmount, - TradeSide.Maker, - TransferType.Fee, - ); - const takerFeeAmount = utils.getPartialAmount( - fillTakerAssetAmount, - signedOrder.takerAssetAmount, - signedOrder.takerFee, - ); - await exchangeTradeEmulator.transferFromAsync( - zrxAssetData, - senderAddress, - signedOrder.feeRecipientAddress, - takerFeeAmount, - TradeSide.Taker, - TransferType.Fee, - ); + try { + const fillMakerTokenAmount = utils.getPartialAmount( + fillTakerAssetAmount, + signedOrder.takerAssetAmount, + signedOrder.makerAssetAmount, + ); + await exchangeTradeEmulator.transferFromAsync( + signedOrder.makerAssetData, + signedOrder.makerAddress, + senderAddress, + fillMakerTokenAmount, + TradeSide.Maker, + TransferType.Trade, + ); + await exchangeTradeEmulator.transferFromAsync( + signedOrder.takerAssetData, + senderAddress, + signedOrder.makerAddress, + fillTakerAssetAmount, + TradeSide.Taker, + TransferType.Trade, + ); + const makerFeeAmount = utils.getPartialAmount( + fillTakerAssetAmount, + signedOrder.takerAssetAmount, + signedOrder.makerFee, + ); + await exchangeTradeEmulator.transferFromAsync( + zrxAssetData, + signedOrder.makerAddress, + signedOrder.feeRecipientAddress, + makerFeeAmount, + TradeSide.Maker, + TransferType.Fee, + ); + const takerFeeAmount = utils.getPartialAmount( + fillTakerAssetAmount, + signedOrder.takerAssetAmount, + signedOrder.takerFee, + ); + await exchangeTradeEmulator.transferFromAsync( + zrxAssetData, + senderAddress, + signedOrder.feeRecipientAddress, + takerFeeAmount, + TradeSide.Taker, + TransferType.Fee, + ); + } catch (err) { + throw new Error(RevertReasons.TransferFailed); + } } private static _validateRemainingFillAmountNotZeroOrThrow( takerAssetAmount: BigNumber, filledTakerTokenAmount: BigNumber, ): void { if (takerAssetAmount.eq(filledTakerTokenAmount)) { - throw new Error(ExchangeContractErrs.OrderRemainingFillAmountZero); + throw new Error(RevertReasons.OrderUnfillable); } } private static _validateOrderNotExpiredOrThrow(expirationTimeSeconds: BigNumber): void { const currentUnixTimestampSec = utils.getCurrentUnixTimestampSec(); if (expirationTimeSeconds.lessThan(currentUnixTimestampSec)) { - throw new Error(ExchangeContractErrs.OrderFillExpired); + throw new Error(RevertReasons.OrderUnfillable); } } constructor(orderFilledCancelledFetcher: AbstractOrderFilledCancelledFetcher) { @@ -153,7 +158,7 @@ export class OrderValidationUtils { zrxAssetData: string, ): Promise<BigNumber> { if (fillTakerAssetAmount.eq(0)) { - throw new Error(ExchangeContractErrs.OrderFillAmountZero); + throw new Error(RevertReasons.TakerAssetFillAmount); } const orderHash = orderHashUtils.getOrderHashHex(signedOrder); const isValid = await isValidSignatureAsync( @@ -171,7 +176,7 @@ export class OrderValidationUtils { filledTakerTokenAmount, ); if (signedOrder.takerAddress !== constants.NULL_ADDRESS && signedOrder.takerAddress !== takerAddress) { - throw new Error(ExchangeContractErrs.TransactionSenderIsNotFillOrderTaker); + throw new Error(RevertReasons.InvalidTaker); } OrderValidationUtils._validateOrderNotExpiredOrThrow(signedOrder.expirationTimeSeconds); const remainingTakerTokenAmount = signedOrder.takerAssetAmount.minus(filledTakerTokenAmount); @@ -192,7 +197,7 @@ export class OrderValidationUtils { signedOrder.makerAssetAmount, ); if (wouldRoundingErrorOccur) { - throw new Error(ExchangeContractErrs.OrderFillRoundingError); + throw new Error(RevertReasons.RoundingError); } return filledTakerTokenAmount; } @@ -213,7 +218,7 @@ export class OrderValidationUtils { zrxAssetData, ); if (filledTakerTokenAmount !== fillTakerAssetAmount) { - throw new Error(ExchangeContractErrs.InsufficientRemainingFillAmount); + throw new Error(RevertReasons.OrderUnfillable); } } } |