@@ -2,18 +2,21 @@ use anyhow::Result;
22use solana_client:: nonblocking:: rpc_client:: RpcClient ;
33use solana_sdk:: commitment_config:: CommitmentConfig ;
44use std:: sync:: Arc ;
5+ use std:: collections:: HashMap ;
56use tokio:: sync:: RwLock ;
67use crate :: transport:: { Transport , JsonRpcMessage , JsonRpcNotification , JsonRpcVersion } ;
78use crate :: { Config , CustomStdioTransport } ;
89
910pub struct ServerState {
1011 pub rpc_client : RpcClient ,
12+ pub svm_clients : HashMap < String , RpcClient > ,
13+ pub config : Config ,
1114 pub initialized : bool ,
1215 pub protocol_version : String ,
1316}
1417
1518impl ServerState {
16- pub fn new ( config : & Config ) -> Self {
19+ pub fn new ( config : Config ) -> Self {
1720 let commitment = match config. commitment . as_str ( ) {
1821 "processed" => CommitmentConfig :: processed ( ) ,
1922 "confirmed" => CommitmentConfig :: confirmed ( ) ,
@@ -23,15 +26,68 @@ impl ServerState {
2326
2427 let rpc_client = RpcClient :: new_with_commitment (
2528 config. rpc_url . clone ( ) ,
26- commitment,
29+ commitment. clone ( ) ,
2730 ) ;
2831
32+ // Create RPC clients for enabled SVM networks
33+ let mut svm_clients = HashMap :: new ( ) ;
34+ for ( network_id, network) in & config. svm_networks {
35+ if network. enabled {
36+ let client = RpcClient :: new_with_commitment (
37+ network. rpc_url . clone ( ) ,
38+ commitment. clone ( ) ,
39+ ) ;
40+ svm_clients. insert ( network_id. clone ( ) , client) ;
41+ }
42+ }
43+
2944 Self {
3045 rpc_client,
46+ svm_clients,
47+ protocol_version : config. protocol_version . clone ( ) ,
48+ config,
3149 initialized : false ,
32- protocol_version : config. protocol_version . clone ( )
3350 }
3451 }
52+
53+ pub fn update_config ( & mut self , new_config : Config ) {
54+ let commitment = match new_config. commitment . as_str ( ) {
55+ "processed" => CommitmentConfig :: processed ( ) ,
56+ "confirmed" => CommitmentConfig :: confirmed ( ) ,
57+ "finalized" => CommitmentConfig :: finalized ( ) ,
58+ _ => CommitmentConfig :: default ( ) ,
59+ } ;
60+
61+ // Update main RPC client if URL changed
62+ if self . config . rpc_url != new_config. rpc_url {
63+ self . rpc_client = RpcClient :: new_with_commitment (
64+ new_config. rpc_url . clone ( ) ,
65+ commitment. clone ( ) ,
66+ ) ;
67+ }
68+
69+ // Update SVM clients
70+ self . svm_clients . clear ( ) ;
71+ for ( network_id, network) in & new_config. svm_networks {
72+ if network. enabled {
73+ let client = RpcClient :: new_with_commitment (
74+ network. rpc_url . clone ( ) ,
75+ commitment. clone ( ) ,
76+ ) ;
77+ self . svm_clients . insert ( network_id. clone ( ) , client) ;
78+ }
79+ }
80+
81+ self . config = new_config;
82+ }
83+
84+ pub fn get_enabled_networks ( & self ) -> Vec < & str > {
85+ self . config . svm_networks
86+ . iter ( )
87+ . filter ( |( _, network) | network. enabled )
88+ . map ( |( id, _) | id. as_str ( ) )
89+ . collect ( )
90+ }
3591}
3692
3793pub async fn start_server ( ) -> Result < ( ) > {
@@ -40,7 +96,7 @@ pub async fn start_server() -> Result<()> {
4096 let config = Config :: load ( ) ?;
4197 log:: info!( "Loaded config: RPC URL: {}, Protocol Version: {}" , config. rpc_url, config. protocol_version) ;
4298
43- let state = Arc :: new ( RwLock :: new ( ServerState :: new ( & config) ) ) ;
99+ let state = Arc :: new ( RwLock :: new ( ServerState :: new ( config. clone ( ) ) ) ) ;
44100
45101 let transport = CustomStdioTransport :: new ( ) ;
46102 transport. open ( ) ?;
0 commit comments