diff --git a/src/CBAppClient.ts b/src/CBAppClient.ts index 0cde0cb..f6fe4c4 100644 --- a/src/CBAppClient.ts +++ b/src/CBAppClient.ts @@ -31,7 +31,13 @@ export class CBAppClient extends BaseRestClient { restClientOptions: RestClientOptions = {}, requestOptions: AxiosRequestConfig = {}, ) { - super(restClientOptions, requestOptions); + super(restClientOptions, { + ...requestOptions, + headers: { + 'CB-VERSION': '2024-09-13', + ...requestOptions.headers, + }, + }); return this; } diff --git a/src/lib/BaseRestClient.ts b/src/lib/BaseRestClient.ts index 336b624..11faebe 100644 --- a/src/lib/BaseRestClient.ts +++ b/src/lib/BaseRestClient.ts @@ -150,6 +150,7 @@ export abstract class BaseRestClient { /** inject custom rquest options based on axios specs - see axios docs for more guidance on AxiosRequestConfig: https://github.com/axios/axios#request-config */ ...networkOptions, headers: { + ...networkOptions.headers, 'Content-Type': 'application/json', locale: 'en-US', 'User-Agent': USER_AGENT, @@ -175,11 +176,7 @@ export abstract class BaseRestClient { }); } - this.baseUrl = getRestBaseUrl( - false, - restClientOptions, - this.getClientType(), - ); + this.baseUrl = getRestBaseUrl(restClientOptions, this.getClientType()); this.apiKey = this.options.apiKey; this.apiSecret = this.options.apiSecret; diff --git a/src/lib/requestUtils.ts b/src/lib/requestUtils.ts index bdbecfa..a9a3965 100644 --- a/src/lib/requestUtils.ts +++ b/src/lib/requestUtils.ts @@ -25,12 +25,22 @@ const exchangeBaseURLMap = { [REST_CLIENT_TYPE_ENUM.advancedTrade]: 'https://api.coinbase.com', [REST_CLIENT_TYPE_ENUM.coinbaseApp]: 'https://api.coinbase.com', [REST_CLIENT_TYPE_ENUM.exchange]: 'https://api.exchange.coinbase.com', - [REST_CLIENT_TYPE_ENUM.prime]: 'https://api.prime.coinbase.com', // /v1 + [REST_CLIENT_TYPE_ENUM.prime]: 'https://api.prime.coinbase.com', [REST_CLIENT_TYPE_ENUM.international]: 'https://api.international.coinbase.com', [REST_CLIENT_TYPE_ENUM.commerce]: 'https://api.commerce.coinbase.com', } as const; +const exchangeSandboxURLMap = { + [REST_CLIENT_TYPE_ENUM.exchange]: + 'https://api-public.sandbox.exchange.coinbase.com', + [REST_CLIENT_TYPE_ENUM.international]: 'https://api-n5e1.coinbase.com', + [REST_CLIENT_TYPE_ENUM.advancedTrade]: 'NoSandboxAvailable', + [REST_CLIENT_TYPE_ENUM.coinbaseApp]: 'NoSandboxAvailable', + [REST_CLIENT_TYPE_ENUM.prime]: 'NoSandboxAvailable', + [REST_CLIENT_TYPE_ENUM.commerce]: 'NoSandboxAvailable', +} as const; + export interface RestClientOptions { /** * Your API key name. @@ -85,6 +95,18 @@ export interface RestClientOptions { | 'pt' | 'pt-br'; + /** + * Connect to the sandbox for supported products + * + * - Coinbase Exchange: https://docs.cdp.coinbase.com/exchange/docs/sandbox + * - Coinbase International: https://docs.cdp.coinbase.com/intx/docs/sandbox + * + * - Coinbase App: No sandbox available. + * - Coinbase Advanced Trade: No sandbox available. + * - Coinbase Prime: No sandbox available. + */ + useSandbox?: boolean; + /** * Enable keep alive for REST API requests (via axios). * See: https://github.com/tiagosiebler/bybit-api/issues/368 @@ -159,20 +181,19 @@ export function logInvalidOrderId( } export function getRestBaseUrl( - useTestnet: boolean, - restInverseOptions: RestClientOptions, + restClientOptions: RestClientOptions, restClientType: RestClientType, ): string { const exchangeBaseUrls = { livenet: exchangeBaseURLMap[restClientType], - testnet: 'https://noTestnet', + testnet: exchangeSandboxURLMap[restClientType], }; - if (restInverseOptions.baseUrl) { - return restInverseOptions.baseUrl; + if (restClientOptions.baseUrl) { + return restClientOptions.baseUrl; } - if (useTestnet) { + if (restClientOptions.useSandbox === true) { return exchangeBaseUrls.testnet; }