-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add todo list contract + tests
- Loading branch information
hieu.ha
committed
Jan 13, 2025
1 parent
f8d906f
commit e4f2142
Showing
3 changed files
with
83 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,54 @@ | ||
/// Interface representing `HelloContract`. | ||
/// This interface allows modification and retrieval of the contract balance. | ||
#[derive(Drop, Serde, Copy)] | ||
pub struct Todo { | ||
pub title: felt252, | ||
pub done: bool, | ||
} | ||
|
||
#[starknet::interface] | ||
pub trait IHelloStarknet<TContractState> { | ||
/// Increase contract balance. | ||
fn increase_balance(ref self: TContractState, amount: felt252); | ||
/// Retrieve contract balance. | ||
fn get_balance(self: @TContractState) -> felt252; | ||
pub trait ITodoList<TContractState> { | ||
fn add_todo(ref self: TContractState, title: felt252); | ||
fn set_todo_done(ref self: TContractState, index: u64); | ||
fn get_todos(self: @TContractState) -> Array<Todo>; | ||
} | ||
|
||
/// Simple contract for managing balance. | ||
#[starknet::contract] | ||
mod HelloStarknet { | ||
use core::starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess}; | ||
mod TodoList { | ||
use core::starknet::storage::{ | ||
StoragePointerWriteAccess, StoragePointerReadAccess, VecTrait, MutableVecTrait, Vec, | ||
}; | ||
use super::Todo; | ||
|
||
#[storage] | ||
struct Storage { | ||
balance: felt252, | ||
todos_title: Vec<felt252>, | ||
todos_done: Vec<bool>, | ||
} | ||
|
||
#[abi(embed_v0)] | ||
impl HelloStarknetImpl of super::IHelloStarknet<ContractState> { | ||
fn increase_balance(ref self: ContractState, amount: felt252) { | ||
assert(amount != 0, 'Amount cannot be 0'); | ||
self.balance.write(self.balance.read() + amount); | ||
impl TodoListImpl of super::ITodoList<ContractState> { | ||
fn add_todo(ref self: ContractState, title: felt252) { | ||
assert(title != '', 'Title cannot be empty'); | ||
|
||
self.todos_title.append().write(title); | ||
self.todos_done.append().write(false); | ||
} | ||
|
||
fn set_todo_done(ref self: ContractState, index: u64) { | ||
// assert(index < self.todos_title.len(), 'Index out of bounds'); | ||
|
||
self.todos_done.at(index).write(true); | ||
} | ||
|
||
fn get_balance(self: @ContractState) -> felt252 { | ||
self.balance.read() | ||
fn get_todos(self: @ContractState) -> Array<Todo> { | ||
let mut result = ArrayTrait::new(); | ||
|
||
for i in 0..self.todos_title.len() { | ||
let title = self.todos_title.at(i).read(); | ||
let done = self.todos_done.at(i).read(); | ||
result.append(Todo { title, done }); | ||
}; | ||
|
||
result | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters