nucleus_sdk/
lib.rs

1pub mod error;
2pub mod config;
3pub mod calldata_queue;
4pub mod utils;
5pub use error::{Result, InvalidInputsError, ProtocolError, SdkError};
6pub use crate::config::{DEFAULT_BASE_URL};
7
8mod client;
9use std::sync::Arc;
10
11pub struct SDK {
12    pub client: Arc<client::Client>
13}
14
15impl SDK {
16    /// Creates a new SDK instance
17    pub async fn new(api_key: String, base_url: Option<String>) -> Result<Self> {
18        let base_url = base_url.unwrap_or_else(|| 
19            DEFAULT_BASE_URL.to_string()
20        );
21        
22        let client = Arc::new(client::Client::new(api_key, Some(base_url)).await?);
23        
24        Ok(Self { client })
25    }
26
27    /// Creates a new SDK instance with custom configuration
28    pub async fn with_config(config: SDKConfig) -> Result<Self> {
29        let client = Arc::new(client::Client::new(
30            config.api_key,
31            Some(config.base_url),
32        ).await?);
33        
34        Ok(Self { client })
35    }
36}
37
38pub struct SDKConfig {
39    pub api_key: String,
40    pub base_url: String,
41    // Add other configuration options as needed
42}
43
44impl Default for SDKConfig {
45    fn default() -> Self {
46        Self {
47            api_key: String::new(),
48            base_url: DEFAULT_BASE_URL.to_string(),
49        }
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56    use serde_json::json;
57    use mockito::{self, Mock, ServerGuard};
58
59    // Helper function to create a mock server with address book endpoint
60    fn setup_mock_server() -> (ServerGuard, Mock) {
61        let mut server = mockito::Server::new();
62        let fake_address_book = json!({
63            "testnet": {
64                "id": 1,
65                "nucleus": {
66                    "TEST": {
67                        "manager": "0x1234567890123456789012345678901234567890"
68                    }
69                }
70            }
71        });
72
73        // Mock the complete address book URL
74        // Note: This should match the actual endpoint your client is calling
75        let mock = server.mock("GET", "/v1/addressbook")  // Adjust this path to match your ADDRESS_BOOK_ENDPOINT
76            .with_status(200)
77            .with_header("content-type", "application/json")
78            .with_body(fake_address_book.to_string())
79            .expect(1)  // Expect exactly one call
80            .create();
81
82        (server, mock)
83    }
84
85    async fn test_sdk_new() {
86        let (server, mock) = setup_mock_server();
87        
88        let base_url = server.url();
89        let sdk = SDK::new("dummy_api_key".to_string(), Some(base_url)).await;
90        
91        // Print any error for debugging
92        if let Err(ref e) = sdk {
93            println!("SDK creation failed: {:?}", e);
94        }
95        
96        assert!(sdk.is_ok(), "SDK::new should succeed with valid address book response");
97        mock.assert();  // Verify the mock was called
98    }
99
100    async fn test_sdk_with_config() {
101        let (server, mock) = setup_mock_server();
102
103        let config = SDKConfig {
104            api_key: "dummy_api_key".to_string(),
105            base_url: server.url(),
106        };
107
108        let sdk = SDK::with_config(config).await;
109        println!("SDK: {:?}", sdk.is_ok());
110        assert!(sdk.is_ok(), "SDK::with_config should succeed with valid configuration");
111    }
112}