-
65 |
71 |
-
73 |
├── .gitignore ├── migrations ├── 2_deploy_contracts.js └── 1_initial_migration.js ├── bs-config.json ├── truffle-config.js ├── contracts ├── Migrations.sol └── TodoList.sol ├── package.json ├── test └── TodoList.test.js └── src ├── index.html └── app.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /migrations/2_deploy_contracts.js: -------------------------------------------------------------------------------- 1 | var TodoList = artifacts.require("./TodoList.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(TodoList); 5 | }; 6 | -------------------------------------------------------------------------------- /migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /bs-config.json: -------------------------------------------------------------------------------- 1 | { 2 | "server": { 3 | "baseDir": [ 4 | "./src", 5 | "./build/contracts" 6 | ], 7 | "routes": { 8 | "/vendor": "./node_modules" 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /truffle-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | networks: { 3 | development: { 4 | host: "127.0.0.1", 5 | port: 7545, 6 | network_id: "*" // Match any network id 7 | } 8 | }, 9 | solc: { 10 | optimizer: { 11 | enabled: true, 12 | runs: 200 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity >=0.4.21 <0.6.0; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | constructor() public { 8 | owner = msg.sender; 9 | } 10 | 11 | modifier restricted() { 12 | if (msg.sender == owner) _; 13 | } 14 | 15 | function setCompleted(uint completed) public restricted { 16 | last_completed_migration = completed; 17 | } 18 | 19 | function upgrade(address new_address) public restricted { 20 | Migrations upgraded = Migrations(new_address); 21 | upgraded.setCompleted(last_completed_migration); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "eth-todo-list", 3 | "version": "1.0.0", 4 | "description": "Blockchain Todo List Powered By Ethereum", 5 | "main": "truffle-config.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "dev": "lite-server", 11 | "test": "echo \"Error: no test specified\" && sexit 1" 12 | }, 13 | "author": "gregory@dappuniversity.com", 14 | "license": "ISC", 15 | "devDependencies": { 16 | "bootstrap": "4.1.3", 17 | "chai": "^4.1.2", 18 | "chai-as-promised": "^7.1.1", 19 | "chai-bignumber": "^2.0.2", 20 | "lite-server": "^2.3.0", 21 | "nodemon": "^1.17.3", 22 | "truffle": "5.0.2", 23 | "truffle-contract": "3.0.6" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /contracts/TodoList.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.5.0; 2 | 3 | contract TodoList { 4 | uint public taskCount = 0; 5 | 6 | struct Task { 7 | uint id; 8 | string content; 9 | bool completed; 10 | } 11 | 12 | mapping(uint => Task) public tasks; 13 | 14 | event TaskCreated( 15 | uint id, 16 | string content, 17 | bool completed 18 | ); 19 | 20 | event TaskCompleted( 21 | uint id, 22 | bool completed 23 | ); 24 | 25 | constructor() public { 26 | createTask("Check out dappuniversity.com"); 27 | } 28 | 29 | function createTask(string memory _content) public { 30 | taskCount ++; 31 | tasks[taskCount] = Task(taskCount, _content, false); 32 | emit TaskCreated(taskCount, _content, false); 33 | } 34 | 35 | function toggleCompleted(uint _id) public { 36 | Task memory _task = tasks[_id]; 37 | _task.completed = !_task.completed; 38 | tasks[_id] = _task; 39 | emit TaskCompleted(_id, _task.completed); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /test/TodoList.test.js: -------------------------------------------------------------------------------- 1 | const TodoList = artifacts.require('./TodoList.sol') 2 | 3 | contract('TodoList', (accounts) => { 4 | before(async () => { 5 | this.todoList = await TodoList.deployed() 6 | }) 7 | 8 | it('deploys successfully', async () => { 9 | const address = await this.todoList.address 10 | assert.notEqual(address, 0x0) 11 | assert.notEqual(address, '') 12 | assert.notEqual(address, null) 13 | assert.notEqual(address, undefined) 14 | }) 15 | 16 | it('lists tasks', async () => { 17 | const taskCount = await this.todoList.taskCount() 18 | const task = await this.todoList.tasks(taskCount) 19 | assert.equal(task.id.toNumber(), taskCount.toNumber()) 20 | assert.equal(task.content, 'Check out dappuniversity.com') 21 | assert.equal(task.completed, false) 22 | assert.equal(taskCount.toNumber(), 1) 23 | }) 24 | 25 | it('creates tasks', async () => { 26 | const result = await this.todoList.createTask('A new task') 27 | const taskCount = await this.todoList.taskCount() 28 | assert.equal(taskCount, 2) 29 | const event = result.logs[0].args 30 | assert.equal(event.id.toNumber(), 2) 31 | assert.equal(event.content, 'A new task') 32 | assert.equal(event.completed, false) 33 | }) 34 | 35 | it('toggles task completion', async () => { 36 | const result = await this.todoList.toggleCompleted(1) 37 | const task = await this.todoList.tasks(1) 38 | assert.equal(task.completed, true) 39 | const event = result.logs[0].args 40 | assert.equal(event.id.toNumber(), 1) 41 | assert.equal(event.completed, true) 42 | }) 43 | 44 | }) 45 | -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 |Loading...
58 |