Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate contract code #59

Merged
merged 7 commits into from
Sep 13, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,26 +729,57 @@ where
}

/// Duplicates the contract code identified by `code_id` and returns
/// the identifier of newly created copy of the contract.
/// the identifier of the newly created copy of the contract code.
///
/// # Examples
///
/// ```
/// use cosmwasm_std::Addr;
/// use cw_multi_test::App;
///
/// //TODO:
/// // Provide a happy path example when testing contracts are public,
/// // otherwise the example code would be unreadable.
/// // contract implementation
/// mod echo {
/// # use cosmwasm_std::{Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response, StdError, SubMsg, WasmMsg};
/// # use serde::{Deserialize, Serialize};
/// # use cw_multi_test::{Contract, ContractWrapper};
/// #
/// # #[derive(Debug, Clone, Serialize, Deserialize, Default)]
/// # pub struct EmptyMsg {}
DariuszDepta marked this conversation as resolved.
Show resolved Hide resolved
/// #
/// # fn instantiate(_: DepsMut, _: Env, _: MessageInfo, _: EmptyMsg) -> Result<Response, StdError> {
/// # Ok(Response::default())
/// # }
/// #
/// # fn execute(_: DepsMut, _: Env, _info: MessageInfo, msg: WasmMsg) -> Result<Response, StdError> {
/// # let message = SubMsg::new(msg);
/// # Ok(Response::new().add_submessage(message))
/// # }
/// #
/// # fn query(_deps: Deps, _env: Env, _msg: EmptyMsg) -> Result<Binary, StdError> {
/// # Err(StdError::generic_err("not implemented yet"))
/// # }
DariuszDepta marked this conversation as resolved.
Show resolved Hide resolved
/// // returns the contract
/// pub fn contract() -> Box<dyn Contract<Empty>> {
/// # Box::new(ContractWrapper::new(execute, instantiate, query))
DariuszDepta marked this conversation as resolved.
Show resolved Hide resolved
/// }
/// }
///
/// let mut app = App::default();
///
/// // there is no contract code with identifier 100 stored yet, returns an error
/// assert_eq!("code id 100: no such code", app.duplicate_code(100).unwrap_err().to_string());
/// // store a new contract, save the code id
/// # #[cfg(not(feature = "multitest_api_1_0"))]
/// let code_id = app.store_code_with_creator(Addr::unchecked("creator"), echo::contract());
/// # #[cfg(feature = "multitest_api_1_0")]
/// # let code_id = app.store_code(Addr::unchecked("creator"), echo::contract());
///
/// // duplicate the existing contract, duplicated contract has different code id
/// assert_ne!(code_id, app.duplicate_code(code_id).unwrap());
///
/// // zero is an invalid identifier for contract code, returns an error
/// assert_eq!("code id: invalid", app.duplicate_code(0).unwrap_err().to_string());
///
/// // there is no contract code with identifier 100 stored yet, returns an error
/// assert_eq!("code id 100: no such code", app.duplicate_code(100).unwrap_err().to_string());
/// ```
pub fn duplicate_code(&mut self, code_id: u64) -> AnyResult<u64> {
self.init_modules(|router, _, _| router.wasm.duplicate_code(code_id))
Expand Down