From cd25d38118de90f8282dc8e5e2b4ffb02fde9990 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 8 Jun 2017 17:28:09 +0200 Subject: Implement fillOrderUpToAsync --- src/contract_wrappers/exchange_wrapper.ts | 66 +++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'src/contract_wrappers/exchange_wrapper.ts') diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 72ae0d07e..524ebdc0f 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -175,6 +175,72 @@ export class ExchangeWrapper extends ContractWrapper { ); this.throwErrorLogsAsErrors(response.logs); } + /** + * Sequentially and atomically fills signedOrders up to takerTokenFillAmount. + * If fill amount is reached - it succeeds and doesn't fill the rest of the orders. + * If fill amount is not reached - it just fills all the orders. + */ + public async fillOrderUpToAsync(signedOrders: SignedOrder[], takerTokenFillAmount: BigNumber.BigNumber, + shouldCheckTransfer: boolean, takerAddress: string): Promise { + const takerTokenAddresses = _.map(signedOrders, signedOrder => signedOrder.takerTokenAddress); + assert.assert(_.uniq(takerTokenAddresses).length === 1, + ExchangeContractErrs.MULTIPLE_TAKER_TOKENS_IN_FILL_UP_TO); + assert.isBigNumber('takerTokenFillAmount', takerTokenFillAmount); + assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer); + await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper); + _.forEach(signedOrders, + async (signedOrder: SignedOrder, i: number) => { + assert.doesConformToSchema(`signedOrder[${i}]`, + SchemaValidator.convertToJSONSchemaCompatibleObject(signedOrder as object), + signedOrderSchema); + await this.validateFillOrderAndThrowIfInvalidAsync( + signedOrder, takerTokenFillAmount, takerAddress); + }); + if (_.isEmpty(signedOrders)) { + return; // no-op + } + + const orderAddressesValuesAndSignatureArray = _.map(signedOrders, signedOrder => { + return [ + ...ExchangeWrapper.getOrderAddressesAndValues(signedOrder), + signedOrder.ecSignature.v, + signedOrder.ecSignature.r, + signedOrder.ecSignature.s, + ]; + }); + // We use _.unzip because _.unzip doesn't type check if values have different types :'( + const [orderAddressesArray, orderValuesArray, vArray, rArray, sArray] = _.unzip( + orderAddressesValuesAndSignatureArray, + ); + + const exchangeInstance = await this.getExchangeContractAsync(); + const gas = await exchangeInstance.fillUpTo.estimateGas( + orderAddressesArray, + orderValuesArray, + takerTokenFillAmount, + shouldCheckTransfer, + vArray, + rArray, + sArray, + { + from: takerAddress, + }, + ); + const response: ContractResponse = await exchangeInstance.fillUpTo( + orderAddressesArray, + orderValuesArray, + takerTokenFillAmount, + shouldCheckTransfer, + vArray, + rArray, + sArray, + { + from: takerAddress, + gas, + }, + ); + this.throwErrorLogsAsErrors(response.logs); + } /** * Batch version of fillOrderAsync. * Executes multiple fills atomically in a single transaction. -- cgit v1.2.3 From 6eb192f0ea056bc84e04df1845f484a906ad392c Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 8 Jun 2017 18:14:29 +0200 Subject: Add tests for fillOrdersUpTo --- src/contract_wrappers/exchange_wrapper.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/contract_wrappers/exchange_wrapper.ts') diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 524ebdc0f..afaa89c94 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -180,10 +180,10 @@ export class ExchangeWrapper extends ContractWrapper { * If fill amount is reached - it succeeds and doesn't fill the rest of the orders. * If fill amount is not reached - it just fills all the orders. */ - public async fillOrderUpToAsync(signedOrders: SignedOrder[], takerTokenFillAmount: BigNumber.BigNumber, - shouldCheckTransfer: boolean, takerAddress: string): Promise { + public async fillOrdersUpToAsync(signedOrders: SignedOrder[], takerTokenFillAmount: BigNumber.BigNumber, + shouldCheckTransfer: boolean, takerAddress: string): Promise { const takerTokenAddresses = _.map(signedOrders, signedOrder => signedOrder.takerTokenAddress); - assert.assert(_.uniq(takerTokenAddresses).length === 1, + assert.assert(_.uniq(takerTokenAddresses).length <= 1, ExchangeContractErrs.MULTIPLE_TAKER_TOKENS_IN_FILL_UP_TO); assert.isBigNumber('takerTokenFillAmount', takerTokenFillAmount); assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer); @@ -395,11 +395,8 @@ export class ExchangeWrapper extends ContractWrapper { * All orders must be from the same maker. */ public async batchCancelOrderAsync(orderCancellationRequests: OrderCancellationRequest[]): Promise { - if (_.isEmpty(orderCancellationRequests)) { - return; // no-op - } const makers = _.map(orderCancellationRequests, cancellationRequest => cancellationRequest.order.maker); - assert.assert(_.uniq(makers).length === 1, ExchangeContractErrs.MULTIPLE_MAKERS_IN_SINGLE_CANCEL_BATCH); + assert.assert(_.uniq(makers).length <= 1, ExchangeContractErrs.MULTIPLE_MAKERS_IN_SINGLE_CANCEL_BATCH); const maker = makers[0]; await assert.isSenderAddressAsync('maker', maker, this.web3Wrapper); _.forEach(orderCancellationRequests, @@ -414,6 +411,9 @@ export class ExchangeWrapper extends ContractWrapper { cancellationRequest.order, cancellationRequest.takerTokenCancelAmount, ); }); + if (_.isEmpty(orderCancellationRequests)) { + return; // no-op + } const exchangeInstance = await this.getExchangeContractAsync(); const orderAddressesValuesAndTakerTokenCancelAmounts = _.map(orderCancellationRequests, cancellationRequest => { return [ -- cgit v1.2.3 From a786776d7cf52ab70d54e0dc421dc645a412b3c7 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 8 Jun 2017 19:21:46 +0200 Subject: Add assert.hashAtMostOneUniqueValue --- src/contract_wrappers/exchange_wrapper.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/contract_wrappers/exchange_wrapper.ts') diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index a7d1f52ae..aa79729c3 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -161,15 +161,15 @@ export class ExchangeWrapper extends ContractWrapper { this.throwErrorLogsAsErrors(response.logs); } /** - * Sequentially and atomically fills signedOrders up to takerTokenFillAmount. - * If fill amount is reached - it succeeds and doesn't fill the rest of the orders. - * If fill amount is not reached - it just fills all the orders. + * Sequentially and atomically fills signedOrders up to the specified takerTokenFillAmount. + * If the fill amount is reached - it succeeds and does not fill the rest of the orders. + * If fill amount is not reached - it fills as much of the fill amount as possible and succeeds. */ public async fillOrdersUpToAsync(signedOrders: SignedOrder[], takerTokenFillAmount: BigNumber.BigNumber, shouldCheckTransfer: boolean, takerAddress: string): Promise { const takerTokenAddresses = _.map(signedOrders, signedOrder => signedOrder.takerTokenAddress); - assert.assert(_.uniq(takerTokenAddresses).length <= 1, - ExchangeContractErrs.MULTIPLE_TAKER_TOKENS_IN_FILL_UP_TO); + assert.hashAtMostOneUniqueValue(takerTokenAddresses, + ExchangeContractErrs.MULTIPLE_TAKER_TOKENS_IN_FILL_UP_TO_DISALLOWED); assert.isBigNumber('takerTokenFillAmount', takerTokenFillAmount); assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer); await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper); @@ -433,7 +433,7 @@ export class ExchangeWrapper extends ContractWrapper { */ public async batchCancelOrderAsync(orderCancellationRequests: OrderCancellationRequest[]): Promise { const makers = _.map(orderCancellationRequests, cancellationRequest => cancellationRequest.order.maker); - assert.assert(_.uniq(makers).length <= 1, ExchangeContractErrs.MULTIPLE_MAKERS_IN_SINGLE_CANCEL_BATCH); + assert.hashAtMostOneUniqueValue(makers, ExchangeContractErrs.MULTIPLE_MAKERS_IN_SINGLE_CANCEL_BATCH); const maker = makers[0]; await assert.isSenderAddressAsync('maker', maker, this.web3Wrapper); _.forEach(orderCancellationRequests, -- cgit v1.2.3 From 6e01cf9a5a319f3f3543955fa9f9d673b2c06a82 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 8 Jun 2017 19:22:27 +0200 Subject: Address feedback --- src/contract_wrappers/exchange_wrapper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/contract_wrappers/exchange_wrapper.ts') diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index aa79729c3..d92f87bd8 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -433,7 +433,7 @@ export class ExchangeWrapper extends ContractWrapper { */ public async batchCancelOrderAsync(orderCancellationRequests: OrderCancellationRequest[]): Promise { const makers = _.map(orderCancellationRequests, cancellationRequest => cancellationRequest.order.maker); - assert.hashAtMostOneUniqueValue(makers, ExchangeContractErrs.MULTIPLE_MAKERS_IN_SINGLE_CANCEL_BATCH); + assert.hashAtMostOneUniqueValue(makers, ExchangeContractErrs.MULTIPLE_MAKERS_IN_SINGLE_CANCEL_BATCH_DISALLOWED); const maker = makers[0]; await assert.isSenderAddressAsync('maker', maker, this.web3Wrapper); _.forEach(orderCancellationRequests, -- cgit v1.2.3 From 96615749f9f48be11cdaff15307315687820ce5c Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 8 Jun 2017 19:29:46 +0200 Subject: Add signedOrdersScxhema --- src/contract_wrappers/exchange_wrapper.ts | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/contract_wrappers/exchange_wrapper.ts') diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index d92f87bd8..943d168c3 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -28,6 +28,7 @@ import {utils} from '../utils/utils'; import {ContractWrapper} from './contract_wrapper'; import * as ExchangeArtifacts from '../artifacts/Exchange.json'; import {ecSignatureSchema} from '../schemas/ec_signature_schema'; +import {signedOrdersSchema} from '../schemas/signed_orders_schema'; import {orderFillOrKillRequestsSchema} from '../schemas/order_fill_or_kill_requests_schema'; import {signedOrderSchema, orderSchema} from '../schemas/order_schemas'; import {SchemaValidator} from '../utils/schema_validator'; @@ -172,6 +173,9 @@ export class ExchangeWrapper extends ContractWrapper { ExchangeContractErrs.MULTIPLE_TAKER_TOKENS_IN_FILL_UP_TO_DISALLOWED); assert.isBigNumber('takerTokenFillAmount', takerTokenFillAmount); assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer); + assert.doesConformToSchema( + 'signedOrders', SchemaValidator.convertToJSONSchemaCompatibleObject(signedOrders), signedOrdersSchema + ); await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper); _.forEach(signedOrders, async (signedOrder: SignedOrder, i: number) => { -- cgit v1.2.3 From 6d4cb28b9d08df574e7f335196dfdd82f02a74c3 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 9 Jun 2017 15:49:56 +0200 Subject: Address feedback --- src/contract_wrappers/exchange_wrapper.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'src/contract_wrappers/exchange_wrapper.ts') diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 943d168c3..8ea402624 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -169,7 +169,7 @@ export class ExchangeWrapper extends ContractWrapper { public async fillOrdersUpToAsync(signedOrders: SignedOrder[], takerTokenFillAmount: BigNumber.BigNumber, shouldCheckTransfer: boolean, takerAddress: string): Promise { const takerTokenAddresses = _.map(signedOrders, signedOrder => signedOrder.takerTokenAddress); - assert.hashAtMostOneUniqueValue(takerTokenAddresses, + assert.hasAtMostOneUniqueValue(takerTokenAddresses, ExchangeContractErrs.MULTIPLE_TAKER_TOKENS_IN_FILL_UP_TO_DISALLOWED); assert.isBigNumber('takerTokenFillAmount', takerTokenFillAmount); assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer); @@ -179,9 +179,6 @@ export class ExchangeWrapper extends ContractWrapper { await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper); _.forEach(signedOrders, async (signedOrder: SignedOrder, i: number) => { - assert.doesConformToSchema(`signedOrder[${i}]`, - SchemaValidator.convertToJSONSchemaCompatibleObject(signedOrder as object), - signedOrderSchema); await this.validateFillOrderAndThrowIfInvalidAsync( signedOrder, takerTokenFillAmount, takerAddress); }); @@ -437,7 +434,7 @@ export class ExchangeWrapper extends ContractWrapper { */ public async batchCancelOrderAsync(orderCancellationRequests: OrderCancellationRequest[]): Promise { const makers = _.map(orderCancellationRequests, cancellationRequest => cancellationRequest.order.maker); - assert.hashAtMostOneUniqueValue(makers, ExchangeContractErrs.MULTIPLE_MAKERS_IN_SINGLE_CANCEL_BATCH_DISALLOWED); + assert.hasAtMostOneUniqueValue(makers, ExchangeContractErrs.MULTIPLE_MAKERS_IN_SINGLE_CANCEL_BATCH_DISALLOWED); const maker = makers[0]; await assert.isSenderAddressAsync('maker', maker, this.web3Wrapper); _.forEach(orderCancellationRequests, -- cgit v1.2.3