aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorLeonid <logvinov.leon@gmail.com>2017-07-05 09:17:57 +0800
committerGitHub <noreply@github.com>2017-07-05 09:17:57 +0800
commit74b2308488832290340f3a6c6473ab7340510dfc (patch)
treebe598e0355a72486125cacfad837a2d06342f170 /src/utils
parent3302d18f6e0a4b7e51b318959c6b2d040ae3c5ed (diff)
parent371acc0ba12197de735dea20e09d50bbfd524118 (diff)
downloaddexon-sol-tools-74b2308488832290340f3a6c6473ab7340510dfc.tar
dexon-sol-tools-74b2308488832290340f3a6c6473ab7340510dfc.tar.gz
dexon-sol-tools-74b2308488832290340f3a6c6473ab7340510dfc.tar.bz2
dexon-sol-tools-74b2308488832290340f3a6c6473ab7340510dfc.tar.lz
dexon-sol-tools-74b2308488832290340f3a6c6473ab7340510dfc.tar.xz
dexon-sol-tools-74b2308488832290340f3a6c6473ab7340510dfc.tar.zst
dexon-sol-tools-74b2308488832290340f3a6c6473ab7340510dfc.zip
Merge pull request #90 from 0xProject/subscribe-token
Add implementation and tests for zeroEx.token.subscribeAsync
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/assert.ts16
-rw-r--r--src/utils/event_utils.ts44
-rw-r--r--src/utils/schema_validator.ts7
-rw-r--r--src/utils/utils.ts4
4 files changed, 63 insertions, 8 deletions
diff --git a/src/utils/assert.ts b/src/utils/assert.ts
index 38c1d4aae..b3c30c11d 100644
--- a/src/utils/assert.ts
+++ b/src/utils/assert.ts
@@ -5,6 +5,7 @@ import {Web3Wrapper} from '../web3_wrapper';
import {Schema} from 'jsonschema';
import {SchemaValidator} from './schema_validator';
import {utils} from './utils';
+import {StringEnum} from '../types';
const HEX_REGEX = /^0x[0-9A-F]*$/i;
@@ -27,6 +28,16 @@ export const assert = {
const web3 = new Web3();
this.assert(web3.isAddress(value), this.typeAssertionMessage(variableName, 'ETHAddressHex', value));
},
+ doesBelongToStringEnum(variableName: string, value: string, stringEnum: StringEnum): void {
+ const doesBelongToStringEnum = !_.isUndefined(stringEnum[value]);
+ const enumValues = _.keys(stringEnum);
+ const enumValuesAsStrings = _.map(enumValues, enumValue => `'${enumValue}'`);
+ const enumValuesAsString = enumValuesAsStrings.join(', ');
+ assert.assert(
+ doesBelongToStringEnum,
+ `Expected ${variableName} to be one of: ${enumValuesAsString}, encountered: ${value}`,
+ );
+ },
async isSenderAddressAsync(variableName: string, senderAddressHex: string,
web3Wrapper: Web3Wrapper): Promise<void> {
assert.isETHAddressHex(variableName, senderAddressHex);
@@ -45,13 +56,10 @@ export const assert = {
isNumber(variableName: string, value: number): void {
this.assert(_.isFinite(value), this.typeAssertionMessage(variableName, 'number', value));
},
- isValidOrderHash(variableName: string, value: string): void {
- this.assert(utils.isValidOrderHash(value), this.typeAssertionMessage(variableName, 'orderHash', value));
- },
isBoolean(variableName: string, value: boolean): void {
this.assert(_.isBoolean(value), this.typeAssertionMessage(variableName, 'boolean', value));
},
- doesConformToSchema(variableName: string, value: object, schema: Schema): void {
+ doesConformToSchema(variableName: string, value: any, schema: Schema): void {
const schemaValidator = new SchemaValidator();
const validationResult = schemaValidator.validate(value, schema);
const hasValidationErrors = validationResult.errors.length > 0;
diff --git a/src/utils/event_utils.ts b/src/utils/event_utils.ts
new file mode 100644
index 000000000..07418cbc4
--- /dev/null
+++ b/src/utils/event_utils.ts
@@ -0,0 +1,44 @@
+import * as _ from 'lodash';
+import * as Web3 from 'web3';
+import {EventCallback, ContractEventArg, ContractEvent, ContractEventObj, ContractEventEmitter} from '../types';
+import * as BigNumber from 'bignumber.js';
+import promisify = require('es6-promisify');
+
+export const eventUtils = {
+ wrapEventEmitter(event: ContractEventObj): ContractEventEmitter {
+ const watch = (eventCallback: EventCallback) => {
+ const bignumberWrappingEventCallback = eventUtils._getBigNumberWrappingEventCallback(eventCallback);
+ event.watch(bignumberWrappingEventCallback);
+ };
+ const zeroExEvent = {
+ watch,
+ stopWatchingAsync: async () => {
+ await promisify(event.stopWatching, event)();
+ },
+ };
+ return zeroExEvent;
+ },
+ /**
+ * Wraps eventCallback function so that all the BigNumber arguments are wrapped in a newer version of BigNumber.
+ * @param eventCallback Event callback function to be wrapped
+ * @return Wrapped event callback function
+ */
+ _getBigNumberWrappingEventCallback(eventCallback: EventCallback): EventCallback {
+ const bignumberWrappingEventCallback = (err: Error, event: ContractEvent) => {
+ if (_.isNull(err)) {
+ const wrapIfBigNumber = (value: ContractEventArg): ContractEventArg => {
+ // HACK: The old version of BigNumber used by Web3@0.19.0 does not support the `isBigNumber`
+ // and checking for a BigNumber instance using `instanceof` does not work either. We therefore
+ // compare the constructor functions of the possible BigNumber instance and the BigNumber used by
+ // Web3.
+ const web3BigNumber = (Web3.prototype as any).BigNumber;
+ const isWeb3BigNumber = web3BigNumber.toString() === value.constructor.toString();
+ return isWeb3BigNumber ? new BigNumber(value) : value;
+ };
+ event.args = _.mapValues(event.args, wrapIfBigNumber);
+ }
+ eventCallback(err, event);
+ };
+ return bignumberWrappingEventCallback;
+ },
+};
diff --git a/src/utils/schema_validator.ts b/src/utils/schema_validator.ts
index e3f911adb..58450ff20 100644
--- a/src/utils/schema_validator.ts
+++ b/src/utils/schema_validator.ts
@@ -1,8 +1,11 @@
import {Validator, ValidatorResult, Schema} from 'jsonschema';
import {ecSignatureSchema, ecSignatureParameterSchema} from '../schemas/ec_signature_schema';
+import {orderHashSchema} from '../schemas/order_hash_schema';
import {orderSchema, signedOrderSchema} from '../schemas/order_schemas';
import {addressSchema, numberSchema} from '../schemas/basic_type_schemas';
import {tokenSchema} from '../schemas/token_schema';
+import {subscriptionOptsSchema, blockParamSchema} from '../schemas/subscription_opts_schema';
+import {indexFilterValuesSchema} from '../schemas/index_filter_values_schema';
import {orderFillOrKillRequestsSchema} from '../schemas/order_fill_or_kill_requests_schema';
export class SchemaValidator {
@@ -13,8 +16,12 @@ export class SchemaValidator {
this.validator.addSchema(orderSchema, orderSchema.id);
this.validator.addSchema(numberSchema, numberSchema.id);
this.validator.addSchema(addressSchema, addressSchema.id);
+ this.validator.addSchema(orderHashSchema, orderHashSchema.id);
+ this.validator.addSchema(blockParamSchema, blockParamSchema.id);
this.validator.addSchema(ecSignatureSchema, ecSignatureSchema.id);
this.validator.addSchema(signedOrderSchema, signedOrderSchema.id);
+ this.validator.addSchema(subscriptionOptsSchema, subscriptionOptsSchema.id);
+ this.validator.addSchema(indexFilterValuesSchema, indexFilterValuesSchema.id);
this.validator.addSchema(ecSignatureParameterSchema, ecSignatureParameterSchema.id);
this.validator.addSchema(orderFillOrKillRequestsSchema, orderFillOrKillRequestsSchema.id);
}
diff --git a/src/utils/utils.ts b/src/utils/utils.ts
index 061e9f99a..ecc171bfe 100644
--- a/src/utils/utils.ts
+++ b/src/utils/utils.ts
@@ -25,10 +25,6 @@ export const utils = {
isTestRpc(nodeVersion: string): boolean {
return _.includes(nodeVersion, 'TestRPC');
},
- isValidOrderHash(orderHashHex: string): boolean {
- const isValid = /^0x[0-9A-F]{64}$/i.test(orderHashHex);
- return isValid;
- },
spawnSwitchErr(name: string, value: any): Error {
return new Error(`Unexpected switch value: ${value} encountered for ${name}`);
},