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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#[macro_export]
macro_rules! deploy_wasm {
    () => {
        "./target/wasm32-unknown-unknown/release/{}.deploy.wasm"
    };
}

pub const DEFAULT_GAS: usize = 500_000_000;
pub const DEFAULT_GAS_PRICE: usize = 1;

#[macro_export]
macro_rules! default_cargo_template {
    () => {
        r#"
[package]
name = "{}"
version = "0.1.0"
edition = "2018"

[lib]
path = "src/lib.rs"
crate-type = ["cdylib"]

# See the following examples
# https://github.com/second-state/SewUp/tree/main/examples/hello-contract
# https://github.com/second-state/SewUp/tree/main/examples/default-contract

[dependencies]
sewup = "*"
sewup-derive = "*"
anyhow = "*"
# thiserror = "*"

[profile.release]
incremental = false
panic = "abort"
lto = true
opt-level = "z"

[profile.release.package.{}]
incremental = false
opt-level = "z"

[features]
constructor = []
constructor-test = []"#
    };
}

pub const DEFAULT_CONTRACT: &str = r#"
use sewup_derive::{ewasm_constructor, ewasm_fn, ewasm_fn_sig, ewasm_main, ewasm_test};

#[ewasm_constructor]
fn constructor() {}

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

#[ewasm_main]
fn main() -> anyhow::Result<()> {
    let contract = sewup::primitives::Contract::new()?;
    match contract.get_function_selector()? {
        ewasm_fn_sig!(hello) => hello()?,
        _ => panic!("unknown handle"),
    };
    Ok(())
}

#[ewasm_test]
mod tests {
    use super::*;
    use sewup_derive::{ewasm_assert_eq, ewasm_assert_ok};

    #[ewasm_test]
    fn test_get_greeting() {
        // The default mode does not use anything successful return from Rust
        // The only thing is return ok or not
        ewasm_assert_eq!(hello(), vec![]);
        ewasm_assert_ok!(hello());
    }
}"#;

#[macro_export]
macro_rules! rusty_cargo_template {
    () => {
        r#"
[package]
name = "{}"
version = "0.1.0"
edition = "2018"

[lib]
path = "src/lib.rs"
crate-type = ["cdylib"]

# See the following examples
# https://github.com/second-state/SewUp/tree/main/examples/rusty-contract

[dependencies]
sewup = "*"
sewup-derive = "*"

[profile.release]
incremental = false
panic = "abort"
lto = true
opt-level = "z"

[profile.release.package.{}]
incremental = false
opt-level = "z"

[features]
constructor = []
constructor-test = []"#
    };
}

pub const RUSTY_CONTRACT: &str = r#"
use sewup_derive::{ewasm_constructor, ewasm_fn, ewasm_fn_sig, ewasm_main, ewasm_test};

#[ewasm_constructor]
fn constructor() {}

#[ewasm_fn]
fn handler() -> Result<(), &'static str> {
    Ok(())
}

#[ewasm_main(rusty)]
fn main() -> Result<(), &'static str> {
    use sewup::primitives::Contract;
    use sewup_derive::ewasm_input_from;

    let contract = Contract::new().map_err(|_| "NewContractError")?;
    match contract
        .get_function_selector()
        .map_err(|_| "FailGetFnSelector")?
    {
        ewasm_fn_sig!(handler) => handler()?,
        _ => return Err("UnknownHandle"),
    };

    Ok(())
}

#[ewasm_test]
mod tests {
    use super::*;
    use sewup::primitives::Contract;
    use sewup_derive::{ewasm_assert_eq, ewasm_rusty_assert_ok, ewasm_rusty_err_output};

    #[ewasm_test]
    fn test_handler_ok() {
        ewasm_rusty_assert_ok!(handler());
    }
}"#;

pub const AUTO_CONTRACT: &str = r#"
use sewup_derive::{ewasm_constructor, ewasm_fn, ewasm_fn_sig, ewasm_main, ewasm_test};

#[ewasm_constructor]
fn constructor() {}

#[ewasm_fn]
fn handler() -> anyhow::Result<sewup::primitives::EwasmAny> {
    Ok(().into())
}


#[ewasm_main(auto)]
fn main() -> anyhow::Result<sewup::primitives::EwasmAny> {
    use sewup_derive::ewasm_input_from;
    let contract = sewup::primitives::Contract::new()?;

    match contract.get_function_selector()? {
        ewasm_fn_sig!(handler) => handler(),
        _ => return Err(anyhow::anyhow!("Unknow Error")),
    }
}

#[ewasm_test]
mod tests {
    use super::*;
    use sewup_derive::{ewasm_assert_eq, ewasm_assert_ok, ewasm_auto_assert_eq, ewasm_err_output};

    #[ewasm_test]
    fn test_handler() {
        ewasm_auto_assert_eq!(handler(), ());
    }
}"#;