Skip to content

Commit

Permalink
update contract information
Browse files Browse the repository at this point in the history
  • Loading branch information
janfabian committed Jun 29, 2023
1 parent f53a632 commit 4d63656
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 12 deletions.
2 changes: 1 addition & 1 deletion scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ END
)

local komple_mint_addr
komple_mint_addr=juno1dxn84lf6ghvp9uc9y5uh6xs530cwjnz8qkqyrtelzdgjkt72sapsekyveh
komple_mint_addr=juno17rth4jstxs7cmrusvyluwlnt34l80cxaz7nufpjfntts00pk79asjxelgs

local msg
msg="$(printf "${instantiate_msg}" "$ADMIN" "$komple_mint_addr")"
Expand Down
24 changes: 24 additions & 0 deletions scripts/update-contract-information.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CONTRACT_ADDR=$(<./scripts/contract-address-junox)

ADMIN="${SENDER:-juno1zk4c4aamef42cgjexlmksypac8j5xw7n3s4wrd}"
KOMPLE_MINT_ADDR="${KOMPLE_MINT_ADDR:-juno17rth4jstxs7cmrusvyluwlnt34l80cxaz7nufpjfntts00pk79asjxelgs}"

SETUP_FARM_MSG=$(cat <<-END
{
"update_contract_information": {
"contract_information": {
"admin": "%s",
"komple_mint_addr": "%s"
}
}
}
END
)

MSG=$(printf "$SETUP_FARM_MSG" "$ADMIN" "$KOMPLE_MINT_ADDR")

echo $MSG

TX_HASH=$(junod --chain-id uni-6 --node https://juno-testnet-rpc.polkachu.com:443 tx wasm execute "$CONTRACT_ADDR" "$MSG" --from "${ADMIN}" --gas-prices 0.075ujunox --gas auto --gas-adjustment 1.3 -o json -y | jq '.txhash' -r)
echo $TX_HASH

18 changes: 16 additions & 2 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cw2::set_contract_version;
use komple_framework_mint_module::msg::ExecuteMsg as KompleMintExecuteMsg;

use crate::error::ContractError;
use crate::msg::{ContractInformationResponse, ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use crate::msg::{ContractInformation, ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};

use crate::helpers::throw_err;
use crate::receive::receive;
Expand All @@ -30,7 +30,7 @@ pub fn instantiate(

INFORMATION.save(
deps.storage,
&ContractInformationResponse {
&ContractInformation {
admin,
komple_mint_addr: msg.komple_mint_addr,
},
Expand Down Expand Up @@ -172,6 +172,20 @@ pub fn execute(
}
};
}
ExecuteMsg::UpdateContractInformation {
contract_information,
} => {
let sender = info.sender.to_string();
let info = INFORMATION.load(deps.storage)?;

if sender != info.admin {
return Err(ContractError::Unauthorized {});
}

INFORMATION.save(deps.storage, &contract_information)?;

Ok(Response::new().add_attribute("action", "update_contract_information"))
}
}
}

Expand Down
27 changes: 21 additions & 6 deletions src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,26 @@ pub struct MigrateMsg {}
#[cw_serde]
pub enum ExecuteMsg {
Start {},
SetupFarm { farm: FarmProfile, addr: Addr },
SetupFarm {
farm: FarmProfile,
addr: Addr,
},
Stop {},
TillGround { x: u8, y: u8 },
WaterPlant { x: u8, y: u8 },
Harvest { x: u8, y: u8 },
TillGround {
x: u8,
y: u8,
},
WaterPlant {
x: u8,
y: u8,
},
Harvest {
x: u8,
y: u8,
},
UpdateContractInformation {
contract_information: ContractInformation,
},
ReceiveNft(Cw721ReceiveMsg),
}

Expand All @@ -32,7 +47,7 @@ pub enum Cw721HookMsg {
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
#[returns(ContractInformationResponse)]
#[returns(ContractInformation)]
ContractInfo {},

// Returns a specific users farm profile from state via query
Expand All @@ -48,7 +63,7 @@ pub struct KompleCollection {
}

#[cw_serde]
pub struct ContractInformationResponse {
pub struct ContractInformation {
pub admin: String,
pub komple_mint_addr: Option<String>,
}
4 changes: 2 additions & 2 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use cw_storage_plus::{Item, Map};
use crate::{
farm::{KomplePlant, Plant, PlantType, Slot, SlotType},
helpers::throw_err,
msg::ContractInformationResponse,
msg::ContractInformation,
params, ContractError,
};

Expand Down Expand Up @@ -83,7 +83,7 @@ pub struct FarmProfileDto {
}

pub const FARM_PROFILES: Map<&str, FarmProfile> = Map::new("farm_profiles");
pub const INFORMATION: Item<ContractInformationResponse> = Item::new("info");
pub const INFORMATION: Item<ContractInformation> = Item::new("info");

fn create_meadow_plot(block: u64) -> Slot {
return Slot {
Expand Down
51 changes: 50 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::contract::{execute, instantiate};

use crate::msg::{ExecuteMsg, InstantiateMsg};
use crate::msg::{ContractInformation, ExecuteMsg, InstantiateMsg};
use crate::state::INFORMATION;

use cosmwasm_std::testing::{
Expand Down Expand Up @@ -111,3 +111,52 @@ fn proper_initialization() {

assert_eq!(information.admin, "creator");
}

#[test]
#[should_panic(expected = "Unauthorized")]
fn unauthorized_config_update() {
let (mut deps, env) = setup_test(Some(InstantiateMsg {
admin: Some("admin".to_string()),
komple_mint_addr: Some(get_komple_addrs().mint.to_string()),
}));

let sender = "non-admin";
let auth_info = mock_info(sender, &vec![]);
let msg = ExecuteMsg::UpdateContractInformation {
contract_information: ContractInformation {
admin: "non-admin".to_string(),
komple_mint_addr: Some("new-komple-mint".to_string()),
},
};

let _res = execute(deps.as_mut(), env.to_owned(), auth_info, msg).unwrap();
}

#[test]
fn authorized_config_update() {
let (mut deps, env) = setup_test(Some(InstantiateMsg {
admin: Some("admin".to_string()),
komple_mint_addr: Some(get_komple_addrs().mint.to_string()),
}));

let sender = "admin";
let auth_info = mock_info(sender, &vec![]);
let msg = ExecuteMsg::UpdateContractInformation {
contract_information: ContractInformation {
admin: "new-admin".to_string(),
komple_mint_addr: Some("new-komple-mint".to_string()),
},
};

let _res = execute(deps.as_mut(), env.to_owned(), auth_info, msg).unwrap();

let information = INFORMATION.load(&deps.storage).unwrap();

assert_eq!(
information,
ContractInformation {
admin: "new-admin".to_string(),
komple_mint_addr: Some("new-komple-mint".to_string())
}
)
}

0 comments on commit 4d63656

Please sign in to comment.