|
1 | | -import React, { createContext, useState, useMemo, useContext } from 'react'; |
2 | | -import { clusterApiUrl } from '@solana/web3.js'; |
| 1 | +import React, { createContext, useState, useMemo, useContext, useEffect } from 'react'; |
| 2 | +import { Connection } from '@solana/web3.js'; |
| 3 | +import { SVM_NETWORKS, getNetworkConfig, getDefaultNetworkConfig } from '../config/networks'; |
| 4 | +import { useProgram } from '../hooks/useProgram'; |
| 5 | +import { usePhantomWallet } from './PhantomWalletProvider'; |
3 | 6 |
|
4 | 7 | // Create a context for global app state |
5 | 8 | export const AppContext = createContext({ |
6 | 9 | network: null, |
7 | 10 | networks: {}, |
8 | 11 | selectedNetwork: 'solana', |
9 | 12 | setSelectedNetwork: () => {}, |
| 13 | + connection: null, |
| 14 | + program: null, |
10 | 15 | }); |
11 | 16 |
|
12 | | -// SVM Networks configuration |
13 | | -const SVM_NETWORKS = { |
14 | | - 'solana': { |
15 | | - name: 'Solana', |
16 | | - endpoint: clusterApiUrl('devnet'), |
17 | | - programId: 'YOUR_SOLANA_PROGRAM_ID', |
18 | | - icon: '/assets/images/solana-logo.svg', |
19 | | - color: '#9945FF', |
20 | | - explorerUrl: 'https://explorer.solana.com', |
21 | | - }, |
22 | | - 'sonic': { |
23 | | - name: 'Sonic', |
24 | | - endpoint: 'https://sonic-api.example.com', |
25 | | - programId: 'YOUR_SONIC_PROGRAM_ID', |
26 | | - icon: '/assets/images/sonic-logo.svg', |
27 | | - color: '#00C2FF', |
28 | | - explorerUrl: 'https://explorer.sonic.example.com', |
29 | | - }, |
30 | | - 'eclipse': { |
31 | | - name: 'Eclipse', |
32 | | - endpoint: 'https://eclipse-api.example.com', |
33 | | - programId: 'YOUR_ECLIPSE_PROGRAM_ID', |
34 | | - icon: '/assets/images/eclipse-logo.svg', |
35 | | - color: '#0052FF', |
36 | | - explorerUrl: 'https://explorer.eclipse.example.com', |
37 | | - }, |
38 | | - 'svmBNB': { |
39 | | - name: 'svmBNB', |
40 | | - endpoint: 'https://svmbnb-api.example.com', |
41 | | - programId: 'YOUR_SVMBNB_PROGRAM_ID', |
42 | | - icon: '/assets/images/svmbnb-logo.svg', |
43 | | - color: '#F0B90B', |
44 | | - explorerUrl: 'https://explorer.svmbnb.example.com', |
45 | | - }, |
46 | | - 's00n': { |
47 | | - name: 's00n', |
48 | | - endpoint: 'https://s00n-api.example.com', |
49 | | - programId: 'YOUR_S00N_PROGRAM_ID', |
50 | | - icon: '/assets/images/s00n-logo.svg', |
51 | | - color: '#00FF9D', |
52 | | - explorerUrl: 'https://explorer.s00n.example.com', |
53 | | - } |
54 | | -}; |
55 | | - |
56 | 17 | export const AppContextProvider = ({ children }) => { |
| 18 | + // Get wallet from PhantomWalletProvider |
| 19 | + const wallet = usePhantomWallet(); |
| 20 | + |
57 | 21 | // State for selected network |
58 | 22 | const [selectedNetwork, setSelectedNetwork] = useState('solana'); |
| 23 | + const [connection, setConnection] = useState(null); |
59 | 24 |
|
60 | 25 | // Get network configuration |
61 | 26 | const network = SVM_NETWORKS[selectedNetwork]; |
62 | 27 |
|
| 28 | + // Create connection when network changes |
| 29 | + useEffect(() => { |
| 30 | + const createConnection = async () => { |
| 31 | + try { |
| 32 | + const networkConfig = getNetworkConfig(selectedNetwork); |
| 33 | + if (!networkConfig) { |
| 34 | + console.warn('[AppContext] Unknown network selected, falling back to Solana'); |
| 35 | + const defaultConfig = getDefaultNetworkConfig(); |
| 36 | + const conn = new Connection(defaultConfig.endpoint, defaultConfig.connectionConfig); |
| 37 | + setConnection(conn); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + // Try primary endpoint first |
| 42 | + try { |
| 43 | + const conn = new Connection(networkConfig.endpoint, networkConfig.connectionConfig); |
| 44 | + // Test the connection |
| 45 | + await conn.getLatestBlockhash('confirmed'); |
| 46 | + setConnection(conn); |
| 47 | + console.log(`[AppContext] Connected to ${selectedNetwork} network`); |
| 48 | + } catch (primaryError) { |
| 49 | + console.warn(`[AppContext] Primary endpoint failed for ${selectedNetwork}:`, primaryError); |
| 50 | + |
| 51 | + // Try fallback endpoints if available |
| 52 | + if (networkConfig.fallbackEndpoints?.length > 0) { |
| 53 | + for (const endpoint of networkConfig.fallbackEndpoints) { |
| 54 | + try { |
| 55 | + const conn = new Connection(endpoint, networkConfig.connectionConfig); |
| 56 | + await conn.getLatestBlockhash('confirmed'); |
| 57 | + setConnection(conn); |
| 58 | + console.log(`[AppContext] Using fallback endpoint: ${endpoint}`); |
| 59 | + return; |
| 60 | + } catch (fallbackError) { |
| 61 | + console.warn('[AppContext] Fallback endpoint failed:', endpoint, fallbackError); |
| 62 | + continue; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // If all endpoints fail, throw the original error |
| 68 | + throw primaryError; |
| 69 | + } |
| 70 | + } catch (error) { |
| 71 | + console.error('[AppContext] Failed to create connection:', error); |
| 72 | + // Set a minimal connection for error recovery |
| 73 | + const defaultConfig = getDefaultNetworkConfig(); |
| 74 | + const conn = new Connection(defaultConfig.endpoint, 'confirmed'); |
| 75 | + setConnection(conn); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + createConnection(); |
| 80 | + }, [selectedNetwork]); |
| 81 | + |
| 82 | + // Create program using the useProgram hook |
| 83 | + const program = useProgram(connection, wallet); |
| 84 | + |
63 | 85 | // Context values |
64 | 86 | const contextValue = useMemo(() => ({ |
65 | 87 | network, |
66 | 88 | networks: SVM_NETWORKS, |
67 | 89 | selectedNetwork, |
68 | 90 | setSelectedNetwork, |
69 | | - }), [network, selectedNetwork]); |
| 91 | + connection, |
| 92 | + program, |
| 93 | + }), [network, selectedNetwork, connection, program]); |
70 | 94 |
|
71 | 95 | return ( |
72 | 96 | <AppContext.Provider value={contextValue}> |
|
0 commit comments