Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

✅ Todo Map Contract

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.

Todo Map Contract

📋 Overview

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

🏗️ Architecture

Core Components

  • 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

Data Structure

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
}

🚀 Features

Task Management

  • 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

Optimized Storage Pattern

  • 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

🛠️ Usage

Building the Contract

# Development build
cargo contract build

# Production build
cargo contract build --release

Deploying

# Deploy to local node
cargo contract instantiate --constructor new --suri //Alice --skip-confirm

Key Functions

Task Operations

// 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() -> u32

🧪 Testing

Unit Tests

cargo test

Integration Tests

cargo test --features e2e-tests

Test Coverage

The contract includes comprehensive tests covering:

  • Task creation and management
  • Task completion and removal
  • Task retrieval and counting
  • Edge cases and error conditions
  • Performance characteristics

📊 Example Usage

// 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);

🔧 Configuration

Cargo.toml

[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"] }

🌐 Use Cases

Enterprise Task Management

  • Large Teams: Scalable team task management
  • Project Tracking: Complex project management
  • Workflow Systems: Business process automation
  • Resource Management: Resource allocation and tracking

Advanced dApps

  • Collaborative Tools: Shared task management
  • Project Management: Decentralized project tracking
  • Workflow Automation: Business process management
  • Resource Planning: Advanced resource management

Educational Purposes

  • Advanced ink!: Learning mapping storage patterns
  • Storage Optimization: Understanding efficient storage
  • Scalability: Learning scalable design patterns
  • Performance: Understanding O(1) operations

🔒 Security Considerations

Advanced Security

  • Efficient Storage: Optimized storage patterns
  • Scalable Design: Handles large datasets efficiently
  • Memory Management: Efficient memory usage
  • Performance: Fast operations for all dataset sizes

Optimizations

  • Mapping Storage: O(1) lookup time
  • Memory Efficient: Only stores active tasks
  • Scalable: Handles thousands of tasks
  • Gas Efficient: Optimized for transaction costs

📈 Performance

Storage Characteristics

  • Mapping Storage: O(1) task lookup
  • Memory Usage: Constant per active task
  • Query Time: O(1) for individual tasks
  • Scalability: Excellent for large datasets

Performance Comparison

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)

🆚 Comparison with Simple Todo

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

🚀 Advanced Features

Scalability

  • Large Datasets: Handles thousands of tasks
  • Efficient Queries: Fast task retrieval
  • Memory Efficient: Optimized storage usage
  • Gas Efficient: Lower transaction costs

Performance

  • O(1) Operations: Constant time operations
  • Efficient Storage: Mapping-based storage
  • Scalable Design: Grows efficiently
  • Optimized Queries: Fast data access

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Submit a pull request

📄 License

This project is provided as-is for educational purposes. Check the main repository for licensing information.

🆘 Support

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.