@orderly.network/hooks 0.0.71 → 0.0.73

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.mjs CHANGED
@@ -51,7 +51,6 @@ var useLazyQuery = (query, options) => {
51
51
  return useSWRMutation(
52
52
  query,
53
53
  (url, options2) => {
54
- console.log(url, options2);
55
54
  url = url.startsWith("http") ? url : `${apiBaseUrl}${url}`;
56
55
  if (options2?.arg) {
57
56
  const queryString = Object.entries(options2.arg).map(
@@ -255,7 +254,6 @@ var usePrivateQuery = (query, options) => {
255
254
  ...swrOptions,
256
255
  use: [signatureMiddleware, ...middleware],
257
256
  onError: (err) => {
258
- console.log("usePrivateQuery error", err);
259
257
  }
260
258
  }
261
259
  );
@@ -325,7 +323,6 @@ function parseJSON(value) {
325
323
  try {
326
324
  return value === "undefined" ? void 0 : JSON.parse(value ?? "");
327
325
  } catch {
328
- console.log("parsing error on", { value });
329
326
  return void 0;
330
327
  }
331
328
  }
@@ -434,7 +431,6 @@ var useRunOnce = ({ fn, sessionKey }) => {
434
431
  };
435
432
  var DataSourceContext = createContext({});
436
433
  var DataSourceProvider = (props) => {
437
- console.log("render DataSourceProvider");
438
434
  useAccountInstance();
439
435
  useConstant(() => {
440
436
  });
@@ -500,12 +496,11 @@ var useTickerStream = (symbol) => {
500
496
  // return "markprices";
501
497
  // },
502
498
  // onError: (error: any) => {
503
- // console.log("error", error);
499
+ //
504
500
  // },
505
501
  }
506
502
  );
507
503
  return () => {
508
- console.log("unsubscribe!!!!!!!");
509
504
  unsubscribe?.();
510
505
  };
511
506
  }
@@ -541,7 +536,6 @@ var useMarkPrice = (symbol) => {
541
536
  }
542
537
  });
543
538
  return () => {
544
- console.log("unsubscribe useMarkPrice !!!!!!!");
545
539
  unsubscribe?.();
546
540
  };
547
541
  });
@@ -662,7 +656,6 @@ var mergeItems = (data, update) => {
662
656
  } else {
663
657
  if (quantity === 0) {
664
658
  data.splice(index, 1);
665
- continue;
666
659
  } else {
667
660
  data[index] = item;
668
661
  }
@@ -672,9 +665,19 @@ var mergeItems = (data, update) => {
672
665
  return data;
673
666
  };
674
667
  var mergeOrderbook = (data, update) => {
668
+ const asks = mergeItems(data.asks, update.asks).sort(asksSortFn);
669
+ const bids = mergeItems(data.bids, update.bids).sort(bidsSortFn);
670
+ if (asks.length > 0) {
671
+ console.log("find first", asks[0], bids[0]);
672
+ const firstPrice = asks[0][0];
673
+ const index = bids.findIndex((item) => item[0] < firstPrice);
674
+ if (index > 0) {
675
+ bids.splice(0, index + 1);
676
+ }
677
+ }
675
678
  return {
676
- asks: mergeItems(data.asks, update.asks).sort(asksSortFn),
677
- bids: mergeItems(data.bids, update.bids).sort(bidsSortFn)
679
+ asks,
680
+ bids
678
681
  };
679
682
  };
680
683
  var useOrderbookStream = (symbol, initial = { asks: [], bids: [] }, options) => {
@@ -950,12 +953,10 @@ var useMarkPricesStream = () => {
950
953
  // return "markprices";
951
954
  // },
952
955
  onError: (error) => {
953
- console.log("error", error);
954
956
  }
955
957
  }
956
958
  );
957
959
  return () => {
958
- console.log("unsubscribe!!!!!!!");
959
960
  unsubscribe?.();
960
961
  };
961
962
  });
@@ -1005,7 +1006,6 @@ var usePositionStream = (symbol, options) => {
1005
1006
  ...options,
1006
1007
  formatter: (data2) => data2,
1007
1008
  onError: (err) => {
1008
- console.log("usePositionStream error", err);
1009
1009
  }
1010
1010
  });
1011
1011
  const { data: markPrices } = useMarkPricesStream();
@@ -1377,9 +1377,9 @@ var BaseOrderCreator = class {
1377
1377
  if (totalNumber.lt(quote_min)) {
1378
1378
  errors.total = {
1379
1379
  type: "min",
1380
- message: `Quantity should be greater or equal than ${new Decimal(
1381
- quote_min
1382
- ).todp(quote_dp)}`
1380
+ message: `Quantity must be at least ${new Decimal(quote_min).todp(
1381
+ quote_dp
1382
+ )}`
1383
1383
  };
1384
1384
  } else if (totalNumber.gt(quote_max)) {
1385
1385
  errors.total = {
@@ -1533,7 +1533,6 @@ var useOrderEntry = (symbol, side, reduceOnly = false, options) => {
1533
1533
  };
1534
1534
  const calculate = useCallback(
1535
1535
  (values, field, value) => {
1536
- console.log("calculate", values, field, value, markPrice);
1537
1536
  const fieldHandler = getCalculateHandler(field);
1538
1537
  const newValues = compose(
1539
1538
  head,
@@ -1642,12 +1641,11 @@ var useMarketsStream = () => {
1642
1641
  // return "markprices";
1643
1642
  // },
1644
1643
  // onError: (error: any) => {
1645
- // console.log("error", error);
1644
+ //
1646
1645
  // },
1647
1646
  }
1648
1647
  );
1649
1648
  return () => {
1650
- console.log("unsubscribe!!!!!!!");
1651
1649
  unsubscribe?.();
1652
1650
  };
1653
1651
  });
@@ -3119,7 +3117,6 @@ var useWithdraw = () => {
3119
3117
  const withdraw = useCallback(
3120
3118
  (inputs) => {
3121
3119
  return account5.assetsManager.withdraw(inputs).then((res) => {
3122
- console.log("---------- withdraw -------", res);
3123
3120
  return res;
3124
3121
  });
3125
3122
  },
@@ -3146,6 +3143,7 @@ var useDeposit = (options) => {
3146
3143
  const [allowance, setAllowance] = useState("0");
3147
3144
  const { account: account5, state } = useAccount();
3148
3145
  const prevAddress = useRef();
3146
+ const getBalanceListener = useRef();
3149
3147
  const dst = useMemo(() => {
3150
3148
  const chain = onlyTestnet ? findByChainId(ARBITRUM_TESTNET_CHAINID) : findByChainId(ARBITRUM_MAINNET_CHAINID);
3151
3149
  const USDC = chain?.token_infos.find((token) => token.symbol === "USDC");
@@ -3188,7 +3186,6 @@ var useDeposit = (options) => {
3188
3186
  return;
3189
3187
  setBalanceRevalidating(true);
3190
3188
  const balance2 = await fetchBalanceHandler(address, decimals);
3191
- console.log("----- refresh balance -----", balance2);
3192
3189
  setBalance(() => balance2);
3193
3190
  setBalanceRevalidating(false);
3194
3191
  } catch (e) {
@@ -3201,15 +3198,13 @@ var useDeposit = (options) => {
3201
3198
  );
3202
3199
  const fetchBalances = useCallback(async (tokens) => {
3203
3200
  const tasks = [];
3204
- console.log("fetch balances ---->>>>", tokens);
3205
3201
  for (const token of tokens) {
3206
3202
  if (isNativeTokenChecker(token.address)) {
3207
3203
  continue;
3208
3204
  }
3209
3205
  tasks.push(account5.assetsManager.getBalanceByAddress(token.address));
3210
3206
  }
3211
- const balances = await Promise.all(tasks);
3212
- console.log("----- get balances from tokens -----", balances);
3207
+ await Promise.all(tasks);
3213
3208
  }, []);
3214
3209
  const getAllowance = async (address, vaultAddress) => {
3215
3210
  const key = `${address}-${vaultAddress}`;
@@ -3234,10 +3229,6 @@ var useDeposit = (options) => {
3234
3229
  return;
3235
3230
  prevAddress.current = address;
3236
3231
  const allowance2 = await account5.assetsManager.getAllowance(address);
3237
- console.log(
3238
- "----- refresh allowance only orderly -----",
3239
- { allowance: allowance2 }
3240
- );
3241
3232
  setAllowance(() => allowance2);
3242
3233
  };
3243
3234
  const getVaultAddress = useCallback(() => {
@@ -3297,6 +3288,26 @@ var useDeposit = (options) => {
3297
3288
  },
3298
3289
  [account5, fetchBalance, getAllowance]
3299
3290
  );
3291
+ const loopGetBalance = async () => {
3292
+ getBalanceListener.current && clearTimeout(getBalanceListener.current);
3293
+ getBalanceListener.current = setTimeout(async () => {
3294
+ const balance2 = await fetchBalanceHandler(
3295
+ options?.address,
3296
+ options?.decimals
3297
+ );
3298
+ setBalance(balance2);
3299
+ loopGetBalance();
3300
+ }, 3e3);
3301
+ };
3302
+ useEffect(() => {
3303
+ if (!options?.address) {
3304
+ return;
3305
+ }
3306
+ loopGetBalance();
3307
+ return () => {
3308
+ getBalanceListener.current && clearTimeout(getBalanceListener.current);
3309
+ };
3310
+ }, [options?.address, options?.decimals]);
3300
3311
  return {
3301
3312
  dst,
3302
3313
  balance,
@@ -3335,7 +3346,6 @@ var useBalance = () => {
3335
3346
  const { data } = useSWRSubscription("balance", (_, { next }) => {
3336
3347
  const unsubscribe = ws.privateSubscribe("balance", {
3337
3348
  onMessage: (data2) => {
3338
- console.log(data2);
3339
3349
  }
3340
3350
  });
3341
3351
  return () => unsubscribe();
@@ -3375,7 +3385,6 @@ var usePrivateDataObserver = () => {
3375
3385
  }
3376
3386
  return orders;
3377
3387
  }).catch((error) => {
3378
- console.log("error", error, error.stack);
3379
3388
  });
3380
3389
  });
3381
3390
  ee.emit("orders:changed");
@@ -3384,7 +3393,6 @@ var usePrivateDataObserver = () => {
3384
3393
  return () => unsubscribe?.();
3385
3394
  }, [state.accountId]);
3386
3395
  useEffect(() => {
3387
- console.log("subscribe: position: %s", state.accountId);
3388
3396
  if (!state.accountId)
3389
3397
  return;
3390
3398
  const key = ["/v1/positions", state.accountId];
@@ -3429,7 +3437,6 @@ var usePrivateDataObserver = () => {
3429
3437
  }
3430
3438
  });
3431
3439
  return () => {
3432
- console.log("unsubscribe: private subscription position");
3433
3440
  unsubscribe?.();
3434
3441
  };
3435
3442
  }, [state.accountId]);
@@ -3439,7 +3446,6 @@ var useExecutionReport = () => {
3439
3446
  const { data } = useSWRSubscription("executionreport", (_, { next }) => {
3440
3447
  const unsubscribe = ws.privateSubscribe("executionreport", {
3441
3448
  onMessage: (data2) => {
3442
- console.log(data2);
3443
3449
  }
3444
3450
  });
3445
3451
  return () => unsubscribe();
@@ -3480,7 +3486,6 @@ var useWooSwapQuery = () => {
3480
3486
  const [loading, { setTrue: start, setFalse: stop }] = useBoolean(false);
3481
3487
  const query = useCallback(
3482
3488
  (inputs) => {
3483
- console.log("inputs", inputs);
3484
3489
  if (loading)
3485
3490
  return;
3486
3491
  start();
@@ -3488,7 +3493,7 @@ var useWooSwapQuery = () => {
3488
3493
  // src_network: inputs.srcNetwork,
3489
3494
  network: "arbitrum",
3490
3495
  from_token: inputs.srcToken,
3491
- to_token: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
3496
+ to_token: inputs.dstToken,
3492
3497
  //account.assetsManager.usdcAddress,
3493
3498
  from_amount: inputs.amount,
3494
3499
  //inputs.amount,
@@ -3554,10 +3559,10 @@ var useWooCrossSwapQuery = () => {
3554
3559
  start();
3555
3560
  const params = {
3556
3561
  // src_network: inputs.srcNetwork,
3557
- src_network: "base",
3562
+ src_network: inputs.srcNetwork.toLowerCase(),
3558
3563
  dst_network: "arbitrum",
3559
3564
  src_token: inputs.srcToken,
3560
- dst_token: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
3565
+ dst_token: inputs.dstToken,
3561
3566
  //account.assetsManager.usdcAddress,
3562
3567
  src_amount: inputs.amount,
3563
3568
  //inputs.amount,
@@ -3589,7 +3594,6 @@ var useWooCrossSwapQuery = () => {
3589
3594
  minToAmount: BigInt(swapInfo.dst_infos.min_to_amount),
3590
3595
  orderlyNativeFees: 0n
3591
3596
  }).then((data) => {
3592
- console.log("res::::", data);
3593
3597
  return {
3594
3598
  ...swapInfo,
3595
3599
  dst_infos: {
@@ -3612,6 +3616,7 @@ var useCrossSwap = () => {
3612
3616
  const [layerStatus, setLayerStatus] = useState(
3613
3617
  "WAITTING" /* INITIALIZING */
3614
3618
  );
3619
+ const ee = useEventEmitter();
3615
3620
  const [bridgeMessage, setBridgeMessage] = useState();
3616
3621
  const [status, setStatus] = useState(
3617
3622
  WS_WalletStatusEnum.NO
@@ -3621,18 +3626,21 @@ var useCrossSwap = () => {
3621
3626
  const { networkId, configStore } = useContext(OrderlyContext);
3622
3627
  const client = useRef(createClient(networkId)).current;
3623
3628
  const timer = useRef();
3624
- useWalletSubscription({
3625
- onMessage: (message) => {
3626
- const { side, transStatus, trxId } = message;
3629
+ useEffect(() => {
3630
+ const handler = (data) => {
3631
+ const { side, transStatus, trxId } = data;
3627
3632
  if (side === "DEPOSIT" && trxId === txHashFromBridge.current) {
3628
3633
  setStatus(transStatus);
3629
3634
  }
3630
- }
3631
- });
3635
+ };
3636
+ ee.on("wallet:changed", handler);
3637
+ return () => {
3638
+ ee.off("wallet:changed", handler);
3639
+ };
3640
+ }, [txHashFromBridge.current]);
3632
3641
  const checkLayerStatus = useCallback((txHash) => {
3633
3642
  const check = async (txHash2) => {
3634
3643
  const { messages } = await client.getMessagesBySrcTxHash(txHash2);
3635
- console.log("messages:", messages);
3636
3644
  if (messages.length > 0) {
3637
3645
  const { status: status2 } = messages[0];
3638
3646
  if (status2 === "INFLIGHT" /* INFLIGHT */) {
@@ -3707,7 +3715,6 @@ var useCrossSwap = () => {
3707
3715
  checkLayerStatus(result.hash);
3708
3716
  return pick(["from", "to", "hash", "value"], result);
3709
3717
  } catch (e) {
3710
- console.log("swap error:", e);
3711
3718
  stop();
3712
3719
  throw new Error(e.errorCode);
3713
3720
  }
@@ -4002,16 +4009,21 @@ var useSwap = () => {
4002
4009
  WS_WalletStatusEnum.NO
4003
4010
  );
4004
4011
  const txHash = useRef();
4005
- useWalletSubscription({
4006
- onMessage: (message) => {
4007
- const { side, transStatus, trxId } = message;
4012
+ const ee = useEventEmitter();
4013
+ useEffect(() => {
4014
+ const handler = (data) => {
4015
+ const { side, transStatus, trxId } = data;
4008
4016
  if (side === "DEPOSIT" && trxId === txHash.current) {
4009
4017
  setStatus(transStatus);
4010
4018
  }
4011
- }
4012
- });
4019
+ };
4020
+ ee.on("wallet:changed", handler);
4021
+ return () => {
4022
+ ee.off("wallet:changed", handler);
4023
+ };
4024
+ }, [txHash.current]);
4013
4025
  const dstValutDeposit = useCallback(() => {
4014
- const brokerId = configStore.get("onlyTestnet") ? "woofi_dex" : "woofi_pro";
4026
+ const brokerId = configStore.get("brokerId");
4015
4027
  return {
4016
4028
  accountId: account5.accountIdHashStr,
4017
4029
  brokerHash: utils.parseBrokerHash(brokerId),
@@ -4045,11 +4057,9 @@ var useSwap = () => {
4045
4057
  }
4046
4058
  );
4047
4059
  stop();
4048
- console.log("single swap result", result);
4049
4060
  txHash.current = result.hash;
4050
4061
  return pick(["from", "to", "hash", "value"], result);
4051
4062
  } catch (e) {
4052
- console.log("\u8C03\u7528\u5408\u7EA6\u62A5\u9519\uFF1A", e);
4053
4063
  throw new Error(e.errorCode);
4054
4064
  }
4055
4065
  },