Skip to content

Commit 75d2ebc

Browse files
committed
Add client for hello contract (#196)
- Update video for hello
1 parent f285f64 commit 75d2ebc

File tree

4 files changed

+67
-13
lines changed

4 files changed

+67
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Furthermore, there is also [wiki site](https://github.com/second-state/SewUp/wik
1414
## Slides & Demo
1515
| Date | Event | Slides / Demo video |
1616
|------------|-----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
17-
| 2021/09/07 | Version 0.1 release | [KV](https://youtu.be/LUpYIFGG36s) [RDB](https://youtu.be/sJLOcJRheIw) [ERC-20](https://youtu.be/sVGEuNBY1dc) [ERC-721](https://youtu.be/ivZIqnhOAfA) [ERC-1155](https://youtu.be/BsbAFT5rNGw) |
17+
| 2021/09/07 | Version 0.1 release | [Hello](https://youtu.be/kbe3uuxkBNQ), [KV](https://youtu.be/LUpYIFGG36s), [RDB](https://youtu.be/sJLOcJRheIw), [ERC-20](https://youtu.be/sVGEuNBY1dc), [ERC-721](https://youtu.be/ivZIqnhOAfA), [ERC-1155](https://youtu.be/BsbAFT5rNGw) |
1818
| 2021/06/22 | Rust online meetup | [v0.0.2](https://slides.com/yanganto/ethereum-wasm-in-rust) |
1919
| 2021/06/19 | Rust meetup (Beijing) | [v0.0.1-pre](https://slides.com/yanganto/sewup) |
2020

examples/hello-contract/Cargo.toml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,26 @@ description = "The example contract using sewup default feature"
77

88
[lib]
99
path = "src/lib.rs"
10-
crate-type = ["cdylib"]
10+
crate-type = ["cdylib", "lib"]
11+
12+
[[bin]]
13+
name = "hello-client"
14+
path = "src/client.rs"
15+
required-features = ["client"]
1116

1217
[dependencies]
1318
sewup ={ version = "*", path = "../../sewup" }
1419
sewup-derive = { version = "*", path = "../../sewup-derive" }
1520
anyhow = "1.0.40"
1621

22+
cargo-sewup = { version = "*", path = "../../cargo-sewup", optional = true }
23+
web3 = { version = "0.16.0", optional = true }
24+
tokio = { version = "1.0", features = ["full"], optional = true }
25+
secp256k1 = { version = "0.20.3", optional = true }
26+
reqwest = { version = "0.11.4", features = ["json"], optional = true }
27+
serde_json = { version = "1.0.66", optional = true }
28+
bincode = { version = "1.3", optional = true }
29+
1730
[profile.release]
1831
incremental = false
1932
panic = "abort"
@@ -32,3 +45,4 @@ address = "0xXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
3245
[features]
3346
constructor = []
3447
constructor-test = []
48+
client = [ "web3", "tokio", "secp256k1", "reqwest", "serde_json", "bincode", "cargo-sewup"]

examples/hello-contract/src/client.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// A example client to interact with kv-contract
2+
use std::str::FromStr;
3+
4+
use anyhow::Result;
5+
use cargo_sewup::config::{get_deploy_config, Deploy};
6+
use sewup_derive::ewasm_input;
7+
use tokio;
8+
use web3::{
9+
types::{Address, CallRequest},
10+
Web3,
11+
};
12+
13+
use hello_contract::*;
14+
15+
#[tokio::main]
16+
async fn main() -> Result<()> {
17+
// NOTE: modify the contract addr after you deploy the contract
18+
let contract_addr = "0xc2f4023e7c181e4419da30eaaa01816afb23be08";
19+
20+
let Deploy { url, address, .. } = get_deploy_config().await?;
21+
22+
let transport = web3::transports::Http::new(&url)?;
23+
let web3 = Web3::new(transport);
24+
25+
let input = ewasm_input!(None for hello);
26+
27+
let call_req = CallRequest {
28+
from: Some(Address::from_str(&address)?),
29+
data: Some(input.into()),
30+
to: Some(Address::from_str(&contract_addr)?),
31+
..Default::default()
32+
};
33+
34+
let resp = web3.eth().call(call_req, None).await?;
35+
println!("resp from contract: {}", std::str::from_utf8(&resp.0)?);
36+
Ok(())
37+
}

sewup-derive/src/lib.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -518,17 +518,20 @@ pub fn ewasm_input(item: TokenStream) -> TokenStream {
518518
if let Some(cap) = re.captures(&item.to_string()) {
519519
let sig = cap.name("sig").unwrap().as_str();
520520
let instance = cap.name("instance").unwrap().as_str();
521-
format!(
522-
"{{
523-
let mut input = {}.to_vec();
524-
input.append(&mut bincode::serialize(&{}).unwrap());
525-
input
526-
}}",
527-
write_function_signature(sig),
528-
instance
529-
)
530-
.parse()
531-
.unwrap()
521+
let output = if instance == "None" {
522+
format!("{}.to_vec()", write_function_signature(sig),)
523+
} else {
524+
format!(
525+
"{{
526+
let mut input = {}.to_vec();
527+
input.append(&mut bincode::serialize(&{}).unwrap());
528+
input
529+
}}",
530+
write_function_signature(sig),
531+
instance
532+
)
533+
};
534+
output.parse().unwrap()
532535
} else {
533536
panic!("ewasm_input input incorrect")
534537
}

0 commit comments

Comments
 (0)