@coinbase/cdp-sdk
Version:
SDK for interacting with the Coinbase Developer Platform Wallet API
421 lines (402 loc) • 17.7 kB
text/typescript
/**
* @module DepositDestinations
*/
// This file was auto-generated by Fern from our API Definition.
import type { BaseClientOptions, BaseRequestOptions } from "../../../../BaseClient.js";
import { type NormalizedClientOptionsWithAuth, normalizeClientOptionsWithAuth } from "../../../../BaseClient.js";
import { mergeHeaders, mergeOnlyDefinedHeaders } from "../../../../core/headers.js";
import * as core from "../../../../core/index.js";
import * as environments from "../../../../environments.js";
import { handleNonStatusCodeError } from "../../../../errors/handleNonStatusCodeError.js";
import * as errors from "../../../../errors/index.js";
import * as CoinbaseApi from "../../../index.js";
export declare namespace DepositDestinationsClient {
export type Options = BaseClientOptions;
export interface RequestOptions extends BaseRequestOptions {}
}
/**
* Deposit Destinations allow you to manage where funds can be deposited into your accounts.
*
* ## Crypto Deposit Destinations
*
* Crypto deposit destinations are cryptocurrency addresses that you can generate and fetch via the API. Once created, these addresses can receive cryptocurrency payments on their specified network and will settle in your account balance.
*
* **Metadata:**
* You can attach metadata to any deposit destination you create to track the purpose or source of deposits.
*
*
* **Example:**
* ```json
* {
* "depositDestinationId": "depositDestination_123",
* "accountId": "account_456",
* "type": "crypto",
* "crypto": {
* "network": "base",
* "address": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e"
* },
* "target": {
* "accountId": "account_789",
* "asset": "usd"
* },
* "status": "active",
* "metadata": {
* "customer_id": "cust_789",
* "reference": "order-12345"
* }
* }
* ```
* Use the list endpoint to retrieve all deposit destinations.
*/
export class DepositDestinationsClient {
protected readonly _options: NormalizedClientOptionsWithAuth<DepositDestinationsClient.Options>;
constructor(options: DepositDestinationsClient.Options) {
this._options = normalizeClientOptionsWithAuth(options);
}
/**
* List deposit destinations. You can optionally filter the results by type, account ID, network, or cryptocurrency address. Results are sorted by creation date in descending order (newest first).
*
* @param {CoinbaseApi.ListDepositDestinationsRequest} request
* @param {DepositDestinationsClient.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws CoinbaseApi.BadRequestError
* @throws CoinbaseApi.UnauthorizedError
* @throws CoinbaseApi.InternalServerError
*
* @example
* ```ts
* await client.depositDestinations.listDepositDestinations({
* accountId: "account_af2937b0-9846-4fe7-bfe9-ccc22d935114",
* address: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
* type: "crypto",
* network: "base",
* pageToken: "eyJsYXN0X2lkIjogImFiYzEyMyIsICJ0aW1lc3RhbXAiOiAxNzA3ODIzNzAxfQ=="
* })
* ```
*/
public listDepositDestinations(
request: CoinbaseApi.ListDepositDestinationsRequest = {},
requestOptions?: DepositDestinationsClient.RequestOptions,
): core.HttpResponsePromise<CoinbaseApi.ListDepositDestinationsResponse> {
return core.HttpResponsePromise.fromPromise(this.__listDepositDestinations(request, requestOptions));
}
private async __listDepositDestinations(
request: CoinbaseApi.ListDepositDestinationsRequest = {},
requestOptions?: DepositDestinationsClient.RequestOptions,
): Promise<core.WithRawResponse<CoinbaseApi.ListDepositDestinationsResponse>> {
const _metadata: core.EndpointMetadata = {
security: [{ apiKeyAuth: [] }],
method: "GET",
path: "v2/deposit-destinations",
operationId: "endpoint_depositDestinations.listDepositDestinations",
};
const { accountId, address, type: type_, network, pageSize, pageToken } = request;
const _queryParams: Record<string, unknown> = {
accountId,
address,
type: type_,
network,
pageSize,
pageToken,
};
const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest({
endpointMetadata: _metadata,
clientOptions: this._options,
request: {
method: "GET",
},
});
const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
_authRequest.headers,
this._options?.headers,
requestOptions?.headers,
);
const _response = await core.fetcher({
url: core.url.join(
(await core.Supplier.get(this._options.baseUrl)) ??
(await core.Supplier.get(this._options.environment)) ??
environments.CoinbaseApiEnvironment.Default,
"v2/deposit-destinations",
),
method: "GET",
headers: _headers,
queryString: core.url
.queryBuilder()
.addMany(_queryParams)
.mergeAdditional(requestOptions?.queryParams)
.build(),
timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
abortSignal: requestOptions?.abortSignal,
endpointMetadata: _metadata,
fetchFn: this._options?.fetch,
logging: this._options.logging,
});
if (_response.ok) {
return {
data: _response.body as CoinbaseApi.ListDepositDestinationsResponse,
rawResponse: _response.rawResponse,
};
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 400:
throw new CoinbaseApi.BadRequestError(
_response.error.body as CoinbaseApi.Error_,
_response.rawResponse,
);
case 401:
throw new CoinbaseApi.UnauthorizedError(
_response.error.body as CoinbaseApi.Error_,
_response.rawResponse,
);
case 500:
throw new CoinbaseApi.InternalServerError(
_response.error.body as CoinbaseApi.Error_,
_response.rawResponse,
);
default:
throw new errors.CoinbaseApiError({
statusCode: _response.error.statusCode,
body: _response.error.body,
rawResponse: _response.rawResponse,
});
}
}
return handleNonStatusCodeError(_response.error, _response.rawResponse, "GET", "/v2/deposit-destinations");
}
/**
* Create a new deposit destination for an account. A deposit destination is a cryptocurrency address that can be used to receive funds. The address will be generated for the specified network.
*
* @param {CoinbaseApi.CreateDepositDestinationBody} request
* @param {DepositDestinationsClient.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws CoinbaseApi.BadRequestError
* @throws CoinbaseApi.UnauthorizedError
* @throws CoinbaseApi.NotFoundError
* @throws CoinbaseApi.UnprocessableEntityError
* @throws CoinbaseApi.InternalServerError
* @throws CoinbaseApi.ServiceUnavailableError
*
* @example
* ```ts
* await client.depositDestinations.createDepositDestination({
* idempotencyKey: "8e03978e-40d5-43e8-bc93-6894a57f9324",
* type: "crypto",
* accountId: "account_af2937b0-9846-4fe7-bfe9-ccc22d935114",
* target: {
* accountId: "account_af2937b0-9846-4fe7-bfe9-ccc22d935114",
* asset: "usd"
* },
* metadata: {
* "customer_id": "123e4567-e89b-12d3-a456-426614174000",
* "reference": "order-12345"
* },
* crypto: {
* network: "base"
* }
* })
* ```
*/
public createDepositDestination(
request: CoinbaseApi.CreateDepositDestinationBody,
requestOptions?: DepositDestinationsClient.RequestOptions,
): core.HttpResponsePromise<CoinbaseApi.DepositDestination> {
return core.HttpResponsePromise.fromPromise(this.__createDepositDestination(request, requestOptions));
}
private async __createDepositDestination(
request: CoinbaseApi.CreateDepositDestinationBody,
requestOptions?: DepositDestinationsClient.RequestOptions,
): Promise<core.WithRawResponse<CoinbaseApi.DepositDestination>> {
const _metadata: core.EndpointMetadata = {
security: [{ apiKeyAuth: [] }],
method: "POST",
path: "v2/deposit-destinations",
operationId: "endpoint_depositDestinations.createDepositDestination",
headers: ["X-Idempotency-Key"],
};
const { idempotencyKey, ..._body } = request;
const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest({
endpointMetadata: _metadata,
clientOptions: this._options,
request: {
method: "POST",
body: _body,
},
});
const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
_authRequest.headers,
this._options?.headers,
mergeOnlyDefinedHeaders({ "X-Idempotency-Key": idempotencyKey }),
requestOptions?.headers,
);
const _response = await core.fetcher({
url: core.url.join(
(await core.Supplier.get(this._options.baseUrl)) ??
(await core.Supplier.get(this._options.environment)) ??
environments.CoinbaseApiEnvironment.Default,
"v2/deposit-destinations",
),
method: "POST",
headers: _headers,
contentType: "application/json",
queryString: core.url.queryBuilder().mergeAdditional(requestOptions?.queryParams).build(),
requestType: "json",
body: _body,
timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
abortSignal: requestOptions?.abortSignal,
endpointMetadata: _metadata,
fetchFn: this._options?.fetch,
logging: this._options.logging,
});
if (_response.ok) {
return { data: _response.body as CoinbaseApi.DepositDestination, rawResponse: _response.rawResponse };
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 400:
throw new CoinbaseApi.BadRequestError(
_response.error.body as CoinbaseApi.Error_,
_response.rawResponse,
);
case 401:
throw new CoinbaseApi.UnauthorizedError(
_response.error.body as CoinbaseApi.Error_,
_response.rawResponse,
);
case 404:
throw new CoinbaseApi.NotFoundError(
_response.error.body as CoinbaseApi.Error_,
_response.rawResponse,
);
case 422:
throw new CoinbaseApi.UnprocessableEntityError(
_response.error.body as CoinbaseApi.Error_,
_response.rawResponse,
);
case 500:
throw new CoinbaseApi.InternalServerError(
_response.error.body as CoinbaseApi.Error_,
_response.rawResponse,
);
case 503:
throw new CoinbaseApi.ServiceUnavailableError(
_response.error.body as CoinbaseApi.Error_,
_response.rawResponse,
);
default:
throw new errors.CoinbaseApiError({
statusCode: _response.error.statusCode,
body: _response.error.body,
rawResponse: _response.rawResponse,
});
}
}
return handleNonStatusCodeError(_response.error, _response.rawResponse, "POST", "/v2/deposit-destinations");
}
/**
* Get a specific deposit destination by its ID.
*
* @param {CoinbaseApi.GetDepositDestinationByIdRequest} request
* @param {DepositDestinationsClient.RequestOptions} requestOptions - Request-specific configuration.
*
* @throws CoinbaseApi.BadRequestError
* @throws CoinbaseApi.UnauthorizedError
* @throws CoinbaseApi.NotFoundError
* @throws CoinbaseApi.InternalServerError
*
* @example
* ```ts
* await client.depositDestinations.getDepositDestinationById({
* depositDestinationId: "depositDestination_af2937b0-9846-4fe7-bfe9-ccc22d935114"
* })
* ```
*/
public getDepositDestinationById(
request: CoinbaseApi.GetDepositDestinationByIdRequest,
requestOptions?: DepositDestinationsClient.RequestOptions,
): core.HttpResponsePromise<CoinbaseApi.DepositDestination> {
return core.HttpResponsePromise.fromPromise(this.__getDepositDestinationById(request, requestOptions));
}
private async __getDepositDestinationById(
request: CoinbaseApi.GetDepositDestinationByIdRequest,
requestOptions?: DepositDestinationsClient.RequestOptions,
): Promise<core.WithRawResponse<CoinbaseApi.DepositDestination>> {
const _metadata: core.EndpointMetadata = {
security: [{ apiKeyAuth: [] }],
method: "GET",
path: "v2/deposit-destinations/{depositDestinationId}",
operationId: "endpoint_depositDestinations.getDepositDestinationById",
};
const { depositDestinationId } = request;
const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest({
endpointMetadata: _metadata,
clientOptions: this._options,
request: {
method: "GET",
},
});
const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
_authRequest.headers,
this._options?.headers,
requestOptions?.headers,
);
const _response = await core.fetcher({
url: core.url.join(
(await core.Supplier.get(this._options.baseUrl)) ??
(await core.Supplier.get(this._options.environment)) ??
environments.CoinbaseApiEnvironment.Default,
`v2/deposit-destinations/${core.url.encodePathParam(depositDestinationId)}`,
),
method: "GET",
headers: _headers,
queryString: core.url.queryBuilder().mergeAdditional(requestOptions?.queryParams).build(),
timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
abortSignal: requestOptions?.abortSignal,
endpointMetadata: _metadata,
fetchFn: this._options?.fetch,
logging: this._options.logging,
});
if (_response.ok) {
return { data: _response.body as CoinbaseApi.DepositDestination, rawResponse: _response.rawResponse };
}
if (_response.error.reason === "status-code") {
switch (_response.error.statusCode) {
case 400:
throw new CoinbaseApi.BadRequestError(
_response.error.body as CoinbaseApi.Error_,
_response.rawResponse,
);
case 401:
throw new CoinbaseApi.UnauthorizedError(
_response.error.body as CoinbaseApi.Error_,
_response.rawResponse,
);
case 404:
throw new CoinbaseApi.NotFoundError(
_response.error.body as CoinbaseApi.Error_,
_response.rawResponse,
);
case 500:
throw new CoinbaseApi.InternalServerError(
_response.error.body as CoinbaseApi.Error_,
_response.rawResponse,
);
default:
throw new errors.CoinbaseApiError({
statusCode: _response.error.statusCode,
body: _response.error.body,
rawResponse: _response.rawResponse,
});
}
}
return handleNonStatusCodeError(
_response.error,
_response.rawResponse,
"GET",
"/v2/deposit-destinations/{depositDestinationId}",
);
}
}