aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJacob Evans <jacob@dekz.net>2017-11-20 07:45:01 +0800
committerJacob Evans <jacob@dekz.net>2017-11-20 07:45:01 +0800
commit7b373e29eae61b7b6e97ad180f6a6614b3242435 (patch)
tree950d50c556a9113fdd40ecb3c034d7ef33a8639e
parent94ce7a54c3e352e671c0ccdde09535f85b05d353 (diff)
downloaddexon-0x-contracts-7b373e29eae61b7b6e97ad180f6a6614b3242435.tar.gz
dexon-0x-contracts-7b373e29eae61b7b6e97ad180f6a6614b3242435.tar.zst
dexon-0x-contracts-7b373e29eae61b7b6e97ad180f6a6614b3242435.zip
Split into Pooled and non-pooled
-rw-r--r--packages/0x.js/src/utils/order_state_utils.ts62
1 files changed, 50 insertions, 12 deletions
diff --git a/packages/0x.js/src/utils/order_state_utils.ts b/packages/0x.js/src/utils/order_state_utils.ts
index 1fe5e3ae4..437ff1d82 100644
--- a/packages/0x.js/src/utils/order_state_utils.ts
+++ b/packages/0x.js/src/utils/order_state_utils.ts
@@ -17,6 +17,7 @@ import {utils} from '../utils/utils';
import {constants} from '../utils/constants';
import {OrderFilledCancelledLazyStore} from '../stores/order_filled_cancelled_lazy_store';
import {BalanceAndProxyAllowanceLazyStore} from '../stores/balance_proxy_allowance_lazy_store';
+import { TokenTransferProxyWrapper } from '../contract_wrappers/token_transfer_proxy_wrapper';
const ACCEPTABLE_RELATIVE_ROUNDING_ERROR = 0.0001;
@@ -84,16 +85,16 @@ export class OrderStateUtils {
const transferrableFeeTokenAmount = BigNumber.min([makerFeeProxyAllowance, makerFeeBalance]);
let remainingFillableMakerTokenAmount;
- if (signedOrder.makerFee.isZero() ||
- (transferrableFeeTokenAmount.greaterThanOrEqualTo(remainingFeeTokenAmount) &&
- transferrableMakerTokenAmount.greaterThanOrEqualTo(remainingMakerTokenAmount) &&
- signedOrder.makerTokenAddress !== zrxTokenAddress)) {
- remainingFillableMakerTokenAmount = remainingMakerTokenAmount;
+ if ((signedOrder.makerTokenAddress !== zrxTokenAddress || signedOrder.makerFee.isZero())) {
+ remainingFillableMakerTokenAmount = this.calculateFillableMakerTokenAmount(
+ transferrableMakerTokenAmount, transferrableFeeTokenAmount, remainingMakerTokenAmount,
+ remainingFeeTokenAmount, totalMakerTokenAmount, signedOrder.makerFee, signedOrder.makerTokenAddress,
+ zrxTokenAddress);
} else {
- remainingFillableMakerTokenAmount = this.calculatePartiallyFillableMakerTokenAmount(
- transferrableMakerTokenAmount, transferrableFeeTokenAmount, remainingMakerTokenAmount,
- remainingFeeTokenAmount, totalMakerTokenAmount, signedOrder.makerFee, signedOrder.makerTokenAddress,
- zrxTokenAddress);
+ remainingFillableMakerTokenAmount = this.calculatePooledFillableMakerTokenAmount(
+ transferrableMakerTokenAmount, transferrableFeeTokenAmount, remainingMakerTokenAmount,
+ remainingFeeTokenAmount, totalMakerTokenAmount, signedOrder.makerFee, signedOrder.makerTokenAddress,
+ zrxTokenAddress);
}
const remainingFillableTakerTokenAmount = remainingFillableMakerTokenAmount
@@ -111,8 +112,44 @@ export class OrderStateUtils {
};
return orderRelevantState;
}
- private calculatePartiallyFillableMakerTokenAmount(makerTransferrableAmount: BigNumber, makerFeeTransferrableAmount: BigNumber,
- remainingMakerAmount: BigNumber, remainingMakerFeeAmount: BigNumber,
+ private calculateFillableMakerTokenAmount(makerTransferrableAmount: BigNumber,
+ makerFeeTransferrableAmount: BigNumber,
+ remainingMakerAmount: BigNumber,
+ remainingMakerFeeAmount: BigNumber,
+ totalMakerAmount: BigNumber, makerFeeAmount: BigNumber,
+ makerTokenAddress: string, zrxTokenAddress: string): BigNumber {
+ if (makerFeeAmount.isZero()) {
+ return BigNumber.min(remainingMakerAmount, makerTransferrableAmount);
+ } else if (makerTransferrableAmount.gte(remainingMakerAmount) &&
+ makerFeeTransferrableAmount.gte(remainingMakerFeeAmount)) {
+ return makerTransferrableAmount;
+ } else {
+ return this.calculatePartiallyFillableMakerTokenAmount(
+ makerTransferrableAmount, makerFeeTransferrableAmount, remainingMakerAmount,
+ remainingMakerFeeAmount, totalMakerAmount, makerFeeAmount, makerTokenAddress,
+ zrxTokenAddress);
+ }
+ }
+ private calculatePooledFillableMakerTokenAmount(makerTransferrableAmount: BigNumber,
+ makerFeeTransferrableAmount: BigNumber,
+ remainingMakerAmount: BigNumber,
+ remainingMakerFeeAmount: BigNumber,
+ totalMakerAmount: BigNumber, makerFeeAmount: BigNumber,
+ makerTokenAddress: string, zrxTokenAddress: string): BigNumber {
+ if (makerTransferrableAmount.plus(makerFeeTransferrableAmount).gte(
+ remainingMakerAmount.plus(remainingMakerFeeAmount))) {
+ return remainingMakerAmount;
+ } else {
+ return this.calculatePartiallyFillableMakerTokenAmount(
+ makerTransferrableAmount, makerFeeTransferrableAmount, remainingMakerAmount,
+ remainingMakerFeeAmount, totalMakerAmount, makerFeeAmount, makerTokenAddress,
+ zrxTokenAddress);
+ }
+ }
+ private calculatePartiallyFillableMakerTokenAmount(makerTransferrableAmount: BigNumber,
+ makerFeeTransferrableAmount: BigNumber,
+ remainingMakerAmount: BigNumber,
+ remainingMakerFeeAmount: BigNumber,
totalMakerAmount: BigNumber, makerFeeAmount: BigNumber,
makerTokenAddress: string, zrxTokenAddress: string): BigNumber {
const orderToFeeRatio = totalMakerAmount.dividedToIntegerBy(makerFeeAmount);
@@ -121,7 +158,8 @@ export class OrderStateUtils {
if (makerTokenAddress === zrxTokenAddress) {
const totalFeeTokenPool = makerTransferrableAmount.plus(makerFeeTransferrableAmount);
fillableTimesInMakerToken = totalFeeTokenPool.dividedToIntegerBy(
- orderToFeeRatio.plus(ZeroEx.toBaseUnitAmount(new BigNumber(1), 18)));
+ orderToFeeRatio.plus(
+ ZeroEx.toBaseUnitAmount(new BigNumber(1), 18)));
}
return BigNumber.min(fillableTimesInMakerToken.times(orderToFeeRatio),