Single Contract Crate Setup
You can find the code for this example in the cw-orch counter-contract folder.
If you are a fast or visual learner, you can find a Before-After view of the cw-orch
integration process in the sample contract.
Dependency
Before we can create an interface we need to add cw-orch to the contract’s Cargo.toml
file. In counter
run:
$ cargo add cw-orch
> Adding cw-orch v0.16.0 to dependencies.
or add it manually to the counter/Cargo.toml
file:
[dependencies]
cw-orch = {version = "0.21.2" } # Latest version at time of writing
Even if you include cw-orch
in your dependencies here, it won’t be included in your wasm
contract. Learn more about this behavior in the section about Wasm Compilation.
Crate Structure
Now that we have our dependency set up, we can create the files that will contain our interface. We will create that interface in an interface.rs
file inside the crate (counter/src/interface.rs
). Then, we need to include that file inside our project. However, that file will contain multiple cw-orch
elements that are not exposed when compiling your WASM. In order to prevent errors when compiling for Wasm, we will import our interface.rs
file and target-flag it like so, in counter/src/lib.rs
:
#[cfg(not(target_arch = "wasm32"))]
mod interface;
In the next section, we will learn how to define the interface to be able to interact with an actual contract inside this interface.rs
file.
Final structure
Following the steps above, the directory structure should eventually look like this:
.
├── Cargo.toml
├── artifacts
│ └── counter_contract.wasm (binary file)
└── counter
├── Cargo.toml
├── bin
│ └── deploy.rs
└── src
├── contract.rs (execute, instantiate, query, ...)
├── msg.rs (ExecuteMsg, QueryMsg, ...)
├── interface.rs (`cw-orch` contract structure definition)
└── ..
NOTE:
The artifacts folder is generated by the rust-optimizer, and contains all your compiled contracts
Place your scripts inside the
bin
directory. Those can be run using the following command:cargo run --bin deploy
Now that the setup is complete, you can go back to the Contracts page to create the interface for your contract and start scripting/testing your contracts withe ease.