aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/utils.ts
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2017-06-07 17:04:42 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2017-06-07 17:04:42 +0800
commit9daca6a4be95a87a63e293300d0768e3e63162d2 (patch)
treeffa51a7275532557a6bd5097f1ced3a19b0725d0 /src/utils/utils.ts
parent56dc33f3a6ef0a9ae00d62d816000dab4cfea07f (diff)
downloaddexon-sol-tools-9daca6a4be95a87a63e293300d0768e3e63162d2.tar
dexon-sol-tools-9daca6a4be95a87a63e293300d0768e3e63162d2.tar.gz
dexon-sol-tools-9daca6a4be95a87a63e293300d0768e3e63162d2.tar.bz2
dexon-sol-tools-9daca6a4be95a87a63e293300d0768e3e63162d2.tar.lz
dexon-sol-tools-9daca6a4be95a87a63e293300d0768e3e63162d2.tar.xz
dexon-sol-tools-9daca6a4be95a87a63e293300d0768e3e63162d2.tar.zst
dexon-sol-tools-9daca6a4be95a87a63e293300d0768e3e63162d2.zip
Address feedback
Diffstat (limited to 'src/utils/utils.ts')
-rw-r--r--src/utils/utils.ts35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/utils/utils.ts b/src/utils/utils.ts
index 114b46f6c..4cea36127 100644
--- a/src/utils/utils.ts
+++ b/src/utils/utils.ts
@@ -1,5 +1,12 @@
import * as _ from 'lodash';
import * as BN from 'bn.js';
+import * as ethABI from 'ethereumjs-abi';
+import * as ethUtil from 'ethereumjs-util';
+import {orderSchema} from '../schemas/order_schemas';
+import {SchemaValidator} from './schema_validator';
+import {Order, SolidityTypes} from '../types';
+import {assert} from './assert';
+import * as BigNumber from 'bignumber.js';
export const utils = {
/**
@@ -25,4 +32,32 @@ export const utils = {
spawnSwitchErr(name: string, value: any) {
return new Error(`Unexpected switch value: ${value} encountered for ${name}`);
},
+ getOrderHashHex(order: Order, exchangeContractAddr: string): string {
+ assert.doesConformToSchema('order',
+ SchemaValidator.convertToJSONSchemaCompatibleObject(order as object),
+ orderSchema);
+
+ const orderParts = [
+ {value: exchangeContractAddr, type: SolidityTypes.address},
+ {value: order.maker, type: SolidityTypes.address},
+ {value: order.taker, type: SolidityTypes.address},
+ {value: order.makerTokenAddress, type: SolidityTypes.address},
+ {value: order.takerTokenAddress, type: SolidityTypes.address},
+ {value: order.feeRecipient, type: SolidityTypes.address},
+ {value: utils.bigNumberToBN(order.makerTokenAmount), type: SolidityTypes.uint256},
+ {value: utils.bigNumberToBN(order.takerTokenAmount), type: SolidityTypes.uint256},
+ {value: utils.bigNumberToBN(order.makerFee), type: SolidityTypes.uint256},
+ {value: utils.bigNumberToBN(order.takerFee), type: SolidityTypes.uint256},
+ {value: utils.bigNumberToBN(order.expirationUnixTimestampSec), type: SolidityTypes.uint256},
+ {value: utils.bigNumberToBN(order.salt), type: SolidityTypes.uint256},
+ ];
+ const types = _.map(orderParts, o => o.type);
+ const values = _.map(orderParts, o => o.value);
+ const hashBuff = ethABI.soliditySHA3(types, values);
+ const hashHex = ethUtil.bufferToHex(hashBuff);
+ return hashHex;
+ },
+ getCurrentUnixTimestamp(): BigNumber.BigNumber {
+ return new BigNumber(Date.now() / 1000);
+ },
};