@orderly.network/hooks 0.0.5 → 0.0.7
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.
- package/dist/index.d.mts +32 -10
- package/dist/index.d.ts +32 -10
- package/dist/index.js +509 -427
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +478 -403
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -1,26 +1,29 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var React = require('react');
|
|
4
4
|
var useSWR = require('swr');
|
|
5
5
|
var net = require('@orderly.network/net');
|
|
6
6
|
var useSWRMutation = require('swr/mutation');
|
|
7
7
|
var core = require('@orderly.network/core');
|
|
8
8
|
var useConstant = require('use-constant');
|
|
9
9
|
var rxjsHooks = require('rxjs-hooks');
|
|
10
|
+
var types = require('@orderly.network/types');
|
|
10
11
|
var rxjs = require('rxjs');
|
|
11
12
|
var operators = require('rxjs/operators');
|
|
12
13
|
var ramda = require('ramda');
|
|
13
14
|
var utils = require('@orderly.network/utils');
|
|
14
|
-
var types = require('@orderly.network/types');
|
|
15
15
|
var formik = require('formik');
|
|
16
16
|
var futures = require('@orderly.network/futures');
|
|
17
|
+
var useSWRSubscription = require('swr/subscription');
|
|
17
18
|
var useSWRInfinite = require('swr/infinite');
|
|
18
19
|
|
|
19
20
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
20
21
|
|
|
22
|
+
var React__default = /*#__PURE__*/_interopDefault(React);
|
|
21
23
|
var useSWR__default = /*#__PURE__*/_interopDefault(useSWR);
|
|
22
24
|
var useSWRMutation__default = /*#__PURE__*/_interopDefault(useSWRMutation);
|
|
23
25
|
var useConstant__default = /*#__PURE__*/_interopDefault(useConstant);
|
|
26
|
+
var useSWRSubscription__default = /*#__PURE__*/_interopDefault(useSWRSubscription);
|
|
24
27
|
var useSWRInfinite__default = /*#__PURE__*/_interopDefault(useSWRInfinite);
|
|
25
28
|
|
|
26
29
|
var __defProp = Object.defineProperty;
|
|
@@ -78,7 +81,7 @@ var __async = (__this, __arguments, generator) => {
|
|
|
78
81
|
step((generator = generator.apply(__this, __arguments)).next());
|
|
79
82
|
});
|
|
80
83
|
};
|
|
81
|
-
var OrderlyContext =
|
|
84
|
+
var OrderlyContext = React.createContext({
|
|
82
85
|
// configStore: new MemoryConfigStore(),
|
|
83
86
|
});
|
|
84
87
|
var OrderlyProvider = OrderlyContext.Provider;
|
|
@@ -86,17 +89,18 @@ var fetcher = (url, init = {}, queryOptions) => net.get(url, init, queryOptions
|
|
|
86
89
|
|
|
87
90
|
// src/useQuery.ts
|
|
88
91
|
var useQuery = (query, options) => {
|
|
89
|
-
const { apiBaseUrl } =
|
|
92
|
+
const { apiBaseUrl } = React.useContext(OrderlyContext);
|
|
90
93
|
const _a = options || {}, { formatter } = _a, swrOptions = __objRest(_a, ["formatter"]);
|
|
91
|
-
if (!query.startsWith("/public")) {
|
|
94
|
+
if (typeof query === "string" && !query.startsWith("/public")) {
|
|
92
95
|
throw new Error("useQuery is only for public api");
|
|
93
96
|
}
|
|
94
97
|
if (typeof apiBaseUrl === "undefined") {
|
|
95
98
|
throw new Error("please add OrderlyProvider to your app");
|
|
96
99
|
}
|
|
97
100
|
return useSWR__default.default(
|
|
98
|
-
`${apiBaseUrl}${query}`,
|
|
99
|
-
|
|
101
|
+
// `${apiBaseUrl}${query}`,
|
|
102
|
+
query,
|
|
103
|
+
(url, init) => fetcher(`${apiBaseUrl}${url}`, init, { formatter }),
|
|
100
104
|
swrOptions
|
|
101
105
|
);
|
|
102
106
|
};
|
|
@@ -108,11 +112,12 @@ var fetcher2 = (url, options) => {
|
|
|
108
112
|
});
|
|
109
113
|
};
|
|
110
114
|
var useMutation = (url, options) => {
|
|
111
|
-
const { apiBaseUrl } =
|
|
115
|
+
const { apiBaseUrl } = React.useContext(OrderlyContext);
|
|
112
116
|
if (!url.startsWith("http")) {
|
|
113
117
|
url = `${apiBaseUrl}${url}`;
|
|
114
118
|
}
|
|
115
|
-
|
|
119
|
+
let account3 = core.SimpleDI.get("account");
|
|
120
|
+
const signer = account3.signer;
|
|
116
121
|
const { trigger, data, error, reset, isMutating } = useSWRMutation__default.default(
|
|
117
122
|
url,
|
|
118
123
|
fetcher2,
|
|
@@ -136,33 +141,84 @@ var useMutation = (url, options) => {
|
|
|
136
141
|
};
|
|
137
142
|
};
|
|
138
143
|
var signatureMiddleware = (useSWRNext) => {
|
|
144
|
+
const { apiBaseUrl } = React.useContext(OrderlyContext);
|
|
139
145
|
return (key, fetcher4, config) => {
|
|
140
|
-
|
|
141
|
-
const
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
146
|
+
try {
|
|
147
|
+
const extendedFetcher = (url) => __async(void 0, null, function* () {
|
|
148
|
+
let account3 = core.SimpleDI.get("account");
|
|
149
|
+
let fullUrl = `${apiBaseUrl}${url}`;
|
|
150
|
+
const signer = account3.signer;
|
|
151
|
+
const payload = {
|
|
152
|
+
method: "GET",
|
|
153
|
+
url: fullUrl
|
|
154
|
+
};
|
|
155
|
+
const signature = yield signer.sign(payload);
|
|
156
|
+
return fetcher4(fullUrl, {
|
|
157
|
+
headers: __spreadProps(__spreadValues({}, signature), {
|
|
158
|
+
"orderly-account-id": account3.accountId
|
|
159
|
+
// "orderly-account-id":
|
|
160
|
+
// "0x47ab075adca7dfe9dd206eb7c50a10f7b99f4f08fa6c3abd4c170d438e15093b",
|
|
161
|
+
})
|
|
162
|
+
});
|
|
151
163
|
});
|
|
152
|
-
|
|
153
|
-
|
|
164
|
+
return useSWRNext(key, extendedFetcher, config);
|
|
165
|
+
} catch (e) {
|
|
166
|
+
console.error("signature error:", e);
|
|
167
|
+
throw e;
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
var useAccount = () => {
|
|
172
|
+
const { configStore, keyStore, walletAdapter } = React.useContext(OrderlyContext);
|
|
173
|
+
if (!configStore)
|
|
174
|
+
throw new Error("configStore is not defined, please use OrderlyProvider");
|
|
175
|
+
if (!keyStore) {
|
|
176
|
+
throw new Error(
|
|
177
|
+
"keyStore is not defined, please use OrderlyProvider and provide keyStore"
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
const account3 = useConstant__default.default(() => {
|
|
181
|
+
let account4 = core.SimpleDI.get("account");
|
|
182
|
+
if (!account4) {
|
|
183
|
+
account4 = new core.Account(configStore, keyStore, walletAdapter);
|
|
184
|
+
core.SimpleDI.registerByName("account", account4);
|
|
185
|
+
}
|
|
186
|
+
return account4;
|
|
187
|
+
});
|
|
188
|
+
const state = rxjsHooks.useObservable(
|
|
189
|
+
() => account3.state$,
|
|
190
|
+
account3.stateValue
|
|
191
|
+
);
|
|
192
|
+
const login = React.useCallback(
|
|
193
|
+
(address) => {
|
|
194
|
+
account3.login(address);
|
|
195
|
+
},
|
|
196
|
+
[account3]
|
|
197
|
+
);
|
|
198
|
+
return {
|
|
199
|
+
// account: state!,
|
|
200
|
+
account: account3,
|
|
201
|
+
state,
|
|
202
|
+
// info: {},
|
|
203
|
+
login
|
|
154
204
|
};
|
|
155
205
|
};
|
|
156
206
|
var usePrivateQuery = (query, options) => {
|
|
157
207
|
var _b;
|
|
158
|
-
const { apiBaseUrl } = react.useContext(OrderlyContext);
|
|
159
208
|
const _a = options || {}, { formatter } = _a, swrOptions = __objRest(_a, ["formatter"]);
|
|
209
|
+
const account3 = useAccount();
|
|
160
210
|
const middleware = Array.isArray(options == null ? void 0 : options.use) ? (_b = options == null ? void 0 : options.use) != null ? _b : [] : [];
|
|
161
211
|
return useSWR__default.default(
|
|
162
|
-
|
|
163
|
-
|
|
212
|
+
() => account3.state.status >= types.AccountStatusEnum.SignedIn ? query : null,
|
|
213
|
+
// query,
|
|
214
|
+
(url, init) => {
|
|
215
|
+
return fetcher(url, init, { formatter });
|
|
216
|
+
},
|
|
164
217
|
__spreadProps(__spreadValues({}, swrOptions), {
|
|
165
|
-
use: [signatureMiddleware, ...middleware]
|
|
218
|
+
use: [signatureMiddleware, ...middleware],
|
|
219
|
+
onError: (err) => {
|
|
220
|
+
console.log("usePrivateQuery error", err);
|
|
221
|
+
}
|
|
166
222
|
})
|
|
167
223
|
);
|
|
168
224
|
};
|
|
@@ -172,8 +228,8 @@ var useTradingView = () => {
|
|
|
172
228
|
return {};
|
|
173
229
|
};
|
|
174
230
|
var usePrivateObserve = () => {
|
|
175
|
-
const [data, setData] =
|
|
176
|
-
|
|
231
|
+
const [data, setData] = React.useState();
|
|
232
|
+
React.useEffect(() => {
|
|
177
233
|
return () => {
|
|
178
234
|
};
|
|
179
235
|
}, []);
|
|
@@ -182,8 +238,8 @@ var usePrivateObserve = () => {
|
|
|
182
238
|
};
|
|
183
239
|
};
|
|
184
240
|
var useTopicObserve = (topic) => {
|
|
185
|
-
const [data, setData] =
|
|
186
|
-
|
|
241
|
+
const [data, setData] = React.useState();
|
|
242
|
+
React.useEffect(() => {
|
|
187
243
|
return () => {
|
|
188
244
|
};
|
|
189
245
|
}, []);
|
|
@@ -191,41 +247,106 @@ var useTopicObserve = (topic) => {
|
|
|
191
247
|
data
|
|
192
248
|
};
|
|
193
249
|
};
|
|
194
|
-
var
|
|
195
|
-
|
|
250
|
+
var AppState = class {
|
|
251
|
+
constructor() {
|
|
252
|
+
this.systemState$ = new rxjs.BehaviorSubject(types.SystemStateEnum.Loading);
|
|
253
|
+
this.exchangeState$ = new rxjs.BehaviorSubject(types.ExchangeStatusEnum.Normal);
|
|
254
|
+
this.prepare = ["symbolInfo", "clientInfo"];
|
|
255
|
+
}
|
|
256
|
+
updateState(name) {
|
|
257
|
+
this.prepare = this.prepare.filter((item) => item !== name);
|
|
258
|
+
if (this.prepare.length === 0) {
|
|
259
|
+
this.systemState$.next(types.SystemStateEnum.Ready);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
udpateSystemState(state) {
|
|
263
|
+
this.systemState$.next(state);
|
|
264
|
+
}
|
|
265
|
+
updateExchangeState(state) {
|
|
266
|
+
this.exchangeState$.next(state);
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
AppState.instanceName = "AppState";
|
|
270
|
+
core.SimpleDI.registerByName(AppState.instanceName, new AppState());
|
|
271
|
+
function getAppState() {
|
|
272
|
+
return core.SimpleDI.get(AppState.instanceName);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// src/useAppState.ts
|
|
276
|
+
var useAppState = () => {
|
|
277
|
+
const appState = getAppState();
|
|
278
|
+
return rxjsHooks.useObservable(
|
|
279
|
+
() => rxjs.merge(appState.exchangeState$, appState.systemState$).pipe(
|
|
280
|
+
operators.map((data) => {
|
|
281
|
+
return {
|
|
282
|
+
systemState: types.SystemStateEnum.Ready,
|
|
283
|
+
exchangeState: types.ExchangeStatusEnum.Normal
|
|
284
|
+
};
|
|
285
|
+
})
|
|
286
|
+
),
|
|
287
|
+
{
|
|
288
|
+
systemState: appState.systemState$.value,
|
|
289
|
+
exchangeState: appState.exchangeState$.value
|
|
290
|
+
}
|
|
291
|
+
);
|
|
292
|
+
};
|
|
293
|
+
var WS_NAME = "websocketClient";
|
|
294
|
+
var useWebSocketClient = () => {
|
|
295
|
+
const ws = useConstant__default.default(() => {
|
|
296
|
+
let websocketClient = core.SimpleDI.get(WS_NAME);
|
|
297
|
+
if (!websocketClient) {
|
|
298
|
+
websocketClient = new net.WebSocketClient({
|
|
299
|
+
accountId: "0x47ab075adca7dfe9dd206eb7c50a10f7b99f4f08fa6c3abd4c170d438e15093b",
|
|
300
|
+
networkId: "testnet",
|
|
301
|
+
onSigntureRequest: (accountId) => __async(void 0, null, function* () {
|
|
302
|
+
const signer = core.getMockSigner();
|
|
303
|
+
const timestamp = (/* @__PURE__ */ new Date()).getTime();
|
|
304
|
+
const result = yield signer.signText(timestamp.toString());
|
|
305
|
+
return __spreadProps(__spreadValues({}, result), { timestamp });
|
|
306
|
+
})
|
|
307
|
+
});
|
|
308
|
+
core.SimpleDI.registerByName(WS_NAME, websocketClient);
|
|
309
|
+
}
|
|
310
|
+
return websocketClient;
|
|
311
|
+
});
|
|
312
|
+
return ws;
|
|
313
|
+
};
|
|
314
|
+
var useAccountInstance = () => {
|
|
315
|
+
const { configStore, keyStore } = React.useContext(OrderlyContext);
|
|
196
316
|
if (!configStore)
|
|
197
317
|
throw new Error("configStore is not defined, please use OrderlyProvider");
|
|
318
|
+
if (!keyStore) {
|
|
319
|
+
throw new Error(
|
|
320
|
+
"keyStore is not defined, please use OrderlyProvider and provide keyStore"
|
|
321
|
+
);
|
|
322
|
+
}
|
|
198
323
|
const account3 = useConstant__default.default(() => {
|
|
199
324
|
let account4 = core.SimpleDI.get("account");
|
|
200
325
|
if (!account4) {
|
|
201
|
-
account4 = new core.Account(configStore);
|
|
326
|
+
account4 = new core.Account(configStore, keyStore);
|
|
202
327
|
core.SimpleDI.registerByName("account", account4);
|
|
203
328
|
}
|
|
204
329
|
return account4;
|
|
205
330
|
});
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
() => account3.state$,
|
|
209
|
-
account3.stateValue
|
|
210
|
-
);
|
|
211
|
-
const login = react.useCallback(
|
|
212
|
-
(address) => {
|
|
213
|
-
account3.login(address);
|
|
214
|
-
},
|
|
215
|
-
[account3]
|
|
216
|
-
);
|
|
217
|
-
return {
|
|
218
|
-
account: state,
|
|
219
|
-
info: accountInfo,
|
|
220
|
-
login
|
|
221
|
-
};
|
|
331
|
+
console.log("account instance ======>>>>>", account3);
|
|
332
|
+
return account3;
|
|
222
333
|
};
|
|
223
|
-
|
|
224
|
-
|
|
334
|
+
|
|
335
|
+
// src/provider/dataProvider.tsx
|
|
336
|
+
var DataSourceContext = React.createContext({});
|
|
337
|
+
var DataSourceProvider = (props) => {
|
|
338
|
+
console.log("render DataSourceProvider");
|
|
339
|
+
useAccountInstance();
|
|
340
|
+
useConstant__default.default(() => {
|
|
341
|
+
});
|
|
342
|
+
return /* @__PURE__ */ React__default.default.createElement(DataSourceContext.Provider, { value: {} }, props.children);
|
|
343
|
+
};
|
|
344
|
+
var WS_NAME2 = "nativeWebsocketClient";
|
|
345
|
+
var useWS = () => {
|
|
225
346
|
const ws = useConstant__default.default(() => {
|
|
226
|
-
let websocketClient = core.SimpleDI.get(
|
|
347
|
+
let websocketClient = core.SimpleDI.get(WS_NAME2);
|
|
227
348
|
if (!websocketClient) {
|
|
228
|
-
websocketClient = new net.
|
|
349
|
+
websocketClient = new net.WS({
|
|
229
350
|
accountId: "0x47ab075adca7dfe9dd206eb7c50a10f7b99f4f08fa6c3abd4c170d438e15093b",
|
|
230
351
|
networkId: "testnet",
|
|
231
352
|
onSigntureRequest: (accountId) => __async(void 0, null, function* () {
|
|
@@ -235,7 +356,7 @@ var useWebSocketClient = () => {
|
|
|
235
356
|
return __spreadProps(__spreadValues({}, result), { timestamp });
|
|
236
357
|
})
|
|
237
358
|
});
|
|
238
|
-
core.SimpleDI.registerByName(
|
|
359
|
+
core.SimpleDI.registerByName(WS_NAME2, websocketClient);
|
|
239
360
|
}
|
|
240
361
|
return websocketClient;
|
|
241
362
|
});
|
|
@@ -336,16 +457,16 @@ var useOrderbook = (symbol, initial = { asks: [], bids: [] }, options) => {
|
|
|
336
457
|
if (!symbol) {
|
|
337
458
|
throw new Error("useOrderbook requires a symbol");
|
|
338
459
|
}
|
|
339
|
-
const [data, setData] =
|
|
340
|
-
const [depth, setDepth] =
|
|
341
|
-
const [level, setLevel] =
|
|
460
|
+
const [data, setData] = React.useState(initial);
|
|
461
|
+
const [depth, setDepth] = React.useState(1e-3);
|
|
462
|
+
const [level, setLevel] = React.useState(() => {
|
|
342
463
|
var _a;
|
|
343
464
|
return (_a = options == null ? void 0 : options.level) != null ? _a : 10;
|
|
344
465
|
});
|
|
345
466
|
const ws = useWebSocketClient();
|
|
346
|
-
const orderbookSubscriberRef =
|
|
467
|
+
const orderbookSubscriberRef = React.useRef();
|
|
347
468
|
const ticker = useTickerStream(symbol);
|
|
348
|
-
const orderbookRequest$ =
|
|
469
|
+
const orderbookRequest$ = React.useMemo(() => {
|
|
349
470
|
return ws.observe(
|
|
350
471
|
{
|
|
351
472
|
event: "request",
|
|
@@ -358,7 +479,7 @@ var useOrderbook = (symbol, initial = { asks: [], bids: [] }, options) => {
|
|
|
358
479
|
(message) => message.event === "request"
|
|
359
480
|
);
|
|
360
481
|
}, [symbol]);
|
|
361
|
-
const orderbookUpdate$ =
|
|
482
|
+
const orderbookUpdate$ = React.useMemo(() => {
|
|
362
483
|
return ws.observe(`${symbol}@orderbookupdate`, () => ({
|
|
363
484
|
event: "subscribe",
|
|
364
485
|
topic: `${symbol}@orderbookupdate`
|
|
@@ -383,7 +504,7 @@ var useOrderbook = (symbol, initial = { asks: [], bids: [] }, options) => {
|
|
|
383
504
|
0,
|
|
384
505
|
[symbol]
|
|
385
506
|
);
|
|
386
|
-
|
|
507
|
+
React.useEffect(() => {
|
|
387
508
|
if (orderbookSubscriberRef.current) {
|
|
388
509
|
orderbookSubscriberRef.current.unsubscribe();
|
|
389
510
|
}
|
|
@@ -403,14 +524,14 @@ var useOrderbook = (symbol, initial = { asks: [], bids: [] }, options) => {
|
|
|
403
524
|
setData(data2);
|
|
404
525
|
});
|
|
405
526
|
}, [orderbookRequest$, orderbookUpdate$]);
|
|
406
|
-
const onDepthChange =
|
|
527
|
+
const onDepthChange = React.useCallback((depth2) => {
|
|
407
528
|
console.log("Orderbook depth has changed:", depth2);
|
|
408
529
|
orderbookOptions$.next(__spreadProps(__spreadValues({}, orderbookOptions$.value), {
|
|
409
530
|
depth: depth2
|
|
410
531
|
// level,
|
|
411
532
|
}));
|
|
412
533
|
}, []);
|
|
413
|
-
const middlePrice =
|
|
534
|
+
const middlePrice = React.useMemo(() => {
|
|
414
535
|
let asksFrist = 0, bidsFirst = 0;
|
|
415
536
|
if (data.asks.length > 0) {
|
|
416
537
|
asksFrist = data.asks[data.bids.length - 1][0];
|
|
@@ -662,77 +783,107 @@ var useMarkPricesSubject = () => {
|
|
|
662
783
|
)
|
|
663
784
|
);
|
|
664
785
|
};
|
|
665
|
-
|
|
666
|
-
|
|
786
|
+
var useMarkPricesStream = () => {
|
|
787
|
+
const ws = useWS();
|
|
788
|
+
return useSWRSubscription__default.default("markPrices", (key, { next }) => {
|
|
789
|
+
const unsubscribe = ws.subscription(
|
|
790
|
+
{ event: "subscribe", topic: "markprices" },
|
|
791
|
+
{
|
|
792
|
+
onMessage: (message) => {
|
|
793
|
+
const data = /* @__PURE__ */ Object.create(null);
|
|
794
|
+
for (let index = 0; index < message.length; index++) {
|
|
795
|
+
const element = message[index];
|
|
796
|
+
data[element.symbol] = element.price;
|
|
797
|
+
}
|
|
798
|
+
next(null, data);
|
|
799
|
+
},
|
|
800
|
+
onUnsubscribe: () => {
|
|
801
|
+
return "markprices";
|
|
802
|
+
},
|
|
803
|
+
onError: (error) => {
|
|
804
|
+
console.log("error", error);
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
);
|
|
808
|
+
return () => {
|
|
809
|
+
console.log("unsubscribe!!!!!!!");
|
|
810
|
+
console.log("unsubscribe", unsubscribe);
|
|
811
|
+
unsubscribe == null ? void 0 : unsubscribe();
|
|
812
|
+
};
|
|
813
|
+
});
|
|
814
|
+
};
|
|
667
815
|
var usePositionStream = (symbol, options) => {
|
|
668
|
-
|
|
816
|
+
React.useState(false);
|
|
817
|
+
const [visibledSymbol, setVisibleSymbol] = React.useState(
|
|
669
818
|
symbol
|
|
670
819
|
);
|
|
671
820
|
const symbolInfo = useSymbolsInfo();
|
|
672
|
-
const {
|
|
821
|
+
const { data: accountInfo } = usePrivateQuery("/client/info");
|
|
822
|
+
const { mutation } = useMutation("/order");
|
|
673
823
|
const fundingRates = useFundingRates();
|
|
674
|
-
|
|
824
|
+
useMarkPricesSubject();
|
|
675
825
|
const { data, error, isLoading } = usePrivateQuery(
|
|
676
826
|
`/positions`,
|
|
677
827
|
__spreadProps(__spreadValues({}, options), {
|
|
678
|
-
formatter: (data2) => data2
|
|
828
|
+
formatter: (data2) => data2,
|
|
829
|
+
onError: (err) => {
|
|
830
|
+
console.log("usePositionStream error", err);
|
|
831
|
+
}
|
|
679
832
|
})
|
|
680
833
|
);
|
|
681
|
-
const
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
})
|
|
720
|
-
})
|
|
721
|
-
)
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
);
|
|
725
|
-
const aggregatedData = react.useMemo(() => {
|
|
834
|
+
const { data: markPrices } = useMarkPricesStream();
|
|
835
|
+
const formatedPositions = React.useMemo(() => {
|
|
836
|
+
if (!(data == null ? void 0 : data.rows) || !symbolInfo || !accountInfo)
|
|
837
|
+
return null;
|
|
838
|
+
let totalCollateral = 0;
|
|
839
|
+
return data.rows.map((item) => {
|
|
840
|
+
const price = ramda.propOr(
|
|
841
|
+
item.mark_price,
|
|
842
|
+
item.symbol,
|
|
843
|
+
markPrices
|
|
844
|
+
);
|
|
845
|
+
const info = symbolInfo == null ? void 0 : symbolInfo[item.symbol];
|
|
846
|
+
const MMR = futures.positions.MMR({
|
|
847
|
+
baseMMR: info("base_mmr"),
|
|
848
|
+
baseIMR: info("base_imr"),
|
|
849
|
+
IMRFactor: accountInfo.imr_factor[info("base")],
|
|
850
|
+
positionNotional: futures.positions.notional(
|
|
851
|
+
item.position_qty,
|
|
852
|
+
price
|
|
853
|
+
),
|
|
854
|
+
IMR_factor_power: 4 / 5
|
|
855
|
+
});
|
|
856
|
+
return __spreadProps(__spreadValues({}, item), {
|
|
857
|
+
mark_price: price,
|
|
858
|
+
est_liq_price: futures.positions.liqPrice({
|
|
859
|
+
markPrice: price,
|
|
860
|
+
totalCollateral,
|
|
861
|
+
positionQty: item.position_qty,
|
|
862
|
+
MMR
|
|
863
|
+
}),
|
|
864
|
+
notional: futures.positions.notional(
|
|
865
|
+
item.position_qty,
|
|
866
|
+
item.average_open_price
|
|
867
|
+
),
|
|
868
|
+
unrealized_pnl: futures.positions.unrealizedPnL({
|
|
869
|
+
qty: item.position_qty,
|
|
870
|
+
openPrice: item.average_open_price,
|
|
871
|
+
markPrice: price
|
|
872
|
+
})
|
|
873
|
+
});
|
|
874
|
+
});
|
|
875
|
+
}, [data == null ? void 0 : data.rows, symbolInfo, accountInfo, markPrices]);
|
|
876
|
+
const aggregatedData = React.useMemo(() => {
|
|
726
877
|
const aggregatedData2 = {
|
|
727
878
|
unsettledPnL: NaN,
|
|
728
879
|
unrealPnL: NaN,
|
|
729
880
|
notional: NaN
|
|
730
881
|
};
|
|
731
|
-
if (
|
|
732
|
-
aggregatedData2.unrealPnL = futures.positions.totalUnrealizedPnL(
|
|
733
|
-
aggregatedData2.notional = futures.positions.totalNotional(
|
|
882
|
+
if (formatedPositions && formatedPositions.length) {
|
|
883
|
+
aggregatedData2.unrealPnL = futures.positions.totalUnrealizedPnL(formatedPositions);
|
|
884
|
+
aggregatedData2.notional = futures.positions.totalNotional(formatedPositions);
|
|
734
885
|
aggregatedData2.unsettledPnL = futures.positions.totalUnsettlementPnL(
|
|
735
|
-
|
|
886
|
+
formatedPositions.map((item) => {
|
|
736
887
|
var _a;
|
|
737
888
|
return __spreadProps(__spreadValues({}, item), {
|
|
738
889
|
sum_unitary_funding: (_a = fundingRates[item.symbol]) == null ? void 0 : _a.call(
|
|
@@ -745,16 +896,22 @@ var usePositionStream = (symbol, options) => {
|
|
|
745
896
|
);
|
|
746
897
|
}
|
|
747
898
|
return aggregatedData2;
|
|
748
|
-
}, [
|
|
749
|
-
const showSymbol =
|
|
899
|
+
}, [formatedPositions, fundingRates]);
|
|
900
|
+
const showSymbol = React.useCallback((symbol2) => {
|
|
750
901
|
setVisibleSymbol(symbol2);
|
|
751
902
|
}, []);
|
|
903
|
+
const onClosePosition = React.useCallback(
|
|
904
|
+
(order2) => {
|
|
905
|
+
return mutation(order2).finally(() => {
|
|
906
|
+
});
|
|
907
|
+
},
|
|
908
|
+
[]
|
|
909
|
+
);
|
|
752
910
|
return [
|
|
753
|
-
{ rows:
|
|
911
|
+
{ rows: formatedPositions, aggregated: aggregatedData },
|
|
754
912
|
createGetter(data, 1),
|
|
755
913
|
{
|
|
756
|
-
close:
|
|
757
|
-
},
|
|
914
|
+
close: onClosePosition,
|
|
758
915
|
loading: false,
|
|
759
916
|
showSymbol,
|
|
760
917
|
error,
|
|
@@ -767,320 +924,155 @@ var usePositionStream = (symbol, options) => {
|
|
|
767
924
|
}
|
|
768
925
|
];
|
|
769
926
|
};
|
|
770
|
-
var fetcher3 = (url, init) => net.get(url, init);
|
|
771
|
-
var usePrivateInfiniteQuery = (getKey, options) => {
|
|
772
|
-
var _a;
|
|
773
|
-
const middleware = Array.isArray(options == null ? void 0 : options.use) ? (_a = options == null ? void 0 : options.use) != null ? _a : [] : [];
|
|
774
|
-
const { apiBaseUrl } = react.useContext(OrderlyContext);
|
|
775
|
-
const result = useSWRInfinite__default.default(
|
|
776
|
-
(index, prevData) => `${apiBaseUrl}${getKey(index, prevData)}`,
|
|
777
|
-
fetcher3,
|
|
778
|
-
__spreadProps(__spreadValues({}, options), {
|
|
779
|
-
use: [signatureMiddleware, ...middleware]
|
|
780
|
-
})
|
|
781
|
-
);
|
|
782
|
-
return result;
|
|
783
|
-
};
|
|
784
|
-
var OrderStatus = /* @__PURE__ */ ((OrderStatus2) => {
|
|
785
|
-
OrderStatus2["FILLED"] = "FILLED";
|
|
786
|
-
OrderStatus2["PARTIAL_FILLED"] = "PARTIAL_FILLED";
|
|
787
|
-
OrderStatus2["CANCELED"] = "CANCELED";
|
|
788
|
-
OrderStatus2["NEW"] = "NEW";
|
|
789
|
-
OrderStatus2["COMPLETED"] = "COMPLETED";
|
|
790
|
-
return OrderStatus2;
|
|
791
|
-
})(OrderStatus || {});
|
|
792
|
-
var useOrderStream = ({
|
|
793
|
-
status = "NEW" /* NEW */,
|
|
794
|
-
symbol
|
|
795
|
-
} = {}) => {
|
|
796
|
-
var _a;
|
|
797
|
-
const markPrices$ = useMarkPricesSubject();
|
|
798
|
-
const res = usePrivateInfiniteQuery(
|
|
799
|
-
(pageIndex, previousPageData) => {
|
|
800
|
-
const search = new URLSearchParams([
|
|
801
|
-
["size", "100"],
|
|
802
|
-
["page", `${pageIndex + 1}`],
|
|
803
|
-
[`status`, status]
|
|
804
|
-
]);
|
|
805
|
-
if (symbol) {
|
|
806
|
-
search.set(`symbol`, symbol);
|
|
807
|
-
}
|
|
808
|
-
return `/orders?${search.toString()}`;
|
|
809
|
-
},
|
|
810
|
-
{
|
|
811
|
-
initialSize: 1,
|
|
812
|
-
onError: (err) => {
|
|
813
|
-
console.error("fetch failed::::", err);
|
|
814
|
-
}
|
|
815
|
-
}
|
|
816
|
-
);
|
|
817
|
-
const orders = rxjsHooks.useObservable(
|
|
818
|
-
(_, input$) => input$.pipe(
|
|
819
|
-
operators.map(([data]) => {
|
|
820
|
-
return data.flat();
|
|
821
|
-
}),
|
|
822
|
-
operators.combineLatestWith(markPrices$),
|
|
823
|
-
operators.map(([data, markPrices]) => {
|
|
824
|
-
return data.map((item) => {
|
|
825
|
-
var _a2;
|
|
826
|
-
return __spreadProps(__spreadValues({}, item), {
|
|
827
|
-
mark_price: (_a2 = markPrices[item.symbol]) != null ? _a2 : 0
|
|
828
|
-
});
|
|
829
|
-
});
|
|
830
|
-
})
|
|
831
|
-
),
|
|
832
|
-
null,
|
|
833
|
-
[(_a = res.data) != null ? _a : []]
|
|
834
|
-
);
|
|
835
|
-
const cancelAllOrders = react.useCallback(() => {
|
|
836
|
-
}, [res.data]);
|
|
837
|
-
const updateOrder = react.useCallback((id, data) => {
|
|
838
|
-
}, []);
|
|
839
|
-
const cancelOrder = react.useCallback((id) => {
|
|
840
|
-
}, []);
|
|
841
|
-
return [
|
|
842
|
-
orders,
|
|
843
|
-
// {
|
|
844
|
-
// ...res,
|
|
845
|
-
// data: res.data?.reduce((acc, cur) => {
|
|
846
|
-
// return [...acc, ...cur];
|
|
847
|
-
// }, []),
|
|
848
|
-
// },
|
|
849
|
-
{
|
|
850
|
-
cancelAllOrders,
|
|
851
|
-
updateOrder,
|
|
852
|
-
cancelOrder
|
|
853
|
-
}
|
|
854
|
-
];
|
|
855
|
-
};
|
|
856
|
-
var useMarkPriceStream = () => {
|
|
857
|
-
const markPrice$ = useMarkPricesSubject();
|
|
858
|
-
return rxjsHooks.useObservable(() => markPrice$, {});
|
|
859
|
-
};
|
|
860
|
-
|
|
861
|
-
// src/orderly/useCollateral.ts
|
|
862
927
|
var totalUnsettlementPnLPath = ramda.pathOr(0, [0, "aggregated", "unsettledPnL"]);
|
|
863
928
|
var positionsPath = ramda.pathOr([], [0, "rows"]);
|
|
864
929
|
var useCollateral = (dp = 6) => {
|
|
865
930
|
const positions2 = usePositionStream();
|
|
866
|
-
const orders =
|
|
867
|
-
const {
|
|
931
|
+
const { data: orders } = usePrivateQuery(`/orders`);
|
|
932
|
+
const { data: accountInfo } = usePrivateQuery("/client/info");
|
|
868
933
|
const symbolInfo = useSymbolsInfo();
|
|
869
|
-
const markPrices =
|
|
870
|
-
const { data } = usePrivateQuery("/client/holding", {
|
|
871
|
-
formatter: (
|
|
872
|
-
return
|
|
934
|
+
const { data: markPrices } = useMarkPricesStream();
|
|
935
|
+
const { data: holding } = usePrivateQuery("/client/holding", {
|
|
936
|
+
formatter: (data) => {
|
|
937
|
+
return data.holding;
|
|
873
938
|
}
|
|
874
939
|
});
|
|
875
|
-
const totalCollateral =
|
|
876
|
-
(
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
});
|
|
893
|
-
}
|
|
894
|
-
});
|
|
895
|
-
const number = futures.account.totalCollateral({
|
|
896
|
-
USDCHolding: USDC_holding,
|
|
897
|
-
nonUSDCHolding: nonUSDC,
|
|
898
|
-
unsettlementPnL: unsettlemnedPnL
|
|
899
|
-
});
|
|
900
|
-
return new utils.Decimal(number);
|
|
901
|
-
}
|
|
902
|
-
)
|
|
903
|
-
),
|
|
904
|
-
utils.zero,
|
|
905
|
-
[data, totalUnsettlementPnLPath(positions2), markPrices]
|
|
906
|
-
);
|
|
907
|
-
const totalInitialMarginWithOrders = rxjsHooks.useObservable(
|
|
908
|
-
(_, input$) => input$.pipe(
|
|
909
|
-
operators.filter((data2) => !!data2[3] && !!data2[4]),
|
|
910
|
-
operators.map(([positions3, orders2, markPrices2, accountInfo2, symbolInfo2]) => {
|
|
911
|
-
var _a;
|
|
912
|
-
return futures.account.totalInitialMarginWithOrders({
|
|
913
|
-
positions: positionsPath(positions3),
|
|
914
|
-
orders: (_a = orders2 == null ? void 0 : orders2[0]) != null ? _a : [],
|
|
915
|
-
markPrices: markPrices2,
|
|
916
|
-
IMR_Factors: accountInfo2.imr_factor,
|
|
917
|
-
maxLeverage: accountInfo2.max_leverage,
|
|
918
|
-
symbolInfo: symbolInfo2
|
|
940
|
+
const [totalCollateral, totalValue] = React.useMemo(() => {
|
|
941
|
+
if (!holding || !markPrices) {
|
|
942
|
+
return [utils.zero, utils.zero];
|
|
943
|
+
}
|
|
944
|
+
const unsettlemnedPnL = totalUnsettlementPnLPath(positions2);
|
|
945
|
+
const nonUSDC = [];
|
|
946
|
+
let USDC_holding = 0;
|
|
947
|
+
holding.forEach((item) => {
|
|
948
|
+
var _a;
|
|
949
|
+
if (item.token === "USDC") {
|
|
950
|
+
USDC_holding = item.holding;
|
|
951
|
+
} else {
|
|
952
|
+
nonUSDC.push({
|
|
953
|
+
holding: item.holding,
|
|
954
|
+
markPrice: (_a = markPrices[item.token]) != null ? _a : 0,
|
|
955
|
+
// markPrice: 0,
|
|
956
|
+
discount: 0
|
|
919
957
|
});
|
|
920
|
-
}
|
|
921
|
-
)
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
958
|
+
}
|
|
959
|
+
});
|
|
960
|
+
const number = futures.account.totalCollateral({
|
|
961
|
+
USDCHolding: USDC_holding,
|
|
962
|
+
nonUSDCHolding: nonUSDC,
|
|
963
|
+
unsettlementPnL: unsettlemnedPnL
|
|
964
|
+
});
|
|
965
|
+
const totalValue2 = futures.account.totalValue({
|
|
966
|
+
totalUnsettlementPnL: unsettlemnedPnL,
|
|
967
|
+
USDCHolding: USDC_holding,
|
|
968
|
+
nonUSDCHolding: nonUSDC
|
|
969
|
+
});
|
|
970
|
+
return [new utils.Decimal(number), totalValue2];
|
|
971
|
+
}, [holding, positions2, markPrices]);
|
|
972
|
+
const totalInitialMarginWithOrders = React.useMemo(() => {
|
|
973
|
+
if (!accountInfo || !symbolInfo || !markPrices) {
|
|
974
|
+
return 0;
|
|
975
|
+
}
|
|
976
|
+
return futures.account.totalInitialMarginWithOrders({
|
|
977
|
+
positions: positionsPath(positions2),
|
|
978
|
+
orders: orders != null ? orders : [],
|
|
979
|
+
markPrices,
|
|
980
|
+
IMR_Factors: accountInfo.imr_factor,
|
|
981
|
+
maxLeverage: accountInfo.max_leverage,
|
|
982
|
+
symbolInfo
|
|
983
|
+
});
|
|
984
|
+
}, [positions2, orders, markPrices, accountInfo, symbolInfo]);
|
|
925
985
|
return {
|
|
926
986
|
totalCollateral: totalCollateral.toDecimalPlaces(dp).toNumber(),
|
|
927
987
|
freeCollateral: futures.account.freeCollateral({
|
|
928
988
|
totalCollateral,
|
|
929
989
|
totalInitialMarginWithOrders
|
|
930
990
|
}).toDecimalPlaces(dp).toNumber(),
|
|
931
|
-
totalValue:
|
|
991
|
+
totalValue: totalValue.toDecimalPlaces(dp).toNumber()
|
|
932
992
|
};
|
|
933
993
|
};
|
|
934
994
|
var positionsPath2 = ramda.pathOr([], [0, "rows"]);
|
|
935
995
|
var useMaxQty = (symbol, side, reduceOnly = false) => {
|
|
936
|
-
|
|
937
|
-
const
|
|
938
|
-
const
|
|
939
|
-
const { info: accountInfo } = useAccount();
|
|
996
|
+
const positionsData = usePositionStream();
|
|
997
|
+
const { data: orders } = usePrivateQuery(`/orders`);
|
|
998
|
+
const { data: accountInfo } = usePrivateQuery("/client/info");
|
|
940
999
|
const symbolInfo = useSymbolsInfo();
|
|
941
1000
|
const { totalCollateral } = useCollateral();
|
|
942
|
-
const markPrices =
|
|
943
|
-
const maxQty =
|
|
944
|
-
(
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
side: side2
|
|
956
|
-
}
|
|
957
|
-
]) => {
|
|
958
|
-
const positionQty = futures.account.getQtyFromPositions(
|
|
959
|
-
positions3,
|
|
960
|
-
symbol2
|
|
961
|
-
);
|
|
962
|
-
if (positionQty > 0) {
|
|
963
|
-
if (side2 === types.OrderSide.BUY) {
|
|
964
|
-
return 0;
|
|
965
|
-
} else {
|
|
966
|
-
return Math.abs(positionQty);
|
|
967
|
-
}
|
|
968
|
-
}
|
|
969
|
-
if (positionQty < 0) {
|
|
970
|
-
if (side2 === types.OrderSide.BUY) {
|
|
971
|
-
return Math.abs(positionQty);
|
|
972
|
-
} else {
|
|
973
|
-
return 0;
|
|
974
|
-
}
|
|
975
|
-
}
|
|
976
|
-
return 0;
|
|
977
|
-
}
|
|
978
|
-
)
|
|
979
|
-
),
|
|
980
|
-
input$.pipe(
|
|
981
|
-
operators.filter(
|
|
982
|
-
([
|
|
983
|
-
{
|
|
984
|
-
markPrices: markPrices2,
|
|
985
|
-
positions: positions3,
|
|
986
|
-
orders: orders2,
|
|
987
|
-
accountInfo: accountInfo2,
|
|
988
|
-
symbolInfo: symbolInfo2,
|
|
989
|
-
symbol: symbol2,
|
|
990
|
-
side: side2,
|
|
991
|
-
totalCollateral: totalCollateral2
|
|
992
|
-
}
|
|
993
|
-
]) => !!symbol2 && !!side2 && !!markPrices2[symbol2] && !!positions3 && !!orders2 && !!accountInfo2 && !!symbolInfo2 && !!totalCollateral2
|
|
994
|
-
),
|
|
995
|
-
// 数据准备
|
|
996
|
-
operators.map(
|
|
997
|
-
([
|
|
998
|
-
{
|
|
999
|
-
markPrices: markPrices2,
|
|
1000
|
-
positions: positions3,
|
|
1001
|
-
orders: orders2,
|
|
1002
|
-
accountInfo: accountInfo2,
|
|
1003
|
-
symbolInfo: symbolInfo2,
|
|
1004
|
-
symbol: symbol2,
|
|
1005
|
-
side: side2,
|
|
1006
|
-
totalCollateral: totalCollateral2
|
|
1007
|
-
}
|
|
1008
|
-
]) => {
|
|
1009
|
-
const getSymbolInfo = symbolInfo2[symbol2];
|
|
1010
|
-
const positionQty = futures.account.getQtyFromPositions(
|
|
1011
|
-
positions3,
|
|
1012
|
-
symbol2
|
|
1013
|
-
);
|
|
1014
|
-
const buyOrdersQty = futures.account.getQtyFromOrdersBySide(
|
|
1015
|
-
orders2,
|
|
1016
|
-
symbol2,
|
|
1017
|
-
types.OrderSide.BUY
|
|
1018
|
-
);
|
|
1019
|
-
const sellOrdersQty = futures.account.getQtyFromOrdersBySide(
|
|
1020
|
-
orders2,
|
|
1021
|
-
symbol2,
|
|
1022
|
-
types.OrderSide.SELL
|
|
1023
|
-
);
|
|
1024
|
-
const otherPositions = positions3.filter(
|
|
1025
|
-
(item) => item.symbol !== symbol2
|
|
1026
|
-
);
|
|
1027
|
-
const otherOrders = orders2.filter(
|
|
1028
|
-
(item) => item.symbol !== symbol2
|
|
1029
|
-
);
|
|
1030
|
-
const otherIMs = futures.account.otherIMs({
|
|
1031
|
-
orders: otherOrders,
|
|
1032
|
-
positions: otherPositions,
|
|
1033
|
-
symbolInfo: symbolInfo2,
|
|
1034
|
-
markPrices: markPrices2,
|
|
1035
|
-
IMR_Factors: accountInfo2.imr_factor,
|
|
1036
|
-
maxLeverage: accountInfo2.max_leverage
|
|
1037
|
-
});
|
|
1038
|
-
return [
|
|
1039
|
-
side2,
|
|
1040
|
-
{
|
|
1041
|
-
markPrice: markPrices2[symbol2],
|
|
1042
|
-
baseMaxQty: getSymbolInfo("base_max"),
|
|
1043
|
-
totalCollateral: totalCollateral2,
|
|
1044
|
-
maxLeverage: accountInfo2.max_leverage,
|
|
1045
|
-
takerFeeRate: accountInfo2.taker_fee_rate,
|
|
1046
|
-
baseIMR: getSymbolInfo("base_imr"),
|
|
1047
|
-
otherIMs,
|
|
1048
|
-
positionQty,
|
|
1049
|
-
buyOrdersQty,
|
|
1050
|
-
sellOrdersQty,
|
|
1051
|
-
IMR_Factor: accountInfo2.imr_factor[getSymbolInfo("base")]
|
|
1052
|
-
},
|
|
1053
|
-
{
|
|
1054
|
-
dp: getSymbolInfo("base_tick")
|
|
1055
|
-
}
|
|
1056
|
-
];
|
|
1057
|
-
}
|
|
1058
|
-
),
|
|
1059
|
-
operators.map(([side2, inputs, options]) => {
|
|
1060
|
-
const maxQty2 = futures.account.maxQty(side2, inputs);
|
|
1061
|
-
return maxQty2;
|
|
1062
|
-
}),
|
|
1063
|
-
operators.distinct()
|
|
1064
|
-
// tap((data: number) => console.log("************", data))
|
|
1065
|
-
)
|
|
1066
|
-
)
|
|
1067
|
-
)
|
|
1068
|
-
),
|
|
1069
|
-
0,
|
|
1070
|
-
[
|
|
1071
|
-
{
|
|
1072
|
-
markPrices,
|
|
1073
|
-
positions: positionsPath2(positions2),
|
|
1074
|
-
orders: (_a = orders[0]) != null ? _a : [],
|
|
1075
|
-
accountInfo,
|
|
1076
|
-
symbolInfo,
|
|
1077
|
-
symbol,
|
|
1078
|
-
side,
|
|
1079
|
-
totalCollateral,
|
|
1080
|
-
reduceOnly
|
|
1001
|
+
const { data: markPrices } = useMarkPricesStream();
|
|
1002
|
+
const maxQty = React.useMemo(() => {
|
|
1003
|
+
if (!symbol)
|
|
1004
|
+
return 0;
|
|
1005
|
+
const positions2 = positionsPath2(positionsData);
|
|
1006
|
+
const positionQty = futures.account.getQtyFromPositions(positions2, symbol);
|
|
1007
|
+
if (reduceOnly) {
|
|
1008
|
+
if (positionQty > 0) {
|
|
1009
|
+
if (side === types.OrderSide.BUY) {
|
|
1010
|
+
return 0;
|
|
1011
|
+
} else {
|
|
1012
|
+
return Math.abs(positionQty);
|
|
1013
|
+
}
|
|
1081
1014
|
}
|
|
1082
|
-
|
|
1083
|
-
|
|
1015
|
+
if (positionQty < 0) {
|
|
1016
|
+
if (side === types.OrderSide.BUY) {
|
|
1017
|
+
return Math.abs(positionQty);
|
|
1018
|
+
} else {
|
|
1019
|
+
return 0;
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
return 0;
|
|
1023
|
+
}
|
|
1024
|
+
if (!markPrices || !markPrices[symbol] || !orders || !accountInfo)
|
|
1025
|
+
return 0;
|
|
1026
|
+
const getSymbolInfo = symbolInfo[symbol];
|
|
1027
|
+
const buyOrdersQty = futures.account.getQtyFromOrdersBySide(
|
|
1028
|
+
orders,
|
|
1029
|
+
symbol,
|
|
1030
|
+
types.OrderSide.BUY
|
|
1031
|
+
);
|
|
1032
|
+
const sellOrdersQty = futures.account.getQtyFromOrdersBySide(
|
|
1033
|
+
orders,
|
|
1034
|
+
symbol,
|
|
1035
|
+
types.OrderSide.SELL
|
|
1036
|
+
);
|
|
1037
|
+
const otherPositions = positions2.filter(
|
|
1038
|
+
(item) => item.symbol !== symbol
|
|
1039
|
+
);
|
|
1040
|
+
const otherOrders = orders.filter(
|
|
1041
|
+
(item) => item.symbol !== symbol
|
|
1042
|
+
);
|
|
1043
|
+
const otherIMs = futures.account.otherIMs({
|
|
1044
|
+
orders: otherOrders,
|
|
1045
|
+
positions: otherPositions,
|
|
1046
|
+
symbolInfo,
|
|
1047
|
+
markPrices,
|
|
1048
|
+
IMR_Factors: accountInfo.imr_factor,
|
|
1049
|
+
maxLeverage: accountInfo.max_leverage
|
|
1050
|
+
});
|
|
1051
|
+
return futures.account.maxQty(side, {
|
|
1052
|
+
markPrice: markPrices[symbol],
|
|
1053
|
+
symbol,
|
|
1054
|
+
baseMaxQty: getSymbolInfo("base_max"),
|
|
1055
|
+
totalCollateral,
|
|
1056
|
+
maxLeverage: accountInfo.max_leverage,
|
|
1057
|
+
takerFeeRate: accountInfo.taker_fee_rate,
|
|
1058
|
+
baseIMR: getSymbolInfo("base_imr"),
|
|
1059
|
+
otherIMs,
|
|
1060
|
+
positionQty,
|
|
1061
|
+
buyOrdersQty,
|
|
1062
|
+
sellOrdersQty,
|
|
1063
|
+
IMR_Factor: accountInfo.imr_factor[getSymbolInfo("base")]
|
|
1064
|
+
});
|
|
1065
|
+
}, [
|
|
1066
|
+
orders,
|
|
1067
|
+
positionsData,
|
|
1068
|
+
markPrices,
|
|
1069
|
+
accountInfo,
|
|
1070
|
+
symbolInfo,
|
|
1071
|
+
symbol,
|
|
1072
|
+
side,
|
|
1073
|
+
totalCollateral,
|
|
1074
|
+
reduceOnly
|
|
1075
|
+
]);
|
|
1084
1076
|
return maxQty;
|
|
1085
1077
|
};
|
|
1086
1078
|
var { maxPrice, minPrice } = futures.order;
|
|
@@ -1202,11 +1194,11 @@ var useOrderEntry = (symbol, initialValue = {}, options) => {
|
|
|
1202
1194
|
const { freeCollateral } = useCollateral();
|
|
1203
1195
|
const symbolInfo = useSymbolsInfo();
|
|
1204
1196
|
const tokenInfo = useTokenInfo();
|
|
1205
|
-
const baseDP =
|
|
1197
|
+
const baseDP = React.useMemo(
|
|
1206
1198
|
() => utils.getPrecisionByNumber(symbolInfo[symbol]("base_tick", 0)),
|
|
1207
1199
|
[symbolInfo]
|
|
1208
1200
|
);
|
|
1209
|
-
const quoteDP =
|
|
1201
|
+
const quoteDP = React.useMemo(() => {
|
|
1210
1202
|
return tokenInfo.USDC("decimals", 0);
|
|
1211
1203
|
}, [tokenInfo]);
|
|
1212
1204
|
const [valuesUpdate, [orderExtraValues]] = rxjsHooks.useEventCallback(
|
|
@@ -1268,7 +1260,7 @@ var useOrderEntry = (symbol, initialValue = {}, options) => {
|
|
|
1268
1260
|
orderExtraValues.side,
|
|
1269
1261
|
orderExtraValues.reduce_only
|
|
1270
1262
|
);
|
|
1271
|
-
const formFieldds =
|
|
1263
|
+
const formFieldds = React.useMemo(() => {
|
|
1272
1264
|
return ["order_quantity", "order_price", "total"];
|
|
1273
1265
|
}, []);
|
|
1274
1266
|
const setValue = (field, value) => {
|
|
@@ -1316,7 +1308,7 @@ var useOrderEntry = (symbol, initialValue = {}, options) => {
|
|
|
1316
1308
|
});
|
|
1317
1309
|
});
|
|
1318
1310
|
};
|
|
1319
|
-
|
|
1311
|
+
React.useEffect(() => {
|
|
1320
1312
|
formik$1.resetForm();
|
|
1321
1313
|
}, [symbol]);
|
|
1322
1314
|
return {
|
|
@@ -1337,9 +1329,9 @@ var useOrderEntry = (symbol, initialValue = {}, options) => {
|
|
|
1337
1329
|
};
|
|
1338
1330
|
var useFetures = () => {
|
|
1339
1331
|
const { data, isLoading, error } = useQuery(`/public/futures`);
|
|
1340
|
-
const [sortedData, setSortedData] =
|
|
1332
|
+
const [sortedData, setSortedData] = React.useState(data);
|
|
1341
1333
|
const ws = useWebSocketClient();
|
|
1342
|
-
|
|
1334
|
+
React.useEffect(() => {
|
|
1343
1335
|
const sub = ws.observe(`tickers`).subscribe((value) => {
|
|
1344
1336
|
console.log("useTickers", value);
|
|
1345
1337
|
});
|
|
@@ -1347,7 +1339,7 @@ var useFetures = () => {
|
|
|
1347
1339
|
sub.unsubscribe();
|
|
1348
1340
|
};
|
|
1349
1341
|
}, []);
|
|
1350
|
-
|
|
1342
|
+
React.useEffect(() => {
|
|
1351
1343
|
if (data) {
|
|
1352
1344
|
const sortedData2 = data.sort((a, b) => {
|
|
1353
1345
|
return 0;
|
|
@@ -1355,9 +1347,9 @@ var useFetures = () => {
|
|
|
1355
1347
|
setSortedData(sortedData2);
|
|
1356
1348
|
}
|
|
1357
1349
|
}, [data]);
|
|
1358
|
-
const sortBy =
|
|
1350
|
+
const sortBy = React.useCallback((key) => {
|
|
1359
1351
|
}, [data]);
|
|
1360
|
-
const filterBy =
|
|
1352
|
+
const filterBy = React.useCallback((key) => {
|
|
1361
1353
|
}, [data]);
|
|
1362
1354
|
return {
|
|
1363
1355
|
// ...data,
|
|
@@ -1368,6 +1360,11 @@ var useFetures = () => {
|
|
|
1368
1360
|
error
|
|
1369
1361
|
};
|
|
1370
1362
|
};
|
|
1363
|
+
|
|
1364
|
+
// src/orderly/useAccountInfo.ts
|
|
1365
|
+
var useAccountInfo = () => {
|
|
1366
|
+
return usePrivateQuery("/client/info");
|
|
1367
|
+
};
|
|
1371
1368
|
var useMarketsStream = () => {
|
|
1372
1369
|
const ws = useWebSocketClient();
|
|
1373
1370
|
const { data } = useQuery(`/public/futures`);
|
|
@@ -1404,14 +1401,14 @@ var useFundingRate = (symbol) => {
|
|
|
1404
1401
|
if (!symbol) {
|
|
1405
1402
|
throw new Error("useFuturesForSymbol requires a symbol");
|
|
1406
1403
|
}
|
|
1407
|
-
const [countDown, setCountDown] =
|
|
1404
|
+
const [countDown, setCountDown] = React.useState("00:00:00");
|
|
1408
1405
|
const { data } = useQuery(`/public/funding_rate/${symbol}`, {
|
|
1409
1406
|
fallbackData: {
|
|
1410
1407
|
est_funding_rate: 0,
|
|
1411
1408
|
next_funing_time: 0
|
|
1412
1409
|
}
|
|
1413
1410
|
});
|
|
1414
|
-
|
|
1411
|
+
React.useEffect(() => {
|
|
1415
1412
|
if (!data)
|
|
1416
1413
|
return;
|
|
1417
1414
|
const { next_funding_time } = data;
|
|
@@ -1435,6 +1432,86 @@ var useFundingRate = (symbol) => {
|
|
|
1435
1432
|
countDown
|
|
1436
1433
|
});
|
|
1437
1434
|
};
|
|
1435
|
+
var fetcher3 = (url, init) => net.get(url, init);
|
|
1436
|
+
var usePrivateInfiniteQuery = (getKey, options) => {
|
|
1437
|
+
var _a;
|
|
1438
|
+
const account3 = useAccount();
|
|
1439
|
+
const middleware = Array.isArray(options == null ? void 0 : options.use) ? (_a = options == null ? void 0 : options.use) != null ? _a : [] : [];
|
|
1440
|
+
const result = useSWRInfinite__default.default(
|
|
1441
|
+
(pageIndex, previousPageData) => account3.state.status >= types.AccountStatusEnum.SignedIn ? getKey(pageIndex, previousPageData) : null,
|
|
1442
|
+
fetcher3,
|
|
1443
|
+
__spreadProps(__spreadValues({}, options), {
|
|
1444
|
+
use: [signatureMiddleware, ...middleware]
|
|
1445
|
+
})
|
|
1446
|
+
);
|
|
1447
|
+
return result;
|
|
1448
|
+
};
|
|
1449
|
+
var OrderStatus = /* @__PURE__ */ ((OrderStatus2) => {
|
|
1450
|
+
OrderStatus2["FILLED"] = "FILLED";
|
|
1451
|
+
OrderStatus2["PARTIAL_FILLED"] = "PARTIAL_FILLED";
|
|
1452
|
+
OrderStatus2["CANCELED"] = "CANCELED";
|
|
1453
|
+
OrderStatus2["NEW"] = "NEW";
|
|
1454
|
+
OrderStatus2["COMPLETED"] = "COMPLETED";
|
|
1455
|
+
return OrderStatus2;
|
|
1456
|
+
})(OrderStatus || {});
|
|
1457
|
+
var useOrderStream = ({
|
|
1458
|
+
status = "NEW" /* NEW */,
|
|
1459
|
+
symbol
|
|
1460
|
+
} = {}) => {
|
|
1461
|
+
const { data: markPrices = {} } = useMarkPricesStream();
|
|
1462
|
+
const res = usePrivateInfiniteQuery(
|
|
1463
|
+
(pageIndex, previousPageData) => {
|
|
1464
|
+
const search = new URLSearchParams([
|
|
1465
|
+
["size", "100"],
|
|
1466
|
+
["page", `${pageIndex + 1}`],
|
|
1467
|
+
[`status`, status]
|
|
1468
|
+
]);
|
|
1469
|
+
if (symbol) {
|
|
1470
|
+
search.set(`symbol`, symbol);
|
|
1471
|
+
}
|
|
1472
|
+
return `/orders?${search.toString()}`;
|
|
1473
|
+
},
|
|
1474
|
+
{
|
|
1475
|
+
initialSize: 1,
|
|
1476
|
+
onError: (err) => {
|
|
1477
|
+
console.error("fetch failed::::", err);
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
);
|
|
1481
|
+
const orders = React.useMemo(() => {
|
|
1482
|
+
var _a;
|
|
1483
|
+
if (!res.data) {
|
|
1484
|
+
return null;
|
|
1485
|
+
}
|
|
1486
|
+
console.log("orders:::", markPrices);
|
|
1487
|
+
return (_a = res.data) == null ? void 0 : _a.flat().map((item) => {
|
|
1488
|
+
var _a2;
|
|
1489
|
+
return __spreadProps(__spreadValues({}, item), {
|
|
1490
|
+
mark_price: (_a2 = markPrices[item.symbol]) != null ? _a2 : 0
|
|
1491
|
+
});
|
|
1492
|
+
});
|
|
1493
|
+
}, [res.data, markPrices]);
|
|
1494
|
+
const cancelAllOrders = React.useCallback(() => {
|
|
1495
|
+
}, [res.data]);
|
|
1496
|
+
const updateOrder = React.useCallback((id, data) => {
|
|
1497
|
+
}, []);
|
|
1498
|
+
const cancelOrder = React.useCallback((id) => {
|
|
1499
|
+
}, []);
|
|
1500
|
+
return [
|
|
1501
|
+
orders,
|
|
1502
|
+
// {
|
|
1503
|
+
// ...res,
|
|
1504
|
+
// data: res.data?.reduce((acc, cur) => {
|
|
1505
|
+
// return [...acc, ...cur];
|
|
1506
|
+
// }, []),
|
|
1507
|
+
// },
|
|
1508
|
+
{
|
|
1509
|
+
cancelAllOrders,
|
|
1510
|
+
updateOrder,
|
|
1511
|
+
cancelOrder
|
|
1512
|
+
}
|
|
1513
|
+
];
|
|
1514
|
+
};
|
|
1438
1515
|
|
|
1439
1516
|
// src/apis/index.ts
|
|
1440
1517
|
var apis_exports = {};
|
|
@@ -1486,14 +1563,18 @@ Object.defineProperty(exports, 'useObservable', {
|
|
|
1486
1563
|
enumerable: true,
|
|
1487
1564
|
get: function () { return rxjsHooks.useObservable; }
|
|
1488
1565
|
});
|
|
1566
|
+
exports.DataSourceProvider = DataSourceProvider;
|
|
1489
1567
|
exports.OrderStatus = OrderStatus;
|
|
1490
1568
|
exports.OrderlyContext = OrderlyContext;
|
|
1491
1569
|
exports.OrderlyProvider = OrderlyProvider;
|
|
1492
1570
|
exports.apis = apis_exports;
|
|
1493
1571
|
exports.useAccount = useAccount;
|
|
1572
|
+
exports.useAccountInfo = useAccountInfo;
|
|
1573
|
+
exports.useAppState = useAppState;
|
|
1494
1574
|
exports.useCollateral = useCollateral;
|
|
1495
1575
|
exports.useFetures = useFetures;
|
|
1496
1576
|
exports.useFundingRate = useFundingRate;
|
|
1577
|
+
exports.useMarkPricesStream = useMarkPricesStream;
|
|
1497
1578
|
exports.useMarketsStream = useMarketsStream;
|
|
1498
1579
|
exports.useMaxQty = useMaxQty;
|
|
1499
1580
|
exports.useMutation = useMutation;
|
|
@@ -1509,6 +1590,7 @@ exports.useTickerStream = useTickerStream;
|
|
|
1509
1590
|
exports.useTokenInfo = useTokenInfo;
|
|
1510
1591
|
exports.useTopicObserve = useTopicObserve;
|
|
1511
1592
|
exports.useTradingView = useTradingView;
|
|
1593
|
+
exports.useWS = useWS;
|
|
1512
1594
|
exports.useWebSocketClient = useWebSocketClient;
|
|
1513
1595
|
//# sourceMappingURL=out.js.map
|
|
1514
1596
|
//# sourceMappingURL=index.js.map
|