Skip to main content

af_sui_types/
lib.rs

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