diff --git a/README.md b/README.md index 7a545041d..9986810a1 100644 --- a/README.md +++ b/README.md @@ -59,19 +59,19 @@ To get a local development version of `0x-api` running: 2. Create an `.env` file and copy the content from the `.env_example` file. Defaults are defined in `config.ts`/`config.js`. The bash environment takes precedence over the `.env` file. If you run `source .env`, changes to the `.env` file will have no effect until you unset the colliding variables. -| Environment Variable | Default | Description | -| ----------------------- | --------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `CHAIN_ID` | Required. No default. | The chain id you'd like your API to run on (e.g: `1` -> mainnet, `42` -> Kovan, `3` -> Ropsten, `1337` -> Ganache). Defaults to `42` in the API, but required for `docker-compose up`. | -| `ETHEREUM_RPC_URL` | Required. No default. | The URL used to issue JSON RPC requests. Use `http://ganache:8545` to use the local ganache instance. | -| `MESH_WEBSOCKET_URI` | Required. Default for dev: `ws://localhost:60557` | The URL pointing to the 0x Mesh node. A default node is spun up in `docker-compose up` | -| `MESH_HTTP_URI` | Required. Default for dev: `http://localhost:60556` | The URL pointing to the Mesh node's HTTP JSON-RPC endpoint. A default node is spun up in `docker-compose up` | -| `POSTGRES_URI` | Required. Default for dev: `postgresql://api:api@localhost/api` | A URI of a running postgres instance. By default, the API will create all necessary tables. A default instance is spun up in `docker-compose up` | -| `FEE_RECIPIENT_ADDRESS` | `0x0000000000000000000000000000000000000000` | The Ethereum address which should be specified as the fee recipient in orders your API accepts. | -| `MAKER_FEE_ASSET_DATA` | `0x` | The maker fee token asset data for created 0x orders. | -| `TAKER_FEE_ASSET_DATA` | `0x` | The taker fee token asset data for created 0x orders. | -| `MAKER_FEE_UNIT_AMOUNT` | `0` | The flat maker fee amount you'd like to receive for filled orders hosted by you. | -| `TAKER_FEE_UNIT_AMOUNT` | `0` | The flat taker fee amount you'd like to receive for filled orders hosted by you. | -| `WHITELIST_ALL_TOKENS` | `false` | A boolean determining whether all tokens should be allowed to be posted. | +| Environment Variable | Default | Description | +| ----------------------- | -------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `CHAIN_ID` | Required. No default. | The chain id you'd like your API to run on (e.g: `1` -> mainnet, `42` -> Kovan, `3` -> Ropsten, `1337` -> Ganache). Defaults to `42` in the API, but required for `docker-compose up`. | +| `ETHEREUM_RPC_URL` | Required. No default. | The URL used to issue JSON RPC requests. Use `http://ganache:8545` to use the local ganache instance. | +| `MESH_WEBSOCKET_URI` | Required. Default for dev: `ws://localhost:60557` | The URL pointing to the 0x Mesh node. A default node is spun up in `docker-compose up` | +| `MESH_HTTP_URI` | Optional. Used when syncing the orderbook; defaults to websocket connection if not provided. | The URL pointing to the Mesh node's HTTP JSON-RPC endpoint. A default node is spun up in `docker-compose up` | +| `POSTGRES_URI` | Required. Default for dev: `postgresql://api:api@localhost/api` | A URI of a running postgres instance. By default, the API will create all necessary tables. A default instance is spun up in `docker-compose up` | +| `FEE_RECIPIENT_ADDRESS` | `0x0000000000000000000000000000000000000000` | The Ethereum address which should be specified as the fee recipient in orders your API accepts. | +| `MAKER_FEE_ASSET_DATA` | `0x` | The maker fee token asset data for created 0x orders. | +| `TAKER_FEE_ASSET_DATA` | `0x` | The taker fee token asset data for created 0x orders. | +| `MAKER_FEE_UNIT_AMOUNT` | `0` | The flat maker fee amount you'd like to receive for filled orders hosted by you. | +| `TAKER_FEE_UNIT_AMOUNT` | `0` | The flat taker fee amount you'd like to receive for filled orders hosted by you. | +| `WHITELIST_ALL_TOKENS` | `false` | A boolean determining whether all tokens should be allowed to be posted. | 3. Install the dependencies: diff --git a/src/app.ts b/src/app.ts index 0c3e32cc3..60b687f71 100644 --- a/src/app.ts +++ b/src/app.ts @@ -43,7 +43,7 @@ export async function getDefaultAppDependenciesAsync( const stakingDataService = new StakingDataService(connection); let meshClient: MeshClient | undefined; - if (config.MESH_WEBSOCKET_URI !== undefined && config.MESH_HTTP_URI !== undefined) { + if (config.MESH_WEBSOCKET_URI !== undefined) { meshClient = new MeshClient(config.MESH_WEBSOCKET_URI, config.MESH_HTTP_URI); } else { logger.warn(`Skipping Mesh client creation because no URI provided`); @@ -79,7 +79,7 @@ export async function getDefaultAppDependenciesAsync( */ export async function getAppAsync( dependencies: AppDependencies, - config: { HTTP_PORT: string; ETHEREUM_RPC_URL: string; MESH_HTTP_URI: string }, + config: { HTTP_PORT: string; ETHEREUM_RPC_URL: string }, ): Promise { const app = express(); await runHttpServiceAsync(dependencies, config, app); diff --git a/src/config.ts b/src/config.ts index 20de7a571..04953a7b7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -40,9 +40,7 @@ export const ETHEREUM_RPC_URL = assertEnvVarType('ETHEREUM_RPC_URL', process.env export const MESH_WEBSOCKET_URI = _.isEmpty(process.env.MESH_WEBSOCKET_URI) ? 'ws://localhost:60557' : assertEnvVarType('MESH_WEBSOCKET_URI', process.env.MESH_WEBSOCKET_URI, EnvVarType.Url); -export const MESH_HTTP_URI = _.isEmpty(process.env.MESH_HTTP_URI) - ? 'http://localhost:60556' - : assertEnvVarType('MESH_HTTP_URI', process.env.MESH_HTTP_URI, EnvVarType.Url); +export const MESH_HTTP_URI = process.env.MESH_HTTP_URI; // The fee recipient for orders export const FEE_RECIPIENT_ADDRESS = _.isEmpty(process.env.FEE_RECIPIENT_ADDRESS) ? NULL_ADDRESS diff --git a/src/utils/mesh_client.ts b/src/utils/mesh_client.ts index 8b3ef82b2..651d3aa1c 100644 --- a/src/utils/mesh_client.ts +++ b/src/utils/mesh_client.ts @@ -8,36 +8,40 @@ import { utils } from './utils'; export class MeshClient extends WSClient { public async addOrdersViaHttpAsync(orders: SignedOrder[], pinned: boolean = false): Promise { - const validationResults: ValidationResults = { accepted: [], rejected: [] }; - const chunks = _.chunk(orders, MESH_ORDERS_BATCH_SIZE); - chunks.forEach(async chunk => { - // format request payload - const data = { - jsonrpc: '2.0', - id: +new Date(), - method: 'mesh_addOrders', - params: [chunk, { pinned }], - }; + if (_.isEmpty(this.httpURI)) { + return super.addOrdersAsync(orders, pinned); + } else { + const validationResults: ValidationResults = { accepted: [], rejected: [] }; + const chunks = _.chunk(orders, MESH_ORDERS_BATCH_SIZE); + chunks.forEach(async chunk => { + // format request payload + const data = { + jsonrpc: '2.0', + id: +new Date(), + method: 'mesh_addOrders', + params: [chunk, { pinned }], + }; - // send the request - const response = await Axios({ - method: 'post', - url: this.httpURI, - data, - }); + // send the request + const response = await Axios({ + method: 'post', + url: this.httpURI, + data, + }); - // validate the response - utils.isValidJsonRpcResponseOrThrow(response.data, data); - const results = response.data.results; + // validate the response + utils.isValidJsonRpcResponseOrThrow(response.data, data); + const results = response.data.results; - // concatenate results - validationResults.accepted = [...validationResults.accepted, ...results.accepted]; - validationResults.rejected = [...validationResults.rejected, ...results.rejected]; - }); - return validationResults; + // concatenate results + validationResults.accepted = [...validationResults.accepted, ...results.accepted]; + validationResults.rejected = [...validationResults.rejected, ...results.rejected]; + }); + return validationResults; + } } - constructor(public readonly websocketURI: string, public readonly httpURI: string, websocketOpts?: WSOpts) { + constructor(public readonly websocketURI: string, public readonly httpURI?: string, websocketOpts?: WSOpts) { super(websocketURI, websocketOpts); } }