Skip to content

Latest commit

 

History

History
221 lines (163 loc) · 5.09 KB

File metadata and controls

221 lines (163 loc) · 5.09 KB

✅ Simple Todo Contract

A basic todo list smart contract built with ink! v5.1.1, demonstrating simple data storage patterns using Vec-based storage for task management.

Todo Contract

📋 Overview

This contract implements a simple todo list system with:

  • Task Management: Add, complete, and remove tasks
  • Vec Storage: Simple vector-based storage pattern
  • Basic Operations: Core CRUD functionality
  • Simple Design: Easy to understand and extend

🏗️ Architecture

Core Components

  • lib.rs - Main contract implementation
  • Vec Storage: Simple vector-based task storage
  • Task Struct: Basic task data structure

Data Structure

pub struct Task {
    pub id: u32,
    pub description: String,
    pub completed: bool,
}

pub struct Todo {
    tasks: Vec<Task>,        // Vector of tasks
    next_id: u32,           // Next available ID
}

🚀 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

Simple Storage Pattern

  • Vec-based: Uses Rust's Vec for storage
  • Sequential IDs: Auto-incrementing task IDs
  • Simple Queries: Basic task retrieval

🛠️ 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>

🧪 Testing

Unit Tests

cargo test

Integration Tests

cargo test --features e2e-tests

Test Coverage

The contract includes tests covering:

  • Task creation
  • Task completion
  • Task removal
  • Task retrieval
  • Edge cases
  • Error conditions

📊 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 all tasks
let all_tasks = todo.get_tasks();

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

Personal Task Management

  • Daily Tasks: Personal todo lists
  • Project Management: Simple project tracking
  • Habit Tracking: Daily habit monitoring
  • Goal Setting: Personal goal management

Educational Purposes

  • Learning ink!: Introduction to smart contracts
  • Storage Patterns: Understanding Vec storage
  • Basic CRUD: Database-like operations
  • Contract Development: First contract example

Simple dApps

  • Task Sharing: Shared todo lists
  • Team Coordination: Basic team task management
  • Event Planning: Simple event task lists
  • Workflow Management: Basic process tracking

🔒 Security Considerations

Simple Security

  • Basic Access: No access control (anyone can modify)
  • Simple Validation: Basic input checking
  • No Complex Logic: Straightforward operations

Limitations

  • No Access Control: Anyone can modify tasks
  • No Data Validation: Limited input validation
  • Simple Storage: Not optimized for large datasets

📈 Performance

Storage Characteristics

  • Vec Storage: Linear search for tasks
  • Memory Usage: Grows with number of tasks
  • Query Time: O(n) for task lookup
  • Simple Operations: Fast for small datasets

Scalability

  • Small Datasets: Good for < 100 tasks
  • Large Datasets: May become slow
  • Memory Growth: Linear with task count

🆚 Comparison with Todo Map

Feature Simple Todo Todo Map
Storage Vec Mapping
Lookup O(n) O(1)
Memory Linear Constant
Scalability Limited Better
Complexity Simple More complex

🤝 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 simple todo contract demonstrates basic smart contract development patterns and serves as an excellent starting point for learning ink! development.