Skip to main content

truthlinked_sdk/
lib.rs

1//! TruthLinked Smart Contract SDK
2//!
3//! This SDK provides a complete toolkit for building smart contracts on the TruthLinked blockchain.
4//!
5//! # Features
6//!
7//! - **Storage**: Type-safe collections (maps, vectors, blobs) with dual API pattern
8//! - **Codec**: Efficient encoding/decoding for 32-byte and variable-length data
9//! - **Events**: Structured logging with indexed topics
10//! - **Manifests**: Declare storage access patterns for parallel execution
11//! - **Oracle**: HTTP requests for external data
12//! - **Testing**: In-memory storage harness for unit tests
13//!
14//! # Quick Start
15//!
16//! ```ignore
17//! use truthlinked_sdk::prelude::*;
18//!
19//! #[derive(Manifest)]
20//! struct Counter {
21//!     #[manifest(read, write)]
22//!     value: Slot,
23//! }
24//!
25//! fn increment() -> Result<()> {
26//!     let counter = Counter {
27//!         value: Slot::from_label("count"),
28//!     };
29//!     let current = counter.value.read_u64()?;
30//!     counter.value.write_u64(current + 1)?;
31//!     Ok(())
32//! }
33//!
34//! contract_entry!(increment);
35//! ```
36//!
37//! # Module Overview
38//!
39//! - [`abi`] - Function selectors and calldata parsing
40//! - [`backend`] - Storage backend abstraction
41//! - [`call`] - Cross-contract calls
42//! - [`codec`] - Encoding/decoding traits and builders
43//! - [`collections`] - StorageMap, StorageVec, StorageBlob
44//! - [`context`] - Execution context (caller, height, timestamp, etc.)
45//! - [`env`] - Low-level WASM host bindings
46//! - [`error`] - Error types and Result alias
47//! - [`hashing`] - Cryptographic utilities
48//! - [`log`] - Event system
49//! - [`manifest`] - Storage access declarations
50//! - [`oracle`] - HTTP oracle interface
51//! - [`storage`] - Direct storage slot access
52//! - [`testing`] - Unit test utilities
53
54#![no_std]
55
56extern crate alloc;
57extern crate self as truthlinked_sdk;
58
59pub mod abi;
60pub mod backend;
61pub mod call;
62pub mod codec;
63pub mod collections;
64pub mod context;
65pub mod env;
66pub mod error;
67pub mod hashing;
68pub mod log;
69pub mod manifest;
70pub mod oracle;
71pub mod storage;
72#[cfg(any(test, feature = "testing"))]
73pub mod testing;
74
75pub use error::{Error, Result};
76pub use truthlinked_sdk_macros::{error_code, require, BytesCodec, Codec32, Event, Manifest};
77
78/// Prelude module with commonly used imports.
79///
80/// Import everything with `use truthlinked_sdk::prelude::*;`
81pub mod prelude {
82    pub use crate::abi;
83    pub use crate::backend::{HostStorage, MemoryStorage, StorageBackend};
84    pub use crate::call;
85    pub use crate::codec::{BytesCodec, Codec32, Decoder, Encoder};
86    pub use crate::collections::{Namespace, StorageBlob, StorageMap, StorageVec};
87    pub use crate::context;
88    pub use crate::env;
89    pub use crate::hashing;
90    pub use crate::log;
91    pub use crate::manifest::{ContractManifest, Manifest, ManifestBuilder, StorageKeySpec};
92    pub use crate::oracle;
93    pub use crate::storage::{self, Slot};
94    #[cfg(any(test, feature = "testing"))]
95    pub use crate::testing::{StorageHarness, TestBlob, TestMap, TestVec};
96    pub use crate::{
97        contract_entry, error_code, require, slot, BytesCodec, Codec32, Error, Event, Manifest,
98        Result,
99    };
100}
101
102/// Defines the contract entry point.
103///
104/// # Example
105///
106/// ```ignore
107/// fn my_handler() -> Result<()> {
108///     // Contract logic
109///     Ok(())
110/// }
111///
112/// contract_entry!(my_handler);
113/// ```
114#[macro_export]
115macro_rules! contract_entry {
116    ($handler:path) => {
117        #[no_mangle]
118        pub extern "C" fn execute() -> i32 {
119            match $handler() {
120                Ok(()) => 0,
121                Err(err) => err.code(),
122            }
123        }
124    };
125}