diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-09-05 21:29:29 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-09-05 21:29:29 +0800 |
commit | ee00769be171531db725288f8d485964941af529 (patch) | |
tree | e98d5c4cef11a67d7cb09f86051b8de9b7e243ca | |
parent | ec22097efb61d27aa73c979758bc0231cea7d61d (diff) | |
download | dexon-0x-contracts-ee00769be171531db725288f8d485964941af529.tar.gz dexon-0x-contracts-ee00769be171531db725288f8d485964941af529.tar.zst dexon-0x-contracts-ee00769be171531db725288f8d485964941af529.zip |
Use schema validation to distinguish txData argument
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | src/contract.ts | 9 | ||||
-rw-r--r-- | yarn.lock | 6 |
3 files changed, 12 insertions, 5 deletions
diff --git a/package.json b/package.json index 2f4bf41ee..50f54e1c4 100644 --- a/package.json +++ b/package.json @@ -90,7 +90,7 @@ "webpack": "^3.1.0" }, "dependencies": { - "0x-json-schemas": "^0.4.0", + "0x-json-schemas": "^0.5.0", "bignumber.js": "^4.0.2", "compare-versions": "^3.0.1", "es6-promisify": "^5.0.0", diff --git a/src/contract.ts b/src/contract.ts index 26da67b62..bd5afc16c 100644 --- a/src/contract.ts +++ b/src/contract.ts @@ -1,12 +1,14 @@ import * as Web3 from 'web3'; import * as _ from 'lodash'; import promisify = require('es6-promisify'); +import {SchemaValidator, schemas} from '0x-json-schemas'; export class Contract implements Web3.ContractInstance { public address: string; public abi: Web3.ContractAbi; private contract: Web3.ContractInstance; private defaults: Partial<Web3.TxData>; + private validator: SchemaValidator; // This class instance is going to be populated with functions and events depending on the ABI // and we don't know their types in advance [name: string]: any; @@ -17,6 +19,7 @@ export class Contract implements Web3.ContractInstance { this.defaults = defaults; this.populateEvents(); this.populateFunctions(); + this.validator = new SchemaValidator(); } private populateFunctions(): void { const functionsAbi = _.filter(this.abi, abiPart => abiPart.type === 'function'); @@ -47,7 +50,7 @@ export class Contract implements Web3.ContractInstance { const promise = new Promise((resolve, reject) => { const lastArg = args[args.length - 1]; let txData: Partial<Web3.TxData> = {}; - if (_.isObject(lastArg) && !_.isArray(lastArg) && !lastArg.isBigNumber) { + if (this.isTxData(lastArg)) { txData = args.pop(); } txData = { @@ -69,4 +72,8 @@ export class Contract implements Web3.ContractInstance { }; return promisifiedWithDefaultParams; } + private isTxData(lastArg: any): boolean { + const isValid = this.validator.isValid(lastArg, schemas.txDataSchema); + return isValid; + } } @@ -2,9 +2,9 @@ # yarn lockfile v1 -"0x-json-schemas@^0.4.0": - version "0.4.0" - resolved "https://registry.yarnpkg.com/0x-json-schemas/-/0x-json-schemas-0.4.0.tgz#93cfa771c6418b3ce67f97595beff3e4b67bc28b" +"0x-json-schemas@^0.5.0": + version "0.5.0" + resolved "https://registry.yarnpkg.com/0x-json-schemas/-/0x-json-schemas-0.5.0.tgz#36b1315480deb01dc0363ada949640e26f24a1d2" dependencies: jsonschema "^1.2.0" |