aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-11-13 08:39:24 +0800
committerGitHub <noreply@github.com>2017-11-13 08:39:24 +0800
commit6becf22a2f752ef7c34ce1b423efd51773cc5fde (patch)
tree809556ebee179b3f4012870ae7353e3b4abcd520
parent6daf70b745b561f9a767886e2b40891da7ebc184 (diff)
parent32246fd26bc71b09818491d513a380a8bd85fdec (diff)
downloaddexon-sol-tools-6becf22a2f752ef7c34ce1b423efd51773cc5fde.tar
dexon-sol-tools-6becf22a2f752ef7c34ce1b423efd51773cc5fde.tar.gz
dexon-sol-tools-6becf22a2f752ef7c34ce1b423efd51773cc5fde.tar.bz2
dexon-sol-tools-6becf22a2f752ef7c34ce1b423efd51773cc5fde.tar.lz
dexon-sol-tools-6becf22a2f752ef7c34ce1b423efd51773cc5fde.tar.xz
dexon-sol-tools-6becf22a2f752ef7c34ce1b423efd51773cc5fde.tar.zst
dexon-sol-tools-6becf22a2f752ef7c34ce1b423efd51773cc5fde.zip
Merge pull request #210 from dekz/orderWatcherRemaining
Calculate the remaining order amount in maker units
-rw-r--r--src/types.ts1
-rw-r--r--src/utils/order_state_utils.ts11
-rw-r--r--test/order_state_watcher_test.ts82
3 files changed, 94 insertions, 0 deletions
diff --git a/src/types.ts b/src/types.ts
index 160b71fda..a366fc31e 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -493,6 +493,7 @@ export interface OrderRelevantState {
makerFeeProxyAllowance: BigNumber;
filledTakerTokenAmount: BigNumber;
canceledTakerTokenAmount: BigNumber;
+ remainingFillableMakerTokenAmount: BigNumber;
}
export interface OrderStateValid {
diff --git a/src/utils/order_state_utils.ts b/src/utils/order_state_utils.ts
index 2a5becf9a..36a4b68d6 100644
--- a/src/utils/order_state_utils.ts
+++ b/src/utils/order_state_utils.ts
@@ -60,6 +60,16 @@ export class OrderStateUtils {
);
const filledTakerTokenAmount = await this.exchangeWrapper.getFilledTakerAmountAsync(orderHash, methodOpts);
const canceledTakerTokenAmount = await this.exchangeWrapper.getCanceledTakerAmountAsync(orderHash, methodOpts);
+ const unavailableTakerTokenAmount =
+ await this.exchangeWrapper.getUnavailableTakerAmountAsync(orderHash, methodOpts);
+ const totalMakerTokenAmount = signedOrder.makerTokenAmount;
+ const totalTakerTokenAmount = signedOrder.takerTokenAmount;
+ const remainingTakerTokenAmount = totalTakerTokenAmount.minus(unavailableTakerTokenAmount);
+ const remainingMakerTokenAmount = remainingTakerTokenAmount.times(totalMakerTokenAmount)
+ .dividedToIntegerBy(totalTakerTokenAmount);
+ const fillableMakerTokenAmount = BigNumber.min([makerProxyAllowance, makerBalance]);
+ const remainingFillableMakerTokenAmount = BigNumber.min(fillableMakerTokenAmount, remainingMakerTokenAmount);
+ // TODO: Handle edge case where maker token is ZRX with fee
const orderRelevantState = {
makerBalance,
makerProxyAllowance,
@@ -67,6 +77,7 @@ export class OrderStateUtils {
makerFeeProxyAllowance,
filledTakerTokenAmount,
canceledTakerTokenAmount,
+ remainingFillableMakerTokenAmount,
};
return orderRelevantState;
}
diff --git a/test/order_state_watcher_test.ts b/test/order_state_watcher_test.ts
index d8ac0af49..41f938584 100644
--- a/test/order_state_watcher_test.ts
+++ b/test/order_state_watcher_test.ts
@@ -215,6 +215,9 @@ describe('OrderStateWatcher', () => {
expect(validOrderState.orderHash).to.be.equal(orderHash);
const orderRelevantState = validOrderState.orderRelevantState;
const remainingMakerBalance = makerBalance.sub(fillAmountInBaseUnits);
+ const remainingFillable = fillableAmount.minus(fillAmountInBaseUnits);
+ expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
+ remainingFillable);
expect(orderRelevantState.makerBalance).to.be.bignumber.equal(remainingMakerBalance);
if (eventCount === 2) {
done();
@@ -227,6 +230,85 @@ describe('OrderStateWatcher', () => {
);
})().catch(done);
});
+ describe('remainingFillableMakerTokenAmount', () => {
+ it('should calculate correct remaining fillable', (done: DoneCallback) => {
+ (async () => {
+ const takerFillableAmount = new BigNumber(10);
+ const makerFillableAmount = new BigNumber(20);
+ signedOrder = await fillScenarios.createAsymmetricFillableSignedOrderAsync(
+ makerToken.address, takerToken.address, maker, taker, makerFillableAmount, takerFillableAmount);
+ const makerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, maker);
+ const takerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, taker);
+ const fillAmountInBaseUnits = new BigNumber(2);
+ const orderHash = ZeroEx.getOrderHashHex(signedOrder);
+ zeroEx.orderStateWatcher.addOrder(signedOrder);
+ let eventCount = 0;
+ const callback = reportCallbackErrors(done)((orderState: OrderState) => {
+ eventCount++;
+ expect(orderState.isValid).to.be.true();
+ const validOrderState = orderState as OrderStateValid;
+ expect(validOrderState.orderHash).to.be.equal(orderHash);
+ const orderRelevantState = validOrderState.orderRelevantState;
+ expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
+ new BigNumber(16));
+ if (eventCount === 2) {
+ done();
+ }
+ });
+ zeroEx.orderStateWatcher.subscribe(callback);
+ const shouldThrowOnInsufficientBalanceOrAllowance = true;
+ await zeroEx.exchange.fillOrderAsync(
+ signedOrder, fillAmountInBaseUnits, shouldThrowOnInsufficientBalanceOrAllowance, taker,
+ );
+ })().catch(done);
+ });
+ it('should equal approved amount when approved amount is lowest', (done: DoneCallback) => {
+ (async () => {
+ signedOrder = await fillScenarios.createFillableSignedOrderAsync(
+ makerToken.address, takerToken.address, maker, taker, fillableAmount,
+ );
+
+ const makerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, maker);
+
+ const changedMakerApprovalAmount = new BigNumber(3);
+ zeroEx.orderStateWatcher.addOrder(signedOrder);
+
+ const callback = reportCallbackErrors(done)((orderState: OrderState) => {
+ const validOrderState = orderState as OrderStateValid;
+ const orderRelevantState = validOrderState.orderRelevantState;
+ expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
+ changedMakerApprovalAmount);
+ done();
+ });
+ zeroEx.orderStateWatcher.subscribe(callback);
+ await zeroEx.token.setProxyAllowanceAsync(makerToken.address, maker, changedMakerApprovalAmount);
+ })().catch(done);
+ });
+ it('should equal balance amount when balance amount is lowest', (done: DoneCallback) => {
+ (async () => {
+ signedOrder = await fillScenarios.createFillableSignedOrderAsync(
+ makerToken.address, takerToken.address, maker, taker, fillableAmount,
+ );
+
+ const makerBalance = await zeroEx.token.getBalanceAsync(makerToken.address, maker);
+
+ const remainingAmount = new BigNumber(1);
+ const transferAmount = makerBalance.sub(remainingAmount);
+ zeroEx.orderStateWatcher.addOrder(signedOrder);
+
+ const callback = reportCallbackErrors(done)((orderState: OrderState) => {
+ const validOrderState = orderState as OrderStateValid;
+ const orderRelevantState = validOrderState.orderRelevantState;
+ expect(orderRelevantState.remainingFillableMakerTokenAmount).to.be.bignumber.equal(
+ remainingAmount);
+ done();
+ });
+ zeroEx.orderStateWatcher.subscribe(callback);
+ await zeroEx.token.transferAsync(
+ makerToken.address, maker, ZeroEx.NULL_ADDRESS, transferAmount);
+ })().catch(done);
+ });
+ });
it('should emit orderStateInvalid when watched order cancelled', (done: DoneCallback) => {
(async () => {
signedOrder = await fillScenarios.createFillableSignedOrderAsync(