@phantom/react-sdk 0.0.9 → 0.1.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.
- package/README.md +477 -161
- package/dist/index.d.ts +72 -15
- package/dist/index.js +311 -17
- package/dist/index.mjs +320 -5
- package/package.json +7 -15
- package/dist/auto-confirm/index.d.ts +0 -44
- package/dist/auto-confirm/index.js +0 -182
- package/dist/auto-confirm/index.mjs +0 -137
- package/dist/chunk-5KSEZ3MC.mjs +0 -25
- package/dist/solana/index.d.ts +0 -64
- package/dist/solana/index.js +0 -230
- package/dist/solana/index.mjs +0 -178
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,323 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
} from "
|
|
1
|
+
// src/PhantomProvider.tsx
|
|
2
|
+
import { createContext, useContext, useState, useEffect, useCallback } from "react";
|
|
3
|
+
import { BrowserSDK } from "@phantom/browser-sdk";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
var PhantomContext = createContext(void 0);
|
|
6
|
+
function PhantomProvider({ children, config }) {
|
|
7
|
+
const [sdk, setSdk] = useState(null);
|
|
8
|
+
const [isConnected, setIsConnected] = useState(false);
|
|
9
|
+
const [addresses, setAddresses] = useState([]);
|
|
10
|
+
const [walletId, setWalletId] = useState(null);
|
|
11
|
+
const [isReady, setIsReady] = useState(false);
|
|
12
|
+
const [error, setError] = useState(null);
|
|
13
|
+
const [currentProviderType, setCurrentProviderType] = useState(null);
|
|
14
|
+
const [isPhantomAvailable, setIsPhantomAvailable] = useState(false);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
try {
|
|
17
|
+
const browserConfig = {
|
|
18
|
+
...config,
|
|
19
|
+
// Use providerType if provided, default to embedded
|
|
20
|
+
providerType: config.providerType || "embedded"
|
|
21
|
+
};
|
|
22
|
+
const browserSDK = new BrowserSDK(browserConfig);
|
|
23
|
+
setSdk(browserSDK);
|
|
24
|
+
const initialProviderInfo = browserSDK.getCurrentProviderInfo();
|
|
25
|
+
setCurrentProviderType(initialProviderInfo?.type || null);
|
|
26
|
+
const checkPhantom = async () => {
|
|
27
|
+
const available = await browserSDK.waitForPhantomExtension(1e3);
|
|
28
|
+
setIsPhantomAvailable(available);
|
|
29
|
+
};
|
|
30
|
+
checkPhantom();
|
|
31
|
+
setIsReady(true);
|
|
32
|
+
} catch (err) {
|
|
33
|
+
setError(err);
|
|
34
|
+
setIsReady(true);
|
|
35
|
+
}
|
|
36
|
+
}, [config]);
|
|
37
|
+
const updateConnectionState = useCallback(async () => {
|
|
38
|
+
if (sdk) {
|
|
39
|
+
try {
|
|
40
|
+
const connected = sdk.isConnected();
|
|
41
|
+
setIsConnected(connected);
|
|
42
|
+
const providerInfo = sdk.getCurrentProviderInfo();
|
|
43
|
+
setCurrentProviderType(providerInfo?.type || null);
|
|
44
|
+
if (connected) {
|
|
45
|
+
const addrs = await sdk.getAddresses();
|
|
46
|
+
setAddresses(addrs);
|
|
47
|
+
setWalletId(sdk.getWalletId());
|
|
48
|
+
} else {
|
|
49
|
+
setAddresses([]);
|
|
50
|
+
setWalletId(null);
|
|
51
|
+
}
|
|
52
|
+
} catch (err) {
|
|
53
|
+
console.error("Error updating connection state:", err);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}, [sdk]);
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
updateConnectionState();
|
|
59
|
+
}, [updateConnectionState]);
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
if (sdk) {
|
|
62
|
+
sdk._updateConnectionState = updateConnectionState;
|
|
63
|
+
}
|
|
64
|
+
}, [sdk, updateConnectionState]);
|
|
65
|
+
const value = {
|
|
66
|
+
sdk,
|
|
67
|
+
isConnected,
|
|
68
|
+
addresses,
|
|
69
|
+
updateConnectionState,
|
|
70
|
+
walletId,
|
|
71
|
+
isReady,
|
|
72
|
+
error,
|
|
73
|
+
currentProviderType,
|
|
74
|
+
isPhantomAvailable
|
|
75
|
+
};
|
|
76
|
+
return /* @__PURE__ */ jsx(PhantomContext.Provider, { value, children });
|
|
77
|
+
}
|
|
78
|
+
function usePhantom() {
|
|
79
|
+
const context = useContext(PhantomContext);
|
|
80
|
+
if (!context) {
|
|
81
|
+
throw new Error("usePhantom must be used within a PhantomProvider");
|
|
82
|
+
}
|
|
83
|
+
return context;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// src/hooks/useConnect.ts
|
|
87
|
+
import { useCallback as useCallback2, useState as useState2 } from "react";
|
|
88
|
+
function useConnect() {
|
|
89
|
+
const context = usePhantom();
|
|
90
|
+
const [isConnecting, setIsConnecting] = useState2(false);
|
|
91
|
+
const [error, setError] = useState2(null);
|
|
92
|
+
const connect = useCallback2(
|
|
93
|
+
async (options) => {
|
|
94
|
+
if (!context.sdk || !context.isReady) {
|
|
95
|
+
throw new Error("SDK not initialized");
|
|
96
|
+
}
|
|
97
|
+
setIsConnecting(true);
|
|
98
|
+
setError(null);
|
|
99
|
+
try {
|
|
100
|
+
const result = await context.sdk.connect(options);
|
|
101
|
+
if (context.sdk._updateConnectionState) {
|
|
102
|
+
await context.sdk._updateConnectionState();
|
|
103
|
+
}
|
|
104
|
+
return result;
|
|
105
|
+
} catch (err) {
|
|
106
|
+
console.error("Error connecting to Phantom:", err);
|
|
107
|
+
setError(err);
|
|
108
|
+
throw err;
|
|
109
|
+
} finally {
|
|
110
|
+
setIsConnecting(false);
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
[context.sdk, context.isReady]
|
|
114
|
+
);
|
|
115
|
+
const switchProvider = useCallback2(
|
|
116
|
+
async (type, options) => {
|
|
117
|
+
if (!context.sdk || !context.isReady) {
|
|
118
|
+
throw new Error("SDK not initialized");
|
|
119
|
+
}
|
|
120
|
+
await context.sdk.switchProvider(type, options);
|
|
121
|
+
if (context.sdk._updateConnectionState) {
|
|
122
|
+
await context.sdk._updateConnectionState();
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
[context.sdk, context.isReady]
|
|
126
|
+
);
|
|
127
|
+
return {
|
|
128
|
+
connect,
|
|
129
|
+
switchProvider,
|
|
130
|
+
isConnecting,
|
|
131
|
+
error,
|
|
132
|
+
currentProviderType: context.currentProviderType,
|
|
133
|
+
isPhantomAvailable: context.isPhantomAvailable
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// src/hooks/useDisconnect.ts
|
|
138
|
+
import { useCallback as useCallback3, useState as useState3 } from "react";
|
|
139
|
+
function useDisconnect() {
|
|
140
|
+
const { sdk, isReady, updateConnectionState } = usePhantom();
|
|
141
|
+
const [isDisconnecting, setIsDisconnecting] = useState3(false);
|
|
142
|
+
const [error, setError] = useState3(null);
|
|
143
|
+
const disconnect = useCallback3(async () => {
|
|
144
|
+
if (!sdk || !isReady) {
|
|
145
|
+
throw new Error("SDK not initialized");
|
|
146
|
+
}
|
|
147
|
+
setIsDisconnecting(true);
|
|
148
|
+
setError(null);
|
|
149
|
+
try {
|
|
150
|
+
await sdk.disconnect();
|
|
151
|
+
await updateConnectionState();
|
|
152
|
+
} catch (err) {
|
|
153
|
+
setError(err);
|
|
154
|
+
throw err;
|
|
155
|
+
} finally {
|
|
156
|
+
setIsDisconnecting(false);
|
|
157
|
+
}
|
|
158
|
+
}, [sdk, isReady, updateConnectionState]);
|
|
159
|
+
return {
|
|
160
|
+
disconnect,
|
|
161
|
+
isDisconnecting,
|
|
162
|
+
error
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// src/hooks/useSignMessage.ts
|
|
167
|
+
import { useCallback as useCallback4, useState as useState4 } from "react";
|
|
168
|
+
function useSignMessage() {
|
|
169
|
+
const { sdk, isConnected } = usePhantom();
|
|
170
|
+
const [isSigning, setIsSigning] = useState4(false);
|
|
171
|
+
const [error, setError] = useState4(null);
|
|
172
|
+
const signMessage = useCallback4(
|
|
173
|
+
async (message, networkId) => {
|
|
174
|
+
if (!sdk) {
|
|
175
|
+
throw new Error("SDK not initialized");
|
|
176
|
+
}
|
|
177
|
+
if (!isConnected) {
|
|
178
|
+
throw new Error("Wallet not connected");
|
|
179
|
+
}
|
|
180
|
+
setIsSigning(true);
|
|
181
|
+
setError(null);
|
|
182
|
+
try {
|
|
183
|
+
const signature = await sdk.signMessage(message, networkId);
|
|
184
|
+
return signature;
|
|
185
|
+
} catch (err) {
|
|
186
|
+
setError(err);
|
|
187
|
+
throw err;
|
|
188
|
+
} finally {
|
|
189
|
+
setIsSigning(false);
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
[sdk, isConnected]
|
|
193
|
+
);
|
|
194
|
+
return {
|
|
195
|
+
signMessage,
|
|
196
|
+
isSigning,
|
|
197
|
+
error
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// src/hooks/useSignAndSendTransaction.ts
|
|
202
|
+
import { useCallback as useCallback5, useState as useState5 } from "react";
|
|
203
|
+
function useSignAndSendTransaction() {
|
|
204
|
+
const { sdk, isConnected } = usePhantom();
|
|
205
|
+
const [isSigning, setIsSigning] = useState5(false);
|
|
206
|
+
const [error, setError] = useState5(null);
|
|
207
|
+
const signAndSendTransaction = useCallback5(
|
|
208
|
+
async (params) => {
|
|
209
|
+
if (!sdk) {
|
|
210
|
+
throw new Error("SDK not initialized");
|
|
211
|
+
}
|
|
212
|
+
if (!isConnected) {
|
|
213
|
+
throw new Error("Wallet not connected");
|
|
214
|
+
}
|
|
215
|
+
setIsSigning(true);
|
|
216
|
+
setError(null);
|
|
217
|
+
try {
|
|
218
|
+
const result = await sdk.signAndSendTransaction(params);
|
|
219
|
+
return result;
|
|
220
|
+
} catch (err) {
|
|
221
|
+
setError(err);
|
|
222
|
+
throw err;
|
|
223
|
+
} finally {
|
|
224
|
+
setIsSigning(false);
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
[sdk, isConnected]
|
|
228
|
+
);
|
|
229
|
+
return {
|
|
230
|
+
signAndSendTransaction,
|
|
231
|
+
isSigning,
|
|
232
|
+
error
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// src/hooks/useAccounts.ts
|
|
237
|
+
function useAccounts() {
|
|
238
|
+
const { addresses, isConnected } = usePhantom();
|
|
239
|
+
return isConnected ? addresses : null;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// src/hooks/useCreateUserOrganization.ts
|
|
243
|
+
import { useCallback as useCallback6, useState as useState6 } from "react";
|
|
244
|
+
function useCreateUserOrganization() {
|
|
245
|
+
const { sdk } = usePhantom();
|
|
246
|
+
const [isCreating, setIsCreating] = useState6(false);
|
|
247
|
+
const [error, setError] = useState6(null);
|
|
248
|
+
const createUserOrganization = useCallback6(
|
|
249
|
+
async (params) => {
|
|
250
|
+
if (!sdk) {
|
|
251
|
+
throw new Error("SDK not initialized");
|
|
252
|
+
}
|
|
253
|
+
setIsCreating(true);
|
|
254
|
+
setError(null);
|
|
255
|
+
try {
|
|
256
|
+
const result = await sdk.createUserOrganization(params);
|
|
257
|
+
return result;
|
|
258
|
+
} catch (err) {
|
|
259
|
+
setError(err);
|
|
260
|
+
throw err;
|
|
261
|
+
} finally {
|
|
262
|
+
setIsCreating(false);
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
[sdk]
|
|
266
|
+
);
|
|
267
|
+
return {
|
|
268
|
+
createUserOrganization,
|
|
269
|
+
isCreating,
|
|
270
|
+
error
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// src/hooks/useIsExtensionInstalled.ts
|
|
275
|
+
import * as React from "react";
|
|
276
|
+
var cachedIsInstalled = null;
|
|
277
|
+
function useIsExtensionInstalled() {
|
|
278
|
+
const { sdk } = usePhantom();
|
|
279
|
+
const [isLoading, setIsLoading] = React.useState(cachedIsInstalled === null);
|
|
280
|
+
const [isInstalled, setIsInstalled] = React.useState(cachedIsInstalled ?? false);
|
|
281
|
+
React.useEffect(() => {
|
|
282
|
+
if (!sdk) {
|
|
283
|
+
setIsLoading(false);
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (cachedIsInstalled !== null) {
|
|
287
|
+
setIsInstalled(cachedIsInstalled);
|
|
288
|
+
setIsLoading(false);
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
const checkExtension = async () => {
|
|
292
|
+
try {
|
|
293
|
+
setIsLoading(true);
|
|
294
|
+
const result = await sdk.waitForPhantomExtension(1e3);
|
|
295
|
+
cachedIsInstalled = result;
|
|
296
|
+
setIsInstalled(result);
|
|
297
|
+
} catch (error) {
|
|
298
|
+
cachedIsInstalled = false;
|
|
299
|
+
setIsInstalled(false);
|
|
300
|
+
} finally {
|
|
301
|
+
setIsLoading(false);
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
checkExtension();
|
|
305
|
+
}, [sdk]);
|
|
306
|
+
return { isLoading, isInstalled };
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// src/index.ts
|
|
310
|
+
import { NetworkId, AddressType } from "@phantom/browser-sdk";
|
|
5
311
|
export {
|
|
312
|
+
AddressType,
|
|
313
|
+
NetworkId,
|
|
6
314
|
PhantomProvider,
|
|
7
|
-
|
|
315
|
+
useAccounts,
|
|
316
|
+
useConnect,
|
|
317
|
+
useCreateUserOrganization,
|
|
318
|
+
useDisconnect,
|
|
319
|
+
useIsExtensionInstalled,
|
|
320
|
+
usePhantom,
|
|
321
|
+
useSignAndSendTransaction,
|
|
322
|
+
useSignMessage
|
|
8
323
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@phantom/react-sdk",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/index.mjs",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -9,16 +9,6 @@
|
|
|
9
9
|
"import": "./dist/index.mjs",
|
|
10
10
|
"require": "./dist/index.js",
|
|
11
11
|
"types": "./dist/index.d.ts"
|
|
12
|
-
},
|
|
13
|
-
"./solana": {
|
|
14
|
-
"import": "./dist/solana/index.mjs",
|
|
15
|
-
"require": "./dist/solana/index.js",
|
|
16
|
-
"types": "./dist/solana/index.d.ts"
|
|
17
|
-
},
|
|
18
|
-
"./auto-confirm": {
|
|
19
|
-
"import": "./dist/auto-confirm/index.mjs",
|
|
20
|
-
"require": "./dist/auto-confirm/index.js",
|
|
21
|
-
"types": "./dist/auto-confirm/index.d.ts"
|
|
22
12
|
}
|
|
23
13
|
},
|
|
24
14
|
"files": [
|
|
@@ -26,15 +16,16 @@
|
|
|
26
16
|
],
|
|
27
17
|
"license": "MIT",
|
|
28
18
|
"scripts": {
|
|
29
|
-
"build": "rimraf ./dist && tsup
|
|
19
|
+
"build": "rimraf ./dist && tsup",
|
|
30
20
|
"?pack-release": "When https://github.com/changesets/changesets/issues/432 has a solution we can remove this trick",
|
|
31
21
|
"pack-release": "rimraf ./_release && yarn pack && mkdir ./_release && tar zxvf ./package.tgz --directory ./_release && rm ./package.tgz",
|
|
32
|
-
"dev": "rimraf ./dist && tsup src/index.ts
|
|
22
|
+
"dev": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --watch",
|
|
33
23
|
"lint": "tsc --noEmit && eslint --cache . --ext .ts,.tsx",
|
|
34
|
-
"test": "jest"
|
|
24
|
+
"test": "jest",
|
|
25
|
+
"prettier": "prettier --write \"src/**/*.{ts,tsx}\""
|
|
35
26
|
},
|
|
36
27
|
"dependencies": {
|
|
37
|
-
"@phantom/browser-sdk": "^0.0
|
|
28
|
+
"@phantom/browser-sdk": "^0.1.0"
|
|
38
29
|
},
|
|
39
30
|
"devDependencies": {
|
|
40
31
|
"@testing-library/dom": "^10.4.0",
|
|
@@ -45,6 +36,7 @@
|
|
|
45
36
|
"eslint": "8.53.0",
|
|
46
37
|
"jest": "^29.7.0",
|
|
47
38
|
"jest-environment-jsdom": "^29.7.0",
|
|
39
|
+
"prettier": "^3.5.2",
|
|
48
40
|
"react": "18.2.0",
|
|
49
41
|
"react-dom": "18.2.0",
|
|
50
42
|
"rimraf": "^6.0.1",
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { AutoConfirmResult, NetworkID, AutoConfirmEnableParams } from '@phantom/browser-sdk/auto-confirm';
|
|
2
|
-
export { AutoConfirmEnableParams, AutoConfirmResult, AutoConfirmSupportedChainsResult, NetworkID } from '@phantom/browser-sdk/auto-confirm';
|
|
3
|
-
|
|
4
|
-
interface AutoConfirmState {
|
|
5
|
-
status: AutoConfirmResult | null;
|
|
6
|
-
supportedChains: NetworkID[] | null;
|
|
7
|
-
isLoading: boolean;
|
|
8
|
-
error: Error | null;
|
|
9
|
-
}
|
|
10
|
-
interface AutoConfirmActions {
|
|
11
|
-
enable: (params?: AutoConfirmEnableParams) => Promise<{
|
|
12
|
-
enabled: boolean;
|
|
13
|
-
chains: NetworkID[];
|
|
14
|
-
}>;
|
|
15
|
-
disable: () => Promise<{
|
|
16
|
-
enabled: boolean;
|
|
17
|
-
chains: NetworkID[];
|
|
18
|
-
}>;
|
|
19
|
-
getSupportedChains: () => Promise<{
|
|
20
|
-
chains: NetworkID[];
|
|
21
|
-
}>;
|
|
22
|
-
getStatus: () => Promise<{
|
|
23
|
-
enabled: boolean;
|
|
24
|
-
chains: NetworkID[];
|
|
25
|
-
}>;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* React hook that provides the current auto-confirm status and supported chains.
|
|
30
|
-
* Automatically updates when auto-confirm is enabled or disabled.
|
|
31
|
-
*
|
|
32
|
-
* @returns Object containing status, supportedChains, isLoading, and error
|
|
33
|
-
*/
|
|
34
|
-
declare function useAutoConfirmState(): AutoConfirmState;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* React hook that provides actions to enable and disable auto-confirm.
|
|
38
|
-
* Operations are async and will trigger state updates in useAutoConfirmState.
|
|
39
|
-
*
|
|
40
|
-
* @returns Object containing enable and disable functions
|
|
41
|
-
*/
|
|
42
|
-
declare function useAutoConfirmActions(): AutoConfirmActions;
|
|
43
|
-
|
|
44
|
-
export { AutoConfirmActions, AutoConfirmState, useAutoConfirmActions, useAutoConfirmState };
|
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/auto-confirm/index.ts
|
|
31
|
-
var auto_confirm_exports = {};
|
|
32
|
-
__export(auto_confirm_exports, {
|
|
33
|
-
useAutoConfirmActions: () => useAutoConfirmActions,
|
|
34
|
-
useAutoConfirmState: () => useAutoConfirmState
|
|
35
|
-
});
|
|
36
|
-
module.exports = __toCommonJS(auto_confirm_exports);
|
|
37
|
-
var import_auto_confirm = require("@phantom/browser-sdk/auto-confirm");
|
|
38
|
-
|
|
39
|
-
// src/auto-confirm/useAutoConfirmState.ts
|
|
40
|
-
var React2 = __toESM(require("react"));
|
|
41
|
-
|
|
42
|
-
// src/PhantomContext.tsx
|
|
43
|
-
var import_browser_sdk = require("@phantom/browser-sdk");
|
|
44
|
-
var React = __toESM(require("react"));
|
|
45
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
46
|
-
var PhantomContext = React.createContext({ phantom: void 0, isReady: false });
|
|
47
|
-
function usePhantom() {
|
|
48
|
-
const context = React.useContext(PhantomContext);
|
|
49
|
-
if (!context) {
|
|
50
|
-
throw new Error("usePhantom must be used within a PhantomProvider");
|
|
51
|
-
}
|
|
52
|
-
return context;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// src/auto-confirm/assertions.ts
|
|
56
|
-
function assertAutoConfirmConfigured(phantom) {
|
|
57
|
-
if (!phantom?.autoConfirm) {
|
|
58
|
-
throw new Error(
|
|
59
|
-
"Phantom auto-confirm plugin not found. Please ensure the auto-confirm plugin is installed and configured properly."
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// src/auto-confirm/useAutoConfirmState.ts
|
|
65
|
-
function useAutoConfirmState() {
|
|
66
|
-
const { phantom, isReady } = usePhantom();
|
|
67
|
-
const isMountedRef = React2.useRef(true);
|
|
68
|
-
const isInitializedRef = React2.useRef(false);
|
|
69
|
-
const [status, setStatus] = React2.useState(null);
|
|
70
|
-
const [supportedChains, setSupportedChains] = React2.useState(null);
|
|
71
|
-
const [isLoading, setIsLoading] = React2.useState(true);
|
|
72
|
-
const [error, setError] = React2.useState(null);
|
|
73
|
-
React2.useEffect(() => {
|
|
74
|
-
return () => {
|
|
75
|
-
isMountedRef.current = false;
|
|
76
|
-
};
|
|
77
|
-
}, []);
|
|
78
|
-
const updateState = React2.useCallback(async () => {
|
|
79
|
-
if (!isReady || !isMountedRef.current)
|
|
80
|
-
return;
|
|
81
|
-
try {
|
|
82
|
-
assertAutoConfirmConfigured(phantom);
|
|
83
|
-
if (!isMountedRef.current)
|
|
84
|
-
return;
|
|
85
|
-
setError(null);
|
|
86
|
-
if (status === null && isMountedRef.current) {
|
|
87
|
-
setIsLoading(true);
|
|
88
|
-
}
|
|
89
|
-
const [statusResult, supportedChainsResult] = await Promise.all([
|
|
90
|
-
phantom.autoConfirm.autoConfirmStatus(),
|
|
91
|
-
phantom.autoConfirm.autoConfirmSupportedChains()
|
|
92
|
-
]);
|
|
93
|
-
if (!isMountedRef.current)
|
|
94
|
-
return;
|
|
95
|
-
setStatus(statusResult);
|
|
96
|
-
setSupportedChains(supportedChainsResult.chains);
|
|
97
|
-
} catch (err) {
|
|
98
|
-
if (!isMountedRef.current)
|
|
99
|
-
return;
|
|
100
|
-
setError(err instanceof Error ? err : new Error("Failed to fetch auto-confirm state"));
|
|
101
|
-
setStatus(null);
|
|
102
|
-
setSupportedChains(null);
|
|
103
|
-
} finally {
|
|
104
|
-
if (isMountedRef.current) {
|
|
105
|
-
setIsLoading(false);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}, [phantom, isReady, status]);
|
|
109
|
-
React2.useEffect(() => {
|
|
110
|
-
if (!isInitializedRef.current && isReady) {
|
|
111
|
-
isInitializedRef.current = true;
|
|
112
|
-
updateState();
|
|
113
|
-
}
|
|
114
|
-
const handleStateChange = () => {
|
|
115
|
-
updateState();
|
|
116
|
-
};
|
|
117
|
-
window.addEventListener("phantomAutoConfirmStateChanged", handleStateChange);
|
|
118
|
-
return () => {
|
|
119
|
-
window.removeEventListener("phantomAutoConfirmStateChanged", handleStateChange);
|
|
120
|
-
};
|
|
121
|
-
}, [updateState, isReady]);
|
|
122
|
-
return {
|
|
123
|
-
status,
|
|
124
|
-
supportedChains,
|
|
125
|
-
isLoading,
|
|
126
|
-
error
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// src/auto-confirm/useAutoConfirmActions.ts
|
|
131
|
-
var React3 = __toESM(require("react"));
|
|
132
|
-
function useAutoConfirmActions() {
|
|
133
|
-
const { phantom, isReady } = usePhantom();
|
|
134
|
-
const enable = React3.useCallback(
|
|
135
|
-
async (params) => {
|
|
136
|
-
if (!isReady) {
|
|
137
|
-
throw new Error("Phantom is not ready");
|
|
138
|
-
}
|
|
139
|
-
assertAutoConfirmConfigured(phantom);
|
|
140
|
-
const result = await phantom.autoConfirm.autoConfirmEnable(params);
|
|
141
|
-
window.dispatchEvent(new CustomEvent("phantomAutoConfirmStateChanged"));
|
|
142
|
-
return result;
|
|
143
|
-
},
|
|
144
|
-
[phantom, isReady]
|
|
145
|
-
);
|
|
146
|
-
const disable = React3.useCallback(async () => {
|
|
147
|
-
if (!isReady) {
|
|
148
|
-
throw new Error("Phantom is not ready");
|
|
149
|
-
}
|
|
150
|
-
assertAutoConfirmConfigured(phantom);
|
|
151
|
-
const result = await phantom.autoConfirm.autoConfirmDisable();
|
|
152
|
-
window.dispatchEvent(new CustomEvent("phantomAutoConfirmStateChanged"));
|
|
153
|
-
return result;
|
|
154
|
-
}, [phantom, isReady]);
|
|
155
|
-
const getSupportedChains = React3.useCallback(async () => {
|
|
156
|
-
if (!isReady) {
|
|
157
|
-
throw new Error("Phantom is not ready");
|
|
158
|
-
}
|
|
159
|
-
assertAutoConfirmConfigured(phantom);
|
|
160
|
-
const result = await phantom.autoConfirm.autoConfirmSupportedChains();
|
|
161
|
-
return result;
|
|
162
|
-
}, [phantom, isReady]);
|
|
163
|
-
const getStatus = React3.useCallback(async () => {
|
|
164
|
-
if (!isReady) {
|
|
165
|
-
throw new Error("Phantom is not ready");
|
|
166
|
-
}
|
|
167
|
-
assertAutoConfirmConfigured(phantom);
|
|
168
|
-
const result = await phantom.autoConfirm.autoConfirmStatus();
|
|
169
|
-
return result;
|
|
170
|
-
}, [phantom, isReady]);
|
|
171
|
-
return {
|
|
172
|
-
enable,
|
|
173
|
-
disable,
|
|
174
|
-
getSupportedChains,
|
|
175
|
-
getStatus
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
179
|
-
0 && (module.exports = {
|
|
180
|
-
useAutoConfirmActions,
|
|
181
|
-
useAutoConfirmState
|
|
182
|
-
});
|