You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: near-sdk/src/lib.rs
+84-3Lines changed: 84 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -180,6 +180,87 @@ extern crate quickcheck;
180
180
///
181
181
/// * list of [**host functions**](collections#calls-to-host-functions-used-in-implementation) used for [`collections`] implementation
182
182
///
183
+
/// ### Implementation details and **host functions** calls used
184
+
///
185
+
/// If the details of [ABI](https://github.com/near/abi) generation layer are put aside, then the macro performs roughly the following:
186
+
///
187
+
/// #### inner workings of `#[near(contract_state)]` macro on struct/enum:
188
+
///
189
+
/// ```rust
190
+
/// # use near_sdk::near;
191
+
/// #[near(contract_state)]
192
+
/// pub struct Contract { /* .. */ }
193
+
/// ```
194
+
///
195
+
/// 1. Macro adds derived implementations of [`borsh::BorshSerialize`]/[`borsh::BorshSerialize`] for `Contract` type
196
+
/// 2. Macro defines a global `CONTRACT_SOURCE_METADATA` variable, which is a string of json serialization of [`near_contract_standards::contract_metadata::ContractSourceMetadata`](https://docs.rs/near-contract-standards/latest/near_contract_standards/contract_metadata/struct.ContractSourceMetadata.html).
/// ##### for a **view** method macro defines the following function:
220
+
/// ```rust,no_run
221
+
/// #[no_mangle]
222
+
/// pub extern "C" fn view_method() { /* .. */ }
223
+
/// ```
224
+
/// which
225
+
///
226
+
/// 1. calls [`env::setup_panic_hook`] host function
227
+
/// 2. calls [`env::state_read`] host function to load `Contract` into a `state` variable
228
+
/// 3. calls original `Contract::view_method(&state)` as defined in `#[near]` annotated block and saves
229
+
/// the returned value into a `result` variable
230
+
/// 4. calls [`serde_json::to_vec`] on obtained `result` and saves returned value to `serialized_result` variable
231
+
/// * `json` format can be changed to serializing with [`borsh::to_vec`] by using [`#[result_serializer(...)]`](`near#result_serializer-annotates-methods-of-a-type-in-its-impl-block`)
232
+
/// 5. if the `serialized_result` is an [`Result::Err`] error, then [`env::panic_str`] host function is called to signal result serialization error
233
+
/// 6. otherwise, if the `serialized_result` is a [`Result::Ok`], then [`env::value_return`] host function is called with unwrapped `serialized_result`
234
+
///
235
+
/// ##### for a **mutating** method macro defines the following function:
/// 1. calls [`env::setup_panic_hook`] host function
243
+
/// 2. calls [`env::input`] host function and saves it to `input` variable
244
+
/// 3. deserializes `Contract::mutating_method` arguments with [`serde_json::from_slice`] and saves it to `deserialized_input` variable
245
+
/// * `json` format can be changed to deserializing with [`borsh::from_slice`] by using [`#[serializer(...)]`](`near#serializer-annotates-function-arguments`)
246
+
/// 4. if the `deserialized_input` is an [`Result::Err`] error, then [`env::panic_str`] host function is called to signal input deserialization error
247
+
/// 5. otherwise, if the `deserialized_input` is a [`Result::Ok`], `deserialized_input` is unwrapped and saved to `deserialized_input_success` variable
248
+
/// 6. calls [`env::state_read`] host function to load `Contract` into a `state` variable
249
+
/// 7. calls original `Contract::mutating_method(&mut state, deserialized_input_success.argument)` as defined in `#[near]` annotated block
250
+
/// 8. calls [`env::state_write`] with `&state` as argument.
251
+
///
252
+
/// ##### using [cargo-expand](https://crates.io/crates/cargo-expand) to view actual macro results
253
+
///
254
+
/// The above is an approximate description of what macro performs.
255
+
///
256
+
/// Running the following in a contract's crate is a way to introspect more details of its operation:
0 commit comments