This site requires Javascript to be enabled.

Compiling a contract

To deploy your smart contracts to the Archway blockchain, you need to build an optimized version of your contract, which will be generated as a .wasm executable.

For building and optimizing your contracts, we use the Rust Optimizer. This tool allows for compiling and optimizing multiple contracts within a workspace project, a feature that the Archway Developer CLI utilizes. This process is handled via Docker, making Docker a required dependency.

Prerequisites

Before proceeding, please refer to the Installation of Development Tools guide to install the necessary dependencies. Specifically, ensure that Docker is installed and running.

Compiling with the Archway Developer CLI

The Archway Developer CLI has a unique project structure with specific requirements for the CLI to function correctly. If you created a smart contract project using the Archway Developer CLI, for example, if you went through the Project Setup guide, you can compile all contracts in your project by executing the following command in the root of your project:

archway contracts build

To build a single contract within your project, execute the following command, replacing [CONTRACT] with the actual name of your contract:

archway contracts build [CONTRACT]

The following is an example of the output:

Building optimized wasm file for all contracts in the workspace using Docker...1.71.0-x86_64-unknown-linux-musl (default)cargo 1.71.0 (cfd3bbd8f 2023-06-08)Building artifacts in workspace...Found workspace member entries: ["contracts/*"]Package directories: ["contracts/increment"]Contracts to be built: ["contracts/increment"]Building "contracts/increment" ...warning: profiles for the non root package will be ignored, specify profiles at the workspace root:package:   /code/contracts/increment/Cargo.tomlworkspace: /code/Cargo.toml   Compiling proc-macro2 v1.0.70   Compiling unicode-ident v1.0.12   Compiling syn v1.0.109   Compiling typenum v1.17.0   Compiling version_check v0.9.4   Compiling serde v1.0.193   Compiling generic-array v0.14.7   Compiling zeroize v1.7.0   Compiling serde_json v1.0.108   Compiling quote v1.0.33   Compiling ryu v1.0.15   Compiling syn v2.0.39   Compiling itoa v1.0.9   Compiling schemars v0.8.16   Compiling thiserror v1.0.50   Compiling crypto-common v0.1.6   Compiling block-buffer v0.10.4   Compiling dyn-clone v1.0.16   Compiling subtle v2.5.0   Compiling const-oid v0.9.5   Compiling cfg-if v1.0.0   Compiling bnum v0.8.1   Compiling digest v0.10.7   Compiling sha2 v0.10.8   Compiling serde_derive v1.0.193   Compiling serde_derive_internals v0.26.0   Compiling thiserror-impl v1.0.50   Compiling static_assertions v1.1.0   Compiling hex v0.4.3   Compiling forward_ref v1.0.0   Compiling base64 v0.21.5   Compiling schemars_derive v0.8.16   Compiling cosmwasm-derive v1.5.0   Compiling derivative v2.2.0   Compiling cosmwasm-schema-derive v1.5.0   Compiling semver v1.0.20   Compiling serde-json-wasm v0.5.1   Compiling cosmwasm-std v1.5.0   Compiling cosmwasm-schema v1.5.0   Compiling cw-storage-plus v1.2.0   Compiling cosmwasm-storage v1.5.0   Compiling archway-bindings v0.2.1   Compiling cw2 v1.1.2   Compiling increment v0.1.0 (/code/contracts/increment)

Compiling other contracts

If your contract was not built using the Archway Developer CLI, you can use the method below to compile your contract. Please note that the following method will also work for contracts created using the Archway Developer CLI.

For this example, we will download and compile an open-source smart contract. Begin by downloading the cw-contracts repository. You will be compiling the nameservice contract. Execute the following set of commands:

git clone https://github.com/InterWasm/cw-contractscd cw-contractsgit checkout maincd contracts/nameservice

Then, run the following command to compile the contract:

docker run --rm -v "$(pwd)":/code \  --mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \  --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \  cosmwasm/optimizer:0.16.0

This command will optimize the contract code and generate an optimized cw_nameservice.wasm file which is then stored in the artifacts directory.

The following is an example of the output:

Building project /code ...Building cw_nameservice with features {}warning: `/code/.cargo/config` is deprecated in favor of `config.toml`note: if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`  Updating crates.io index  Downloading crates ...  Downloaded thiserror-impl v1.0.37  Downloaded itoa v0.4.8  Downloaded getrandom v0.1.16  Downloaded base64 v0.13.0  Downloaded unicode-xid v0.2.2  Downloaded version_check v0.9.3  Downloaded proc-macro2 v1.0.28  Downloaded typenum v1.15.0  Downloaded syn v1.0.75  Downloaded uint v0.9.4  Downloaded cosmwasm-crypto v1.1.2  Downloaded const-oid v0.9.0  Downloaded libc v0.2.100  Downloaded serde_json v1.0.66  Downloaded serde_derive v1.0.129  Downloaded serde v1.0.129  Downloaded schemars v0.8.10  Downloaded k256 v0.11.5  Downloaded ed25519-zebra v3.0.0  Downloaded ecdsa v0.14.7  Downloaded der v0.6.0  Downloaded cw-storage-plus v0.13.4  Downloaded crypto-bigint v0.4.8  Downloaded cosmwasm-storage v1.1.2  Downloaded cosmwasm-std v1.1.2  Downloaded cosmwasm-schema v1.1.2  Downloaded cosmwasm-derive v1.1.2  Downloaded signature v1.6.3  Downloaded schemars_derive v0.8.10  Downloaded rfc6979 v0.3.0  Downloaded ff v0.12.0  Downloaded cosmwasm-schema-derive v1.1.2  Downloaded thiserror v1.0.37  Downloaded ryu v1.0.5  Downloaded quote v1.0.9  Downloaded base64ct v1.5.2  Downloaded dyn-clone v1.0.4  Downloaded cpufeatures v0.1.5  Downloaded rand_core v0.6.3  Downloaded getrandom v0.2.3  Downloaded generic-array v0.14.4  Downloaded sha2 v0.9.5  Downloaded group v0.12.0  Downloaded digest v0.10.5  Compiling proc-macro2 v1.0.28  Compiling unicode-xid v0.2.2  Compiling syn v1.0.75  Compiling serde_derive v1.0.129  Compiling serde v1.0.129  Compiling ryu v1.0.5  Compiling serde_json v1.0.66  Compiling itoa v0.4.8  Compiling crunchy v0.2.2  Compiling thiserror v1.0.37  Compiling quote v1.0.9  Compiling schemars v0.8.10  Compiling dyn-clone v1.0.4  Compiling byteorder v1.4.3  Compiling hex v0.4.3  Compiling static_assertions v1.1.0  Compiling base64 v0.13.0  Compiling forward_ref v1.0.0  Compiling uint v0.9.4  Compiling serde_derive_internals v0.26.0  Compiling schemars_derive v0.8.10  Compiling thiserror-impl v1.0.37  Compiling cosmwasm-derive v1.1.2  Compiling derivative v2.2.0  Compiling cosmwasm-schema-derive v1.1.2  Compiling serde-json-wasm v0.4.1  Compiling cosmwasm-std v1.1.2  Compiling cosmwasm-schema v1.1.2  Compiling cosmwasm-storage v1.1.2  Compiling cw-storage-plus v0.13.4  Compiling cw-nameservice v0.12.0 (/code)  Finished `release` profile [optimized] target(s) in 4m 08sOptimizing artifacts ...Optimizing cw_nameservice.wasm ...Post-processing artifacts...810e980d972f1dbdd2887541067b11ae10603ee8c227f70cc221c2c3c27c4ea6  cw_nameservice.wasmDone.