Skip to content

Commit

Permalink
Implemented issues: #69 #70.
Browse files Browse the repository at this point in the history
  • Loading branch information
DariuszDepta committed Nov 8, 2023
1 parent ab22123 commit fe69c90
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 54 deletions.
24 changes: 15 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,18 +234,23 @@ where
CustomT::ExecT: Clone + Debug + PartialEq + JsonSchema + DeserializeOwned + 'static,
CustomT::QueryT: CustomQuery + DeserializeOwned + 'static,
{
/// Registers contract code (like uploading wasm bytecode on a chain),
/// so it can later be used to instantiate a contract.
pub fn store_code(&mut self, code: Box<dyn Contract<CustomT::ExecT, CustomT::QueryT>>) -> u64 {
self.init_modules(|router, _, _| {
router
.wasm
.store_code(Addr::unchecked("code-creator"), code)
})
/// Registers contract code, like uploading wasm bytecode on a chain.
///
/// Registered contract's code can be used later to instantiate a contract.
pub fn store_code(
&mut self,
creator: Addr,
code: Box<dyn Contract<CustomT::ExecT, CustomT::QueryT>>,
) -> u64 {
self.init_modules(|router, _, _| router.wasm.store_code(creator, code))
}

/// Registers contract code (like [store_code](Self::store_code)),
/// but takes the address of the code creator as an additional argument.
#[deprecated(
since = "1.0.0",
note = "use store_code instead, will be removed in version 2.0.0"
)]
pub fn store_code_with_creator(
&mut self,
creator: Addr,
Expand Down Expand Up @@ -290,9 +295,10 @@ where
/// }
///
/// let mut app = App::default();
/// let creator = Addr::unchecked("creator");
///
/// // store a new contract, save the code id
/// let code_id = app.store_code(echo::contract());
/// let code_id = app.store_code(creator, echo::contract());
///
/// // duplicate the existing contract, duplicated contract has different code id
/// assert_ne!(code_id, app.duplicate_code(code_id).unwrap());
Expand Down
78 changes: 39 additions & 39 deletions src/tests/test_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
custom_app, next_block, App, AppResponse, Bank, CosmosRouter, Distribution, Executor, Module,
Router, Staking, Wasm, WasmSudo,
};
use cosmwasm_std::testing::{mock_env, MockQuerier};
use cosmwasm_std::testing::{mock_env, MockApi, MockQuerier};
use cosmwasm_std::{
coin, coins, from_json, to_json_binary, Addr, AllBalanceResponse, Api, Attribute, BankMsg,
BankQuery, Binary, BlockInfo, Coin, CosmosMsg, CustomQuery, Empty, Event, OverflowError,
Expand Down Expand Up @@ -171,7 +171,7 @@ fn duplicate_contract_code() {
let mut app = App::default();

// store the original contract code
let code_id = app.store_code(payout::contract());
let code_id = app.store_code(Addr::unchecked("creator"), payout::contract());

// duplicate previously stored contract code
let dup_code_id = app.duplicate_code(code_id).unwrap();
Expand Down Expand Up @@ -246,7 +246,7 @@ fn simple_contract() {
});

// set up contract
let code_id = app.store_code(payout::contract());
let code_id = app.store_code(owner.clone(), payout::contract());

let msg = payout::InstantiateMessage {
payout: coin(5, "eth"),
Expand Down Expand Up @@ -333,7 +333,7 @@ fn reflect_success() {
});

// set up payout contract
let payout_id = app.store_code(payout::contract());
let payout_id = app.store_code(owner.clone(), payout::contract());

let msg = payout::InstantiateMessage {
payout: coin(5, "eth"),
Expand All @@ -350,7 +350,7 @@ fn reflect_success() {
.unwrap();

// set up reflect contract
let reflect_id = app.store_code(reflect::contract());
let reflect_id = app.store_code(owner.clone(), reflect::contract());

let reflect_addr = app
.instantiate_contract(reflect_id, owner, &Empty {}, &[], "Reflect", None)
Expand Down Expand Up @@ -439,7 +439,7 @@ fn reflect_error() {
});

// set up reflect contract
let reflect_id = app.store_code(reflect::contract());
let reflect_id = app.store_code(owner.clone(), reflect::contract());

let reflect_addr = app
.instantiate_contract(
Expand Down Expand Up @@ -533,7 +533,7 @@ fn sudo_works() {
.unwrap();
});

let payout_id = app.store_code(payout::contract());
let payout_id = app.store_code(owner.clone(), payout::contract());

let msg = payout::InstantiateMessage {
payout: coin(5, "eth"),
Expand Down Expand Up @@ -590,7 +590,7 @@ fn reflect_sub_message_reply_works() {
});

// set up reflect contract
let reflect_id = app.store_code(reflect::contract());
let reflect_id = app.store_code(owner.clone(), reflect::contract());

let reflect_addr = app
.instantiate_contract(
Expand Down Expand Up @@ -680,7 +680,7 @@ fn send_update_admin_works() {
let mut app = App::default();

// create a hackatom contract with some funds
let code_id = app.store_code(hackatom::contract());
let code_id = app.store_code(owner.clone(), hackatom::contract());

let contract = app
.instantiate_contract(
Expand Down Expand Up @@ -749,7 +749,7 @@ fn sent_wasm_migration_works() {
});

// create a hackatom contract with some funds
let code_id = app.store_code(hackatom::contract());
let code_id = app.store_code(owner.clone(), hackatom::contract());

let contract = app
.instantiate_contract(
Expand Down Expand Up @@ -816,7 +816,7 @@ fn sent_funds_properly_visible_on_execution() {
.unwrap();
});

let code_id = app.store_code(hackatom::contract());
let code_id = app.store_code(owner.clone(), hackatom::contract());

let contract = app
.instantiate_contract(
Expand Down Expand Up @@ -1036,7 +1036,7 @@ mod reply_data_overwrite {

let owner = Addr::unchecked("owner");

let code_id = app.store_code(echo::contract());
let code_id = app.store_code(owner.clone(), echo::contract());

let contract = app
.instantiate_contract(code_id, owner.clone(), &Empty {}, &[], "Echo", None)
Expand All @@ -1063,7 +1063,7 @@ mod reply_data_overwrite {

let owner = Addr::unchecked("owner");

let code_id = app.store_code(echo::contract());
let code_id = app.store_code(owner.clone(), echo::contract());

let contract = app
.instantiate_contract(code_id, owner.clone(), &Empty {}, &[], "Echo", None)
Expand Down Expand Up @@ -1096,7 +1096,7 @@ mod reply_data_overwrite {

let owner = Addr::unchecked("owner");

let code_id = app.store_code(echo::contract());
let code_id = app.store_code(owner.clone(), echo::contract());

let contract = app
.instantiate_contract(code_id, owner.clone(), &Empty {}, &[], "Echo", None)
Expand Down Expand Up @@ -1124,7 +1124,7 @@ mod reply_data_overwrite {

let owner = Addr::unchecked("owner");

let code_id = app.store_code(echo::contract());
let code_id = app.store_code(owner.clone(), echo::contract());

let contract = app
.instantiate_contract(code_id, owner.clone(), &Empty {}, &[], "Echo", None)
Expand Down Expand Up @@ -1152,7 +1152,7 @@ mod reply_data_overwrite {

let owner = Addr::unchecked("owner");

let code_id = app.store_code(echo::contract());
let code_id = app.store_code(owner.clone(), echo::contract());

let contract = app
.instantiate_contract(code_id, owner.clone(), &Empty {}, &[], "Echo", None)
Expand Down Expand Up @@ -1192,14 +1192,14 @@ mod reply_data_overwrite {
});

// set up reflect contract
let reflect_id = app.store_code(reflect::contract());
let reflect_id = app.store_code(owner.clone(), reflect::contract());

let reflect_addr = app
.instantiate_contract(reflect_id, owner.clone(), &Empty {}, &[], "Reflect", None)
.unwrap();

// set up echo contract
let echo_id = app.store_code(echo::custom_contract());
let echo_id = app.store_code(owner.clone(), echo::custom_contract());

let echo_addr = app
.instantiate_contract(echo_id, owner.clone(), &Empty {}, &[], "Echo", None)
Expand Down Expand Up @@ -1240,7 +1240,7 @@ mod reply_data_overwrite {

let owner = Addr::unchecked("owner");

let code_id = app.store_code(echo::contract());
let code_id = app.store_code(owner.clone(), echo::contract());

let contract = app
.instantiate_contract(code_id, owner.clone(), &Empty {}, &[], "Echo", None)
Expand Down Expand Up @@ -1283,7 +1283,7 @@ mod reply_data_overwrite {

let owner = Addr::unchecked("owner");

let code_id = app.store_code(echo::contract());
let code_id = app.store_code(owner.clone(), echo::contract());

let contract = app
.instantiate_contract(code_id, owner.clone(), &Empty {}, &[], "Echo", None)
Expand Down Expand Up @@ -1316,7 +1316,7 @@ mod reply_data_overwrite {

let owner = Addr::unchecked("owner");

let code_id = app.store_code(echo::contract());
let code_id = app.store_code(owner.clone(), echo::contract());

let contract = app
.instantiate_contract(code_id, owner.clone(), &Empty {}, &[], "Echo", None)
Expand Down Expand Up @@ -1354,7 +1354,7 @@ mod reply_data_overwrite {

let owner = Addr::unchecked("owner");

let code_id = app.store_code(echo::contract());
let code_id = app.store_code(owner.clone(), echo::contract());

let contract = app
.instantiate_contract(code_id, owner.clone(), &Empty {}, &[], "Echo", None)
Expand Down Expand Up @@ -1407,7 +1407,7 @@ mod response_validation {

let owner = Addr::unchecked("owner");

let code_id = app.store_code(echo::contract());
let code_id = app.store_code(owner.clone(), echo::contract());

let contract = app
.instantiate_contract(code_id, owner.clone(), &Empty {}, &[], "Echo", None)
Expand Down Expand Up @@ -1438,7 +1438,7 @@ mod response_validation {

let owner = Addr::unchecked("owner");

let code_id = app.store_code(echo::contract());
let code_id = app.store_code(owner.clone(), echo::contract());

let contract = app
.instantiate_contract(code_id, owner.clone(), &Empty {}, &[], "Echo", None)
Expand Down Expand Up @@ -1469,7 +1469,7 @@ mod response_validation {

let owner = Addr::unchecked("owner");

let code_id = app.store_code(echo::contract());
let code_id = app.store_code(owner.clone(), echo::contract());

let contract = app
.instantiate_contract(code_id, owner.clone(), &Empty {}, &[], "Echo", None)
Expand Down Expand Up @@ -1499,7 +1499,7 @@ mod response_validation {

let owner = Addr::unchecked("owner");

let code_id = app.store_code(echo::contract());
let code_id = app.store_code(owner.clone(), echo::contract());

let contract = app
.instantiate_contract(code_id, owner.clone(), &Empty {}, &[], "Echo", None)
Expand Down Expand Up @@ -1529,7 +1529,7 @@ mod response_validation {

let owner = Addr::unchecked("owner");

let code_id = app.store_code(echo::contract());
let code_id = app.store_code(owner.clone(), echo::contract());

let contract = app
.instantiate_contract(code_id, owner.clone(), &Empty {}, &[], "Echo", None)
Expand Down Expand Up @@ -1563,7 +1563,7 @@ mod contract_instantiation {
let sender = Addr::unchecked("sender");

// store contract's code
let code_id = app.store_code_with_creator(Addr::unchecked("creator"), echo::contract());
let code_id = app.store_code(Addr::unchecked("creator"), echo::contract());

// initialize the contract
let init_msg = to_json_binary(&Empty {}).unwrap();
Expand Down Expand Up @@ -1594,7 +1594,7 @@ mod wasm_queries {
fn query_existing_code_info() {
use super::*;
let mut app = App::default();
let code_id = app.store_code_with_creator(Addr::unchecked("creator"), echo::contract());
let code_id = app.store_code(Addr::unchecked("creator"), echo::contract());
let code_info_response = app.wrap().query_wasm_code_info(code_id).unwrap();
assert_eq!(code_id, code_info_response.code_id);
assert_eq!("creator", code_info_response.creator);
Expand Down Expand Up @@ -1678,7 +1678,7 @@ mod protobuf_wrapped_data {
});

// set up reflect contract
let code_id = app.store_code(reflect::contract());
let code_id = app.store_code(owner.clone(), reflect::contract());

let init_msg = to_json_binary(&Empty {}).unwrap();
let msg = WasmMsg::Instantiate {
Expand Down Expand Up @@ -1708,7 +1708,7 @@ mod protobuf_wrapped_data {
let mut app = BasicApp::new(|_, _, _| {});

// set up echo contract
let code_id = app.store_code(echo::contract());
let code_id = app.store_code(owner.clone(), echo::contract());

let msg = echo::InitMessage::<Empty> {
data: Some("food".into()),
Expand Down Expand Up @@ -1737,7 +1737,7 @@ mod protobuf_wrapped_data {
let mut app = BasicApp::new(|_, _, _| {});

// set up echo contract
let code_id = app.store_code(echo::contract());
let code_id = app.store_code(owner.clone(), echo::contract());

let msg = echo::InitMessage::<Empty> {
data: Some("food".into()),
Expand Down Expand Up @@ -1789,7 +1789,7 @@ mod protobuf_wrapped_data {
let mut app = BasicApp::new(|_, _, _| {});

// set up reflect contract
let code_id = app.store_code(echo::contract());
let code_id = app.store_code(owner.clone(), echo::contract());

let echo_addr = app
.instantiate_contract(code_id, owner.clone(), &Empty {}, &[], "label", None)
Expand All @@ -1816,7 +1816,7 @@ mod errors {
let mut app = App::default();

// set up contract
let code_id = app.store_code(error::contract(false));
let code_id = app.store_code(owner.clone(), error::contract(false));

let msg = Empty {};
let err = app
Expand All @@ -1842,7 +1842,7 @@ mod errors {
let mut app = App::default();

// set up contract
let code_id = app.store_code(error::contract(true));
let code_id = app.store_code(owner.clone(), error::contract(true));

let msg = Empty {};
let contract_addr = app
Expand Down Expand Up @@ -1872,8 +1872,8 @@ mod errors {
let owner = Addr::unchecked("owner");
let mut app = App::default();

let error_code_id = app.store_code(error::contract(true));
let caller_code_id = app.store_code(caller::contract());
let error_code_id = app.store_code(owner.clone(), error::contract(true));
let caller_code_id = app.store_code(owner.clone(), caller::contract());

// set up contract_helpers
let msg = Empty {};
Expand Down Expand Up @@ -1912,8 +1912,8 @@ mod errors {
let owner = Addr::unchecked("owner");
let mut app = App::default();

let error_code_id = app.store_code(error::contract(true));
let caller_code_id = app.store_code(caller::contract());
let error_code_id = app.store_code(owner.clone(), error::contract(true));
let caller_code_id = app.store_code(owner.clone(), caller::contract());

// set up contract_helpers
let msg = Empty {};
Expand Down
4 changes: 2 additions & 2 deletions src/tests/test_gov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Gov for AcceptingModule {}
#[test]
fn default_gov() {
let mut app = App::default();
let code = app.store_code(stargate::contract());
let code = app.store_code(Addr::unchecked("creator"), stargate::contract());
let contract = app
.instantiate_contract(
code,
Expand All @@ -82,7 +82,7 @@ fn substituting_gov() {
let mut app = AppBuilder::new()
.with_gov(AcceptingModule)
.build(|_, _, _| ());
let code = app.store_code(stargate::contract());
let code = app.store_code(Addr::unchecked("creator"), stargate::contract());
let contract = app
.instantiate_contract(
code,
Expand Down
Loading

0 comments on commit fe69c90

Please sign in to comment.