aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-06-08 17:25:47 +0800
committerFabio Berger <me@fabioberger.com>2017-06-08 17:25:47 +0800
commit8ab80914e01ce67020ee2c94e6390309ca1b850f (patch)
treefde73d8042f8fb7855e07ee1be2db3d0ff549bb7
parentca308354880e81425043cd586da6c781013bea9d (diff)
downloaddexon-sol-tools-8ab80914e01ce67020ee2c94e6390309ca1b850f.tar
dexon-sol-tools-8ab80914e01ce67020ee2c94e6390309ca1b850f.tar.gz
dexon-sol-tools-8ab80914e01ce67020ee2c94e6390309ca1b850f.tar.bz2
dexon-sol-tools-8ab80914e01ce67020ee2c94e6390309ca1b850f.tar.lz
dexon-sol-tools-8ab80914e01ce67020ee2c94e6390309ca1b850f.tar.xz
dexon-sol-tools-8ab80914e01ce67020ee2c94e6390309ca1b850f.tar.zst
dexon-sol-tools-8ab80914e01ce67020ee2c94e6390309ca1b850f.zip
Implement batchFillOrKill and tests
-rw-r--r--src/contract_wrappers/exchange_wrapper.ts77
-rw-r--r--src/types.ts11
-rw-r--r--test/exchange_wrapper_test.ts127
3 files changed, 157 insertions, 58 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts
index 4f132656e..901ea4749 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,
@@ -188,13 +189,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);
@@ -224,6 +220,62 @@ 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 to the desired fillAmount or aborted.
+ */
+ public async batchFillOrKillAsync(orderFillOrKillRequests: OrderFillOrKillRequest[],
+ takerAddress: string) {
+ await assert.isSenderAddressAsync('takerAddress', takerAddress, this.web3Wrapper);
+ const exchangeInstance = await this.getExchangeContractAsync();
+ _.each(orderFillOrKillRequests, request => {
+ assert.doesConformToSchema('signedOrder',
+ SchemaValidator.convertToJSONSchemaCompatibleObject(request.signedOrder as object),
+ signedOrderSchema);
+ assert.isBigNumber('fillTakerAmount', request.fillTakerAmount);
+ 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,
+ ];
+ });
+
+ 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(order: Order|SignedOrder, takerTokenCancelAmount: BigNumber.BigNumber):
@@ -334,6 +386,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/types.ts b/src/types.ts
index cc145dc2e..d19fb2d43 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -80,6 +80,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;
};
@@ -222,3 +228,8 @@ export interface SubscriptionOpts {
}
export type DoneCallback = (err?: Error) => void;
+
+export interface OrderFillOrKillRequest {
+ signedOrder: SignedOrder;
+ fillTakerAmount: BigNumber.BigNumber;
+}
diff --git a/test/exchange_wrapper_test.ts b/test/exchange_wrapper_test.ts
index 1186345be..4a004fbcf 100644
--- a/test/exchange_wrapper_test.ts
+++ b/test/exchange_wrapper_test.ts
@@ -120,7 +120,7 @@ describe('ExchangeWrapper', () => {
expect(isValid).to.be.true();
});
});
- describe('#fillOrKillOrderAsync', () => {
+ describe('fillOrKill order(s)', () => {
let makerTokenAddress: string;
let takerTokenAddress: string;
let coinbase: string;
@@ -135,63 +135,88 @@ describe('ExchangeWrapper', () => {
makerTokenAddress = makerToken.address;
takerTokenAddress = takerToken.address;
});
- describe('failed fillOrKill', () => {
- it('should throw if remaining fillAmount is less then the desired fillAmount', async () => {
+ describe('#batchFillOrKillAsync', () => {
+ it('successfuly batch fillOrKill', async () => {
const fillableAmount = new BigNumber(5);
+ const partialFillTakerAmount = new BigNumber(2);
const signedOrder = await fillScenarios.createFillableSignedOrderAsync(
makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount,
);
- const tooLargeFillAmount = new BigNumber(7);
- const fillAmountDifference = tooLargeFillAmount.minus(fillableAmount);
- await zeroEx.token.transferAsync(takerTokenAddress, coinbase, takerAddress, fillAmountDifference);
- await zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, tooLargeFillAmount);
- await zeroEx.token.transferAsync(makerTokenAddress, coinbase, makerAddress, fillAmountDifference);
- await zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, tooLargeFillAmount);
-
- return expect(zeroEx.exchange.fillOrKillOrderAsync(
- signedOrder, tooLargeFillAmount, takerAddress,
- )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_REMAINING_FILL_AMOUNT);
- });
- });
- describe('successful fills', () => {
- it('should fill a valid order', async () => {
- const fillableAmount = new BigNumber(5);
- const signedOrder = await fillScenarios.createFillableSignedOrderAsync(
+ const anotherSignedOrder = await fillScenarios.createFillableSignedOrderAsync(
makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount,
);
- expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress))
- .to.be.bignumber.equal(fillableAmount);
- expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress))
- .to.be.bignumber.equal(0);
- expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress))
- .to.be.bignumber.equal(0);
- expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress))
- .to.be.bignumber.equal(fillableAmount);
- await zeroEx.exchange.fillOrKillOrderAsync(signedOrder, fillTakerAmount, takerAddress);
- expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress))
- .to.be.bignumber.equal(fillableAmount.minus(fillTakerAmount));
- expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress))
- .to.be.bignumber.equal(fillTakerAmount);
- expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress))
- .to.be.bignumber.equal(fillTakerAmount);
- expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress))
- .to.be.bignumber.equal(fillableAmount.minus(fillTakerAmount));
+ const orderFillOrKillRequests = [
+ {
+ signedOrder,
+ fillTakerAmount: partialFillTakerAmount,
+ },
+ {
+ signedOrder: anotherSignedOrder,
+ fillTakerAmount: partialFillTakerAmount,
+ },
+ ];
+ await zeroEx.exchange.batchFillOrKillAsync(orderFillOrKillRequests, takerAddress);
});
- it('should partially fill a valid order', async () => {
- const fillableAmount = new BigNumber(5);
- const signedOrder = await fillScenarios.createFillableSignedOrderAsync(
- makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount,
- );
- const partialFillAmount = new BigNumber(3);
- await zeroEx.exchange.fillOrKillOrderAsync(signedOrder, partialFillAmount, takerAddress);
- expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress))
- .to.be.bignumber.equal(fillableAmount.minus(partialFillAmount));
- expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress))
- .to.be.bignumber.equal(partialFillAmount);
- expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress))
- .to.be.bignumber.equal(partialFillAmount);
- expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress))
- .to.be.bignumber.equal(fillableAmount.minus(partialFillAmount));
+ });
+ describe('#fillOrKillOrderAsync', () => {
+ describe('failed fillOrKill', () => {
+ it('should throw if remaining fillAmount is less then the desired fillAmount', async () => {
+ const fillableAmount = new BigNumber(5);
+ const signedOrder = await fillScenarios.createFillableSignedOrderAsync(
+ makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount,
+ );
+ const tooLargeFillAmount = new BigNumber(7);
+ const fillAmountDifference = tooLargeFillAmount.minus(fillableAmount);
+ await zeroEx.token.transferAsync(takerTokenAddress, coinbase, takerAddress, fillAmountDifference);
+ await zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, tooLargeFillAmount);
+ await zeroEx.token.transferAsync(makerTokenAddress, coinbase, makerAddress, fillAmountDifference);
+ await zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, tooLargeFillAmount);
+
+ return expect(zeroEx.exchange.fillOrKillOrderAsync(
+ signedOrder, tooLargeFillAmount, takerAddress,
+ )).to.be.rejectedWith(ExchangeContractErrs.INSUFFICIENT_REMAINING_FILL_AMOUNT);
+ });
+ });
+ describe('successful fills', () => {
+ it('should fill a valid order', async () => {
+ const fillableAmount = new BigNumber(5);
+ const signedOrder = await fillScenarios.createFillableSignedOrderAsync(
+ makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount,
+ );
+ expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress))
+ .to.be.bignumber.equal(fillableAmount);
+ expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress))
+ .to.be.bignumber.equal(0);
+ expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress))
+ .to.be.bignumber.equal(0);
+ expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress))
+ .to.be.bignumber.equal(fillableAmount);
+ await zeroEx.exchange.fillOrKillOrderAsync(signedOrder, fillTakerAmount, takerAddress);
+ expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress))
+ .to.be.bignumber.equal(fillableAmount.minus(fillTakerAmount));
+ expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress))
+ .to.be.bignumber.equal(fillTakerAmount);
+ expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress))
+ .to.be.bignumber.equal(fillTakerAmount);
+ expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress))
+ .to.be.bignumber.equal(fillableAmount.minus(fillTakerAmount));
+ });
+ it('should partially fill a valid order', async () => {
+ const fillableAmount = new BigNumber(5);
+ const signedOrder = await fillScenarios.createFillableSignedOrderAsync(
+ makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount,
+ );
+ const partialFillAmount = new BigNumber(3);
+ await zeroEx.exchange.fillOrKillOrderAsync(signedOrder, partialFillAmount, takerAddress);
+ expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, makerAddress))
+ .to.be.bignumber.equal(fillableAmount.minus(partialFillAmount));
+ expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, makerAddress))
+ .to.be.bignumber.equal(partialFillAmount);
+ expect(await zeroEx.token.getBalanceAsync(makerTokenAddress, takerAddress))
+ .to.be.bignumber.equal(partialFillAmount);
+ expect(await zeroEx.token.getBalanceAsync(takerTokenAddress, takerAddress))
+ .to.be.bignumber.equal(fillableAmount.minus(partialFillAmount));
+ });
});
});
});