elko/examples/
addition.rs1use crate::examples::Example;
2pub const ADDITION: Example = Example {
4 lib_rs: r#"
5//! ${name}
6#![no_std]
7#[cfg(not(test))]
8extern crate zink;
9
10#[cfg(all(test, not(target_arch = "wasm32")))]
11extern crate alloc;
12
13/// Adds two numbers together.
14#[no_mangle]
15pub extern "C" fn addition(x: u64, y: u64) -> u64 {
16 x + y
17}
18
19#[cfg(not(target_arch = "wasm32"))]
20fn main() {}
21
22#[cfg(test)]
23mod tests {
24 use zint::Contract;
25 use alloc::vec;
26
27 #[test]
28 fn test_addition() {
29 // Assumes `elko build` has run and produced target/zink/addition.wasm
30 let contract = Contract::search("addition").expect("Failed to find addition contract");
31 let mut contract = contract
32 .pure()
33 .compile()
34 .expect("Failed to compile contract");
35 let inputs = vec![
36 2u64.to_le_bytes().to_vec(),
37 3u64.to_le_bytes().to_vec(),
38 ];
39 let info = contract.execute(&inputs).expect("Failed to execute addition");
40 let result = info.ret;
41 let mut expected = [0u8; 32];
42 let expected_bytes = 5u64.to_le_bytes();
43 expected[24..32].copy_from_slice(&expected_bytes);
44 assert_eq!(result, expected, "addition(2, 3) should return 5");
45 }
46}
47"#,
48 readme: r#"
49# ${name}
50
51> An EVM contract written in Rust with [The Zink Project][zink].
52
53## Getting Started
54
55```
56cargo install zinkup
57elko build
58ls target/zink/${name}.bin
59```
60
61[zink]: https://github.com/zink-lang/zink
62"#,
63};