forked from Calyptus-Learn/anchor-todo-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
110 lines (84 loc) · 2.93 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use anchor_lang::prelude::*;
declare_id!("<PLACE YOUR ADDRESS HERE>");
#[program]
pub mod todo_list_app {
use super::*;
pub fn adding_task(ctx: Context<AddingTask>, text: String) -> Result<()> {
let task = &mut ctx.accounts.task;
let author = &ctx.accounts.author; // The `author` account
let clock = Clock::get().unwrap(); // Getting the current timestamp
if text.chars().count() > 400 {
return Err(ErrorCode::TextTooLong.into());
}
task.author = *author.key;
task.is_done = false;
task.created_at = clock.unix_timestamp;
task.updated_at = clock.unix_timestamp;
task.text = text;
Ok(())
}
pub fn updating_task(ctx: Context<UpdatingTask>, is_done: bool) -> Result<()> {
let task = &mut ctx.accounts.task;
let author = &ctx.accounts.author; // The `author` account
let clock = Clock::get().unwrap(); // Getting the current timestamp
task.author = *author.key;
task.is_done = is_done;
task.updated_at = clock.unix_timestamp;
Ok(())
}
pub fn deleting_task(ctx: Context<DeletingTask>) -> Result<()> {
let task = &mut ctx.accounts.task;
let author = &ctx.accounts.author; // The `author` account
let clock = Clock::get().unwrap(); // Getting the current timestamp
task.author = *author.key;
task.is_done = true;
task.updated_at = clock.unix_timestamp;
Ok(())
}
}
#[derive(Accounts)]
pub struct AddingTask<'info> {
#[account(init, payer = author, space = Task::LEN)]
pub task: Account<'info, Task>,
#[account(mut)]
pub author: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct UpdatingTask<'info> {
#[account(mut, has_one = author)]
pub task: Account<'info, Task>,
pub author: Signer<'info>,
}
#[derive(Accounts)]
pub struct DeletingTask<'info> {
#[account(mut, has_one = author)]
pub task: Account<'info, Task>,
pub author: Signer<'info>,
}
#[account]
pub struct Task {
pub author: Pubkey, // The account that owns the task
pub is_done: bool, // Whether the task is done or not
pub text: String, // The text of the task
pub created_at: i64, // The timestamp when the task was created
pub updated_at: i64, // The timestamp when the task was last updated
}
const DISCRIMINATOR: usize = 8;
const PUBLIC_KEY_LENGTH: usize = 32;
const BOOL_LENGTH: usize = 1;
const TEXT_LENGTH: usize = 4 + 400 * 4; // 400 chars
const TIMESTAMP_LENGTH: usize = 8;
impl Task {
const LEN: usize = DISCRIMINATOR + // discriminator
PUBLIC_KEY_LENGTH + // author
BOOL_LENGTH + // is_done
TEXT_LENGTH + // text
TIMESTAMP_LENGTH + // created_at
TIMESTAMP_LENGTH; // updated_at
}
#[error_code]
pub enum ErrorCode {
#[msg("The text is too long")]
TextTooLong,
}