@paradex/sdk 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,206 @@
1
+ import { type SessionOrder } from './session-auth.js';
2
+ import { type Session } from './session-connector.js';
3
+ import { type HttpRequest, type HttpTransport, type WebSocketFactory, type WebSocketLike } from './session-transport.js';
4
+ import { SessionGrantError, type SessionEnvironment } from './session-typed-data.js';
5
+ export type GrantState = 'active' | 'revoked' | 'expired';
6
+ export interface GrantStatus {
7
+ readonly grant_id: string;
8
+ readonly state: GrantState;
9
+ readonly expires_at?: number;
10
+ readonly revoked_at?: number | null;
11
+ readonly accounts?: readonly string[];
12
+ readonly scopes?: readonly string[];
13
+ readonly label?: string;
14
+ }
15
+ export interface SessionAccountsParams {
16
+ readonly session: Pick<Session, 'grant' | 'key' | 'grantId'>;
17
+ /**
18
+ * Optional: the grant carries its environment and chain. When given, a
19
+ * grant for anything else is refused.
20
+ */
21
+ readonly environment?: SessionEnvironment;
22
+ readonly paradexChain?: string;
23
+ readonly transport?: HttpTransport;
24
+ readonly apiUrl?: string;
25
+ readonly wsUrl?: string;
26
+ readonly webSocketFactory?: WebSocketFactory;
27
+ /** Refresh a JWT this many seconds before it expires. Defaults to 60. */
28
+ readonly refreshMarginSeconds?: number;
29
+ /** Websocket RPC timeout. Defaults to 10 s. */
30
+ readonly rpcTimeoutMs?: number;
31
+ /** Seconds since the epoch. */
32
+ readonly now?: () => number;
33
+ /**
34
+ * Called once when the grant is revoked or expires. The key record stays
35
+ * in storage until `connector.forget(session)` deletes it; do that here, or
36
+ * `restore()` keeps returning the ended session until it expires.
37
+ */
38
+ readonly onDropped?: (reason: SessionGrantError) => void;
39
+ }
40
+ export interface SessionRequest {
41
+ readonly method: HttpRequest['method'];
42
+ /** Path below the API root, e.g. `/v1/orders`. */
43
+ readonly path: string;
44
+ readonly body?: unknown;
45
+ }
46
+ export interface RevokeOptions {
47
+ /**
48
+ * Also cancel the session's resting orders, TWAPs and untriggered stops on
49
+ * every account. Defaults to `true`.
50
+ */
51
+ readonly cancelOrders?: boolean;
52
+ }
53
+ export type SubscriptionHandler = (data: unknown) => void;
54
+ /**
55
+ * Trades every account a grant lists with one session key: a JWT per account
56
+ * (`POST /v1/auth/{session_public_key}` with `PARADEX-STARKNET-ACCOUNT`,
57
+ * signed by the session key), one
58
+ * websocket per account, and requests routed by account. Sessions never use
59
+ * `on_behalf_of`; each account authenticates as itself.
60
+ *
61
+ * When the grant expires or is revoked, every account is dropped: tokens are
62
+ * discarded, sockets closed, and later calls throw. Whenever the server
63
+ * refuses a session token or the session key, the grant's state is re-checked
64
+ * with `GET /v2/sessions/{grant_id}/status`; any other answer is transient.
65
+ *
66
+ * @experimental
67
+ */
68
+ export declare class SessionAccounts {
69
+ readonly accounts: readonly string[];
70
+ private readonly params;
71
+ private readonly transport;
72
+ private readonly apiUrl;
73
+ private readonly wsUrl;
74
+ private readonly webSocketFactory;
75
+ private readonly refreshMargin;
76
+ private readonly now;
77
+ private readonly tokens;
78
+ private readonly inflight;
79
+ private readonly refreshTimers;
80
+ private readonly sockets;
81
+ private statusCheck;
82
+ private expiryTimer;
83
+ private dropped;
84
+ private closed;
85
+ constructor(params: SessionAccountsParams);
86
+ /** Why the accounts were dropped, if they were. */
87
+ get dropReason(): SessionGrantError | undefined;
88
+ /**
89
+ * Mints a JWT for every listed account, at most 10 at a time to stay
90
+ * inside the server's per-IP `/auth` rate limit.
91
+ */
92
+ start(): Promise<void>;
93
+ /** A valid JWT for `account`, minted or refreshed as needed. */
94
+ getJwt(account: string): Promise<string>;
95
+ /** Authenticated REST call as `account`. */
96
+ request(account: string, req: SessionRequest): Promise<unknown>;
97
+ /** Signs `order` with the session key for `account` and submits it. */
98
+ placeOrder(account: string, order: SessionOrder): Promise<unknown>;
99
+ cancelOrder(account: string, orderId: string): Promise<unknown>;
100
+ /** The grant's details (`GET /v2/sessions/{grant_id}`). */
101
+ status(): Promise<GrantStatus>;
102
+ /**
103
+ * Revokes the grant on the server (every account under it) and drops
104
+ * every account locally.
105
+ *
106
+ * With `cancelOrders` (the default) the server also cancels the session's
107
+ * resting orders, TWAPs and untriggered stops on every account. Once the
108
+ * grant is revoked this key can no longer cancel anything, so a bot or
109
+ * builder session cancels by default. The Paradex app's own sign-out does
110
+ * the opposite, because orders placed through it are the owner's own
111
+ * trading.
112
+ *
113
+ * With `cancelOrders: false`, a grant without a builder signs out: its
114
+ * resting orders, TWAPs and stops keep working. A builder grant still has
115
+ * its TWAPs and untriggered stops cancelled, and keeps only its resting
116
+ * orders.
117
+ *
118
+ * A 404 `NOT_FOUND` means the server no longer has the grant, so it counts
119
+ * as revoked and drops the session too. Any other refusal (a 403, a 404
120
+ * for an unrouted path, a 5xx) throws and leaves the session live, so the
121
+ * call can be retried.
122
+ */
123
+ revoke({ cancelOrders }?: RevokeOptions): Promise<void>;
124
+ /**
125
+ * Subscribes to a channel on `account`'s socket, opening and authenticating
126
+ * it if needed. Returns an unsubscribe function; the socket closes once its
127
+ * last subscription is gone.
128
+ */
129
+ subscribe(account: string, channel: string, handler: SubscriptionHandler): Promise<() => void>;
130
+ /** Closes sockets and timers. The session itself stays valid. */
131
+ close(): void;
132
+ /** @internal */
133
+ createWebSocket(): WebSocketLike;
134
+ /** @internal */
135
+ get isLive(): boolean;
136
+ /** @internal Drops every account. Idempotent. */
137
+ drop(reason: SessionGrantError): void;
138
+ /** @internal The cached JWT for `account`, if any. */
139
+ cachedToken(account: string): string | undefined;
140
+ /** @internal The socket for `account` has no subscriptions left. */
141
+ socketIdle(account: string, socket: AccountSocket): void;
142
+ /**
143
+ * @internal Called when the server refused a session token or the session
144
+ * key. Asks the server whether the grant is still active; drops every
145
+ * account and throws if it is not.
146
+ */
147
+ confirmActive(): Promise<void>;
148
+ /**
149
+ * `GET /v2/sessions/{grant_id}/status` needs no token, so it still answers
150
+ * once the session's tokens are refused. A 404 with `NOT_FOUND` means the
151
+ * grant is gone; any other 404 (e.g. an unrouted path) is transient.
152
+ */
153
+ private checkGrantState;
154
+ private grantPath;
155
+ private send;
156
+ private teardown;
157
+ private assertLive;
158
+ private resolveAccount;
159
+ /**
160
+ * A token needs refreshing within the margin before it expires. Tokens cut
161
+ * short by the grant's own expiry are kept to the end: re-minting cannot
162
+ * extend them, and the expiry timer drops everything at that point.
163
+ */
164
+ private needsRefresh;
165
+ private mint;
166
+ private doMint;
167
+ private clearRefresh;
168
+ /** @internal Keeps an account's socket authenticated across JWT expiry. */
169
+ scheduleRefresh(account: string): void;
170
+ private scheduleExpiry;
171
+ }
172
+ /** One authenticated websocket for one account. */
173
+ declare class AccountSocket {
174
+ private readonly owner;
175
+ private readonly account;
176
+ private readonly rpcTimeoutMs;
177
+ private ws;
178
+ private ready;
179
+ private readonly handlers;
180
+ /** Subscriptions sent on the current connection, settled or pending. */
181
+ private readonly subscribed;
182
+ private readonly pending;
183
+ private nextId;
184
+ private reconnectAttempts;
185
+ private reconnectTimer;
186
+ private closed;
187
+ /** The token this connection last authenticated with. */
188
+ private authedToken;
189
+ constructor(owner: SessionAccounts, account: string, rpcTimeoutMs?: number);
190
+ subscribe(channel: string, handler: SubscriptionHandler): Promise<() => void>;
191
+ reauthenticate(token: string): Promise<void>;
192
+ /** Drops the connection; the reconnect path takes over. */
193
+ restart(): void;
194
+ close(): void;
195
+ private removeHandler;
196
+ private ensureSubscribed;
197
+ private connect;
198
+ private open;
199
+ private call;
200
+ private onMessage;
201
+ private onClose;
202
+ private scheduleReconnect;
203
+ private resubscribe;
204
+ private rejectPending;
205
+ }
206
+ export {};