From bb992f8a49b42a1cd50d5bcc868f6d69fcd1a7ae Mon Sep 17 00:00:00 2001 From: fragosti Date: Tue, 14 Aug 2018 19:16:36 -0700 Subject: Update parsing code --- packages/connect/src/http_client.ts | 22 ++++++++++++---------- packages/connect/src/types.ts | 4 ++++ .../src/utils/relayer_response_json_parsers.ts | 22 ++++++++++++---------- packages/connect/src/utils/type_converters.ts | 17 +++++++++++++++-- 4 files changed, 43 insertions(+), 22 deletions(-) (limited to 'packages/connect') diff --git a/packages/connect/src/http_client.ts b/packages/connect/src/http_client.ts index 20a2f9cc0..10c956ff8 100644 --- a/packages/connect/src/http_client.ts +++ b/packages/connect/src/http_client.ts @@ -8,8 +8,8 @@ import * as queryString from 'query-string'; import { schemas as clientSchemas } from './schemas/schemas'; import { APIOrder, - AssetPairsItem, AssetPairsRequestOpts, + AssetPairsResponse, Client, HttpRequestOptions, HttpRequestType, @@ -18,6 +18,7 @@ import { OrderConfigRequest, OrderConfigResponse, OrdersRequestOpts, + OrdersResponse, PagedRequestOpts, PaginatedCollection, } from './types'; @@ -61,9 +62,9 @@ export class HttpClient implements Client { * @param requestOpts Options specifying assetData information to retrieve and page information, defaults to { page: 1, perPage: 100 } * @return The resulting AssetPairsItems that match the request */ - public async getAssetPairsAsync(requestOpts?: AssetPairsRequestOpts & PagedRequestOpts): Promise> { + public async getAssetPairsAsync(requestOpts?: AssetPairsRequestOpts & PagedRequestOpts): Promise { if (!_.isUndefined(requestOpts)) { - assert.doesConformToSchema('requestOpts', requestOpts, clientSchemas.AssetPairsRequestOptsSchema); + assert.doesConformToSchema('requestOpts', requestOpts, clientSchemas.assetPairsRequestOptsSchema); assert.doesConformToSchema('requestOpts', requestOpts, clientSchemas.pagedRequestOptsSchema); } const httpRequestOpts = { @@ -78,7 +79,7 @@ export class HttpClient implements Client { * @param requestOpts Options specifying orders to retrieve and page information, defaults to { page: 1, perPage: 100 } * @return The resulting SignedOrders that match the request */ - public async getOrdersAsync(requestOpts?: OrdersRequestOpts & PagedRequestOpts): Promise> { + public async getOrdersAsync(requestOpts?: OrdersRequestOpts & PagedRequestOpts): Promise { if (!_.isUndefined(requestOpts)) { assert.doesConformToSchema('requestOpts', requestOpts, clientSchemas.ordersRequestOptsSchema); assert.doesConformToSchema('requestOpts', requestOpts, clientSchemas.pagedRequestOptsSchema); @@ -98,7 +99,7 @@ export class HttpClient implements Client { public async getOrderAsync(orderHash: string): Promise { assert.doesConformToSchema('orderHash', orderHash, schemas.orderHashSchema); const responseJson = await this._requestAsync(`/order/${orderHash}`, HttpRequestType.Get); - const order = relayerResponseJsonParsers.parseOrderJson(responseJson); + const order = relayerResponseJsonParsers.parseAPIOrderJson(responseJson); return order; } /** @@ -128,18 +129,19 @@ export class HttpClient implements Client { * @return The resulting OrderConfigResponse that matches the request */ public async getOrderConfigAsync(request: OrderConfigRequest): Promise { - assert.doesConformToSchema('request', request, clientSchemas.feesRequestSchema); + assert.doesConformToSchema('request', request, clientSchemas.orderConfigRequestSchema); const httpRequestOpts = { payload: request, }; - const responseJson = await this._requestAsync('/fees', HttpRequestType.Post, httpRequestOpts); + const responseJson = await this._requestAsync('/order_config', HttpRequestType.Post, httpRequestOpts); const fees = relayerResponseJsonParsers.parseOrderConfigResponseJson(responseJson); return fees; } - + /** + * Retrieve the list of fee recipient addresses used by + */ public async getFeeRecipientsAsync(): Promise> { - // TODO - return; + return this._requestAsync('/fee_recipients', HttpRequestType.Get); } /** diff --git a/packages/connect/src/types.ts b/packages/connect/src/types.ts index dab01c935..8c2eaff7d 100644 --- a/packages/connect/src/types.ts +++ b/packages/connect/src/types.ts @@ -71,6 +71,8 @@ export enum WebsocketClientEventType { ConnectFailed = 'connectFailed', } +export type OrdersResponse = PaginatedCollection; + export interface APIOrder { order: SignedOrder; metaData: object; @@ -81,6 +83,8 @@ export interface AssetPairsRequestOpts { assetDataB?: string; } +export type AssetPairsResponse = PaginatedCollection; + export interface AssetPairsItem { assetDataA: Asset; assetDataB: Asset; diff --git a/packages/connect/src/utils/relayer_response_json_parsers.ts b/packages/connect/src/utils/relayer_response_json_parsers.ts index 6463aefb0..9a17b23d3 100644 --- a/packages/connect/src/utils/relayer_response_json_parsers.ts +++ b/packages/connect/src/utils/relayer_response_json_parsers.ts @@ -1,14 +1,16 @@ import { assert } from '@0xproject/assert'; import { schemas } from '@0xproject/json-schemas'; -import { SignedOrder } from '@0xproject/types'; -import { AssetPairsItem, OrderbookResponse, OrderConfigResponse } from '../types'; +import { APIOrder, AssetPairsItem, AssetPairsResponse, OrderbookResponse, OrderConfigResponse, OrdersResponse } from '../types'; import { typeConverters } from './type_converters'; export const relayerResponseJsonParsers = { - parseAssetDataPairsJson(json: any): AssetPairsItem[] { + parseAssetDataPairsJson(json: any): AssetPairsResponse { assert.doesConformToSchema('assetDataPairs', json, schemas.relayerApiAssetDataPairsResponseSchema); + return { ...json, records: relayerResponseJsonParsers.parseAssetDataPairsJson(json.records) }; + }, + parseAssetPairsItemJson(json: any): AssetPairsItem[] { return json.map((assetDataPair: any) => { return typeConverters.convertStringsFieldsToBigNumbers(assetDataPair, [ 'assetDataA.minAmount', @@ -18,20 +20,20 @@ export const relayerResponseJsonParsers = { ]); }); }, - parseOrdersJson(json: any): SignedOrder[] { - assert.doesConformToSchema('orders', json, schemas.signedOrdersSchema); - return json.map((order: object) => typeConverters.convertOrderStringFieldsToBigNumber(order)); + parseOrdersJson(json: any): OrdersResponse { + assert.doesConformToSchema('relayerApiOrdersResponse', json, schemas.relayerApiOrdersResponseSchema); + return { ...json, records: json.records.map(relayerResponseJsonParsers.parseAPIOrderJson.bind(relayerResponseJsonParsers)) }; }, - parseOrderJson(json: any): SignedOrder { - assert.doesConformToSchema('order', json, schemas.signedOrderSchema); + parseAPIOrderJson(json: any): APIOrder { + assert.doesConformToSchema('relayerApiOrder', json, schemas.relayerApiOrderSchema); return typeConverters.convertOrderStringFieldsToBigNumber(json); }, parseOrderbookResponseJson(json: any): OrderbookResponse { - assert.doesConformToSchema('orderBook', json, schemas.relayerApiOrdersResponseSchema); + assert.doesConformToSchema('orderBook', json, schemas.relayerApiOrderbookResponseSchema); return typeConverters.convertOrderbookStringFieldsToBigNumber(json); }, parseOrderConfigResponseJson(json: any): OrderConfigResponse { - assert.doesConformToSchema('fees', json, schemas.relayerApiOrderConfigResponseSchema); + assert.doesConformToSchema('orderConfig', json, schemas.relayerApiOrderConfigResponseSchema); return typeConverters.convertStringsFieldsToBigNumbers(json, ['makerFee', 'takerFee']); }, }; diff --git a/packages/connect/src/utils/type_converters.ts b/packages/connect/src/utils/type_converters.ts index 513f9b39f..c28cba0e1 100644 --- a/packages/connect/src/utils/type_converters.ts +++ b/packages/connect/src/utils/type_converters.ts @@ -1,15 +1,28 @@ import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; +import { APIOrder } from '../types'; + export const typeConverters = { convertOrderbookStringFieldsToBigNumber(orderbook: any): any { const bids = _.get(orderbook, 'bids', []); const asks = _.get(orderbook, 'asks', []); + const convertedBids = { + ...bids, + records: bids.records.map((order: any) => typeConverters.convertAPIOrderStringFieldsToBigNumber(order)), + }; + const convertedAsks = { + ...asks, + records: asks.records.map((order: any) => typeConverters.convertAPIOrderStringFieldsToBigNumber(order)), + }; return { - bids: bids.map((order: any) => typeConverters.convertOrderStringFieldsToBigNumber(order)), - asks: asks.map((order: any) => typeConverters.convertOrderStringFieldsToBigNumber(order)), + bids: convertedBids, + asks: convertedAsks, }; }, + convertAPIOrderStringFieldsToBigNumber(apiOrder: any): APIOrder { + return { ...apiOrder, order: typeConverters.convertOrderStringFieldsToBigNumber(apiOrder.order) }; + }, convertOrderStringFieldsToBigNumber(order: any): any { return typeConverters.convertStringsFieldsToBigNumbers(order, [ 'makerAssetAmount', -- cgit v1.2.3