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