@desk-exchange/typescript-sdk
Version:
Typescript SDK for DESK Exchange API
316 lines (306 loc) • 9.74 kB
TypeScript
import { ethers } from 'ethers';
import { AxiosInstance } from 'axios';
import { EventEmitter } from 'events';
declare const BASE_URLS: {
mainnet: string;
testnet: string;
};
type Network = keyof typeof BASE_URLS;
interface OpenOrder {
order_digest: string;
symbol: string;
side: "Long" | "Short";
price: string;
original_quantity: string;
remaining_quantity: string;
order_type: "Limit" | "StopMarket" | "TakeProfitMarket";
client_order_id: string | null;
time_in_force: "GTC" | "IOC";
created_at: number;
is_reduce_only: boolean;
is_conditional_order: boolean;
trigger_price: string | null;
is_trigger_above_threshold: boolean | null;
}
interface Collateral {
asset: string;
collateral_id: string;
amount: string;
}
interface Position {
symbol: string;
quantity: string;
avg_entry_price: string;
side: "Long" | "Short";
last_updated_funding_fee: string;
}
interface SubaccountSummary {
open_orders: OpenOrder[];
collaterals: Collateral[];
positions: Position[];
account_margin: string;
collateral_value: string;
unrealized_pnl: string;
pending_funding_fee: string;
pending_borrowing_fee: string;
account_imr: string;
order_imr: string;
position_imr: string;
position_mmr: string;
}
interface OrderRequest {
symbol: string;
amount: string;
price: string;
side: "Long" | "Short";
orderType: "Limit" | "Market" | "StopMarket" | "TakeProfitMarket";
reduceOnly: boolean;
timeInForce?: "GTC" | "IOC" | "FOK" | undefined;
waitForReply?: boolean;
}
interface OrderApiRequest {
symbol: string;
subaccount: string;
amount: string;
price: string;
side: "Long" | "Short";
nonce: string;
broker_id: string;
order_type: "Limit" | "Market" | "StopMarket" | "TakeProfitMarket";
reduce_only: boolean;
time_in_force: "GTC" | "IOC" | "FOK";
wait_for_reply?: boolean;
}
interface OrderApiResponse {
subaccount: string;
symbol: string;
side: "Long" | "Short";
price: string;
quantity: string;
nonce: string;
order_type: "Limit" | "Market" | "StopMarket" | "TakeProfitMarket";
time_in_force: "GTC" | "IOC" | "FOK";
order_digest: string;
filled_quantity: string;
avg_fill_price: string;
execution_fee: string;
client_order_id: string | null;
trigger_price: string | null;
}
interface CancelOrderApiRequest {
symbol: string;
subaccount: string;
nonce: string;
order_digest: string;
is_conditional_order: boolean;
wait_for_reply: boolean;
}
interface CancelOrderRequest {
symbol: string;
orderDigest: string;
waitForReply: boolean;
}
interface CancelledOrder {
order_digest: string;
status: "Success" | "Failed";
}
interface CancelOrderApiResponse {
orders: CancelledOrder[];
}
interface MarkPrice {
symbol: string;
markPrice: string;
indexPrice: string;
}
type OrderBookEntry = [string, string];
interface OrderBook {
bids: OrderBookEntry[];
asks: OrderBookEntry[];
}
interface MarketInfo {
id: number;
symbol: string;
name: string;
base_asset: string;
quote_asset: string;
imf: string;
mmf: string;
maker_fee: string;
taker_fee: string;
price_feed_id: number;
tick_size: string;
lot_size: string;
min_notional_size: string;
max_conditional_orders: number;
}
interface TokenAddress {
chain: string;
chain_id: number;
address: string;
}
interface CollateralInfo {
asset: string;
collateral_id: string;
token_addresses: TokenAddress[];
decimals: number;
collat_factor_bps: string;
borrow_factor_bps: string;
price_feed_id: number;
discount_rate_bps: string;
withdrawal_base_fee: string;
priority: number;
}
interface HistorialFundingRateData {
funding_rate: string;
apr: string;
avg_premium_index: string;
created_at: number;
}
type StreamMessage = {
type: string;
};
type TradeStreamMessage = StreamMessage & {
symbol: string;
data: {
price: string;
quantity: string;
side: string;
tradedAt: Date;
};
};
type CollateralPriceStreamMessage = StreamMessage & {
collateralId: string;
asset: string;
price: string;
};
declare enum OrderUpdateStatus {
Open = "Open",
Filled = "Filled",
Canceled = "Canceled",
Expired = "Expired",
NotFound = "NotFound"
}
declare enum OrderUpdateClientStatus {
PartialFilled = "PartialFilled"
}
type OrderUpdateStreamMessage = StreamMessage & {
subaccount: string;
data: {
symbol: string;
avgPrice: string;
originalQuantity: string;
filledQuantity: string;
side: string;
orderType: string;
status: OrderUpdateStatus;
orderUpdatedAt: Date;
clientOrderId: string;
orderDigest: string;
tif: string;
};
};
type PositionUpdateStreamMessage = StreamMessage & {
subaccount: string;
data: {
symbol: string;
quantity: string;
avgEntryPrice: string;
side: string;
};
};
declare class Auth {
readonly wallet: ethers.Wallet | undefined;
readonly subAccountId: number;
private authenticated;
readonly client: AxiosInstance;
readonly crmClient: AxiosInstance;
readonly network: Network;
readonly provider: ethers.Provider;
constructor(network: Network, privateKey: string | undefined, subAccountId: number);
generateNonce(): string;
generateJwt(): Promise<void>;
getSubaccount: () => string;
isAuthenticated(): boolean;
}
declare class Exchange {
private auth;
private parent;
constructor(auth: Auth, parent: DeskExchange);
depositCollateral(tokenAddress: string, amount: number): Promise<ethers.ContractTransactionReceipt>;
withdrawCollateral(tokenAddress: string, amount: number): Promise<ethers.ContractTransactionReceipt>;
getSubAccountSummary(): Promise<SubaccountSummary>;
placeOrder(request: OrderRequest): Promise<OrderApiResponse>;
batchPlaceOrder(requests: OrderRequest[]): Promise<OrderApiResponse[]>;
cancelOrder(request: CancelOrderRequest): Promise<CancelOrderApiResponse>;
batchCancelOrder(requests: CancelOrderRequest[]): Promise<CancelOrderApiResponse[]>;
}
declare class WebSocketClient extends EventEmitter {
private ws;
private url;
private pingInterval;
private reconnectAttempts;
private maxReconnectAttempts;
private initialReconnectDelay;
private maxReconnectDelay;
constructor(auth: Auth);
connect(): Promise<void>;
private reconnect;
private startPingInterval;
private stopPingInterval;
sendMessage(message: any): void;
close(): void;
removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
removeAllListeners(event?: string | symbol): this;
}
declare class WebSocketSubscriptions {
private ws;
private parent;
private subscriptions;
constructor(ws: WebSocketClient, parent: DeskExchange);
private getSubscriptionKey;
private addSubscription;
private subscribe;
private unsubscribe;
private removeSubscription;
subscribeToMarkPrices(callback: (data: MarkPrice[]) => void): Promise<void>;
unsubscribeFromMarkPrices(): Promise<void>;
subscribeToOrderbook(symbol: string, callback: (data: OrderBook) => void): Promise<void>;
unsubscribeFromOrderbook(symbol: string): Promise<void>;
subscribeToTrades(symbol: string, callback: (data: TradeStreamMessage) => void): Promise<void>;
unsubscribeFromTrades(symbol: string): Promise<void>;
subscribeToCollateralPrices(callback: (data: CollateralPriceStreamMessage) => void): Promise<void>;
unsubscribeFromCollateralPrices(symbol: string): Promise<void>;
subscribeToOrderUpdates(subaccount: string | undefined, callback: (data: OrderUpdateStreamMessage) => void): Promise<void>;
unsubscribeFromOrderUpdates(symbol: string): Promise<void>;
subscribeToPositionUpdates(subaccount: string | undefined, callback: (data: PositionUpdateStreamMessage) => void): Promise<void>;
unsubscribeFromPositionUpdates(symbol: string): Promise<void>;
}
declare class Info {
private auth;
private parent;
constructor(auth: Auth, parent: DeskExchange);
getMarketInfos(): Promise<MarketInfo[]>;
getCollateralInfos(): Promise<CollateralInfo[]>;
getHistoricalFundingRates(symbol: string, from: number, to: number): Promise<HistorialFundingRateData[]>;
getCurrentFundingRate(symbol: string): Promise<string>;
}
declare class DeskExchange {
readonly auth: Auth;
readonly exchange: Exchange;
readonly info: Info;
readonly wsClient: WebSocketClient;
readonly subscriptions: WebSocketSubscriptions;
readonly enableWs: boolean;
private initialized;
constructor(params: {
network: Network;
privateKey: string;
subAccountId: number;
enableWs: boolean;
});
private initialize;
ensureInitialized(): Promise<void>;
authenticate(): Promise<void>;
isInitialized(): boolean;
}
export { type CancelOrderApiRequest, type CancelOrderApiResponse, type CancelOrderRequest, type Collateral, type CollateralInfo, type CollateralPriceStreamMessage, DeskExchange, type HistorialFundingRateData, type MarkPrice, type MarketInfo, type Network, type OpenOrder, type OrderApiRequest, type OrderApiResponse, type OrderBook, type OrderRequest, OrderUpdateClientStatus, OrderUpdateStatus, type OrderUpdateStreamMessage, type Position, type PositionUpdateStreamMessage, type StreamMessage, type SubaccountSummary, type TokenAddress, type TradeStreamMessage };