diff --git a/src/client.ts b/src/client.ts index 803f43b..27b943b 100644 --- a/src/client.ts +++ b/src/client.ts @@ -14,6 +14,7 @@ import { FilterParams, TradeHistory, OrderHistory, + OptionalParams, } from "./types"; import { createL1Headers, createL2Headers } from "./headers"; import { del, DELETE, GET, get, POST, post } from "./http_helpers"; @@ -100,28 +101,32 @@ export class ClobClient { /** * Creates a new API key for a user * @param nonce + * @param optionalParams - query parameters * @returns ApiKeyCreds */ - public async createApiKey(nonce?: number): Promise { + public async createApiKey(nonce?: number, optionalParams?: OptionalParams): Promise { this.canL1Auth(); const endpoint = `${this.host}${CREATE_API_KEY}`; const headers = await createL1Headers(this.signer as Wallet | JsonRpcSigner, nonce); - const resp = await post(endpoint, headers); + + const resp = await post(endpoint, headers, undefined, optionalParams); return resp; } /** * Derives an existing API key for a user * @param nonce + * @param optionalParams - query parameters * @returns ApiKeyCreds */ - public async deriveApiKey(nonce?: number): Promise { + public async deriveApiKey(nonce?: number, optionalParams?: OptionalParams): Promise { this.canL1Auth(); const endpoint = `${this.host}${DERIVE_API_KEY}`; const headers = await createL1Headers(this.signer as Wallet | JsonRpcSigner, nonce); - const resp = await get(endpoint, headers); + + const resp = await get(endpoint, headers, undefined, optionalParams); return resp; } @@ -248,13 +253,17 @@ export class ClobClient { return get(url, headers); } - public async postOrder(order: LimitOrderAndSignature | MarketOrderAndSignature): Promise { + public async postOrder( + order: LimitOrderAndSignature | MarketOrderAndSignature, + optionalParams?: OptionalParams, + ): Promise { this.canL2Auth(); const endpoint = POST_ORDER; const orderPayload = order.orderType === "limit" ? limitOrderToJson(order as LimitOrderAndSignature) : marketOrderToJson(order as MarketOrderAndSignature); + const l2HeaderArgs = { method: POST, requestPath: endpoint, @@ -266,7 +275,8 @@ export class ClobClient { this.creds as ApiKeyCreds, l2HeaderArgs, ); - return post(`${this.host}${endpoint}`, headers, orderPayload); + + return post(`${this.host}${endpoint}`, headers, orderPayload, optionalParams); } public async cancelOrder(payload: OrderPayload): Promise { diff --git a/src/http_helpers/index.ts b/src/http_helpers/index.ts index ff943d4..af060e3 100644 --- a/src/http_helpers/index.ts +++ b/src/http_helpers/index.ts @@ -5,9 +5,15 @@ export const POST = "POST"; export const DELETE = "DELETE"; export const PUT = "PUT"; -export const request = async (endpoint: string, method: Method, headers?: any, data?: any): Promise => { +export const request = async ( + endpoint: string, + method: Method, + headers?: any, + data?: any, + params?: any, +): Promise => { try { - const response = await axios({ method, url: endpoint, headers, data }); + const response = await axios({ method, url: endpoint, headers, data, params }); return response; } catch (err) { if (axios.isAxiosError(err)) { @@ -22,17 +28,17 @@ export const request = async (endpoint: string, method: Method, headers?: any, d } }; -export const post = async (endpoint: string, headers: any, data?: any): Promise => { - const resp = await request(endpoint, POST, headers, data); +export const post = async (endpoint: string, headers: any, data?: any, params?: any): Promise => { + const resp = await request(endpoint, POST, headers, data, params); return "error" in resp ? resp : resp.data; }; -export const get = async (endpoint: string, headers?: any, data?: any): Promise => { - const resp = await request(endpoint, GET, headers, data); +export const get = async (endpoint: string, headers?: any, data?: any, params?: any): Promise => { + const resp = await request(endpoint, GET, headers, data, params); return "error" in resp ? resp : resp.data; }; -export const del = async (endpoint: string, headers?: any, data?: any): Promise => { - const resp = await request(endpoint, DELETE, headers, data); +export const del = async (endpoint: string, headers?: any, data?: any, params?: any): Promise => { + const resp = await request(endpoint, DELETE, headers, data, params); return "error" in resp ? resp : resp.data; }; diff --git a/src/types.ts b/src/types.ts index 8c3430b..ab4a5ba 100644 --- a/src/types.ts +++ b/src/types.ts @@ -145,3 +145,5 @@ export interface TradeHistory { export interface OrderHistory { history: Order[]; } + +export type OptionalParams = { [query: string]: string };