Cross-deploy Vanilla CW and Secret Contracts
A description of building the same code for both vanilla CosmWasm and the Secret version.
Crosschain Contract Demo
The crosschain contract that we are examining can be cloned from this repo. This repo is a demo of writing a simple voting contract that is intended to be deployed on multiple chains with different types of CosmWasm in a single codebase. This demo supports two types of CosmWasm: vanilla
(i.e. original, like Terra or Juno supported) and secret
(i.e. SecretNetwork supported).
The contract code uses conditional compilation and feature flags in Rust to support different configurations within a single codebase. If you navigate to contract.rs, you will find two sets of imports for different features, either "secret"
or "vanilla"
. The imports are conditionally compiled using #[cfg(feature = "…")]
attributes. The "secret"
feature uses the secret_std
crate, whereas the "vanilla"
feature uses the cosmwasm_std
crate.
For more information about the feature differences of Standard CosmWasm vs the secret version please visit this page. TLDR: No raw queries, iterators and upgradeability
Contract.rs
Contract.rs defines several functions:
instantiate
: This function initializes the contract and sets the initial vote count for both options to 0. In this instance, it is the same for both Secret and vanilla CosmWasm.execute
: This function processes theExecuteMsg
, which supports voting. It calls thevote_impl
function to perform the vote.vote_impl
: This function is implemented twice, once for each feature ("secret"
and"vanilla"
). It checks whether the voter has already voted, and if not, it updates the tally for the chosen option.query
: This function processesQueryMsg
, which supports two queries: getting the vote tallies (Tally
) and getting a list of voters (Voters
). For theTally
query, it returns the vote count for both options. For theVoters
query, it calls thevoters_query_impl
function.voters_query_impl
: This function is also implemented twice for each feature. It retrieves the voters list based on the provided page number.
Let's examine the differences in vote_impl
based on which type of CosmWasm we are using. The overall structure and logic of the function are the same, but there are differences in the specific methods called on the VOTERS
, OPTION_1
, and OPTION_2
objects. These differences arise from the different crates used for the "secret" and "vanilla" features.
fn vote_impl: Secret
In the "secret" version of vote_impl
:
VOTERS.contains(deps.storage, &info.sender)
is used to check if the voter already exists in the storage.VOTERS.insert(deps.storage, &info.sender, &1)
is used to insert the voter into the storage.
fn vote_impl: Vanilla CosmWasm
In the "vanilla" version of vote_impl
:
VOTERS.has(deps.storage, info.sender.clone())
is used to check if the voter already exists in the storage.VOTERS.save(deps.storage, info.sender, &1)
is used to save the voter into the storage.
The rest of the function, including the match statement for updating the vote tally, is the same between the two versions. Now let's examine the difference in querying functions between the two versions.
fn voters_query_impl: Secret
Vanilla CosmWasm Iterators are not supported on Secret Network because users cannot iterate over data stored by different users as there is no access to their dedicated encryption key. However, iteration is still possible on Secret Network through the use of the secret_toolkit
for storage in place of cosmwasm_std
.
In the "secret" version of voters_query_impl
:
The
VOTERS.paging_keys(deps.storage, page, PAGE_SIZE as u32)?
method is used to retrieve a list of voters corresponding to the requested page. This method is specific to thesecret_toolkit
storage API and directly handles pagination.
fn voters_query_impl: Vanilla
In the "vanilla" version of voters_query_impl
:
The
VOTERS.keys(deps.storage, None, None, cosmwasm_std::Order::Ascending)
method is used to retrieve an iterator over all the keys (voters) in the storage. Pagination is implemented manually in the function.
The main difference between the two implementations is that the Secret version relies on a secret_toolkit
pagination method (paging_keys
), whereas the Vanilla version manually implements pagination using iterator methods.
State.rs: Secret vs CosmWasm
The contract uses state_secret
or state_vanilla
modules for managing the state, depending on the selected feature. The state management includes saving the vote counts for each option and managing the list of voters. Let's examine the differences between state_secret.rs
and state_vanilla.rs
.
state_secret.rs
In the Secret version:
The
secret_std::Addr
is used as the address type.The
secret_toolkit::storage::Item
andsecret_toolkit::storage::Keymap
types are used for storage management.The storage objects are initialized with
Keymap::new
andItem::new
methods, passing the byte representation of the corresponding prefixes.
state_vanilla.rs
In the Vanilla version:
The
cosmwasm_std::Addr
is used as the address type.The
cw_storage_plus::Item
andcw_storage_plus::Map
types are used for storage management.The storage objects are initialized with the
Map::new
andItem::new
methods, passing the corresponding prefixes directly.
Thus, the Secret version relies on the secret_std
and secret_toolkit
crates, while the Vanilla version uses the cosmwasm_std
and cw_storage_plus
crates. However, the overall purpose of the state management objects (VOTERS, OPTION_1, and OPTION_2) remains the same in both versions.
How to cross-compilw on different chains
Last updated