ToDo APP
with SewUp
Hackathon
Intro
SewUp
- Rust crate
- Open source at Github
- Utilize Ethereum WebAssembly
- Contract Dev as Backend (CDAB)
- Rusty api
- Learn more with previous slides
RDB Feature
The storage operation on blockchain just like operation on Relational Database
- Define storage with struct
- Provide CRUD handlers
Let's SewUp
ToDo App
Rules
- User can create a todo task for him self
- User can query all todo tasks for him self
- User can mark one of todo task as completed
- User can delete one of completed todo task
Init Project
- Install Rust
curl --proto '=https' --tlsv1.2 -sSf \ https://sh.rustup.rs | sh
-
setup rust defualt nightly-2021-11-27 rustup default nightly-2021-11-27
- Install cargo sewup
cargo install cargo-sewup
- Initialize project
cargo sewup init --mode=auto
- Add rdb feature
features = [ "rdb" ]
How to debug
- Use cargo test to validate you code
- If _compile_runtime_test fail,
run cargo build --target=wasm32-unknown-unknown - If _compile_constructor_test fail,
run cargo build --features=constructor --target=wasm32-unknown-unknown
Tips for development
Setup Data Structure
- Provide the struct like schema to store
- use #[Table] derive
- use todotask, ToDoTask, TODOTASK
All lower case
(CRUD handlers)
All upper case (Functional sig)
Setup Constructor
- Constructor run once when contract deploy
- Create Table from schema
- ${lower_case}::Protocol, ${lower_case}::Wrapper generated automatically
Wrapper
#[Table] Instance { field: usize }
InstanceWrapper { id: usize, field: Option<usize> }
add id filed
Help the struct become a record
Protocol
#[Table] Instance { field: usize }
instance::Protocol { select_fields: Option<HashSet<String>>, filter: bool, records: Vec<Wrapper> }
use for communicate with handler
Table, Wrapper, & Protocol
Handler
Table on Chain
Protocol: return multiple records
Wrapper: a unique record for storage
Setup Basic CRUD
- Sewup already generate CRUD handler
- Just reuse it
Protocol for single object
let p = instance::protocol(i);
#[Table] Instance { field: usize }
Transfer object to protocol
let i = Instance { field: 1}
let mut expected = p .clone()
expected.set_id(1);
Transfer object to protocol
Add API
- Get caller address
- Update address to the input instance
Rule 1 implementation
Query API
- Get caller address
- Filter the result with caller
Rule 2 implementation
Complete API
- Get caller address
- Get the task of the caller
- update task complete flag
Rule 3 implementation
Delete API
- Get caller address
- Get the task of the caller
- Check the task completed
- Delete the task
Rule 4 implementation
Well Done
Hack with SewUp
Database Backup Icons (PSD & PNG) from tw.365psd.com
It's Your Turn
Refactor Update Handler
- Currently, anyone can use update API to update any todotask
-
The expected behavior is that only the owner can modify the todotask.
Summary
Blockchain bring TRUST to the world
Rust is the stem of TRUST
SewUp base on Rust and Ewasm helps you build great blockchain applications
Thanks for listening
Star the project if you like it
There are also help want issues and good first issues
Interested in VM?
WasmEdge is welcom to contribute
Q&A
Sewup sew your ethereum project up to the world
Writing Ethereum WebAssembly in Rust IV - ToDo App with SewUp
By Antonio Yang
Writing Ethereum WebAssembly in Rust IV - ToDo App with SewUp
Secondstate EWasm Utility Program, a library to help you to write Ethereum WebAssmbly in Rust
- 1,061