1
+ // ! Functionalities for interacting with the Starknet network.
2
+ // !
3
+ // ! # Core Components
4
+ // !
5
+ // ! - **Storage**: The `storage` module defines abstractions on how to interact with Starknet
6
+ // ! contract storage.
7
+ // ! - **Syscalls**: The `syscalls` module contains the extern declarations for all the system calls
8
+ // ! available in Starknet.
9
+ // ! - **Contract Addresses**: The `contract_address` and `eth_address` modules provide types and
10
+ // ! utilities for working with Starknet contract addresses and Ethereum addresses.
11
+ // ! - **Cryptography**: The `secp256k1`, `secp256r1`, `secp256_trait`, and `eth_signature` modules
12
+ // ! handle various elliptic curve operations.
13
+ // ! - **Execution Info**: The `info` module exposes functions for accessing information about the
14
+ // ! current contract execution, such as the caller address, contract address, block info, and
15
+ // ! transaction info.
16
+
1
17
#[allow(unused_imports)]
2
18
use core :: box :: Box ;
3
19
#[allow(unused_imports)]
@@ -9,9 +25,7 @@ use core::traits::{Into, TryInto};
9
25
#[allow(unused_imports)]
10
26
use core :: zeroable :: Zeroable ;
11
27
12
- /// Store trait and implementations for various types.
13
28
pub mod storage_access ;
14
- /// Re-imports
15
29
pub use storage_access :: {Store , StorageAddress };
16
30
#[allow(unused_imports)]
17
31
use storage_access :: {
@@ -20,7 +34,6 @@ use storage_access::{
20
34
storage_address_try_from_felt252 ,
21
35
};
22
36
23
- /// Module containing all the extern declaration of the syscalls.
24
37
pub mod syscalls ;
25
38
#[allow(unused_imports)]
26
39
use syscalls :: {
@@ -30,12 +43,10 @@ use syscalls::{
30
43
get_class_hash_at_syscall,
31
44
};
32
45
33
- /// secp256
34
46
pub mod secp256_trait ;
35
47
pub mod secp256k1 ;
36
48
pub mod secp256r1 ;
37
49
38
- /// ContractAddress
39
50
pub mod contract_address ;
40
51
pub use contract_address :: {ContractAddress , contract_address_const};
41
52
#[allow(unused_imports)]
@@ -44,20 +55,17 @@ use contract_address::{
44
55
contract_address_try_from_felt252 ,
45
56
};
46
57
47
- /// EthAddress
48
58
pub mod eth_address ;
49
59
pub use eth_address :: EthAddress ;
50
60
#[allow(unused_imports)]
51
61
use eth_address :: {
52
62
EthAddressIntoFelt252 , EthAddressSerde , EthAddressZeroable , Felt252TryIntoEthAddress ,
53
63
};
54
64
55
- /// EthSignature
56
65
pub mod eth_signature ;
57
66
#[allow(unused_imports)]
58
67
use eth_signature :: verify_eth_signature;
59
68
60
- /// ClassHash
61
69
pub mod class_hash ;
62
70
pub use class_hash :: ClassHash ;
63
71
#[allow(unused_imports)]
@@ -66,7 +74,7 @@ use class_hash::{
66
74
class_hash_try_from_felt252 ,
67
75
};
68
76
69
- /// Not `pub` on purpose, only used for direct reexport by the next line.
77
+ // Not `pub` on purpose, only used for direct reexport by the next line.
70
78
mod info ;
71
79
pub use info :: {
72
80
v2 :: ExecutionInfo as ExecutionInfo , BlockInfo , v2 :: TxInfo as TxInfo , get_execution_info,
@@ -80,91 +88,37 @@ pub use event::Event;
80
88
pub mod account ;
81
89
pub use account :: AccountContract ;
82
90
83
- /// This module contains the storage-related types and traits for Cairo contracts. It provides
84
- /// abstractions for reading and writing to Starknet storage.
85
- ///
86
- /// The front facing interface for the user is simple and intuitive, for example consider the
87
- /// following storage struct:
88
- /// ```
89
- /// #[storage]
90
- /// struct Storage {
91
- /// a: felt252,
92
- /// b: Map<felt252, felt52>,
93
- /// c: Map<felt52, Map<felt52, felt52>>,
94
- /// }
95
- /// ```
96
- /// The user can access the storage members `a` and `b` using the following code:
97
- /// ```
98
- /// fn use_storage(self: @ContractState) {
99
- /// let a_value = self.a.read();
100
- /// // For a Map, the user can use the `entry` method to access the value at a specific key:
101
- /// let b_value = self.b.entry(42).read();
102
- /// // Or simply pass the key to the `read` method:
103
- /// let b_value = self.b.read(42);
104
- /// // Accessing a nested Map must be done using the `entry` method, either:
105
- /// let c_value = self.c.entry(42).entry(43).read()
106
- /// // Or:
107
- /// let c_value = self.c.entry(42).read(43);
108
- /// }
109
- /// ```
110
- ///
111
- /// Under the hood, the storage access is more complex. The life cycle of a storage object is as
112
- /// follows:
113
- /// 1. The storage struct of a contract is represented by a `FlattenedStorage` struct, which
114
- /// can be derefed into a struct containing a member for each storage member of the contract.
115
- /// This member can be either a `StorageBase` or a `FlattenedStorage` instance. Members are
116
- /// represented as a `FlattenedStorage` if the storage member is attributed with either
117
- /// `#[substorage(v0)]` (for backward compatibility) or `#[flat]`. `FlattenedStorage` is used to
118
- /// structure the storage access; however, it does not affect the address of the storage object.
119
- /// 2. `StorageBase` members of a `FlattenedStorage` struct hold a single `felt252` value, which is
120
- /// the Keccak hash of the name of the member. For simple types, this value will be the address
121
- /// of the member in the storage.
122
- /// 3. `StorageBase` members are then converted to `StoragePath` instances, which are essentially
123
- /// a wrapper around a `HashState` instance, used to account for more values when computing the
124
- /// address of the storage object. `StoragePath` instances can be updated with values coming from
125
- /// two sources:
126
- /// - Storage nodes, which are structs that represent another struct with all its members
127
- /// in the storage, similar to `FlattenedStorage`. However, unlike `FlattenedStorage`, the
128
- /// path to the storage node does affect the address of the storage object. See `StorageNode`
129
- /// for more details.
130
- /// - Storage collections, specifically `Map` and `Vec`, simulate the behavior of collections by
131
- /// updating the hash state with the key or index of the collection member.
132
- /// 4. After finishing the updates, the `StoragePath` instance is finalized, resulting in a
133
- /// `StoragePointer0Offset` instance, which is a pointer to the address of the storage object. If
134
- /// the pointer is to an object of size greater than 1, the object is stored in a sequential
135
- /// manner starting from the address of the pointer. The whole object can be read or written
136
- /// using `read` and `write` methods, and specific members can also be accessed in the case of a
137
- /// struct. See `SubPointers` for more details.
138
- ///
139
- /// The transitioning between the different types of storage objects is also called from the
140
- /// `Deref` trait, and thus, allowing an access to the members of the storage object in a simple
141
- /// way.
142
- ///
143
- /// The types mentioned above are generic in the stored object type. This is done to provide
144
- /// specific behavior for each type of stored object, e.g., a `StoragePath` of `Map` type will have
145
- /// an `entry` method, but it won't have a `read` or `write` method, as `Map` is not storable by
146
- /// itself, only its values are.
147
- /// The generic type of the storage object can also be wrapped with a `Mutable` type, which
148
- /// indicates that the storage object is mutable, i.e., it was created from a `ref` contract state,
149
- /// and thus the object can be written to.
150
91
pub mod storage ;
151
92
152
93
pub extern type System ;
153
94
154
- /// An Helper function to force the inclusion of `System` in the list of implicits.
95
+ // A helper function to force the inclusion of `System` in the list of implicits.
155
96
#[deprecated(
156
97
feature: " use_system_implicit" ,
157
98
note: " Use `core::internal::require_implicit::<System>` instead." ,
158
99
)]
159
100
fn use_system_implicit () implicits (System ) {}
160
101
161
- /// The result type for a syscall.
102
+ /// The `Result` type for a syscall.
162
103
pub type SyscallResult <T > = Result <T , Array <felt252 >>;
163
104
105
+ /// Trait for handling syscalls results.
164
106
pub trait SyscallResultTrait <T > {
165
- /// If `val` is `Result::Ok(x)`, returns `x`. Otherwise, panics with the revert reason.
107
+ /// Unwraps a syscall result, yielding the content of an `Ok`.
108
+ ///
109
+ /// # Panics
110
+ ///
111
+ /// Panics with the syscall error message if the value is an `Err`.
112
+ ///
113
+ /// # Examples
114
+ ///
115
+ /// ```
116
+ /// let result = starknet::syscalls::get_execution_info_v2_syscall();
117
+ /// let info = result.unwrap_syscall();
118
+ /// ```
166
119
fn unwrap_syscall (self : SyscallResult <T >) -> T ;
167
120
}
121
+
168
122
impl SyscallResultTraitImpl <T > of SyscallResultTrait <T > {
169
123
fn unwrap_syscall (self : SyscallResult <T >) -> T {
170
124
match self {
@@ -174,10 +128,11 @@ impl SyscallResultTraitImpl<T> of SyscallResultTrait<T> {
174
128
}
175
129
}
176
130
177
- /// The expected return value of the `__validate*__` functions of an accounted contract.
131
+ /// The expected return value of the `__validate__` function in account contracts.
132
+ ///
133
+ /// This constant is used to indicate that a transaction validation was successful.
134
+ /// Account contracts must return this value from their `__validate__` function to
135
+ /// signal that the transaction should proceed.
178
136
pub const VALIDATED : felt252 = ' VALID' ;
179
137
180
- /// Module for starknet testing only.
181
- /// Provides functions useful for testing event emission, starknet state information, and the
182
- /// cheatcode concept in general.
183
138
pub mod testing ;
0 commit comments