Skip to content

Commit 4879000

Browse files
CHENkai8385alex4506
authored andcommitted
100243175 dapp
1 parent d21341e commit 4879000

20 files changed

+18984
-0
lines changed

100243175/backend/Move.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "my_todo_list"
3+
version = "1.0.0"
4+
authors = []
5+
6+
[addresses]
7+
todolist_addr = "0x0a442ebb9976a0c795ca9684e020fed4e52463bd8beed607b7f29d085822c8ce"
8+
9+
[dev-addresses]
10+
11+
[dependencies.AptosFramework]
12+
git = "https://github.com/aptos-labs/aptos-core.git"
13+
rev = "mainnet"
14+
subdir = "aptos-move/framework/aptos-framework"
15+
16+
[dev-dependencies]

100243175/backend/Readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1. 一个帐户创建一个新列表。
2+
2. 一个帐户在其列表中创建一个新任务。
3+
- 每当有人创建一个新任务时,发出一个task_created事件。
4+
3. 让帐户将其任务标记为已完成。
+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
module todolist_addr::todolist {
2+
3+
use std::string::{String};
4+
#[test_only]
5+
use std::string; // add this
6+
use aptos_framework::event;
7+
use aptos_std::table::{Self,Table};
8+
use aptos_framework::account;
9+
use aptos_framework::signer;
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<u64, Task>,
18+
set_task_event: event::EventHandle<Task>,
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 tasks_holder = TodoList {
31+
tasks: table::new(),
32+
set_task_event: account::new_event_handle<Task>(account),
33+
task_counter: 0
34+
};
35+
// move the TodoList resource under the signer account
36+
move_to(account, tasks_holder);
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+
43+
// assert signer has created a list
44+
assert!(exists<TodoList>(signer_address), E_NOT_INITIALIZED);
45+
46+
// gets the TodoList resource
47+
let todo_list = borrow_global_mut<TodoList>(signer_address);
48+
// increment task counter
49+
let counter = todo_list.task_counter + 1;
50+
// creates a new Task
51+
let new_task = Task {
52+
task_id: counter,
53+
address: signer_address,
54+
content,
55+
completed: false
56+
};
57+
// adds the new task into the tasks table
58+
table::upsert(&mut todo_list.tasks, counter, new_task);
59+
// sets the task counter to be the incremented counter
60+
todo_list.task_counter = counter;
61+
// fires a new task created event
62+
event::emit_event<Task>(
63+
&mut borrow_global_mut<TodoList>(signer_address).set_task_event,
64+
new_task,
65+
);
66+
}
67+
68+
public entry fun complete_task(account: &signer, task_id: u64) acquires TodoList {
69+
// gets the signer address
70+
let signer_address = signer::address_of(account);
71+
// assert signer has created a list
72+
assert!(exists<TodoList>(signer_address), E_NOT_INITIALIZED);
73+
// gets the TodoList resource
74+
let todo_list = borrow_global_mut<TodoList>(signer_address);
75+
// assert task exists
76+
assert!(table::contains(&todo_list.tasks, task_id), ETASK_DOESNT_EXIST);
77+
// gets the task matched the task_id
78+
let task_record = table::borrow_mut(&mut todo_list.tasks, task_id);
79+
// assert task is not completed
80+
assert!(task_record.completed == false, ETASK_IS_COMPLETED);
81+
// update task as completed
82+
task_record.completed = true;
83+
}
84+
85+
#[test(admin = @0x123)]
86+
public entry fun test_flow(admin: signer) acquires TodoList {
87+
// create a list
88+
// create a task
89+
// update task as completed
90+
91+
// creates an admin @todolist_addr account for test
92+
account::create_account_for_test(signer::address_of(&admin));
93+
// initialize contract with admin account
94+
create_list(&admin);
95+
96+
// creates a task by the admin account
97+
create_task(&admin, string::utf8(b"New Task"));
98+
let task_count = event::counter(&borrow_global<TodoList>(signer::address_of(&admin)).set_task_event);
99+
assert!(task_count == 1, 4);
100+
let todo_list = borrow_global<TodoList>(signer::address_of(&admin));
101+
assert!(todo_list.task_counter == 1, 5);
102+
let task_record = table::borrow(&todo_list.tasks, todo_list.task_counter);
103+
assert!(task_record.task_id == 1, 6);
104+
assert!(task_record.completed == false, 7);
105+
assert!(task_record.content == string::utf8(b"New Task"), 8);
106+
assert!(task_record.address == signer::address_of(&admin), 9);
107+
108+
// updates task as completed
109+
complete_task(&admin, 1);
110+
let todo_list = borrow_global<TodoList>(signer::address_of(&admin));
111+
let task_record = table::borrow(&todo_list.tasks, 1);
112+
assert!(task_record.task_id == 1, 10);
113+
assert!(task_record.completed == true, 11);
114+
assert!(task_record.content == string::utf8(b"New Task"), 12);
115+
assert!(task_record.address == signer::address_of(&admin), 13);
116+
}
117+
118+
#[test(admin = @0x123)]
119+
#[expected_failure(abort_code = E_NOT_INITIALIZED)]
120+
public entry fun account_can_not_update_task(admin: signer) acquires TodoList {
121+
// creates an admin @todolist_addr account for test
122+
account::create_account_for_test(signer::address_of(&admin));
123+
// account can not toggle task as no list was created
124+
complete_task(&admin, 2);
125+
}
126+
}

100243175/dapp/.gitignore

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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*

100243175/dapp/README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
You should first install the dependencies:
8+
9+
### `npm install`
10+
11+
In the project directory, you can run:
12+
13+
### `npm start`
14+
15+
Runs the app in the development mode.\
16+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
17+
18+
The page will reload if you make edits.\
19+
You will also see any lint errors in the console.
20+
21+
### `npm test`
22+
23+
Launches the test runner in the interactive watch mode.\
24+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
25+
26+
### `npm run build`
27+
28+
Builds the app for production to the `build` folder.\
29+
It correctly bundles React in production mode and optimizes the build for the best performance.
30+
31+
The build is minified and the filenames include the hashes.\
32+
Your app is ready to be deployed!
33+
34+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
35+
36+
### `npm run eject`
37+
38+
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
39+
40+
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.
41+
42+
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.
43+
44+
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.
45+
46+
## Learn More
47+
48+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
49+
50+
To learn React, check out the [React documentation](https://reactjs.org/).

0 commit comments

Comments
 (0)