Skip to content

Instantly share code, notes, and snippets.

@cdinu
Created February 12, 2018 12:11
Show Gist options
  • Save cdinu/b5a42e561bd9a3f5bad457356ec791e9 to your computer and use it in GitHub Desktop.
Save cdinu/b5a42e561bd9a3f5bad457356ec791e9 to your computer and use it in GitHub Desktop.
A simple todo list smart contract in Solidity
pragma solidity ^0.4.0;
contract TodosContract {
address private _owner ;
struct Todo {
string title;
bool completed;
}
mapping (address => Todo[]) public todos;
event TodoCreated(address owner, uint todoId, string title);
event TodoToggled(address owner, uint todoId, bool completed);
function Todos() public {
_owner = msg.sender;
}
function addTodo(string _title) public {
require(bytes(_title).length != 0);
uint id = todos[msg.sender].push(Todo(_title, false)) - 1;
TodoCreated(msg.sender, id, _title);
}
function toggleTodo(uint _todoId) external {
require(_todoId < todos[msg.sender].length);
Todo storage todo = todos[msg.sender][_todoId];
todo.completed = !todo.completed;
TodoToggled(msg.sender, _todoId, todo.completed);
}
function getTodosCount() public constant returns (uint){
return todos[msg.sender].length;
}
function getTodos() public constant returns (Todo[]){
return todos[msg.sender];
}
function getTodo(uint _todoId) public constant returns (Todo) {
require(_todoId < todos[msg.sender].length);
return todos[msg.sender][_todoId];
}
function getTodoTitle(uint _todoId) public constant returns (string) {
Todo memory todo = getTodo(_todoId);
return todo.title;
}
function getTodoCompleted(uint _todoId) public constant returns (bool) {
Todo memory todo = getTodo(_todoId);
return todo.completed;
}
function deleteContract() external {
require(msg.sender == _owner);
selfdestruct(_owner);
}
function () public {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment