|
| 1 | +--- |
| 2 | +title: Workspaces |
| 3 | +hide_table_of_contents: true |
| 4 | +description: Organize contracts using Cargo workspaces |
| 5 | +--- |
| 6 | + |
| 7 | +# Workspace |
| 8 | + |
| 9 | +Using Cargo's workspace [feature](https://doc.rust-lang.org/book/ch14-03-cargo-workspaces.html) makes it very convenient to organize your smart contracts in subdirectories of your project's root. |
| 10 | + |
| 11 | +It's very simple to get started using the cli: |
| 12 | + |
| 13 | +``` |
| 14 | +stellar contract init soroban-project --name add_contract |
| 15 | +``` |
| 16 | + |
| 17 | +Running this command will create a root project directory (`soroban-project`) and then initialize a project workspace with a single contract named `add_contract`. |
| 18 | + |
| 19 | +Adding one more contract template to the project can be done using the same command: |
| 20 | + |
| 21 | +``` |
| 22 | +stellar contract init soroban-project --name main_contract |
| 23 | +``` |
| 24 | + |
| 25 | +The project tree in the `soroban-project` directory will look like this: |
| 26 | + |
| 27 | +``` |
| 28 | +. |
| 29 | +├── Cargo.toml |
| 30 | +├── contracts |
| 31 | +│ ├── add-contract |
| 32 | +│ │ ├── Cargo.toml |
| 33 | +│ │ ├── Makefile |
| 34 | +│ │ └── src |
| 35 | +│ │ ├── lib.rs |
| 36 | +│ │ └── test.rs |
| 37 | +│ └── main-contract |
| 38 | +│ ├── Cargo.toml |
| 39 | +│ ├── Makefile |
| 40 | +│ └── src |
| 41 | +│ ├── lib.rs |
| 42 | +│ └── test.rs |
| 43 | +└── README.md |
| 44 | +``` |
| 45 | + |
| 46 | +Running `stellar contract init` command created a new contract, located in `./contracts`, each containing: |
| 47 | + |
| 48 | +- Cargo.toml file with the `soroban-sdk` dependency |
| 49 | +- `src` directory with an example hello-world contract and a test. |
| 50 | + |
| 51 | +Build the contracts with the following command (don't forget to change working directory to `soroban-project` first before running it), and check the build directory `target/wasm32-unknown-unknown/release/` for the compiled `.wasm` files. |
| 52 | + |
| 53 | +``` |
| 54 | +stellar contract build |
| 55 | +``` |
| 56 | + |
| 57 | +### Integrating Contracts in the Same Workspace |
| 58 | + |
| 59 | +With the given project structure, cross-contract calls can be easily made. Starting with modifying the sample hello world contract into an add contract: |
| 60 | + |
| 61 | +```rust title="contracts/add_contract/src/lib.rs" |
| 62 | +#![no_std] |
| 63 | +use soroban_sdk::{contract, contractimpl}; |
| 64 | + |
| 65 | +#[contract] |
| 66 | +pub struct ContractAdd; |
| 67 | + |
| 68 | +#[contractimpl] |
| 69 | +impl ContractAdd { |
| 70 | + pub fn add(x: u32, y: u32) -> u32 { |
| 71 | + x.checked_add(y).expect("no overflow") |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +:::tip |
| 77 | + |
| 78 | +In this tutorial we use workspaces to import contract client. However, it's also possible to use contract's compiled code instead (for example, if you don't have a source code for it). See [making cross-contract calls](/docs/build/guides/conventions/cross-contract.mdx) guide for more info |
| 79 | + |
| 80 | +::: |
| 81 | + |
| 82 | +Next, in order to call `ContractAdd` from another contract, it's necessary to add a workspace dependency: |
| 83 | + |
| 84 | +```toml title="./contracts/main_contract/Cargo.toml" |
| 85 | +# <...> |
| 86 | +[dependencies] |
| 87 | +soroban-sdk = { workspace = true } |
| 88 | +add_contract = { path = "../add_contract" } |
| 89 | +# <...> |
| 90 | +``` |
| 91 | + |
| 92 | +The `ContractAdd` can now be referenced and used from another contracts using `ContractAddClient`: |
| 93 | + |
| 94 | +```rust title="contracts/main_contract/src/lib.rs" |
| 95 | +#![no_std] |
| 96 | +use add_contract::ContractAddClient; |
| 97 | +use soroban_sdk::{contract, contractimpl, Address, Env}; |
| 98 | + |
| 99 | +#[contract] |
| 100 | +pub struct ContractMain; |
| 101 | + |
| 102 | +#[contractimpl] |
| 103 | +impl ContractMain { |
| 104 | + pub fn add_with(env: Env, contract: Address, x: u32, y: u32) -> u32 { |
| 105 | + let client = ContractAddClient::new(&env, &contract); |
| 106 | + client.add(&x, &y) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +mod test; |
| 111 | +``` |
| 112 | + |
| 113 | +Here, main contract will invoke `ContractAdd`'s `add` function to calculate the sum of 2 numbers. It's a good idea to update tests for our main contract as well: |
| 114 | + |
| 115 | +```rust title="contracts/main_contract/src/test.rs" |
| 116 | +#![cfg(test)] |
| 117 | + |
| 118 | +use crate::{ContractMain, ContractMainClient}; |
| 119 | +use soroban_sdk::Env; |
| 120 | +use add_contract::ContractAdd; |
| 121 | + |
| 122 | +#[test] |
| 123 | +fn test_adding_cross_contract() { |
| 124 | + let env = Env::default(); |
| 125 | + |
| 126 | + // Register add contract using the imported contract. |
| 127 | + let contract_add_id = env.register(ContractAdd, ()); |
| 128 | + |
| 129 | + // Register main contract defined in this crate. |
| 130 | + let contract_main_id = env.register(ContractMain, ()); |
| 131 | + |
| 132 | + // Create a client for calling main contract. |
| 133 | + let client = ContractMainClient::new(&env, &contract_main_id); |
| 134 | + |
| 135 | + // Invoke main contract via its client. Main contract will invoke add contract. |
| 136 | + let sum = client.add_with(&contract_add_id, &5, &7); |
| 137 | + assert_eq!(sum, 12); |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +Contracts can now be re-complied running the following command from the project root: |
| 142 | + |
| 143 | +``` |
| 144 | +stellar contract build |
| 145 | +``` |
| 146 | + |
| 147 | +And check that the test is working correctly, running tests in `contracts/main_contract`: |
| 148 | + |
| 149 | +``` |
| 150 | +cargo test |
| 151 | +
|
| 152 | +running 1 test |
| 153 | +test test::test_adding_cross_contract ... ok |
| 154 | +
|
| 155 | +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s |
| 156 | +``` |
| 157 | + |
| 158 | +Finally, let's deploy this contracts and call our main contract using cli. If you haven't already, set up an account (alice) first |
| 159 | + |
| 160 | +```bash |
| 161 | +stellar keys generate alice --fund --network testnet |
| 162 | +stellar keys use alice |
| 163 | +``` |
| 164 | + |
| 165 | +Second is to deploy the contracts: |
| 166 | + |
| 167 | +```bash |
| 168 | +stellar contract deploy --network testnet --wasm target/wasm32-unknown-unknown/release/add_contract.wasm --alias add_contract |
| 169 | +stellar contract deploy --network testnet --wasm target/wasm32-unknown-unknown/release/main_contract.wasm --alias main_contract |
| 170 | +``` |
| 171 | + |
| 172 | +And finally call the main contract: |
| 173 | + |
| 174 | +``` |
| 175 | +$ stellar contract invoke --id main_contract --network testnet -- add_with --contract add_contract --x 9 --y 10 |
| 176 | +ℹ️ Send skipped because simulation identified as read-only. Send by rerunning with `--send=yes`. |
| 177 | +19 |
| 178 | +``` |
| 179 | + |
| 180 | +### Adding contract interfaces |
| 181 | + |
| 182 | +As the next step, we can abstract away add contract and allow it to have multiple implementations. Main contract will in turn use the contract interface that is not bound to its implementation. |
| 183 | + |
| 184 | +``` |
| 185 | +stellar contract init . --name adder_interface |
| 186 | +stellar contract init . --name add_extra_contract |
| 187 | +``` |
| 188 | + |
| 189 | +First, let's create an interface and change our existing implementation to use this interface: |
| 190 | + |
| 191 | +```rust title="contracts/adder_interface/src/lib.rs" |
| 192 | +#![no_std] |
| 193 | + |
| 194 | +use soroban_sdk::contractclient; |
| 195 | + |
| 196 | +#[contractclient(name = "ContractAClient")] |
| 197 | +pub trait ContractAInterface { |
| 198 | + fn add(x: u32, y: u32) -> u32; |
| 199 | +} |
| 200 | +``` |
| 201 | + |
| 202 | +To use the interface definition our workspace members will now have an `adder_interface` as a dependency: |
| 203 | + |
| 204 | +```toml title="./Cargo.toml" |
| 205 | +# <...> |
| 206 | +[workspace.dependencies] |
| 207 | +soroban-sdk = "21.0.0" |
| 208 | +adder-interface = { path = "contracts/adder_interface" } |
| 209 | +# <...> |
| 210 | +``` |
| 211 | + |
| 212 | +```toml title="./contracts/add_contract/Cargo.toml" |
| 213 | +# <...> |
| 214 | +[dependencies] |
| 215 | +soroban-sdk = { workspace = true } |
| 216 | +adder-interface = {workspace = true} |
| 217 | +# <...> |
| 218 | +``` |
| 219 | + |
| 220 | +```toml title="./contracts/main_contract/Cargo.toml" |
| 221 | +# <...> |
| 222 | +[dependencies] |
| 223 | +soroban-sdk = { workspace = true } |
| 224 | +adder-interface = {workspace = true} |
| 225 | +add_contract = { path = "../add_contract" } |
| 226 | +# <...> |
| 227 | +``` |
| 228 | + |
| 229 | +```toml title="./contracts/add_extra_contract/Cargo.toml" |
| 230 | +# <...> |
| 231 | +[dependencies] |
| 232 | +soroban-sdk = { workspace = true } |
| 233 | +adder-interface = {workspace = true} |
| 234 | +# <...> |
| 235 | +``` |
| 236 | + |
| 237 | +And change lib type of `adder_interface` crate: |
| 238 | + |
| 239 | +```toml title="./contracts/adder_interface/Cargo.toml" |
| 240 | +# <...> |
| 241 | +[lib] |
| 242 | +crate-type = ["rlib"] |
| 243 | +# <...> |
| 244 | +``` |
| 245 | + |
| 246 | +```rust title="./contracts/adder_interface/src/lib.rs" |
| 247 | +#![no_std] |
| 248 | + |
| 249 | +use soroban_sdk::contractclient; |
| 250 | + |
| 251 | +#[contractclient(name = "AdderClient")] |
| 252 | +pub trait Adder { |
| 253 | + fn add(x: u32, y: u32) -> u32; |
| 254 | +} |
| 255 | + |
| 256 | +``` |
| 257 | + |
| 258 | +```rust title="contracts/add_contract/src/lib.rs" |
| 259 | +#![no_std] |
| 260 | +use soroban_sdk::{contract, contractimpl}; |
| 261 | +use adder_interface::Adder; |
| 262 | + |
| 263 | +#[contract] |
| 264 | +pub struct ContractAdd; |
| 265 | + |
| 266 | +#[contractimpl] |
| 267 | +impl Adder for ContractAdd { |
| 268 | + fn add(x: u32, y: u32) -> u32 { |
| 269 | + x.checked_add(y).expect("no overflow") |
| 270 | + } |
| 271 | +} |
| 272 | + |
| 273 | +``` |
| 274 | + |
| 275 | +```rust title="contracts/main_contract/src/lib.rs" |
| 276 | +#![no_std] |
| 277 | + |
| 278 | +use soroban_sdk::{contract, contractimpl, Address, Env}; |
| 279 | +use adder_interface::AdderClient; |
| 280 | + |
| 281 | +#[contract] |
| 282 | +pub struct ContractMain; |
| 283 | + |
| 284 | +#[contractimpl] |
| 285 | +impl ContractMain { |
| 286 | + pub fn add_with(env: Env, contract: Address, x: u32, y: u32) -> u32 { |
| 287 | + let client = AdderClient::new(&env, &contract); |
| 288 | + client.add(&x, &y) |
| 289 | + } |
| 290 | +} |
| 291 | + |
| 292 | +mod test; |
| 293 | +``` |
| 294 | + |
| 295 | +As the final step we can create an alternative `Adder` implementation that adds an extra 1: |
| 296 | + |
| 297 | +```rust title="contracts/add_extra_contract/src/lib.rs" |
| 298 | +#![no_std] |
| 299 | +use soroban_sdk::{contract, contractimpl}; |
| 300 | +use adder_interface::Adder; |
| 301 | + |
| 302 | +#[contract] |
| 303 | +pub struct ContractAdd; |
| 304 | + |
| 305 | +#[contractimpl] |
| 306 | +impl Adder for ContractAdd { |
| 307 | + fn add(x: u32, y: u32) -> u32 { |
| 308 | + x.checked_add(y).expect("no overflow").checked_add(1).expect("no overflow") |
| 309 | + } |
| 310 | +} |
| 311 | +``` |
| 312 | + |
| 313 | +We can now deploy this contracts and test the new behavior: |
| 314 | + |
| 315 | +```bash |
| 316 | +stellar contract build |
| 317 | +stellar contract deploy --network testnet --wasm target/wasm32-unknown-unknown/release/add_contract.wasm --alias add_contract |
| 318 | +stellar contract deploy --network testnet --wasm target/wasm32-unknown-unknown/release/add_extra_contract.wasm --alias wrong_math_contract |
| 319 | +stellar contract deploy --network testnet --wasm target/wasm32-unknown-unknown/release/main_contract.wasm --alias main_contract |
| 320 | +``` |
| 321 | + |
| 322 | +Now let's try to do sum 2 unsigned integers causing an overflow: |
| 323 | + |
| 324 | +``` |
| 325 | +$ stellar contract invoke --id main_contract --network testnet -- add_with --contract add_contract --x 2 --y 2 |
| 326 | +ℹ️ Send skipped because simulation identified as read-only. Send by rerunning with `--send=yes`. |
| 327 | +4 |
| 328 | +$ stellar contract invoke --id main_contract --network testnet -- add_with --contract wrong_math_contract --x 2 --y 2 |
| 329 | +ℹ️ Send skipped because simulation identified as read-only. Send by rerunning with `--send=yes`. |
| 330 | +5 |
| 331 | +``` |
0 commit comments