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#![allow(incomplete_features)]
33#![cfg_attr(feature = "frozen-abi", feature(specialization))]
34
35// Allows macro expansion of `use ::solana_sdk::*` to work within this crate
36extern crate self as solana_sdk;
37
38#[cfg(feature = "full")]
39pub use signer::signers;
40#[cfg(not(target_os = "solana"))]
41pub use solana_program::program_stubs;
42// These solana_program imports could be *-imported, but that causes a bunch of
43// confusing duplication in the docs due to a rustdoc bug. #26211
44#[allow(deprecated)]
45pub use solana_program::sdk_ids;
46#[cfg(target_arch = "wasm32")]
47pub use solana_program::wasm_bindgen;
48pub use solana_program::{
49    account_info, address_lookup_table, big_mod_exp, blake3, bpf_loader, bpf_loader_deprecated,
50    bpf_loader_upgradeable, clock, config, custom_heap_default, custom_panic_default,
51    debug_account_data, declare_deprecated_sysvar_id, declare_sysvar_id, ed25519_program,
52    epoch_rewards, epoch_schedule, fee_calculator, impl_sysvar_get, incinerator, instruction,
53    keccak, lamports, loader_instruction, loader_upgradeable_instruction, loader_v4,
54    loader_v4_instruction, message, msg, native_token, nonce, program, program_error,
55    program_option, program_pack, rent, secp256k1_program, serialize_utils, slot_hashes,
56    slot_history, stable_layout, stake, stake_history, syscalls, system_instruction,
57    system_program, sysvar, unchecked_div_by_const, vote,
58};
59#[cfg(feature = "borsh")]
60pub use solana_program::{borsh, borsh0_10, borsh1};
61pub mod client;
62pub mod commitment_config;
63pub mod compute_budget;
64pub mod deserialize_utils;
65pub mod ed25519_instruction;
66pub mod entrypoint;
67pub mod entrypoint_deprecated;
68pub mod epoch_info;
69pub mod epoch_rewards_hasher;
70pub mod example_mocks;
71pub mod exit;
72pub mod feature;
73pub mod fee;
74pub mod genesis_config;
75pub mod hard_forks;
76pub mod hash;
77pub mod inner_instruction;
78pub mod log;
79pub mod native_loader;
80pub mod net;
81pub mod nonce_account;
82pub mod offchain_message;
83pub mod poh_config;
84pub mod precompiles;
85pub mod program_utils;
86pub mod pubkey;
87pub mod quic;
88pub mod rent_collector;
89pub mod rent_debits;
90pub mod reserved_account_keys;
91pub mod reward_info;
92pub mod reward_type;
93pub mod rpc_port;
94pub mod secp256k1_instruction;
95pub mod shred_version;
96pub mod signature;
97pub mod signer;
98pub mod simple_vote_transaction_checker;
99pub mod system_transaction;
100pub mod timing;
101pub mod transaction;
102pub mod transaction_context;
103pub mod transport;
104pub mod wasm;
105
106#[deprecated(since = "2.1.0", note = "Use `solana-account` crate instead")]
107pub use solana_account as account;
108#[deprecated(
109    since = "2.1.0",
110    note = "Use `solana_account::state_traits` crate instead"
111)]
112pub use solana_account::state_traits as account_utils;
113#[deprecated(since = "2.1.0", note = "Use `solana-bn254` crate instead")]
114pub use solana_bn254 as alt_bn128;
115#[deprecated(since = "2.1.0", note = "Use `solana-decode-error` crate instead")]
116pub use solana_decode_error as decode_error;
117#[deprecated(since = "2.1.0", note = "Use `solana-derivation-path` crate instead")]
118pub use solana_derivation_path as derivation_path;
119#[deprecated(since = "2.1.0", note = "Use `solana-feature-set` crate instead")]
120pub use solana_feature_set as feature_set;
121#[deprecated(since = "2.1.0", note = "Use `solana-inflation` crate instead")]
122pub use solana_inflation as inflation;
123#[deprecated(since = "2.1.0", note = "Use `solana-packet` crate instead")]
124pub use solana_packet as packet;
125#[deprecated(since = "2.1.0", note = "Use `solana-program-memory` crate instead")]
126pub use solana_program_memory as program_memory;
127#[deprecated(since = "2.1.0", note = "Use `solana_pubkey::pubkey` instead")]
128/// Convenience macro to define a static public key.
129///
130/// Input: a single literal base58 string representation of a Pubkey
131///
132/// # Example
133///
134/// ```
135/// use std::str::FromStr;
136/// use solana_program::{pubkey, pubkey::Pubkey};
137///
138/// static ID: Pubkey = pubkey!("My11111111111111111111111111111111111111111");
139///
140/// let my_id = Pubkey::from_str("My11111111111111111111111111111111111111111").unwrap();
141/// assert_eq!(ID, my_id);
142/// ```
143pub use solana_pubkey::pubkey;
144#[deprecated(since = "2.1.0", note = "Use `solana-sanitize` crate instead")]
145pub use solana_sanitize as sanitize;
146/// Same as `declare_id` except report that this id has been deprecated.
147pub use solana_sdk_macro::declare_deprecated_id;
148/// Convenience macro to declare a static public key and functions to interact with it.
149///
150/// Input: a single literal base58 string representation of a program's id
151///
152/// # Example
153///
154/// ```
155/// # // wrapper is used so that the macro invocation occurs in the item position
156/// # // rather than in the statement position which isn't allowed.
157/// use std::str::FromStr;
158/// use solana_sdk::{declare_id, pubkey::Pubkey};
159///
160/// # mod item_wrapper {
161/// #   use solana_sdk::declare_id;
162/// declare_id!("My11111111111111111111111111111111111111111");
163/// # }
164/// # use item_wrapper::id;
165///
166/// let my_id = Pubkey::from_str("My11111111111111111111111111111111111111111").unwrap();
167/// assert_eq!(id(), my_id);
168/// ```
169pub use solana_sdk_macro::declare_id;
170/// Convenience macro to define multiple static public keys.
171pub use solana_sdk_macro::pubkeys;
172#[deprecated(since = "2.1.0", note = "Use `solana-secp256k1-recover` crate instead")]
173pub use solana_secp256k1_recover as secp256k1_recover;
174#[deprecated(since = "2.1.0", note = "Use `solana-serde-varint` crate instead")]
175pub use solana_serde_varint as serde_varint;
176#[deprecated(since = "2.1.0", note = "Use `solana-short-vec` crate instead")]
177pub use solana_short_vec as short_vec;
178
179/// Convenience macro for `AddAssign` with saturating arithmetic.
180/// Replace by `std::num::Saturating` once stable
181#[macro_export]
182macro_rules! saturating_add_assign {
183    ($i:expr, $v:expr) => {{
184        $i = $i.saturating_add($v)
185    }};
186}
187
188#[macro_use]
189extern crate serde_derive;
190pub extern crate bs58;
191extern crate log as logger;
192
193#[cfg_attr(feature = "frozen-abi", macro_use)]
194#[cfg(feature = "frozen-abi")]
195extern crate solana_frozen_abi_macro;
196
197#[cfg(test)]
198mod tests {
199    #[test]
200    fn test_saturating_add_assign() {
201        let mut i = 0u64;
202        let v = 1;
203        saturating_add_assign!(i, v);
204        assert_eq!(i, 1);
205
206        i = u64::MAX;
207        saturating_add_assign!(i, v);
208        assert_eq!(i, u64::MAX);
209    }
210}