diff options
author | Leonid <logvinov.leon@gmail.com> | 2017-06-23 04:50:35 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-23 04:50:35 +0800 |
commit | c52d6753cefcc6b55a6683e5a1c45ed62f698dc3 (patch) | |
tree | da804f637b18b8589be98d81b34b26cbfc2b9032 | |
parent | 49e43c98767666fcf457edfe2f3cbe098d12b6a5 (diff) | |
parent | e5785532ed1f4cada70db7815a44ffdd005077f5 (diff) | |
download | dexon-0x-contracts-c52d6753cefcc6b55a6683e5a1c45ed62f698dc3.tar.gz dexon-0x-contracts-c52d6753cefcc6b55a6683e5a1c45ed62f698dc3.tar.zst dexon-0x-contracts-c52d6753cefcc6b55a6683e5a1c45ed62f698dc3.zip |
Merge pull request #72 from 0xProject/fill-order-amuont
Return filledAmount from fillOrderAsync
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 28 | ||||
-rw-r--r-- | test/exchange_wrapper_test.ts | 38 |
2 files changed, 60 insertions, 6 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index e33dd6f6e..d6de801a3 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -23,6 +23,8 @@ import { OrderCancellationRequest, OrderFillRequest, LogErrorContractEventArgs, + LogFillContractEventArgs, + LogCancelContractEventArgs, } from '../types'; import {assert} from '../utils/assert'; import {utils} from '../utils/utils'; @@ -140,10 +142,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 that was filled (in taker token baseUnits). */ @decorators.contractCallErrorHandler public async fillOrderAsync(signedOrder: SignedOrder, takerTokenFillAmount: BigNumber.BigNumber, - shouldCheckTransfer: boolean, takerAddress: string): Promise<void> { + shouldCheckTransfer: boolean, takerAddress: string): Promise<BigNumber.BigNumber> { assert.doesConformToSchema('signedOrder', signedOrder, signedOrderSchema); assert.isBigNumber('takerTokenFillAmount', takerTokenFillAmount); assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer); @@ -180,6 +183,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. @@ -194,11 +200,12 @@ 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<void> { - const takerTokenAddresses = _.map(signedOrders, signedOrder => signedOrder.takerTokenAddress); + shouldCheckTransfer: boolean, takerAddress: string): Promise<BigNumber.BigNumber> { + const takerTokenAddresses = _map(signedOrders, signedOrder => signedOrder.takerTokenAddress); assert.hasAtMostOneUniqueValue(takerTokenAddresses, ExchangeContractErrs.MULTIPLE_TAKER_TOKENS_IN_FILL_UP_TO_DISALLOWED); assert.isBigNumber('takerTokenFillAmount', takerTokenFillAmount); @@ -209,8 +216,8 @@ export class ExchangeWrapper extends ContractWrapper { await this._validateFillOrderAndThrowIfInvalidAsync( signedOrder, takerTokenFillAmount, takerAddress); } - if (_.isEmpty(signedOrders)) { - return; // no-op + if (_isEmpty(signedOrders)) { + return new BigNumber(0); // no-op } const orderAddressesValuesAndSignatureArray = _.map(signedOrders, signedOrder => { @@ -253,6 +260,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. @@ -434,10 +446,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. + * @return The amount of the order that was cancelled (in taker token baseUnits). */ @decorators.contractCallErrorHandler public async cancelOrderAsync( - order: Order|SignedOrder, takerTokenCancelAmount: BigNumber.BigNumber): Promise<void> { + order: Order|SignedOrder, takerTokenCancelAmount: BigNumber.BigNumber): Promise<BigNumber.BigNumber> { assert.doesConformToSchema('order', order, orderSchema); assert.isBigNumber('takerTokenCancelAmount', takerTokenCancelAmount); await assert.isSenderAddressAsync('order.maker', order.maker, this._web3Wrapper); @@ -464,6 +477,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. diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts index e4b2e3f86..211c2819c 100644 --- a/test/exchange_wrapper_test.ts +++ b/test/exchange_wrapper_test.ts @@ -339,6 +339,34 @@ 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); + 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, + ); + 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); @@ -424,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); + }); }); }); }); @@ -476,6 +510,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', () => { |