diff --git a/types/websocket.ts b/api/_shared.ts similarity index 100% rename from types/websocket.ts rename to api/_shared.ts diff --git a/types/marketData.ts b/api/marketData.ts similarity index 56% rename from types/marketData.ts rename to api/marketData.ts index 605220e..0c40ccd 100644 --- a/types/marketData.ts +++ b/api/marketData.ts @@ -1,3 +1,5 @@ +import { ClientContext } from "../factory/createClient.ts"; + export interface CorporateActionsQueryParams { symbols: string; types?: string; @@ -617,3 +619,378 @@ export interface HistoricalAuctionsParams { page_token?: string; sort?: string; } + +export const rest = ({ request }: ClientContext) => ({ + v1beta1: { + corporateActions: (queryParams: CorporateActionsQueryParams) => + request({ + path: "/v1beta1/corporate-actions", + method: "GET", + params: queryParams, + }), + forex: { + latestRates: (currencyPairs: string) => + request({ + path: "/v1beta1/forex/latest/rates", + method: "GET", + params: { currency_pairs: currencyPairs }, + }), + historicalRates: ( + currencyPairs: string, + timeframe: string, + start?: string, + end?: string, + limit?: number, + sort?: string, + pageToken?: string + ) => + request({ + path: "/v1beta1/forex/rates", + method: "GET", + params: { + currency_pairs: currencyPairs, + timeframe: timeframe, + start: start, + end: end, + limit: limit, + sort: sort, + page_token: pageToken, + }, + }), + }, + + logos: (symbol: string, placeholder?: boolean) => + // uhh, not sure yet how to handle this + request({ + path: `/v1beta1/logos/${symbol}`, + method: "GET", + params: { placeholder: placeholder }, + }), + + news: ( + start?: string, + end?: string, + sort?: string, + symbols?: string, + limit?: number, + includeContent?: boolean, + excludeContentless?: boolean, + pageToken?: string + ) => + request({ + path: "/v1beta1/news", + method: "GET", + params: { + start: start, + end: end, + sort: sort, + symbols: symbols, + limit: limit, + include_content: includeContent, + exclude_contentless: excludeContentless, + page_token: pageToken, + }, + }), + options: { + bars: ( + symbols: string, + timeframe: string, + start?: string, + end?: string, + limit?: number, + pageToken?: string, + sort?: string + ) => + request({ + path: "/v1beta1/options/bars", + method: "GET", + params: { + symbols: symbols, + timeframe: timeframe, + start: start, + end: end, + limit: limit, + page_token: pageToken, + sort: sort, + }, + }), + meta: { + exchanges: () => + request({ + path: "/v1beta1/options/meta/exchanges", + method: "GET", + }), + }, + quotes: { + latest: (symbols: string, feed?: string) => + request({ + path: "/v1beta1/options/quotes/latest", + method: "GET", + params: { symbols, feed }, + }), + }, + trades: { + latest: (symbols: string, feed?: string) => + request({ + path: "/v1beta1/options/trades/latest", + method: "GET", + params: { symbols, feed }, + }), + historical: ( + symbols: string, + start?: string, + end?: string, + limit?: number, + pageToken?: string, + sort?: string + ) => + request({ + path: "/v1beta1/options/trades", + method: "GET", + params: { symbols, start, end, limit, pageToken, sort }, + }), + }, + snapshots: (symbols: string, feed?: string) => + request({ + path: "/v1beta1/options/snapshots", + method: "GET", + params: { symbols, feed }, + }), + snapshotsByUnderlying: (underlyingSymbol: string, feed?: string) => + // uhh, not sure yet how to handle this + request({ + path: `/v1beta1/options/snapshots/${underlyingSymbol}`, + method: "GET", + params: { feed }, + }), + }, + screener: { + getMostActives: (by = "volume", top = 10) => + request({ + path: "/v1beta1/screener/stocks/most-actives", + method: "GET", + params: { by, top }, + }), + getMarketMovers: (marketType: string, top: number = 10) => + request({ + path: `/v1beta1/screener/${marketType}/movers`, + method: "GET", + params: { top }, + }), + }, + }, + v1beta3: { + crypto: { + getHistoricalBars: ( + loc: string, + symbols: string, + timeframe: string, + start?: string, + end?: string, + limit: number = 1000, + page_token?: string, + sort?: string + ) => + request({ + path: `/v1beta3/crypto/${loc}/bars`, + method: "GET", + params: { + symbols, + timeframe, + start, + end, + limit, + page_token, + sort, + }, + }), + getLatestBars: (loc: string, symbols: string) => + request({ + path: `/v1beta3/crypto/${loc}/latest/bars`, + method: "GET", + params: { + symbols, + }, + }), + getLatestOrderbooks: (loc: string, symbols: string) => + request({ + path: `/v1beta3/crypto/${loc}/latest/orderbooks`, + method: "GET", + params: { + symbols, + }, + }), + getLatestQuotes: (loc: string, symbols: string) => + request({ + path: `/v1beta3/crypto/${loc}/latest/quotes`, + method: "GET", + params: { + symbols, + }, + }), + getLatestTrades: (loc: string, symbols: string) => + request({ + path: `/v1beta3/crypto/${loc}/latest/trades`, + method: "GET", + params: { + symbols, + }, + }), + getHistoricalQuotes: ( + loc: string, + symbols: string, + start?: string, + end?: string, + limit?: number, + pageToken?: string, + sort?: string + ) => + request({ + path: `/v1beta3/crypto/${loc}/quotes`, + method: "GET", + params: { + symbols, + start, + end, + limit, + page_token: pageToken, + sort, + }, + }), + getSnapshots: (loc: string, symbols: string) => + request({ + path: `/v1beta3/crypto/${loc}/snapshots`, + method: "GET", + params: { + symbols, + }, + }), + getHistoricalTrades: ( + loc: string, + symbols: string, + start?: string, + end?: string, + limit?: number, + page_token?: string, + sort?: string + ) => + request({ + path: `/v1beta3/crypto/${loc}/trades`, + method: "GET", + params: { + symbols, + start, + end, + limit, + page_token, + sort, + }, + }), + }, + }, + v2: { + stocks: { + getHistoricalAuctions: ( + symbols: string, + start?: string, + end?: string, + limit?: number, + asof?: string, + feed?: string, + page_token?: string, + sort?: string + ) => + request({ + path: `/v2/stocks/auctions`, + method: "GET", + params: { + symbols, + start, + end, + limit, + asof, + feed, + page_token, + sort, + }, + }), + getHistoricalBars: ( + symbols: string, + timeframe: string, + start?: string, + end?: string, + limit?: number, + adjustment?: string, + asof?: string, + feed?: string, + page_token?: string, + sort?: string + ) => + request({ + path: `/v2/stocks/bars`, + method: "GET", + params: { + symbols, + timeframe, + start, + end, + limit, + adjustment, + asof, + feed, + page_token, + sort, + }, + }), + getLatestBars: (symbols: string, feed?: string, currency?: string) => + request({ + path: `/v2/stocks/bars/latest`, + method: "GET", + params: { symbols, feed, currency }, + }), + getConditionCodes: (tickType: string, tape: string) => + request({ + path: `/v2/stocks/meta/conditions/${tickType}`, + method: "GET", + params: { tape }, + }), + getExchangeCodes: () => + request({ + path: "/v2/stocks/meta/exchanges", + method: "GET", + }), + getHistoricalQuotes: (params: HistoricalQuotesParams) => + request({ + path: "/v2/stocks/quotes", + method: "GET", + params, + }), + getLatestQuotes: (params: LatestQuotesParams) => + request({ + path: "/v2/stocks/quotes/latest", + method: "GET", + params, + }), + getSnapshots: (params: SnapshotParams) => + request({ + path: "/v2/stocks/snapshots", + method: "GET", + params, + }), + getTrades: (params: TradeParams) => + request({ + path: "/v2/stocks/trades", + method: "GET", + params, + }), + getLatestTrades: (params: LatestTradeParams) => + request({ + path: "/v2/stocks/trades/latest", + method: "GET", + params, + }), + }, + }, +}); + +export const websocket = ({ options, request }: ClientContext) => null; diff --git a/types/trade.ts b/api/trade.ts similarity index 64% rename from types/trade.ts rename to api/trade.ts index 79c4b19..d12f677 100644 --- a/types/trade.ts +++ b/api/trade.ts @@ -1,4 +1,5 @@ -import { EventMap, WebSocketWithEvents } from "./websocket.ts"; +import { ClientContext } from "../factory/createClient.ts"; +import { EventMap, WebSocketWithEvents } from "../types/websocket.ts"; export type Account = { id: string; @@ -899,3 +900,344 @@ export type OptionsWebSocket = { on: (event: "message", handler: (data: OptionMessage[]) => void) => void; send: (data: OptionSubscriptionRequest) => void; }; + +export const rest = ({ request }: ClientContext) => ({ + v2: { + account: { + get: () => + request({ + path: "/v2/account", + }), + }, + orders: { + post: (options: CreateOrderOptions) => + request({ + path: "/v2/orders", + method: "POST", + data: options, + }), + get: (options?: PatchOrderOptions | string, nested?: boolean) => { + let path = "/v2/orders"; + let params = {}; + + if (typeof options === "string") { + path += `/${options}`; + + if (nested !== undefined) { + params = { nested }; + } + } else if (typeof options === "object") { + params = options; + } + + return request({ + path, + method: "GET", + params, + }); + }, + delete: (orderId?: string) => { + let path = "/v2/orders"; + + if (orderId) { + path += `/${orderId}`; + } + + return request<{ id?: string; status?: string }[] | void>({ + path, + method: "DELETE", + }); + }, + patch: (orderId: string, options: PatchOrderOptions) => + request({ + path: `/v2/orders/${orderId}`, + method: "PATCH", + data: options, + }), + }, + positions: { + get: (symbol_or_asset_id?: string) => + request({ + path: symbol_or_asset_id + ? `/v2/positions/${symbol_or_asset_id}` + : "/v2/positions", + method: "GET", + }), + delete: (params: ClosePositionOptions) => + request({ + path: params.symbol_or_asset_id + ? `/v2/positions/${params.symbol_or_asset_id}` + : "/v2/positions", + method: "DELETE", + params: params.symbol_or_asset_id + ? { qty: params.qty, percentage: params.percentage } + : { cancel_orders: params.cancel_orders }, + }), + post: (symbol_or_contract_id: string) => + request({ + path: `/v2/positions/${symbol_or_contract_id}/exercise`, + method: "POST", + }), + }, + portfolioHistory: { + get: (params?: PortfolioHistoryParams) => + request({ + path: "/v2/account/portfolio/history", + method: "GET", + params, + }), + }, + watchlists: { + get: (watchlist_id?: string) => + request({ + path: watchlist_id + ? `/v2/watchlists/${watchlist_id}` + : "/v2/watchlists", + method: "GET", + }), + post: ({ name, symbols }: CreateWatchlistParams) => + request({ + path: "/v2/watchlists", + method: "POST", + data: { name, symbols }, + }), + put: ({ + watchlist_id, + name, + symbols, + }: { watchlist_id: string } & UpdateWatchlistParams) => + request({ + path: `/v2/watchlists/${watchlist_id}`, + method: "PUT", + data: { name, symbols }, + }), + deleteById: ({ watchlist_id }: { watchlist_id: string }) => + request({ + path: `/v2/watchlists/${watchlist_id}`, + method: "DELETE", + }), + addAsset: ({ + watchlist_id, + symbol, + }: { + watchlist_id: string; + symbol: string; + }) => + request({ + path: `/v2/watchlists/${watchlist_id}`, + method: "POST", + data: { symbol }, + }), + getByName: ({ name }: GetWatchlistByNameParams) => + request({ + path: "/v2/watchlists:by_name", + method: "GET", + params: { name }, + }), + updateByName: ({ name, newName, symbols }: UpdateWatchlistByNameParams) => + request({ + path: "/v2/watchlists:by_name", + method: "PUT", + params: { name }, + data: { name: newName, symbols }, + }), + addAssetByName: ({ name, symbol }: AddAssetToWatchlistParams) => + request({ + path: "/v2/watchlists:by_name", + method: "POST", + params: { name }, + data: { symbol }, + }), + deleteByName: ({ name }: DeleteWatchlistByNameParams) => + request({ + path: "/v2/watchlists:by_name", + method: "DELETE", + params: { name }, + }), + deleteSymbol: ({ + watchlistId, + symbol, + }: DeleteSymbolFromWatchlistParams) => + request({ + path: `/v2/watchlists/${watchlistId}/${symbol}`, + method: "DELETE", + }), + }, + accountConfigurations: { + get: () => + request({ + path: "/v2/account/configurations", + method: "GET", + }), + patch: (updatedConfig: UpdatedAccountConfigurations) => + request({ + path: "/v2/account/configurations", + method: "PATCH", + data: updatedConfig, + }), + }, + activities: { + get: ( + activityType?: string, + options?: { + date?: string; + until?: string; + after?: string; + direction?: string; + pageSize?: number; + pageToken?: string; + category?: string; + } + ) => + request({ + path: `/v2/account/activities${ + activityType ? `/${activityType}` : "" + }`, + method: "GET", + params: { + date: options?.date, + until: options?.until, + after: options?.after, + direction: options?.direction, + page_size: options?.pageSize, + page_token: options?.pageToken, + category: options?.category, + }, + }), + }, + calendar: { + get: (options?: { + start?: string; + end?: string; + dateType?: "TRADING" | "SETTLEMENT"; + }) => + request({ + path: "/v2/calendar", + method: "GET", + params: { + start: options?.start, + end: options?.end, + date_type: options?.dateType, + }, + }), + }, + clock: { + get: () => + request({ + path: "/v2/clock", + method: "GET", + }), + }, + assets: { + getAll: (options?: { + status?: string; + asset_class?: string; + exchange?: string; + attributes?: string[]; + }) => + request({ + path: "/v2/assets", + method: "GET", + params: { + status: options?.status, + asset_class: options?.asset_class, + exchange: options?.exchange, + attributes: options?.attributes?.join(","), + }, + }), + getAsset: (symbolOrAssetId: string) => + request({ + path: `/v2/assets/${symbolOrAssetId}`, + method: "GET", + }), + }, + options: { + get: (queryParams: OptionContractsQueryParams, symbolOrId?: string) => { + let path = "/v2/options/contracts"; + if (symbolOrId) { + path += `/${symbolOrId}`; + } + return request({ + path, + method: "GET", + params: queryParams, + }); + }, + }, + corporateActions: { + announcements: { + get: ( + queryParams?: AnnouncementsQueryParams, + announcementId?: string + ) => { + if (announcementId) { + return request({ + path: `/v2/corporate_actions/announcements/${announcementId}`, + method: "GET", + }); + } else { + return request({ + path: "/v2/corporate_actions/announcements", + method: "GET", + params: queryParams, + }); + } + }, + }, + }, + cryptoFunding: { + getWallets: (asset?: string) => + request({ + path: "/v2/wallets", + method: "GET", + params: { asset }, + }), + + requestWithdrawal: (withdrawalParams: WithdrawalParams) => + request({ + path: "/v2/wallets/transfers", + method: "POST", + data: withdrawalParams, + }), + getTransfers: (transferId?: string) => { + if (transferId) { + return request({ + path: `/v2/wallets/transfers/${transferId}`, + method: "GET", + }); + } else { + return request({ + path: "/v2/wallets/transfers", + method: "GET", + }); + } + }, + getWhitelistedAddresses: () => + request({ + path: "/v2/wallets/whitelists", + method: "GET", + }), + requestWhitelistedAddress: ( + whitelistedAddressParams: WhitelistedAddressParams + ) => + request({ + path: "/v2/wallets/whitelists", + method: "POST", + data: whitelistedAddressParams, + }), + deleteWhitelistedAddress: (whitelistedAddressId: string) => + request({ + path: `/v2/wallets/whitelists/${whitelistedAddressId}`, + method: "DELETE", + }), + estimateTransactionFee: (transactionParams: TransactionParams) => + request({ + path: "/v2/wallets/fees/estimate", + method: "GET", + params: transactionParams, + }), + }, + }, +}); + +export const websocket = ({ options, request }: ClientContext) => null; diff --git a/factories/index.ts b/factories/index.ts deleted file mode 100644 index f35f2e7..0000000 --- a/factories/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export { createClient } from "./createClient.ts"; -export { createTokenBucket } from "./createTokenBucket.ts"; diff --git a/factories/createClient.test.ts b/factory/createClient.test.ts similarity index 100% rename from factories/createClient.test.ts rename to factory/createClient.test.ts diff --git a/factories/createClient.ts b/factory/createClient.ts similarity index 77% rename from factories/createClient.ts rename to factory/createClient.ts index b444143..6145ecd 100644 --- a/factories/createClient.ts +++ b/factory/createClient.ts @@ -1,13 +1,3 @@ -import { - CryptoWebSocket, - NewsWebSocket, - OptionsWebSocket, - StockDataWebSocket, - TradeWebSocket, -} from "../types/trade.ts"; - -import { default as marketData } from "../methods/marketData.ts"; -import { default as trade } from "../methods/trade.ts"; import { TokenBucketOptions, createTokenBucket } from "./createTokenBucket.ts"; // The options required to make a request @@ -49,20 +39,48 @@ type ClientMethods any> = ReturnType< ClientContextConsumer> >; +// The expected type for an event map for a given channel +export type EventMap = { + // deno-lint-ignore no-explicit-any + [EventKey: string]: { T: string; data: any }; +}; + +// The expected type of a subscription request for a channel event map +export type SubscriptionRequest = { + channel: keyof ChannelEventMap; + symbols?: string[]; +}; + +export type WebSocketWithEvents = { + on: ( + channel: E, + event: ChannelEventMap[E]["T"], + handler: ( + data: Extract + ) => void + ) => void; + subscribe: ( + requests: SubscriptionRequest[] + ) => Promise[]>; + unsubscribe: ( + requests: SubscriptionRequest[] + ) => Promise[]>; +}; + +export type ClientSub = { + rest: ClientMethods; + websocket: WebSocketWithEvents; +}; + // The object returned by createClient export type Client = { - rest: { - trade: ClientMethods; - marketData: ClientMethods; + trade: { + rest: {}; + websocket: {}; }; - websocket: { - trade: TradeWebSocket; - marketData: { - stock: StockDataWebSocket; - crypto: CryptoWebSocket; - news: NewsWebSocket; - options: OptionsWebSocket; - }; + marketData: { + rest: {}; + websocket: {}; }; }; diff --git a/factories/createTokenBucket.test.ts b/factory/createTokenBucket.test.ts similarity index 100% rename from factories/createTokenBucket.test.ts rename to factory/createTokenBucket.test.ts diff --git a/factories/createTokenBucket.ts b/factory/createTokenBucket.ts similarity index 100% rename from factories/createTokenBucket.ts rename to factory/createTokenBucket.ts diff --git a/methods/marketData.ts b/methods/marketData.ts deleted file mode 100644 index 38911c4..0000000 --- a/methods/marketData.ts +++ /dev/null @@ -1,409 +0,0 @@ -// @todo fix type conflicts with crypto and stocks - -import { - ConditionCodesResponse, - CorporateActionsQueryParams, - CorporateActionsResponse, - CryptoHistoricalTradesResponse, - CryptoSnapshotsResponse, - ExchangeCodesResponse, - HistoricalAuctionsResponse, - HistoricalBarsResponse, - HistoricalForexRatesResponse, - HistoricalOptionBarsResponse, - HistoricalQuotesParams, - HistoricalQuotesResponse, - HistoricalTradesResponse, - LatestBarsResponse, - LatestForexRatesResponse, - LatestOrderbooksResponse, - LatestQuotesParams, - LatestQuotesResponse, - LatestTradeParams, - LatestTradeResponse, - LatestTradesResponse, - MarketMoversResponse, - MostActivesResponse, - NewsResponse, - OptionExchangeMapping, - SnapshotParams, - SnapshotResponse, - SnapshotsResponse, - TradeParams, - TradeResponse, -} from "../types/marketData.ts"; - -import { ClientContext } from "../factories/createClient.ts"; - -export default ({ request }: ClientContext) => ({ - v1beta1: { - corporateActions: (queryParams: CorporateActionsQueryParams) => - request({ - path: "/v1beta1/corporate-actions", - method: "GET", - params: queryParams, - }), - forex: { - latestRates: (currencyPairs: string) => - request({ - path: "/v1beta1/forex/latest/rates", - method: "GET", - params: { currency_pairs: currencyPairs }, - }), - historicalRates: ( - currencyPairs: string, - timeframe: string, - start?: string, - end?: string, - limit?: number, - sort?: string, - pageToken?: string - ) => - request({ - path: "/v1beta1/forex/rates", - method: "GET", - params: { - currency_pairs: currencyPairs, - timeframe: timeframe, - start: start, - end: end, - limit: limit, - sort: sort, - page_token: pageToken, - }, - }), - }, - - logos: (symbol: string, placeholder?: boolean) => - // uhh, not sure yet how to handle this - request({ - path: `/v1beta1/logos/${symbol}`, - method: "GET", - params: { placeholder: placeholder }, - }), - - news: ( - start?: string, - end?: string, - sort?: string, - symbols?: string, - limit?: number, - includeContent?: boolean, - excludeContentless?: boolean, - pageToken?: string - ) => - request({ - path: "/v1beta1/news", - method: "GET", - params: { - start: start, - end: end, - sort: sort, - symbols: symbols, - limit: limit, - include_content: includeContent, - exclude_contentless: excludeContentless, - page_token: pageToken, - }, - }), - options: { - bars: ( - symbols: string, - timeframe: string, - start?: string, - end?: string, - limit?: number, - pageToken?: string, - sort?: string - ) => - request({ - path: "/v1beta1/options/bars", - method: "GET", - params: { - symbols: symbols, - timeframe: timeframe, - start: start, - end: end, - limit: limit, - page_token: pageToken, - sort: sort, - }, - }), - meta: { - exchanges: () => - request({ - path: "/v1beta1/options/meta/exchanges", - method: "GET", - }), - }, - quotes: { - latest: (symbols: string, feed?: string) => - request({ - path: "/v1beta1/options/quotes/latest", - method: "GET", - params: { symbols, feed }, - }), - }, - trades: { - latest: (symbols: string, feed?: string) => - request({ - path: "/v1beta1/options/trades/latest", - method: "GET", - params: { symbols, feed }, - }), - historical: ( - symbols: string, - start?: string, - end?: string, - limit?: number, - pageToken?: string, - sort?: string - ) => - request({ - path: "/v1beta1/options/trades", - method: "GET", - params: { symbols, start, end, limit, pageToken, sort }, - }), - }, - snapshots: (symbols: string, feed?: string) => - request({ - path: "/v1beta1/options/snapshots", - method: "GET", - params: { symbols, feed }, - }), - snapshotsByUnderlying: (underlyingSymbol: string, feed?: string) => - // uhh, not sure yet how to handle this - request({ - path: `/v1beta1/options/snapshots/${underlyingSymbol}`, - method: "GET", - params: { feed }, - }), - }, - screener: { - getMostActives: (by = "volume", top = 10) => - request({ - path: "/v1beta1/screener/stocks/most-actives", - method: "GET", - params: { by, top }, - }), - getMarketMovers: (marketType: string, top: number = 10) => - request({ - path: `/v1beta1/screener/${marketType}/movers`, - method: "GET", - params: { top }, - }), - }, - }, - v1beta3: { - crypto: { - getHistoricalBars: ( - loc: string, - symbols: string, - timeframe: string, - start?: string, - end?: string, - limit: number = 1000, - page_token?: string, - sort?: string - ) => - request({ - path: `/v1beta3/crypto/${loc}/bars`, - method: "GET", - params: { - symbols, - timeframe, - start, - end, - limit, - page_token, - sort, - }, - }), - getLatestBars: (loc: string, symbols: string) => - request({ - path: `/v1beta3/crypto/${loc}/latest/bars`, - method: "GET", - params: { - symbols, - }, - }), - getLatestOrderbooks: (loc: string, symbols: string) => - request({ - path: `/v1beta3/crypto/${loc}/latest/orderbooks`, - method: "GET", - params: { - symbols, - }, - }), - getLatestQuotes: (loc: string, symbols: string) => - request({ - path: `/v1beta3/crypto/${loc}/latest/quotes`, - method: "GET", - params: { - symbols, - }, - }), - getLatestTrades: (loc: string, symbols: string) => - request({ - path: `/v1beta3/crypto/${loc}/latest/trades`, - method: "GET", - params: { - symbols, - }, - }), - getHistoricalQuotes: ( - loc: string, - symbols: string, - start?: string, - end?: string, - limit?: number, - pageToken?: string, - sort?: string - ) => - request({ - path: `/v1beta3/crypto/${loc}/quotes`, - method: "GET", - params: { - symbols, - start, - end, - limit, - page_token: pageToken, - sort, - }, - }), - getSnapshots: (loc: string, symbols: string) => - request({ - path: `/v1beta3/crypto/${loc}/snapshots`, - method: "GET", - params: { - symbols, - }, - }), - getHistoricalTrades: ( - loc: string, - symbols: string, - start?: string, - end?: string, - limit?: number, - page_token?: string, - sort?: string - ) => - request({ - path: `/v1beta3/crypto/${loc}/trades`, - method: "GET", - params: { - symbols, - start, - end, - limit, - page_token, - sort, - }, - }), - }, - }, - v2: { - stocks: { - getHistoricalAuctions: ( - symbols: string, - start?: string, - end?: string, - limit?: number, - asof?: string, - feed?: string, - page_token?: string, - sort?: string - ) => - request({ - path: `/v2/stocks/auctions`, - method: "GET", - params: { - symbols, - start, - end, - limit, - asof, - feed, - page_token, - sort, - }, - }), - getHistoricalBars: ( - symbols: string, - timeframe: string, - start?: string, - end?: string, - limit?: number, - adjustment?: string, - asof?: string, - feed?: string, - page_token?: string, - sort?: string - ) => - request({ - path: `/v2/stocks/bars`, - method: "GET", - params: { - symbols, - timeframe, - start, - end, - limit, - adjustment, - asof, - feed, - page_token, - sort, - }, - }), - getLatestBars: (symbols: string, feed?: string, currency?: string) => - request({ - path: `/v2/stocks/bars/latest`, - method: "GET", - params: { symbols, feed, currency }, - }), - getConditionCodes: (tickType: string, tape: string) => - request({ - path: `/v2/stocks/meta/conditions/${tickType}`, - method: "GET", - params: { tape }, - }), - getExchangeCodes: () => - request({ - path: "/v2/stocks/meta/exchanges", - method: "GET", - }), - getHistoricalQuotes: (params: HistoricalQuotesParams) => - request({ - path: "/v2/stocks/quotes", - method: "GET", - params, - }), - getLatestQuotes: (params: LatestQuotesParams) => - request({ - path: "/v2/stocks/quotes/latest", - method: "GET", - params, - }), - getSnapshots: (params: SnapshotParams) => - request({ - path: "/v2/stocks/snapshots", - method: "GET", - params, - }), - getTrades: (params: TradeParams) => - request({ - path: "/v2/stocks/trades", - method: "GET", - params, - }), - getLatestTrades: (params: LatestTradeParams) => - request({ - path: "/v2/stocks/trades/latest", - method: "GET", - params, - }), - }, - }, -}); diff --git a/methods/trade.ts b/methods/trade.ts deleted file mode 100644 index 495cc1b..0000000 --- a/methods/trade.ts +++ /dev/null @@ -1,376 +0,0 @@ -import { - Account, - AccountActivity, - AccountConfigurations, - AddAssetToWatchlistParams, - AnnouncementsQueryParams, - Asset, - ClosePositionOptions, - CorporateActionAnnouncement, - CreateOrderOptions, - CreateWatchlistParams, - CryptoFundingTransfer, - CryptoFundingWallet, - DeleteSymbolFromWatchlistParams, - DeleteWatchlistByNameParams, - GetWatchlistByNameParams, - MarketCalendar, - MarketClock, - OptionContract, - OptionContractsQueryParams, - Order, - PatchOrderOptions, - PortfolioHistoryParams, - PortfolioHistoryResponse, - Position, - TransactionFeeResponse, - TransactionParams, - UpdateWatchlistByNameParams, - UpdateWatchlistParams, - UpdatedAccountConfigurations, - Watchlist, - WhitelistedAddress, - WhitelistedAddressParams, - WithdrawalParams, -} from "../types/trade.ts"; - -import { ClientContext } from "../factories/createClient.ts"; - -export default ({ request }: ClientContext) => ({ - v2: { - account: { - get: () => - request({ - path: "/v2/account", - }), - }, - orders: { - post: (options: CreateOrderOptions) => - request({ - path: "/v2/orders", - method: "POST", - data: options, - }), - get: (options?: PatchOrderOptions | string, nested?: boolean) => { - let path = "/v2/orders"; - let params = {}; - - if (typeof options === "string") { - path += `/${options}`; - - if (nested !== undefined) { - params = { nested }; - } - } else if (typeof options === "object") { - params = options; - } - - return request({ - path, - method: "GET", - params, - }); - }, - delete: (orderId?: string) => { - let path = "/v2/orders"; - - if (orderId) { - path += `/${orderId}`; - } - - return request<{ id?: string; status?: string }[] | void>({ - path, - method: "DELETE", - }); - }, - patch: (orderId: string, options: PatchOrderOptions) => - request({ - path: `/v2/orders/${orderId}`, - method: "PATCH", - data: options, - }), - }, - positions: { - get: (symbol_or_asset_id?: string) => - request({ - path: symbol_or_asset_id - ? `/v2/positions/${symbol_or_asset_id}` - : "/v2/positions", - method: "GET", - }), - delete: (params: ClosePositionOptions) => - request({ - path: params.symbol_or_asset_id - ? `/v2/positions/${params.symbol_or_asset_id}` - : "/v2/positions", - method: "DELETE", - params: params.symbol_or_asset_id - ? { qty: params.qty, percentage: params.percentage } - : { cancel_orders: params.cancel_orders }, - }), - post: (symbol_or_contract_id: string) => - request({ - path: `/v2/positions/${symbol_or_contract_id}/exercise`, - method: "POST", - }), - }, - portfolioHistory: { - get: (params?: PortfolioHistoryParams) => - request({ - path: "/v2/account/portfolio/history", - method: "GET", - params, - }), - }, - watchlists: { - get: (watchlist_id?: string) => - request({ - path: watchlist_id - ? `/v2/watchlists/${watchlist_id}` - : "/v2/watchlists", - method: "GET", - }), - post: ({ name, symbols }: CreateWatchlistParams) => - request({ - path: "/v2/watchlists", - method: "POST", - data: { name, symbols }, - }), - put: ({ - watchlist_id, - name, - symbols, - }: { watchlist_id: string } & UpdateWatchlistParams) => - request({ - path: `/v2/watchlists/${watchlist_id}`, - method: "PUT", - data: { name, symbols }, - }), - deleteById: ({ watchlist_id }: { watchlist_id: string }) => - request({ - path: `/v2/watchlists/${watchlist_id}`, - method: "DELETE", - }), - addAsset: ({ - watchlist_id, - symbol, - }: { - watchlist_id: string; - symbol: string; - }) => - request({ - path: `/v2/watchlists/${watchlist_id}`, - method: "POST", - data: { symbol }, - }), - getByName: ({ name }: GetWatchlistByNameParams) => - request({ - path: "/v2/watchlists:by_name", - method: "GET", - params: { name }, - }), - updateByName: ({ name, newName, symbols }: UpdateWatchlistByNameParams) => - request({ - path: "/v2/watchlists:by_name", - method: "PUT", - params: { name }, - data: { name: newName, symbols }, - }), - addAssetByName: ({ name, symbol }: AddAssetToWatchlistParams) => - request({ - path: "/v2/watchlists:by_name", - method: "POST", - params: { name }, - data: { symbol }, - }), - deleteByName: ({ name }: DeleteWatchlistByNameParams) => - request({ - path: "/v2/watchlists:by_name", - method: "DELETE", - params: { name }, - }), - deleteSymbol: ({ - watchlistId, - symbol, - }: DeleteSymbolFromWatchlistParams) => - request({ - path: `/v2/watchlists/${watchlistId}/${symbol}`, - method: "DELETE", - }), - }, - accountConfigurations: { - get: () => - request({ - path: "/v2/account/configurations", - method: "GET", - }), - patch: (updatedConfig: UpdatedAccountConfigurations) => - request({ - path: "/v2/account/configurations", - method: "PATCH", - data: updatedConfig, - }), - }, - activities: { - get: ( - activityType?: string, - options?: { - date?: string; - until?: string; - after?: string; - direction?: string; - pageSize?: number; - pageToken?: string; - category?: string; - } - ) => - request({ - path: `/v2/account/activities${ - activityType ? `/${activityType}` : "" - }`, - method: "GET", - params: { - date: options?.date, - until: options?.until, - after: options?.after, - direction: options?.direction, - page_size: options?.pageSize, - page_token: options?.pageToken, - category: options?.category, - }, - }), - }, - calendar: { - get: (options?: { - start?: string; - end?: string; - dateType?: "TRADING" | "SETTLEMENT"; - }) => - request({ - path: "/v2/calendar", - method: "GET", - params: { - start: options?.start, - end: options?.end, - date_type: options?.dateType, - }, - }), - }, - clock: { - get: () => - request({ - path: "/v2/clock", - method: "GET", - }), - }, - assets: { - getAll: (options?: { - status?: string; - asset_class?: string; - exchange?: string; - attributes?: string[]; - }) => - request({ - path: "/v2/assets", - method: "GET", - params: { - status: options?.status, - asset_class: options?.asset_class, - exchange: options?.exchange, - attributes: options?.attributes?.join(","), - }, - }), - getAsset: (symbolOrAssetId: string) => - request({ - path: `/v2/assets/${symbolOrAssetId}`, - method: "GET", - }), - }, - options: { - get: (queryParams: OptionContractsQueryParams, symbolOrId?: string) => { - let path = "/v2/options/contracts"; - if (symbolOrId) { - path += `/${symbolOrId}`; - } - return request({ - path, - method: "GET", - params: queryParams, - }); - }, - }, - corporateActions: { - announcements: { - get: ( - queryParams?: AnnouncementsQueryParams, - announcementId?: string - ) => { - if (announcementId) { - return request({ - path: `/v2/corporate_actions/announcements/${announcementId}`, - method: "GET", - }); - } else { - return request({ - path: "/v2/corporate_actions/announcements", - method: "GET", - params: queryParams, - }); - } - }, - }, - }, - cryptoFunding: { - getWallets: (asset?: string) => - request({ - path: "/v2/wallets", - method: "GET", - params: { asset }, - }), - - requestWithdrawal: (withdrawalParams: WithdrawalParams) => - request({ - path: "/v2/wallets/transfers", - method: "POST", - data: withdrawalParams, - }), - getTransfers: (transferId?: string) => { - if (transferId) { - return request({ - path: `/v2/wallets/transfers/${transferId}`, - method: "GET", - }); - } else { - return request({ - path: "/v2/wallets/transfers", - method: "GET", - }); - } - }, - getWhitelistedAddresses: () => - request({ - path: "/v2/wallets/whitelists", - method: "GET", - }), - requestWhitelistedAddress: ( - whitelistedAddressParams: WhitelistedAddressParams - ) => - request({ - path: "/v2/wallets/whitelists", - method: "POST", - data: whitelistedAddressParams, - }), - deleteWhitelistedAddress: (whitelistedAddressId: string) => - request({ - path: `/v2/wallets/whitelists/${whitelistedAddressId}`, - method: "DELETE", - }), - estimateTransactionFee: (transactionParams: TransactionParams) => - request({ - path: "/v2/wallets/fees/estimate", - method: "GET", - params: transactionParams, - }), - }, - }, -});