diff options
author | Leonid <logvinov.leon@gmail.com> | 2017-06-09 00:33:19 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-09 00:33:19 +0800 |
commit | c94485dfbba388f481ca6c10bd62b863d7429223 (patch) | |
tree | 1faaa6e52e941831034469c9bc56a47bfc9a6a19 /src | |
parent | 7f6a6dd18983980c08c53b2b561ec57dfaed755f (diff) | |
parent | 743ba29918715e21c7891b3c7426dcb5fdc14e17 (diff) | |
download | dexon-sol-tools-c94485dfbba388f481ca6c10bd62b863d7429223.tar dexon-sol-tools-c94485dfbba388f481ca6c10bd62b863d7429223.tar.gz dexon-sol-tools-c94485dfbba388f481ca6c10bd62b863d7429223.tar.bz2 dexon-sol-tools-c94485dfbba388f481ca6c10bd62b863d7429223.tar.lz dexon-sol-tools-c94485dfbba388f481ca6c10bd62b863d7429223.tar.xz dexon-sol-tools-c94485dfbba388f481ca6c10bd62b863d7429223.tar.zst dexon-sol-tools-c94485dfbba388f481ca6c10bd62b863d7429223.zip |
Merge branch 'master' into fillOrderUpToAsync
Diffstat (limited to 'src')
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 113 | ||||
-rw-r--r-- | src/schemas/basic_type_schemas.ts | 11 | ||||
-rw-r--r-- | src/schemas/order_fill_or_kill_requests_schema.ts | 12 | ||||
-rw-r--r-- | src/schemas/order_schemas.ts | 12 | ||||
-rw-r--r-- | src/types.ts | 11 | ||||
-rw-r--r-- | src/utils/schema_validator.ts | 7 |
6 files changed, 128 insertions, 38 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index afaa89c94..a7d1f52ae 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -10,6 +10,7 @@ import { OrderValues, OrderAddresses, Order, + OrderFillOrKillRequest, SignedOrder, ContractEvent, ExchangeEvents, @@ -27,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 {orderFillOrKillRequestsSchema} from '../schemas/order_fill_or_kill_requests_schema'; import {signedOrderSchema, orderSchema} from '../schemas/order_schemas'; import {SchemaValidator} from '../utils/schema_validator'; import {constants} from '../utils/constants'; @@ -71,23 +73,6 @@ export class ExchangeWrapper extends ContractWrapper { await this.stopWatchingExchangeLogEventsAsync(); delete this.exchangeContractIfExists; } - private async isValidSignatureUsingContractCallAsync(dataHex: string, ecSignature: ECSignature, - signerAddressHex: string): Promise<boolean> { - assert.isHexString('dataHex', dataHex); - assert.doesConformToSchema('ecSignature', ecSignature, ecSignatureSchema); - assert.isETHAddressHex('signerAddressHex', signerAddressHex); - - const exchangeInstance = await this.getExchangeContractAsync(); - - const isValidSignature = await exchangeInstance.isValidSignature.call( - signerAddressHex, - dataHex, - ecSignature.v, - ecSignature.r, - ecSignature.s, - ); - return isValidSignature; - } /** * Returns the unavailable takerAmount of an order. Unavailable amount is defined as the total * amount that has been filled or cancelled. The remaining takerAmount can be calculated by @@ -321,13 +306,8 @@ export class ExchangeWrapper extends ContractWrapper { const exchangeInstance = await this.getExchangeContractAsync(); await this.validateFillOrderAndThrowIfInvalidAsync(signedOrder, fillTakerAmount, takerAddress); - // Check that fillValue available >= fillTakerAmount - const orderHashHex = await this.getOrderHashHexAsync(signedOrder); - const unavailableTakerAmount = await this.getUnavailableTakerAmountAsync(orderHashHex); - const remainingTakerAmount = signedOrder.takerTokenAmount.minus(unavailableTakerAmount); - if (remainingTakerAmount < fillTakerAmount) { - throw new Error(ExchangeContractErrs.INSUFFICIENT_REMAINING_FILL_AMOUNT); - } + await this.validateFillOrKillOrderAndThrowIfInvalidAsync(signedOrder, exchangeInstance.address, + fillTakerAmount); const [orderAddresses, orderValues] = ExchangeWrapper.getOrderAddressesAndValues(signedOrder); @@ -357,6 +337,63 @@ export class ExchangeWrapper extends ContractWrapper { this.throwErrorLogsAsErrors(response.logs); } /** + * Batch version of fillOrKill. Allows a taker to specify a batch of orders that will either be atomically + * filled (each to the specified fillAmount) or aborted. + */ + public async batchFillOrKillAsync(orderFillOrKillRequests: OrderFillOrKillRequest[], + takerAddress: string) { + await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper); + assert.doesConformToSchema('orderFillOrKillRequests', + SchemaValidator.convertToJSONSchemaCompatibleObject(orderFillOrKillRequests), + orderFillOrKillRequestsSchema, + ); + const exchangeInstance = await this.getExchangeContractAsync(); + _.each(orderFillOrKillRequests, request => { + this.validateFillOrKillOrderAndThrowIfInvalidAsync(request.signedOrder, + exchangeInstance.address, + request.fillTakerAmount); + }); + + const orderAddressesValuesAndTakerTokenFillAmounts = _.map(orderFillOrKillRequests, request => { + return [ + ...ExchangeWrapper.getOrderAddressesAndValues(request.signedOrder), + request.fillTakerAmount, + request.signedOrder.ecSignature.v, + request.signedOrder.ecSignature.r, + request.signedOrder.ecSignature.s, + ]; + }); + + // We use _.unzip<any> because _.unzip doesn't type check if values have different types :'( + const [orderAddresses, orderValues, fillTakerAmounts, vParams, rParams, sParams] = + _.unzip<any>(orderAddressesValuesAndTakerTokenFillAmounts); + + const gas = await exchangeInstance.batchFillOrKill.estimateGas( + orderAddresses, + orderValues, + fillTakerAmounts, + vParams, + rParams, + sParams, + { + from: takerAddress, + }, + ); + const response: ContractResponse = await exchangeInstance.batchFillOrKill( + orderAddresses, + orderValues, + fillTakerAmounts, + vParams, + rParams, + sParams, + { + from: takerAddress, + gas, + }, + ); + this.throwErrorLogsAsErrors(response.logs); + } + /** * Cancel a given fill amount of an order. Cancellations are cumulative. */ public async cancelOrderAsync( @@ -469,6 +506,23 @@ export class ExchangeWrapper extends ContractWrapper { logEventObj.watch(callback); this.exchangeLogEventObjs.push(logEventObj); } + private async isValidSignatureUsingContractCallAsync(dataHex: string, ecSignature: ECSignature, + signerAddressHex: string): Promise<boolean> { + assert.isHexString('dataHex', dataHex); + assert.doesConformToSchema('ecSignature', ecSignature, ecSignatureSchema); + assert.isETHAddressHex('signerAddressHex', signerAddressHex); + + const exchangeInstance = await this.getExchangeContractAsync(); + + const isValidSignature = await exchangeInstance.isValidSignature.call( + signerAddressHex, + dataHex, + ecSignature.v, + ecSignature.r, + ecSignature.s, + ); + return isValidSignature; + } private async getOrderHashHexAsync(order: Order|SignedOrder): Promise<string> { const exchangeInstance = await this.getExchangeContractAsync(); const orderHashHex = utils.getOrderHashHex(order, exchangeInstance.address); @@ -526,6 +580,17 @@ export class ExchangeWrapper extends ContractWrapper { throw new Error(ExchangeContractErrs.ORDER_CANCEL_EXPIRED); } } + private async validateFillOrKillOrderAndThrowIfInvalidAsync(signedOrder: SignedOrder, + exchangeAddress: string, + fillTakerAmount: BigNumber.BigNumber) { + // Check that fillValue available >= fillTakerAmount + const orderHashHex = utils.getOrderHashHex(signedOrder, exchangeAddress); + const unavailableTakerAmount = await this.getUnavailableTakerAmountAsync(orderHashHex); + const remainingTakerAmount = signedOrder.takerTokenAmount.minus(unavailableTakerAmount); + if (remainingTakerAmount < fillTakerAmount) { + throw new Error(ExchangeContractErrs.INSUFFICIENT_REMAINING_FILL_AMOUNT); + } + } /** * This method does not currently validate the edge-case where the makerToken or takerToken is also the token used * to pay fees (ZRX). It is possible for them to have enough for fees and the transfer but not both. diff --git a/src/schemas/basic_type_schemas.ts b/src/schemas/basic_type_schemas.ts new file mode 100644 index 000000000..c3b81185d --- /dev/null +++ b/src/schemas/basic_type_schemas.ts @@ -0,0 +1,11 @@ +export const addressSchema = { + id: '/addressSchema', + type: 'string', + pattern: '^0[xX][0-9A-Fa-f]{40}$', +}; + +export const numberSchema = { + id: '/numberSchema', + type: 'string', + pattern: '^\\d+(\\.\\d+)?$', +}; diff --git a/src/schemas/order_fill_or_kill_requests_schema.ts b/src/schemas/order_fill_or_kill_requests_schema.ts new file mode 100644 index 000000000..4db7113de --- /dev/null +++ b/src/schemas/order_fill_or_kill_requests_schema.ts @@ -0,0 +1,12 @@ +export const orderFillOrKillRequestsSchema = { + id: '/OrderFillOrKillRequests', + type: 'array', + items: { + properties: { + signedOrder: {$ref: '/signedOrderSchema'}, + fillTakerAmount: {type: '/numberSchema'}, + }, + required: ['signedOrder', 'fillTakerAmount'], + type: 'object', + }, +}; diff --git a/src/schemas/order_schemas.ts b/src/schemas/order_schemas.ts index 4999f3e9d..133736b3d 100644 --- a/src/schemas/order_schemas.ts +++ b/src/schemas/order_schemas.ts @@ -1,15 +1,3 @@ -export const addressSchema = { - id: '/addressSchema', - type: 'string', - pattern: '^0[xX][0-9A-Fa-f]{40}$', -}; - -export const numberSchema = { - id: '/numberSchema', - type: 'string', - pattern: '^\\d+(\\.\\d+)?$', -}; - export const orderSchema = { id: '/orderSchema', properties: { diff --git a/src/types.ts b/src/types.ts index 2dabfaa3c..53476ab85 100644 --- a/src/types.ts +++ b/src/types.ts @@ -98,6 +98,12 @@ export interface ExchangeContract extends ContractInstance { estimateGas: (orderAddresses: OrderAddresses, orderValues: OrderValues, fillAmount: BigNumber.BigNumber, v: number, r: string, s: string, txOpts?: TxOpts) => number; }; + batchFillOrKill: { + (orderAddresses: OrderAddresses[], orderValues: OrderValues[], fillValuesT: BigNumber.BigNumber[], + v: number[], r: string[], s: string[], txOpts: TxOpts): ContractResponse; + estimateGas: (orderAddresses: OrderAddresses[], orderValues: OrderValues[], fillValuesT: BigNumber.BigNumber[], + v: number[], r: string[], s: string[], txOpts?: TxOpts) => number; + }; filled: { call: (orderHash: string) => BigNumber.BigNumber; }; @@ -243,6 +249,11 @@ export interface SubscriptionOpts { export type DoneCallback = (err?: Error) => void; +export interface OrderFillOrKillRequest { + signedOrder: SignedOrder; + fillTakerAmount: BigNumber.BigNumber; +} + export interface OrderCancellationRequest { order: Order|SignedOrder; takerTokenCancelAmount: BigNumber.BigNumber; diff --git a/src/utils/schema_validator.ts b/src/utils/schema_validator.ts index 932ddf62a..72f6afffa 100644 --- a/src/utils/schema_validator.ts +++ b/src/utils/schema_validator.ts @@ -1,7 +1,9 @@ import {Validator, ValidatorResult} from 'jsonschema'; import {ecSignatureSchema, ecSignatureParameter} from '../schemas/ec_signature_schema'; -import {addressSchema, numberSchema, orderSchema, signedOrderSchema} from '../schemas/order_schemas'; +import {orderSchema, signedOrderSchema} from '../schemas/order_schemas'; +import {addressSchema, numberSchema} from '../schemas/basic_type_schemas'; import {tokenSchema} from '../schemas/token_schema'; +import {orderFillOrKillRequestsSchema} from '../schemas/order_fill_or_kill_requests_schema'; export class SchemaValidator { private validator: Validator; @@ -9,7 +11,7 @@ export class SchemaValidator { // sub-types (e.g BigNumber) with a simpler string representation. Since BigNumber and other // complex types implement the `toString` method, we can stringify the object and // then parse it. The resultant object can then be checked using jsonschema. - public static convertToJSONSchemaCompatibleObject(obj: object): object { + public static convertToJSONSchemaCompatibleObject(obj: any): any { return JSON.parse(JSON.stringify(obj)); } constructor() { @@ -21,6 +23,7 @@ export class SchemaValidator { this.validator.addSchema(ecSignatureSchema, ecSignatureSchema.id); this.validator.addSchema(signedOrderSchema, signedOrderSchema.id); this.validator.addSchema(ecSignatureParameter, ecSignatureParameter.id); + this.validator.addSchema(orderFillOrKillRequestsSchema, orderFillOrKillRequestsSchema.id); } public validate(instance: object, schema: Schema): ValidatorResult { return this.validator.validate(instance, schema); |