Build on Internet Computer with ICP Rust CDK
State Management 1
Building an Exam Smart Contract on the Internet Computer Protocol
In this video, we begin building a smart contract called Exam, which serves as a practical example to demonstrate core functionalities of smart contracts on the Internet Computer Protocol (ICP). We’ll cover everything from setting up the project and importing libraries to struct creation and memory management.
Setting Up the Project and Imports
We start by cleaning out the existing files in our backend src directory to begin with a fresh workspace. Then, we include several necessary imports:
- From the Candid module, we import tools for type conversion and data serialization.
- We also import utilities from ic-stable-structures and standard Rust libraries.
Next, we update the Cargo.toml file by adding the ic-stable-structures dependency:
[dependencies]
ic-stable-structures = "0.5.4"
This resolves any import errors and prepares the environment for smart contract development.
Understanding Stable Structures
ICP uses a different state management approach compared to traditional smart contracts. One key concept is stable memory, which ensures the contract's state persists even after redeployments.
This is crucial—without it, data would reset on each update, disrupting application logic and testing. Our contract will use stable structures to maintain consistent state.
Creating the Exam Struct
We define a new struct called Exam:
struct Exam {
out_of: u8,
course_name: String,
curve: u8,
}
This struct contains:
- out_of: The total points of the exam.
- course_name: A string indicating the course title.
- curve: An optional grading curve.
We annotate the struct with macros to make it compatible with ICP's Candid and serialization requirements:
#[derive(CandidType, Serialize, Deserialize)]
These macros allow the frontend to recognize the struct and facilitate seamless data conversion between memory and UI.
Implementing Traits for State Management
To work with stable memory, we implement the following traits for our struct:
1. Storable
Enables converting the struct to and from bytes:
- to_bytes(): Serializes the data.
- from_bytes(): Deserializes the data back into the original struct.
2. BoundedStorable
Specifies:
- MAX_SIZE: The maximum data size (set to 100).
- IS_FIXED_SIZE: Indicates if the data size is dynamic (set to false).
These definitions ensure efficient and predictable memory usage when interacting with ICP’s stable memory.
We also define a type Memory alias to simplify references to virtual memory:
type Memory = VirtualMemory<DefaultMemoryImpl>;
const MAX_VALUE_SIZE: u32 = 100;
This approach avoids repetitive long type definitions and improves code readability.
Recap: What We’ve Done
- Defined the Exam struct with key attributes.
- Added necessary macros for Candid compatibility and serialization.
- Implemented storable traits to enable storing and retrieving the struct in stable memory.
- Defined dynamic memory limits using the BoundedStorable trait.
These steps represent the foundational structure for many smart contracts on the Internet Computer. Once understood, the process becomes much more intuitive and repeatable for different projects.
Comments
You need to enroll in the course to be able to comment!