import { EventEmitter } from 'events'; import * as request from 'request'; import { Readable } from 'stream'; declare module 'gdax' { export type HttpResponse = request.Response; export type callback = (err: any, response: HttpResponse, data: T) => void; interface ApiServerTime { iso: string; epoch: number; } export type ProductTicker = { trade_id: string, price: string, size: string, bid: string, ask: string, volume: string, time: string } interface BaseOrder { type: string; side: 'buy' | 'sell'; product_id: string; client_oid?: string; stp?: 'dc' | 'co' | 'cn' | 'cb'; stop?: 'loss' | 'entry'; stop_price?: string; } interface LimitOrder extends BaseOrder { type: 'limit'; price: string; size: string; time_in_force?: 'GTC' | 'GTT' | 'IOC' | 'FOK'; cancel_after?: 'min' | 'hour' | 'day'; post_only?: boolean; } /** * Only one of `size` and `funds` are required for market and limit orders (the other can be explicitly assigned null or undefined). However, * it is advisable to include both. If funds is not specified, the entire user balance is placed on hold until the order is filled which * will prevent other orders from being placed in the interim. This can cause issues for HFT algorithms for example. */ interface MarketOrder extends BaseOrder { type: 'market'; size: string | null; funds: string | null; } interface StopOrder extends BaseOrder { type: 'stop'; size: string; funds: string; } export type OrderParams = MarketOrder | LimitOrder | StopOrder; export interface BaseOrderInfo { id: string; price: string; size: string; product_id: string; side: 'buy' | 'sell'; stp: 'dc' | 'co' | 'cn' | 'cb'; type: 'limit' | 'market' | 'stop'; created_at: string; post_only: boolean; fill_fees: string; filled_size: string; status: 'rejected' | 'received' | 'open' | 'done' | 'pending'; settled: boolean; executed_value: string; } export interface OrderResult extends BaseOrderInfo { time_in_force: 'GTC' | 'GTT' | 'IOC' | 'FOK'; status: 'rejected' | 'received' | 'open' | 'done'; } export interface OrderInfo extends BaseOrderInfo { status: 'received' | 'open' | 'done' | 'pending'; funds: number; specified_funds: number; done_at: string; } export type PageArgs = { before?: number; after?: number; limit?: number; }; export type FillFilter = { product_id?: string; order_id?: string; } & PageArgs; export type OrderFilter = { product_id?: string; status?: string; } & PageArgs; export type Account = { id: string, profile_id: string, currency: CurrencyType, balance: string, available: string, hold: string }; export type CoinbaseAccount = { id: string, name: string, balance: number, currency: CurrencyType, type: 'wallet' | 'fiat', primary: boolean, active: boolean }; export type CurrencyType = 'USD' | 'BTC' | 'LTC' | 'ETH' | 'BCH' | 'ETC'; export type CurrencyInfo = { id: CurrencyType, name: string, min_size: string }; export interface ProductInfo { id: string; base_currency: string; quote_currency: string; base_min_size: string; base_max_size: string; quote_increment: string; display_name: string; margin_enabled: boolean; } /** * If a PublicClient or AuthenticatedClient method that does an * HTTP request throws an error, then it will have this shape. */ export interface HttpError extends Error { response: HttpResponse; data?: any; } export interface ClientOptions { timeout?: number; } export class PublicClient { constructor(apiURI?: string, options?: ClientOptions); getProducts(callback: callback): void; getProducts(): Promise; getProductOrderBook(productID: string, options: any, callback: callback): void; getProductOrderBook(productID: string, options: any): Promise; getProductTicker(productID: string, callback: callback): void; getProductTicker(productID: string, ): Promise; getProductTrades(productID: string, callback: callback): void; getProductTrades(productID: string): Promise; getProductTrades(productID: string, pageArgs: PageArgs, callback: callback): void; getProductTrades(productID: string, pageArgs: PageArgs): Promise; getProductTradeStream(productID: string, tradesFrom: number, tradesTo: any): Readable; getProductHistoricRates(productID: string, args: any, callback: callback): void; getProductHistoricRates(productID: string, args: any): Promise; getProduct24HrStats(productID: string, callback: callback): void; getProduct24HrStats(productID: string): Promise; getCurrencies(callback: callback): void; getCurrencies(): Promise; getTime(callback: callback): void; getTime(): Promise; } export class AuthenticatedClient extends PublicClient { constructor(key: string, secret: string, passphrase: string, apiURI?: string, options?: ClientOptions); getCoinbaseAccounts(callback: callback): void getCoinbaseAccounts(): Promise; getAccounts(callback: callback): void; getAccounts(): Promise; getAccount(accountID: string, callback: callback): void; getAccount(accountID: string): Promise; getAccountHistory(accountID: string, callback: callback): void; getAccountHistory(accountID: string): Promise; getAccountHistory(accountID: string, pageArgs: PageArgs, callback: callback): void; getAccountHistory(accountID: string, pageArgs: PageArgs): Promise; getAccountTransfers(accountID: string, callback: callback): void; getAccountTransfers(accountID: string): Promise; getAccountTransfers(accountID: string, pageArgs: PageArgs, callback: callback): void getAccountTransfers(accountID: string, pageArgs: PageArgs): Promise; getAccountHolds(accountID: string, callback: callback): void; getAccountHolds(accountID: string): Promise; getAccountHolds(accountID: string, pageArgs: PageArgs, callback: callback): void; getAccountHolds(accountID: string, pageArgs: PageArgs): Promise; buy(params: OrderParams, callback: callback): void; buy(params: OrderParams): Promise; sell(params: OrderParams, callback: callback): void; sell(params: OrderParams): Promise; placeOrder(params: OrderParams, callback: callback): void; placeOrder(params: OrderParams): Promise; cancelOrder(orderID: string, callback: callback): void; cancelOrder(orderID: string): Promise; cancelAllOrders(args: { product_id?: string }, callback: callback): void; cancelAllOrders(args: { product_id?: string }): Promise; getOrders(callback: callback): void; getOrders(): Promise; getOrders(props: OrderFilter, callback: callback): void; getOrders(props: OrderFilter): Promise; getOrder(orderID: string, callback: callback): void; getOrder(orderID: string): Promise; getFills(callback: callback): void; getFills(): Promise; getFills(props: FillFilter, callback: callback): void; getFills(props: FillFilter): Promise; getFundings(params: any, callback: callback): void; getFundings(params: any): Promise; repay(params: any, callback: callback): void; repay(params: any): Promise; marginTransfer(params: any, callback: callback): void; marginTransfer(params: any): Promise; closePosition(params: any, callback: callback): void; closePosition(params: any): Promise; convert(params: any, callback: callback): void; convert(params: any): Promise; deposit(params: any, callback: callback): void; deposit(params: any): Promise; withdraw(params: any, callback: callback): void; withdraw(params: any): Promise; withdrawCrypto(params: any, callback: callback): void; withdrawCrypto(params: any): Promise; getTrailingVolume(callback: callback): void; getTrailingVolume(): Promise; } export namespace WebsocketMessage { export type Heartbeat = { type: 'heartbeat' sequence: number last_trade_id: number product_id: string time: string // ISO Date string without time zone } export type L2Snapshot = { type: 'snapshot' product_id: string bids: [string, string][] // strings are serialized fixed-point numbers asks: [string, string][] } export type L2Update = { type: 'l2update' product_id: string changes: [string, string, string][] // [side, price, new size] } export type Received = { type: 'received' time: string product_id: string sequence: number order_id: string side: 'buy' | 'sell' } & (ReceivedLimit | ReceivedMarket) export type ReceivedLimit = { order_type: 'limit' size: string price: string } export type ReceivedMarket = { order_type: 'market' funds: string } export type Match = { type: 'match' trade_id: number sequence: number maker_order_id: string taker_order_id: string time: string product_id: string size: string price: string side: 'buy' | 'sell' } // Add as necessary. There are still Opens, Dones, Changes, and some other things } export type WebsocketMessage = WebsocketMessage.Heartbeat | WebsocketMessage.L2Snapshot | WebsocketMessage.L2Update | WebsocketMessage.Received | WebsocketMessage.Match // Add as necessary. export interface WebsocketAuthentication { key: string, secret: string, passphrase: string } interface WebsocketClientOptions { channels?: string[]; } export class WebsocketClient extends EventEmitter { constructor( productIds: string[], websocketURI?: string, auth?: WebsocketAuthentication, { channels }?: WebsocketClientOptions ); on(event: 'message', eventHandler: (data: WebsocketMessage) => void): this; on(event: 'error', eventHandler: (err:any) => void): this; on(event: 'open', eventHandler: () => void): this; on(event: 'close', eventHandler: () => void): this; connect(): void; disconnect(): void; } }