Skip to content

Commit

Permalink
Refactor and fix warning in examples (#90)
Browse files Browse the repository at this point in the history
Refactor
- fix warning in examples
- fix test on ascii tables in doc string
  • Loading branch information
yanganto authored Jul 9, 2021
1 parent 6cd051e commit 55ef787
Show file tree
Hide file tree
Showing 26 changed files with 165 additions and 157 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
nix_path: nixpkgs=channel:nixos-21.05-small

- name: Build Documentation
run: nix-shell --run "cargo doc -p sewup -p sewup-derive --features=kv"
run: nix-shell --run "cargo doc -p sewup -p sewup-derive --features=kv,rdb"

- name: Deploy Documentation
env:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ crate-type = ["cdylib"]

[dependencies]
sewup = { version = "*", features = ['kv'] }
sewup-derive = { version = "*" }
sewup-derive = { version = "*", features = ['kv'] }

anyhow = "1.0"

Expand Down
4 changes: 2 additions & 2 deletions examples/default-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ path = "src/lib.rs"
crate-type = ["cdylib"]

[dependencies]
sewup ={ version = "0.0.4", path = "../../sewup" }
sewup-derive = { version = "0.0.4", path = "../../sewup-derive" }
sewup ={ version = "*", path = "../../sewup" }
sewup-derive = { version = "*", path = "../../sewup-derive" }
anyhow = "1.0.40"
thiserror = "1.0.24"
serde = "1.0"
Expand Down
1 change: 1 addition & 0 deletions examples/default-contract/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use thiserror::Error;

#[allow(dead_code)]
#[derive(Error, Debug, PartialEq)]
pub enum Error {
#[error("not trust input")]
Expand Down
12 changes: 6 additions & 6 deletions examples/default-contract/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use anyhow::Result;
use serde_derive::{Deserialize, Serialize};

use sewup::primitives::Contract;
use sewup_derive::{ewasm_fn, ewasm_fn_sig, ewasm_input_from, ewasm_main, ewasm_test};
use sewup_derive::{ewasm_fn, ewasm_fn_sig, ewasm_main, ewasm_test};

mod errors;
use errors::Error;
Expand All @@ -14,15 +11,18 @@ struct SimpleStruct {
}

#[ewasm_fn]
fn check_input_object(s: SimpleStruct) -> Result<()> {
fn check_input_object(s: SimpleStruct) -> anyhow::Result<()> {
if !s.trust {
return Err(Error::NotTrustedInput.into());
}
Ok(())
}

#[ewasm_main]
fn main() -> Result<()> {
fn main() -> anyhow::Result<()> {
use sewup::primitives::Contract;
use sewup_derive::ewasm_input_from;

let contract = Contract::new()?;
match contract.get_function_selector()? {
ewasm_fn_sig!(check_input_object) => ewasm_input_from!(contract, check_input_object)?,
Expand Down
4 changes: 2 additions & 2 deletions examples/erc20-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ path = "src/token.rs"
crate-type = ["cdylib"]

[dependencies]
sewup ={ version = "0.0.4", path = "../../sewup", features = [ "token" ] }
sewup-derive = { version = "0.0.4", path = "../../sewup-derive" }
sewup ={ version = "*", path = "../../sewup", features = [ "token" ] }
sewup-derive = { version = "*", path = "../../sewup-derive" }
anyhow = "1.0.40"
ewasm_api = { version = "0.11.0", default-features = false, features = ["std", "qimalloc"], package = "ss_ewasm_api" }

Expand Down
74 changes: 31 additions & 43 deletions examples/erc20-contract/src/token.rs
Original file line number Diff line number Diff line change
@@ -1,113 +1,100 @@
use anyhow::Result;
use sewup::primitives::Contract;
use sewup::token::{
erc20::{
allowance as erc20_allowance, approve as erc20_approve, decimals,
do_balance as erc20_do_balance, do_transfer as erc20_do_transfer, mint as erc20_mint, name,
symbol, total_supply, transfer_from as erc20_transfer_from, DECIMALS_SIG, NAME_SIG,
SYMBOL_SIG, TOTAL_SUPPLY_SIG,
},
helpers::{copy_into_address, copy_into_array, copy_into_storage_value},
};
use sewup_derive::{ewasm_fn, ewasm_fn_sig, ewasm_main, ewasm_test};

#[cfg(target_arch = "wasm32")]
use ewasm_api::types::*;

#[ewasm_fn]
fn do_balance(contract: &Contract) {
fn do_balance(contract: &sewup::primitives::Contract) {
if contract.data_size != 24 {
ewasm_api::revert();
}
let address_data = contract.input_data[4..].to_vec();
let address = copy_into_address(&address_data[0..20]);

erc20_do_balance(address);
let address = sewup::token::helpers::copy_into_address(&address_data[0..20]);
sewup::token::erc20::do_balance(address);
}

#[ewasm_fn]
fn do_transfer(contract: &Contract) {
fn do_transfer(contract: &sewup::primitives::Contract) {
if contract.input_data.len() != 32 {
ewasm_api::revert();
}

let recipient_data = contract.input_data[4..24].to_vec();
let recipient = copy_into_address(&recipient_data[0..20]);
let recipient = sewup::token::helpers::copy_into_address(&recipient_data[0..20]);

let value_data: [u8; 8] = copy_into_array(&contract.input_data[24..]);
let value_data: [u8; 8] = sewup::token::helpers::copy_into_array(&contract.input_data[24..]);
let mut value = StorageValue::default();
let value_len = value_data.len();
let start = 32 - value_len;

value.bytes[start..(value_len + start)]
.clone_from_slice(&value_data[..((value_len + start) - start)]);

erc20_do_transfer(recipient, value);
sewup::token::erc20::do_transfer(recipient, value);
}

#[ewasm_fn]
fn approve(contract: &Contract) {
fn approve(contract: &sewup::primitives::Contract) {
let spender_data = contract.input_data[4..24].to_vec();
let spender = copy_into_address(&spender_data[0..20]);
let spender = sewup::token::helpers::copy_into_address(&spender_data[0..20]);

let value = contract.input_data[24..32].to_vec();
let storage_value = copy_into_storage_value(&value[0..8]);

erc20_approve(spender, storage_value);
let storage_value = sewup::token::helpers::copy_into_storage_value(&value[0..8]);
sewup::token::erc20::approve(spender, storage_value);
}

#[ewasm_fn]
fn allowance(contract: &Contract) {
fn allowance(contract: &sewup::primitives::Contract) {
if contract.data_size != 44 {
ewasm_api::revert();
}

let from_data = contract.input_data[4..24].to_vec();
let from = copy_into_address(&from_data[0..20]);
let from = sewup::token::helpers::copy_into_address(&from_data[0..20]);

let spender_data = contract.input_data[24..44].to_vec();
let spender = copy_into_address(&spender_data[0..20]);
let spender = sewup::token::helpers::copy_into_address(&spender_data[0..20]);

erc20_allowance(from, spender);
sewup::token::erc20::allowance(from, spender);
}

#[ewasm_fn]
fn transfer_from(contract: &Contract) {
fn transfer_from(contract: &sewup::primitives::Contract) {
if contract.data_size != 52 {
ewasm_api::revert();
}

let owner = copy_into_address(&contract.input_data[4..24]);
let owner = sewup::token::helpers::copy_into_address(&contract.input_data[4..24]);

let recipient = copy_into_address(&contract.input_data[24..44]);
let recipient = sewup::token::helpers::copy_into_address(&contract.input_data[24..44]);

let value_data: [u8; 8] = copy_into_array(&contract.input_data[44..52]);
let value_data: [u8; 8] = sewup::token::helpers::copy_into_array(&contract.input_data[44..52]);

let value = u64::from_be_bytes(value_data);

erc20_transfer_from(owner, recipient, value);
sewup::token::erc20::transfer_from(owner, recipient, value);
}

#[ewasm_fn]
fn mint(contract: &Contract) {
let adddress = copy_into_address(&contract.input_data[4..24]);
fn mint(contract: &sewup::primitives::Contract) {
let adddress = sewup::token::helpers::copy_into_address(&contract.input_data[4..24]);

let value_data: [u8; 8] = copy_into_array(&contract.input_data[24..32]);
let value_data: [u8; 8] = sewup::token::helpers::copy_into_array(&contract.input_data[24..32]);
let value = u64::from_be_bytes(value_data);

erc20_mint(adddress, value);
sewup::token::erc20::mint(adddress, value);
}

#[ewasm_main]
fn main() -> Result<()> {
let contract = Contract::new()?;
fn main() -> anyhow::Result<()> {
let contract = sewup::primitives::Contract::new()?;
match contract.get_function_selector()? {
ewasm_fn_sig!(do_balance) => do_balance(&contract),
ewasm_fn_sig!(do_transfer) => do_transfer(&contract),
NAME_SIG => name(),
SYMBOL_SIG => symbol("ETD"),
DECIMALS_SIG => decimals(),
TOTAL_SUPPLY_SIG => total_supply(),
sewup::token::erc20::NAME_SIG => sewup::token::erc20::name(),
sewup::token::erc20::SYMBOL_SIG => sewup::token::erc20::symbol("ETD"),
sewup::token::erc20::DECIMALS_SIG => sewup::token::erc20::decimals(),
sewup::token::erc20::TOTAL_SUPPLY_SIG => sewup::token::erc20::total_supply(),
ewasm_fn_sig!(approve) => approve(&contract),
ewasm_fn_sig!(allowance) => allowance(&contract),
ewasm_fn_sig!(transfer_from) => transfer_from(&contract),
Expand All @@ -121,6 +108,7 @@ fn main() -> Result<()> {
mod tests {
use super::*;
use hex_literal::hex;
use sewup::erc20::{DECIMALS_SIG, NAME_SIG, SYMBOL_SIG, TOTAL_SUPPLY_SIG};
use sewup_derive::ewasm_assert_eq;

#[ewasm_test]
Expand Down
4 changes: 2 additions & 2 deletions examples/hello-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ path = "src/lib.rs"
crate-type = ["cdylib"]

[dependencies]
sewup ={ version = "0.0.4", path = "../../sewup" }
sewup-derive = { version = "0.0.4", path = "../../sewup-derive" }
sewup ={ version = "*", path = "../../sewup" }
sewup-derive = { version = "*", path = "../../sewup-derive" }
anyhow = "1.0.40"

[profile.release]
Expand Down
9 changes: 3 additions & 6 deletions examples/hello-contract/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use anyhow::Result;

use sewup::primitives::Contract;
use sewup_derive::{ewasm_fn, ewasm_fn_sig, ewasm_main, ewasm_test};

#[ewasm_fn]
fn hello() -> Result<String> {
fn hello() -> anyhow::Result<String> {
Ok("hello world".to_string())
}

#[ewasm_main(auto)]
fn main() -> Result<String> {
let contract = Contract::new()?;
fn main() -> anyhow::Result<String> {
let contract = sewup::primitives::Contract::new()?;
let greeting = match contract.get_function_selector()? {
ewasm_fn_sig!(hello) => hello()?,
_ => panic!("unknown handle"),
Expand Down
4 changes: 2 additions & 2 deletions examples/kv-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ path = "src/lib.rs"
crate-type = ["cdylib"]

[dependencies]
sewup ={ version = "0.0.4", path = "../../sewup", features = [ "kv" ] }
sewup-derive = { version = "0.0.4", path = "../../sewup-derive", features = [ "kv" ] }
sewup ={ version = "*", path = "../../sewup", features = [ "kv" ] }
sewup-derive = { version = "*", path = "../../sewup-derive", features = [ "kv" ] }
anyhow = "1.0.40"
thiserror = "1.0.24"
serde = "1.0"
Expand Down
1 change: 1 addition & 0 deletions examples/kv-contract/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use sewup::kv::Feature;
use thiserror::Error;

#[allow(dead_code)]
#[derive(Error, Debug, PartialEq)]
pub enum KVError {
#[error("the DB version `{0}` is unexpected.")]
Expand Down
Loading

0 comments on commit 55ef787

Please sign in to comment.