@ivem/kit-react 0.0.1
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/README.md +35 -0
- package/dist/index.cjs +282 -0
- package/dist/index.d.cts +90 -0
- package/dist/index.d.ts +90 -0
- package/dist/index.js +248 -0
- package/package.json +62 -0
package/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# `@ivem/kit-react`
|
|
2
|
+
|
|
3
|
+
React hooks and provider utilities for IOST wallet flows.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @ivem/kit-react @tanstack/react-query react react-dom
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { createConfig, iwallet, mainnet, IvemProvider, useAccount } from "@ivem/kit-react";
|
|
15
|
+
|
|
16
|
+
const config = createConfig({
|
|
17
|
+
chains: [mainnet],
|
|
18
|
+
connector: iwallet(),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export function App() {
|
|
22
|
+
return (
|
|
23
|
+
<IvemProvider config={config}>
|
|
24
|
+
<Account />
|
|
25
|
+
</IvemProvider>
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function Account() {
|
|
30
|
+
const account = useAccount();
|
|
31
|
+
return <div>{account.address ?? "Not connected"}</div>;
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Requires `react`, `@tanstack/react-query`, and transitively installs `@ivem/kit`.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
var kit = require('@ivem/kit');
|
|
5
|
+
var reactQuery = require('@tanstack/react-query');
|
|
6
|
+
var withSelector_js = require('use-sync-external-store/shim/with-selector.js');
|
|
7
|
+
var index_js = require('use-sync-external-store/shim/index.js');
|
|
8
|
+
|
|
9
|
+
// src/context.ts
|
|
10
|
+
var IvemContext = react.createContext(void 0);
|
|
11
|
+
function IvemProvider(props) {
|
|
12
|
+
const { children, config, initialState, reconnectOnMount = true } = props;
|
|
13
|
+
const queryClient = react.useMemo(
|
|
14
|
+
() => props.queryClient ?? new reactQuery.QueryClient(props.queryClientConfig),
|
|
15
|
+
[props.queryClient, props.queryClientConfig]
|
|
16
|
+
);
|
|
17
|
+
const reconnectAttempted = react.useRef(false);
|
|
18
|
+
react.useEffect(() => {
|
|
19
|
+
if (initialState && !config._internal.ssr) {
|
|
20
|
+
config.setState(initialState);
|
|
21
|
+
}
|
|
22
|
+
if (config._internal.ssr) return;
|
|
23
|
+
if (reconnectOnMount && !reconnectAttempted.current) {
|
|
24
|
+
reconnectAttempted.current = true;
|
|
25
|
+
kit.reconnect(config).catch(() => {
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}, [config, initialState, reconnectOnMount]);
|
|
29
|
+
return react.createElement(
|
|
30
|
+
reactQuery.QueryClientProvider,
|
|
31
|
+
{ client: queryClient },
|
|
32
|
+
react.createElement(IvemContext.Provider, { value: config }, children)
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
function useConfig() {
|
|
36
|
+
const config = react.useContext(IvemContext);
|
|
37
|
+
if (!config) {
|
|
38
|
+
throw new Error("useConfig must be used within IvemProvider");
|
|
39
|
+
}
|
|
40
|
+
return config;
|
|
41
|
+
}
|
|
42
|
+
function useAccount() {
|
|
43
|
+
const config = useConfig();
|
|
44
|
+
const account = withSelector_js.useSyncExternalStoreWithSelector(
|
|
45
|
+
(onChange) => config.subscribe(
|
|
46
|
+
(state) => state,
|
|
47
|
+
() => onChange()
|
|
48
|
+
),
|
|
49
|
+
() => config.state,
|
|
50
|
+
() => config.state,
|
|
51
|
+
(state) => ({
|
|
52
|
+
address: state.accounts[0],
|
|
53
|
+
addresses: state.accounts,
|
|
54
|
+
chainId: state.chainId,
|
|
55
|
+
isConnected: state.status === "connected",
|
|
56
|
+
isConnecting: state.status === "connecting",
|
|
57
|
+
isDisconnected: state.status === "disconnected",
|
|
58
|
+
isReconnecting: state.status === "reconnecting",
|
|
59
|
+
status: state.status
|
|
60
|
+
}),
|
|
61
|
+
(a, b) => a.address === b.address && a.status === b.status && a.chainId === b.chainId && a.addresses.length === b.addresses.length
|
|
62
|
+
);
|
|
63
|
+
return account;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/hooks/error.ts
|
|
67
|
+
function safeStringify(value) {
|
|
68
|
+
try {
|
|
69
|
+
return JSON.stringify(value, null, 2);
|
|
70
|
+
} catch {
|
|
71
|
+
return String(value);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function createRichError(message, extra) {
|
|
75
|
+
const error = new Error(message);
|
|
76
|
+
if (extra) Object.assign(error, extra);
|
|
77
|
+
return error;
|
|
78
|
+
}
|
|
79
|
+
function normalizeHookError(err) {
|
|
80
|
+
if (err instanceof Error) return err;
|
|
81
|
+
if (typeof err === "string") return new Error(err);
|
|
82
|
+
if (err && typeof err === "object") {
|
|
83
|
+
const record = err;
|
|
84
|
+
const nestedMessage = record.message ?? record.reason ?? record.error?.message ?? record.data?.message;
|
|
85
|
+
if (typeof nestedMessage === "string" && nestedMessage.length > 0) {
|
|
86
|
+
return createRichError(nestedMessage, record);
|
|
87
|
+
}
|
|
88
|
+
return createRichError(safeStringify(err), record);
|
|
89
|
+
}
|
|
90
|
+
return new Error(String(err));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/hooks/useConnect.ts
|
|
94
|
+
function useConnect(parameters = {}) {
|
|
95
|
+
const contextConfig = useConfig();
|
|
96
|
+
const config = parameters.config ?? contextConfig;
|
|
97
|
+
return reactQuery.useMutation({
|
|
98
|
+
...parameters.mutation,
|
|
99
|
+
mutationFn: async (params) => {
|
|
100
|
+
try {
|
|
101
|
+
return await kit.connect(config, params ?? {});
|
|
102
|
+
} catch (err) {
|
|
103
|
+
throw normalizeHookError(err);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
function useReconnect(parameters = {}) {
|
|
109
|
+
const contextConfig = useConfig();
|
|
110
|
+
const config = parameters.config ?? contextConfig;
|
|
111
|
+
return reactQuery.useMutation({
|
|
112
|
+
...parameters.mutation,
|
|
113
|
+
mutationFn: async () => {
|
|
114
|
+
try {
|
|
115
|
+
return await kit.reconnect(config);
|
|
116
|
+
} catch (err) {
|
|
117
|
+
throw normalizeHookError(err);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
function useDisconnect(parameters = {}) {
|
|
123
|
+
const contextConfig = useConfig();
|
|
124
|
+
const config = parameters.config ?? contextConfig;
|
|
125
|
+
return reactQuery.useMutation({
|
|
126
|
+
...parameters.mutation,
|
|
127
|
+
mutationFn: async () => {
|
|
128
|
+
try {
|
|
129
|
+
return await kit.disconnect(config);
|
|
130
|
+
} catch (err) {
|
|
131
|
+
throw normalizeHookError(err);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
function useChainId() {
|
|
137
|
+
const config = useConfig();
|
|
138
|
+
const chainId = index_js.useSyncExternalStore(
|
|
139
|
+
(onChange) => {
|
|
140
|
+
return config.subscribe(
|
|
141
|
+
(state) => state.chainId,
|
|
142
|
+
() => onChange()
|
|
143
|
+
);
|
|
144
|
+
},
|
|
145
|
+
() => kit.getChainId(config),
|
|
146
|
+
() => kit.getChainId(config)
|
|
147
|
+
);
|
|
148
|
+
return chainId;
|
|
149
|
+
}
|
|
150
|
+
function useSignMessage(parameters = {}) {
|
|
151
|
+
const contextConfig = useConfig();
|
|
152
|
+
const config = parameters.config ?? contextConfig;
|
|
153
|
+
return reactQuery.useMutation({
|
|
154
|
+
...parameters.mutation,
|
|
155
|
+
mutationFn: async (params) => {
|
|
156
|
+
try {
|
|
157
|
+
return await kit.signMessage(config, params);
|
|
158
|
+
} catch (err) {
|
|
159
|
+
throw normalizeHookError(err);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
function useSendTransaction(parameters = {}) {
|
|
165
|
+
const contextConfig = useConfig();
|
|
166
|
+
const config = parameters.config ?? contextConfig;
|
|
167
|
+
return reactQuery.useMutation({
|
|
168
|
+
...parameters.mutation,
|
|
169
|
+
mutationFn: async (params) => {
|
|
170
|
+
try {
|
|
171
|
+
return await kit.sendTransaction(config, params);
|
|
172
|
+
} catch (err) {
|
|
173
|
+
throw normalizeHookError(err);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
function useWriteContract(parameters = {}) {
|
|
179
|
+
const contextConfig = useConfig();
|
|
180
|
+
const config = parameters.config ?? contextConfig;
|
|
181
|
+
return reactQuery.useMutation({
|
|
182
|
+
...parameters.mutation,
|
|
183
|
+
mutationFn: async (params) => {
|
|
184
|
+
try {
|
|
185
|
+
return await kit.writeContract(config, params);
|
|
186
|
+
} catch (err) {
|
|
187
|
+
throw normalizeHookError(err);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
function useCallContract(parameters = {}) {
|
|
193
|
+
const contextConfig = useConfig();
|
|
194
|
+
const config = parameters.config ?? contextConfig;
|
|
195
|
+
return reactQuery.useMutation({
|
|
196
|
+
...parameters.mutation,
|
|
197
|
+
mutationFn: async (params) => {
|
|
198
|
+
try {
|
|
199
|
+
return await kit.callContract(config, params);
|
|
200
|
+
} catch (err) {
|
|
201
|
+
throw normalizeHookError(err);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
function useWaitForTransactionReceipt(parameters = { hash: "" }) {
|
|
207
|
+
const config = useConfig();
|
|
208
|
+
const { hash = "", enabled = true, ...rest } = parameters;
|
|
209
|
+
const { retryCount, retryDelay, timeout, stage } = rest;
|
|
210
|
+
const query = reactQuery.useQuery({
|
|
211
|
+
queryKey: [
|
|
212
|
+
"waitForTransactionReceipt",
|
|
213
|
+
config.state.chainId,
|
|
214
|
+
hash,
|
|
215
|
+
retryCount,
|
|
216
|
+
retryDelay,
|
|
217
|
+
timeout,
|
|
218
|
+
stage
|
|
219
|
+
],
|
|
220
|
+
enabled: Boolean(hash) && enabled,
|
|
221
|
+
queryFn: async () => {
|
|
222
|
+
try {
|
|
223
|
+
return await kit.waitForTransactionReceipt(config, {
|
|
224
|
+
hash,
|
|
225
|
+
retryCount,
|
|
226
|
+
retryDelay,
|
|
227
|
+
timeout,
|
|
228
|
+
stage
|
|
229
|
+
});
|
|
230
|
+
} catch (err) {
|
|
231
|
+
throw normalizeHookError(err);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
return {
|
|
236
|
+
data: query.data ?? null,
|
|
237
|
+
error: query.error ?? null,
|
|
238
|
+
isLoading: query.isLoading || query.isFetching,
|
|
239
|
+
isSuccess: query.isSuccess,
|
|
240
|
+
isError: query.isError,
|
|
241
|
+
refetch: async () => {
|
|
242
|
+
const result = await query.refetch();
|
|
243
|
+
return result.data ?? null;
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
Object.defineProperty(exports, "createConfig", {
|
|
249
|
+
enumerable: true,
|
|
250
|
+
get: function () { return kit.createConfig; }
|
|
251
|
+
});
|
|
252
|
+
Object.defineProperty(exports, "iwallet", {
|
|
253
|
+
enumerable: true,
|
|
254
|
+
get: function () { return kit.iwallet; }
|
|
255
|
+
});
|
|
256
|
+
Object.defineProperty(exports, "mainnet", {
|
|
257
|
+
enumerable: true,
|
|
258
|
+
get: function () { return kit.mainnet; }
|
|
259
|
+
});
|
|
260
|
+
Object.defineProperty(exports, "testnet", {
|
|
261
|
+
enumerable: true,
|
|
262
|
+
get: function () { return kit.testnet; }
|
|
263
|
+
});
|
|
264
|
+
exports.IvemContext = IvemContext;
|
|
265
|
+
exports.IvemProvider = IvemProvider;
|
|
266
|
+
exports.useAccount = useAccount;
|
|
267
|
+
exports.useCallContract = useCallContract;
|
|
268
|
+
exports.useChainId = useChainId;
|
|
269
|
+
exports.useConfig = useConfig;
|
|
270
|
+
exports.useConnect = useConnect;
|
|
271
|
+
exports.useDisconnect = useDisconnect;
|
|
272
|
+
exports.useReconnect = useReconnect;
|
|
273
|
+
exports.useSendTransaction = useSendTransaction;
|
|
274
|
+
exports.useSignMessage = useSignMessage;
|
|
275
|
+
exports.useWaitForTransactionReceipt = useWaitForTransactionReceipt;
|
|
276
|
+
exports.useWriteContract = useWriteContract;
|
|
277
|
+
Object.keys(kit).forEach(function (k) {
|
|
278
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
279
|
+
enumerable: true,
|
|
280
|
+
get: function () { return kit[k]; }
|
|
281
|
+
});
|
|
282
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import * as _tanstack_react_query from '@tanstack/react-query';
|
|
2
|
+
import { QueryClient, QueryClientConfig, UseMutationOptions, UseMutationResult } from '@tanstack/react-query';
|
|
3
|
+
import * as react from 'react';
|
|
4
|
+
import { ReactNode } from 'react';
|
|
5
|
+
import { Config, State, GetAccountReturnType, ConnectReturnType, ConnectParameters, ReconnectReturnType, DisconnectReturnType, SignMessageReturnType, SignMessageParameters, SendTransactionReturnType, SendTransactionParameters, WriteContractReturnType, WriteContractParameters, CallContractReturnType, CallContractParameters, WaitForTransactionReceiptParameters, WaitForTransactionReceiptReturnType } from '@ivem/kit';
|
|
6
|
+
export * from '@ivem/kit';
|
|
7
|
+
export { Config, CreateConfigParameters, IWalletParameters, createConfig, iwallet, mainnet, testnet } from '@ivem/kit';
|
|
8
|
+
|
|
9
|
+
declare const IvemContext: react.Context<Config | undefined>;
|
|
10
|
+
type IvemProviderProps = {
|
|
11
|
+
config: Config;
|
|
12
|
+
initialState?: State;
|
|
13
|
+
reconnectOnMount?: boolean;
|
|
14
|
+
queryClient?: QueryClient;
|
|
15
|
+
queryClientConfig?: QueryClientConfig;
|
|
16
|
+
children: ReactNode;
|
|
17
|
+
};
|
|
18
|
+
declare function IvemProvider(props: IvemProviderProps): react.FunctionComponentElement<_tanstack_react_query.QueryClientProviderProps>;
|
|
19
|
+
|
|
20
|
+
declare function useConfig(): Config;
|
|
21
|
+
|
|
22
|
+
type UseAccountReturnType = GetAccountReturnType;
|
|
23
|
+
declare function useAccount(): UseAccountReturnType;
|
|
24
|
+
|
|
25
|
+
type UseConnectParameters = {
|
|
26
|
+
config?: Config;
|
|
27
|
+
mutation?: Omit<UseMutationOptions<ConnectReturnType, Error, ConnectParameters | void, unknown>, "mutationFn">;
|
|
28
|
+
};
|
|
29
|
+
type UseConnectReturnType = UseMutationResult<ConnectReturnType, Error, ConnectParameters | void, unknown>;
|
|
30
|
+
declare function useConnect(parameters?: UseConnectParameters): UseConnectReturnType;
|
|
31
|
+
|
|
32
|
+
type UseReconnectParameters = {
|
|
33
|
+
config?: Config;
|
|
34
|
+
mutation?: Omit<UseMutationOptions<ReconnectReturnType, Error, void, unknown>, "mutationFn">;
|
|
35
|
+
};
|
|
36
|
+
type UseReconnectReturnType = UseMutationResult<ReconnectReturnType, Error, void, unknown>;
|
|
37
|
+
declare function useReconnect(parameters?: UseReconnectParameters): UseReconnectReturnType;
|
|
38
|
+
|
|
39
|
+
type UseDisconnectParameters = {
|
|
40
|
+
config?: Config;
|
|
41
|
+
mutation?: Omit<UseMutationOptions<DisconnectReturnType, Error, void, unknown>, "mutationFn">;
|
|
42
|
+
};
|
|
43
|
+
type UseDisconnectReturnType = UseMutationResult<DisconnectReturnType, Error, void, unknown>;
|
|
44
|
+
declare function useDisconnect(parameters?: UseDisconnectParameters): UseDisconnectReturnType;
|
|
45
|
+
|
|
46
|
+
type UseChainIdReturnType = string;
|
|
47
|
+
declare function useChainId(): UseChainIdReturnType;
|
|
48
|
+
|
|
49
|
+
type UseSignMessageParameters = {
|
|
50
|
+
config?: Config;
|
|
51
|
+
mutation?: Omit<UseMutationOptions<SignMessageReturnType, Error, SignMessageParameters, unknown>, "mutationFn">;
|
|
52
|
+
};
|
|
53
|
+
type UseSignMessageReturnType = UseMutationResult<SignMessageReturnType, Error, SignMessageParameters, unknown>;
|
|
54
|
+
declare function useSignMessage(parameters?: UseSignMessageParameters): UseSignMessageReturnType;
|
|
55
|
+
|
|
56
|
+
type UseSendTransactionParameters = {
|
|
57
|
+
config?: Config;
|
|
58
|
+
mutation?: Omit<UseMutationOptions<SendTransactionReturnType, Error, SendTransactionParameters, unknown>, "mutationFn">;
|
|
59
|
+
};
|
|
60
|
+
type UseSendTransactionReturnType = UseMutationResult<SendTransactionReturnType, Error, SendTransactionParameters, unknown>;
|
|
61
|
+
declare function useSendTransaction(parameters?: UseSendTransactionParameters): UseSendTransactionReturnType;
|
|
62
|
+
|
|
63
|
+
type UseWriteContractParameters = {
|
|
64
|
+
config?: Config;
|
|
65
|
+
mutation?: Omit<UseMutationOptions<WriteContractReturnType, Error, WriteContractParameters, unknown>, "mutationFn">;
|
|
66
|
+
};
|
|
67
|
+
type UseWriteContractReturnType = UseMutationResult<WriteContractReturnType, Error, WriteContractParameters, unknown>;
|
|
68
|
+
declare function useWriteContract(parameters?: UseWriteContractParameters): UseWriteContractReturnType;
|
|
69
|
+
|
|
70
|
+
type UseCallContractParameters = {
|
|
71
|
+
config?: Config;
|
|
72
|
+
mutation?: Omit<UseMutationOptions<CallContractReturnType, Error, CallContractParameters, unknown>, "mutationFn">;
|
|
73
|
+
};
|
|
74
|
+
type UseCallContractReturnType = UseMutationResult<CallContractReturnType, Error, CallContractParameters, unknown>;
|
|
75
|
+
declare function useCallContract(parameters?: UseCallContractParameters): UseCallContractReturnType;
|
|
76
|
+
|
|
77
|
+
type UseWaitForTransactionReceiptParameters = WaitForTransactionReceiptParameters & {
|
|
78
|
+
enabled?: boolean;
|
|
79
|
+
};
|
|
80
|
+
type UseWaitForTransactionReceiptReturnType = {
|
|
81
|
+
data: WaitForTransactionReceiptReturnType | null;
|
|
82
|
+
error: Error | null;
|
|
83
|
+
isLoading: boolean;
|
|
84
|
+
isSuccess: boolean;
|
|
85
|
+
isError: boolean;
|
|
86
|
+
refetch: () => Promise<WaitForTransactionReceiptReturnType | null>;
|
|
87
|
+
};
|
|
88
|
+
declare function useWaitForTransactionReceipt(parameters?: UseWaitForTransactionReceiptParameters): UseWaitForTransactionReceiptReturnType;
|
|
89
|
+
|
|
90
|
+
export { IvemContext, IvemProvider, type IvemProviderProps, type UseAccountReturnType, type UseCallContractParameters, type UseCallContractReturnType, type UseChainIdReturnType, type UseConnectParameters, type UseConnectReturnType, type UseDisconnectParameters, type UseDisconnectReturnType, type UseReconnectParameters, type UseReconnectReturnType, type UseSendTransactionParameters, type UseSendTransactionReturnType, type UseSignMessageParameters, type UseSignMessageReturnType, type UseWaitForTransactionReceiptParameters, type UseWaitForTransactionReceiptReturnType, type UseWriteContractParameters, type UseWriteContractReturnType, useAccount, useCallContract, useChainId, useConfig, useConnect, useDisconnect, useReconnect, useSendTransaction, useSignMessage, useWaitForTransactionReceipt, useWriteContract };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import * as _tanstack_react_query from '@tanstack/react-query';
|
|
2
|
+
import { QueryClient, QueryClientConfig, UseMutationOptions, UseMutationResult } from '@tanstack/react-query';
|
|
3
|
+
import * as react from 'react';
|
|
4
|
+
import { ReactNode } from 'react';
|
|
5
|
+
import { Config, State, GetAccountReturnType, ConnectReturnType, ConnectParameters, ReconnectReturnType, DisconnectReturnType, SignMessageReturnType, SignMessageParameters, SendTransactionReturnType, SendTransactionParameters, WriteContractReturnType, WriteContractParameters, CallContractReturnType, CallContractParameters, WaitForTransactionReceiptParameters, WaitForTransactionReceiptReturnType } from '@ivem/kit';
|
|
6
|
+
export * from '@ivem/kit';
|
|
7
|
+
export { Config, CreateConfigParameters, IWalletParameters, createConfig, iwallet, mainnet, testnet } from '@ivem/kit';
|
|
8
|
+
|
|
9
|
+
declare const IvemContext: react.Context<Config | undefined>;
|
|
10
|
+
type IvemProviderProps = {
|
|
11
|
+
config: Config;
|
|
12
|
+
initialState?: State;
|
|
13
|
+
reconnectOnMount?: boolean;
|
|
14
|
+
queryClient?: QueryClient;
|
|
15
|
+
queryClientConfig?: QueryClientConfig;
|
|
16
|
+
children: ReactNode;
|
|
17
|
+
};
|
|
18
|
+
declare function IvemProvider(props: IvemProviderProps): react.FunctionComponentElement<_tanstack_react_query.QueryClientProviderProps>;
|
|
19
|
+
|
|
20
|
+
declare function useConfig(): Config;
|
|
21
|
+
|
|
22
|
+
type UseAccountReturnType = GetAccountReturnType;
|
|
23
|
+
declare function useAccount(): UseAccountReturnType;
|
|
24
|
+
|
|
25
|
+
type UseConnectParameters = {
|
|
26
|
+
config?: Config;
|
|
27
|
+
mutation?: Omit<UseMutationOptions<ConnectReturnType, Error, ConnectParameters | void, unknown>, "mutationFn">;
|
|
28
|
+
};
|
|
29
|
+
type UseConnectReturnType = UseMutationResult<ConnectReturnType, Error, ConnectParameters | void, unknown>;
|
|
30
|
+
declare function useConnect(parameters?: UseConnectParameters): UseConnectReturnType;
|
|
31
|
+
|
|
32
|
+
type UseReconnectParameters = {
|
|
33
|
+
config?: Config;
|
|
34
|
+
mutation?: Omit<UseMutationOptions<ReconnectReturnType, Error, void, unknown>, "mutationFn">;
|
|
35
|
+
};
|
|
36
|
+
type UseReconnectReturnType = UseMutationResult<ReconnectReturnType, Error, void, unknown>;
|
|
37
|
+
declare function useReconnect(parameters?: UseReconnectParameters): UseReconnectReturnType;
|
|
38
|
+
|
|
39
|
+
type UseDisconnectParameters = {
|
|
40
|
+
config?: Config;
|
|
41
|
+
mutation?: Omit<UseMutationOptions<DisconnectReturnType, Error, void, unknown>, "mutationFn">;
|
|
42
|
+
};
|
|
43
|
+
type UseDisconnectReturnType = UseMutationResult<DisconnectReturnType, Error, void, unknown>;
|
|
44
|
+
declare function useDisconnect(parameters?: UseDisconnectParameters): UseDisconnectReturnType;
|
|
45
|
+
|
|
46
|
+
type UseChainIdReturnType = string;
|
|
47
|
+
declare function useChainId(): UseChainIdReturnType;
|
|
48
|
+
|
|
49
|
+
type UseSignMessageParameters = {
|
|
50
|
+
config?: Config;
|
|
51
|
+
mutation?: Omit<UseMutationOptions<SignMessageReturnType, Error, SignMessageParameters, unknown>, "mutationFn">;
|
|
52
|
+
};
|
|
53
|
+
type UseSignMessageReturnType = UseMutationResult<SignMessageReturnType, Error, SignMessageParameters, unknown>;
|
|
54
|
+
declare function useSignMessage(parameters?: UseSignMessageParameters): UseSignMessageReturnType;
|
|
55
|
+
|
|
56
|
+
type UseSendTransactionParameters = {
|
|
57
|
+
config?: Config;
|
|
58
|
+
mutation?: Omit<UseMutationOptions<SendTransactionReturnType, Error, SendTransactionParameters, unknown>, "mutationFn">;
|
|
59
|
+
};
|
|
60
|
+
type UseSendTransactionReturnType = UseMutationResult<SendTransactionReturnType, Error, SendTransactionParameters, unknown>;
|
|
61
|
+
declare function useSendTransaction(parameters?: UseSendTransactionParameters): UseSendTransactionReturnType;
|
|
62
|
+
|
|
63
|
+
type UseWriteContractParameters = {
|
|
64
|
+
config?: Config;
|
|
65
|
+
mutation?: Omit<UseMutationOptions<WriteContractReturnType, Error, WriteContractParameters, unknown>, "mutationFn">;
|
|
66
|
+
};
|
|
67
|
+
type UseWriteContractReturnType = UseMutationResult<WriteContractReturnType, Error, WriteContractParameters, unknown>;
|
|
68
|
+
declare function useWriteContract(parameters?: UseWriteContractParameters): UseWriteContractReturnType;
|
|
69
|
+
|
|
70
|
+
type UseCallContractParameters = {
|
|
71
|
+
config?: Config;
|
|
72
|
+
mutation?: Omit<UseMutationOptions<CallContractReturnType, Error, CallContractParameters, unknown>, "mutationFn">;
|
|
73
|
+
};
|
|
74
|
+
type UseCallContractReturnType = UseMutationResult<CallContractReturnType, Error, CallContractParameters, unknown>;
|
|
75
|
+
declare function useCallContract(parameters?: UseCallContractParameters): UseCallContractReturnType;
|
|
76
|
+
|
|
77
|
+
type UseWaitForTransactionReceiptParameters = WaitForTransactionReceiptParameters & {
|
|
78
|
+
enabled?: boolean;
|
|
79
|
+
};
|
|
80
|
+
type UseWaitForTransactionReceiptReturnType = {
|
|
81
|
+
data: WaitForTransactionReceiptReturnType | null;
|
|
82
|
+
error: Error | null;
|
|
83
|
+
isLoading: boolean;
|
|
84
|
+
isSuccess: boolean;
|
|
85
|
+
isError: boolean;
|
|
86
|
+
refetch: () => Promise<WaitForTransactionReceiptReturnType | null>;
|
|
87
|
+
};
|
|
88
|
+
declare function useWaitForTransactionReceipt(parameters?: UseWaitForTransactionReceiptParameters): UseWaitForTransactionReceiptReturnType;
|
|
89
|
+
|
|
90
|
+
export { IvemContext, IvemProvider, type IvemProviderProps, type UseAccountReturnType, type UseCallContractParameters, type UseCallContractReturnType, type UseChainIdReturnType, type UseConnectParameters, type UseConnectReturnType, type UseDisconnectParameters, type UseDisconnectReturnType, type UseReconnectParameters, type UseReconnectReturnType, type UseSendTransactionParameters, type UseSendTransactionReturnType, type UseSignMessageParameters, type UseSignMessageReturnType, type UseWaitForTransactionReceiptParameters, type UseWaitForTransactionReceiptReturnType, type UseWriteContractParameters, type UseWriteContractReturnType, useAccount, useCallContract, useChainId, useConfig, useConnect, useDisconnect, useReconnect, useSendTransaction, useSignMessage, useWaitForTransactionReceipt, useWriteContract };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { createContext, useMemo, useRef, useEffect, createElement, useContext } from 'react';
|
|
2
|
+
import { reconnect, connect, disconnect, getChainId, signMessage, sendTransaction, writeContract, callContract, waitForTransactionReceipt } from '@ivem/kit';
|
|
3
|
+
export * from '@ivem/kit';
|
|
4
|
+
export { createConfig, iwallet, mainnet, testnet } from '@ivem/kit';
|
|
5
|
+
import { QueryClient, QueryClientProvider, useMutation, useQuery } from '@tanstack/react-query';
|
|
6
|
+
import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/shim/with-selector.js';
|
|
7
|
+
import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';
|
|
8
|
+
|
|
9
|
+
// src/context.ts
|
|
10
|
+
var IvemContext = createContext(void 0);
|
|
11
|
+
function IvemProvider(props) {
|
|
12
|
+
const { children, config, initialState, reconnectOnMount = true } = props;
|
|
13
|
+
const queryClient = useMemo(
|
|
14
|
+
() => props.queryClient ?? new QueryClient(props.queryClientConfig),
|
|
15
|
+
[props.queryClient, props.queryClientConfig]
|
|
16
|
+
);
|
|
17
|
+
const reconnectAttempted = useRef(false);
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
if (initialState && !config._internal.ssr) {
|
|
20
|
+
config.setState(initialState);
|
|
21
|
+
}
|
|
22
|
+
if (config._internal.ssr) return;
|
|
23
|
+
if (reconnectOnMount && !reconnectAttempted.current) {
|
|
24
|
+
reconnectAttempted.current = true;
|
|
25
|
+
reconnect(config).catch(() => {
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}, [config, initialState, reconnectOnMount]);
|
|
29
|
+
return createElement(
|
|
30
|
+
QueryClientProvider,
|
|
31
|
+
{ client: queryClient },
|
|
32
|
+
createElement(IvemContext.Provider, { value: config }, children)
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
function useConfig() {
|
|
36
|
+
const config = useContext(IvemContext);
|
|
37
|
+
if (!config) {
|
|
38
|
+
throw new Error("useConfig must be used within IvemProvider");
|
|
39
|
+
}
|
|
40
|
+
return config;
|
|
41
|
+
}
|
|
42
|
+
function useAccount() {
|
|
43
|
+
const config = useConfig();
|
|
44
|
+
const account = useSyncExternalStoreWithSelector(
|
|
45
|
+
(onChange) => config.subscribe(
|
|
46
|
+
(state) => state,
|
|
47
|
+
() => onChange()
|
|
48
|
+
),
|
|
49
|
+
() => config.state,
|
|
50
|
+
() => config.state,
|
|
51
|
+
(state) => ({
|
|
52
|
+
address: state.accounts[0],
|
|
53
|
+
addresses: state.accounts,
|
|
54
|
+
chainId: state.chainId,
|
|
55
|
+
isConnected: state.status === "connected",
|
|
56
|
+
isConnecting: state.status === "connecting",
|
|
57
|
+
isDisconnected: state.status === "disconnected",
|
|
58
|
+
isReconnecting: state.status === "reconnecting",
|
|
59
|
+
status: state.status
|
|
60
|
+
}),
|
|
61
|
+
(a, b) => a.address === b.address && a.status === b.status && a.chainId === b.chainId && a.addresses.length === b.addresses.length
|
|
62
|
+
);
|
|
63
|
+
return account;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/hooks/error.ts
|
|
67
|
+
function safeStringify(value) {
|
|
68
|
+
try {
|
|
69
|
+
return JSON.stringify(value, null, 2);
|
|
70
|
+
} catch {
|
|
71
|
+
return String(value);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function createRichError(message, extra) {
|
|
75
|
+
const error = new Error(message);
|
|
76
|
+
if (extra) Object.assign(error, extra);
|
|
77
|
+
return error;
|
|
78
|
+
}
|
|
79
|
+
function normalizeHookError(err) {
|
|
80
|
+
if (err instanceof Error) return err;
|
|
81
|
+
if (typeof err === "string") return new Error(err);
|
|
82
|
+
if (err && typeof err === "object") {
|
|
83
|
+
const record = err;
|
|
84
|
+
const nestedMessage = record.message ?? record.reason ?? record.error?.message ?? record.data?.message;
|
|
85
|
+
if (typeof nestedMessage === "string" && nestedMessage.length > 0) {
|
|
86
|
+
return createRichError(nestedMessage, record);
|
|
87
|
+
}
|
|
88
|
+
return createRichError(safeStringify(err), record);
|
|
89
|
+
}
|
|
90
|
+
return new Error(String(err));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/hooks/useConnect.ts
|
|
94
|
+
function useConnect(parameters = {}) {
|
|
95
|
+
const contextConfig = useConfig();
|
|
96
|
+
const config = parameters.config ?? contextConfig;
|
|
97
|
+
return useMutation({
|
|
98
|
+
...parameters.mutation,
|
|
99
|
+
mutationFn: async (params) => {
|
|
100
|
+
try {
|
|
101
|
+
return await connect(config, params ?? {});
|
|
102
|
+
} catch (err) {
|
|
103
|
+
throw normalizeHookError(err);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
function useReconnect(parameters = {}) {
|
|
109
|
+
const contextConfig = useConfig();
|
|
110
|
+
const config = parameters.config ?? contextConfig;
|
|
111
|
+
return useMutation({
|
|
112
|
+
...parameters.mutation,
|
|
113
|
+
mutationFn: async () => {
|
|
114
|
+
try {
|
|
115
|
+
return await reconnect(config);
|
|
116
|
+
} catch (err) {
|
|
117
|
+
throw normalizeHookError(err);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
function useDisconnect(parameters = {}) {
|
|
123
|
+
const contextConfig = useConfig();
|
|
124
|
+
const config = parameters.config ?? contextConfig;
|
|
125
|
+
return useMutation({
|
|
126
|
+
...parameters.mutation,
|
|
127
|
+
mutationFn: async () => {
|
|
128
|
+
try {
|
|
129
|
+
return await disconnect(config);
|
|
130
|
+
} catch (err) {
|
|
131
|
+
throw normalizeHookError(err);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
function useChainId() {
|
|
137
|
+
const config = useConfig();
|
|
138
|
+
const chainId = useSyncExternalStore(
|
|
139
|
+
(onChange) => {
|
|
140
|
+
return config.subscribe(
|
|
141
|
+
(state) => state.chainId,
|
|
142
|
+
() => onChange()
|
|
143
|
+
);
|
|
144
|
+
},
|
|
145
|
+
() => getChainId(config),
|
|
146
|
+
() => getChainId(config)
|
|
147
|
+
);
|
|
148
|
+
return chainId;
|
|
149
|
+
}
|
|
150
|
+
function useSignMessage(parameters = {}) {
|
|
151
|
+
const contextConfig = useConfig();
|
|
152
|
+
const config = parameters.config ?? contextConfig;
|
|
153
|
+
return useMutation({
|
|
154
|
+
...parameters.mutation,
|
|
155
|
+
mutationFn: async (params) => {
|
|
156
|
+
try {
|
|
157
|
+
return await signMessage(config, params);
|
|
158
|
+
} catch (err) {
|
|
159
|
+
throw normalizeHookError(err);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
function useSendTransaction(parameters = {}) {
|
|
165
|
+
const contextConfig = useConfig();
|
|
166
|
+
const config = parameters.config ?? contextConfig;
|
|
167
|
+
return useMutation({
|
|
168
|
+
...parameters.mutation,
|
|
169
|
+
mutationFn: async (params) => {
|
|
170
|
+
try {
|
|
171
|
+
return await sendTransaction(config, params);
|
|
172
|
+
} catch (err) {
|
|
173
|
+
throw normalizeHookError(err);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
function useWriteContract(parameters = {}) {
|
|
179
|
+
const contextConfig = useConfig();
|
|
180
|
+
const config = parameters.config ?? contextConfig;
|
|
181
|
+
return useMutation({
|
|
182
|
+
...parameters.mutation,
|
|
183
|
+
mutationFn: async (params) => {
|
|
184
|
+
try {
|
|
185
|
+
return await writeContract(config, params);
|
|
186
|
+
} catch (err) {
|
|
187
|
+
throw normalizeHookError(err);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
function useCallContract(parameters = {}) {
|
|
193
|
+
const contextConfig = useConfig();
|
|
194
|
+
const config = parameters.config ?? contextConfig;
|
|
195
|
+
return useMutation({
|
|
196
|
+
...parameters.mutation,
|
|
197
|
+
mutationFn: async (params) => {
|
|
198
|
+
try {
|
|
199
|
+
return await callContract(config, params);
|
|
200
|
+
} catch (err) {
|
|
201
|
+
throw normalizeHookError(err);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
function useWaitForTransactionReceipt(parameters = { hash: "" }) {
|
|
207
|
+
const config = useConfig();
|
|
208
|
+
const { hash = "", enabled = true, ...rest } = parameters;
|
|
209
|
+
const { retryCount, retryDelay, timeout, stage } = rest;
|
|
210
|
+
const query = useQuery({
|
|
211
|
+
queryKey: [
|
|
212
|
+
"waitForTransactionReceipt",
|
|
213
|
+
config.state.chainId,
|
|
214
|
+
hash,
|
|
215
|
+
retryCount,
|
|
216
|
+
retryDelay,
|
|
217
|
+
timeout,
|
|
218
|
+
stage
|
|
219
|
+
],
|
|
220
|
+
enabled: Boolean(hash) && enabled,
|
|
221
|
+
queryFn: async () => {
|
|
222
|
+
try {
|
|
223
|
+
return await waitForTransactionReceipt(config, {
|
|
224
|
+
hash,
|
|
225
|
+
retryCount,
|
|
226
|
+
retryDelay,
|
|
227
|
+
timeout,
|
|
228
|
+
stage
|
|
229
|
+
});
|
|
230
|
+
} catch (err) {
|
|
231
|
+
throw normalizeHookError(err);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
return {
|
|
236
|
+
data: query.data ?? null,
|
|
237
|
+
error: query.error ?? null,
|
|
238
|
+
isLoading: query.isLoading || query.isFetching,
|
|
239
|
+
isSuccess: query.isSuccess,
|
|
240
|
+
isError: query.isError,
|
|
241
|
+
refetch: async () => {
|
|
242
|
+
const result = await query.refetch();
|
|
243
|
+
return result.data ?? null;
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export { IvemContext, IvemProvider, useAccount, useCallContract, useChainId, useConfig, useConnect, useDisconnect, useReconnect, useSendTransaction, useSignMessage, useWaitForTransactionReceipt, useWriteContract };
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ivem/kit-react",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "React hooks for IOST blockchain",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/ivemlabs/ivem-kit",
|
|
8
|
+
"directory": "packages/react"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "./dist/index.cjs",
|
|
12
|
+
"module": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js",
|
|
21
|
+
"require": "./dist/index.cjs"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"iost",
|
|
29
|
+
"react",
|
|
30
|
+
"hooks",
|
|
31
|
+
"blockchain"
|
|
32
|
+
],
|
|
33
|
+
"author": "",
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"use-sync-external-store": "^1.2.0",
|
|
37
|
+
"@ivem/kit": "0.0.1"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@tanstack/react-query": "^5.90.2",
|
|
41
|
+
"@types/node": "^20.0.0",
|
|
42
|
+
"@types/react": "^18.2.0",
|
|
43
|
+
"@types/use-sync-external-store": "^0.0.6",
|
|
44
|
+
"react": "^18.2.0",
|
|
45
|
+
"tsup": "^8.0.0",
|
|
46
|
+
"typescript": "^5.3.0"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"@tanstack/react-query": ">=5.0.0",
|
|
50
|
+
"react": ">=18.0.0",
|
|
51
|
+
"typescript": ">=5.0.4"
|
|
52
|
+
},
|
|
53
|
+
"peerDependenciesMeta": {
|
|
54
|
+
"typescript": {
|
|
55
|
+
"optional": true
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "tsup",
|
|
60
|
+
"dev": "tsup --watch"
|
|
61
|
+
}
|
|
62
|
+
}
|