aboutsummaryrefslogtreecommitdiffstats
path: root/packages/order-utils
diff options
context:
space:
mode:
authorBrandon Millman <brandon@0xproject.com>2018-08-06 05:47:42 +0800
committerGitHub <noreply@github.com>2018-08-06 05:47:42 +0800
commit47fef1f8ff583879fa83a7a44086b14f69c09e21 (patch)
tree333f93d99e32e75739e3f76bbaa8c393249f71ff /packages/order-utils
parent9f7f61085c1a6989b79df575beb0b5d8f2b3652d (diff)
parent3cb955c136bf47b5f40cdbc44bcc4d19ec6d6453 (diff)
downloaddexon-sol-tools-47fef1f8ff583879fa83a7a44086b14f69c09e21.tar
dexon-sol-tools-47fef1f8ff583879fa83a7a44086b14f69c09e21.tar.gz
dexon-sol-tools-47fef1f8ff583879fa83a7a44086b14f69c09e21.tar.bz2
dexon-sol-tools-47fef1f8ff583879fa83a7a44086b14f69c09e21.tar.lz
dexon-sol-tools-47fef1f8ff583879fa83a7a44086b14f69c09e21.tar.xz
dexon-sol-tools-47fef1f8ff583879fa83a7a44086b14f69c09e21.tar.zst
dexon-sol-tools-47fef1f8ff583879fa83a7a44086b14f69c09e21.zip
Merge pull request #936 from 0xProject/feature/contract-wrappers/forwader-optimizations
ForwarderWrapper order optimizations
Diffstat (limited to 'packages/order-utils')
-rw-r--r--packages/order-utils/CHANGELOG.json10
-rw-r--r--packages/order-utils/src/constants.ts2
-rw-r--r--packages/order-utils/src/index.ts10
-rw-r--r--packages/order-utils/src/order_factory.ts78
-rw-r--r--packages/order-utils/src/types.ts12
5 files changed, 89 insertions, 23 deletions
diff --git a/packages/order-utils/CHANGELOG.json b/packages/order-utils/CHANGELOG.json
index a399f5ea1..70a75854a 100644
--- a/packages/order-utils/CHANGELOG.json
+++ b/packages/order-utils/CHANGELOG.json
@@ -1,5 +1,15 @@
[
{
+ "version": "1.0.1-rc.3",
+ "changes": [
+ {
+ "note":
+ "Added a synchronous `createOrder` method in `orderFactory`, updated public interfaces to support some optional parameters",
+ "pr": 936
+ }
+ ]
+ },
+ {
"version": "1.0.1-rc.2",
"changes": [
{
diff --git a/packages/order-utils/src/constants.ts b/packages/order-utils/src/constants.ts
index bb7482184..ea3f8b932 100644
--- a/packages/order-utils/src/constants.ts
+++ b/packages/order-utils/src/constants.ts
@@ -10,4 +10,6 @@ export const constants = {
ERC721_ASSET_DATA_MINIMUM_BYTE_LENGTH: 53,
SELECTOR_LENGTH: 4,
BASE_16: 16,
+ INFINITE_TIMESTAMP_SEC: new BigNumber(2524604400), // Close to infinite
+ ZERO_AMOUNT: new BigNumber(0),
};
diff --git a/packages/order-utils/src/index.ts b/packages/order-utils/src/index.ts
index 76be63bb8..129eb0a3d 100644
--- a/packages/order-utils/src/index.ts
+++ b/packages/order-utils/src/index.ts
@@ -13,7 +13,15 @@ export { orderFactory } from './order_factory';
export { constants } from './constants';
export { crypto } from './crypto';
export { generatePseudoRandomSalt } from './salt';
-export { OrderError, MessagePrefixType, MessagePrefixOpts, EIP712Parameter, EIP712Schema, EIP712Types } from './types';
+export {
+ CreateOrderOpts,
+ OrderError,
+ MessagePrefixType,
+ MessagePrefixOpts,
+ EIP712Parameter,
+ EIP712Schema,
+ EIP712Types,
+} from './types';
export { AbstractBalanceAndProxyAllowanceFetcher } from './abstract/abstract_balance_and_proxy_allowance_fetcher';
export { AbstractOrderFilledCancelledFetcher } from './abstract/abstract_order_filled_cancelled_fetcher';
export { BalanceAndProxyAllowanceLazyStore } from './store/balance_and_proxy_allowance_lazy_store';
diff --git a/packages/order-utils/src/order_factory.ts b/packages/order-utils/src/order_factory.ts
index 803cb82b1..14727fd97 100644
--- a/packages/order-utils/src/order_factory.ts
+++ b/packages/order-utils/src/order_factory.ts
@@ -1,49 +1,63 @@
-import { ECSignature, SignedOrder } from '@0xproject/types';
+import { ECSignature, Order, SignedOrder } from '@0xproject/types';
import { BigNumber } from '@0xproject/utils';
import { Provider } from 'ethereum-types';
import * as ethUtil from 'ethereumjs-util';
import * as _ from 'lodash';
+import { constants } from './constants';
import { orderHashUtils } from './order_hash';
import { generatePseudoRandomSalt } from './salt';
import { ecSignOrderHashAsync } from './signature_utils';
-import { MessagePrefixType } from './types';
+import { CreateOrderOpts, MessagePrefixType } from './types';
export const orderFactory = {
- async createSignedOrderAsync(
- provider: Provider,
+ createOrder(
makerAddress: string,
- takerAddress: string,
- senderAddress: string,
- makerFee: BigNumber,
- takerFee: BigNumber,
makerAssetAmount: BigNumber,
makerAssetData: string,
takerAssetAmount: BigNumber,
takerAssetData: string,
exchangeAddress: string,
- feeRecipientAddress: string,
- expirationTimeSecondsIfExists?: BigNumber,
- ): Promise<SignedOrder> {
- const defaultExpirationUnixTimestampSec = new BigNumber(2524604400); // Close to infinite
- const expirationTimeSeconds = _.isUndefined(expirationTimeSecondsIfExists)
- ? defaultExpirationUnixTimestampSec
- : expirationTimeSecondsIfExists;
+ createOrderOpts: CreateOrderOpts = generateDefaultCreateOrderOpts(),
+ ): Order {
+ const defaultCreateOrderOpts = generateDefaultCreateOrderOpts();
const order = {
makerAddress,
- takerAddress,
- senderAddress,
- makerFee,
- takerFee,
makerAssetAmount,
takerAssetAmount,
makerAssetData,
takerAssetData,
- salt: generatePseudoRandomSalt(),
exchangeAddress,
- feeRecipientAddress,
- expirationTimeSeconds,
+ takerAddress: createOrderOpts.takerAddress || defaultCreateOrderOpts.takerAddress,
+ senderAddress: createOrderOpts.senderAddress || defaultCreateOrderOpts.senderAddress,
+ makerFee: createOrderOpts.makerFee || defaultCreateOrderOpts.makerFee,
+ takerFee: createOrderOpts.takerFee || defaultCreateOrderOpts.takerFee,
+ feeRecipientAddress: createOrderOpts.feeRecipientAddress || defaultCreateOrderOpts.feeRecipientAddress,
+ salt: createOrderOpts.salt || defaultCreateOrderOpts.salt,
+ expirationTimeSeconds:
+ createOrderOpts.expirationTimeSeconds || defaultCreateOrderOpts.expirationTimeSeconds,
};
+ return order;
+ },
+ async createSignedOrderAsync(
+ provider: Provider,
+ makerAddress: string,
+ makerAssetAmount: BigNumber,
+ makerAssetData: string,
+ takerAssetAmount: BigNumber,
+ takerAssetData: string,
+ exchangeAddress: string,
+ createOrderOpts?: CreateOrderOpts,
+ ): Promise<SignedOrder> {
+ const order = orderFactory.createOrder(
+ makerAddress,
+ makerAssetAmount,
+ makerAssetData,
+ takerAssetAmount,
+ takerAssetData,
+ exchangeAddress,
+ createOrderOpts,
+ );
const orderHash = orderHashUtils.getOrderHashHex(order);
const messagePrefixOpts = {
prefixType: MessagePrefixType.EthSign,
@@ -56,6 +70,26 @@ export const orderFactory = {
},
};
+function generateDefaultCreateOrderOpts(): {
+ takerAddress: string;
+ senderAddress: string;
+ makerFee: BigNumber;
+ takerFee: BigNumber;
+ feeRecipientAddress: string;
+ salt: BigNumber;
+ expirationTimeSeconds: BigNumber;
+} {
+ return {
+ takerAddress: constants.NULL_ADDRESS,
+ senderAddress: constants.NULL_ADDRESS,
+ makerFee: constants.ZERO_AMOUNT,
+ takerFee: constants.ZERO_AMOUNT,
+ feeRecipientAddress: constants.NULL_ADDRESS,
+ salt: generatePseudoRandomSalt(),
+ expirationTimeSeconds: constants.INFINITE_TIMESTAMP_SEC,
+ };
+}
+
function getVRSHexString(ecSignature: ECSignature): string {
const ETH_SIGN_SIGNATURE_TYPE = '03';
const vrs = `${intToHex(ecSignature.v)}${ethUtil.stripHexPrefix(ecSignature.r)}${ethUtil.stripHexPrefix(
diff --git a/packages/order-utils/src/types.ts b/packages/order-utils/src/types.ts
index b08e74e71..f44e94349 100644
--- a/packages/order-utils/src/types.ts
+++ b/packages/order-utils/src/types.ts
@@ -1,3 +1,5 @@
+import { BigNumber } from '@0xproject/utils';
+
export enum OrderError {
InvalidSignature = 'INVALID_SIGNATURE',
}
@@ -51,3 +53,13 @@ export enum EIP712Types {
String = 'string',
Uint256 = 'uint256',
}
+
+export interface CreateOrderOpts {
+ takerAddress?: string;
+ senderAddress?: string;
+ makerFee?: BigNumber;
+ takerFee?: BigNumber;
+ feeRecipientAddress?: string;
+ salt?: BigNumber;
+ expirationTimeSeconds?: BigNumber;
+}