├── README.md ├── client ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ ├── index.html │ ├── logo192.png │ ├── logo512.png │ ├── manifest.json │ └── robots.txt ├── src │ ├── App.test.tsx │ ├── App.tsx │ ├── index.tsx │ ├── react-app-env.d.ts │ ├── reportWebVitals.ts │ └── setupTests.ts └── tsconfig.json └── move ├── .gitignore ├── Move.toml └── sources └── todolist.move /README.md: -------------------------------------------------------------------------------- 1 | # todolist-dapp-tutorial 2 | 3 | This repo follows [Build an End-to-End Dapp on Aptos tutorial](https://aptos.dev/tutorials/build-e2e-dapp/e2e-dapp-index) 4 | -------------------------------------------------------------------------------- /client/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /client/README.md: -------------------------------------------------------------------------------- 1 | # Getting Started with Create React App 2 | 3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 4 | 5 | ## Available Scripts 6 | 7 | In the project directory, you can run: 8 | 9 | ### `npm start` 10 | 11 | Runs the app in the development mode.\ 12 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 13 | 14 | The page will reload if you make edits.\ 15 | You will also see any lint errors in the console. 16 | 17 | ### `npm test` 18 | 19 | Launches the test runner in the interactive watch mode.\ 20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 21 | 22 | ### `npm run build` 23 | 24 | Builds the app for production to the `build` folder.\ 25 | It correctly bundles React in production mode and optimizes the build for the best performance. 26 | 27 | The build is minified and the filenames include the hashes.\ 28 | Your app is ready to be deployed! 29 | 30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 31 | 32 | ### `npm run eject` 33 | 34 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 35 | 36 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 37 | 38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 39 | 40 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 41 | 42 | ## Learn More 43 | 44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 45 | 46 | To learn React, check out the [React documentation](https://reactjs.org/). 47 | -------------------------------------------------------------------------------- /client/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "client", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@aptos-labs/wallet-adapter-ant-design": "^0.1.0", 7 | "@aptos-labs/wallet-adapter-react": "^0.2.2", 8 | "@testing-library/jest-dom": "^5.16.5", 9 | "@testing-library/react": "^13.4.0", 10 | "@testing-library/user-event": "^13.5.0", 11 | "@types/jest": "^27.5.2", 12 | "@types/node": "^16.18.11", 13 | "@types/react": "^18.0.27", 14 | "@types/react-dom": "^18.0.10", 15 | "antd": "^5.1.4", 16 | "aptos": "^1.6.0", 17 | "petra-plugin-wallet-adapter": "^0.1.3", 18 | "react": "^18.2.0", 19 | "react-dom": "^18.2.0", 20 | "react-scripts": "5.0.1", 21 | "typescript": "^4.9.4", 22 | "web-vitals": "^2.1.4" 23 | }, 24 | "scripts": { 25 | "start": "react-scripts start", 26 | "build": "react-scripts build", 27 | "test": "react-scripts test", 28 | "eject": "react-scripts eject" 29 | }, 30 | "eslintConfig": { 31 | "extends": [ 32 | "react-app", 33 | "react-app/jest" 34 | ] 35 | }, 36 | "browserslist": { 37 | "production": [ 38 | ">0.2%", 39 | "not dead", 40 | "not op_mini all" 41 | ], 42 | "development": [ 43 | "last 1 chrome version", 44 | "last 1 firefox version", 45 | "last 1 safari version" 46 | ] 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /client/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aptos-labs/todolist-dapp-tutorial/a268ddddec3f9e0cf0f1a2c6f3e11f77c48198fa/client/public/favicon.ico -------------------------------------------------------------------------------- /client/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /client/public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aptos-labs/todolist-dapp-tutorial/a268ddddec3f9e0cf0f1a2c6f3e11f77c48198fa/client/public/logo192.png -------------------------------------------------------------------------------- /client/public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aptos-labs/todolist-dapp-tutorial/a268ddddec3f9e0cf0f1a2c6f3e11f77c48198fa/client/public/logo512.png -------------------------------------------------------------------------------- /client/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /client/public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /client/src/App.test.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render, screen } from '@testing-library/react'; 3 | import App from './App'; 4 | 5 | test('renders learn react link', () => { 6 | render(); 7 | const linkElement = screen.getByText(/learn react/i); 8 | expect(linkElement).toBeInTheDocument(); 9 | }); 10 | -------------------------------------------------------------------------------- /client/src/App.tsx: -------------------------------------------------------------------------------- 1 | import { WalletSelector } from "@aptos-labs/wallet-adapter-ant-design"; 2 | import { Layout, Row, Col, Button, Spin, List, Checkbox, Input } from "antd"; 3 | 4 | import React, { useEffect, useState } from "react"; 5 | import { useWallet } from "@aptos-labs/wallet-adapter-react"; 6 | 7 | import "@aptos-labs/wallet-adapter-ant-design/dist/index.css"; 8 | import { CheckboxChangeEvent } from "antd/es/checkbox"; 9 | import { AptosClient } from "aptos"; 10 | 11 | type Task = { 12 | address: string; 13 | completed: boolean; 14 | content: string; 15 | task_id: string; 16 | }; 17 | 18 | export const NODE_URL = "https://fullnode.devnet.aptoslabs.com"; 19 | export const client = new AptosClient(NODE_URL); 20 | // change this to be your module account address 21 | export const moduleAddress = 22 | "0xcbddf398841353776903dbab2fdaefc54f181d07e114ae818b1a67af28d1b018"; 23 | 24 | function App() { 25 | const [tasks, setTasks] = useState([]); 26 | const [newTask, setNewTask] = useState(""); 27 | const { account, signAndSubmitTransaction } = useWallet(); 28 | const [accountHasList, setAccountHasList] = useState(false); 29 | const [transactionInProgress, setTransactionInProgress] = 30 | useState(false); 31 | 32 | const onWriteTask = (event: React.ChangeEvent) => { 33 | const value = event.target.value; 34 | setNewTask(value); 35 | }; 36 | 37 | const fetchList = async () => { 38 | if (!account) return []; 39 | try { 40 | const todoListResource = await client.getAccountResource( 41 | account?.address, 42 | `${moduleAddress}::todolist::TodoList` 43 | ); 44 | setAccountHasList(true); 45 | // tasks table handle 46 | const tableHandle = (todoListResource as any).data.tasks.handle; 47 | // tasks table counter 48 | const taskCounter = (todoListResource as any).data.task_counter; 49 | 50 | let tasks = []; 51 | let counter = 1; 52 | while (counter <= taskCounter) { 53 | const tableItem = { 54 | key_type: "u64", 55 | value_type: `${moduleAddress}::todolist::Task`, 56 | key: `${counter}`, 57 | }; 58 | const task = await client.getTableItem(tableHandle, tableItem); 59 | tasks.push(task); 60 | counter++; 61 | } 62 | // set tasks in local state 63 | setTasks(tasks); 64 | } catch (e: any) { 65 | setAccountHasList(false); 66 | } 67 | }; 68 | 69 | const addNewList = async () => { 70 | if (!account) return []; 71 | setTransactionInProgress(true); 72 | // build a transaction payload to be submited 73 | const payload = { 74 | type: "entry_function_payload", 75 | function: `${moduleAddress}::todolist::create_list`, 76 | type_arguments: [], 77 | arguments: [], 78 | }; 79 | try { 80 | // sign and submit transaction to chain 81 | const response = await signAndSubmitTransaction(payload); 82 | // wait for transaction 83 | await client.waitForTransaction(response.hash); 84 | setAccountHasList(true); 85 | } catch (error: any) { 86 | setAccountHasList(false); 87 | } finally { 88 | setTransactionInProgress(false); 89 | } 90 | }; 91 | 92 | const onTaskAdded = async () => { 93 | // check for connected account 94 | if (!account) return; 95 | setTransactionInProgress(true); 96 | // build a transaction payload to be submited 97 | const payload = { 98 | type: "entry_function_payload", 99 | function: `${moduleAddress}::todolist::create_task`, 100 | type_arguments: [], 101 | arguments: [newTask], 102 | }; 103 | 104 | // hold the latest task.task_id from our local state 105 | const latestId = 106 | tasks.length > 0 ? parseInt(tasks[tasks.length - 1].task_id) + 1 : 1; 107 | 108 | // build a newTaskToPush objct into our local state 109 | const newTaskToPush = { 110 | address: account.address, 111 | completed: false, 112 | content: newTask, 113 | task_id: latestId + "", 114 | }; 115 | 116 | try { 117 | // sign and submit transaction to chain 118 | const response = await signAndSubmitTransaction(payload); 119 | // wait for transaction 120 | await client.waitForTransaction(response.hash); 121 | 122 | // Create a new array based on current state: 123 | let newTasks = [...tasks]; 124 | 125 | // Add item to the tasks array 126 | newTasks.push(newTaskToPush); 127 | // Set state 128 | setTasks(newTasks); 129 | // clear input text 130 | setNewTask(""); 131 | } catch (error: any) { 132 | console.log("error", error); 133 | } finally { 134 | setTransactionInProgress(false); 135 | } 136 | }; 137 | 138 | const onCheckboxChange = async ( 139 | event: CheckboxChangeEvent, 140 | taskId: string 141 | ) => { 142 | if (!account) return; 143 | if (!event.target.checked) return; 144 | setTransactionInProgress(true); 145 | const payload = { 146 | type: "entry_function_payload", 147 | function: `${moduleAddress}::todolist::complete_task`, 148 | type_arguments: [], 149 | arguments: [taskId], 150 | }; 151 | 152 | try { 153 | // sign and submit transaction to chain 154 | const response = await signAndSubmitTransaction(payload); 155 | // wait for transaction 156 | await client.waitForTransaction(response.hash); 157 | 158 | setTasks((prevState) => { 159 | const newState = prevState.map((obj) => { 160 | // if task_id equals the checked taskId, update completed property 161 | if (obj.task_id === taskId) { 162 | return { ...obj, completed: true }; 163 | } 164 | 165 | // otherwise return object as is 166 | return obj; 167 | }); 168 | 169 | return newState; 170 | }); 171 | } catch (error: any) { 172 | console.log("error", error); 173 | } finally { 174 | setTransactionInProgress(false); 175 | } 176 | }; 177 | 178 | useEffect(() => { 179 | fetchList(); 180 | }, [account?.address]); 181 | 182 | return ( 183 | <> 184 | 185 | 186 | 187 |

