@orderly.network/hooks 0.0.9 → 0.0.10
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 +288 -0
- package/dist/index.d.ts +288 -0
- package/dist/index.js +434 -336
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +435 -341
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -8
package/dist/index.js
CHANGED
|
@@ -4,17 +4,16 @@ 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
|
-
var core = require('@orderly.network/core');
|
|
8
7
|
var useConstant = require('use-constant');
|
|
8
|
+
var core = require('@orderly.network/core');
|
|
9
9
|
var rxjsHooks = require('rxjs-hooks');
|
|
10
10
|
var types = require('@orderly.network/types');
|
|
11
11
|
var rxjs = require('rxjs');
|
|
12
12
|
var operators = require('rxjs/operators');
|
|
13
13
|
var ramda = require('ramda');
|
|
14
|
+
var useSWRSubscription = require('swr/subscription');
|
|
14
15
|
var utils = require('@orderly.network/utils');
|
|
15
|
-
var formik = require('formik');
|
|
16
16
|
var futures = require('@orderly.network/futures');
|
|
17
|
-
var useSWRSubscription = require('swr/subscription');
|
|
18
17
|
var useSWRInfinite = require('swr/infinite');
|
|
19
18
|
|
|
20
19
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
@@ -104,26 +103,52 @@ var useQuery = (query, options) => {
|
|
|
104
103
|
swrOptions
|
|
105
104
|
);
|
|
106
105
|
};
|
|
106
|
+
var useAccountInstance = () => {
|
|
107
|
+
const { configStore, keyStore, walletAdapter } = React.useContext(OrderlyContext);
|
|
108
|
+
if (!configStore)
|
|
109
|
+
throw new Error("configStore is not defined, please use OrderlyProvider");
|
|
110
|
+
if (!keyStore) {
|
|
111
|
+
throw new Error(
|
|
112
|
+
"keyStore is not defined, please use OrderlyProvider and provide keyStore"
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
const account4 = useConstant__default.default(() => {
|
|
116
|
+
let account5 = core.SimpleDI.get("account");
|
|
117
|
+
if (!account5) {
|
|
118
|
+
account5 = new core.Account(configStore, keyStore, walletAdapter);
|
|
119
|
+
core.SimpleDI.registerByName("account", account5);
|
|
120
|
+
}
|
|
121
|
+
return account5;
|
|
122
|
+
});
|
|
123
|
+
return account4;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
// src/useMutation.ts
|
|
107
127
|
var fetcher2 = (url, options) => {
|
|
108
128
|
return net.post(url, options.arg.data, {
|
|
109
129
|
headers: __spreadValues({}, options.arg.signature)
|
|
110
130
|
});
|
|
111
131
|
};
|
|
112
|
-
var
|
|
132
|
+
var deleteFetcher = (url, options) => {
|
|
133
|
+
return net.del(url, options.arg.data, {
|
|
134
|
+
headers: __spreadValues({}, options.arg.signature)
|
|
135
|
+
});
|
|
136
|
+
};
|
|
137
|
+
var useMutation = (url, options, method = "POST") => {
|
|
113
138
|
const { apiBaseUrl } = React.useContext(OrderlyContext);
|
|
114
139
|
if (!url.startsWith("http")) {
|
|
115
140
|
url = `${apiBaseUrl}${url}`;
|
|
116
141
|
}
|
|
117
|
-
|
|
118
|
-
const signer =
|
|
142
|
+
const account4 = useAccountInstance();
|
|
143
|
+
const signer = account4.signer;
|
|
119
144
|
const { trigger, data, error, reset, isMutating } = useSWRMutation__default.default(
|
|
120
145
|
url,
|
|
121
|
-
fetcher2,
|
|
146
|
+
method === "POST" ? fetcher2 : deleteFetcher,
|
|
122
147
|
options
|
|
123
148
|
);
|
|
124
149
|
const mutation = (data2) => __async(void 0, null, function* () {
|
|
125
150
|
const payload = {
|
|
126
|
-
method
|
|
151
|
+
method,
|
|
127
152
|
url,
|
|
128
153
|
data: data2
|
|
129
154
|
};
|
|
@@ -131,26 +156,28 @@ var useMutation = (url, options) => {
|
|
|
131
156
|
return trigger({
|
|
132
157
|
data: data2,
|
|
133
158
|
signature: __spreadProps(__spreadValues({}, signature), {
|
|
134
|
-
"orderly-account-id":
|
|
159
|
+
"orderly-account-id": account4.accountId
|
|
135
160
|
})
|
|
136
161
|
});
|
|
137
162
|
});
|
|
138
|
-
return
|
|
163
|
+
return [
|
|
139
164
|
mutation,
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
165
|
+
{
|
|
166
|
+
data,
|
|
167
|
+
error,
|
|
168
|
+
reset,
|
|
169
|
+
isMutating
|
|
170
|
+
}
|
|
171
|
+
];
|
|
145
172
|
};
|
|
146
173
|
var signatureMiddleware = (useSWRNext) => {
|
|
147
174
|
const { apiBaseUrl } = React.useContext(OrderlyContext);
|
|
148
175
|
return (key, fetcher4, config) => {
|
|
149
176
|
try {
|
|
150
177
|
const extendedFetcher = (url) => __async(void 0, null, function* () {
|
|
151
|
-
let
|
|
178
|
+
let account4 = core.SimpleDI.get("account");
|
|
152
179
|
let fullUrl = `${apiBaseUrl}${url}`;
|
|
153
|
-
const signer =
|
|
180
|
+
const signer = account4.signer;
|
|
154
181
|
const payload = {
|
|
155
182
|
method: "GET",
|
|
156
183
|
url: fullUrl
|
|
@@ -158,7 +185,7 @@ var signatureMiddleware = (useSWRNext) => {
|
|
|
158
185
|
const signature = yield signer.sign(payload);
|
|
159
186
|
return fetcher4(fullUrl, {
|
|
160
187
|
headers: __spreadProps(__spreadValues({}, signature), {
|
|
161
|
-
"orderly-account-id":
|
|
188
|
+
"orderly-account-id": account4.accountId
|
|
162
189
|
})
|
|
163
190
|
});
|
|
164
191
|
});
|
|
@@ -170,7 +197,13 @@ var signatureMiddleware = (useSWRNext) => {
|
|
|
170
197
|
};
|
|
171
198
|
};
|
|
172
199
|
var useAccount = () => {
|
|
173
|
-
const {
|
|
200
|
+
const {
|
|
201
|
+
configStore,
|
|
202
|
+
keyStore,
|
|
203
|
+
walletAdapter,
|
|
204
|
+
onWalletConnect,
|
|
205
|
+
onWalletDisconnect
|
|
206
|
+
} = React.useContext(OrderlyContext);
|
|
174
207
|
if (!configStore)
|
|
175
208
|
throw new Error("configStore is not defined, please use OrderlyProvider");
|
|
176
209
|
if (!keyStore) {
|
|
@@ -178,39 +211,58 @@ var useAccount = () => {
|
|
|
178
211
|
"keyStore is not defined, please use OrderlyProvider and provide keyStore"
|
|
179
212
|
);
|
|
180
213
|
}
|
|
181
|
-
const
|
|
182
|
-
let
|
|
183
|
-
if (!
|
|
184
|
-
|
|
185
|
-
core.SimpleDI.registerByName("account",
|
|
214
|
+
const account4 = useConstant__default.default(() => {
|
|
215
|
+
let account5 = core.SimpleDI.get("account");
|
|
216
|
+
if (!account5) {
|
|
217
|
+
account5 = new core.Account(configStore, keyStore, walletAdapter);
|
|
218
|
+
core.SimpleDI.registerByName("account", account5);
|
|
186
219
|
}
|
|
187
|
-
return
|
|
220
|
+
return account5;
|
|
188
221
|
});
|
|
189
222
|
const state = rxjsHooks.useObservable(
|
|
190
|
-
() =>
|
|
191
|
-
|
|
223
|
+
() => account4.state$,
|
|
224
|
+
account4.stateValue
|
|
192
225
|
);
|
|
193
226
|
const login = React.useCallback(
|
|
194
227
|
(address) => {
|
|
195
|
-
|
|
228
|
+
account4.login(address);
|
|
196
229
|
},
|
|
197
|
-
[
|
|
230
|
+
[account4]
|
|
231
|
+
);
|
|
232
|
+
const createOrderlyKey = React.useCallback(
|
|
233
|
+
(remember) => __async(void 0, null, function* () {
|
|
234
|
+
return account4.createOrderlyKey(remember ? 365 : 30);
|
|
235
|
+
}),
|
|
236
|
+
[account4]
|
|
198
237
|
);
|
|
238
|
+
const createAccount = React.useCallback(() => __async(void 0, null, function* () {
|
|
239
|
+
return account4.createAccount();
|
|
240
|
+
}), [account4]);
|
|
241
|
+
const connect = React.useCallback(() => __async(void 0, null, function* () {
|
|
242
|
+
return onWalletConnect == null ? void 0 : onWalletConnect();
|
|
243
|
+
}), [account4]);
|
|
244
|
+
const disconnect = React.useCallback(() => __async(void 0, null, function* () {
|
|
245
|
+
return onWalletDisconnect == null ? void 0 : onWalletDisconnect();
|
|
246
|
+
}), [account4]);
|
|
199
247
|
return {
|
|
200
248
|
// account: state!,
|
|
201
|
-
account:
|
|
249
|
+
account: account4,
|
|
202
250
|
state,
|
|
203
251
|
// info: {},
|
|
204
|
-
login
|
|
252
|
+
login,
|
|
253
|
+
createOrderlyKey,
|
|
254
|
+
createAccount,
|
|
255
|
+
disconnect,
|
|
256
|
+
connect
|
|
205
257
|
};
|
|
206
258
|
};
|
|
207
259
|
var usePrivateQuery = (query, options) => {
|
|
208
260
|
var _b;
|
|
209
261
|
const _a = options || {}, { formatter } = _a, swrOptions = __objRest(_a, ["formatter"]);
|
|
210
|
-
const
|
|
262
|
+
const account4 = useAccount();
|
|
211
263
|
const middleware = Array.isArray(options == null ? void 0 : options.use) ? (_b = options == null ? void 0 : options.use) != null ? _b : [] : [];
|
|
212
264
|
return useSWR__default.default(
|
|
213
|
-
() =>
|
|
265
|
+
() => account4.state.status >= types.AccountStatusEnum.EnableTrading ? query : null,
|
|
214
266
|
// query,
|
|
215
267
|
(url, init) => {
|
|
216
268
|
return fetcher(url, init, { formatter });
|
|
@@ -297,7 +349,7 @@ var useWebSocketClient = () => {
|
|
|
297
349
|
let websocketClient = core.SimpleDI.get(WS_NAME);
|
|
298
350
|
if (!websocketClient) {
|
|
299
351
|
websocketClient = new net.WebSocketClient({
|
|
300
|
-
accountId: "
|
|
352
|
+
accountId: "OqdphuyCtYWxwzhxyLLjOWNdFP7sQt8RPWzmb5xY",
|
|
301
353
|
networkId: "testnet",
|
|
302
354
|
onSigntureRequest: (accountId) => __async(void 0, null, function* () {
|
|
303
355
|
const signer = core.getMockSigner();
|
|
@@ -312,28 +364,6 @@ var useWebSocketClient = () => {
|
|
|
312
364
|
});
|
|
313
365
|
return ws;
|
|
314
366
|
};
|
|
315
|
-
var useAccountInstance = () => {
|
|
316
|
-
const { configStore, keyStore } = React.useContext(OrderlyContext);
|
|
317
|
-
if (!configStore)
|
|
318
|
-
throw new Error("configStore is not defined, please use OrderlyProvider");
|
|
319
|
-
if (!keyStore) {
|
|
320
|
-
throw new Error(
|
|
321
|
-
"keyStore is not defined, please use OrderlyProvider and provide keyStore"
|
|
322
|
-
);
|
|
323
|
-
}
|
|
324
|
-
const account3 = useConstant__default.default(() => {
|
|
325
|
-
let account4 = core.SimpleDI.get("account");
|
|
326
|
-
if (!account4) {
|
|
327
|
-
account4 = new core.Account(configStore, keyStore);
|
|
328
|
-
core.SimpleDI.registerByName("account", account4);
|
|
329
|
-
}
|
|
330
|
-
return account4;
|
|
331
|
-
});
|
|
332
|
-
console.log("account instance ======>>>>>", account3);
|
|
333
|
-
return account3;
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
// src/provider/dataProvider.tsx
|
|
337
367
|
var DataSourceContext = React.createContext({});
|
|
338
368
|
var DataSourceProvider = (props) => {
|
|
339
369
|
console.log("render DataSourceProvider");
|
|
@@ -346,12 +376,13 @@ var WS_NAME2 = "nativeWebsocketClient";
|
|
|
346
376
|
var useWS = () => {
|
|
347
377
|
const ws = useConstant__default.default(() => {
|
|
348
378
|
let websocketClient = core.SimpleDI.get(WS_NAME2);
|
|
379
|
+
const account4 = core.SimpleDI.get(core.Account.instanceName);
|
|
349
380
|
if (!websocketClient) {
|
|
350
381
|
websocketClient = new net.WS({
|
|
351
|
-
accountId: "
|
|
382
|
+
// accountId: "OqdphuyCtYWxwzhxyLLjOWNdFP7sQt8RPWzmb5xY",
|
|
352
383
|
networkId: "testnet",
|
|
353
384
|
onSigntureRequest: (accountId) => __async(void 0, null, function* () {
|
|
354
|
-
const signer =
|
|
385
|
+
const signer = account4.signer;
|
|
355
386
|
const timestamp = (/* @__PURE__ */ new Date()).getTime();
|
|
356
387
|
const result = yield signer.signText(timestamp.toString());
|
|
357
388
|
return __spreadProps(__spreadValues({}, result), { timestamp });
|
|
@@ -368,34 +399,69 @@ var useTickerStream = (symbol) => {
|
|
|
368
399
|
throw new Error("useFuturesForSymbol requires a symbol");
|
|
369
400
|
}
|
|
370
401
|
const { data: info } = useQuery(`/public/futures/${symbol}`);
|
|
371
|
-
const ws =
|
|
372
|
-
const ticker =
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
if (ticker2.close !== void 0 && ticker2.open !== void 0) {
|
|
389
|
-
config["change"] = new utils.Decimal(ticker2.close).minus(ticker2.open).div(ticker2.open).toNumber();
|
|
402
|
+
const ws = useWS();
|
|
403
|
+
const { data: ticker } = useSWRSubscription__default.default(
|
|
404
|
+
`${symbol}@ticker`,
|
|
405
|
+
(key, { next }) => {
|
|
406
|
+
const unsubscribe = ws.subscribe(
|
|
407
|
+
// { event: "subscribe", topic: "markprices" },
|
|
408
|
+
`${symbol}@ticker`,
|
|
409
|
+
{
|
|
410
|
+
onMessage: (message) => {
|
|
411
|
+
next(null, message);
|
|
412
|
+
}
|
|
413
|
+
// onUnsubscribe: () => {
|
|
414
|
+
// return "markprices";
|
|
415
|
+
// },
|
|
416
|
+
// onError: (error: any) => {
|
|
417
|
+
// console.log("error", error);
|
|
418
|
+
// },
|
|
390
419
|
}
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
420
|
+
);
|
|
421
|
+
return () => {
|
|
422
|
+
console.log("unsubscribe!!!!!!!");
|
|
423
|
+
unsubscribe == null ? void 0 : unsubscribe();
|
|
424
|
+
};
|
|
425
|
+
}
|
|
396
426
|
);
|
|
397
|
-
|
|
427
|
+
const value = React.useMemo(() => {
|
|
428
|
+
if (!info)
|
|
429
|
+
return null;
|
|
430
|
+
if (!ticker)
|
|
431
|
+
return info;
|
|
432
|
+
const config = __spreadValues({}, info);
|
|
433
|
+
if (ticker.close !== void 0) {
|
|
434
|
+
config["24h_close"] = ticker.close;
|
|
435
|
+
}
|
|
436
|
+
if (ticker.open !== void 0) {
|
|
437
|
+
config["24h_open"] = ticker.open;
|
|
438
|
+
}
|
|
439
|
+
if (ticker.volume !== void 0) {
|
|
440
|
+
config["24h_volumn"] = ticker.volume;
|
|
441
|
+
}
|
|
442
|
+
if (ticker.close !== void 0 && ticker.open !== void 0) {
|
|
443
|
+
config["change"] = new utils.Decimal(ticker.close).minus(ticker.open).div(ticker.open).toNumber();
|
|
444
|
+
}
|
|
445
|
+
return config;
|
|
446
|
+
}, [info, ticker]);
|
|
447
|
+
return value;
|
|
398
448
|
};
|
|
449
|
+
var useMarkPrice = (symbol) => {
|
|
450
|
+
const ws = useWS();
|
|
451
|
+
return useSWRSubscription__default.default(`${symbol}@markprice`, (key, { next }) => {
|
|
452
|
+
const unsubscribe = ws.subscribe(`${symbol}@markprice`, {
|
|
453
|
+
onMessage: (message) => {
|
|
454
|
+
next(null, message.price);
|
|
455
|
+
}
|
|
456
|
+
});
|
|
457
|
+
return () => {
|
|
458
|
+
console.log("unsubscribe useMarkPrice !!!!!!!");
|
|
459
|
+
unsubscribe == null ? void 0 : unsubscribe();
|
|
460
|
+
};
|
|
461
|
+
});
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
// src/orderly/useOrderbookStream.ts
|
|
399
465
|
ramda.compose(ramda.defaultTo(0), ramda.head, ramda.last, ramda.pathOr([], ["asks"]));
|
|
400
466
|
ramda.pathOr(0, ["bids", 0, 0]);
|
|
401
467
|
var paddingFn = (len) => Array(len).fill([Number.NaN, Number.NaN, Number.NaN]);
|
|
@@ -408,7 +474,13 @@ var reduceItems = (depth, level, data) => {
|
|
|
408
474
|
const result = [];
|
|
409
475
|
for (let i = 0; i < data.length; i++) {
|
|
410
476
|
const [price, quantity] = data[i];
|
|
411
|
-
|
|
477
|
+
if (isNaN(price) || isNaN(quantity))
|
|
478
|
+
continue;
|
|
479
|
+
result.push([
|
|
480
|
+
price,
|
|
481
|
+
quantity,
|
|
482
|
+
quantity + (result.length > 0 ? result[result.length - 1][2] : 0)
|
|
483
|
+
]);
|
|
412
484
|
if (i + 1 >= level) {
|
|
413
485
|
break;
|
|
414
486
|
}
|
|
@@ -454,21 +526,22 @@ var mergeOrderbook = (data, update) => {
|
|
|
454
526
|
bids: mergeItems(data.bids, update.bids).sort(bidsSortFn)
|
|
455
527
|
};
|
|
456
528
|
};
|
|
457
|
-
var
|
|
529
|
+
var useOrderbookStream = (symbol, initial = { asks: [], bids: [] }, options) => {
|
|
458
530
|
if (!symbol) {
|
|
459
|
-
throw new Error("
|
|
531
|
+
throw new Error("useOrderbookStream requires a symbol");
|
|
460
532
|
}
|
|
533
|
+
const [requestData, setRequestData] = React.useState(null);
|
|
461
534
|
const [data, setData] = React.useState(initial);
|
|
535
|
+
const [isLoading, setIsLoading] = React.useState(true);
|
|
462
536
|
const [depth, setDepth] = React.useState(1e-3);
|
|
463
537
|
const [level, setLevel] = React.useState(() => {
|
|
464
538
|
var _a;
|
|
465
539
|
return (_a = options == null ? void 0 : options.level) != null ? _a : 10;
|
|
466
540
|
});
|
|
467
|
-
const ws =
|
|
468
|
-
const orderbookSubscriberRef = React.useRef();
|
|
541
|
+
const ws = useWS();
|
|
469
542
|
const ticker = useTickerStream(symbol);
|
|
470
|
-
|
|
471
|
-
|
|
543
|
+
React.useEffect(() => {
|
|
544
|
+
ws.onceSubscribe(
|
|
472
545
|
{
|
|
473
546
|
event: "request",
|
|
474
547
|
params: {
|
|
@@ -476,66 +549,52 @@ var useOrderbook = (symbol, initial = { asks: [], bids: [] }, options) => {
|
|
|
476
549
|
symbol
|
|
477
550
|
}
|
|
478
551
|
},
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
552
|
+
{
|
|
553
|
+
onMessage: (message) => {
|
|
554
|
+
console.log("orderbook request message", message);
|
|
555
|
+
if (!!message) {
|
|
556
|
+
const reduceOrderbookData = reduceOrderbook(depth, level, message);
|
|
557
|
+
setRequestData(reduceOrderbookData);
|
|
558
|
+
setData(reduceOrderbookData);
|
|
559
|
+
}
|
|
560
|
+
setIsLoading(false);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
490
563
|
);
|
|
564
|
+
return () => {
|
|
565
|
+
setRequestData(null);
|
|
566
|
+
};
|
|
491
567
|
}, [symbol]);
|
|
492
|
-
const
|
|
493
|
-
return new rxjs.BehaviorSubject({
|
|
494
|
-
depth: 1e-3,
|
|
495
|
-
level: 10
|
|
496
|
-
});
|
|
497
|
-
});
|
|
498
|
-
const markPrice = rxjsHooks.useObservable(
|
|
499
|
-
(_, input$) => input$.pipe(
|
|
500
|
-
operators.debounceTime(200),
|
|
501
|
-
operators.switchMap(([symbol2]) => {
|
|
502
|
-
return ws.observe(`${symbol2}@markprice`).pipe(operators.map((data2) => data2.price));
|
|
503
|
-
})
|
|
504
|
-
),
|
|
505
|
-
0,
|
|
506
|
-
[symbol]
|
|
507
|
-
);
|
|
568
|
+
const { data: markPrice } = useMarkPrice(symbol);
|
|
508
569
|
React.useEffect(() => {
|
|
509
|
-
if (
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
570
|
+
if (!requestData)
|
|
571
|
+
return;
|
|
572
|
+
const subscription = ws.subscribe(
|
|
573
|
+
{
|
|
574
|
+
event: "subscribe",
|
|
575
|
+
topic: `${symbol}@orderbookupdate`
|
|
576
|
+
},
|
|
577
|
+
{
|
|
578
|
+
onMessage: (message) => {
|
|
579
|
+
setData((data2) => {
|
|
580
|
+
const mergedData = !message.asks && !message.bids ? data2 : mergeOrderbook(data2, message);
|
|
581
|
+
const reducedData = reduceOrderbook(depth, level, mergedData);
|
|
582
|
+
return reducedData;
|
|
583
|
+
});
|
|
520
584
|
}
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
}, [orderbookRequest$, orderbookUpdate$]);
|
|
585
|
+
}
|
|
586
|
+
);
|
|
587
|
+
return () => {
|
|
588
|
+
subscription == null ? void 0 : subscription();
|
|
589
|
+
};
|
|
590
|
+
}, [symbol, requestData]);
|
|
528
591
|
const onDepthChange = React.useCallback((depth2) => {
|
|
529
592
|
console.log("Orderbook depth has changed:", depth2);
|
|
530
|
-
orderbookOptions$.next(__spreadProps(__spreadValues({}, orderbookOptions$.value), {
|
|
531
|
-
depth: depth2
|
|
532
|
-
// level,
|
|
533
|
-
}));
|
|
534
593
|
}, []);
|
|
535
594
|
const middlePrice = React.useMemo(() => {
|
|
536
595
|
let asksFrist = 0, bidsFirst = 0;
|
|
537
596
|
if (data.asks.length > 0) {
|
|
538
|
-
asksFrist = data.asks[data.
|
|
597
|
+
asksFrist = data.asks[data.asks.length - 1][0];
|
|
539
598
|
}
|
|
540
599
|
if (data.bids.length > 0) {
|
|
541
600
|
bidsFirst = data.bids[0][0];
|
|
@@ -546,7 +605,7 @@ var useOrderbook = (symbol, initial = { asks: [], bids: [] }, options) => {
|
|
|
546
605
|
}, [ticker, data]);
|
|
547
606
|
return [
|
|
548
607
|
__spreadProps(__spreadValues({}, data), { markPrice, middlePrice }),
|
|
549
|
-
{ onDepthChange, depth }
|
|
608
|
+
{ onDepthChange, depth, isLoading }
|
|
550
609
|
];
|
|
551
610
|
};
|
|
552
611
|
|
|
@@ -650,14 +709,15 @@ function orderEntityFormatHandle(baseTick, quoteTick) {
|
|
|
650
709
|
}
|
|
651
710
|
function priceInputHandle(inputs) {
|
|
652
711
|
const [values, input, value, markPrice, config] = inputs;
|
|
712
|
+
console.log("priceInputHandle", inputs);
|
|
653
713
|
if (value === "") {
|
|
654
714
|
return [__spreadProps(__spreadValues({}, values), { total: "" }), input, value, markPrice, config];
|
|
655
715
|
}
|
|
656
716
|
const price = new utils.Decimal(value);
|
|
657
717
|
const priceDP = price.dp();
|
|
718
|
+
console.log("priceInputHandle", priceDP, config.quoteDP);
|
|
658
719
|
if (priceDP > config.quoteDP) {
|
|
659
|
-
price.toDecimalPlaces(config.quoteDP);
|
|
660
|
-
values.order_price = price.toNumber();
|
|
720
|
+
values.order_price = price.toDecimalPlaces(config.quoteDP).toString();
|
|
661
721
|
}
|
|
662
722
|
price.toDecimalPlaces(Math.min(priceDP, config.quoteDP));
|
|
663
723
|
if (!values.order_quantity) {
|
|
@@ -665,9 +725,10 @@ function priceInputHandle(inputs) {
|
|
|
665
725
|
}
|
|
666
726
|
const total = price.mul(values.order_quantity);
|
|
667
727
|
const quantityDP = total.dp();
|
|
668
|
-
total.toDecimalPlaces(Math.min(quantityDP, config.baseDP));
|
|
669
728
|
return [
|
|
670
|
-
__spreadProps(__spreadValues({}, values), {
|
|
729
|
+
__spreadProps(__spreadValues({}, values), {
|
|
730
|
+
total: total.toDecimalPlaces(Math.min(quantityDP, config.baseDP)).toString()
|
|
731
|
+
}),
|
|
671
732
|
input,
|
|
672
733
|
value,
|
|
673
734
|
markPrice,
|
|
@@ -691,10 +752,9 @@ function quantityInputHandle(inputs) {
|
|
|
691
752
|
}
|
|
692
753
|
const total = quantity.mul(price);
|
|
693
754
|
const totalDP = total.dp();
|
|
694
|
-
total.todp(Math.min(config.quoteDP, totalDP));
|
|
695
755
|
return [
|
|
696
756
|
__spreadProps(__spreadValues({}, values), {
|
|
697
|
-
total: total.toNumber()
|
|
757
|
+
total: total.todp(Math.min(config.quoteDP, totalDP)).toNumber()
|
|
698
758
|
}),
|
|
699
759
|
input,
|
|
700
760
|
value,
|
|
@@ -770,25 +830,12 @@ var useFundingRates = () => {
|
|
|
770
830
|
);
|
|
771
831
|
return createGetter(data);
|
|
772
832
|
};
|
|
773
|
-
var useMarkPricesSubject = () => {
|
|
774
|
-
const ws = useWebSocketClient();
|
|
775
|
-
return useConstant__default.default(
|
|
776
|
-
() => ws.observe("markprices").pipe(
|
|
777
|
-
operators.map((data) => {
|
|
778
|
-
const prices = {};
|
|
779
|
-
data.forEach((item) => {
|
|
780
|
-
prices[item.symbol] = item.price;
|
|
781
|
-
});
|
|
782
|
-
return prices;
|
|
783
|
-
})
|
|
784
|
-
)
|
|
785
|
-
);
|
|
786
|
-
};
|
|
787
833
|
var useMarkPricesStream = () => {
|
|
788
834
|
const ws = useWS();
|
|
789
835
|
return useSWRSubscription__default.default("markPrices", (key, { next }) => {
|
|
790
|
-
const unsubscribe = ws.
|
|
791
|
-
{ event: "subscribe", topic: "markprices" },
|
|
836
|
+
const unsubscribe = ws.subscribe(
|
|
837
|
+
// { event: "subscribe", topic: "markprices" },
|
|
838
|
+
"markprices",
|
|
792
839
|
{
|
|
793
840
|
onMessage: (message) => {
|
|
794
841
|
const data = /* @__PURE__ */ Object.create(null);
|
|
@@ -798,9 +845,9 @@ var useMarkPricesStream = () => {
|
|
|
798
845
|
}
|
|
799
846
|
next(null, data);
|
|
800
847
|
},
|
|
801
|
-
onUnsubscribe: () => {
|
|
802
|
-
|
|
803
|
-
},
|
|
848
|
+
// onUnsubscribe: () => {
|
|
849
|
+
// return "markprices";
|
|
850
|
+
// },
|
|
804
851
|
onError: (error) => {
|
|
805
852
|
console.log("error", error);
|
|
806
853
|
}
|
|
@@ -808,36 +855,34 @@ var useMarkPricesStream = () => {
|
|
|
808
855
|
);
|
|
809
856
|
return () => {
|
|
810
857
|
console.log("unsubscribe!!!!!!!");
|
|
811
|
-
console.log("unsubscribe", unsubscribe);
|
|
812
858
|
unsubscribe == null ? void 0 : unsubscribe();
|
|
813
859
|
};
|
|
814
860
|
});
|
|
815
861
|
};
|
|
816
862
|
var usePositionStream = (symbol, options) => {
|
|
817
|
-
React.useState(false);
|
|
818
863
|
const [visibledSymbol, setVisibleSymbol] = React.useState(
|
|
819
864
|
symbol
|
|
820
865
|
);
|
|
821
866
|
const symbolInfo = useSymbolsInfo();
|
|
822
867
|
const { data: accountInfo } = usePrivateQuery("/client/info");
|
|
823
|
-
const { mutation } = useMutation("/order");
|
|
824
868
|
const fundingRates = useFundingRates();
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
console.log("usePositionStream error", err);
|
|
832
|
-
}
|
|
833
|
-
})
|
|
834
|
-
);
|
|
869
|
+
const { data, error } = usePrivateQuery(`/positions`, __spreadProps(__spreadValues({}, options), {
|
|
870
|
+
formatter: (data2) => data2,
|
|
871
|
+
onError: (err) => {
|
|
872
|
+
console.log("usePositionStream error", err);
|
|
873
|
+
}
|
|
874
|
+
}));
|
|
835
875
|
const { data: markPrices } = useMarkPricesStream();
|
|
836
876
|
const formatedPositions = React.useMemo(() => {
|
|
837
877
|
if (!(data == null ? void 0 : data.rows) || !symbolInfo || !accountInfo)
|
|
838
878
|
return null;
|
|
839
879
|
let totalCollateral = 0;
|
|
840
|
-
|
|
880
|
+
const filteredData = typeof symbol === "undefined" || symbol === "" ? data.rows.filter((item) => {
|
|
881
|
+
return item.position_qty !== 0;
|
|
882
|
+
}) : data.rows.filter((item) => {
|
|
883
|
+
return item.symbol === symbol && item.position_qty !== 0;
|
|
884
|
+
});
|
|
885
|
+
return filteredData.map((item) => {
|
|
841
886
|
const price = ramda.propOr(
|
|
842
887
|
item.mark_price,
|
|
843
888
|
item.symbol,
|
|
@@ -873,7 +918,7 @@ var usePositionStream = (symbol, options) => {
|
|
|
873
918
|
})
|
|
874
919
|
});
|
|
875
920
|
});
|
|
876
|
-
}, [data == null ? void 0 : data.rows, symbolInfo, accountInfo, markPrices]);
|
|
921
|
+
}, [data == null ? void 0 : data.rows, symbolInfo, accountInfo, markPrices, symbol]);
|
|
877
922
|
const aggregatedData = React.useMemo(() => {
|
|
878
923
|
const aggregatedData2 = {
|
|
879
924
|
unsettledPnL: NaN,
|
|
@@ -901,18 +946,11 @@ var usePositionStream = (symbol, options) => {
|
|
|
901
946
|
const showSymbol = React.useCallback((symbol2) => {
|
|
902
947
|
setVisibleSymbol(symbol2);
|
|
903
948
|
}, []);
|
|
904
|
-
const onClosePosition = React.useCallback(
|
|
905
|
-
(order2) => {
|
|
906
|
-
return mutation(order2).finally(() => {
|
|
907
|
-
});
|
|
908
|
-
},
|
|
909
|
-
[]
|
|
910
|
-
);
|
|
911
949
|
return [
|
|
912
950
|
{ rows: formatedPositions, aggregated: aggregatedData },
|
|
913
951
|
createGetter(data, 1),
|
|
914
952
|
{
|
|
915
|
-
close: onClosePosition,
|
|
953
|
+
// close: onClosePosition,
|
|
916
954
|
loading: false,
|
|
917
955
|
showSymbol,
|
|
918
956
|
error,
|
|
@@ -927,7 +965,8 @@ var usePositionStream = (symbol, options) => {
|
|
|
927
965
|
};
|
|
928
966
|
var totalUnsettlementPnLPath = ramda.pathOr(0, [0, "aggregated", "unsettledPnL"]);
|
|
929
967
|
var positionsPath = ramda.pathOr([], [0, "rows"]);
|
|
930
|
-
var useCollateral = (
|
|
968
|
+
var useCollateral = (options = { dp: 6 }) => {
|
|
969
|
+
const { dp } = options;
|
|
931
970
|
const positions2 = usePositionStream();
|
|
932
971
|
const { data: orders } = usePrivateQuery(`/orders`);
|
|
933
972
|
const { data: accountInfo } = usePrivateQuery("/client/info");
|
|
@@ -1081,7 +1120,7 @@ var BaseOrderCreator = class {
|
|
|
1081
1120
|
baseOrder(data) {
|
|
1082
1121
|
const order2 = {
|
|
1083
1122
|
// symbol: data.symbol,
|
|
1084
|
-
order_type: !!data.order_type_ext ? data.order_type_ext : data.order_type,
|
|
1123
|
+
order_type: data.order_type === types.OrderType.LIMIT ? !!data.order_type_ext ? data.order_type_ext : data.order_type : data.order_type,
|
|
1085
1124
|
side: data.side,
|
|
1086
1125
|
// reduce_only: data.reduce_only,
|
|
1087
1126
|
order_quantity: data.order_quantity
|
|
@@ -1094,25 +1133,41 @@ var BaseOrderCreator = class {
|
|
|
1094
1133
|
baseValidate(values, configs) {
|
|
1095
1134
|
const errors = {};
|
|
1096
1135
|
const { maxQty } = configs;
|
|
1136
|
+
console.log("baseValidate", values, configs);
|
|
1097
1137
|
const { order_quantity, total } = values;
|
|
1098
1138
|
if (!order_quantity) {
|
|
1099
|
-
errors.order_quantity =
|
|
1139
|
+
errors.order_quantity = {
|
|
1140
|
+
type: "required",
|
|
1141
|
+
message: "quantity is required"
|
|
1142
|
+
};
|
|
1100
1143
|
} else {
|
|
1101
1144
|
const { base_max, base_min } = configs.symbol;
|
|
1102
1145
|
const qty = new utils.Decimal(order_quantity);
|
|
1103
1146
|
if (qty.lt(base_min)) {
|
|
1104
|
-
errors.order_quantity =
|
|
1147
|
+
errors.order_quantity = {
|
|
1148
|
+
type: "min",
|
|
1149
|
+
message: `quantity must be greater than ${base_min}`
|
|
1150
|
+
};
|
|
1105
1151
|
} else if (qty.gt(maxQty)) {
|
|
1106
|
-
errors.order_quantity =
|
|
1152
|
+
errors.order_quantity = {
|
|
1153
|
+
type: "max",
|
|
1154
|
+
message: `quantity must be less than ${maxQty}`
|
|
1155
|
+
};
|
|
1107
1156
|
}
|
|
1108
1157
|
}
|
|
1109
1158
|
if (!!total) {
|
|
1110
1159
|
const { quote_max, quote_min } = configs.symbol;
|
|
1111
1160
|
const totalNumber = new utils.Decimal(total);
|
|
1112
1161
|
if (totalNumber.lt(quote_min)) {
|
|
1113
|
-
errors.total =
|
|
1162
|
+
errors.total = {
|
|
1163
|
+
type: "min",
|
|
1164
|
+
message: `Quantity should be greater or equal than ${quote_min}`
|
|
1165
|
+
};
|
|
1114
1166
|
} else if (totalNumber.gt(quote_max)) {
|
|
1115
|
-
errors.total =
|
|
1167
|
+
errors.total = {
|
|
1168
|
+
type: "max",
|
|
1169
|
+
message: `Quantity should be less or equal than ${quote_max}`
|
|
1170
|
+
};
|
|
1116
1171
|
}
|
|
1117
1172
|
}
|
|
1118
1173
|
return Promise.resolve(errors);
|
|
@@ -1128,7 +1183,10 @@ var LimitOrderCreator = class extends BaseOrderCreator {
|
|
|
1128
1183
|
return this.baseValidate(values, config).then((errors) => {
|
|
1129
1184
|
const { order_price } = values;
|
|
1130
1185
|
if (!order_price) {
|
|
1131
|
-
errors.order_price =
|
|
1186
|
+
errors.order_price = {
|
|
1187
|
+
type: "required",
|
|
1188
|
+
message: "price is required"
|
|
1189
|
+
};
|
|
1132
1190
|
} else {
|
|
1133
1191
|
const price = new utils.Decimal(order_price);
|
|
1134
1192
|
const { symbol } = config;
|
|
@@ -1136,9 +1194,15 @@ var LimitOrderCreator = class extends BaseOrderCreator {
|
|
|
1136
1194
|
const maxPriceNumber = maxPrice(config.markPrice, price_range);
|
|
1137
1195
|
const minPriceNumber = minPrice(config.markPrice, price_range);
|
|
1138
1196
|
if (price.lt(minPriceNumber)) {
|
|
1139
|
-
errors.order_price =
|
|
1197
|
+
errors.order_price = {
|
|
1198
|
+
type: "min",
|
|
1199
|
+
message: `price must be greater than ${minPriceNumber}`
|
|
1200
|
+
};
|
|
1140
1201
|
} else if (price.gt(maxPriceNumber)) {
|
|
1141
|
-
errors.order_price =
|
|
1202
|
+
errors.order_price = {
|
|
1203
|
+
type: "max",
|
|
1204
|
+
message: `price must be less than ${maxPriceNumber}`
|
|
1205
|
+
};
|
|
1142
1206
|
}
|
|
1143
1207
|
}
|
|
1144
1208
|
return errors;
|
|
@@ -1147,7 +1211,9 @@ var LimitOrderCreator = class extends BaseOrderCreator {
|
|
|
1147
1211
|
};
|
|
1148
1212
|
var MarketOrderCreator = class extends BaseOrderCreator {
|
|
1149
1213
|
create(values) {
|
|
1150
|
-
|
|
1214
|
+
const data = this.baseOrder(values);
|
|
1215
|
+
delete data["order_price"];
|
|
1216
|
+
return __spreadValues({}, data);
|
|
1151
1217
|
}
|
|
1152
1218
|
validate(values, configs) {
|
|
1153
1219
|
return this.baseValidate(values, configs);
|
|
@@ -1171,8 +1237,8 @@ var GeneralOrderCreator = class extends BaseOrderCreator {
|
|
|
1171
1237
|
}
|
|
1172
1238
|
};
|
|
1173
1239
|
var OrderFactory = class {
|
|
1174
|
-
static create(
|
|
1175
|
-
switch (
|
|
1240
|
+
static create(type) {
|
|
1241
|
+
switch (type) {
|
|
1176
1242
|
case types.OrderType.LIMIT:
|
|
1177
1243
|
return new LimitOrderCreator();
|
|
1178
1244
|
case types.OrderType.MARKET:
|
|
@@ -1190,8 +1256,8 @@ var OrderFactory = class {
|
|
|
1190
1256
|
};
|
|
1191
1257
|
|
|
1192
1258
|
// src/orderly/useOrderEntry.ts
|
|
1193
|
-
var useOrderEntry = (symbol,
|
|
1194
|
-
const
|
|
1259
|
+
var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
|
|
1260
|
+
const [mutation] = useMutation("/order");
|
|
1195
1261
|
const { freeCollateral } = useCollateral();
|
|
1196
1262
|
const symbolInfo = useSymbolsInfo();
|
|
1197
1263
|
const tokenInfo = useTokenInfo();
|
|
@@ -1202,97 +1268,29 @@ var useOrderEntry = (symbol, initialValue = {}, options) => {
|
|
|
1202
1268
|
const quoteDP = React.useMemo(() => {
|
|
1203
1269
|
return tokenInfo.USDC("decimals", 0);
|
|
1204
1270
|
}, [tokenInfo]);
|
|
1205
|
-
const
|
|
1206
|
-
(event$, state$) => {
|
|
1207
|
-
return event$.pipe(
|
|
1208
|
-
operators.withLatestFrom(state$),
|
|
1209
|
-
operators.map(([event, state]) => {
|
|
1210
|
-
const { field, value } = event;
|
|
1211
|
-
console.log("orderExtraValues", field, value);
|
|
1212
|
-
return [__spreadProps(__spreadValues({}, state[0]), { [field]: value })];
|
|
1213
|
-
})
|
|
1214
|
-
);
|
|
1215
|
-
},
|
|
1216
|
-
[
|
|
1217
|
-
{
|
|
1218
|
-
order_type: types.OrderType.MARKET,
|
|
1219
|
-
side: types.OrderSide.BUY,
|
|
1220
|
-
reduce_only: false
|
|
1221
|
-
}
|
|
1222
|
-
]
|
|
1223
|
-
);
|
|
1224
|
-
const ws = useWebSocketClient();
|
|
1225
|
-
const markPrice = rxjsHooks.useObservable(
|
|
1226
|
-
(_, input$) => input$.pipe(
|
|
1227
|
-
operators.switchMap(([symbol2]) => {
|
|
1228
|
-
return ws.observe(`${symbol2}@markprice`).pipe(
|
|
1229
|
-
operators.map((data) => data.price)
|
|
1230
|
-
// takeWhile(() => type === OrderType.MARKET)
|
|
1231
|
-
);
|
|
1232
|
-
})
|
|
1233
|
-
),
|
|
1234
|
-
0,
|
|
1235
|
-
[symbol]
|
|
1236
|
-
);
|
|
1237
|
-
const formik$1 = formik.useFormik({
|
|
1238
|
-
initialValues: __spreadValues({
|
|
1239
|
-
// order_type: OrderType.MARKET,
|
|
1240
|
-
// side: OrderSide.BUY,
|
|
1241
|
-
order_quantity: "",
|
|
1242
|
-
total: "",
|
|
1243
|
-
order_price: "",
|
|
1244
|
-
visible_quantity: 1
|
|
1245
|
-
}, initialValue),
|
|
1246
|
-
validate: (values) => {
|
|
1247
|
-
const creator = OrderFactory.create(orderExtraValues.order_type);
|
|
1248
|
-
return creator == null ? void 0 : creator.validate(values, {
|
|
1249
|
-
symbol: symbolInfo[symbol](),
|
|
1250
|
-
token: tokenInfo[symbol](),
|
|
1251
|
-
maxQty,
|
|
1252
|
-
markPrice
|
|
1253
|
-
});
|
|
1254
|
-
},
|
|
1255
|
-
onSubmit: (values) => {
|
|
1256
|
-
console.log(values);
|
|
1257
|
-
}
|
|
1258
|
-
});
|
|
1271
|
+
const { data: markPrice } = useMarkPrice(symbol);
|
|
1259
1272
|
const maxQty = useMaxQty(
|
|
1260
1273
|
symbol,
|
|
1261
|
-
|
|
1262
|
-
orderExtraValues.reduce_only
|
|
1274
|
+
side,
|
|
1275
|
+
// orderExtraValues.reduce_only
|
|
1276
|
+
reduceOnly
|
|
1263
1277
|
);
|
|
1264
|
-
const formFieldds = React.useMemo(() => {
|
|
1265
|
-
return ["order_quantity", "order_price", "total"];
|
|
1266
|
-
}, []);
|
|
1267
|
-
const setValue = (field, value) => {
|
|
1268
|
-
if (formFieldds.indexOf(field) < 0) {
|
|
1269
|
-
valuesUpdate({ field, value });
|
|
1270
|
-
return;
|
|
1271
|
-
}
|
|
1272
|
-
const fieldHandler = getCalculateHandler(field);
|
|
1273
|
-
const newValues = ramda.compose(
|
|
1274
|
-
ramda.head,
|
|
1275
|
-
orderEntityFormatHandle(),
|
|
1276
|
-
fieldHandler,
|
|
1277
|
-
baseInputHandle
|
|
1278
|
-
)([
|
|
1279
|
-
__spreadValues(__spreadValues({}, formik$1.values), orderExtraValues),
|
|
1280
|
-
field,
|
|
1281
|
-
value,
|
|
1282
|
-
markPrice,
|
|
1283
|
-
{ baseDP, quoteDP }
|
|
1284
|
-
]);
|
|
1285
|
-
formik$1.setValues(newValues, true);
|
|
1286
|
-
};
|
|
1287
1278
|
const onSubmit = (values) => {
|
|
1288
|
-
values
|
|
1289
|
-
if (typeof values.order_type === "undefined" || values.order_type !== types.OrderType.MARKET && values.order_type !== types.OrderType.LIMIT) {
|
|
1279
|
+
if (!values || typeof values.order_type === "undefined" || values.order_type !== types.OrderType.MARKET && values.order_type !== types.OrderType.LIMIT) {
|
|
1290
1280
|
throw new Error("order_type is error");
|
|
1291
1281
|
}
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1282
|
+
const orderCreator = OrderFactory.create(
|
|
1283
|
+
!!values.order_type_ext ? values.order_type_ext : values.order_type
|
|
1284
|
+
);
|
|
1285
|
+
if (!orderCreator) {
|
|
1286
|
+
return Promise.reject(new Error("orderCreator is null"));
|
|
1287
|
+
}
|
|
1288
|
+
return orderCreator == null ? void 0 : orderCreator.validate(values, {
|
|
1289
|
+
symbol: symbolInfo[symbol](),
|
|
1290
|
+
token: tokenInfo[symbol](),
|
|
1291
|
+
maxQty,
|
|
1292
|
+
markPrice
|
|
1293
|
+
}).then(() => {
|
|
1296
1294
|
if (!orderCreator) {
|
|
1297
1295
|
throw new Error("orderCreator is null");
|
|
1298
1296
|
}
|
|
@@ -1300,31 +1298,43 @@ var useOrderEntry = (symbol, initialValue = {}, options) => {
|
|
|
1300
1298
|
throw new Error("symbol is null");
|
|
1301
1299
|
}
|
|
1302
1300
|
const data = orderCreator.create(values);
|
|
1303
|
-
console.log("orderentry data:::", data);
|
|
1304
|
-
formik$1.setSubmitting(true);
|
|
1305
1301
|
return mutation(__spreadProps(__spreadValues({}, data), {
|
|
1306
1302
|
symbol
|
|
1307
|
-
}))
|
|
1308
|
-
|
|
1309
|
-
|
|
1303
|
+
}));
|
|
1304
|
+
});
|
|
1305
|
+
};
|
|
1306
|
+
const calculate = React.useCallback(
|
|
1307
|
+
(values, field, value) => {
|
|
1308
|
+
console.log("calculate", values, field, value, markPrice);
|
|
1309
|
+
const fieldHandler = getCalculateHandler(field);
|
|
1310
|
+
const newValues = ramda.compose(
|
|
1311
|
+
ramda.head,
|
|
1312
|
+
orderEntityFormatHandle(),
|
|
1313
|
+
fieldHandler,
|
|
1314
|
+
baseInputHandle
|
|
1315
|
+
)([values, field, value, markPrice, { baseDP, quoteDP }]);
|
|
1316
|
+
return newValues;
|
|
1317
|
+
},
|
|
1318
|
+
[markPrice]
|
|
1319
|
+
);
|
|
1320
|
+
const validator = (values) => {
|
|
1321
|
+
const creator = OrderFactory.create(values.order_type);
|
|
1322
|
+
return creator == null ? void 0 : creator.validate(values, {
|
|
1323
|
+
symbol: symbolInfo[symbol](),
|
|
1324
|
+
token: tokenInfo[symbol](),
|
|
1325
|
+
maxQty,
|
|
1326
|
+
markPrice
|
|
1310
1327
|
});
|
|
1311
1328
|
};
|
|
1312
|
-
React.useEffect(() => {
|
|
1313
|
-
formik$1.resetForm();
|
|
1314
|
-
}, [symbol]);
|
|
1315
1329
|
return {
|
|
1316
1330
|
maxQty,
|
|
1317
|
-
// formState,
|
|
1318
|
-
values: __spreadValues(__spreadValues({}, formik$1.values), orderExtraValues),
|
|
1319
|
-
errors: formik$1.errors,
|
|
1320
1331
|
freeCollateral,
|
|
1321
1332
|
markPrice,
|
|
1322
|
-
setValue,
|
|
1323
1333
|
onSubmit,
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1334
|
+
helper: {
|
|
1335
|
+
calculate,
|
|
1336
|
+
validator
|
|
1337
|
+
},
|
|
1328
1338
|
symbolConfig: symbolInfo[symbol]()
|
|
1329
1339
|
};
|
|
1330
1340
|
};
|
|
@@ -1367,35 +1377,49 @@ var useAccountInfo = () => {
|
|
|
1367
1377
|
return usePrivateQuery("/client/info");
|
|
1368
1378
|
};
|
|
1369
1379
|
var useMarketsStream = () => {
|
|
1370
|
-
const ws =
|
|
1371
|
-
const { data } = useQuery(`/public/futures`);
|
|
1372
|
-
const
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1380
|
+
const ws = useWS();
|
|
1381
|
+
const { data: futures } = useQuery(`/public/futures`);
|
|
1382
|
+
const { data: tickers } = useSWRSubscription__default.default("tickers", (_, { next }) => {
|
|
1383
|
+
const unsubscribe = ws.subscribe(
|
|
1384
|
+
// { event: "subscribe", topic: "markprices" },
|
|
1385
|
+
"tickers",
|
|
1386
|
+
{
|
|
1387
|
+
onMessage: (message) => {
|
|
1388
|
+
next(null, message);
|
|
1379
1389
|
}
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1390
|
+
// onUnsubscribe: () => {
|
|
1391
|
+
// return "markprices";
|
|
1392
|
+
// },
|
|
1393
|
+
// onError: (error: any) => {
|
|
1394
|
+
// console.log("error", error);
|
|
1395
|
+
// },
|
|
1396
|
+
}
|
|
1397
|
+
);
|
|
1398
|
+
return () => {
|
|
1399
|
+
console.log("unsubscribe!!!!!!!");
|
|
1400
|
+
unsubscribe == null ? void 0 : unsubscribe();
|
|
1401
|
+
};
|
|
1402
|
+
});
|
|
1403
|
+
const value = React.useMemo(() => {
|
|
1404
|
+
if (!futures)
|
|
1405
|
+
return null;
|
|
1406
|
+
if (!tickers)
|
|
1407
|
+
return futures;
|
|
1408
|
+
return futures.map((item) => {
|
|
1409
|
+
const ticker = tickers.find(
|
|
1410
|
+
(t) => t.symbol === item.symbol
|
|
1411
|
+
);
|
|
1412
|
+
if (ticker) {
|
|
1413
|
+
return __spreadProps(__spreadValues({}, item), {
|
|
1414
|
+
["24h_close"]: ticker.close,
|
|
1415
|
+
["24h_open"]: ticker.open,
|
|
1416
|
+
["24h_volumn"]: ticker.volume,
|
|
1417
|
+
change: 0
|
|
1393
1418
|
});
|
|
1394
|
-
}
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
);
|
|
1419
|
+
}
|
|
1420
|
+
return item;
|
|
1421
|
+
});
|
|
1422
|
+
}, [futures, tickers]);
|
|
1399
1423
|
return { data: value };
|
|
1400
1424
|
};
|
|
1401
1425
|
var useFundingRate = (symbol) => {
|
|
@@ -1436,10 +1460,10 @@ var useFundingRate = (symbol) => {
|
|
|
1436
1460
|
var fetcher3 = (url, init) => net.get(url, init);
|
|
1437
1461
|
var usePrivateInfiniteQuery = (getKey, options) => {
|
|
1438
1462
|
var _a;
|
|
1439
|
-
const
|
|
1463
|
+
const account4 = useAccount();
|
|
1440
1464
|
const middleware = Array.isArray(options == null ? void 0 : options.use) ? (_a = options == null ? void 0 : options.use) != null ? _a : [] : [];
|
|
1441
1465
|
const result = useSWRInfinite__default.default(
|
|
1442
|
-
(pageIndex, previousPageData) =>
|
|
1466
|
+
(pageIndex, previousPageData) => account4.state.status >= types.AccountStatusEnum.EnableTrading ? getKey(pageIndex, previousPageData) : null,
|
|
1443
1467
|
fetcher3,
|
|
1444
1468
|
__spreadProps(__spreadValues({}, options), {
|
|
1445
1469
|
use: [signatureMiddleware, ...middleware]
|
|
@@ -1484,7 +1508,6 @@ var useOrderStream = ({
|
|
|
1484
1508
|
if (!res.data) {
|
|
1485
1509
|
return null;
|
|
1486
1510
|
}
|
|
1487
|
-
console.log("orders:::", markPrices);
|
|
1488
1511
|
return (_a = res.data) == null ? void 0 : _a.flat().map((item) => {
|
|
1489
1512
|
var _a2;
|
|
1490
1513
|
return __spreadProps(__spreadValues({}, item), {
|
|
@@ -1514,6 +1537,77 @@ var useOrderStream = ({
|
|
|
1514
1537
|
];
|
|
1515
1538
|
};
|
|
1516
1539
|
|
|
1540
|
+
// src/orderly/useTradeStream.ts
|
|
1541
|
+
var useTradeStream = (symbol) => {
|
|
1542
|
+
if (!symbol) {
|
|
1543
|
+
throw new Error("useTradeStream: symbol is required");
|
|
1544
|
+
}
|
|
1545
|
+
const { data, isLoading } = useQuery(
|
|
1546
|
+
`/public/market_trades?symbol=${symbol}&limit=20`
|
|
1547
|
+
);
|
|
1548
|
+
return { data, isLoading };
|
|
1549
|
+
};
|
|
1550
|
+
var useMarginRatio = () => {
|
|
1551
|
+
const [{ rows }] = usePositionStream();
|
|
1552
|
+
const { data: markPrices } = useMarkPricesStream();
|
|
1553
|
+
const { totalCollateral } = useCollateral();
|
|
1554
|
+
const marginRatio = React.useMemo(() => {
|
|
1555
|
+
const ratio = futures.account.totalMarginRatio({
|
|
1556
|
+
totalCollateral,
|
|
1557
|
+
markPrices,
|
|
1558
|
+
positions: rows != null ? rows : []
|
|
1559
|
+
});
|
|
1560
|
+
return ratio;
|
|
1561
|
+
}, [rows, markPrices, totalCollateral]);
|
|
1562
|
+
return marginRatio;
|
|
1563
|
+
};
|
|
1564
|
+
var useChains = (networkId, options) => {
|
|
1565
|
+
const field = options == null ? void 0 : options.pick;
|
|
1566
|
+
const { data } = useSWR__default.default(
|
|
1567
|
+
"https://fi-api.woo.org/swap_support",
|
|
1568
|
+
(url) => fetch(url).then((res) => res.json()),
|
|
1569
|
+
{
|
|
1570
|
+
revalidateOnFocus: false,
|
|
1571
|
+
revalidateOnReconnect: false
|
|
1572
|
+
}
|
|
1573
|
+
);
|
|
1574
|
+
const chains = React.useMemo(() => {
|
|
1575
|
+
if (!data || !data.data)
|
|
1576
|
+
return data;
|
|
1577
|
+
let arr = [];
|
|
1578
|
+
Object.keys(data.data).forEach((key) => {
|
|
1579
|
+
const item = data.data[key];
|
|
1580
|
+
arr.push(__spreadProps(__spreadValues({}, item), {
|
|
1581
|
+
name: key
|
|
1582
|
+
}));
|
|
1583
|
+
});
|
|
1584
|
+
if (networkId === "mainnet") {
|
|
1585
|
+
arr = arr.filter((item) => item.network_infos.mainnet);
|
|
1586
|
+
}
|
|
1587
|
+
if (networkId === "testnet") {
|
|
1588
|
+
arr = arr.filter((item) => !item.network_infos.mainnet);
|
|
1589
|
+
}
|
|
1590
|
+
if (typeof (options == null ? void 0 : options.filter) === "function") {
|
|
1591
|
+
arr = arr.filter(options.filter);
|
|
1592
|
+
}
|
|
1593
|
+
if (typeof field !== "undefined") {
|
|
1594
|
+
return arr.map((item) => {
|
|
1595
|
+
return item[field];
|
|
1596
|
+
});
|
|
1597
|
+
}
|
|
1598
|
+
return arr;
|
|
1599
|
+
}, [data, networkId, field, options]);
|
|
1600
|
+
const findByChainId = React.useCallback(
|
|
1601
|
+
(chainId) => {
|
|
1602
|
+
if (!data || !data.data)
|
|
1603
|
+
return void 0;
|
|
1604
|
+
return data.data[chainId];
|
|
1605
|
+
},
|
|
1606
|
+
[data]
|
|
1607
|
+
);
|
|
1608
|
+
return [chains, { findByChainId }];
|
|
1609
|
+
};
|
|
1610
|
+
|
|
1517
1611
|
// src/apis/index.ts
|
|
1518
1612
|
var apis_exports = {};
|
|
1519
1613
|
__export(apis_exports, {
|
|
@@ -1572,16 +1666,19 @@ exports.apis = apis_exports;
|
|
|
1572
1666
|
exports.useAccount = useAccount;
|
|
1573
1667
|
exports.useAccountInfo = useAccountInfo;
|
|
1574
1668
|
exports.useAppState = useAppState;
|
|
1669
|
+
exports.useChains = useChains;
|
|
1575
1670
|
exports.useCollateral = useCollateral;
|
|
1576
1671
|
exports.useFetures = useFetures;
|
|
1577
1672
|
exports.useFundingRate = useFundingRate;
|
|
1673
|
+
exports.useMarginRatio = useMarginRatio;
|
|
1674
|
+
exports.useMarkPrice = useMarkPrice;
|
|
1578
1675
|
exports.useMarkPricesStream = useMarkPricesStream;
|
|
1579
1676
|
exports.useMarketsStream = useMarketsStream;
|
|
1580
1677
|
exports.useMaxQty = useMaxQty;
|
|
1581
1678
|
exports.useMutation = useMutation;
|
|
1582
1679
|
exports.useOrderEntry = useOrderEntry;
|
|
1583
1680
|
exports.useOrderStream = useOrderStream;
|
|
1584
|
-
exports.
|
|
1681
|
+
exports.useOrderbookStream = useOrderbookStream;
|
|
1585
1682
|
exports.usePositionStream = usePositionStream;
|
|
1586
1683
|
exports.usePrivateObserve = usePrivateObserve;
|
|
1587
1684
|
exports.usePrivateQuery = usePrivateQuery;
|
|
@@ -1590,6 +1687,7 @@ exports.useSymbolsInfo = useSymbolsInfo;
|
|
|
1590
1687
|
exports.useTickerStream = useTickerStream;
|
|
1591
1688
|
exports.useTokenInfo = useTokenInfo;
|
|
1592
1689
|
exports.useTopicObserve = useTopicObserve;
|
|
1690
|
+
exports.useTradeStream = useTradeStream;
|
|
1593
1691
|
exports.useTradingView = useTradingView;
|
|
1594
1692
|
exports.useWS = useWS;
|
|
1595
1693
|
exports.useWebSocketClient = useWebSocketClient;
|