ckb_ssri_std/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#![no_std]
//! # CKB SSRI std
//! 
//! Utils for implementing SSRI-compliant smart contracts on the Nervos CKB blockchain.
//! 
//! ## Overview
//! 
//! The SSRI (Standard Smart Contract Runtime Interface) std provides a standardized way to develop
//! smart contracts that are compliant with the SSRI protocol. This enables better interoperability
//! and a more consistent development experience across the CKB ecosystem.
//!
//! ## Features
//! 
//! - **Public Traits**: Pre-defined interfaces that receive first-class support within the ecosystem
//! - **Utility Functions**: Helper functions for SSRI-VM syscalls and data handling
//! - **Procedural Macros**: Simplify contract development with automatic SSRI method generation
//! - **No Standard Library**: Designed for the constrained smart contract environment
//!
//! ## Usage
//!
//! Add this to your `Cargo.toml`:
//! ```toml
//! [dependencies]
//! ckb-ssri-std = "0.1.0"
//! ```
//!
//! ## Example
//!
//! ```rust,no_run
//! use ckb_ssri_std::prelude::*;
//! use ckb_ssri_std::public_module_traits::udt::UDT;
//! 
//! // Implement a basic UDT (User-Defined Token)
//! #[derive(Default)]
//! struct MyToken;
//! 
//! impl UDT for MyToken {
//!     type Error = ();
//!     // ... implement required methods
//! }
//! ```

pub mod public_module_traits;
pub mod prelude;
pub mod utils;
pub mod macros;

// Re-export proc macros at crate root for convenience
pub use macros::*;

extern crate alloc;

// macro_rules! ssri_entry {
//     ( $( $module:path ),* $(,)? ) => {
//         pub fn unified_dispatch(namespace_and_function: &str, args: Vec<&str>) -> Result<String, crate::error::DispatchError> {
//             $(
//                 let argv = ckb_std::env::argv();
//                 if argv.is_empty() {
//                     return fallback::fallback().map(|_| ());
//                 }

//                 if vm_version() != u64::MAX {
//                     return Err(Error::InvalidVmVersion);
//                 }

//                 set_content(&res)?;
//                 if $module::EXPOSED_FUNCTIONS.contains(&namespace_and_function) {
//                     return $module::dispatch_function(namespace_and_function, args);
//                 }
//             )*
//             Err(crate::error::DispatchError::FunctionNotFound)
//         }

//         pub fn get_methods() -> Vec<&'static str> {
//             let mut methods = Vec::new();
//             $(
//                 methods.extend_from_slice($module::EXPOSED_FUNCTIONS);
//             )*
//             methods
//         }
//     };
// }

#[repr(i8)]
#[derive(Debug)]
/// Represents possible errors that can occur during SSRI method execution
///
/// This enum provides a standardized set of errors that can occur when executing
/// SSRI methods. These errors help identify issues with method discovery,
/// argument validation, implementation status, and environment compatibility.
///
/// # Examples
///
/// ```rust
/// use ckb_ssri_std::SSRIError;
///
/// fn example_handler() -> Result<(), SSRIError> {
///     // Method implementation missing
///     Err(SSRIError::SSRIMethodsNotImplemented)
/// }
/// ```
pub enum SSRIError {
    /// The requested SSRI method was not found in the contract
    SSRIMethodsNotFound,
    /// The arguments provided to the SSRI method were invalid
    SSRIMethodsArgsInvalid,
    /// The requested SSRI method is not implemented
    SSRIMethodsNotImplemented,
    /// The method requires a higher execution environment level
    SSRIMethodRequireHigherLevel,
    /// The CKB VM version is not compatible with this implementation
    InvalidVmVersion
}