@tokemak/hooks 0.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.
@@ -0,0 +1,59 @@
1
+ import * as _tokemak_config from '@tokemak/config';
2
+ import { CoreNetworkConfig } from '@tokemak/config';
3
+ import * as viem from 'viem';
4
+ import * as react from 'react';
5
+
6
+ declare const useClipboard: () => {
7
+ isCopied: boolean;
8
+ copyToClipboard: (text: string) => void;
9
+ };
10
+
11
+ declare const useInputErrors: (balance: string | undefined, amount: string) => {
12
+ hasError: boolean | undefined;
13
+ inputMsg: string | undefined;
14
+ };
15
+
16
+ declare const useCoreConfig: (chainId?: number) => CoreNetworkConfig;
17
+
18
+ declare const useUserAccount: () => {
19
+ address: `0x${string}` | undefined;
20
+ ensName: viem.GetEnsNameReturnType | undefined;
21
+ ensAvatar: viem.GetEnsAvatarReturnType | undefined;
22
+ truncatedAddress: string | null;
23
+ };
24
+
25
+ declare const useIsChainSupported: () => boolean;
26
+
27
+ declare const useMainnetConfig: () => _tokemak_config.MainnetNetworkConfig;
28
+
29
+ declare const useAmountPrice: (amount?: string, price?: number) => string;
30
+
31
+ interface AutoSlippageConfig {
32
+ estimatedPriceImpact?: number;
33
+ maxSlippage?: number;
34
+ margin?: number;
35
+ defaultSlippage?: number;
36
+ }
37
+ declare const useAutoSlippage: ({ estimatedPriceImpact, maxSlippage, margin, defaultSlippage, }: AutoSlippageConfig) => number;
38
+
39
+ declare const useDebounce: <T>(value: T, delay: number) => [T, boolean];
40
+
41
+ declare function useLocalStorageState<T>(key: string, defaultValue: T): readonly [T, react.Dispatch<react.SetStateAction<T>>];
42
+
43
+ declare const useIsDev: () => boolean;
44
+
45
+ declare const useBrowserDetect: () => {
46
+ isChrome: boolean;
47
+ isSafari: boolean;
48
+ isFirefox: boolean;
49
+ isEdge: boolean;
50
+ isOpera: boolean;
51
+ isIE: boolean;
52
+ isIOS: boolean;
53
+ isAndroid: boolean;
54
+ isWindows: boolean;
55
+ isMacOS: boolean;
56
+ isLinux: boolean;
57
+ };
58
+
59
+ export { useAmountPrice, useAutoSlippage, useBrowserDetect, useClipboard, useCoreConfig, useDebounce, useInputErrors, useIsChainSupported, useIsDev, useLocalStorageState, useMainnetConfig, useUserAccount };
package/dist/index.js ADDED
@@ -0,0 +1,249 @@
1
+ // src/ui/useClipboard.ts
2
+ import { useState } from "react";
3
+ var useClipboard = () => {
4
+ const [isCopied, setIsCopied] = useState(false);
5
+ const copyToClipboard = (text) => {
6
+ setIsCopied(true);
7
+ navigator.clipboard.writeText(text);
8
+ setTimeout(() => setIsCopied(false), 1e3);
9
+ };
10
+ return { isCopied, copyToClipboard };
11
+ };
12
+
13
+ // src/ui/useInputErrors.ts
14
+ import { useMemo } from "react";
15
+ var useInputErrors = (balance, amount) => {
16
+ const hasError = useMemo(() => {
17
+ if (parseFloat(amount) < 0) return true;
18
+ if (balance) return parseFloat(amount) > parseFloat(balance);
19
+ }, [amount, balance]);
20
+ const inputMsg = useMemo(() => {
21
+ if (!amount || parseFloat(amount) === 0) {
22
+ return "Enter an amount";
23
+ }
24
+ if (parseFloat(amount) < 0) {
25
+ return "Invalid amount";
26
+ }
27
+ if (balance) {
28
+ if (parseFloat(amount) > parseFloat(balance)) {
29
+ return "Insufficient balance";
30
+ }
31
+ }
32
+ }, [amount, balance]);
33
+ return { hasError, inputMsg };
34
+ };
35
+
36
+ // src/web3/useCoreConfig.ts
37
+ import {
38
+ getCoreConfig,
39
+ isSupportedChainId
40
+ } from "@tokemak/config";
41
+ import { useMemo as useMemo2 } from "react";
42
+ import { mainnet } from "viem/chains";
43
+ import { useAccount } from "wagmi";
44
+ var useCoreConfig = (chainId) => {
45
+ const { chain } = useAccount();
46
+ const tokemakConfig = useMemo2(() => {
47
+ if (chainId && isSupportedChainId(chainId)) {
48
+ return getCoreConfig(chainId);
49
+ }
50
+ if (chain && isSupportedChainId(chain.id)) {
51
+ return getCoreConfig(chain.id);
52
+ }
53
+ return getCoreConfig(mainnet.id);
54
+ }, [chain, chainId]);
55
+ return tokemakConfig;
56
+ };
57
+
58
+ // src/web3/useUserAccount.tsx
59
+ import { useMemo as useMemo3 } from "react";
60
+ import { truncateAddress } from "@tokemak/utils";
61
+ import { useAccount as useAccount2, useEnsAvatar, useEnsName } from "wagmi";
62
+ var useUserAccount = () => {
63
+ const { address } = useAccount2();
64
+ const { data: ensName } = useEnsName({ address });
65
+ const { data: ensAvatar } = useEnsAvatar({ name: ensName || "" });
66
+ const truncatedAddress = useMemo3(() => truncateAddress(address), [address]);
67
+ return { address, ensName, ensAvatar, truncatedAddress };
68
+ };
69
+
70
+ // src/web3/useIsChainSupported.ts
71
+ import { useMemo as useMemo4 } from "react";
72
+ import { useAccount as useAccount3, useConfig } from "wagmi";
73
+ var useIsChainSupported = () => {
74
+ const { chain } = useAccount3();
75
+ const { chains } = useConfig();
76
+ const isChainSupported = useMemo4(
77
+ () => chain && chains.some((item) => item.id === chain.id),
78
+ [chain, chains]
79
+ );
80
+ return !!isChainSupported;
81
+ };
82
+
83
+ // src/web3/useMainnetConfig.ts
84
+ import { mainnetConfig } from "@tokemak/config";
85
+ import { useMemo as useMemo5 } from "react";
86
+ import { useAccount as useAccount4 } from "wagmi";
87
+ var useMainnetConfig = () => {
88
+ const { chain } = useAccount4();
89
+ const config = useMemo5(() => {
90
+ if (chain === void 0) {
91
+ return mainnetConfig;
92
+ } else {
93
+ return mainnetConfig;
94
+ }
95
+ }, [chain]);
96
+ return config;
97
+ };
98
+
99
+ // src/web3/useAmountPrice.ts
100
+ import { formatCurrency } from "@tokemak/utils";
101
+ import numbro from "numbro";
102
+ import { useMemo as useMemo6 } from "react";
103
+ var useAmountPrice = (amount, price) => {
104
+ return useMemo6(
105
+ () => amount && price ? formatCurrency(parseFloat(amount) * price) : numbro(0).formatCurrency({ mantissa: 2 }),
106
+ [amount, price]
107
+ );
108
+ };
109
+
110
+ // src/web3/useAutoSlippage.ts
111
+ import { useMemo as useMemo7 } from "react";
112
+ var MAX_SLIPPAGE_DEFAULT = 0.02;
113
+ var MARGIN_DEFAULT = 2e-3;
114
+ var DEFAULT_SLIPPAGE = 5e-3;
115
+ var calculateAutoSlippage = (priceImpact, margin, maxSlippage) => {
116
+ const slippage = priceImpact + margin;
117
+ return Math.min(slippage, maxSlippage);
118
+ };
119
+ var useAutoSlippage = ({
120
+ estimatedPriceImpact,
121
+ maxSlippage = MAX_SLIPPAGE_DEFAULT,
122
+ margin = MARGIN_DEFAULT,
123
+ defaultSlippage = DEFAULT_SLIPPAGE
124
+ }) => {
125
+ const autoSlippage = useMemo7(() => {
126
+ if (estimatedPriceImpact !== void 0 && !isNaN(estimatedPriceImpact)) {
127
+ return calculateAutoSlippage(estimatedPriceImpact, margin, maxSlippage);
128
+ }
129
+ return defaultSlippage;
130
+ }, [estimatedPriceImpact]);
131
+ return autoSlippage;
132
+ };
133
+
134
+ // src/utils/useDebounce.ts
135
+ import { useState as useState2, useEffect } from "react";
136
+ var useDebounce = (value, delay) => {
137
+ const [debouncedValue, setDebouncedValue] = useState2(value);
138
+ const [isLoading, setIsLoading] = useState2(false);
139
+ useEffect(() => {
140
+ setIsLoading(true);
141
+ const handler = setTimeout(() => {
142
+ setDebouncedValue(value);
143
+ setIsLoading(false);
144
+ }, delay);
145
+ return () => {
146
+ clearTimeout(handler);
147
+ setIsLoading(false);
148
+ };
149
+ }, [value, delay]);
150
+ return [debouncedValue, isLoading];
151
+ };
152
+
153
+ // src/utils/useLocalStorageState.ts
154
+ import { useState as useState3, useEffect as useEffect2 } from "react";
155
+ function useLocalStorageState(key, defaultValue) {
156
+ const [state, setState] = useState3(() => {
157
+ if (typeof window === "undefined") {
158
+ return defaultValue;
159
+ }
160
+ try {
161
+ const localStorageValue = localStorage.getItem(key);
162
+ return localStorageValue !== null ? JSON.parse(localStorageValue) : defaultValue;
163
+ } catch (error) {
164
+ console.error("Error reading localStorage key", key, error);
165
+ return defaultValue;
166
+ }
167
+ });
168
+ useEffect2(() => {
169
+ if (typeof window === "undefined") {
170
+ return;
171
+ }
172
+ try {
173
+ localStorage.setItem(key, JSON.stringify(state));
174
+ } catch (error) {
175
+ console.error("Error setting localStorage key", key, error);
176
+ }
177
+ }, [key, state]);
178
+ return [state, setState];
179
+ }
180
+
181
+ // src/utils/useIsDev.ts
182
+ var useIsDev = () => {
183
+ return process.env.NEXT_PUBLIC_ENV === "development";
184
+ };
185
+
186
+ // src/utils/useBrowserDetect.ts
187
+ import { useEffect as useEffect3, useState as useState4 } from "react";
188
+ var useBrowserDetect = () => {
189
+ const [isChrome, setIsChrome] = useState4(false);
190
+ const [isSafari, setIsSafari] = useState4(false);
191
+ const [isFirefox, setIsFirefox] = useState4(false);
192
+ const [isEdge, setIsEdge] = useState4(false);
193
+ const [isOpera, setIsOpera] = useState4(false);
194
+ const [isIE, setIsIE] = useState4(false);
195
+ const [isIOS, setIsIOS] = useState4(false);
196
+ const [isAndroid, setIsAndroid] = useState4(false);
197
+ const [isWindows, setIsWindows] = useState4(false);
198
+ const [isMacOS, setIsMacOS] = useState4(false);
199
+ const [isLinux, setIsLinux] = useState4(false);
200
+ useEffect3(() => {
201
+ const userAgent = navigator.userAgent;
202
+ setIsChrome(
203
+ /chrome|crios/i.test(userAgent) && !/edge|opr\//i.test(userAgent)
204
+ );
205
+ setIsSafari(
206
+ /^((?!chrome|android).)*safari/i.test(userAgent) && !/crios|edgios|fxios/i.test(userAgent)
207
+ );
208
+ setIsFirefox(/firefox|fxios/i.test(userAgent));
209
+ setIsEdge(/edg/i.test(userAgent));
210
+ setIsOpera(/opr\//i.test(userAgent));
211
+ setIsIE(/msie|trident/i.test(userAgent));
212
+ setIsIOS(
213
+ /iPad|iPhone|iPod/.test(userAgent) || navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1
214
+ );
215
+ setIsAndroid(/android/i.test(userAgent));
216
+ setIsWindows(/win/i.test(userAgent));
217
+ setIsMacOS(
218
+ /mac/i.test(navigator.platform) && !/iPhone|iPad|iPod/.test(userAgent)
219
+ );
220
+ setIsLinux(/linux/i.test(userAgent) && !/android/i.test(userAgent));
221
+ }, []);
222
+ return {
223
+ isChrome,
224
+ isSafari,
225
+ isFirefox,
226
+ isEdge,
227
+ isOpera,
228
+ isIE,
229
+ isIOS,
230
+ isAndroid,
231
+ isWindows,
232
+ isMacOS,
233
+ isLinux
234
+ };
235
+ };
236
+ export {
237
+ useAmountPrice,
238
+ useAutoSlippage,
239
+ useBrowserDetect,
240
+ useClipboard,
241
+ useCoreConfig,
242
+ useDebounce,
243
+ useInputErrors,
244
+ useIsChainSupported,
245
+ useIsDev,
246
+ useLocalStorageState,
247
+ useMainnetConfig,
248
+ useUserAccount
249
+ };
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@tokemak/hooks",
3
+ "version": "0.0.0",
4
+ "main": "./dist/index.js",
5
+ "types": "./dist/index.d.ts",
6
+ "sideEffects": false,
7
+ "license": "MIT",
8
+ "type": "module",
9
+ "files": [
10
+ "dist/**"
11
+ ],
12
+ "dependencies": {
13
+ "@tokemak/config": "0.1.1",
14
+ "@tokemak/utils": "0.1.1",
15
+ "@tokemak/abis": "0.1.1"
16
+ },
17
+ "peerDependencies": {
18
+ "react": "19.x",
19
+ "react-dom": "19.x",
20
+ "wagmi": "2.x",
21
+ "viem": "2.x"
22
+ },
23
+ "scripts": {
24
+ "build": "tsup",
25
+ "lint": "eslint \"src/**/*.ts*\"",
26
+ "clean": "rimraf .turbo node_modules dist"
27
+ }
28
+ }