Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(order): Extend query parameters to list orders #432

Merged
merged 5 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions src/order/OrderAPI.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ describe('OrderAPI', () => {
nock(global.REST_URL)
.get(OrderAPI.URL.ORDERS)
.query(true)
.reply(
200,
JSON.stringify([
.reply(200, (uri: string) => {
expect(uri).toBe('/orders?status=open,pending,active');

return JSON.stringify([
{
created_at: '2019-04-22T20:21:20.897409Z',
executed_value: '0.0000000000000000',
Expand All @@ -70,14 +71,48 @@ describe('OrderAPI', () => {
stp: SelfTradePrevention.DECREMENT_AND_CANCEL,
type: OrderType.MARKET,
},
])
);
]);
});

const openOrders = await global.client.rest.order.getOpenOrders();

expect(openOrders.data.length).toBe(1);
expect(openOrders.data[0].status).toBe(OrderStatus.OPEN);
});

it('accepts a list of different order statuses', async () => {
nock(global.REST_URL)
.get(OrderAPI.URL.ORDERS)
.query(true)
.reply(200, (uri: string) => {
expect(uri).toBe('/orders?status=open,pending');

return JSON.stringify([
{
created_at: '2019-04-22T20:21:20.897409Z',
executed_value: '0.0000000000000000',
fill_fees: '0.0000000000000000',
filled_size: '0.00000000',
funds: '207850.8486540300000000',
id: '8eba9e7b-08d6-4667-90ca-6db445d743c0',
post_only: false,
product_id: 'BTC-EUR',
settled: false,
side: OrderSide.BUY,
size: '0.10000000',
status: OrderStatus.OPEN,
stp: SelfTradePrevention.DECREMENT_AND_CANCEL,
type: OrderType.MARKET,
},
]);
});

const openOrders = await global.client.rest.order.getOpenOrders({
status: [OrderStatus.OPEN, OrderStatus.PENDING],
});

expect(openOrders.data.length).toBe(1);
});
});

describe('getOrder', () => {
Expand Down
20 changes: 17 additions & 3 deletions src/order/OrderAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ export interface FilledOrder extends BasePlacedOrder {
status: OrderStatus.DONE;
}

/** @see https://docs.pro.coinbase.com/#list-orders */
export interface OrderListQueryParam extends Pagination {
/** Only list orders for a specific product. */
product_id?: string;
/** Limit list of orders to these statuses. Passing "all" returns orders of all statuses. Default: [open, pending, active] */
status?: (OrderStatus.OPEN | OrderStatus.PENDING | OrderStatus.ACTIVE | 'all')[];
}

export type Order = PendingOrder | FilledOrder;

export class OrderAPI {
Expand Down Expand Up @@ -139,12 +147,18 @@ export class OrderAPI {
* orders are returned. As soon as an order is no longer open and settled, it will no longer appear
* in the default request.
*
* @param pagination - Pagination field
* @param query - Available query parameters (Pagination, Product ID and/or Order Status)
* @see https://docs.pro.coinbase.com/#list-orders
*/
async getOpenOrders(pagination?: Pagination): Promise<PaginatedData<Order>> {
async getOpenOrders(query?: OrderListQueryParam): Promise<PaginatedData<Order>> {
const resource = OrderAPI.URL.ORDERS;
const response = await this.apiClient.get<Order[]>(resource, {params: pagination});
const status = query?.status || [OrderStatus.OPEN, OrderStatus.PENDING, OrderStatus.ACTIVE];
const response = await this.apiClient.get<Order[]>(resource, {
params: {
...query,
status: status.join(','),
},
});
return {
data: response.data,
pagination: {
Expand Down