switchboard-on-demand 0.12.1

A Rust library to interact with the Switchboard Solana program.
Documentation
1
2
3
4
5
6
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
77
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
use switchboard_utils::SbError;
use borsh::{BorshDeserialize, BorshSerialize};
use crate::Pubkey;
use crate::cfg_client;
use std::str::FromStr;

/// Switchboard Subscriber Program ID
pub const SUBSCRIPTION_PROGRAM_ID: &str = "SbsCRB7Y3SGNEwVgvwE4R8ZYuhertgc7ekUXQbD6pNH";

/// SWTCH Token Mint Address
pub const SWTCH_MINT: &str = "SW1TCHLmRGTfW5xZknqQdpdarB8PD95sJYWpNp9TbFx";

/// SWTCH/USDT Feed ID for oracle pricing
pub const SWTCH_FEED_ID: &str =
    "148aaedb33ff23593805377f131af7fe01b4770ae2b9e4a2f7f8b3a180314711";

/// Subscription account information
#[derive(Debug, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize)]
pub struct SubscriptionInfo {
    pub owner: Pubkey,
    pub tier_id: u16,
    pub tier_id_in_transition: u16,
    pub subscription_start_epoch: i64,
    pub subscription_end_epoch: i64,
    pub last_tier_change_epoch: i64,
    pub is_active: u8,
    pub authorized_users: Vec<Pubkey>,
    pub authorized_user_count: u8,
    pub total_tokens_paid: u64,
    pub last_payment_amount: u64,
    pub last_payment_token: Pubkey,
    pub contact_name: Vec<u8>,
    pub contact_email: Vec<u8>,
}

impl SubscriptionInfo {
    /// Deserialize from account data
    pub fn deserialize(data: &[u8]) -> Result<Self, SbError> {
        BorshDeserialize::try_from_slice(data).map_err(|e| SbError::CustomError {
            message: format!("Failed to deserialize subscription info: {}", e),
            source: std::sync::Arc::new(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                e,
            )),
        })
    }

    /// Check if subscription is currently active
    pub fn is_subscription_active(&self) -> bool {
        self.is_active == 1
    }

    /// Check if subscription has expired
    pub fn is_expired(&self, current_epoch: i64) -> bool {
        current_epoch > self.subscription_end_epoch
    }

    /// Get remaining epochs
    pub fn remaining_epochs(&self, current_epoch: i64) -> i64 {
        (self.subscription_end_epoch - current_epoch).max(0)
    }

    /// Get contact name as string
    pub fn contact_name_str(&self) -> Option<String> {
        if self.contact_name.is_empty() {
            None
        } else {
            String::from_utf8(self.contact_name.clone()).ok()
        }
    }

    /// Get contact email as string
    pub fn contact_email_str(&self) -> Option<String> {
        if self.contact_email.is_empty() {
            None
        } else {
            String::from_utf8(self.contact_email.clone()).ok()
        }
    }

    /// Check if a user is authorized
    pub fn is_user_authorized(&self, user: &Pubkey) -> bool {
        self.authorized_users.contains(user)
    }
}

/// Subscription tier levels
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u16)]
pub enum SubscriptionTier {
    Free = 1,
    Pro = 2,
    Surge = 3,
    Enterprise = 10,
}

impl SubscriptionTier {
    pub fn from_id(id: u16) -> Option<Self> {
        match id {
            1 => Some(Self::Free),
            2 => Some(Self::Pro),
            3 => Some(Self::Surge),
            10 => Some(Self::Enterprise),
            _ => None,
        }
    }

    pub fn as_id(&self) -> u16 {
        *self as u16
    }
}

/// PDA derivation helpers
pub mod pda {
    use super::*;

    /// Derive the state PDA
    pub fn get_state_pda(program_id: &Pubkey) -> (Pubkey, u8) {
        Pubkey::find_program_address(&[b"STATE"], program_id)
    }

    /// Derive the tier PDA
    pub fn get_tier_pda(tier_id: u16, program_id: &Pubkey) -> (Pubkey, u8) {
        let tier_id_bytes = tier_id.to_le_bytes();
        Pubkey::find_program_address(&[b"TIER", &tier_id_bytes], program_id)
    }

    /// Derive the subscription PDA
    pub fn get_subscription_pda(owner: &Pubkey, program_id: &Pubkey) -> (Pubkey, u8) {
        Pubkey::find_program_address(&[b"SUBSCRIPTION", owner.as_ref()], program_id)
    }

    /// Derive the token vault PDA
    pub fn get_token_vault_pda(mint: &Pubkey, program_id: &Pubkey) -> (Pubkey, u8) {
        Pubkey::find_program_address(&[b"TOKEN_VAULT", mint.as_ref()], program_id)
    }
}

/// Get the subscription program ID
pub fn get_program_id() -> Pubkey {
    Pubkey::from_str(SUBSCRIPTION_PROGRAM_ID).unwrap()
}

/// Get the SWTCH mint address
pub fn get_swtch_mint() -> Pubkey {
    Pubkey::from_str(SWTCH_MINT).unwrap()
}

cfg_client! {
    pub mod client_utils {
        use super::*;
        use crate::client::Queue;
        use std::sync::Arc;

        #[cfg(feature = "solana-v2")]
        use crate::solana_compat::solana_client::nonblocking::rpc_client::RpcClient;
        #[cfg(feature = "solana-v2")]
        use crate::solana_sdk::signature::Keypair;
        use crate::Instruction;

    /// Parameters for creating a new subscription
    pub struct CreateSubscriptionParams {
        /// Solana RPC client
        pub client: Arc<RpcClient>,
        /// Payer and subscription owner
        pub payer: Arc<Keypair>,
        /// Tier ID (1=Free, 2=Pro, 3=Surge, 10+=Enterprise)
        pub tier_id: u16,
        /// Number of epochs to pay for (minimum 2)
        pub epoch_amount: u64,
        /// Optional contact name
        pub contact_name: Option<String>,
        /// Optional contact email
        pub contact_email: Option<String>,
        /// Switchboard queue for oracle updates
        pub queue: Queue,
    }

    /// Parameters for upgrading a subscription
    pub struct UpgradeSubscriptionParams {
        /// Solana RPC client
        pub client: Arc<RpcClient>,
        /// Subscription owner
        pub owner: Arc<Keypair>,
        /// New tier ID (must be more expensive than current)
        pub new_tier_id: u16,
        /// Number of epochs requested (credit applied, minimum 1)
        pub epoch_amount: u64,
        /// Switchboard queue for oracle updates
        pub queue: Queue,
    }

    /// Parameters for downgrading a subscription
    pub struct DowngradeSubscriptionParams {
        /// Solana RPC client
        pub client: Arc<RpcClient>,
        /// Subscription owner
        pub owner: Arc<Keypair>,
        /// New tier ID (must be less expensive than current)
        pub new_tier_id: u16,
    }

    /// Parameters for extending a subscription
    pub struct ExtendSubscriptionParams {
        /// Solana RPC client
        pub client: Arc<RpcClient>,
        /// Subscription owner (or admin if admin_extend=true)
        pub owner: Arc<Keypair>,
        /// Number of epochs to add
        pub epoch_amount: u64,
        /// If true, no payment required (admin only)
        pub admin_extend: bool,
        /// Switchboard queue for oracle updates (required if admin_extend=false)
        pub queue: Option<Queue>,
    }

    /// Parameters for managing team members
    pub struct ManageTeamMemberParams {
        /// Solana RPC client
        pub client: Arc<RpcClient>,
        /// Subscription owner
        pub owner: Arc<Keypair>,
        /// User to add/remove
        pub user: Pubkey,
    }

    /// Subscription manager for handling all subscription operations
    pub struct SubscriptionManager {
        program_id: Pubkey,
    }

    impl SubscriptionManager {
        /// Create a new subscription manager
        pub fn new() -> Self {
            Self {
                program_id: get_program_id(),
            }
        }

        /// Fetch subscription info from chain
        pub async fn fetch_subscription(
            &self,
            client: &RpcClient,
            owner: &Pubkey,
        ) -> Result<SubscriptionInfo, SbError> {
            let (subscription_pda, _) = pda::get_subscription_pda(owner, &self.program_id);

            let account_data = client
                .get_account_data(&subscription_pda)
                .await
                .map_err(|e| SbError::CustomError {
                    message: format!("Failed to fetch subscription account: {}", e),
                    source: Arc::new(std::io::Error::new(std::io::ErrorKind::Other, e)),
                })?;

            SubscriptionInfo::deserialize(&account_data)
        }

        /// Add a team member to the subscription
        pub fn add_team_member_ix(
            &self,
            owner: &Pubkey,
            user: &Pubkey,
        ) -> Result<Instruction, SbError> {
            let (subscription_pda, _) = pda::get_subscription_pda(owner, &self.program_id);

            // Build instruction data: discriminator (8) + user (32)
            let mut data = Vec::with_capacity(40);
            // Discriminator for add_team_member
            data.extend_from_slice(&[1, 0, 0, 0, 0, 0, 0, 0]);
            data.extend_from_slice(user.as_ref());

            Ok(Instruction {
                program_id: self.program_id,
                accounts: vec![
                    crate::AccountMeta::new(subscription_pda, false),
                    crate::AccountMeta::new_readonly(*owner, true),
                ],
                data,
            })
        }

        /// Remove a team member from the subscription
        pub fn remove_team_member_ix(
            &self,
            owner: &Pubkey,
            user: &Pubkey,
        ) -> Result<Instruction, SbError> {
            let (subscription_pda, _) = pda::get_subscription_pda(owner, &self.program_id);

            // Build instruction data: discriminator (8) + user (32)
            let mut data = Vec::with_capacity(40);
            // Discriminator for remove_team_member
            data.extend_from_slice(&[2, 0, 0, 0, 0, 0, 0, 0]);
            data.extend_from_slice(user.as_ref());

            Ok(Instruction {
                program_id: self.program_id,
                accounts: vec![
                    crate::AccountMeta::new(subscription_pda, false),
                    crate::AccountMeta::new_readonly(*owner, true),
                ],
                data,
            })
        }

        /// Check if a user is a team member
        pub async fn is_team_member(
            &self,
            client: &RpcClient,
            owner: &Pubkey,
            user: &Pubkey,
        ) -> Result<bool, SbError> {
            let subscription_info = self.fetch_subscription(client, owner).await?;
            Ok(subscription_info.is_user_authorized(user))
        }

        /// Get all team members
        pub async fn get_team_members(
            &self,
            client: &RpcClient,
            owner: &Pubkey,
        ) -> Result<Vec<Pubkey>, SbError> {
            let subscription_info = self.fetch_subscription(client, owner).await?;
            Ok(subscription_info.authorized_users)
        }
    }

    impl Default for SubscriptionManager {
        fn default() -> Self {
            Self::new()
        }
    }

    /// Helper to get the quote account for SWTCH/USDT feed
    pub fn get_quote_account(_queue_pubkey: &Pubkey) -> Pubkey {
        // This would use OracleQuote::getCanonicalPubkey
        // For now, return a placeholder
        // TODO: Implement proper quote account derivation
        Pubkey::default()
    }
} // close cfg_client!
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_subscription_tier_conversion() {
        assert_eq!(SubscriptionTier::Free.as_id(), 1);
        assert_eq!(SubscriptionTier::Pro.as_id(), 2);
        assert_eq!(SubscriptionTier::Surge.as_id(), 3);
        assert_eq!(SubscriptionTier::Enterprise.as_id(), 10);

        assert_eq!(SubscriptionTier::from_id(1), Some(SubscriptionTier::Free));
        assert_eq!(SubscriptionTier::from_id(2), Some(SubscriptionTier::Pro));
        assert_eq!(
            SubscriptionTier::from_id(3),
            Some(SubscriptionTier::Surge)
        );
        assert_eq!(
            SubscriptionTier::from_id(10),
            Some(SubscriptionTier::Enterprise)
        );
        assert_eq!(SubscriptionTier::from_id(999), None);
    }

    #[test]
    fn test_pda_derivation() {
        let program_id = get_program_id();
        let owner = Pubkey::new_unique();
        let mint = get_swtch_mint();

        // Test state PDA derivation
        let (state_pda, _) = pda::get_state_pda(&program_id);
        assert!(state_pda != Pubkey::default());

        // Test tier PDA derivation
        let (tier_pda, _) = pda::get_tier_pda(2, &program_id);
        assert!(tier_pda != Pubkey::default());

        // Test subscription PDA derivation
        let (sub_pda, _) = pda::get_subscription_pda(&owner, &program_id);
        assert!(sub_pda != Pubkey::default());

        // Test token vault PDA derivation
        let (vault_pda, _) = pda::get_token_vault_pda(&mint, &program_id);
        assert!(vault_pda != Pubkey::default());
    }

    #[test]
    fn test_program_id() {
        let program_id = get_program_id();
        assert_eq!(
            program_id.to_string(),
            "SbsCRB7Y3SGNEwVgvwE4R8ZYuhertgc7ekUXQbD6pNH"
        );
    }

    #[test]
    fn test_swtch_mint() {
        let mint = get_swtch_mint();
        assert_eq!(
            mint.to_string(),
            "SW1TCHLmRGTfW5xZknqQdpdarB8PD95sJYWpNp9TbFx"
        );
    }

    #[test]
    fn test_subscription_info_helpers() {
        let sub_info = SubscriptionInfo {
            owner: Pubkey::new_unique(),
            tier_id: 2,
            tier_id_in_transition: 2,
            subscription_start_epoch: 100,
            subscription_end_epoch: 200,
            last_tier_change_epoch: 100,
            is_active: 1,
            authorized_users: vec![Pubkey::new_unique(), Pubkey::new_unique()],
            authorized_user_count: 2,
            total_tokens_paid: 1000,
            last_payment_amount: 500,
            last_payment_token: get_swtch_mint(),
            contact_name: b"Alice".to_vec(),
            contact_email: b"alice@example.com".to_vec(),
        };

        assert!(sub_info.is_subscription_active());
        assert!(!sub_info.is_expired(150));
        assert!(sub_info.is_expired(201));
        assert_eq!(sub_info.remaining_epochs(150), 50);
        assert_eq!(sub_info.contact_name_str(), Some("Alice".to_string()));
        assert_eq!(
            sub_info.contact_email_str(),
            Some("alice@example.com".to_string())
        );
    }
}