Skip to content

Commit 10bf1e7

Browse files
Add basic workspace example (stellar#281)
1 parent d2b3015 commit 10bf1e7

File tree

10 files changed

+1683
-0
lines changed

10 files changed

+1683
-0
lines changed

Diff for: workspace/Cargo.lock

+1,511
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: workspace/Cargo.toml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[workspace]
2+
resolver = "2"
3+
4+
members = [
5+
"contract_a_interface",
6+
"contract_a",
7+
"contract_b",
8+
]
9+
10+
[workspace.package]
11+
version = "0.0.0"
12+
13+
[workspace.dependencies]
14+
soroban-sdk = { version = "0.9.2" }
15+
soroban-workspace-contract-a-interface = { path = "contract_a_interface" }
16+
soroban-workspace-contract-a = { path = "contract_a" }
17+
18+
[profile.release]
19+
opt-level = "z"
20+
overflow-checks = true
21+
debug = 0
22+
strip = "symbols"
23+
debug-assertions = false
24+
panic = "abort"
25+
codegen-units = 1
26+
lto = true
27+
28+
[profile.release-with-logs]
29+
inherits = "release"
30+
debug-assertions = true

Diff for: workspace/Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
default: build
2+
3+
all: test
4+
5+
test:
6+
cargo test
7+
8+
build:
9+
soroban contract build
10+
@ls -l target/wasm32-unknown-unknown/release/*.wasm
11+
12+
fmt:
13+
cargo fmt --all
14+
15+
clean:
16+
cargo clean

Diff for: workspace/contract_a/Cargo.toml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "soroban-workspace-contract-a"
3+
version.workspace = true
4+
authors = ["Stellar Development Foundation <[email protected]>"]
5+
license = "Apache-2.0"
6+
edition = "2021"
7+
publish = false
8+
9+
[lib]
10+
crate-type = ["cdylib", "rlib"]
11+
doctest = false
12+
13+
[features]
14+
testutils = []
15+
16+
[dependencies]
17+
soroban-sdk = { workspace = true }
18+
soroban-workspace-contract-a-interface = { workspace = true }
19+
20+
[dev_dependencies]
21+
soroban-sdk = { workspace = true, features = ["testutils"] }
22+
soroban-workspace-contract-a-interface = { workspace = true, features = ["testutils"] }

Diff for: workspace/contract_a/src/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![no_std]
2+
3+
use soroban_sdk::{contract, contractimpl};
4+
use soroban_workspace_contract_a_interface::ContractAInterface;
5+
6+
#[contract]
7+
pub struct ContractA;
8+
9+
#[contractimpl]
10+
impl ContractAInterface for ContractA {
11+
fn add(x: u32, y: u32) -> u32 {
12+
x.checked_add(y).expect("no overflow")
13+
}
14+
}

Diff for: workspace/contract_a_interface/Cargo.toml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "soroban-workspace-contract-a-interface"
3+
version.workspace = true
4+
authors = ["Stellar Development Foundation <[email protected]>"]
5+
license = "Apache-2.0"
6+
edition = "2021"
7+
publish = false
8+
9+
[lib]
10+
crate-type = ["rlib"]
11+
doctest = false
12+
13+
[features]
14+
testutils = []
15+
16+
[dependencies]
17+
soroban-sdk = { workspace = true }
18+
19+
[dev_dependencies]
20+
soroban-sdk = { workspace = true, features = ["testutils"] }

Diff for: workspace/contract_a_interface/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![no_std]
2+
3+
use soroban_sdk::contractclient;
4+
5+
#[contractclient(name = "ContractAClient")]
6+
pub trait ContractAInterface {
7+
fn add(x: u32, y: u32) -> u32;
8+
}

Diff for: workspace/contract_b/Cargo.toml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "soroban-workspace-contract-b"
3+
version.workspace = true
4+
authors = ["Stellar Development Foundation <[email protected]>"]
5+
license = "Apache-2.0"
6+
edition = "2021"
7+
publish = false
8+
9+
[lib]
10+
crate-type = ["cdylib"]
11+
doctest = false
12+
13+
[dependencies]
14+
soroban-sdk = { workspace = true }
15+
soroban-workspace-contract-a-interface = { workspace = true }
16+
17+
[dev_dependencies]
18+
soroban-sdk = { workspace = true, features = ["testutils"] }
19+
soroban-workspace-contract-a-interface = { workspace = true, features = ["testutils"] }
20+
soroban-workspace-contract-a = { workspace = true, features = ["testutils"] }

Diff for: workspace/contract_b/src/lib.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![no_std]
2+
3+
use soroban_sdk::{contract, contractimpl, Address, Env};
4+
use soroban_workspace_contract_a_interface::ContractAClient;
5+
6+
#[contract]
7+
pub struct ContractB;
8+
9+
#[contractimpl]
10+
impl ContractB {
11+
pub fn add_with(env: Env, contract: Address, x: u32, y: u32) -> u32 {
12+
let client = ContractAClient::new(&env, &contract);
13+
client.add(&x, &y)
14+
}
15+
}
16+
17+
mod test;

Diff for: workspace/contract_b/src/test.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#![cfg(test)]
2+
3+
use soroban_sdk::Env;
4+
5+
use crate::{ContractB, ContractBClient};
6+
7+
use soroban_workspace_contract_a::ContractA;
8+
9+
#[test]
10+
fn test() {
11+
let env = Env::default();
12+
13+
// Register contract A using the native contract imported.
14+
let contract_a_id = env.register_contract(None, ContractA);
15+
16+
// Register contract B defined in this crate.
17+
let contract_b_id = env.register_contract(None, ContractB);
18+
19+
// Create a client for calling contract B.
20+
let client = ContractBClient::new(&env, &contract_b_id);
21+
22+
// Invoke contract B via its client. Contract B will invoke contract A.
23+
let sum = client.add_with(&contract_a_id, &5, &7);
24+
assert_eq!(sum, 12);
25+
}

0 commit comments

Comments
 (0)