An optimized todo list smart contract built with ink! v5.1.1, demonstrating efficient data storage patterns using Mapping-based storage for scalable task management.
This contract implements an optimized todo list system with:
- Task Management: Add, complete, and remove tasks
- Mapping Storage: Efficient mapping-based storage pattern
- Scalable Design: Better performance for large datasets
- Optimized Operations: O(1) task lookup and management
lib.rs- Main contract implementation- Mapping Storage: Efficient mapping-based task storage
- Task Struct: Optimized task data structure
- ID Management: Efficient ID generation and tracking
pub struct Task {
pub id: u32,
pub description: String,
pub completed: bool,
}
pub struct Todo {
tasks: Mapping<u32, Task>, // Mapping of task ID to task
next_id: u32, // Next available ID
task_count: u32, // Total number of tasks
}- Add Task: Create new todo items
- Complete Task: Mark tasks as done
- Remove Task: Delete tasks from the list
- Get Tasks: Retrieve all tasks
- Get Task: Retrieve specific task by ID
- Mapping-based: Uses ink!'s Mapping for storage
- O(1) Lookup: Constant time task retrieval
- Scalable: Efficient for large datasets
- Memory Efficient: Only stores active tasks
# Development build
cargo contract build
# Production build
cargo contract build --release# Deploy to local node
cargo contract instantiate --constructor new --suri //Alice --skip-confirm// Add a new task
add_task(description: String) -> u32
// Mark task as complete
complete_task(id: u32) -> bool
// Remove a task
remove_task(id: u32) -> bool
// Get all tasks
get_tasks() -> Vec<Task>
// Get specific task
get_task(id: u32) -> Option<Task>
// Get task count
get_task_count() -> u32cargo testcargo test --features e2e-testsThe contract includes comprehensive tests covering:
- Task creation and management
- Task completion and removal
- Task retrieval and counting
- Edge cases and error conditions
- Performance characteristics
// Create a new todo contract
let mut todo = Todo::new();
// Add tasks
let task1_id = todo.add_task("Buy groceries".to_string());
let task2_id = todo.add_task("Walk the dog".to_string());
// Complete a task
todo.complete_task(task1_id);
// Get specific task
let task = todo.get_task(task1_id);
// Get all tasks
let all_tasks = todo.get_tasks();
// Get task count
let count = todo.get_task_count();
// Remove a task
todo.remove_task(task2_id);[dependencies]
ink = { version = "5.1.1", default-features = false }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.11", default-features = false, features = ["derive"] }- Large Teams: Scalable team task management
- Project Tracking: Complex project management
- Workflow Systems: Business process automation
- Resource Management: Resource allocation and tracking
- Collaborative Tools: Shared task management
- Project Management: Decentralized project tracking
- Workflow Automation: Business process management
- Resource Planning: Advanced resource management
- Advanced ink!: Learning mapping storage patterns
- Storage Optimization: Understanding efficient storage
- Scalability: Learning scalable design patterns
- Performance: Understanding O(1) operations
- Efficient Storage: Optimized storage patterns
- Scalable Design: Handles large datasets efficiently
- Memory Management: Efficient memory usage
- Performance: Fast operations for all dataset sizes
- Mapping Storage: O(1) lookup time
- Memory Efficient: Only stores active tasks
- Scalable: Handles thousands of tasks
- Gas Efficient: Optimized for transaction costs
- Mapping Storage: O(1) task lookup
- Memory Usage: Constant per active task
- Query Time: O(1) for individual tasks
- Scalability: Excellent for large datasets
| Operation | Simple Todo | Todo Map |
|---|---|---|
| Add Task | O(1) | O(1) |
| Get Task | O(n) | O(1) |
| Complete Task | O(n) | O(1) |
| Remove Task | O(n) | O(1) |
| Get All Tasks | O(n) | O(n) |
| Feature | Simple Todo | Todo Map |
|---|---|---|
| Storage | Vec | Mapping |
| Lookup | O(n) | O(1) |
| Memory | Linear | Constant |
| Scalability | Limited | Excellent |
| Complexity | Simple | More complex |
| Use Case | Small datasets | Large datasets |
- Large Datasets: Handles thousands of tasks
- Efficient Queries: Fast task retrieval
- Memory Efficient: Optimized storage usage
- Gas Efficient: Lower transaction costs
- O(1) Operations: Constant time operations
- Efficient Storage: Mapping-based storage
- Scalable Design: Grows efficiently
- Optimized Queries: Fast data access
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
This project is provided as-is for educational purposes. Check the main repository for licensing information.
For issues and questions:
- Create an issue in the repository
- Check the main README for troubleshooting
- Review the ink! documentation
🦀 Built with Rust + ink! v5.1.1
This optimized todo contract demonstrates advanced smart contract development patterns and serves as an excellent example of scalable, efficient smart contract design.
