@phantom/react-sdk 0.0.2

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 ADDED
@@ -0,0 +1,77 @@
1
+ # Phantom React SDK
2
+
3
+ React hooks and components for integrating with Phantom wallet.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @phantom/react-sdk @phantom/browser-sdk
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```tsx
14
+ import React from 'react';
15
+ import { PhantomProvider } from '@phantom/react-sdk';
16
+ import { createSolanaPlugin } from '@phantom/browser-sdk/solana';
17
+ import { useProvider } from '@phantom/react-sdk/solana';
18
+
19
+ function App() {
20
+ return (
21
+ <PhantomProvider config={{ chainPlugins: [createSolanaPlugin()] }}>
22
+ <WalletComponent />
23
+ </PhantomProvider>
24
+ );
25
+ }
26
+
27
+ function WalletComponent() {
28
+ const { status, provider } = useProvider();
29
+
30
+ if (status === 'loading') {
31
+ return <div>Loading wallet...</div>;
32
+ }
33
+
34
+ if (status === 'error') {
35
+ return <div>Error</div>;
36
+ }
37
+
38
+ if (status === 'success') {
39
+ return (
40
+ <div>
41
+ <div>Wallet connected!</div>
42
+ <button onClick={() => provider.connect()}>
43
+ Connect to Solana
44
+ </button>
45
+ </div>
46
+ );
47
+ }
48
+
49
+ return null;
50
+ }
51
+ ```
52
+
53
+ ## API Reference
54
+
55
+ ### PhantomProvider
56
+
57
+ The PhantomProvider component provides the Phantom context to child components.
58
+
59
+ ```tsx
60
+ import { PhantomProvider } from '@phantom/react-sdk';
61
+ import { createSolanaPlugin } from '@phantom/browser-sdk/solana';
62
+
63
+ <PhantomProvider config={{ chainPlugins: [createSolanaPlugin()] }}>
64
+ {children}
65
+ </PhantomProvider>
66
+ ```
67
+
68
+ ### useProvider (Solana)
69
+
70
+ The `useProvider` hook provides access to the Solana provider with automatic retry logic and state management.
71
+
72
+ #### Return Value
73
+
74
+ The hook returns an object with the following properties:
75
+
76
+ - `status: 'loading' | 'success' | 'error'` - Current status of the provider
77
+ - `provider: NonNullable<unknown> | null` - The Solana provider instance (null when not available)
@@ -0,0 +1,25 @@
1
+ // src/PhantomContext.tsx
2
+ import { createPhantom } from "@phantom/browser-sdk";
3
+ import * as React from "react";
4
+ import { jsx } from "react/jsx-runtime";
5
+ var PhantomContext = React.createContext({ phantom: void 0, isReady: false });
6
+ var PhantomProvider = ({ children, config }) => {
7
+ const [context, setContext] = React.useState({ phantom: void 0, isReady: false });
8
+ React.useEffect(() => {
9
+ const phantom = createPhantom(config);
10
+ setContext({ phantom, isReady: true });
11
+ }, [config]);
12
+ return /* @__PURE__ */ jsx(PhantomContext.Provider, { value: context, children });
13
+ };
14
+ function usePhantom() {
15
+ const context = React.useContext(PhantomContext);
16
+ if (!context) {
17
+ throw new Error("usePhantom must be used within a PhantomProvider");
18
+ }
19
+ return context;
20
+ }
21
+
22
+ export {
23
+ PhantomProvider,
24
+ usePhantom
25
+ };
@@ -0,0 +1,18 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { CreatePhantomConfig, Phantom } from '@phantom/browser-sdk';
3
+ import * as React from 'react';
4
+
5
+ type PhantomContextType = {
6
+ phantom: undefined;
7
+ isReady: false;
8
+ } | {
9
+ phantom: Phantom;
10
+ isReady: true;
11
+ };
12
+ declare const PhantomProvider: ({ children, config }: {
13
+ children: React.ReactNode;
14
+ config: CreatePhantomConfig;
15
+ }) => react_jsx_runtime.JSX.Element;
16
+ declare function usePhantom(): PhantomContextType;
17
+
18
+ export { PhantomProvider, usePhantom };
package/dist/index.js ADDED
@@ -0,0 +1,62 @@
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/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ PhantomProvider: () => PhantomProvider,
34
+ usePhantom: () => usePhantom
35
+ });
36
+ module.exports = __toCommonJS(src_exports);
37
+
38
+ // src/PhantomContext.tsx
39
+ var import_browser_sdk = require("@phantom/browser-sdk");
40
+ var React = __toESM(require("react"));
41
+ var import_jsx_runtime = require("react/jsx-runtime");
42
+ var PhantomContext = React.createContext({ phantom: void 0, isReady: false });
43
+ var PhantomProvider = ({ children, config }) => {
44
+ const [context, setContext] = React.useState({ phantom: void 0, isReady: false });
45
+ React.useEffect(() => {
46
+ const phantom = (0, import_browser_sdk.createPhantom)(config);
47
+ setContext({ phantom, isReady: true });
48
+ }, [config]);
49
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PhantomContext.Provider, { value: context, children });
50
+ };
51
+ function usePhantom() {
52
+ const context = React.useContext(PhantomContext);
53
+ if (!context) {
54
+ throw new Error("usePhantom must be used within a PhantomProvider");
55
+ }
56
+ return context;
57
+ }
58
+ // Annotate the CommonJS export names for ESM import in node:
59
+ 0 && (module.exports = {
60
+ PhantomProvider,
61
+ usePhantom
62
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,8 @@
1
+ import {
2
+ PhantomProvider,
3
+ usePhantom
4
+ } from "./chunk-5KSEZ3MC.mjs";
5
+ export {
6
+ PhantomProvider,
7
+ usePhantom
8
+ };
@@ -0,0 +1,17 @@
1
+ type ProviderState = {
2
+ status: "loading";
3
+ provider: null;
4
+ } | {
5
+ status: "success";
6
+ provider: NonNullable<unknown>;
7
+ } | {
8
+ status: "error";
9
+ provider: null;
10
+ };
11
+ /**
12
+ * Retrieves the Phantom injected provider. If for some reason the provider is not available, it will retry up to 10 times with exponential backoff.
13
+ * @returns Object containing the provider status, provider instance, and any error.
14
+ */
15
+ declare function useProvider(): ProviderState;
16
+
17
+ export { useProvider };
@@ -0,0 +1,123 @@
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/solana/index.ts
31
+ var solana_exports = {};
32
+ __export(solana_exports, {
33
+ useProvider: () => useProvider
34
+ });
35
+ module.exports = __toCommonJS(solana_exports);
36
+ var import_solana = require("@phantom/browser-sdk/solana");
37
+
38
+ // src/solana/useProvider.ts
39
+ var React2 = __toESM(require("react"));
40
+
41
+ // src/PhantomContext.tsx
42
+ var import_browser_sdk = require("@phantom/browser-sdk");
43
+ var React = __toESM(require("react"));
44
+ var import_jsx_runtime = require("react/jsx-runtime");
45
+ var PhantomContext = React.createContext({ phantom: void 0, isReady: false });
46
+ function usePhantom() {
47
+ const context = React.useContext(PhantomContext);
48
+ if (!context) {
49
+ throw new Error("usePhantom must be used within a PhantomProvider");
50
+ }
51
+ return context;
52
+ }
53
+
54
+ // src/solana/useProvider.ts
55
+ var MAX_RETRIES = 10;
56
+ var BASE_DELAY = 100;
57
+ function useProvider() {
58
+ const { phantom, isReady } = usePhantom();
59
+ const [state, setState] = React2.useState({
60
+ status: "loading",
61
+ provider: null
62
+ });
63
+ const tryResolvingProvider = React2.useCallback(() => {
64
+ if (!phantom) {
65
+ setState({
66
+ status: "error",
67
+ provider: null
68
+ });
69
+ return false;
70
+ }
71
+ if (!phantom.solana) {
72
+ setState({
73
+ status: "error",
74
+ provider: null
75
+ });
76
+ return false;
77
+ }
78
+ try {
79
+ const provider = phantom.solana.getProvider();
80
+ if (provider != null) {
81
+ setState({ status: "success", provider });
82
+ return true;
83
+ } else {
84
+ return false;
85
+ }
86
+ } catch (error) {
87
+ return false;
88
+ }
89
+ }, [phantom]);
90
+ React2.useEffect(() => {
91
+ if (!isReady) {
92
+ setState({ status: "loading", provider: null });
93
+ return;
94
+ }
95
+ if (tryResolvingProvider()) {
96
+ return;
97
+ }
98
+ let retryCount = 0;
99
+ const scheduleRetry = () => {
100
+ const delay = BASE_DELAY * Math.pow(2, Math.min(retryCount, 5));
101
+ setTimeout(() => {
102
+ if (tryResolvingProvider()) {
103
+ return;
104
+ }
105
+ retryCount++;
106
+ if (retryCount >= MAX_RETRIES) {
107
+ setState({
108
+ status: "error",
109
+ provider: null
110
+ });
111
+ } else {
112
+ scheduleRetry();
113
+ }
114
+ }, delay);
115
+ };
116
+ scheduleRetry();
117
+ }, [isReady, tryResolvingProvider]);
118
+ return state;
119
+ }
120
+ // Annotate the CommonJS export names for ESM import in node:
121
+ 0 && (module.exports = {
122
+ useProvider
123
+ });
@@ -0,0 +1,77 @@
1
+ import {
2
+ usePhantom
3
+ } from "../chunk-5KSEZ3MC.mjs";
4
+
5
+ // src/solana/index.ts
6
+ import "@phantom/browser-sdk/solana";
7
+
8
+ // src/solana/useProvider.ts
9
+ import * as React from "react";
10
+ var MAX_RETRIES = 10;
11
+ var BASE_DELAY = 100;
12
+ function useProvider() {
13
+ const { phantom, isReady } = usePhantom();
14
+ const [state, setState] = React.useState({
15
+ status: "loading",
16
+ provider: null
17
+ });
18
+ const tryResolvingProvider = React.useCallback(() => {
19
+ if (!phantom) {
20
+ setState({
21
+ status: "error",
22
+ provider: null
23
+ });
24
+ return false;
25
+ }
26
+ if (!phantom.solana) {
27
+ setState({
28
+ status: "error",
29
+ provider: null
30
+ });
31
+ return false;
32
+ }
33
+ try {
34
+ const provider = phantom.solana.getProvider();
35
+ if (provider != null) {
36
+ setState({ status: "success", provider });
37
+ return true;
38
+ } else {
39
+ return false;
40
+ }
41
+ } catch (error) {
42
+ return false;
43
+ }
44
+ }, [phantom]);
45
+ React.useEffect(() => {
46
+ if (!isReady) {
47
+ setState({ status: "loading", provider: null });
48
+ return;
49
+ }
50
+ if (tryResolvingProvider()) {
51
+ return;
52
+ }
53
+ let retryCount = 0;
54
+ const scheduleRetry = () => {
55
+ const delay = BASE_DELAY * Math.pow(2, Math.min(retryCount, 5));
56
+ setTimeout(() => {
57
+ if (tryResolvingProvider()) {
58
+ return;
59
+ }
60
+ retryCount++;
61
+ if (retryCount >= MAX_RETRIES) {
62
+ setState({
63
+ status: "error",
64
+ provider: null
65
+ });
66
+ } else {
67
+ scheduleRetry();
68
+ }
69
+ }, delay);
70
+ };
71
+ scheduleRetry();
72
+ }, [isReady, tryResolvingProvider]);
73
+ return state;
74
+ }
75
+ export {
76
+ useProvider
77
+ };
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@phantom/react-sdk",
3
+ "version": "0.0.2",
4
+ "main": "dist/index.js",
5
+ "module": "dist/index.mjs",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.mjs",
10
+ "require": "./dist/index.js",
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
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "license": "MIT",
23
+ "scripts": {
24
+ "build": "rm -rf dist && tsup src/index.ts src/solana/index.ts --format cjs,esm --dts",
25
+ "dev": "rm -rf dist && tsup src/index.ts src/solana/index.ts --format cjs,esm --dts --watch",
26
+ "lint": "tsc --noEmit && eslint --cache . --ext .ts,.tsx",
27
+ "test": "jest"
28
+ },
29
+ "dependencies": {
30
+ "@phantom/browser-sdk": "workspace:^"
31
+ },
32
+ "devDependencies": {
33
+ "@testing-library/dom": "^10.4.0",
34
+ "@testing-library/react": "^16.3.0",
35
+ "@types/jest": "^29.5.14",
36
+ "@types/react": "^19.1.5",
37
+ "@types/react-dom": "^19.1.5",
38
+ "eslint": "8.53.0",
39
+ "jest": "^29.7.0",
40
+ "jest-environment-jsdom": "^29.7.0",
41
+ "react": "18.2.0",
42
+ "react-dom": "18.2.0",
43
+ "ts-jest": "^29",
44
+ "tsup": "^6.7.0",
45
+ "typescript": "^5.0.4"
46
+ },
47
+ "peerDependencies": {
48
+ "react": ">=18.0.0"
49
+ }
50
+ }