Skip to content

Commit

Permalink
Merge pull request #18 from tiagosiebler/orderchecks
Browse files Browse the repository at this point in the history
chore(): extract order types
  • Loading branch information
tiagosiebler authored Sep 13, 2024
2 parents 9252d34 + d7cf0b6 commit 29717e3
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 65 deletions.
23 changes: 5 additions & 18 deletions src/CBAdvancedTradeClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AxiosRequestConfig } from 'axios';
import { nanoid } from 'nanoid';

import { BaseRestClient } from './lib/BaseRestClient.js';
import {
Expand All @@ -9,6 +8,7 @@ import {
} from './lib/requestUtils.js';
import {
AllocateAdvTradePortfolioRequest,
CloseAdvTradePositionRequest,
GetAdvTradeFillsRequest,
GetAdvTradeMarketTradesRequest,
GetAdvTradeOrdersRequest,
Expand Down Expand Up @@ -71,16 +71,6 @@ export class CBAdvancedTradeClient extends BaseRestClient {
return REST_CLIENT_TYPE_ENUM.advancedTrade;
}

/**
*
* Misc Utility Methods
*
*/

generateNewOrderID(): string {
return nanoid(32);
}

/**
*
* Account Endpoints
Expand Down Expand Up @@ -342,13 +332,10 @@ export class CBAdvancedTradeClient extends BaseRestClient {
* Places an order to close any open positions for a specified product_id.
*
*/
closePosition(params: {
// TODO: extract type
client_order_id: string;
product_id: string;
size?: string;
}): Promise<AdvTradeClosePositionResponse> {
this.validateOrderId(params as any, 'client_order_id');
closePosition(
params: CloseAdvTradePositionRequest,
): Promise<AdvTradeClosePositionResponse> {
this.validateOrderId(params, 'client_order_id');
return this.postPrivate(`/api/v3/brokerage/orders/close_position`, {
body: params,
});
Expand Down
11 changes: 0 additions & 11 deletions src/CBAppClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AxiosRequestConfig } from 'axios';
import { nanoid } from 'nanoid';

import { BaseRestClient } from './lib/BaseRestClient.js';
import {
Expand Down Expand Up @@ -38,16 +37,6 @@ export class CBAppClient extends BaseRestClient {
return REST_CLIENT_TYPE_ENUM.coinbaseApp;
}

/**
*
* Misc Utility Methods
*
*/

generateNewOrderID(): string {
return nanoid(32);
}

/**
*
* Account Endpoints
Expand Down
11 changes: 0 additions & 11 deletions src/CBExchangeClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AxiosRequestConfig } from 'axios';
import { nanoid } from 'nanoid';

import { BaseRestClient } from './lib/BaseRestClient.js';
import {
Expand Down Expand Up @@ -56,16 +55,6 @@ export class CBExchangeClient extends BaseRestClient {
return REST_CLIENT_TYPE_ENUM.exchange;
}

/**
*
* Misc Utility Methods
*
*/

generateNewOrderID(): string {
return nanoid(32);
}

/**
*
* Accounts Endpoints
Expand Down
11 changes: 0 additions & 11 deletions src/CBInternationalClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AxiosRequestConfig } from 'axios';
import { nanoid } from 'nanoid';

import { BaseRestClient } from './lib/BaseRestClient.js';
import {
Expand Down Expand Up @@ -41,16 +40,6 @@ export class CBInternationalClient extends BaseRestClient {
return REST_CLIENT_TYPE_ENUM.international;
}

/**
*
* Misc Utility Methods
*
*/

generateNewOrderID(): string {
return nanoid(32);
}

/**
*
* Assets Endpoints
Expand Down
11 changes: 0 additions & 11 deletions src/CBPrimeClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AxiosRequestConfig } from 'axios';
import { nanoid } from 'nanoid';

import { BaseRestClient } from './lib/BaseRestClient.js';
import {
Expand Down Expand Up @@ -51,16 +50,6 @@ export class CBPrimeClient extends BaseRestClient {
return REST_CLIENT_TYPE_ENUM.prime;
}

/**
*
* Misc Utility Methods
*
*/

generateNewOrderID(): string {
return nanoid(32);
}

/**
*
* Allocation Endpoints
Expand Down
7 changes: 5 additions & 2 deletions src/lib/BaseRestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import axios, { AxiosRequestConfig, AxiosResponse, Method } from 'axios';
import https from 'https';
import { nanoid } from 'nanoid';

import { SubmitAdvTradeOrderRequest } from '../types/request/advanced-trade-client.js';
import {
CloseAdvTradePositionRequest,
SubmitAdvTradeOrderRequest,
} from '../types/request/advanced-trade-client.js';
import { CustomOrderIdProperty } from '../types/shared.types.js';
import { signJWT } from './jwtNode.js';
import { neverGuard } from './misc-util.js';
Expand Down Expand Up @@ -268,7 +271,7 @@ export abstract class BaseRestClient {
* Validate syntax meets requirements set by binance. Log warning if not.
*/
protected validateOrderId(
params: SubmitAdvTradeOrderRequest,
params: SubmitAdvTradeOrderRequest | CloseAdvTradePositionRequest,
orderIdProperty: CustomOrderIdProperty,
): void {
if (!params[orderIdProperty]) {
Expand Down
15 changes: 14 additions & 1 deletion src/types/request/advanced-trade-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export interface GetAdvTradeMarketTradesRequest {
/**
* Note: client_order_id is optional, because the SDK will automatically generate a value for you during the request.
*
* If you are generating your own, make sure you either use the client.generateNewOrderId() method or make sure to prefix your value with "cbnode". The SDK will automatically do this, if the prefix is missing.
* If you are generating your own, make sure you either use the client.generateNewOrderId() method or make sure to prefix your value with "cbnode".
* The SDK will automatically add the prefix if the prefix is missing.
*/
export interface SubmitAdvTradeOrderRequest {
client_order_id?: string;
Expand Down Expand Up @@ -123,6 +124,18 @@ export interface PreviewAdvTradeOrderRequest {
retail_portfolio_id?: string;
}

/**
* Note: client_order_id is optional, because the SDK will automatically generate a value for you during the request.
*
* If you are generating your own, make sure you either use the client.generateNewOrderId() method or make sure to prefix your value with "cbnode".
* The SDK will automatically add the prefix if the prefix is missing.
*/
export interface CloseAdvTradePositionRequest {
client_order_id?: string;
product_id: string;
size?: string;
}

/**
*
* Portfolios Endpoints
Expand Down

0 comments on commit 29717e3

Please sign in to comment.