import { assert } from '@0x/assert' ; import { BigNumber } from '@0x/utils' ; import * as crypto from 'crypto' ; import * as fs from 'fs' ; import * as _ from 'lodash' ; import * as path from 'path' ; import { NULL_BYTES } from './constants' ; const metadataPath = path . join ( __dirname , '../../metadata.json' ) ; enum EnvVarType { Port , ChainId , FeeRecipient , UnitAmount , Url , WhitelistAllTokens , Boolean , FeeAssetData } export const WHITELISTED_TOKENS = _ . isEmpty ( process . env . WHITELIST_ALL_TOKENS ) ? [ '0x2002d3812f58e35f0ea1ffbf80a75a38c32175fa' , '0xd0a1e359811322d97991e03f863a0c30c2cf029c' , ] : assertEnvVarType ( 'WHITELIST_ALL_TOKENS' , process . env . WHITELIST_ALL_TOKENS , EnvVarType . WhitelistAllTokens ) ; export const HTTP_PORT = _ . isEmpty ( process . env . HTTP_PORT ) ? 3000 : assertEnvVarType ( 'HTTP_PORT' , process . env . HTTP_PORT , EnvVarType . Port ) ; export const CHAIN_ID = _ . isEmpty ( process . env . CHAIN_ID ) ? 42 : assertEnvVarType ( 'CHAIN_ID' , process . env . CHAIN_ID , EnvVarType . ChainId ) ; export const MESH_ENDPOINT = _ . isEmpty ( process . env . MESH_ENDPOINT ) ? 'ws://localhost:60557' : assertEnvVarType ( 'MESH_ENDPOINT' , process . env . MESH_ENDPOINT , EnvVarType . Url ) ; export const FEE_RECIPIENT = _ . isEmpty ( process . env . FEE_RECIPIENT ) ? getDefaultFeeRecipient ( ) : assertEnvVarType ( 'FEE_RECIPIENT' , process . env . FEE_RECIPIENT , EnvVarType . FeeRecipient ) ; export const MAKER_FEE_UNIT_AMOUNT = _ . isEmpty ( process . env . MAKER_FEE_UNIT_AMOUNT ) ? new BigNumber ( 0 ) : assertEnvVarType ( 'MAKER_FEE_UNIT_AMOUNT' , process . env . MAKER_FEE_UNIT_AMOUNT , EnvVarType . UnitAmount ) ; export const TAKER_FEE_UNIT_AMOUNT = _ . isEmpty ( process . env . TAKER_FEE_UNIT_AMOUNT ) ? new BigNumber ( 0 ) : assertEnvVarType ( 'TAKER_FEE_UNIT_AMOUNT' , process . env . TAKER_FEE_UNIT_AMOUNT , EnvVarType . UnitAmount ) ; export const MAKER_FEE_ASSET_DATA = _ . isEmpty ( process . env . MAKER_FEE_ASSET_DATA ) ? NULL_BYTES : assertEnvVarType ( 'MAKER_FEE_ASSET_DATA' , process . env . MAKER_FEE_ASSET_DATA , EnvVarType . FeeAssetData ) ; export const TAKER_FEE_ASSET_DATA = _ . isEmpty ( process . env . TAKER_FEE_ASSET_DATA ) ? NULL_BYTES : assertEnvVarType ( 'TAKER_FEE_ASSET_DATA' , process . env . TAKER_FEE_ASSET_DATA , EnvVarType . FeeAssetData ) ; export const MAX_PER_PAGE = 1000 ; export const DEFAULT_ERC20_TOKEN_PRECISION = 18 ; export const DEFAULT_TAKER_SIMULATION_ADDRESS = '0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ; function assertEnvVarType ( name , value , expectedType ) { let returnValue ; switch ( expectedType ) { case EnvVarType . Port : try { returnValue = parseInt ( value , 10 ) ; const isWithinRange = returnValue >= 0 && returnValue <= 65535 ; if ( ! isWithinRange ) { throw new Error ( ) ; } } catch ( err ) { throw new Error ( `${ name } must be between 0 to 65535, found ${ value }.` ) ; } return returnValue ; case EnvVarType . ChainId : try { returnValue = parseInt ( value , 10 ) ; } catch ( err ) { throw new Error ( `${ name } must be a valid integer, found ${ value }.` ) ; } return returnValue ; case EnvVarType . FeeRecipient : assert . isETHAddressHex ( name , value ) ; return value ; case EnvVarType . Url : assert . isUri ( name , value ) ; return value ; case EnvVarType . Boolean : return value === 'true' ; case EnvVarType . UnitAmount : try { returnValue = new BigNumber ( parseFloat ( value ) ) ; if ( returnValue . isNegative ( ) ) { throw new Error ( ) ; } } catch ( err ) { throw new Error ( `${ name } must be valid number greater than 0.` ) ; } return returnValue ; case EnvVarType . WhitelistAllTokens : return '*' ; case EnvVarType . FeeAssetData : assert . isString ( name , value ) ; return value ; default : throw new Error ( `Unrecognised EnvVarType: ${ expectedType } encountered for variable ${ name }.` ) ; } } function getDefaultFeeRecipient ( ) { const metadata = JSON . parse ( fs . readFileSync ( metadataPath ) . toString ( ) ) ; const existingDefault = metadata . DEFAULT_FEE_RECIPIENT ; const newDefault = existingDefault || `0xabcabc${ crypto . randomBytes ( 17 ) . toString ( 'hex' ) }` ; if ( _ . isEmpty ( existingDefault ) ) { const metadataCopy = JSON . parse ( JSON . stringify ( metadata ) ) ; metadataCopy . DEFAULT_FEE_RECIPIENT = newDefault ; fs . writeFileSync ( metadataPath , JSON . stringify ( metadataCopy ) ) ; } return newDefault ; }