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