Skip to content

Instantly share code, notes, and snippets.

@zulhfreelancer
Forked from lukehedger/HelloYou.sol
Created March 17, 2018 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zulhfreelancer/e2499efee8d372c68c12e28a41cb7ebb to your computer and use it in GitHub Desktop.
Save zulhfreelancer/e2499efee8d372c68c12e28a41cb7ebb to your computer and use it in GitHub Desktop.
Created using browser-solidity: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.18+commit.9cf6e910.js&optimize=undefined&gist=
pragma solidity ^0.4.18;
// import './SomeContract.sol';
contract HelloYou {
event Hello(address you);
function sayHello() public {
address _person = msg.sender;
Hello(_person);
}
}
pragma solidity ^0.4.18;
import './TodoBank.sol';
import './TodoStorage.sol';
// @title Todo
// @author Luke Hedger
contract Todo {
/*
* @notice todoBank state variable to store reference to TodoBank contract
* @see https://solidity.readthedocs.io/en/develop/structure-of-a-contract.html#state-variables
*/
TodoBank todoBank;
/*
* @notice todoStorage state variable to store reference to TodoStorage
* contract
* @see https://solidity.readthedocs.io/en/develop/structure-of-a-contract.html#state-variables
*/
TodoStorage todoStorage;
/*
* @notice TodoAdded event
* @param {bytes32} todoId
* @see https://solidity.readthedocs.io/en/develop/structure-of-a-contract.html#events
*/
event TodoAdded(bytes32 todoId);
/*
* @notice Todo contract constructor function - called when creating the
* contract. Will instantiate the TodoBank and TodoStorage contracts
* at the provided addresses.
* @param {address} _todoBank Address of the deployed TodoBank contract
* @param {address} _todoStorage Address of the deployed TodoStorage contract
* @see http://solidity.readthedocs.io/en/develop/contracts.html#creating-contracts
*/
function Todo(address _todoBank, address _todoStorage) public {
todoBank = TodoBank(_todoBank);
todoStorage = TodoStorage(_todoStorage);
}
/*
* @notice Method to add a new todo for message sender
* @param {bytes32} _todoId Swarm hash of todo metadata
* @see https://solidity.readthedocs.io/en/develop/structure-of-a-contract.html#functions
*/
function addTodo(bytes32 _todoId) public payable {
/*
* @notice Compute `_userId` from hash of `msg.sender` address
*/
bytes32 _userId = keccak256(msg.sender);
/*
* @notice Call the `setTodo` method on the TodoStorage contract
*/
todoStorage.setTodo(_todoId, _userId);
/*
* @notice Call the `setTodoDeposit` method on the TodoBank contract.
* `msg.value` sent in transaction is used as the deposit amount.
*/
todoBank.setTodoDeposit(_todoId, msg.value);
/*
* @notice Trigger the `TodoAdded` event
*/
TodoAdded(_todoId);
}
/*
* @notice Method to get a user's todo by index from the array of todos stored
* @param {uint} _index Index of required todo
* @return {bytes32} todoId
* @see https://solidity.readthedocs.io/en/develop/structure-of-a-contract.html#functions
*/
function getTodo(uint _index) public view returns (bytes32) {
/*
* @notice Compute `_userId` from hash of `msg.sender` address
*/
bytes32 _userId = keccak256(msg.sender);
/*
* @notice Call the `getTodoByIndex` method on the TodoStorage contract
*/
return todoStorage.getTodoByIndex(_index, _userId);
}
/*
* @notice Method to get a user's todo by index from the array of todos stored
* @return {uint} todoCount
* @see https://solidity.readthedocs.io/en/develop/structure-of-a-contract.html#functions
*/
function getTodoCount() public view returns (uint) {
/*
* @notice Compute `_userId` from hash of `msg.sender` address
*/
bytes32 _userId = keccak256(msg.sender);
/*
* @notice Call the `getTodoCount` method on the TodoStorage contract.
*/
return todoStorage.getTodoCount(_userId);
}
}
pragma solidity ^0.4.18;
// @title TodoBank
// @author Luke Hedger
contract TodoBank {
/*
* @notice Store todo deposit in TodoVault as a mapping of `bytes32` to `uint`
* @see http://solidity.readthedocs.io/en/develop/types.html#mappings
*/
mapping(bytes32 => uint) TodoVault;
/*
* @notice Get a todo deposit value from the vault
* @param {bytes32} _todoId
* @return {uint} todoDeposit
* @see https://solidity.readthedocs.io/en/develop/structure-of-a-contract.html#functions
*/
function getTodoDeposit(bytes32 _todoId) public view returns (uint) {
/*
* @notice Get deposit from vault by todoId
*/
return TodoVault[_todoId];
}
/*
* @notice Set a todo deposit value to the vault
* @param {bytes32} _todoId
* @param {uint} _deposit
* @see https://solidity.readthedocs.io/en/develop/structure-of-a-contract.html#functions
*/
function setTodoDeposit(bytes32 _todoId, uint _deposit) public {
/*
* @notice Set deposit to vault by todoId
*/
TodoVault[_todoId] = _deposit;
}
}
pragma solidity ^0.4.18;
// @title TodoStorage
// @author Luke Hedger
contract TodoStorage {
/*
* @notice Store todos in TodoStore as a mapping of `bytes32` to `bytes32[]`
* @see http://solidity.readthedocs.io/en/develop/types.html#mappings
*/
mapping(bytes32 => bytes32[]) TodoStore;
/*
* @notice Get todo by index
* @param {uint} _index
* @param {bytes32} _user
* @return {bytes32} todo
* @see https://solidity.readthedocs.io/en/develop/structure-of-a-contract.html#functions
*/
function getTodoByIndex(uint _index, bytes32 _user) public view returns (bytes32) {
/*
* @notice Get todo from store by userId and index
*/
return TodoStore[_user][_index];
}
/*
* @notice Get number of todos for a user
* @param {bytes32} _user
* @return {uint} todoCount
* @see https://solidity.readthedocs.io/en/develop/structure-of-a-contract.html#functions
*/
function getTodoCount(bytes32 _user) public view returns (uint) {
/*
* @notice Get length of todo array from store by userId
*/
return TodoStore[_user].length;
}
/*
* @notice Set a todo to the store
* @param {bytes32} _todoId
* @param {bytes32} _user
* @see https://solidity.readthedocs.io/en/develop/structure-of-a-contract.html#functions
*/
function setTodo(bytes32 _todoId, bytes32 _user) public {
/*
* @notice Push todo to store by userId
*/
TodoStore[_user].push(_todoId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment