@0xsquid/react-hooks 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/.babelrc +7 -0
  2. package/.eslintignore +1 -0
  3. package/.eslintrc.json +44 -0
  4. package/dist/index.js +6 -0
  5. package/jest.config.ts +28 -0
  6. package/package.json +124 -0
  7. package/scripts/copyAssets.js +27 -0
  8. package/scripts/removeBuildTestFolder.js +28 -0
  9. package/scripts/replacePaths.js +37 -0
  10. package/src/assets/images/icons/wallets/binance.svg +4 -0
  11. package/src/assets/images/icons/wallets/bitget.svg +43 -0
  12. package/src/assets/images/icons/wallets/bitkeep.svg +9 -0
  13. package/src/assets/images/icons/wallets/blockchaindotcom.svg +11 -0
  14. package/src/assets/images/icons/wallets/brave.svg +18 -0
  15. package/src/assets/images/icons/wallets/c98.svg +3 -0
  16. package/src/assets/images/icons/wallets/coinbase.svg +5 -0
  17. package/src/assets/images/icons/wallets/cosmostation.svg +9 -0
  18. package/src/assets/images/icons/wallets/defiConnect.svg +25 -0
  19. package/src/assets/images/icons/wallets/exodus.svg +33 -0
  20. package/src/assets/images/icons/wallets/fetchai.svg +1 -0
  21. package/src/assets/images/icons/wallets/index.ts +43 -0
  22. package/src/assets/images/icons/wallets/keplr.svg +9 -0
  23. package/src/assets/images/icons/wallets/leap.svg +35 -0
  24. package/src/assets/images/icons/wallets/metamask.svg +48 -0
  25. package/src/assets/images/icons/wallets/okx.svg +1 -0
  26. package/src/assets/images/icons/wallets/rabby.svg +1 -0
  27. package/src/assets/images/icons/wallets/trustwallet.svg +11 -0
  28. package/src/assets/images/icons/wallets/walletConnect.svg +11 -0
  29. package/src/assets/images/icons/wallets/wallet_icon.svg +1 -0
  30. package/src/assets/images/icons/wallets/xdefi.svg +9 -0
  31. package/src/assets/images/icons/wallets/zerion.svg +11 -0
  32. package/src/connectors/ExodusConnector.ts +6 -0
  33. package/src/connectors/LedgerLiveConnector.ts +226 -0
  34. package/src/contracts/typechain/ERC20.d.ts +441 -0
  35. package/src/contracts/typechain/factories/ERC20__factory.d.ts +56 -0
  36. package/src/contracts/typechain/factories/ERC20__factory.ts +340 -0
  37. package/src/core/constants.ts +549 -0
  38. package/src/core/externalLinks.ts +26 -0
  39. package/src/core/index.ts +1 -0
  40. package/src/core/multicall3.ts +22 -0
  41. package/src/core/numbers.ts +44 -0
  42. package/src/core/providers/CosmosProvider.tsx +55 -0
  43. package/src/core/queries/queries-keys.ts +156 -0
  44. package/src/core/queries/react-query-config.ts +13 -0
  45. package/src/core/types/components.ts +16 -0
  46. package/src/core/types/config.ts +52 -0
  47. package/src/core/types/dex.ts +31 -0
  48. package/src/core/types/error.ts +52 -0
  49. package/src/core/types/event.ts +16 -0
  50. package/src/core/types/route.ts +21 -0
  51. package/src/core/types/swap.ts +8 -0
  52. package/src/core/types/tokens.ts +30 -0
  53. package/src/core/types/transaction.ts +84 -0
  54. package/src/core/types/types.ts +37 -0
  55. package/src/core/types/wallet.ts +83 -0
  56. package/src/hooks/chains/useSquidChains.ts +66 -0
  57. package/src/hooks/cosmos/useCosmos.ts +279 -0
  58. package/src/hooks/cosmos/useCosmosForChain.ts +33 -0
  59. package/src/hooks/index.ts +32 -0
  60. package/src/hooks/navigation/useKeyboardNavigation.ts +88 -0
  61. package/src/hooks/store/useSquidStore.ts +80 -0
  62. package/src/hooks/swap/useSwap.ts +359 -0
  63. package/src/hooks/tokens/useAllTokensWithBalance.ts +157 -0
  64. package/src/hooks/tokens/useBalance.ts +180 -0
  65. package/src/hooks/tokens/useMultiChainBalance.ts +59 -0
  66. package/src/hooks/tokens/usePrices.ts +52 -0
  67. package/src/hooks/tokens/useSingleTokenPrice.ts +45 -0
  68. package/src/hooks/tokens/useSquidTokens.ts +48 -0
  69. package/src/hooks/tokens/useTokensWithBalance.ts +169 -0
  70. package/src/hooks/transaction/useEstimate.ts +397 -0
  71. package/src/hooks/transaction/useEstimatePriceImpact.ts +39 -0
  72. package/src/hooks/transaction/useExecuteTransaction.ts +440 -0
  73. package/src/hooks/transaction/useExpress.ts +46 -0
  74. package/src/hooks/transaction/useGetRoute.ts +145 -0
  75. package/src/hooks/transaction/useSingleTransaction.ts +155 -0
  76. package/src/hooks/transaction/useTransaction.ts +734 -0
  77. package/src/hooks/user/useUserParams.ts +63 -0
  78. package/src/hooks/wallet/useAddToken.ts +53 -0
  79. package/src/hooks/wallet/useAutoConnect.ts +56 -0
  80. package/src/hooks/wallet/useGnosisContext.ts +84 -0
  81. package/src/hooks/wallet/useIntegratorContext.ts +44 -0
  82. package/src/hooks/wallet/useMultiChainWallet.ts +146 -0
  83. package/src/hooks/wallet/useWallet.ts +86 -0
  84. package/src/hooks/webapp-router/useSquidRouter.ts +42 -0
  85. package/src/index.ts +5 -0
  86. package/src/provider/index.tsx +105 -0
  87. package/src/react-app-env.d.ts +2 -0
  88. package/src/services/external/analyticsService.ts +46 -0
  89. package/src/services/external/coingeckoService.ts +35 -0
  90. package/src/services/external/rpcService.ts +196 -0
  91. package/src/services/external/secretService.ts +178 -0
  92. package/src/services/index.ts +1 -0
  93. package/src/services/internal/apiService.ts +6 -0
  94. package/src/services/internal/assetsService.ts +84 -0
  95. package/src/services/internal/configService.ts +261 -0
  96. package/src/services/internal/errorService.ts +124 -0
  97. package/src/services/internal/eventService.ts +93 -0
  98. package/src/services/internal/priceService.ts +10 -0
  99. package/src/services/internal/transactionService.ts +255 -0
  100. package/src/services/internal/transactionStatusService.ts +293 -0
  101. package/src/services/internal/walletService.ts +158 -0
  102. package/src/tests/assetsService.test.ts +176 -0
  103. package/src/tests/configService.test.ts +486 -0
  104. package/src/tests/fetchSquidData.ts +43 -0
  105. package/src/tests/jest-svg-transform.ts +19 -0
  106. package/src/tests/sample.json +0 -0
  107. package/src/tests/walletService.test.ts +178 -0
  108. package/tsconfig.json +30 -0
