aboutsummaryrefslogtreecommitdiffstats
path: root/packages/order-utils/src/order_factory.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/order-utils/src/order_factory.ts')
-rw-r--r--packages/order-utils/src/order_factory.ts97
1 files changed, 56 insertions, 41 deletions
diff --git a/packages/order-utils/src/order_factory.ts b/packages/order-utils/src/order_factory.ts
index 14122f353..46a69ae4d 100644
--- a/packages/order-utils/src/order_factory.ts
+++ b/packages/order-utils/src/order_factory.ts
@@ -1,75 +1,90 @@
-import { ECSignature, SignedOrder } from '@0xproject/types';
+import { Order, SignedOrder, SignerType } 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 { signatureUtils } from './signature_utils';
-import { MessagePrefixType } from './types';
+import { CreateOrderOpts } 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,
- shouldAddPrefixBeforeCallingEthSign: false,
- };
- const ecSignature = await signatureUtils.ecSignOrderHashAsync(
+ const signature = await signatureUtils.ecSignOrderHashAsync(
provider,
orderHash,
makerAddress,
- messagePrefixOpts,
+ SignerType.Default,
);
- const signature = getVRSHexString(ecSignature);
const signedOrder: SignedOrder = _.assign(order, { signature });
return signedOrder;
},
};
-function getVRSHexString(ecSignature: ECSignature): string {
- const ETH_SIGN_SIGNATURE_TYPE = '03';
- const vrs = `${intToHex(ecSignature.v)}${ethUtil.stripHexPrefix(ecSignature.r)}${ethUtil.stripHexPrefix(
- ecSignature.s,
- )}${ETH_SIGN_SIGNATURE_TYPE}`;
- return vrs;
-}
-
-function intToHex(i: number): string {
- const hex = ethUtil.bufferToHex(ethUtil.toBuffer(i));
- return hex;
+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,
+ };
}