diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-02-09 01:01:53 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-02-15 03:22:36 +0800 |
commit | f3e6ef0fa96e2252e41b7ed6f2c3e88a1560153e (patch) | |
tree | 0208e1e119e1a6976e8e2332eaca9681b4ae20ed /packages | |
parent | 18e1c2dea5d3c5387db6f616cd0ebbfe89dc308b (diff) | |
download | dexon-sol-tools-f3e6ef0fa96e2252e41b7ed6f2c3e88a1560153e.tar dexon-sol-tools-f3e6ef0fa96e2252e41b7ed6f2c3e88a1560153e.tar.gz dexon-sol-tools-f3e6ef0fa96e2252e41b7ed6f2c3e88a1560153e.tar.bz2 dexon-sol-tools-f3e6ef0fa96e2252e41b7ed6f2c3e88a1560153e.tar.lz dexon-sol-tools-f3e6ef0fa96e2252e41b7ed6f2c3e88a1560153e.tar.xz dexon-sol-tools-f3e6ef0fa96e2252e41b7ed6f2c3e88a1560153e.tar.zst dexon-sol-tools-f3e6ef0fa96e2252e41b7ed6f2c3e88a1560153e.zip |
Better validate ZeroExConfig on public networks
Diffstat (limited to 'packages')
-rw-r--r-- | packages/0x.js/src/0x.ts | 5 | ||||
-rw-r--r-- | packages/0x.js/src/schemas/zero_ex_config_schema.ts | 24 | ||||
-rw-r--r-- | packages/0x.js/src/schemas/zero_ex_private_network_config_schema.ts | 35 | ||||
-rw-r--r-- | packages/0x.js/src/schemas/zero_ex_public_network_config_schema.ts | 29 | ||||
-rw-r--r-- | packages/0x.js/src/types.ts | 36 | ||||
-rw-r--r-- | packages/assert/src/index.ts | 5 |
6 files changed, 99 insertions, 35 deletions
diff --git a/packages/0x.js/src/0x.ts b/packages/0x.js/src/0x.ts index 6cfa65cc2..d024e6097 100644 --- a/packages/0x.js/src/0x.ts +++ b/packages/0x.js/src/0x.ts @@ -13,6 +13,8 @@ import { TokenTransferProxyWrapper } from './contract_wrappers/token_transfer_pr import { TokenWrapper } from './contract_wrappers/token_wrapper'; import { OrderStateWatcher } from './order_watcher/order_state_watcher'; import { zeroExConfigSchema } from './schemas/zero_ex_config_schema'; +import { zeroExPrivateNetworkConfigSchema } from './schemas/zero_ex_private_network_config_schema'; +import { zeroExPublicNetworkConfigSchema } from './schemas/zero_ex_public_network_config_schema'; import { ECSignature, Order, SignedOrder, Web3Provider, ZeroExConfig, ZeroExError } from './types'; import { assert } from './utils/assert'; import { constants } from './utils/constants'; @@ -20,6 +22,9 @@ import { decorators } from './utils/decorators'; import { signatureUtils } from './utils/signature_utils'; import { utils } from './utils/utils'; +assert.schemaValidator.addSchema(zeroExPrivateNetworkConfigSchema); +assert.schemaValidator.addSchema(zeroExPublicNetworkConfigSchema); + /** * The ZeroEx class is the single entry-point into the 0x.js library. It contains all of the library's functionality * and all calls to the library should be made through a ZeroEx instance. diff --git a/packages/0x.js/src/schemas/zero_ex_config_schema.ts b/packages/0x.js/src/schemas/zero_ex_config_schema.ts index 546b1c2d0..a9c3c64fc 100644 --- a/packages/0x.js/src/schemas/zero_ex_config_schema.ts +++ b/packages/0x.js/src/schemas/zero_ex_config_schema.ts @@ -1,27 +1,5 @@ export const zeroExConfigSchema = { id: '/ZeroExConfig', - properties: { - networkId: { - type: 'number', - minimum: 0, - }, - gasPrice: { $ref: '/Number' }, - exchangeContractAddress: { $ref: '/Address' }, - tokenRegistryContractAddress: { $ref: '/Address' }, - orderWatcherConfig: { - type: 'object', - properties: { - pollingIntervalMs: { - type: 'number', - minimum: 0, - }, - numConfirmations: { - type: 'number', - minimum: 0, - }, - }, - }, - }, + oneOf: [{ $ref: '/ZeroExPrivateNetworkConfig' }, { $ref: '/ZeroExPublicNetworkConfig' }], type: 'object', - required: ['networkId'], }; diff --git a/packages/0x.js/src/schemas/zero_ex_private_network_config_schema.ts b/packages/0x.js/src/schemas/zero_ex_private_network_config_schema.ts new file mode 100644 index 000000000..f7f649a6d --- /dev/null +++ b/packages/0x.js/src/schemas/zero_ex_private_network_config_schema.ts @@ -0,0 +1,35 @@ +export const zeroExPrivateNetworkConfigSchema = { + id: '/ZeroExPrivateNetworkConfig', + properties: { + networkId: { + type: 'number', + minimum: 1, + }, + gasPrice: { $ref: '/Number' }, + zrxContractAddress: { $ref: '/Address' }, + exchangeContractAddress: { $ref: '/Address' }, + tokenRegistryContractAddress: { $ref: '/Address' }, + tokenTransferProxyContractAddress: { $ref: '/Address' }, + orderWatcherConfig: { + type: 'object', + properties: { + pollingIntervalMs: { + type: 'number', + minimum: 0, + }, + numConfirmations: { + type: 'number', + minimum: 0, + }, + }, + }, + }, + type: 'object', + required: [ + 'networkId', + 'zrxContractAddress', + 'exchangeContractAddress', + 'tokenRegistryContractAddress', + 'tokenTransferProxyContractAddress', + ], +}; diff --git a/packages/0x.js/src/schemas/zero_ex_public_network_config_schema.ts b/packages/0x.js/src/schemas/zero_ex_public_network_config_schema.ts new file mode 100644 index 000000000..9da31481a --- /dev/null +++ b/packages/0x.js/src/schemas/zero_ex_public_network_config_schema.ts @@ -0,0 +1,29 @@ +export const zeroExPublicNetworkConfigSchema = { + id: '/ZeroExPublicNetworkConfig', + properties: { + networkId: { + type: 'number', + enum: [1, 3, 4, 42, 50], + }, + gasPrice: { $ref: '/Number' }, + zrxContractAddress: { $ref: '/Address' }, + exchangeContractAddress: { $ref: '/Address' }, + tokenRegistryContractAddress: { $ref: '/Address' }, + tokenTransferProxyContractAddress: { $ref: '/Address' }, + orderWatcherConfig: { + type: 'object', + properties: { + pollingIntervalMs: { + type: 'number', + minimum: 0, + }, + numConfirmations: { + type: 'number', + minimum: 0, + }, + }, + }, + }, + type: 'object', + required: ['networkId'], +}; diff --git a/packages/0x.js/src/types.ts b/packages/0x.js/src/types.ts index ab97f7775..f0660391b 100644 --- a/packages/0x.js/src/types.ts +++ b/packages/0x.js/src/types.ts @@ -9,6 +9,10 @@ import { ExchangeContractEventArgs, ExchangeEvents } from './contract_wrappers/g import { TokenContractEventArgs, TokenEvents } from './contract_wrappers/generated/token'; export enum ZeroExError { + ZRXAddressRequired = 'ZRX_ADDREESS_REQUIRED', + ExchangeAddressRequired = 'EXCHANGE_ADDREESS_REQUIRED', + TokenRegistryAddressRequired = 'TOKEN_REGISTRY_ADDREESS_REQUIRED', + TokenTransferProxyAddressRequired = 'TOKEN_TRANSFER_PROXY_ADDREESS_REQUIRED', ExchangeContractDoesNotExist = 'EXCHANGE_CONTRACT_DOES_NOT_EXIST', ZRXContractDoesNotExist = 'ZRX_CONTRACT_DOES_NOT_EXIST', EtherTokenContractDoesNotExist = 'ETHER_TOKEN_CONTRACT_DOES_NOT_EXIST', @@ -195,8 +199,28 @@ export interface OrderStateWatcherConfig { cleanupJobIntervalMs?: number; } +export interface ZeroExPublicNetworkConfig { + networkId: 1 | 3 | 4 | 42 | 50; + gasPrice?: BigNumber; + exchangeContractAddress?: string; + zrxContractAddress?: string; + tokenRegistryContractAddress?: string; + tokenTransferProxyContractAddress?: string; + orderWatcherConfig?: OrderStateWatcherConfig; +} + +export interface ZeroExPrivateNetworkConfig { + networkId: number; + gasPrice?: BigNumber; + exchangeContractAddress: string; + zrxContractAddress: string; + tokenRegistryContractAddress: string; + tokenTransferProxyContractAddress: string; + orderWatcherConfig?: OrderStateWatcherConfig; +} + /* - * networkId: The id of the underlying ethereum network your provider is connected to. (1-mainnet, 42-kovan, 50-testrpc) + * networkId: The id of the underlying ethereum network your provider is connected to. (1-mainnet, 3-ropsten, 4-rinkeby, 42-kovan, 50-testrpc) * gasPrice: Gas price to use with every transaction * exchangeContractAddress: The address of an exchange contract to use * zrxContractAddress: The address of the ZRX contract to use @@ -204,15 +228,7 @@ export interface OrderStateWatcherConfig { * tokenTransferProxyContractAddress: The address of the token transfer proxy contract to use * orderWatcherConfig: All the configs related to the orderWatcher */ -export interface ZeroExConfig { - networkId: number; - gasPrice?: BigNumber; - exchangeContractAddress?: string; - zrxContractAddress?: string; - tokenRegistryContractAddress?: string; - tokenTransferProxyContractAddress?: string; - orderWatcherConfig?: OrderStateWatcherConfig; -} +export type ZeroExConfig = ZeroExPublicNetworkConfig | ZeroExPrivateNetworkConfig; export type ArtifactContractName = 'ZRX' | 'TokenTransferProxy' | 'TokenRegistry' | 'Token' | 'Exchange' | 'EtherToken'; diff --git a/packages/assert/src/index.ts b/packages/assert/src/index.ts index 7ad574ec7..38b330a46 100644 --- a/packages/assert/src/index.ts +++ b/packages/assert/src/index.ts @@ -4,8 +4,10 @@ import * as _ from 'lodash'; import * as validUrl from 'valid-url'; const HEX_REGEX = /^0x[0-9A-F]*$/i; +const schemaValidator = new SchemaValidator(); export const assert = { + schemaValidator, isBigNumber(variableName: string, value: BigNumber): void { const isBigNumber = _.isObject(value) && (value as any).isBigNumber; this.assert(isBigNumber, this.typeAssertionMessage(variableName, 'BigNumber', value)); @@ -67,8 +69,7 @@ export const assert = { this.assert(isWeb3Provider, this.typeAssertionMessage(variableName, 'Web3.Provider', value)); }, doesConformToSchema(variableName: string, value: any, schema: Schema): void { - const schemaValidator = new SchemaValidator(); - const validationResult = schemaValidator.validate(value, schema); + const validationResult = assert.schemaValidator.validate(value, schema); const hasValidationErrors = validationResult.errors.length > 0; const msg = `Expected ${variableName} to conform to schema ${schema.id} Encountered: ${JSON.stringify(value, null, '\t')} |