diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-04-24 22:36:35 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-05-02 20:10:24 +0800 |
commit | 0499541e114e6dd36565428f4f914e0dbdece2b8 (patch) | |
tree | 6d4740f4343f4516a9ecafc4795b68aed0865c75 /packages/order-utils/src/order_factory.ts | |
parent | f08738e13379ea86a33ad9be9f7579e637e69acb (diff) | |
download | dexon-0x-contracts-0499541e114e6dd36565428f4f914e0dbdece2b8.tar.gz dexon-0x-contracts-0499541e114e6dd36565428f4f914e0dbdece2b8.tar.zst dexon-0x-contracts-0499541e114e6dd36565428f4f914e0dbdece2b8.zip |
Move order utils to @0xproject/order-utils
Diffstat (limited to 'packages/order-utils/src/order_factory.ts')
-rw-r--r-- | packages/order-utils/src/order_factory.ts | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/packages/order-utils/src/order_factory.ts b/packages/order-utils/src/order_factory.ts new file mode 100644 index 000000000..2759aac81 --- /dev/null +++ b/packages/order-utils/src/order_factory.ts @@ -0,0 +1,49 @@ +import { Provider, SignedOrder } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; +import * as _ from 'lodash'; + +import { getOrderHashHex } from './order_hash'; +import { generatePseudoRandomSalt } from './salt'; +import { signOrderHashAsync } from './signature_utils'; + +const SHOULD_ADD_PERSONAL_MESSAGE_PREFIX = false; + +export const orderFactory = { + async createSignedOrderAsync( + provider: Provider, + maker: string, + taker: string, + makerFee: BigNumber, + takerFee: BigNumber, + makerTokenAmount: BigNumber, + makerTokenAddress: string, + takerTokenAmount: BigNumber, + takerTokenAddress: string, + exchangeContractAddress: string, + feeRecipient: string, + expirationUnixTimestampSecIfExists?: BigNumber, + ): Promise<SignedOrder> { + const defaultExpirationUnixTimestampSec = new BigNumber(2524604400); // Close to infinite + const expirationUnixTimestampSec = _.isUndefined(expirationUnixTimestampSecIfExists) + ? defaultExpirationUnixTimestampSec + : expirationUnixTimestampSecIfExists; + const order = { + maker, + taker, + makerFee, + takerFee, + makerTokenAmount, + takerTokenAmount, + makerTokenAddress, + takerTokenAddress, + salt: generatePseudoRandomSalt(), + exchangeContractAddress, + feeRecipient, + expirationUnixTimestampSec, + }; + const orderHash = getOrderHashHex(order); + const ecSignature = await signOrderHashAsync(provider, orderHash, maker, SHOULD_ADD_PERSONAL_MESSAGE_PREFIX); + const signedOrder: SignedOrder = _.assign(order, { ecSignature }); + return signedOrder; + }, +}; |