diff options
author | Amir Bandeali <abandeali1@gmail.com> | 2018-03-08 08:05:43 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2018-04-21 04:56:16 +0800 |
commit | 3ff8a319c5460caaf9edc1bea68e807fe0611aaa (patch) | |
tree | 77c255e42d1dfcedad7f1692c9467e28cd9a3802 /packages/contracts/src/utils/order_utils.ts | |
parent | cd8f8e1e4acc00e9e31265050456a4c98d79a7d4 (diff) | |
download | dexon-0x-contracts-3ff8a319c5460caaf9edc1bea68e807fe0611aaa.tar.gz dexon-0x-contracts-3ff8a319c5460caaf9edc1bea68e807fe0611aaa.tar.zst dexon-0x-contracts-3ff8a319c5460caaf9edc1bea68e807fe0611aaa.zip |
Add utils for hashing and signing orders, update wrappers
Diffstat (limited to 'packages/contracts/src/utils/order_utils.ts')
-rw-r--r-- | packages/contracts/src/utils/order_utils.ts | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/packages/contracts/src/utils/order_utils.ts b/packages/contracts/src/utils/order_utils.ts new file mode 100644 index 000000000..513d0087e --- /dev/null +++ b/packages/contracts/src/utils/order_utils.ts @@ -0,0 +1,80 @@ +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 { OrderStruct, SignatureType, SignedOrder, UnsignedOrder } from './types'; + +export const orderUtils = { + createFill: (signedOrder: SignedOrder, takerTokenFillAmount?: BigNumber) => { + const fill = { + order: orderUtils.getOrderStruct(signedOrder), + takerTokenFillAmount: takerTokenFillAmount || signedOrder.takerTokenAmount, + signature: signedOrder.signature, + }; + return fill; + }, + createCancel(signedOrder: SignedOrder, takerTokenCancelAmount?: BigNumber) { + const cancel = { + order: orderUtils.getOrderStruct(signedOrder), + takerTokenCancelAmount: takerTokenCancelAmount || signedOrder.takerTokenAmount, + }; + return cancel; + }, + getOrderStruct(signedOrder: SignedOrder): OrderStruct { + const orderStruct = { + makerAddress: signedOrder.makerAddress, + takerAddress: signedOrder.takerAddress, + makerTokenAddress: signedOrder.makerTokenAddress, + takerTokenAddress: signedOrder.takerTokenAddress, + feeRecipientAddress: signedOrder.feeRecipientAddress, + makerTokenAmount: signedOrder.makerTokenAmount, + takerTokenAmount: signedOrder.takerTokenAmount, + makerFeeAmount: signedOrder.makerFeeAmount, + takerFeeAmount: signedOrder.takerFeeAmount, + expirationTimeSeconds: signedOrder.expirationTimeSeconds, + salt: signedOrder.salt, + }; + return orderStruct; + }, + getOrderHashBuff(order: SignedOrder | UnsignedOrder): Buffer { + const orderSchemaHashBuff = crypto.solSHA3([ + 'address exchangeAddress', + 'address makerAddress', + 'address takerAddress', + 'address makerTokenAddress', + 'address takerTokenAddress', + 'address feeRecipientAddress', + 'uint256 makerTokenAmount', + 'uint256 takerTokenAmount', + 'uint256 makerFeeAmount', + 'uint256 takerFeeAmount', + 'uint256 expirationTimeSeconds', + 'uint256 salt', + ]); + const orderSchemaHashHex = `0x${orderSchemaHashBuff.toString('hex')}`; + const orderHashBuff = crypto.solSHA3([ + order.exchangeAddress, + order.makerAddress, + order.takerAddress, + order.makerTokenAddress, + order.takerTokenAddress, + order.feeRecipientAddress, + order.makerTokenAmount, + order.takerTokenAmount, + order.makerFeeAmount, + order.takerFeeAmount, + order.expirationTimeSeconds, + order.salt, + ]); + const orderHashHex = `0x${orderHashBuff.toString('hex')}`; + const prefixedOrderHashBuff = crypto.solSHA3([new BigNumber(orderSchemaHashHex), new BigNumber(orderHashHex)]); + return prefixedOrderHashBuff; + }, + getOrderHashHex(order: SignedOrder | UnsignedOrder): string { + const orderHashBuff = orderUtils.getOrderHashBuff(order); + const orderHashHex = `0x${orderHashBuff.toString('hex')}`; + return orderHashHex; + }, +}; |