Skip to content

Commit

Permalink
Merge pull request #19 from tiagosiebler/typechecks
Browse files Browse the repository at this point in the history
feat(): extend order ID checks
  • Loading branch information
tiagosiebler authored Sep 13, 2024
2 parents 29717e3 + 4576acc commit 104978c
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/CBExchangeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ export class CBExchangeClient extends BaseRestClient {
* Once an order is placed, your account funds will be put on hold for the duration of the order.
*/
submitOrder(params: SubmitCBExchOrderRequest): Promise<any> {
this.validateOrderId(params, 'client_oid');
return this.postPrivate('/orders', { body: params });
}

Expand Down
1 change: 1 addition & 0 deletions src/CBInternationalClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export class CBInternationalClient extends BaseRestClient {
* Creates a new order.
*/
submitOrder(params: SubmitINTXOrderRequest): Promise<any> {
this.validateOrderId(params, 'client_order_id');
return this.postPrivate('/api/v1/orders', { body: params });
}

Expand Down
1 change: 1 addition & 0 deletions src/CBPrimeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ export class CBPrimeClient extends BaseRestClient {
* Create an order for a given portfolio.
*/
submitOrder(params: SubmitPrimeOrderRequest): Promise<any> {
this.validateOrderId(params, 'client_order_id');
const { portfolio_id, ...body } = params;
return this.postPrivate(`/v1/portfolios/${portfolio_id}/order`, {
body: body,
Expand Down
29 changes: 20 additions & 9 deletions src/lib/BaseRestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
CloseAdvTradePositionRequest,
SubmitAdvTradeOrderRequest,
} from '../types/request/advanced-trade-client.js';
import { SubmitCBExchOrderRequest } from '../types/request/coinbase-exchange.js';
import { SubmitINTXOrderRequest } from '../types/request/coinbase-international.js';
import { SubmitPrimeOrderRequest } from '../types/request/coinbase-prime.js';
import { CustomOrderIdProperty } from '../types/shared.types.js';
import { signJWT } from './jwtNode.js';
import { neverGuard } from './misc-util.js';
Expand Down Expand Up @@ -264,31 +267,39 @@ export abstract class BaseRestClient {
}

public generateNewOrderId(): string {
return APIIDPrefix + nanoid(30);
return APIIDPrefix + nanoid(14);
}

/**
* Validate syntax meets requirements set by binance. Log warning if not.
* Validate syntax meets requirements set by coinbase. Log warning if not.
*/
protected validateOrderId(
params: SubmitAdvTradeOrderRequest | CloseAdvTradePositionRequest,
params:
| SubmitAdvTradeOrderRequest
| CloseAdvTradePositionRequest
| SubmitCBExchOrderRequest
| SubmitINTXOrderRequest
| SubmitPrimeOrderRequest,
orderIdProperty: CustomOrderIdProperty,
): void {
if (!params[orderIdProperty]) {
params[orderIdProperty] = this.generateNewOrderId();
// Not the cleanest but strict checks aren't quite necessary here either
const requestParams = params as any;

if (!requestParams[orderIdProperty]) {
requestParams[orderIdProperty] = this.generateNewOrderId();
return;
}

if (!params[orderIdProperty].startsWith(APIIDPrefix)) {
if (!requestParams[orderIdProperty].startsWith(APIIDPrefix)) {
logInvalidOrderId(orderIdProperty, APIIDPrefix, params);

const previousValue = params[orderIdProperty];
const newValue = APIIDPrefix + params[orderIdProperty];
const previousValue = requestParams[orderIdProperty];
const newValue = APIIDPrefix + requestParams[orderIdProperty];
console.warn(
`WARNING: "${orderIdProperty}" was automatically prefixed. Changed from "${previousValue}" to "${newValue}". To avoid this, apply the prefix before submitting an order or use the client.generateNewOrderId() utility method.`,
);

params[orderIdProperty] = newValue;
requestParams[orderIdProperty] = newValue;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/request/coinbase-international.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface GetINTXAggregatedCandlesData {
*/

export interface SubmitINTXOrderRequest {
client_order_id: string;
client_order_id?: string;
side: string;
size: string;
tif: string;
Expand Down
2 changes: 1 addition & 1 deletion src/types/request/coinbase-prime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export interface SubmitPrimeOrderRequest {
portfolio_id: string;
product_id: string;
side: 'BUY' | 'SELL';
client_order_id: string;
client_order_id?: string;
type?: 'MARKET' | 'LIMIT' | 'TWAP' | 'VWAP' | 'STOP_LIMIT';
base_quantity?: string;
quote_value?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/types/shared.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ export interface OrderConfiguration {
trigger_bracket_gtd?: TriggerBracketGTD;
}

export type CustomOrderIdProperty = 'client_order_id';
export type CustomOrderIdProperty = 'client_order_id' | 'client_oid';

0 comments on commit 104978c

Please sign in to comment.