keynesol-shared 1.0.4 → 1.0.5
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WalletProvider.d.ts","sourceRoot":"","sources":["../../../src/components/Wallet/WalletProvider.tsx"],"names":[],"mappings":"AAAA,OAAc,EAAW,SAAS,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"WalletProvider.d.ts","sourceRoot":"","sources":["../../../src/components/Wallet/WalletProvider.tsx"],"names":[],"mappings":"AAAA,OAAc,EAAW,SAAS,EAAE,MAAM,OAAO,CAAC;AAUlD,UAAU,mBAAmB;IAC3B,QAAQ,EAAE,SAAS,CAAC;IACpB,OAAO,CAAC,EAAE,QAAQ,GAAG,cAAc,GAAG,SAAS,CAAC;IAChD,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,eAAO,MAAM,cAAc,GAAI,kEAI5B,mBAAmB,4CAoFrB,CAAC"}
|
|
@@ -16,7 +16,8 @@ var jsx_runtime_1 = require("react/jsx-runtime");
|
|
|
16
16
|
var react_1 = require("react");
|
|
17
17
|
var wallet_adapter_react_1 = require("@solana/wallet-adapter-react");
|
|
18
18
|
var wallet_adapter_base_1 = require("@solana/wallet-adapter-base");
|
|
19
|
-
var
|
|
19
|
+
var wallet_adapter_phantom_1 = require("@solana/wallet-adapter-phantom");
|
|
20
|
+
var wallet_adapter_solflare_1 = require("@solana/wallet-adapter-solflare");
|
|
20
21
|
var wallet_adapter_react_ui_1 = require("@solana/wallet-adapter-react-ui");
|
|
21
22
|
var web3_js_1 = require("@solana/web3.js");
|
|
22
23
|
require('@solana/wallet-adapter-react-ui/styles.css');
|
|
@@ -46,7 +47,7 @@ var WalletProvider = function (_a) {
|
|
|
46
47
|
var walletAdapters = [];
|
|
47
48
|
// Only add Phantom if it's not already registered as Standard Wallet
|
|
48
49
|
try {
|
|
49
|
-
var phantom = new
|
|
50
|
+
var phantom = new wallet_adapter_phantom_1.PhantomWalletAdapter();
|
|
50
51
|
walletAdapters.push(phantom);
|
|
51
52
|
}
|
|
52
53
|
catch (error) {
|
|
@@ -54,7 +55,7 @@ var WalletProvider = function (_a) {
|
|
|
54
55
|
}
|
|
55
56
|
// Add Solflare
|
|
56
57
|
try {
|
|
57
|
-
var solflare = new
|
|
58
|
+
var solflare = new wallet_adapter_solflare_1.SolflareWalletAdapter();
|
|
58
59
|
walletAdapters.push(solflare);
|
|
59
60
|
}
|
|
60
61
|
catch (error) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "keynesol-shared",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Shared code package for Keynesol Web3 Prediction Platform",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -26,9 +26,10 @@
|
|
|
26
26
|
"@coral-xyz/anchor": "^0.31.1",
|
|
27
27
|
"@solana/spl-token": "^0.3.9",
|
|
28
28
|
"@solana/wallet-adapter-base": "^0.9.23",
|
|
29
|
+
"@solana/wallet-adapter-phantom": "^0.9.28",
|
|
29
30
|
"@solana/wallet-adapter-react": "^0.15.35",
|
|
30
31
|
"@solana/wallet-adapter-react-ui": "^0.9.35",
|
|
31
|
-
"@solana/wallet-adapter-
|
|
32
|
+
"@solana/wallet-adapter-solflare": "^0.6.32",
|
|
32
33
|
"@solana/web3.js": "^1.87.6",
|
|
33
34
|
"@supabase/supabase-js": "^2.90.1",
|
|
34
35
|
"react-hot-toast": "^2.4.1"
|
|
@@ -1,104 +1,105 @@
|
|
|
1
|
-
import React, { useMemo, ReactNode } from 'react';
|
|
2
|
-
import { ConnectionProvider, WalletProvider as SolanaWalletProvider } from '@solana/wallet-adapter-react';
|
|
3
|
-
import { WalletAdapterNetwork } from '@solana/wallet-adapter-base';
|
|
4
|
-
import { PhantomWalletAdapter
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
'
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
//
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
!customEndpoint.includes('api.
|
|
77
|
-
!customEndpoint.includes('api.
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
//
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
1
|
+
import React, { useMemo, ReactNode } from 'react';
|
|
2
|
+
import { ConnectionProvider, WalletProvider as SolanaWalletProvider } from '@solana/wallet-adapter-react';
|
|
3
|
+
import { WalletAdapterNetwork } from '@solana/wallet-adapter-base';
|
|
4
|
+
import { PhantomWalletAdapter } from '@solana/wallet-adapter-phantom';
|
|
5
|
+
import { SolflareWalletAdapter } from '@solana/wallet-adapter-solflare';
|
|
6
|
+
import { WalletModalProvider } from '@solana/wallet-adapter-react-ui';
|
|
7
|
+
import { clusterApiUrl } from '@solana/web3.js';
|
|
8
|
+
|
|
9
|
+
require('@solana/wallet-adapter-react-ui/styles.css');
|
|
10
|
+
|
|
11
|
+
interface WalletProviderProps {
|
|
12
|
+
children: ReactNode;
|
|
13
|
+
network?: 'devnet' | 'mainnet-beta' | 'testnet';
|
|
14
|
+
rpcEndpoint?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const WalletProvider = ({
|
|
18
|
+
children,
|
|
19
|
+
network: networkProp,
|
|
20
|
+
rpcEndpoint: rpcEndpointProp
|
|
21
|
+
}: WalletProviderProps) => {
|
|
22
|
+
// Get network from prop, environment variable, or default to devnet
|
|
23
|
+
const networkEnv = networkProp ||
|
|
24
|
+
(typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_SOLANA_NETWORK) ||
|
|
25
|
+
'devnet';
|
|
26
|
+
|
|
27
|
+
const network = networkEnv === 'mainnet-beta'
|
|
28
|
+
? WalletAdapterNetwork.Mainnet
|
|
29
|
+
: networkEnv === 'testnet'
|
|
30
|
+
? WalletAdapterNetwork.Testnet
|
|
31
|
+
: WalletAdapterNetwork.Devnet;
|
|
32
|
+
|
|
33
|
+
// Use custom RPC endpoint if provided, otherwise use cluster API URL
|
|
34
|
+
const endpoint = useMemo(() => {
|
|
35
|
+
const customEndpoint = rpcEndpointProp ||
|
|
36
|
+
(typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_RPC_ENDPOINT);
|
|
37
|
+
if (customEndpoint) {
|
|
38
|
+
return customEndpoint;
|
|
39
|
+
}
|
|
40
|
+
return clusterApiUrl(network);
|
|
41
|
+
}, [network, rpcEndpointProp]);
|
|
42
|
+
|
|
43
|
+
const wallets = useMemo(
|
|
44
|
+
() => {
|
|
45
|
+
const walletAdapters = [];
|
|
46
|
+
|
|
47
|
+
// Only add Phantom if it's not already registered as Standard Wallet
|
|
48
|
+
try {
|
|
49
|
+
const phantom = new PhantomWalletAdapter();
|
|
50
|
+
walletAdapters.push(phantom);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
console.warn('Phantom wallet adapter already registered or error:', error);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Add Solflare
|
|
56
|
+
try {
|
|
57
|
+
const solflare = new SolflareWalletAdapter();
|
|
58
|
+
walletAdapters.push(solflare);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.warn('Solflare wallet adapter error:', error);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return walletAdapters;
|
|
64
|
+
},
|
|
65
|
+
[]
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
// Disable WebSocket for custom RPC endpoints that may not support it
|
|
69
|
+
// Solana SDK will automatically fallback to HTTP polling
|
|
70
|
+
const connectionConfig = useMemo(() => {
|
|
71
|
+
const customEndpoint = rpcEndpointProp ||
|
|
72
|
+
(typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_RPC_ENDPOINT);
|
|
73
|
+
|
|
74
|
+
// If using custom endpoint (not public Solana RPC), disable WebSocket
|
|
75
|
+
const isCustomEndpoint = customEndpoint &&
|
|
76
|
+
!customEndpoint.includes('api.devnet.solana.com') &&
|
|
77
|
+
!customEndpoint.includes('api.mainnet-beta.solana.com') &&
|
|
78
|
+
!customEndpoint.includes('api.testnet.solana.com');
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
commitment: 'confirmed' as const,
|
|
82
|
+
// Disable WebSocket for custom endpoints to avoid connection errors
|
|
83
|
+
// SDK will automatically use HTTP polling instead
|
|
84
|
+
...(isCustomEndpoint ? {
|
|
85
|
+
wsEndpoint: undefined,
|
|
86
|
+
disableRetryOnRateLimit: false,
|
|
87
|
+
} : {}),
|
|
88
|
+
};
|
|
89
|
+
}, [rpcEndpointProp]);
|
|
90
|
+
|
|
91
|
+
// Type assertion to fix React 18/19 compatibility
|
|
92
|
+
const ConnectionProviderComponent = ConnectionProvider as any;
|
|
93
|
+
|
|
94
|
+
// Type assertion to fix React 18/19 compatibility
|
|
95
|
+
|
|
96
|
+
return (
|
|
97
|
+
<ConnectionProviderComponent endpoint={endpoint} config={connectionConfig}>
|
|
98
|
+
<SolanaWalletProvider wallets={wallets} autoConnect>
|
|
99
|
+
<WalletModalProvider>
|
|
100
|
+
{children}
|
|
101
|
+
</WalletModalProvider>
|
|
102
|
+
</SolanaWalletProvider>
|
|
103
|
+
</ConnectionProviderComponent>
|
|
104
|
+
);
|
|
104
105
|
};
|
package/src/hooks/useProgram.ts
CHANGED
|
@@ -1,93 +1,93 @@
|
|
|
1
|
-
import { useMemo, useState, useEffect } from 'react';
|
|
2
|
-
import { useConnection, useWallet } from '@solana/wallet-adapter-react';
|
|
3
|
-
import { Program, AnchorProvider, Idl } from '@coral-xyz/anchor';
|
|
4
|
-
import { PublicKey } from '@solana/web3.js';
|
|
5
|
-
|
|
6
|
-
// Program ID from environment or default
|
|
7
|
-
const getProgramId = (): PublicKey => {
|
|
8
|
-
const PROGRAM_ID_STRING = typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_PROGRAM_ID
|
|
9
|
-
? process.env.NEXT_PUBLIC_PROGRAM_ID
|
|
10
|
-
: 'Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS';
|
|
11
|
-
|
|
12
|
-
try {
|
|
13
|
-
return new PublicKey(PROGRAM_ID_STRING);
|
|
14
|
-
} catch (error) {
|
|
15
|
-
console.error('Invalid Program ID:', PROGRAM_ID_STRING, error);
|
|
16
|
-
// Fallback to default
|
|
17
|
-
return new PublicKey('Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS');
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const PROGRAM_ID = getProgramId();
|
|
22
|
-
|
|
23
|
-
export interface UseProgramOptions {
|
|
24
|
-
idlPath?: string; // Path to IDL file, default: '/web3_prediction_platform.json'
|
|
25
|
-
programId?: PublicKey; // Override program ID
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export const useProgram = (options: UseProgramOptions = {}) => {
|
|
29
|
-
const { connection } = useConnection();
|
|
30
|
-
const wallet = useWallet();
|
|
31
|
-
const [idl, setIdl] = useState<Idl | null>(null);
|
|
32
|
-
const [idlError, setIdlError] = useState<string | null>(null);
|
|
33
|
-
|
|
34
|
-
const idlPath = options.idlPath || '/web3_prediction_platform.json';
|
|
35
|
-
const programId = options.programId || PROGRAM_ID;
|
|
36
|
-
|
|
37
|
-
// Load IDL from public folder (works with static export)
|
|
38
|
-
useEffect(() => {
|
|
39
|
-
const loadIdl = async () => {
|
|
40
|
-
try {
|
|
41
|
-
const response = await fetch(idlPath);
|
|
42
|
-
if (!response.ok) {
|
|
43
|
-
throw new Error(`Failed to load IDL: ${response.statusText}`);
|
|
44
|
-
}
|
|
45
|
-
const idlData = await response.json();
|
|
46
|
-
setIdl(idlData as Idl);
|
|
47
|
-
setIdlError(null);
|
|
48
|
-
} catch (error: any) {
|
|
49
|
-
console.error('Error loading IDL:', error);
|
|
50
|
-
setIdlError(error.message || 'Failed to load IDL');
|
|
51
|
-
setIdl(null);
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
loadIdl();
|
|
56
|
-
}, [idlPath]);
|
|
57
|
-
|
|
58
|
-
const provider = useMemo(() => {
|
|
59
|
-
if (!wallet.publicKey || !connection) return null;
|
|
60
|
-
|
|
61
|
-
try {
|
|
62
|
-
return new AnchorProvider(
|
|
63
|
-
connection,
|
|
64
|
-
wallet as any,
|
|
65
|
-
{ commitment: 'confirmed' }
|
|
66
|
-
);
|
|
67
|
-
} catch (error) {
|
|
68
|
-
console.error('Error creating provider:', error);
|
|
69
|
-
return null;
|
|
70
|
-
}
|
|
71
|
-
}, [connection, wallet]);
|
|
72
|
-
|
|
73
|
-
const program = useMemo(() => {
|
|
74
|
-
if (!provider || !idl) return null;
|
|
75
|
-
|
|
76
|
-
try {
|
|
77
|
-
// Anchor Program constructor: new Program(idl, programId, provider)
|
|
78
|
-
// @ts-ignore - TypeScript inference issue with Program constructor argument types
|
|
79
|
-
return new Program(idl as any, programId, provider as AnchorProvider) as any;
|
|
80
|
-
} catch (error) {
|
|
81
|
-
console.error('Error creating program:', error);
|
|
82
|
-
return null;
|
|
83
|
-
}
|
|
84
|
-
}, [provider, idl, programId]);
|
|
85
|
-
|
|
86
|
-
return {
|
|
87
|
-
program,
|
|
88
|
-
provider,
|
|
89
|
-
idl,
|
|
90
|
-
idlError,
|
|
91
|
-
programId
|
|
92
|
-
};
|
|
1
|
+
import { useMemo, useState, useEffect } from 'react';
|
|
2
|
+
import { useConnection, useWallet } from '@solana/wallet-adapter-react';
|
|
3
|
+
import { Program, AnchorProvider, Idl } from '@coral-xyz/anchor';
|
|
4
|
+
import { PublicKey } from '@solana/web3.js';
|
|
5
|
+
|
|
6
|
+
// Program ID from environment or default
|
|
7
|
+
const getProgramId = (): PublicKey => {
|
|
8
|
+
const PROGRAM_ID_STRING = typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_PROGRAM_ID
|
|
9
|
+
? process.env.NEXT_PUBLIC_PROGRAM_ID
|
|
10
|
+
: 'Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS';
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
return new PublicKey(PROGRAM_ID_STRING);
|
|
14
|
+
} catch (error) {
|
|
15
|
+
console.error('Invalid Program ID:', PROGRAM_ID_STRING, error);
|
|
16
|
+
// Fallback to default
|
|
17
|
+
return new PublicKey('Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS');
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const PROGRAM_ID = getProgramId();
|
|
22
|
+
|
|
23
|
+
export interface UseProgramOptions {
|
|
24
|
+
idlPath?: string; // Path to IDL file, default: '/web3_prediction_platform.json'
|
|
25
|
+
programId?: PublicKey; // Override program ID
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const useProgram = (options: UseProgramOptions = {}) => {
|
|
29
|
+
const { connection } = useConnection();
|
|
30
|
+
const wallet = useWallet();
|
|
31
|
+
const [idl, setIdl] = useState<Idl | null>(null);
|
|
32
|
+
const [idlError, setIdlError] = useState<string | null>(null);
|
|
33
|
+
|
|
34
|
+
const idlPath = options.idlPath || '/web3_prediction_platform.json';
|
|
35
|
+
const programId = options.programId || PROGRAM_ID;
|
|
36
|
+
|
|
37
|
+
// Load IDL from public folder (works with static export)
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
const loadIdl = async () => {
|
|
40
|
+
try {
|
|
41
|
+
const response = await fetch(idlPath);
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
throw new Error(`Failed to load IDL: ${response.statusText}`);
|
|
44
|
+
}
|
|
45
|
+
const idlData = await response.json();
|
|
46
|
+
setIdl(idlData as Idl);
|
|
47
|
+
setIdlError(null);
|
|
48
|
+
} catch (error: any) {
|
|
49
|
+
console.error('Error loading IDL:', error);
|
|
50
|
+
setIdlError(error.message || 'Failed to load IDL');
|
|
51
|
+
setIdl(null);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
loadIdl();
|
|
56
|
+
}, [idlPath]);
|
|
57
|
+
|
|
58
|
+
const provider = useMemo(() => {
|
|
59
|
+
if (!wallet.publicKey || !connection) return null;
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
return new AnchorProvider(
|
|
63
|
+
connection,
|
|
64
|
+
wallet as any,
|
|
65
|
+
{ commitment: 'confirmed' }
|
|
66
|
+
);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
console.error('Error creating provider:', error);
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
}, [connection, wallet]);
|
|
72
|
+
|
|
73
|
+
const program = useMemo(() => {
|
|
74
|
+
if (!provider || !idl) return null;
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
// Anchor Program constructor: new Program(idl, programId, provider)
|
|
78
|
+
// @ts-ignore - TypeScript inference issue with Program constructor argument types
|
|
79
|
+
return new Program(idl as any, programId, provider as AnchorProvider) as any;
|
|
80
|
+
} catch (error) {
|
|
81
|
+
console.error('Error creating program:', error);
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}, [provider, idl, programId]);
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
program,
|
|
88
|
+
provider,
|
|
89
|
+
idl,
|
|
90
|
+
idlError,
|
|
91
|
+
programId
|
|
92
|
+
};
|
|
93
93
|
};
|