tally_sdk/
lib.rs

1//! Tally SDK - Rust SDK for the Solana Subscriptions Platform
2//!
3//! This crate provides a comprehensive Rust SDK for interacting with the Tally
4//! subscription program on Solana. It includes utilities for:
5//!
6//! - Computing Program Derived Addresses (PDAs) and Associated Token Accounts (ATAs)
7//! - Building subscription transactions (approve→start, revoke→cancel flows)
8//! - Token program detection (SPL Token vs Token-2022)
9//!
10//! # Example Usage
11//!
12//! ```no_run
13//! use tally_sdk::{pda, ata, SimpleTallyClient};
14//! use anchor_client::solana_sdk::pubkey::Pubkey;
15//! use anchor_client::solana_sdk::signature::{Keypair, Signer};
16//! use std::str::FromStr;
17//!
18//! # fn main() -> tally_sdk::Result<()> {
19//! // Initialize client
20//! let client = SimpleTallyClient::new("https://api.devnet.solana.com")?;
21//!
22//! // Compute PDAs
23//! let authority = Pubkey::from(Keypair::new().pubkey().to_bytes());
24//! let merchant_pda = pda::merchant_address(&authority)?;
25//! let plan_pda = pda::plan_address_from_string(&merchant_pda, "premium_plan")?;
26//!
27//! // Compute ATAs
28//! let usdc_mint = Pubkey::try_from("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v").map_err(|_| tally_sdk::TallyError::from("Invalid pubkey"))?;
29//! let user_ata = ata::get_associated_token_address_for_mint(&authority, &usdc_mint)?;
30//!
31//! # Ok(())
32//! # }
33//! ```
34
35#![forbid(unsafe_code)]
36#![deny(clippy::all)]
37#![warn(clippy::pedantic)]
38#![warn(clippy::nursery)]
39#![allow(clippy::missing_errors_doc)]
40#![allow(clippy::missing_panics_doc)]
41
42pub mod simple_client;
43// pub mod client;  // Disabled for now due to missing discriminator implementations
44pub mod ata;
45pub mod dashboard;
46pub mod dashboard_types;
47pub mod error;
48pub mod event_query;
49pub mod events;
50pub mod keypair;
51pub mod pda;
52pub mod program_types;
53pub mod signature;
54pub mod transaction_builder;
55pub mod transaction_utils;
56pub mod utils;
57pub mod validation;
58
59// Re-export commonly used items
60pub use simple_client::SimpleTallyClient;
61// pub use client::TallyClient;  // Disabled for now
62pub use dashboard::DashboardClient;
63pub use dashboard_types::{
64    DashboardEvent, DashboardEventType, DashboardSubscription, EventStream, Overview,
65    PlanAnalytics, SubscriptionStatus,
66};
67pub use error::{Result, TallyError};
68pub use event_query::{EventQueryClient, EventQueryClientConfig, EventQueryConfig, ParsedEvent};
69pub use events::{
70    create_receipt, create_receipt_legacy, extract_memo_from_logs, parse_events_from_logs,
71    parse_events_with_context, Canceled, ParsedEventWithContext, PaymentFailed, ReceiptParams,
72    Renewed, StreamableEventData, Subscribed, TallyEvent, TallyReceipt,
73};
74pub use keypair::load_keypair;
75pub use program_types::*;
76pub use transaction_builder::{
77    accept_authority, admin_withdraw_fees, cancel_authority_transfer, cancel_subscription,
78    close_subscription, create_merchant, create_plan, init_config, pause, renew_subscription,
79    start_subscription, transfer_authority, unpause, update_config, update_merchant_tier,
80    update_plan_terms, AcceptAuthorityBuilder, AdminWithdrawFeesBuilder,
81    CancelAuthorityTransferBuilder, CancelSubscriptionBuilder, CloseSubscriptionBuilder,
82    CreateMerchantBuilder, CreatePlanBuilder, InitConfigBuilder, PauseBuilder,
83    RenewSubscriptionBuilder, StartSubscriptionBuilder, TransferAuthorityBuilder, UnpauseBuilder,
84    UpdateConfigBuilder, UpdateMerchantTierBuilder, UpdatePlanTermsBuilder,
85};
86pub use validation::*;
87
88// Re-export signature verification and transaction signing utilities
89pub use signature::{
90    extract_transaction_signature, is_valid_wallet_address, normalize_signature_format,
91    prepare_transaction_for_signing, transaction_signing, verify_signed_transaction,
92    verify_wallet_signature,
93};
94
95// Re-export transaction utilities
96pub use transaction_utils::{
97    build_transaction, convert_anchor_pubkey, create_memo_instruction, get_user_usdc_ata,
98    map_tally_error_to_string, SubscribeTransactionParams,
99};
100
101// Re-export general utilities
102pub use utils::{
103    calculate_next_renewal, format_duration, is_renewal_due, is_subscription_overdue,
104    is_valid_pubkey, micro_lamports_to_usdc, system_programs, usdc_to_micro_lamports,
105};
106
107// Re-export commonly used external types
108pub use anchor_client::solana_account_decoder;
109pub use anchor_client::solana_client;
110pub use anchor_client::solana_sdk;
111pub use anchor_client::ClientError;
112pub use anchor_lang::{AnchorDeserialize, AnchorSerialize};
113pub use spl_associated_token_account;
114pub use spl_token;
115
116/// Default/fallback program ID (when no environment override is provided)
117pub const DEFAULT_PROGRAM_ID: &str = "Fwrs8tRRtw8HwmQZFS3XRRVcKBQhe1nuZ5heB4FgySXV";
118
119/// Get the program ID as a string, checking environment first, then falling back to default
120#[must_use]
121pub fn program_id_string() -> String {
122    std::env::var("PROGRAM_ID").unwrap_or_else(|_| DEFAULT_PROGRAM_ID.to_string())
123}
124
125/// Get the program ID as a `Pubkey`
126///
127/// # Panics
128/// Panics if the program ID (from environment or default) is not a valid Pubkey
129#[must_use]
130pub fn program_id() -> anchor_client::solana_sdk::pubkey::Pubkey {
131    program_id_string().parse().expect("Valid program ID")
132}