Skip to main content

neo_devpack/
lib.rs

1// Copyright (c) 2025-2026 R3E Network
2// Licensed under the MIT License
3
4//! Neo N3 Rust Development Pack
5//!
6//! Complete Rust SDK for Neo N3 smart contract development
7
8#[cfg(feature = "serde")]
9pub mod codec;
10pub mod native_contracts;
11pub mod nep_macros;
12pub mod standards;
13#[cfg(feature = "serde")]
14pub mod storage;
15#[cfg(feature = "serde")]
16pub mod utils;
17
18// Re-export specific items to avoid conflicts
19pub use native_contracts::*;
20pub use neo_macros::*;
21pub use neo_runtime::{
22    call_typed, ContractCallError, DefaultContractCaller, NeoContractRuntime, NeoRuntime,
23    NeoRuntimeContext, NeoStorage, RawKeyBuilder, RawStorage, RawStorageGet,
24};
25#[cfg(feature = "std")]
26pub use neo_runtime::{NeoCrypto, NeoJSON};
27pub use neo_syscalls::*;
28pub use neo_types::{
29    serialise_array, serialise_notification, serialise_value, BigInt, ContractCaller,
30    FromNeoValue, Hash160, Hash256, NeoArray, NeoBoolean, NeoByteString, NeoContract,
31    NeoContractABI, NeoContractEntry, NeoContractEvent, NeoContractManifest, NeoContractMethod,
32    NeoContractMethodTrait, NeoContractParameter, NeoContractPermission, NeoError, NeoInteger,
33    NeoIterator, NeoMap, NeoResult, NeoStorageContext, NeoString, NeoStruct, NeoValue,
34    MAX_NOTIFICATION_SIZE, MAX_STACK_SIZE,
35};
36
37#[cfg(feature = "serde")]
38pub use serde;
39pub use standards::*;
40
41/// Neo N3 Prelude - commonly used items
42pub mod prelude {
43    #[cfg(feature = "serde")]
44    pub use crate::serde;
45    pub use crate::{
46        call_typed, native_contracts::*, neo_contract, neo_entry, neo_event, neo_manifest_overlay,
47        neo_method, neo_permission, neo_safe, neo_safe_methods, neo_supported_standards,
48        neo_trusts, serialise_array, serialise_notification, serialise_value, standards::*, BigInt,
49        ContractCallError, ContractCaller, DefaultContractCaller, FromNeoValue, Hash160, Hash256,
50        NeoArray, NeoBoolean, NeoByteString, NeoContract, NeoContractABI, NeoContractEntry,
51        NeoContractEvent, NeoContractManifest, NeoContractMethod, NeoContractMethodTrait,
52        NeoContractParameter, NeoContractPermission, NeoContractRuntime, NeoError, NeoInteger,
53        NeoIterator, NeoMap, NeoResult, NeoRuntime, NeoRuntimeContext, NeoStorage,
54        NeoStorageContext, NeoString, NeoStruct, NeoValue, RawKeyBuilder, RawStorage, RawStorageGet,
55        MAX_NOTIFICATION_SIZE, MAX_STACK_SIZE,
56    };
57    #[cfg(feature = "std")]
58    pub use crate::{NeoCrypto, NeoJSON};
59}
60
61/// Neo N3 Contract Examples
62///
63/// Basic Hello-World pattern showing how to expose a method that simply
64/// returns contract state. This shows canonical devpack syntax for contract methods and metadata:
65///
66/// ```rust
67/// use neo_devpack::prelude::*;
68///
69/// #[neo_contract]
70/// pub struct HelloWorld {
71///     greeting: NeoString,
72/// }
73///
74/// impl HelloWorld {
75///     #[neo_method]
76///     pub fn say_hello(&self) -> NeoResult<NeoString> {
77///         Ok(self.greeting.clone())
78///     }
79/// }
80/// ```
81///
82/// A more complete storage-backed counter that demonstrates manifest overlays,
83/// permissions, witnesses and event emission:
84///
85/// ```rust
86/// use neo_devpack::prelude::*;
87///
88/// #[neo_event]
89/// pub struct CounterIncreased {
90///     pub account: NeoByteString,
91///     pub new_value: NeoInteger,
92/// }
93///
94/// neo_manifest_overlay!(r#"{
95///     "name": "FamousCounter",
96///     "features": { "storage": true }
97/// }"#);
98/// neo_permission!("*", ["balanceOf"]);
99/// neo_supported_standards!(["NEP-17"]);
100///
101/// #[neo_contract]
102/// pub struct FamousCounter;
103///
104/// impl FamousCounter {
105///     #[neo_method]
106///     pub fn increment(&self, caller: NeoByteString) -> NeoResult<NeoInteger> {
107///         if !NeoRuntime::check_witness(&caller)?.as_bool() {
108///             return Err(NeoError::InvalidOperation);
109///         }
110///
111///         let context = NeoStorage::get_context()?;
112///         let counter_key = NeoByteString::from_slice(b"counter");
113///         NeoStorage::put(&context, &counter_key, &NeoByteString::from_slice(b"1"))?;
114///
115///         CounterIncreased {
116///             account: caller.clone(),
117///             new_value: NeoInteger::new(1),
118///         }
119///         .emit()?;
120///
121///         Ok(NeoInteger::new(1))
122///     }
123/// }
124/// ```
125pub struct ExampleContract;
126
127#[cfg(test)]
128mod tests {
129    use super::*;
130
131    #[test]
132    fn test_neo_types() {
133        let int = NeoInteger::new(42);
134        assert_eq!(int.as_i32_saturating(), 42);
135
136        let bool_val = NeoBoolean::new(true);
137        assert!(bool_val.as_bool());
138
139        let string = NeoString::from_str("Hello, Neo!");
140        assert_eq!(string.as_str(), "Hello, Neo!");
141    }
142}