aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/util
diff options
context:
space:
mode:
Diffstat (limited to 'packages/contracts/util')
-rw-r--r--packages/contracts/util/exchange_wrapper.ts60
-rw-r--r--packages/contracts/util/formatters.ts100
-rw-r--r--packages/contracts/util/order.ts106
-rw-r--r--packages/contracts/util/order_factory.ts45
-rw-r--r--packages/contracts/util/signed_order_utils.ts49
-rw-r--r--packages/contracts/util/types.ts33
6 files changed, 158 insertions, 235 deletions
diff --git a/packages/contracts/util/exchange_wrapper.ts b/packages/contracts/util/exchange_wrapper.ts
index b749cd72e..03d04629d 100644
--- a/packages/contracts/util/exchange_wrapper.ts
+++ b/packages/contracts/util/exchange_wrapper.ts
@@ -1,4 +1,4 @@
-import { TransactionReceiptWithDecodedLogs, ZeroEx } from '0x.js';
+import { SignedOrder, TransactionReceiptWithDecodedLogs, ZeroEx } from '0x.js';
import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';
import * as Web3 from 'web3';
@@ -6,7 +6,7 @@ import * as Web3 from 'web3';
import { ExchangeContract } from '../src/contract_wrappers/generated/exchange';
import { formatters } from './formatters';
-import { Order } from './order';
+import { signedOrderUtils } from './signed_order_utils';
export class ExchangeWrapper {
private _exchange: ExchangeContract;
@@ -16,7 +16,7 @@ export class ExchangeWrapper {
this._zeroEx = zeroEx;
}
public async fillOrderAsync(
- order: Order,
+ signedOrder: SignedOrder,
from: string,
opts: {
fillTakerTokenAmount?: BigNumber;
@@ -24,15 +24,19 @@ export class ExchangeWrapper {
} = {},
): Promise<TransactionReceiptWithDecodedLogs> {
const shouldThrowOnInsufficientBalanceOrAllowance = !!opts.shouldThrowOnInsufficientBalanceOrAllowance;
- const params = order.createFill(shouldThrowOnInsufficientBalanceOrAllowance, opts.fillTakerTokenAmount);
+ const params = signedOrderUtils.createFill(
+ signedOrder,
+ shouldThrowOnInsufficientBalanceOrAllowance,
+ opts.fillTakerTokenAmount,
+ );
const txHash = await this._exchange.fillOrder.sendTransactionAsync(
params.orderAddresses,
params.orderValues,
params.fillTakerTokenAmount,
params.shouldThrowOnInsufficientBalanceOrAllowance,
- params.v as number,
- params.r as string,
- params.s as string,
+ params.v,
+ params.r,
+ params.s,
{ from },
);
const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
@@ -41,11 +45,11 @@ export class ExchangeWrapper {
return tx;
}
public async cancelOrderAsync(
- order: Order,
+ signedOrder: SignedOrder,
from: string,
opts: { cancelTakerTokenAmount?: BigNumber } = {},
): Promise<TransactionReceiptWithDecodedLogs> {
- const params = order.createCancel(opts.cancelTakerTokenAmount);
+ const params = signedOrderUtils.createCancel(signedOrder, opts.cancelTakerTokenAmount);
const txHash = await this._exchange.cancelOrder.sendTransactionAsync(
params.orderAddresses,
params.orderValues,
@@ -58,19 +62,23 @@ export class ExchangeWrapper {
return tx;
}
public async fillOrKillOrderAsync(
- order: Order,
+ signedOrder: SignedOrder,
from: string,
opts: { fillTakerTokenAmount?: BigNumber } = {},
): Promise<TransactionReceiptWithDecodedLogs> {
const shouldThrowOnInsufficientBalanceOrAllowance = true;
- const params = order.createFill(shouldThrowOnInsufficientBalanceOrAllowance, opts.fillTakerTokenAmount);
+ const params = signedOrderUtils.createFill(
+ signedOrder,
+ shouldThrowOnInsufficientBalanceOrAllowance,
+ opts.fillTakerTokenAmount,
+ );
const txHash = await this._exchange.fillOrKillOrder.sendTransactionAsync(
params.orderAddresses,
params.orderValues,
params.fillTakerTokenAmount,
- params.v as number,
- params.r as string,
- params.s as string,
+ params.v,
+ params.r,
+ params.s,
{ from },
);
const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash);
@@ -79,7 +87,7 @@ export class ExchangeWrapper {
return tx;
}
public async batchFillOrdersAsync(
- orders: Order[],
+ orders: SignedOrder[],
from: string,
opts: {
fillTakerTokenAmounts?: BigNumber[];
@@ -108,7 +116,7 @@ export class ExchangeWrapper {
return tx;
}
public async batchFillOrKillOrdersAsync(
- orders: Order[],
+ orders: SignedOrder[],
from: string,
opts: { fillTakerTokenAmounts?: BigNumber[]; shouldThrowOnInsufficientBalanceOrAllowance?: boolean } = {},
): Promise<TransactionReceiptWithDecodedLogs> {
@@ -133,7 +141,7 @@ export class ExchangeWrapper {
return tx;
}
public async fillOrdersUpToAsync(
- orders: Order[],
+ orders: SignedOrder[],
from: string,
opts: { fillTakerTokenAmount: BigNumber; shouldThrowOnInsufficientBalanceOrAllowance?: boolean },
): Promise<TransactionReceiptWithDecodedLogs> {
@@ -159,7 +167,7 @@ export class ExchangeWrapper {
return tx;
}
public async batchCancelOrdersAsync(
- orders: Order[],
+ orders: SignedOrder[],
from: string,
opts: { cancelTakerTokenAmounts?: BigNumber[] } = {},
): Promise<TransactionReceiptWithDecodedLogs> {
@@ -175,19 +183,19 @@ export class ExchangeWrapper {
_.each(tx.logs, log => wrapLogBigNumbers(log));
return tx;
}
- public async getOrderHashAsync(order: Order): Promise<string> {
+ public async getOrderHashAsync(signedOrder: SignedOrder): Promise<string> {
const shouldThrowOnInsufficientBalanceOrAllowance = false;
- const params = order.createFill(shouldThrowOnInsufficientBalanceOrAllowance);
+ const params = signedOrderUtils.getOrderAddressesAndValues(signedOrder);
const orderHash = await this._exchange.getOrderHash(params.orderAddresses, params.orderValues);
return orderHash;
}
- public async isValidSignatureAsync(order: Order): Promise<boolean> {
+ public async isValidSignatureAsync(signedOrder: SignedOrder): Promise<boolean> {
const isValidSignature = await this._exchange.isValidSignature(
- order.params.maker,
- order.params.orderHashHex as string,
- order.params.v as number,
- order.params.r as string,
- order.params.s as string,
+ signedOrder.maker,
+ ZeroEx.getOrderHashHex(signedOrder),
+ signedOrder.ecSignature.v,
+ signedOrder.ecSignature.r,
+ signedOrder.ecSignature.s,
);
return isValidSignature;
}
diff --git a/packages/contracts/util/formatters.ts b/packages/contracts/util/formatters.ts
index e16fe8d45..8e0dfb09e 100644
--- a/packages/contracts/util/formatters.ts
+++ b/packages/contracts/util/formatters.ts
@@ -1,12 +1,12 @@
+import { SignedOrder } from '0x.js';
import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';
-import { Order } from './order';
import { BatchCancelOrders, BatchFillOrders, FillOrdersUpTo } from './types';
export const formatters = {
createBatchFill(
- orders: Order[],
+ signedOrders: SignedOrder[],
shouldThrowOnInsufficientBalanceOrAllowance: boolean,
fillTakerTokenAmounts: BigNumber[] = [],
) {
@@ -19,33 +19,33 @@ export const formatters = {
r: [],
s: [],
};
- _.forEach(orders, order => {
+ _.forEach(signedOrders, signedOrder => {
batchFill.orderAddresses.push([
- order.params.maker,
- order.params.taker,
- order.params.makerToken,
- order.params.takerToken,
- order.params.feeRecipient,
+ signedOrder.maker,
+ signedOrder.taker,
+ signedOrder.makerTokenAddress,
+ signedOrder.takerTokenAddress,
+ signedOrder.feeRecipient,
]);
batchFill.orderValues.push([
- order.params.makerTokenAmount,
- order.params.takerTokenAmount,
- order.params.makerFee,
- order.params.takerFee,
- order.params.expirationTimestampInSec,
- order.params.salt,
+ signedOrder.makerTokenAmount,
+ signedOrder.takerTokenAmount,
+ signedOrder.makerFee,
+ signedOrder.takerFee,
+ signedOrder.expirationUnixTimestampSec,
+ signedOrder.salt,
]);
- batchFill.v.push(order.params.v as number);
- batchFill.r.push(order.params.r as string);
- batchFill.s.push(order.params.s as string);
- if (fillTakerTokenAmounts.length < orders.length) {
- batchFill.fillTakerTokenAmounts.push(order.params.takerTokenAmount);
+ batchFill.v.push(signedOrder.ecSignature.v);
+ batchFill.r.push(signedOrder.ecSignature.r);
+ batchFill.s.push(signedOrder.ecSignature.s);
+ if (fillTakerTokenAmounts.length < signedOrders.length) {
+ batchFill.fillTakerTokenAmounts.push(signedOrder.takerTokenAmount);
}
});
return batchFill;
},
createFillUpTo(
- orders: Order[],
+ signedOrders: SignedOrder[],
shouldThrowOnInsufficientBalanceOrAllowance: boolean,
fillTakerTokenAmount: BigNumber,
) {
@@ -58,52 +58,52 @@ export const formatters = {
r: [],
s: [],
};
- orders.forEach(order => {
+ signedOrders.forEach(signedOrder => {
fillUpTo.orderAddresses.push([
- order.params.maker,
- order.params.taker,
- order.params.makerToken,
- order.params.takerToken,
- order.params.feeRecipient,
+ signedOrder.maker,
+ signedOrder.taker,
+ signedOrder.makerTokenAddress,
+ signedOrder.takerTokenAddress,
+ signedOrder.feeRecipient,
]);
fillUpTo.orderValues.push([
- order.params.makerTokenAmount,
- order.params.takerTokenAmount,
- order.params.makerFee,
- order.params.takerFee,
- order.params.expirationTimestampInSec,
- order.params.salt,
+ signedOrder.makerTokenAmount,
+ signedOrder.takerTokenAmount,
+ signedOrder.makerFee,
+ signedOrder.takerFee,
+ signedOrder.expirationUnixTimestampSec,
+ signedOrder.salt,
]);
- fillUpTo.v.push(order.params.v as number);
- fillUpTo.r.push(order.params.r as string);
- fillUpTo.s.push(order.params.s as string);
+ fillUpTo.v.push(signedOrder.ecSignature.v);
+ fillUpTo.r.push(signedOrder.ecSignature.r);
+ fillUpTo.s.push(signedOrder.ecSignature.s);
});
return fillUpTo;
},
- createBatchCancel(orders: Order[], cancelTakerTokenAmounts: BigNumber[] = []) {
+ createBatchCancel(signedOrders: SignedOrder[], cancelTakerTokenAmounts: BigNumber[] = []) {
const batchCancel: BatchCancelOrders = {
orderAddresses: [],
orderValues: [],
cancelTakerTokenAmounts,
};
- orders.forEach(order => {
+ signedOrders.forEach(signedOrder => {
batchCancel.orderAddresses.push([
- order.params.maker,
- order.params.taker,
- order.params.makerToken,
- order.params.takerToken,
- order.params.feeRecipient,
+ signedOrder.maker,
+ signedOrder.taker,
+ signedOrder.makerTokenAddress,
+ signedOrder.takerTokenAddress,
+ signedOrder.feeRecipient,
]);
batchCancel.orderValues.push([
- order.params.makerTokenAmount,
- order.params.takerTokenAmount,
- order.params.makerFee,
- order.params.takerFee,
- order.params.expirationTimestampInSec,
- order.params.salt,
+ signedOrder.makerTokenAmount,
+ signedOrder.takerTokenAmount,
+ signedOrder.makerFee,
+ signedOrder.takerFee,
+ signedOrder.expirationUnixTimestampSec,
+ signedOrder.salt,
]);
- if (cancelTakerTokenAmounts.length < orders.length) {
- batchCancel.cancelTakerTokenAmounts.push(order.params.takerTokenAmount);
+ if (cancelTakerTokenAmounts.length < signedOrders.length) {
+ batchCancel.cancelTakerTokenAmounts.push(signedOrder.takerTokenAmount);
}
});
return batchCancel;
diff --git a/packages/contracts/util/order.ts b/packages/contracts/util/order.ts
deleted file mode 100644
index 57bb2bcbf..000000000
--- a/packages/contracts/util/order.ts
+++ /dev/null
@@ -1,106 +0,0 @@
-import { BigNumber } from '@0xproject/utils';
-import { Web3Wrapper } from '@0xproject/web3-wrapper';
-import ethUtil = require('ethereumjs-util');
-import * as _ from 'lodash';
-
-import { crypto } from './crypto';
-import { OrderParams } from './types';
-
-export class Order {
- public params: OrderParams;
- private _web3Wrapper: Web3Wrapper;
- constructor(web3Wrapper: Web3Wrapper, params: OrderParams) {
- this.params = params;
- this._web3Wrapper = web3Wrapper;
- }
- public isValidSignature() {
- const { v, r, s } = this.params;
- if (_.isUndefined(v) || _.isUndefined(r) || _.isUndefined(s)) {
- throw new Error('Cannot call isValidSignature on unsigned order');
- }
- const orderHash = this._getOrderHash();
- const msgHash = ethUtil.hashPersonalMessage(ethUtil.toBuffer(orderHash));
- try {
- const pubKey = ethUtil.ecrecover(msgHash, v, ethUtil.toBuffer(r), ethUtil.toBuffer(s));
- const recoveredAddress = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey));
- return recoveredAddress === this.params.maker;
- } catch (err) {
- return false;
- }
- }
- public async signAsync() {
- const orderHash = this._getOrderHash();
- const signature = await this._web3Wrapper.signTransactionAsync(this.params.maker, orderHash);
- const { v, r, s } = ethUtil.fromRpcSig(signature);
- this.params = _.assign(this.params, {
- orderHashHex: orderHash,
- v,
- r: ethUtil.bufferToHex(r),
- s: ethUtil.bufferToHex(s),
- });
- }
- public createFill(shouldThrowOnInsufficientBalanceOrAllowance?: boolean, fillTakerTokenAmount?: BigNumber) {
- const fill = {
- orderAddresses: [
- this.params.maker,
- this.params.taker,
- this.params.makerToken,
- this.params.takerToken,
- this.params.feeRecipient,
- ],
- orderValues: [
- this.params.makerTokenAmount,
- this.params.takerTokenAmount,
- this.params.makerFee,
- this.params.takerFee,
- this.params.expirationTimestampInSec,
- this.params.salt,
- ],
- fillTakerTokenAmount: fillTakerTokenAmount || this.params.takerTokenAmount,
- shouldThrowOnInsufficientBalanceOrAllowance: !!shouldThrowOnInsufficientBalanceOrAllowance,
- v: this.params.v,
- r: this.params.r,
- s: this.params.s,
- };
- return fill;
- }
- public createCancel(cancelTakerTokenAmount?: BigNumber) {
- const cancel = {
- orderAddresses: [
- this.params.maker,
- this.params.taker,
- this.params.makerToken,
- this.params.takerToken,
- this.params.feeRecipient,
- ],
- orderValues: [
- this.params.makerTokenAmount,
- this.params.takerTokenAmount,
- this.params.makerFee,
- this.params.takerFee,
- this.params.expirationTimestampInSec,
- this.params.salt,
- ],
- cancelTakerTokenAmount: cancelTakerTokenAmount || this.params.takerTokenAmount,
- };
- return cancel;
- }
- private _getOrderHash(): string {
- const orderHash = crypto.solSHA3([
- this.params.exchangeContractAddress,
- this.params.maker,
- this.params.taker,
- this.params.makerToken,
- this.params.takerToken,
- this.params.feeRecipient,
- this.params.makerTokenAmount,
- this.params.takerTokenAmount,
- this.params.makerFee,
- this.params.takerFee,
- this.params.expirationTimestampInSec,
- this.params.salt,
- ]);
- const orderHashHex = ethUtil.bufferToHex(orderHash);
- return orderHashHex;
- }
-}
diff --git a/packages/contracts/util/order_factory.ts b/packages/contracts/util/order_factory.ts
index 2b50f13e8..8ba5df24a 100644
--- a/packages/contracts/util/order_factory.ts
+++ b/packages/contracts/util/order_factory.ts
@@ -1,32 +1,37 @@
-import { ZeroEx } from '0x.js';
+import { Order, SignedOrder, ZeroEx } from '0x.js';
import { BigNumber } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as _ from 'lodash';
-import { Order } from './order';
-import { DefaultOrderParams, OptionalOrderParams, OrderParams } from './types';
+import { DefaultOrderParams } from './types';
export class OrderFactory {
- private _defaultOrderParams: DefaultOrderParams;
- private _web3Wrapper: Web3Wrapper;
- constructor(web3Wrapper: Web3Wrapper, defaultOrderParams: DefaultOrderParams) {
+ private _defaultOrderParams: Partial<Order>;
+ private _zeroEx: ZeroEx;
+ constructor(zeroEx: ZeroEx, defaultOrderParams: Partial<Order>) {
this._defaultOrderParams = defaultOrderParams;
- this._web3Wrapper = web3Wrapper;
+ this._zeroEx = zeroEx;
}
- public async newSignedOrderAsync(customOrderParams: OptionalOrderParams = {}): Promise<Order> {
+ public async newSignedOrderAsync(customOrderParams: Partial<Order> = {}): Promise<SignedOrder> {
const randomExpiration = new BigNumber(Math.floor((Date.now() + Math.random() * 100000000000) / 1000));
- const orderParams: OrderParams = _.assign(
- {},
- {
- expirationTimestampInSec: randomExpiration,
- salt: ZeroEx.generatePseudoRandomSalt(),
- taker: ZeroEx.NULL_ADDRESS,
- },
- this._defaultOrderParams,
- customOrderParams,
+ const order = ({
+ expirationUnixTimestampSec: randomExpiration,
+ salt: ZeroEx.generatePseudoRandomSalt(),
+ taker: ZeroEx.NULL_ADDRESS,
+ ...this._defaultOrderParams,
+ ...customOrderParams,
+ } as any) as Order;
+ const orderHashHex = ZeroEx.getOrderHashHex(order);
+ const shouldAddPersonalMessagePrefix = false;
+ const ecSignature = await this._zeroEx.signOrderHashAsync(
+ orderHashHex,
+ order.maker,
+ shouldAddPersonalMessagePrefix,
);
- const order = new Order(this._web3Wrapper, orderParams);
- await order.signAsync();
- return order;
+ const signedOrder = {
+ ...order,
+ ecSignature,
+ };
+ return signedOrder;
}
}
diff --git a/packages/contracts/util/signed_order_utils.ts b/packages/contracts/util/signed_order_utils.ts
new file mode 100644
index 000000000..fc2f800cd
--- /dev/null
+++ b/packages/contracts/util/signed_order_utils.ts
@@ -0,0 +1,49 @@
+import { SignedOrder } from '0x.js';
+import { BigNumber } from '@0xproject/utils';
+import { Web3Wrapper } from '@0xproject/web3-wrapper';
+import ethUtil = require('ethereumjs-util');
+import * as _ from 'lodash';
+
+import { crypto } from './crypto';
+
+export const signedOrderUtils = {
+ createFill: (
+ signedOrder: SignedOrder,
+ shouldThrowOnInsufficientBalanceOrAllowance?: boolean,
+ fillTakerTokenAmount?: BigNumber,
+ ) => {
+ const fill = {
+ ...signedOrderUtils.getOrderAddressesAndValues(signedOrder),
+ fillTakerTokenAmount: fillTakerTokenAmount || signedOrder.takerTokenAmount,
+ shouldThrowOnInsufficientBalanceOrAllowance: !!shouldThrowOnInsufficientBalanceOrAllowance,
+ ...signedOrder.ecSignature,
+ };
+ return fill;
+ },
+ createCancel(signedOrder: SignedOrder, cancelTakerTokenAmount?: BigNumber) {
+ const cancel = {
+ ...signedOrderUtils.getOrderAddressesAndValues(signedOrder),
+ cancelTakerTokenAmount: cancelTakerTokenAmount || signedOrder.takerTokenAmount,
+ };
+ return cancel;
+ },
+ getOrderAddressesAndValues(signedOrder: SignedOrder) {
+ return {
+ orderAddresses: [
+ signedOrder.maker,
+ signedOrder.taker,
+ signedOrder.makerTokenAddress,
+ signedOrder.takerTokenAddress,
+ signedOrder.feeRecipient,
+ ],
+ orderValues: [
+ signedOrder.makerTokenAmount,
+ signedOrder.takerTokenAmount,
+ signedOrder.makerFee,
+ signedOrder.takerFee,
+ signedOrder.expirationUnixTimestampSec,
+ signedOrder.salt,
+ ],
+ };
+ },
+};
diff --git a/packages/contracts/util/types.ts b/packages/contracts/util/types.ts
index 38fd33161..65bc26f79 100644
--- a/packages/contracts/util/types.ts
+++ b/packages/contracts/util/types.ts
@@ -49,39 +49,6 @@ export interface DefaultOrderParams {
takerFee: BigNumber;
}
-export interface OptionalOrderParams {
- exchangeContractAddress?: string;
- maker?: string;
- taker?: string;
- feeRecipient?: string;
- makerToken?: string;
- takerToken?: string;
- makerTokenAmount?: BigNumber;
- takerTokenAmount?: BigNumber;
- makerFee?: BigNumber;
- takerFee?: BigNumber;
- expirationTimestampInSec?: BigNumber;
-}
-
-export interface OrderParams {
- exchangeContractAddress: string;
- maker: string;
- taker: string;
- feeRecipient: string;
- makerToken: string;
- takerToken: string;
- makerTokenAmount: BigNumber;
- takerTokenAmount: BigNumber;
- makerFee: BigNumber;
- takerFee: BigNumber;
- expirationTimestampInSec: BigNumber;
- salt: BigNumber;
- orderHashHex?: string;
- v?: number;
- r?: string;
- s?: string;
-}
-
export interface TransactionDataParams {
name: string;
abi: Web3.AbiDefinition[];