aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2017-06-07 17:50:23 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2017-06-07 17:50:23 +0800
commit491a6e3c04c3d9a63930eaeba6fb4632f3ae9cb1 (patch)
tree24b40ba936b87c8428ecfbccf91f7159690a873c /src
parent498cc6cea12d9c7d2bb1af6b50f5609efb074734 (diff)
downloaddexon-0x-contracts-491a6e3c04c3d9a63930eaeba6fb4632f3ae9cb1.tar.gz
dexon-0x-contracts-491a6e3c04c3d9a63930eaeba6fb4632f3ae9cb1.tar.zst
dexon-0x-contracts-491a6e3c04c3d9a63930eaeba6fb4632f3ae9cb1.zip
Add initial implementation of batchCancelAsync
Diffstat (limited to 'src')
-rw-r--r--src/contract_wrappers/exchange_wrapper.ts64
-rw-r--r--src/types.ts6
-rw-r--r--src/utils/assert.ts6
3 files changed, 55 insertions, 21 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts
index fe03dbd16..bd057eacd 100644
--- a/src/contract_wrappers/exchange_wrapper.ts
+++ b/src/contract_wrappers/exchange_wrapper.ts
@@ -134,37 +134,59 @@ export class ExchangeWrapper extends ContractWrapper {
*/
public async fillOrderAsync(signedOrder: SignedOrder, fillTakerAmount: BigNumber.BigNumber,
shouldCheckTransfer: boolean, takerAddress: string): Promise<void> {
- assert.doesConformToSchema('signedOrder',
- SchemaValidator.convertToJSONSchemaCompatibleObject(signedOrder as object),
- signedOrderSchema);
- assert.isBigNumber('fillTakerAmount', fillTakerAmount);
+ await this.batchFillOrderAsync([signedOrder], [fillTakerAmount], shouldCheckTransfer, takerAddress);
+ }
+ /**
+ * Batched version of fillOrderAsync. Executes fills atomically in a single transaction.
+ */
+ public async batchFillOrderAsync(signedOrders: SignedOrder[], fillTakerAmounts: BigNumber.BigNumber[],
+ shouldCheckTransfer: boolean, takerAddress: string): Promise<void> {
+ assert.isSameLength('signedOrders', signedOrders, 'fillTakerAmounts', fillTakerAmounts);
assert.isBoolean('shouldCheckTransfer', shouldCheckTransfer);
await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper);
-
+ // _.zip doesn't type check if values have different types :'(
+ const ordersAndAmounts = _.zip<any>(signedOrders, fillTakerAmounts);
+ _.forEach(ordersAndAmounts,
+ async ([signedOrder, fillTakerAmount]: [SignedOrder, BigNumber.BigNumber]) => {
+ assert.doesConformToSchema('signedOrder',
+ SchemaValidator.convertToJSONSchemaCompatibleObject(signedOrder as object), signedOrderSchema);
+ assert.isBigNumber('fillTakerAmount', fillTakerAmount);
+ await this.validateFillOrderAndThrowIfInvalidAsync(signedOrder, fillTakerAmount, takerAddress);
+ });
const exchangeInstance = await this.getExchangeContractAsync();
- await this.validateFillOrderAndThrowIfInvalidAsync(signedOrder, fillTakerAmount, takerAddress);
- const [orderAddresses, orderValues] = ExchangeWrapper.getOrderAddressesAndValues(signedOrder);
- const gas = await exchangeInstance.fill.estimateGas(
- orderAddresses,
- orderValues,
- fillTakerAmount,
+ const orderAddressesValuesAndSignatureArray = _.map(signedOrders, signedOrder => {
+ return [
+ ...ExchangeWrapper.getOrderAddressesAndValues(signedOrder),
+ signedOrder.ecSignature.v,
+ signedOrder.ecSignature.r,
+ signedOrder.ecSignature.s,
+ ];
+ });
+ // _.unzip doesn't type check if values have different types :'(
+ const [orderAddressesArray, orderValuesArray, vArray, rArray, sArray] = _.unzip<any>(
+ orderAddressesValuesAndSignatureArray,
+ );
+ const gas = await exchangeInstance.batchFill.estimateGas(
+ orderAddressesArray,
+ orderValuesArray,
+ fillTakerAmounts,
shouldCheckTransfer,
- signedOrder.ecSignature.v,
- signedOrder.ecSignature.r,
- signedOrder.ecSignature.s,
+ vArray,
+ rArray,
+ sArray,
{
from: takerAddress,
},
);
- const response: ContractResponse = await exchangeInstance.fill(
- orderAddresses,
- orderValues,
- fillTakerAmount,
+ const response: ContractResponse = await exchangeInstance.batchFill(
+ orderAddressesArray,
+ orderValuesArray,
+ fillTakerAmounts,
shouldCheckTransfer,
- signedOrder.ecSignature.v,
- signedOrder.ecSignature.r,
- signedOrder.ecSignature.s,
+ vArray,
+ rArray,
+ sArray,
{
from: takerAddress,
gas,
diff --git a/src/types.ts b/src/types.ts
index aa699110b..0623ec6a7 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -71,6 +71,12 @@ export interface ExchangeContract extends ContractInstance {
estimateGas: (orderAddresses: OrderAddresses, orderValues: OrderValues, fillAmount: BigNumber.BigNumber,
shouldCheckTransfer: boolean, v: number, r: string, s: string, txOpts?: TxOpts) => number;
};
+ batchFill: {
+ (orderAddresses: OrderAddresses[], orderValues: OrderValues[], fillAmount: BigNumber.BigNumber[],
+ shouldCheckTransfer: boolean, v: number[], r: string[], s: string[], txOpts?: TxOpts): ContractResponse;
+ estimateGas: (orderAddresses: OrderAddresses[], orderValues: OrderValues[], fillAmount: BigNumber.BigNumber[],
+ shouldCheckTransfer: boolean, v: number[], r: string[], s: string[], txOpts?: TxOpts) => number;
+ };
cancel: {
(orderAddresses: OrderAddresses, orderValues: OrderValues, cancelAmount: BigNumber.BigNumber,
txOpts?: TxOpts): ContractResponse;
diff --git a/src/utils/assert.ts b/src/utils/assert.ts
index 4dc6945a2..3842185bb 100644
--- a/src/utils/assert.ts
+++ b/src/utils/assert.ts
@@ -42,6 +42,12 @@ export const assert = {
const availableAddresses = await web3Wrapper.getAvailableAddressesAsync();
this.assert(!_.isEmpty(availableAddresses), 'No addresses were available on the provided web3 instance');
},
+ isSameLength(variableName1: string, value1: any[], variableName2: string, value2: any[]) {
+ const length1 = value1.length;
+ const length2 = value2.length;
+ this.assert(length1 === length2, `${variableName1} and ${variableName2} length mismatch.
+ ${length1} != ${length2}`);
+ },
isNumber(variableName: string, value: number): void {
this.assert(_.isFinite(value), this.typeAssertionMessage(variableName, 'number', value));
},