Our todolist

188 | 189 | 190 | 191 | 192 |
193 |
194 | 195 | {!accountHasList ? ( 196 | 197 | 198 | 207 | 208 | 209 | ) : ( 210 | 211 | 212 | 213 | onWriteTask(event)} 215 | style={{ width: "calc(100% - 60px)" }} 216 | placeholder="Add a Task" 217 | size="large" 218 | value={newTask} 219 | /> 220 | 227 | 228 | 229 | 230 | {tasks && ( 231 | ( 236 | 239 | {task.completed ? ( 240 | 241 | ) : ( 242 | 244 | onCheckboxChange(event, task.task_id) 245 | } 246 | /> 247 | )} 248 | , 249 | ]} 250 | > 251 | {`${task.address.slice(0, 6)}...${task.address.slice( 258 | -5 259 | )}`} 260 | } 261 | /> 262 | 263 | )} 264 | /> 265 | )} 266 | 267 | 268 | )} 269 | 270 | 271 | ); 272 | } 273 | 274 | export default App; 275 | -------------------------------------------------------------------------------- /client/src/index.tsx: -------------------------------------------------------------------------------- 1 | import { PetraWallet } from "petra-plugin-wallet-adapter"; 2 | import { AptosWalletAdapterProvider } from "@aptos-labs/wallet-adapter-react"; 3 | import React from "react"; 4 | import ReactDOM from "react-dom/client"; 5 | import App from "./App"; 6 | import reportWebVitals from "./reportWebVitals"; 7 | 8 | const wallets = [new PetraWallet()]; 9 | 10 | const root = ReactDOM.createRoot( 11 | document.getElementById("root") as HTMLElement 12 | ); 13 | root.render( 14 | 15 | 16 | 17 | 18 | 19 | ); 20 | 21 | // If you want to start measuring performance in your app, pass a function 22 | // to log results (for example: reportWebVitals(console.log)) 23 | // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals 24 | reportWebVitals(); 25 | -------------------------------------------------------------------------------- /client/src/react-app-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /client/src/reportWebVitals.ts: -------------------------------------------------------------------------------- 1 | import { ReportHandler } from 'web-vitals'; 2 | 3 | const reportWebVitals = (onPerfEntry?: ReportHandler) => { 4 | if (onPerfEntry && onPerfEntry instanceof Function) { 5 | import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => { 6 | getCLS(onPerfEntry); 7 | getFID(onPerfEntry); 8 | getFCP(onPerfEntry); 9 | getLCP(onPerfEntry); 10 | getTTFB(onPerfEntry); 11 | }); 12 | } 13 | }; 14 | 15 | export default reportWebVitals; 16 | -------------------------------------------------------------------------------- /client/src/setupTests.ts: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom'; 6 | -------------------------------------------------------------------------------- /client/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "dom", 6 | "dom.iterable", 7 | "esnext" 8 | ], 9 | "allowJs": true, 10 | "skipLibCheck": true, 11 | "esModuleInterop": true, 12 | "allowSyntheticDefaultImports": true, 13 | "strict": true, 14 | "forceConsistentCasingInFileNames": true, 15 | "noFallthroughCasesInSwitch": true, 16 | "module": "esnext", 17 | "moduleResolution": "node", 18 | "resolveJsonModule": true, 19 | "isolatedModules": true, 20 | "noEmit": true, 21 | "jsx": "react-jsx" 22 | }, 23 | "include": [ 24 | "src" 25 | ] 26 | } 27 | -------------------------------------------------------------------------------- /move/.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | .aptos -------------------------------------------------------------------------------- /move/Move.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = 'my_todo_list' 3 | version = '1.0.0' 4 | 5 | [dependencies.AptosFramework] 6 | git = 'https://github.com/aptos-labs/aptos-core.git' 7 | rev = 'devnet' 8 | subdir = 'aptos-move/framework/aptos-framework' 9 | 10 | [addresses] 11 | todolist_addr='_' 12 | -------------------------------------------------------------------------------- /move/sources/todolist.move: -------------------------------------------------------------------------------- 1 | module todolist_addr::todolist { 2 | 3 | use aptos_framework::account; 4 | use std::signer; 5 | use aptos_framework::event; 6 | use std::string::String; 7 | use aptos_std::table::{Self, Table}; 8 | #[test_only] 9 | use std::string; 10 | 11 | // Errors 12 | const E_NOT_INITIALIZED: u64 = 1; 13 | const ETASK_DOESNT_EXIST: u64 = 2; 14 | const ETASK_IS_COMPLETED: u64 = 3; 15 | 16 | struct TodoList has key { 17 | tasks: Table, 18 | set_task_event: event::EventHandle, 19 | task_counter: u64 20 | } 21 | 22 | struct Task has store, drop, copy { 23 | task_id: u64, 24 | address:address, 25 | content: String, 26 | completed: bool, 27 | } 28 | 29 | public entry fun create_list(account: &signer){ 30 | let todo_list = TodoList { 31 | tasks: table::new(), 32 | set_task_event: account::new_event_handle(account), 33 | task_counter: 0 34 | }; 35 | // move the TodoList resource under the signer account 36 | move_to(account, todo_list); 37 | } 38 | 39 | public entry fun create_task(account: &signer, content: String) acquires TodoList { 40 | // gets the signer address 41 | let signer_address = signer::address_of(account); 42 | // assert signer has created a list 43 | assert!(exists(signer_address), E_NOT_INITIALIZED); 44 | // gets the TodoList resource 45 | let todo_list = borrow_global_mut(signer_address); 46 | // increment task counter 47 | let counter = todo_list.task_counter + 1; 48 | // creates a new Task 49 | let new_task = Task { 50 | task_id: counter, 51 | address: signer_address, 52 | content, 53 | completed: false 54 | }; 55 | // adds the new task into the tasks table 56 | table::upsert(&mut todo_list.tasks, counter, new_task); 57 | // sets the task counter to be the incremented counter 58 | todo_list.task_counter = counter; 59 | // fires a new task created event 60 | event::emit_event( 61 | &mut borrow_global_mut(signer_address).set_task_event, 62 | new_task, 63 | ); 64 | } 65 | 66 | public entry fun complete_task(account: &signer, task_id: u64) acquires TodoList { 67 | // gets the signer address 68 | let signer_address = signer::address_of(account); 69 | // assert signer has created a list 70 | assert!(exists(signer_address), E_NOT_INITIALIZED); 71 | // gets the TodoList resource 72 | let todo_list = borrow_global_mut(signer_address); 73 | // assert task exists 74 | assert!(table::contains(&todo_list.tasks, task_id), ETASK_DOESNT_EXIST); 75 | // gets the task matched the task_id 76 | let task_record = table::borrow_mut(&mut todo_list.tasks, task_id); 77 | // assert task is not completed 78 | assert!(task_record.completed == false, ETASK_IS_COMPLETED); 79 | // update task as completed 80 | task_record.completed = true; 81 | } 82 | 83 | #[test(admin = @0x123)] 84 | public entry fun test_flow(admin: signer) acquires TodoList { 85 | // creates an admin @todolist_addr account for test 86 | account::create_account_for_test(signer::address_of(&admin)); 87 | // initialize contract with admin account 88 | create_list(&admin); 89 | 90 | // creates a task by the admin account 91 | create_task(&admin, string::utf8(b"New Task")); 92 | let task_count = event::counter(&borrow_global(signer::address_of(&admin)).set_task_event); 93 | assert!(task_count == 1, 4); 94 | let todo_list = borrow_global(signer::address_of(&admin)); 95 | assert!(todo_list.task_counter == 1, 5); 96 | let task_record = table::borrow(&todo_list.tasks, todo_list.task_counter); 97 | assert!(task_record.task_id == 1, 6); 98 | assert!(task_record.completed == false, 7); 99 | assert!(task_record.content == string::utf8(b"New Task"), 8); 100 | assert!(task_record.address == signer::address_of(&admin), 9); 101 | 102 | // updates task as completed 103 | complete_task(&admin, 1); 104 | let todo_list = borrow_global(signer::address_of(&admin)); 105 | let task_record = table::borrow(&todo_list.tasks, 1); 106 | assert!(task_record.task_id == 1, 10); 107 | assert!(task_record.completed == true, 11); 108 | assert!(task_record.content == string::utf8(b"New Task"), 12); 109 | assert!(task_record.address == signer::address_of(&admin), 13); 110 | } 111 | 112 | #[test(admin = @0x123)] 113 | #[expected_failure(abort_code = E_NOT_INITIALIZED)] 114 | public entry fun account_can_not_update_task(admin: signer) acquires TodoList { 115 | // creates an admin @todolist_addr account for test 116 | account::create_account_for_test(signer::address_of(&admin)); 117 | // account can not toggle task as no list was created 118 | complete_task(&admin, 2); 119 | } 120 | 121 | } 122 | --------------------------------------------------------------------------------