@phantom/react-sdk 0.0.8 → 0.0.9

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 CHANGED
@@ -17,7 +17,7 @@ import { createSolanaPlugin } from "@phantom/browser-sdk/solana";
17
17
 
18
18
  function App() {
19
19
  return (
20
- <PhantomProvider config={{ chainPlugins: [createSolanaPlugin()] }}>
20
+ <PhantomProvider config={{ plugins: [createSolanaPlugin()] }}>
21
21
  <WalletComponent />
22
22
  </PhantomProvider>
23
23
  );
@@ -55,7 +55,7 @@ The PhantomProvider component provides the Phantom context to child components.
55
55
  import { PhantomProvider } from "@phantom/react-sdk";
56
56
  import { createSolanaPlugin } from "@phantom/browser-sdk/solana";
57
57
 
58
- <PhantomProvider config={{ chainPlugins: [createSolanaPlugin()] }}>{children}</PhantomProvider>;
58
+ <PhantomProvider config={{ plugins: [createSolanaPlugin()] }}>{children}</PhantomProvider>;
59
59
  ```
60
60
 
61
61
  ### usePhantom
@@ -0,0 +1,44 @@
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 };
@@ -0,0 +1,182 @@
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
+ });
@@ -0,0 +1,137 @@
1
+ import {
2
+ usePhantom
3
+ } from "../chunk-5KSEZ3MC.mjs";
4
+
5
+ // src/auto-confirm/index.ts
6
+ import "@phantom/browser-sdk/auto-confirm";
7
+
8
+ // src/auto-confirm/useAutoConfirmState.ts
9
+ import * as React from "react";
10
+
11
+ // src/auto-confirm/assertions.ts
12
+ function assertAutoConfirmConfigured(phantom) {
13
+ if (!phantom?.autoConfirm) {
14
+ throw new Error(
15
+ "Phantom auto-confirm plugin not found. Please ensure the auto-confirm plugin is installed and configured properly."
16
+ );
17
+ }
18
+ }
19
+
20
+ // src/auto-confirm/useAutoConfirmState.ts
21
+ function useAutoConfirmState() {
22
+ const { phantom, isReady } = usePhantom();
23
+ const isMountedRef = React.useRef(true);
24
+ const isInitializedRef = React.useRef(false);
25
+ const [status, setStatus] = React.useState(null);
26
+ const [supportedChains, setSupportedChains] = React.useState(null);
27
+ const [isLoading, setIsLoading] = React.useState(true);
28
+ const [error, setError] = React.useState(null);
29
+ React.useEffect(() => {
30
+ return () => {
31
+ isMountedRef.current = false;
32
+ };
33
+ }, []);
34
+ const updateState = React.useCallback(async () => {
35
+ if (!isReady || !isMountedRef.current)
36
+ return;
37
+ try {
38
+ assertAutoConfirmConfigured(phantom);
39
+ if (!isMountedRef.current)
40
+ return;
41
+ setError(null);
42
+ if (status === null && isMountedRef.current) {
43
+ setIsLoading(true);
44
+ }
45
+ const [statusResult, supportedChainsResult] = await Promise.all([
46
+ phantom.autoConfirm.autoConfirmStatus(),
47
+ phantom.autoConfirm.autoConfirmSupportedChains()
48
+ ]);
49
+ if (!isMountedRef.current)
50
+ return;
51
+ setStatus(statusResult);
52
+ setSupportedChains(supportedChainsResult.chains);
53
+ } catch (err) {
54
+ if (!isMountedRef.current)
55
+ return;
56
+ setError(err instanceof Error ? err : new Error("Failed to fetch auto-confirm state"));
57
+ setStatus(null);
58
+ setSupportedChains(null);
59
+ } finally {
60
+ if (isMountedRef.current) {
61
+ setIsLoading(false);
62
+ }
63
+ }
64
+ }, [phantom, isReady, status]);
65
+ React.useEffect(() => {
66
+ if (!isInitializedRef.current && isReady) {
67
+ isInitializedRef.current = true;
68
+ updateState();
69
+ }
70
+ const handleStateChange = () => {
71
+ updateState();
72
+ };
73
+ window.addEventListener("phantomAutoConfirmStateChanged", handleStateChange);
74
+ return () => {
75
+ window.removeEventListener("phantomAutoConfirmStateChanged", handleStateChange);
76
+ };
77
+ }, [updateState, isReady]);
78
+ return {
79
+ status,
80
+ supportedChains,
81
+ isLoading,
82
+ error
83
+ };
84
+ }
85
+
86
+ // src/auto-confirm/useAutoConfirmActions.ts
87
+ import * as React2 from "react";
88
+ function useAutoConfirmActions() {
89
+ const { phantom, isReady } = usePhantom();
90
+ const enable = React2.useCallback(
91
+ async (params) => {
92
+ if (!isReady) {
93
+ throw new Error("Phantom is not ready");
94
+ }
95
+ assertAutoConfirmConfigured(phantom);
96
+ const result = await phantom.autoConfirm.autoConfirmEnable(params);
97
+ window.dispatchEvent(new CustomEvent("phantomAutoConfirmStateChanged"));
98
+ return result;
99
+ },
100
+ [phantom, isReady]
101
+ );
102
+ const disable = React2.useCallback(async () => {
103
+ if (!isReady) {
104
+ throw new Error("Phantom is not ready");
105
+ }
106
+ assertAutoConfirmConfigured(phantom);
107
+ const result = await phantom.autoConfirm.autoConfirmDisable();
108
+ window.dispatchEvent(new CustomEvent("phantomAutoConfirmStateChanged"));
109
+ return result;
110
+ }, [phantom, isReady]);
111
+ const getSupportedChains = React2.useCallback(async () => {
112
+ if (!isReady) {
113
+ throw new Error("Phantom is not ready");
114
+ }
115
+ assertAutoConfirmConfigured(phantom);
116
+ const result = await phantom.autoConfirm.autoConfirmSupportedChains();
117
+ return result;
118
+ }, [phantom, isReady]);
119
+ const getStatus = React2.useCallback(async () => {
120
+ if (!isReady) {
121
+ throw new Error("Phantom is not ready");
122
+ }
123
+ assertAutoConfirmConfigured(phantom);
124
+ const result = await phantom.autoConfirm.autoConfirmStatus();
125
+ return result;
126
+ }, [phantom, isReady]);
127
+ return {
128
+ enable,
129
+ disable,
130
+ getSupportedChains,
131
+ getStatus
132
+ };
133
+ }
134
+ export {
135
+ useAutoConfirmActions,
136
+ useAutoConfirmState
137
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phantom/react-sdk",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",
@@ -14,6 +14,11 @@
14
14
  "import": "./dist/solana/index.mjs",
15
15
  "require": "./dist/solana/index.js",
16
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"
17
22
  }
18
23
  },
19
24
  "files": [
@@ -21,15 +26,15 @@
21
26
  ],
22
27
  "license": "MIT",
23
28
  "scripts": {
24
- "build": "rimraf ./dist && tsup src/index.ts src/solana/index.ts --format cjs,esm --dts",
29
+ "build": "rimraf ./dist && tsup src/index.ts src/solana/index.ts src/auto-confirm/index.ts --format cjs,esm --dts",
25
30
  "?pack-release": "When https://github.com/changesets/changesets/issues/432 has a solution we can remove this trick",
26
31
  "pack-release": "rimraf ./_release && yarn pack && mkdir ./_release && tar zxvf ./package.tgz --directory ./_release && rm ./package.tgz",
27
- "dev": "rimraf ./dist && tsup src/index.ts src/solana/index.ts --format cjs,esm --dts --watch",
32
+ "dev": "rimraf ./dist && tsup src/index.ts src/solana/index.ts src/auto-confirm/index.ts --format cjs,esm --dts --watch",
28
33
  "lint": "tsc --noEmit && eslint --cache . --ext .ts,.tsx",
29
34
  "test": "jest"
30
35
  },
31
36
  "dependencies": {
32
- "@phantom/browser-sdk": "^0.0.8"
37
+ "@phantom/browser-sdk": "^0.0.9"
33
38
  },
34
39
  "devDependencies": {
35
40
  "@testing-library/dom": "^10.4.0",