solana_sdk/
lib.rs

1//! The Solana host and client SDK.
2//!
3//! This is the base library for all off-chain programs that interact with
4//! Solana or otherwise operate on Solana data structures. On-chain programs
5//! instead use the [`solana-program`] crate, the modules of which are
6//! re-exported by this crate, like the relationship between the Rust
7//! `core` and `std` crates. As much of the functionality of this crate is
8//! provided by `solana-program`, see that crate's documentation for an
9//! overview.
10//!
11//! [`solana-program`]: https://docs.rs/solana-program
12//!
13//! Many of the modules in this crate are primarily of use to the Solana runtime
14//! itself. Additional crates provide capabilities built on `solana-sdk`, and
15//! many programs will need to link to those crates as well, particularly for
16//! clients communicating with Solana nodes over RPC.
17//!
18//! Such crates include:
19//!
20//! - [`solana-client`] - For interacting with a Solana node via the [JSON-RPC API][json].
21//! - [`solana-cli-config`] - Loading and saving the Solana CLI configuration file.
22//! - [`solana-clap-utils`] - Routines for setting up the CLI using [`clap`], as
23//!   used by the Solana CLI. Includes functions for loading all types of
24//!   signers supported by the CLI.
25//!
26//! [`solana-client`]: https://docs.rs/solana-client
27//! [`solana-cli-config`]: https://docs.rs/solana-cli-config
28//! [`solana-clap-utils`]: https://docs.rs/solana-clap-utils
29//! [json]: https://solana.com/docs/rpc
30//! [`clap`]: https://docs.rs/clap
31
32#![cfg_attr(docsrs, feature(doc_auto_cfg))]
33
34// Allows macro expansion of `use ::solana_sdk::*` to work within this crate
35extern crate self as solana_sdk;
36
37#[cfg(feature = "full")]
38pub use solana_commitment_config as commitment_config;
39#[cfg(not(target_os = "solana"))]
40pub use solana_program::program_stubs;
41// These solana_program imports could be *-imported, but that causes a bunch of
42// confusing duplication in the docs due to a rustdoc bug. #26211
43#[allow(deprecated)]
44pub use solana_program::sdk_ids;
45#[cfg(target_arch = "wasm32")]
46pub use solana_program::wasm_bindgen;
47pub use solana_program::{
48    account_info, address_lookup_table, big_mod_exp, blake3, bpf_loader, bpf_loader_deprecated,
49    bpf_loader_upgradeable, clock, config, custom_heap_default, custom_panic_default,
50    debug_account_data, declare_deprecated_sysvar_id, declare_sysvar_id, ed25519_program,
51    epoch_rewards, epoch_schedule, fee_calculator, impl_sysvar_get, incinerator, instruction,
52    keccak, lamports, loader_instruction, loader_upgradeable_instruction, loader_v4,
53    loader_v4_instruction, message, msg, native_token, nonce, program, program_error,
54    program_option, program_pack, rent, secp256k1_program, serialize_utils, slot_hashes,
55    slot_history, stable_layout, stake, stake_history, syscalls, system_instruction,
56    system_program, sysvar, unchecked_div_by_const, vote,
57};
58#[cfg(feature = "borsh")]
59pub use solana_program::{borsh, borsh0_10, borsh1};
60#[cfg(feature = "full")]
61#[deprecated(since = "2.2.0", note = "Use `solana-signer` crate instead")]
62pub use solana_signer::signers;
63pub mod entrypoint;
64pub mod entrypoint_deprecated;
65pub mod example_mocks;
66pub mod feature;
67#[cfg(feature = "full")]
68#[deprecated(since = "2.2.0", note = "Use `solana-genesis-config` crate instead")]
69pub use solana_genesis_config as genesis_config;
70#[cfg(feature = "full")]
71#[deprecated(since = "2.2.0", note = "Use `solana-hard-forks` crate instead")]
72pub use solana_hard_forks as hard_forks;
73pub mod hash;
74pub mod log;
75pub mod native_loader;
76pub mod net;
77#[deprecated(since = "2.2.2", note = "Use `agave-precompiles` crate instead")]
78pub mod precompiles;
79pub mod program_utils;
80pub mod pubkey;
81#[cfg(feature = "full")]
82#[deprecated(since = "2.2.0", note = "Use `solana_rent_collector` crate instead")]
83pub use solana_rent_collector as rent_collector;
84#[deprecated(since = "2.2.0", note = "Use `solana-reward-info` crate instead")]
85pub mod reward_info {
86    pub use solana_reward_info::RewardInfo;
87}
88#[deprecated(since = "2.2.0", note = "Use `solana-reward-info` crate instead")]
89pub mod reward_type {
90    pub use solana_reward_info::RewardType;
91}
92pub mod rpc_port;
93#[cfg(feature = "full")]
94#[deprecated(since = "2.2.0", note = "Use `solana-shred-version` crate instead")]
95pub use solana_shred_version as shred_version;
96pub mod signature;
97pub mod signer;
98pub mod transaction;
99pub mod transport;
100pub mod wasm;
101
102#[deprecated(since = "2.1.0", note = "Use `solana-account` crate instead")]
103pub use solana_account as account;
104#[deprecated(
105    since = "2.1.0",
106    note = "Use `solana_account::state_traits` crate instead"
107)]
108pub use solana_account::state_traits as account_utils;
109#[deprecated(since = "2.1.0", note = "Use `solana-bn254` crate instead")]
110pub use solana_bn254 as alt_bn128;
111#[cfg(feature = "full")]
112#[deprecated(since = "2.2.0", note = "Use `solana-client-traits` crate instead")]
113pub use solana_client_traits as client;
114#[deprecated(
115    since = "2.2.0",
116    note = "Use `solana-compute-budget-interface` crate instead"
117)]
118#[cfg(feature = "full")]
119pub use solana_compute_budget_interface as compute_budget;
120#[deprecated(since = "2.1.0", note = "Use `solana-decode-error` crate instead")]
121pub use solana_decode_error as decode_error;
122#[deprecated(since = "2.1.0", note = "Use `solana-derivation-path` crate instead")]
123pub use solana_derivation_path as derivation_path;
124#[cfg(feature = "full")]
125#[deprecated(since = "2.2.0", note = "Use `solana-ed25519-program` crate instead")]
126pub use solana_ed25519_program as ed25519_instruction;
127#[deprecated(since = "2.2.0", note = "Use `solana-epoch-info` crate instead")]
128pub use solana_epoch_info as epoch_info;
129#[deprecated(
130    since = "2.2.0",
131    note = "Use `solana-epoch-rewards-hasher` crate instead"
132)]
133pub use solana_epoch_rewards_hasher as epoch_rewards_hasher;
134#[deprecated(since = "2.2.2", note = "Use `agave-feature-set` crate instead")]
135pub mod feature_set {
136    pub use solana_feature_set::*;
137}
138#[deprecated(since = "2.2.0", note = "Use `solana-fee-structure` crate instead")]
139pub use solana_fee_structure as fee;
140#[deprecated(since = "2.1.0", note = "Use `solana-inflation` crate instead")]
141pub use solana_inflation as inflation;
142#[deprecated(
143    since = "2.2.0",
144    note = "Use `solana_message::inner_instruction` instead"
145)]
146pub use solana_message::inner_instruction;
147#[deprecated(since = "2.2.0", note = "Use `solana-nonce-account` crate instead")]
148pub use solana_nonce_account as nonce_account;
149#[cfg(feature = "full")]
150#[deprecated(since = "2.2.0", note = "Use `solana-offchain-message` crate instead")]
151pub use solana_offchain_message as offchain_message;
152#[deprecated(since = "2.1.0", note = "Use `solana-packet` crate instead")]
153pub use solana_packet as packet;
154#[deprecated(since = "2.2.0", note = "Use `solana-poh-config` crate instead")]
155pub use solana_poh_config as poh_config;
156#[deprecated(since = "2.1.0", note = "Use `solana-program-memory` crate instead")]
157pub use solana_program_memory as program_memory;
158#[deprecated(since = "2.1.0", note = "Use `solana_pubkey::pubkey` instead")]
159/// Convenience macro to define a static public key.
160///
161/// Input: a single literal base58 string representation of a Pubkey
162///
163/// # Example
164///
165/// ```
166/// use std::str::FromStr;
167/// use solana_program::{pubkey, pubkey::Pubkey};
168///
169/// static ID: Pubkey = pubkey!("My11111111111111111111111111111111111111111");
170///
171/// let my_id = Pubkey::from_str("My11111111111111111111111111111111111111111").unwrap();
172/// assert_eq!(ID, my_id);
173/// ```
174pub use solana_pubkey::pubkey;
175#[cfg(feature = "full")]
176#[deprecated(since = "2.2.0", note = "Use `solana-quic-definitions` crate instead")]
177pub use solana_quic_definitions as quic;
178#[deprecated(since = "2.2.0", note = "Use `solana-rent-debits` crate instead")]
179pub use solana_rent_debits as rent_debits;
180#[cfg(feature = "full")]
181#[deprecated(
182    since = "2.2.2",
183    note = "Use `agave-reserved-account-keys` crate instead"
184)]
185pub mod reserved_account_keys {
186    pub use solana_reserved_account_keys::*;
187}
188#[deprecated(since = "2.1.0", note = "Use `solana-sanitize` crate instead")]
189pub use solana_sanitize as sanitize;
190/// Same as `declare_id` except report that this id has been deprecated.
191pub use solana_sdk_macro::declare_deprecated_id;
192/// Convenience macro to declare a static public key and functions to interact with it.
193///
194/// Input: a single literal base58 string representation of a program's id
195///
196/// # Example
197///
198/// ```
199/// # // wrapper is used so that the macro invocation occurs in the item position
200/// # // rather than in the statement position which isn't allowed.
201/// use std::str::FromStr;
202/// use solana_sdk::{declare_id, pubkey::Pubkey};
203///
204/// # mod item_wrapper {
205/// #   use solana_sdk::declare_id;
206/// declare_id!("My11111111111111111111111111111111111111111");
207/// # }
208/// # use item_wrapper::id;
209///
210/// let my_id = Pubkey::from_str("My11111111111111111111111111111111111111111").unwrap();
211/// assert_eq!(id(), my_id);
212/// ```
213pub use solana_sdk_macro::declare_id;
214/// Convenience macro to define multiple static public keys.
215pub use solana_sdk_macro::pubkeys;
216#[deprecated(since = "2.2.0", note = "Use `solana-secp256k1-program` crate instead")]
217#[cfg(feature = "full")]
218pub use solana_secp256k1_program as secp256k1_instruction;
219#[deprecated(since = "2.1.0", note = "Use `solana-secp256k1-recover` crate instead")]
220pub use solana_secp256k1_recover as secp256k1_recover;
221#[deprecated(since = "2.2.0", note = "Use `solana-serde` crate instead")]
222pub use solana_serde as deserialize_utils;
223#[deprecated(since = "2.1.0", note = "Use `solana-serde-varint` crate instead")]
224pub use solana_serde_varint as serde_varint;
225#[deprecated(since = "2.1.0", note = "Use `solana-short-vec` crate instead")]
226pub use solana_short_vec as short_vec;
227#[cfg(feature = "full")]
228#[deprecated(
229    since = "2.2.0",
230    note = "Use `solana-system-transaction` crate instead"
231)]
232pub use solana_system_transaction as system_transaction;
233#[deprecated(since = "2.2.0", note = "Use `solana-time-utils` crate instead")]
234pub use solana_time_utils as timing;
235#[cfg(feature = "full")]
236#[deprecated(
237    since = "2.2.0",
238    note = "Use `solana_transaction::simple_vote_transaction_checker` instead"
239)]
240pub use solana_transaction::simple_vote_transaction_checker;
241#[deprecated(
242    since = "2.2.0",
243    note = "Use `solana-transaction-context` crate instead"
244)]
245pub mod transaction_context {
246    pub use solana_transaction_context::*;
247}
248#[deprecated(since = "2.2.0", note = "Use `solana-validator-exit` crate instead")]
249pub use solana_validator_exit as exit;
250
251/// Convenience macro for `AddAssign` with saturating arithmetic.
252/// Replace by `std::num::Saturating` once stable
253#[macro_export]
254macro_rules! saturating_add_assign {
255    ($i:expr, $v:expr) => {{
256        $i = $i.saturating_add($v)
257    }};
258}
259
260pub extern crate bs58;
261
262#[cfg(test)]
263mod tests {
264    #[test]
265    fn test_saturating_add_assign() {
266        let mut i = 0u64;
267        let v = 1;
268        saturating_add_assign!(i, v);
269        assert_eq!(i, 1);
270
271        i = u64::MAX;
272        saturating_add_assign!(i, v);
273        assert_eq!(i, u64::MAX);
274    }
275}