delta_domain_sdk/lib.rs
1//! # delta domain SDK
2//!
3//! This crate provides a comprehensive SDK for building and running domains on
4//! delta network. It includes components for managing the domain lifecycle,
5//! handling transactions, storing state, and communicating with the base layer
6//! (delta network).
7//!
8//! ## Running a Domain
9//!
10//! The [`Domain`] struct is the entry point for building and running a domain.
11//! It provides three components:
12//! - a [`runner`](Domain::runner) to launch the domain's event loop;
13//! - a [`client`](Domain::client) to perform actions (apply, submit, prove, ...); and
14//! - [`views`](Domain::views) to read state and data.
15//!
16//! Create a [`Domain`] with the [builder](Domain::builder) or [from a
17//! configuration file](Domain::from_config), and destructure it into its
18//! subcomponents upon creation:
19//!
20//! ```rust,no_run
21//! use delta_domain_sdk::{Domain, Config};
22//!
23//! async fn example() -> Result<(), Box<dyn std::error::Error>> {
24//! let config = Config::load()?;
25//! let Domain { runner, client, views } = Domain::from_config(config)
26//! .build()
27//! .await?;
28//! Ok(())
29//! }
30//! ```
31//!
32//! The runner must be launched before using the client or views — see
33//! [`Runner`](runner::Runner) for details.
34//!
35//! [API Documentation](crate::domain)
36//!
37//! ### Connectivity
38//!
39//! The RPC adapter handles communication with the base layer:
40//! - fetching vault data;
41//! - submitting transactions; and
42//! - streaming events from the base layer.
43//!
44//! Configure the RPC URL in the [configuration file](Config) via the `rpc_url`
45//! field, or programmatically with
46//! [`with_rpc`](builder::DomainBuilder::with_rpc).
47//!
48//! When no RPC URL is configured, a mock RPC is used. The mock RPC provides
49//! testing utilities like mock vaults and simulation of base layer events.
50//!
51//! ### Storage
52//!
53//! [`storage`] handles the persistence of vault and state diff data. The SDK
54//! supports different backends:
55//! - in-memory storage for testing and development; and
56//! - [RocksDB](https://rocksdb.org/) for production use
57//!
58//! You can choose which storage backend is used by setting or omitting the
59//! `rocksdb` crate feature.
60//!
61//! [API Documentation](crate::storage)
62//!
63//! ### Proving
64//!
65//! The proving client generates zero-knowledge proofs for state transitions.
66//! Find the available implementations
67//! [here](./proving/trait.Proving.html#implementors).
68//!
69//! [API Documentation](crate::proving)
70//!
71//! ## Key Concepts
72//!
73//! ### Vaults
74//!
75//! Vaults represent account state. There are two types:
76//!
77//! - [`BaseVault`](base_sdk::vaults::BaseVault): Used on the base shard and
78//! contains native token balance and account nonce.
79//!
80//! - [`Vault`](base_sdk::vaults::Vault): Used on domains and contains native
81//! token balance, shard ID, and optional data such as token holdings, token
82//! mint metadata, and NFT mint metadata.
83//!
84//! Vaults are uniquely identified by an
85//! ([`Address`](base_sdk::vaults::Address)).
86//!
87//! ### State Diff List (SDL)
88//!
89//! A [`StateDiffList`](base_sdk::sdl::StateDiffList) is a collection of state
90//! changes (diffs) resulting from transaction execution. SDLs:
91//! - represent atomic batches of state changes;
92//! - include information about the originating shard and proposer; and
93//! - require cryptographic proofs for validation.
94//!
95//! ### Verifiables
96//!
97//! [`VerifiableType`](crate::base::verifiable::VerifiableType) are
98//! cryptographically signed messages that can be verified and executed to
99//! produce state changes. such as:
100//! - [`DebitAllowance`](crate::base::verifiable::debit_allowance::DebitAllowance):
101//! permission to debit tokens from a source vault; or
102//! - [`fungible::Mint`](crate::base::verifiable::fungible::Mint): instruction
103//! to mint new fungible tokens.
104//!
105//! ## Features
106//!
107//! - `rocksdb`: enables persistent [RocksDB](https://rocksdb.org/) storage
108//! (*use for production*), if disabled an in-memory storage is used by
109//! default
110//! - `sp1`: enables [SP1]-based zero-knowledge proof generation
111//! - `metrics`: enables performance metrics collection
112//!
113//! [SP1]: https://docs.succinct.xyz/docs/sp1/introduction
114//!
115//! ## Example Usage
116//!
117//! ```rust,no_run
118//! use delta_domain_sdk::{Domain, Config};
119//!
120//! async fn run_domain() -> Result<(), Box<dyn std::error::Error>> {
121//! let config = Config::load()?;
122//! let Domain { runner, client, views } = Domain::from_config(config)
123//! .with_rpc("http://localhost:8545")
124//! .build().await.unwrap();
125//!
126//! runner.run_in_background().await.unwrap();
127//! Ok(())
128//! }
129//! ```
130//!
131//! ## Development
132//!
133//! Unlike a smart contract, a domain is a server application in the traditional
134//! sense. Developers should apply well-know application development practices
135//! like [the twelve-factor app](https://12factor.net/). Some tips:
136//!
137//! - Listen to any IPv4 or IPv6 address instead of `localhost`.
138
139pub mod builder;
140
141pub mod config;
142pub use config::Config;
143
144pub mod client;
145pub use client::DomainClient;
146pub mod domain;
147pub use domain::Domain;
148pub mod runner;
149pub mod views;
150
151pub mod storage;
152pub use storage::in_memory;
153#[cfg(feature = "rocksdb")]
154pub use storage::rocksdb;
155
156// Re-exported core components from domain_runtime
157pub use domain_runtime::{
158 execution,
159 sdls::types::{
160 SdlState,
161 SdlUpdate,
162 },
163};
164
165/// Logic to generate proofs
166pub mod proving {
167 pub use local_laws;
168 pub use proving_clients::*;
169}
170
171/// General purpose delta types and helpers re-exported from the base SDK
172pub mod base {
173 pub use base_sdk::*;
174
175 /// Types to verify global laws and generate state diffs
176 pub mod verifiable {
177 pub use verifiable::types::*;
178 }
179}
180
181pub(crate) mod base_layer;
182
183/// Utils to deal with Domain Agreements
184pub mod domain_agreement;
185
186#[cfg(feature = "admin-api")]
187mod admin_api;
188
189#[cfg(test)]
190mod tests;