af_sui_types/
lib.rs

1#![cfg_attr(all(doc, not(doctest)), feature(doc_auto_cfg))]
2// Copyright (c) Mysten Labs, Inc.
3// SPDX-License-Identifier: Apache-2.0
4//
5// Includes a lot of the constants in the original `sui-types`
6// https://github.com/MystenLabs/sui/tree/main/crates/sui-types
7
8//! Aftermath's extensions to [`sui_sdk_types`].
9//!
10//! Includes some types and constants from the original [`sui_types`] and [`move_core_types`] that
11//! are not present in `sui_sdk_types`. This crate also re-exports a lot of the types in
12//! `sui_sdk_types`.
13//!
14//! This crate was originally conceived with the following objectives:
15//! - [`serde`] compatibility with the full Sui checkpoint data
16//! - avoiding dynamic error types so that callers could match against errors and react accordingly
17//! - using a minimal set of dependencies
18//! - [SemVer](https://doc.rust-lang.org/cargo/reference/semver.html) compatibility
19//!
20//! <div class="warning">
21//!
22//! The long-term plan is to deprecate most of this in favor of [`sui_sdk_types`]. However, there
23//! are some types in that crate that don't expose all of the attributes/methods we need yet.
24//!
25//! </div>
26//!
27//! [`serde`]: https://docs.rs/serde/latest/serde/index.html
28//! [`sui_types`]: https://mystenlabs.github.io/sui/sui_types/index.html
29//! [`move_core_types`]: https://github.com/MystenLabs/sui/tree/main/external-crates/move/crates/move-core-types
30//! [`sui_sdk_types`]: https://docs.rs/sui-sdk-types/latest/sui_sdk_types/
31
32#[doc(no_inline)]
33pub use sui_sdk_types::{
34    ActiveJwk,
35    Address,
36    Argument,
37    CheckpointCommitment,
38    CheckpointContents,
39    CheckpointContentsDigest,
40    CheckpointDigest,
41    CheckpointSequenceNumber,
42    CheckpointSummary,
43    CheckpointTimestamp,
44    Command,
45    ConsensusCommitDigest,
46    Digest,
47    EffectsAuxiliaryDataDigest,
48    EndOfEpochData,
49    EpochId,
50    Event,
51    ExecutionError,
52    ExecutionStatus,
53    GasCostSummary,
54    IdOperation,
55    Identifier,
56    Jwk,
57    JwkId,
58    MoveCall,
59    MovePackage,
60    ObjectDigest,
61    ObjectId,
62    ObjectIn,
63    ObjectOut,
64    ProgrammableTransaction,
65    ProtocolVersion,
66    SignedTransaction,
67    StructTag,
68    Transaction,
69    TransactionDigest,
70    TransactionEffects,
71    TransactionEffectsDigest,
72    TransactionEffectsV1,
73    TransactionEffectsV2,
74    TransactionEvents,
75    TransactionEventsDigest,
76    TransactionExpiration,
77    TransactionKind,
78    TypeOrigin,
79    TypeParseError,
80    TypeTag,
81    UnchangedSharedKind,
82    UpgradeInfo,
83    UserSignature,
84    Version,
85};
86
87mod const_address;
88pub mod encoding;
89#[cfg(feature = "hash")]
90mod hash;
91/// Aftermath's versions of [`move_core_types`](https://github.com/MystenLabs/sui/tree/main/external-crates/move/crates/move-core-types).
92pub(crate) mod move_core;
93/// Aftermath's versions of [`sui_types`](https://mystenlabs.github.io/sui/sui_types/index.html).
94pub mod sui;
95
96#[doc(inline)]
97pub use self::move_core::identifier::{IdentStr, InvalidIdentifierError};
98#[cfg(feature = "u256")]
99#[doc(inline)]
100pub use self::move_core::u256::{self, U256};
101#[doc(inline)]
102pub use self::sui::chain_identifier::ChainIdentifier;
103#[doc(inline)]
104pub use self::sui::effects::TransactionEffectsAPI;
105#[doc(inline)]
106pub use self::sui::full_checkpoint_content::{CheckpointData, CheckpointTransaction};
107#[doc(inline)]
108pub use self::sui::move_object_type::MoveObjectType;
109#[doc(inline)]
110pub use self::sui::object::{MoveObject, Object, Owner};
111#[doc(inline)]
112pub use self::sui::transaction::{
113    GasData,
114    ObjectArg,
115    TransactionData,
116    TransactionDataAPI,
117    TransactionDataV1,
118    TransactionFromBase64Error,
119};
120
121// =============================================================================
122//  Aliases
123// =============================================================================
124
125/// Reference to a particular object.
126///
127/// Used for immutable or owned object transaction inputs (fast-path).
128///
129/// Can be created from [`ObjectReference::into_parts`].
130///
131/// [`ObjectReference::into_parts`]: sui_sdk_types::ObjectReference::into_parts
132pub type ObjectRef = (ObjectId, Version, ObjectDigest);
133
134// =============================================================================
135//  Constants
136// =============================================================================
137
138/// Object ID of the onchain `Clock`.
139pub const CLOCK_ID: ObjectId = ObjectId::new(hex_address_bytes(b"0x6"));
140
141const OBJECT_DIGEST_DELETED_BYTE_VAL: u8 = 99;
142const OBJECT_DIGEST_WRAPPED_BYTE_VAL: u8 = 88;
143const OBJECT_DIGEST_CANCELLED_BYTE_VAL: u8 = 77;
144
145/// A marker that signifies the object is deleted.
146pub const OBJECT_DIGEST_DELETED: ObjectDigest =
147    ObjectDigest::new([OBJECT_DIGEST_DELETED_BYTE_VAL; 32]);
148
149/// A marker that signifies the object is wrapped into another object.
150pub const OBJECT_DIGEST_WRAPPED: ObjectDigest =
151    ObjectDigest::new([OBJECT_DIGEST_WRAPPED_BYTE_VAL; 32]);
152
153pub const OBJECT_DIGEST_CANCELLED: ObjectDigest =
154    ObjectDigest::new([OBJECT_DIGEST_CANCELLED_BYTE_VAL; 32]);
155
156pub const COIN_MODULE_NAME: &IdentStr = IdentStr::cast("coin");
157pub const COIN_STRUCT_NAME: &IdentStr = IdentStr::cast("Coin");
158pub const COIN_METADATA_STRUCT_NAME: &IdentStr = IdentStr::cast("CoinMetadata");
159pub const COIN_TREASURE_CAP_NAME: &IdentStr = IdentStr::cast("TreasuryCap");
160
161pub const PAY_MODULE_NAME: &IdentStr = IdentStr::cast("pay");
162pub const PAY_JOIN_FUNC_NAME: &IdentStr = IdentStr::cast("join");
163pub const PAY_SPLIT_N_FUNC_NAME: &IdentStr = IdentStr::cast("divide_and_keep");
164pub const PAY_SPLIT_VEC_FUNC_NAME: &IdentStr = IdentStr::cast("split_vec");
165
166pub const DYNAMIC_FIELD_MODULE_NAME: &IdentStr = IdentStr::cast("dynamic_field");
167pub const DYNAMIC_FIELD_FIELD_STRUCT_NAME: &IdentStr = IdentStr::cast("Field");
168
169pub const DYNAMIC_OBJECT_FIELD_MODULE_NAME: &IdentStr = IdentStr::cast("dynamic_object_field");
170pub const DYNAMIC_OBJECT_FIELD_WRAPPER_STRUCT_NAME: &IdentStr = IdentStr::cast("Wrapper");
171
172pub const MIST_PER_SUI: u64 = 1_000_000_000;
173
174/// Total supply denominated in Sui
175pub const TOTAL_SUPPLY_SUI: u64 = 10_000_000_000;
176
177// Note: cannot use checked arithmetic here since `const unwrap` is still unstable.
178/// Total supply denominated in Mist
179pub const TOTAL_SUPPLY_MIST: u64 = TOTAL_SUPPLY_SUI * MIST_PER_SUI;
180
181pub const GAS_MODULE_NAME: &IdentStr = IdentStr::cast("sui");
182pub const GAS_STRUCT_NAME: &IdentStr = IdentStr::cast("SUI");
183
184/// Maximum number of active validators at any moment.
185/// We do not allow the number of validators in any epoch to go above this.
186pub const MAX_VALIDATOR_COUNT: u64 = 150;
187
188/// Lower-bound on the amount of stake required to become a validator.
189///
190/// 30 million SUI
191pub const MIN_VALIDATOR_JOINING_STAKE_MIST: u64 = 30_000_000 * MIST_PER_SUI;
192
193/// Threshold below which validators enter a grace period to be removed.
194///
195/// Validators with stake amount below `validator_low_stake_threshold` are considered to
196/// have low stake and will be escorted out of the validator set after being below this
197/// threshold for more than `validator_low_stake_grace_period` number of epochs.
198///
199/// 20 million SUI
200pub const VALIDATOR_LOW_STAKE_THRESHOLD_MIST: u64 = 20_000_000 * MIST_PER_SUI;
201
202/// Validators with stake below `validator_very_low_stake_threshold` will be removed
203/// immediately at epoch change, no grace period.
204///
205/// 15 million SUI
206pub const VALIDATOR_VERY_LOW_STAKE_THRESHOLD_MIST: u64 = 15_000_000 * MIST_PER_SUI;
207
208/// A validator can have stake below `validator_low_stake_threshold`
209/// for this many epochs before being kicked out.
210pub const VALIDATOR_LOW_STAKE_GRACE_PERIOD: u64 = 7;
211
212pub const STAKING_POOL_MODULE_NAME: &IdentStr = IdentStr::cast("staking_pool");
213pub const STAKED_SUI_STRUCT_NAME: &IdentStr = IdentStr::cast("StakedSui");
214
215pub const ADD_STAKE_MUL_COIN_FUN_NAME: &IdentStr = IdentStr::cast("request_add_stake_mul_coin");
216pub const ADD_STAKE_FUN_NAME: &IdentStr = IdentStr::cast("request_add_stake");
217pub const WITHDRAW_STAKE_FUN_NAME: &IdentStr = IdentStr::cast("request_withdraw_stake");
218
219macro_rules! built_in_ids {
220    ($($addr:ident / $id:ident = $init:expr);* $(;)?) => {
221        $(
222            pub const $addr: Address = builtin_address($init);
223            pub const $id: ObjectId = ObjectId::new($addr.into_inner());
224        )*
225    }
226}
227
228macro_rules! built_in_pkgs {
229    ($($addr:ident / $id:ident = $init:expr);* $(;)?) => {
230        built_in_ids! { $($addr / $id = $init;)* }
231        pub const SYSTEM_PACKAGE_ADDRESSES: &[Address] = &[$($addr),*];
232        pub fn is_system_package(addr: impl Into<Address>) -> bool {
233            matches!(addr.into(), $($addr)|*)
234        }
235    }
236}
237
238built_in_pkgs! {
239    MOVE_STDLIB_ADDRESS / MOVE_STDLIB_PACKAGE_ID = 0x1;
240    SUI_FRAMEWORK_ADDRESS / SUI_FRAMEWORK_PACKAGE_ID = 0x2;
241    SUI_SYSTEM_ADDRESS / SUI_SYSTEM_PACKAGE_ID = 0x3;
242    BRIDGE_ADDRESS / BRIDGE_PACKAGE_ID = 0xb;
243    DEEPBOOK_ADDRESS / DEEPBOOK_PACKAGE_ID = 0xdee9;
244}
245
246const fn builtin_address(suffix: u16) -> Address {
247    let mut addr = [0u8; Address::LENGTH];
248    let [hi, lo] = suffix.to_be_bytes();
249    addr[Address::LENGTH - 2] = hi;
250    addr[Address::LENGTH - 1] = lo;
251    Address::new(addr)
252}
253
254// =============================================================================
255//  Functions
256// =============================================================================
257
258#[doc(inline)]
259pub use self::const_address::hex_address_bytes;
260#[doc(inline)]
261pub use self::encoding::{decode_base64_default, encode_base64_default};
262
263/// `const`-ructor for Sui addresses.
264pub const fn address(bytes: &[u8]) -> Address {
265    Address::new(hex_address_bytes(bytes))
266}
267
268/// `const`-ructor for object IDs.
269pub const fn object_id(bytes: &[u8]) -> ObjectId {
270    ObjectId::new(hex_address_bytes(bytes))
271}