Rust Engineer

Antonio

yanganto@Github

2021/12/03 Hackthon @OnAir

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

  1. User can create a todo task for him self
  2. User can query all todo tasks for him self
  3. User can mark one of todo task as completed
  4. User can delete one of completed todo task

Init Project

  1. Install Rust
    curl --proto '=https' --tlsv1.2 -sSf \ https://sh.rustup.rs | sh
  2. setup rust defualt nightly-2021-11-27
    rustup default nightly-2021-11-27
  3. Install cargo sewup
    cargo install cargo-sewup
  4. Initialize project
    cargo sewup init --mode=auto
  5. Add rdb feature
    features = [ "rdb" ]

Check out Commit b02bfb8

How to debug

  1. Use cargo test to validate you code
  2. If _compile_runtime_test fail,
    run cargo build --target=wasm32-unknown-unknown
  3. If _compile_constructor_test fail,
    run cargo build --features=constructor --target=wasm32-unknown-unknown

Tips for development

Setup Data Structure

Check out Commit 564b82a

  1. Provide the struct like schema to store
  2. use #[Table] derive
  3. use todotask, ToDoTask, TODOTASK

All lower case

(CRUD handlers)

All upper case (Functional sig)

Setup Constructor

Check out Commit e9442cf

  1. Constructor run once when contract deploy
  2. Create Table from schema
  3. ${lower_case}::Protocol, ${lower_case}::Wrapper generated automatically

Wrapper

Check out Commit e9442cf

#[Table]
Instance {
   field: usize
}
InstanceWrapper {
   id: usize,
   field: Option<usize>
}

add id filed

Help the struct become a record

Protocol

Check out Commit e9442cf

#[Table]
Instance {
   field: usize
}
instance::Protocol {
   select_fields: Option<HashSet<String>>,
   filter: bool,
   records: Vec<Wrapper>
}

use for communicate with handler

Table, Wrapper, & Protocol

Check out Commit e9442cf

Handler

Table on Chain

Protocol: return multiple records

Wrapper: a unique record for storage

Setup Basic CRUD

Check out Commit a34ca72

  1. Sewup already generate CRUD handler
  2. 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

Check out Commit 930d595

  1. Get caller address
  2. Update address to the input instance

Rule 1 implementation

Query API

Check out Commit 594b4b7

  1. Get caller address
  2. Filter the result with caller

Rule 2 implementation

Complete API

Check out Commit a1d30fc

  1. Get caller address
  2. Get the task of the caller
  3. update task complete flag

Rule 3 implementation

Delete API

Check out Commit cf092d0

  1. Get caller address
  2. Get the task of the caller
  3. Check the task completed
  4. Delete the task

Rule 4 implementation

Well Done

Hack with SewUp

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