inf_circle_sdk/lib.rs
1//! # Circle Rust SDK
2//!
3//! A comprehensive Rust SDK for [Circle's Web3 Services API](https://developers.circle.com/), designed with
4//! a clean separation between write (`CircleOps`) and read (`CircleView`) operations.
5//!
6//! ## Architecture
7//!
8//! The SDK provides two main clients:
9//!
10//! - **[`CircleOps`](circle_ops::circler_ops::CircleOps)**: Handles all write operations (POST, PUT, PATCH)
11//! that require entity-level authentication. Uses an entity secret to sign requests.
12//! - **[`CircleView`](circle_view::circle_view::CircleView)**: Handles all read operations (GET) that
13//! only require API key authentication.
14//!
15//! This separation ensures that read-only processes don't require access to sensitive entity secrets,
16//! enhancing security and simplifying access control.
17//!
18//! ## Features
19//!
20//! - ✅ **Clean Separation**: Write operations require entity secret, reads only need API key
21//! - ✅ **Async First**: Built on `tokio` for high-performance async I/O
22//! - ✅ **Type Safety**: Strongly typed request/response structures prevent common errors
23//! - ✅ **Fluent Builders**: Easy-to-use builders for complex API requests
24//! - ✅ **Comprehensive Error Handling**: Detailed error types with helpful messages
25//! - ✅ **Developer Wallets**: Create, manage, and transact with developer-controlled wallets
26//! - ✅ **Smart Contracts**: Deploy, import, query, and interact with smart contracts
27//! - ✅ **Event Monitoring**: Create monitors for contract events and retrieve event logs
28//! - ✅ **Webhook Support**: Subscribe to and manage webhook notifications
29//!
30//! ## Quick Start
31//!
32//! ### Environment Setup
33//!
34//! Create a `.env` file in your project root:
35//!
36//! ```bash
37//! CIRCLE_BASE_URL="https://api.circle.com"
38//! CIRCLE_API_KEY="YOUR_API_KEY"
39//! CIRCLE_ENTITY_SECRET="YOUR_ENTITY_SECRET_HEX"
40//! CIRCLE_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----\n...\n-----END PUBLIC KEY-----"
41//! CIRCLE_WALLET_SET_ID="YOUR_WALLET_SET_ID"
42//! ```
43//!
44//! ### Create a Wallet
45//!
46//! ```rust,no_run
47//! use inf_circle_sdk::{
48//! circle_ops::circler_ops::CircleOps,
49//! dev_wallet::{dto::AccountType, ops::create_dev_wallet::CreateDevWalletRequestBuilder},
50//! types::Blockchain,
51//! };
52//!
53//! #[tokio::main]
54//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
55//! let ops = CircleOps::new(None)?;
56//! let wallet_set_id = std::env::var("CIRCLE_WALLET_SET_ID")?;
57//!
58//! let builder = CreateDevWalletRequestBuilder::new(
59//! wallet_set_id,
60//! vec![Blockchain::EthSepolia]
61//! )?
62//! .account_type(AccountType::Sca)
63//! .count(1)
64//! .build();
65//!
66//! let response = ops.create_dev_wallet(builder).await?;
67//! println!("Created wallet: {}", response.wallets[0].address);
68//! Ok(())
69//! }
70//! ```
71//!
72//! ### Query Wallet Balance
73//!
74//! ```rust,no_run
75//! use inf_circle_sdk::{
76//! circle_view::circle_view::CircleView,
77//! dev_wallet::views::query::QueryParamsBuilder,
78//! };
79//!
80//! #[tokio::main]
81//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
82//! let view = CircleView::new()?;
83//!
84//! let params = QueryParamsBuilder::new().build();
85//! let balances = view.get_token_balances("wallet-id", params).await?;
86//!
87//! for balance in balances.token_balances {
88//! println!("{}: {}", balance.token.symbol.unwrap_or_default(), balance.amount);
89//! }
90//! Ok(())
91//! }
92//! ```
93//!
94//! ### Transfer Tokens
95//!
96//! ```rust,no_run
97//! use inf_circle_sdk::{
98//! circle_ops::circler_ops::CircleOps,
99//! dev_wallet::{
100//! dto::FeeLevel,
101//! ops::create_transfer_transaction::CreateTransferTransactionRequestBuilder,
102//! },
103//! types::Blockchain,
104//! };
105//! use uuid::Uuid;
106//!
107//! #[tokio::main]
108//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
109//! let ops = CircleOps::new(None)?;
110//!
111//! let builder = CreateTransferTransactionRequestBuilder::new()
112//! .wallet_id("wallet-id".to_string())
113//! .destination_address("0x1234...".to_string())
114//! .amounts(vec!["0.1".to_string()])
115//! .blockchain(Blockchain::EthSepolia)
116//! .fee_level(FeeLevel::Medium)
117//! .idempotency_key(Uuid::new_v4().to_string())
118//! .build();
119//!
120//! let response = ops.create_dev_transfer_transaction(builder).await?;
121//! println!("Transaction ID: {}", response.id);
122//! Ok(())
123//! }
124//! ```
125//!
126//! ## Examples
127//!
128//! The `examples/` directory contains comprehensive examples for all major features:
129//!
130//! - **[circle_ops_example.rs](https://github.com/Inferenco/inf-circle-sdk/examples/circle_ops_example.rs)**: Wallet creation
131//! - **[transfer_transaction_example.rs](https://github.com/Inferenco/inf-circle-sdk/examples/transfer_transaction_example.rs)**: Native and ERC-20 token transfers
132//! - **[contract_interaction_example.rs](https://github.com/Inferenco/inf-circle-sdk/examples/contract_interaction_example.rs)**: Execute contract functions
133//! - **[deploy_contract_example.rs](https://github.com/Inferenco/inf-circle-sdk/examples/deploy_contract_example.rs)**: Deploy custom contracts
134//! - **[create_event_monitor_example.rs](https://github.com/Inferenco/inf-circle-sdk/examples/create_event_monitor_example.rs)**: Monitor contract events
135//! - **[sign_message_example.rs](https://github.com/Inferenco/inf-circle-sdk/examples/sign_message_example.rs)**: Sign messages and typed data
136//! - **[transaction_management_example.rs](https://github.com/Inferenco/inf-circle-sdk/examples/transaction_management_example.rs)**: Cancel and accelerate transactions
137//!
138//! Run any example with:
139//! ```bash
140//! cargo run --example circle_ops_example
141//! ```
142//!
143//! ## Module Organization
144//!
145//! - [`circle_ops`]: Write operations requiring entity secret authentication
146//! - [`circle_view`]: Read operations requiring only API key
147//! - [`dev_wallet`]: Developer-controlled wallet operations and views
148//! - [`contract`]: Smart contract deployment, import, and interaction
149//! - [`types`]: Common types used across the SDK (blockchains, etc.)
150//! - [`helper`]: Utility functions and error handling
151//!
152//! ## Error Handling
153//!
154//! The SDK uses a custom [`CircleError`](helper::CircleError) type for comprehensive error reporting:
155//!
156//! ```rust,no_run
157//! # use inf_circle_sdk::circle_ops::circler_ops::CircleOps;
158//! # use inf_circle_sdk::dev_wallet::ops::create_dev_wallet::CreateDevWalletRequestBuilder;
159//! # use inf_circle_sdk::dev_wallet::dto::AccountType;
160//! # use inf_circle_sdk::types::Blockchain;
161//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
162//! # let ops = CircleOps::new(None)?;
163//! # let wallet_set_id = "test".to_string();
164//! # let builder = CreateDevWalletRequestBuilder::new(wallet_set_id, vec![Blockchain::EthSepolia])?.build();
165//! match ops.create_dev_wallet(builder).await {
166//! Ok(response) => println!("Success!"),
167//! Err(e) => eprintln!("Error: {}", e), // Detailed error message
168//! }
169//! # Ok(())
170//! # }
171//! ```
172//!
173//! ## Testing
174//!
175//! See [TESTING.md](https://github.com/Inferenco/inf-circle-sdk/TESTING.md) for comprehensive testing guide.
176
177pub mod circle_ops;
178pub mod circle_view;
179pub mod contract;
180pub mod dev_wallet;
181pub mod helper;
182pub mod near;
183pub mod types;
184
185// Re-export main types for convenience
186pub use helper::{encrypt_entity_secret, CircleError, CircleResult};
187
188// Re-export commonly used types
189pub use serde::{Deserialize, Serialize};
190pub use uuid::Uuid;