@@ -0,0 +1,155 @@
1
+ import type { StatusResponse } from "@0xsquid/sdk/dist/types";
2
+ import { ChainType } from "@0xsquid/sdk/dist/types";
3
+
4
+ import {
5
+ axelarEndStatuses,
6
+ axelarSuccessStatuses,
7
+ backendVersion,
8
+ } from "@/core/constants";
9
+ import { keys } from "@/core/queries/queries-keys";
10
+ import { useQuery } from "@tanstack/react-query";
11
+ import type { AxiosError } from "axios";
12
+ import axios from "axios";
13
+ import { useMemo, useState } from "react";
14
+
15
+ import type { TransactionParams } from "@/core/types/transaction";
16
+ import { TransactionStatus } from "@/core/types/transaction";
17
+ import { usePersistStore, useSquidStore } from "@/hooks/store/useSquidStore";
18
+ import { useSquidRouter } from "@/hooks/webapp-router/useSquidRouter";
19
+ import { getQueryHeaders } from "@/services/internal/apiService";
20
+ import { WidgetEvents } from "@/services/internal/eventService";
21
+ import {
22
+ findHistoryItem,
23
+ isCCTPBridge,
24
+ updateTransactionHistoryStatus,
25
+ } from "@/services/internal/transactionService";
26
+
27
+ export const useSingleTransaction = (
28
+ transaction: TransactionParams | undefined
29
+ ) => {
30
+ const { config } = useSquidStore();
31
+ const { currentRoute } = useSquidRouter();
32
+ const [refetchInterval, setRefetchInterval] = useState(10000);
33
+
34
+ const currentHistoryItem = useMemo(
35
+ () => findHistoryItem(transaction?.transactionId),
36
+ [transaction?.transactionId]
37
+ );
38
+
39
+ // TODO: Might need to be outside the hooks library
40
+ const isTransactionPage = useMemo(
41
+ // () => currentRoute?.path === routes.transaction.path,
42
+ // TODO: Update this to use the correct route
43
+ () => true,
44
+ [currentRoute?.path]
45
+ );
46
+
47
+ /**
48
+ * Transaction status endpoint
49
+ * Squid api is using axelar endpoint and parsing the response
50
+ * @returns {StatusResponse} Status response
51
+ */
52
+ const transactionStatusQuery = useQuery(
53
+ keys({ apiUrl: config.apiUrl }).transactionStatus(transaction),
54
+ async () => {
55
+ const statusEndpoint = `${config.apiUrl}/${backendVersion}/status`;
56
+ const response = await axios
57
+ .get<StatusResponse>(statusEndpoint, {
58
+ params: {
59
+ transactionId: transaction?.transactionId,
60
+ fromChainId: transaction?.fromChain?.chainId,
61
+ toChainId: transaction?.toChain?.chainId,
62
+ ...(isCCTPBridge(transaction) ? { bridgeType: "cctp" } : {}),
63
+ },
64
+ headers: getQueryHeaders(
65
+ config.integratorId,
66
+ useSquidStore.getState().currentRequestId ?? ""
67
+ ),
68
+ })
69
+ ?.catch((error: AxiosError) => {
70
+ throw new Error("Fetch transaction status failed", {
71
+ cause: error,
72
+ });
73
+ });
74
+ return response.data;
75
+ },
76
+
77
+ {
78
+ enabled:
79
+ !!transaction?.transactionId &&
80
+ transaction !== undefined &&
81
+ currentHistoryItem?.status !== "error" &&
82
+ currentHistoryItem?.status !== "success",
83
+ refetchInterval(data, _) {
84
+ const statusResponse = data;
85
+ // If the status response is something telling that the transaction
86
+ // is finished, then store transaction history state if success
87
+ // And return false to indicate refetcher to stop
88
+ if (
89
+ statusResponse &&
90
+ axelarEndStatuses.includes(statusResponse.status ?? "")
91
+ ) {
92
+ return false;
93
+ }
94
+ return refetchInterval; // Had to handle a variable here because after onError, we want the interval to stop
95
+ },
96
+
97
+ // At the moment Cosmos indexing takes more time, so need more time between retries
98
+ retryDelay:
99
+ transaction?.fromChain?.chainType === ChainType.COSMOS ? 5000 : 3000, // Will wait some seconds before retrying after an error, until max errors (retry below) has been reached
100
+ retry:
101
+ transaction?.fromChain?.chainType === ChainType.COSMOS
102
+ ? 6
103
+ : currentRoute.id === "transaction"
104
+ ? 25
105
+ : 1, // Will retry failed requests x times before displaying an error
106
+ refetchOnWindowFocus: isTransactionPage ? "always" : false,
107
+
108
+ onSuccess: (data) => {
109
+ const statusResponse = data;
110
+
111
+ // Dispatch event
112
+ WidgetEvents.getInstance().dispatchSwapStatus(
113
+ statusResponse.squidTransactionStatus ?? ""
114
+ );
115
+
116
+ if (
117
+ statusResponse &&
118
+ axelarSuccessStatuses.includes(statusResponse.status ?? "") &&
119
+ currentHistoryItem?.status !== "success"
120
+ ) {
121
+ usePersistStore.setState({
122
+ transactionsHistory: updateTransactionHistoryStatus(
123
+ transaction?.transactionId,
124
+ TransactionStatus.SUCCESS,
125
+ usePersistStore.getState().transactionsHistory,
126
+ statusResponse
127
+ ),
128
+ });
129
+ }
130
+ },
131
+ onError: (error: Error | AxiosError) => {
132
+ // Check if axios error and if it's a 404
133
+ const is404 = (error.cause as AxiosError)?.response?.status === 404;
134
+ setRefetchInterval(-1);
135
+ usePersistStore.setState({
136
+ transactionsHistory: updateTransactionHistoryStatus(
137
+ transaction?.transactionId,
138
+ is404 ? TransactionStatus.NOT_FOUND : TransactionStatus.ERROR,
139
+ usePersistStore.getState().transactionsHistory,
140
+ undefined
141
+ ),
142
+ });
143
+ },
144
+ }
145
+ );
146
+
147
+ return {
148
+ transactionStatusQuery,
149
+ latestStatus:
150
+ (transactionStatusQuery.data
151
+ ?.squidTransactionStatus as TransactionStatus) ??
152
+ transaction?.status ??
153
+ currentHistoryItem?.status,
154
+ };
155
+ };