From 42c61ecda5c29018b5984fb377eb9a3ba5cb1c86 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 21 Jun 2017 17:48:03 +0200 Subject: Return filledAmount from fillOrderAsync --- src/contract_wrappers/exchange_wrapper.ts | 7 ++++++- test/exchange_wrapper_test.ts | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 6d42dc110..1044362a2 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -27,6 +27,7 @@ import { OrderCancellationRequest, OrderFillRequest, LogErrorContractEventArgs, + LogFillContractEventArgs, } from '../types'; import {assert} from '../utils/assert'; import {utils} from '../utils/utils'; @@ -144,10 +145,11 @@ export class ExchangeWrapper extends ContractWrapper { * execution the tokens cannot be transferred. * @param takerAddress The user Ethereum address who would like to fill this order. * Must be available via the supplied Web3.Provider passed to 0x.js. + * @return The amount of the order (in taker tokens baseUnits) that was filled. */ @decorators.contractCallErrorHandler public async fillOrderAsync(signedOrder: SignedOrder, takerTokenFillAmount: BigNumber.BigNumber, - shouldCheckTransfer: boolean, takerAddress: string): Promise { + shouldCheckTransfer: boolean, takerAddress: string): Promise { assert.doesConformToSchema('signedOrder', signedOrder, signedOrderSchema); assert.isBigNumber('takerTokenFillAmount', takerTokenFillAmount); assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer); @@ -184,6 +186,9 @@ export class ExchangeWrapper extends ContractWrapper { }, ); this._throwErrorLogsAsErrors(response.logs); + const logFillArgs = response.logs[0].args as LogFillContractEventArgs; + const filledAmount = new BigNumber(logFillArgs.filledValueT); + return filledAmount; } /** * Sequentially and atomically fills signedOrders up to the specified takerTokenFillAmount. diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index e4b2e3f86..6425e308a 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -339,6 +339,24 @@ describe('ExchangeWrapper', () => { expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress)) .to.be.bignumber.equal(fillableAmount.minus(partialFillAmount)); }); + it('should return filled amount', async () => { + const signedOrder = await fillScenarios.createFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, + ); + const partialFillAmount = new BigNumber(3); + await zeroEx.exchange.fillOrderAsync( + signedOrder, partialFillAmount, shouldCheckTransfer, takerAddress); + const missingBalance = new BigNumber(1); + const totalBalance = partialFillAmount.plus(missingBalance); + await zeroEx.token.transferAsync(takerTokenAddress, coinbase, takerAddress, missingBalance); + await zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, totalBalance); + await zeroEx.token.transferAsync(makerTokenAddress, coinbase, makerAddress, missingBalance); + await zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, totalBalance); + const remainingFillAmount = fillableAmount.minus(partialFillAmount); + const filledAmount = await zeroEx.exchange.fillOrderAsync( + signedOrder, partialFillAmount, shouldCheckTransfer, takerAddress); + expect(filledAmount).to.be.bignumber.equal(remainingFillAmount); + }); it('should fill the valid orders with fees', async () => { const makerFee = new BigNumber(1); const takerFee = new BigNumber(2); -- cgit v1.2.3 From 7b471858fa78d6f4f7116c198490a83f6e1ddd35 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 22 Jun 2017 00:16:15 +0200 Subject: Fix return comment --- src/contract_wrappers/exchange_wrapper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 1044362a2..f47e7a5ac 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -145,7 +145,7 @@ export class ExchangeWrapper extends ContractWrapper { * execution the tokens cannot be transferred. * @param takerAddress The user Ethereum address who would like to fill this order. * Must be available via the supplied Web3.Provider passed to 0x.js. - * @return The amount of the order (in taker tokens baseUnits) that was filled. + * @return The amount of the order that was filled (in taker token baseUnits). */ @decorators.contractCallErrorHandler public async fillOrderAsync(signedOrder: SignedOrder, takerTokenFillAmount: BigNumber.BigNumber, -- cgit v1.2.3 From d088dcdd36ddab32568bd8b71fb59cb59e52acc2 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 22 Jun 2017 00:16:35 +0200 Subject: Add simple test checking that fillOrderAsync return filled amount --- test/exchange_wrapper_test.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 6425e308a..cda2d4435 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -340,6 +340,16 @@ describe('ExchangeWrapper', () => { .to.be.bignumber.equal(fillableAmount.minus(partialFillAmount)); }); it('should return filled amount', async () => { + const signedOrder = await fillScenarios.createFillableSignedOrderAsync( + makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, + ); + const partialFillAmount = new BigNumber(3); + const filledAmount = await zeroEx.exchange.fillOrderAsync( + signedOrder, partialFillAmount, shouldCheckTransfer, takerAddress); + expect(filledAmount).to.be.bignumber.equal(partialFillAmount); + }); + it('should return the partially filled amount \ + if the fill amount specified is greater then the amount available', async () => { const signedOrder = await fillScenarios.createFillableSignedOrderAsync( makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); -- cgit v1.2.3 From 0dbad86d239cc1afb9df3c805a7567180b458b36 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 22 Jun 2017 13:41:07 +0200 Subject: Return cancelledAmount from cancelOrderAsync --- src/contract_wrappers/exchange_wrapper.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index f47e7a5ac..92c18e3e0 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -28,6 +28,7 @@ import { OrderFillRequest, LogErrorContractEventArgs, LogFillContractEventArgs, + LogCancelContractEventArgs, } from '../types'; import {assert} from '../utils/assert'; import {utils} from '../utils/utils'; @@ -443,10 +444,11 @@ export class ExchangeWrapper extends ContractWrapper { * @param order An object that conforms to the Order or SignedOrder interface. * The order you would like to cancel. * @param takerTokenCancelAmount The amount (specified in taker tokens) that you would like to cancel. + * @returns The amount of the order that was cancelled (in taker token baseUnits). */ @decorators.contractCallErrorHandler public async cancelOrderAsync( - order: Order|SignedOrder, takerTokenCancelAmount: BigNumber.BigNumber): Promise { + order: Order|SignedOrder, takerTokenCancelAmount: BigNumber.BigNumber): Promise { assert.doesConformToSchema('order', order, orderSchema); assert.isBigNumber('takerTokenCancelAmount', takerTokenCancelAmount); await assert.isSenderAddressAsync('order.maker', order.maker, this._web3Wrapper); @@ -473,6 +475,9 @@ export class ExchangeWrapper extends ContractWrapper { }, ); this._throwErrorLogsAsErrors(response.logs); + const logFillArgs = response.logs[0].args as LogCancelContractEventArgs; + const cancelledAmount = new BigNumber(logFillArgs.cancelledValueT); + return cancelledAmount; } /** * Batch version of cancelOrderAsync. Atomically cancels multiple orders in a single transaction. -- cgit v1.2.3 From b28b0fad7a30d4914eaae76efc96ece483184079 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 22 Jun 2017 13:45:43 +0200 Subject: Add test for cancelOrderAsync return value --- test/exchange_wrapper_test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index cda2d4435..6ff710dda 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -504,6 +504,10 @@ describe('ExchangeWrapper', () => { const cancelledAmount = await zeroEx.exchange.getCanceledTakerAmountAsync(orderHashHex); expect(cancelledAmount).to.be.bignumber.equal(cancelAmount); }); + it('should return cancelled amount', async () => { + const cancelledAmount = await zeroEx.exchange.cancelOrderAsync(signedOrder, cancelAmount); + expect(cancelledAmount).to.be.bignumber.equal(cancelAmount); + }); }); }); describe('#batchCancelOrderAsync', () => { -- cgit v1.2.3 From 9c2a332b699e7bad85a284a69c8cd6cca31bc519 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 22 Jun 2017 14:12:17 +0200 Subject: Return filledTakerTokenAmount from fillOrdersUpToAsync --- src/contract_wrappers/exchange_wrapper.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 92c18e3e0..30cdbd101 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -1,6 +1,7 @@ import map = require('lodash/map'); import isEmpty = require('lodash/isEmpty'); import find = require('lodash/find'); +import each = require('lodash/each'); import isUndefined = require('lodash/isUndefined'); import unzip = require('lodash/unzip'); import * as BigNumber from 'bignumber.js'; @@ -204,10 +205,11 @@ export class ExchangeWrapper extends ContractWrapper { * some cannot be filled. * @param takerAddress The user Ethereum address who would like to fill these orders. * Must be available via the supplied Web3.Provider passed to 0x.js. + * @return The amount of the orders that was filled (in taker token baseUnits). */ @decorators.contractCallErrorHandler public async fillOrdersUpToAsync(signedOrders: SignedOrder[], takerTokenFillAmount: BigNumber.BigNumber, - shouldCheckTransfer: boolean, takerAddress: string): Promise { + shouldCheckTransfer: boolean, takerAddress: string): Promise { const takerTokenAddresses = map(signedOrders, signedOrder => signedOrder.takerTokenAddress); assert.hasAtMostOneUniqueValue(takerTokenAddresses, ExchangeContractErrs.MULTIPLE_TAKER_TOKENS_IN_FILL_UP_TO_DISALLOWED); @@ -220,7 +222,7 @@ export class ExchangeWrapper extends ContractWrapper { signedOrder, takerTokenFillAmount, takerAddress); } if (isEmpty(signedOrders)) { - return; // no-op + return new BigNumber(0); // no-op } const orderAddressesValuesAndSignatureArray = map(signedOrders, signedOrder => { @@ -263,6 +265,11 @@ export class ExchangeWrapper extends ContractWrapper { }, ); this._throwErrorLogsAsErrors(response.logs); + let filledTakerTokenAmount = new BigNumber(0); + const filledAmounts = each(response.logs, log => { + filledTakerTokenAmount = filledTakerTokenAmount.plus((log.args as LogFillContractEventArgs).filledValueT); + }); + return filledTakerTokenAmount; } /** * Batch version of fillOrderAsync. @@ -444,7 +451,7 @@ export class ExchangeWrapper extends ContractWrapper { * @param order An object that conforms to the Order or SignedOrder interface. * The order you would like to cancel. * @param takerTokenCancelAmount The amount (specified in taker tokens) that you would like to cancel. - * @returns The amount of the order that was cancelled (in taker token baseUnits). + * @return The amount of the order that was cancelled (in taker token baseUnits). */ @decorators.contractCallErrorHandler public async cancelOrderAsync( -- cgit v1.2.3 From bd9fa3d335afdd0cd8b573f60b3ce3c0db6ad4dd Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 22 Jun 2017 14:15:35 +0200 Subject: Add test for return amount from fillOrdersUpToAsync --- test/exchange_wrapper_test.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index 6ff710dda..211c2819c 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -452,6 +452,12 @@ describe('ExchangeWrapper', () => { const remainingFillAmount = fillableAmount.minus(1); expect(anotherFilledAmount).to.be.bignumber.equal(remainingFillAmount); }); + it('should return filled amount', async () => { + const filledTakerTokenAmount = await zeroEx.exchange.fillOrdersUpToAsync( + signedOrders, fillUpToAmount, shouldCheckTransfer, takerAddress, + ); + expect(filledTakerTokenAmount).to.be.bignumber.equal(fillUpToAmount); + }); }); }); }); -- cgit v1.2.3