Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
make MESH_HTTP_URI optional; fallback to websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
xianny committed Feb 27, 2020
1 parent 4b852e7 commit d561929
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 43 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
4 changes: 2 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down Expand Up @@ -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<Express.Application> {
const app = express();
await runHttpServiceAsync(dependencies, config, app);
Expand Down
4 changes: 1 addition & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 29 additions & 25 deletions src/utils/mesh_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,40 @@ import { utils } from './utils';

export class MeshClient extends WSClient {
public async addOrdersViaHttpAsync(orders: SignedOrder[], pinned: boolean = false): Promise<ValidationResults> {
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)) {
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);
}
}

0 comments on commit d561929

Please sign in to comment.