Skip to content

Commit

Permalink
added UpdateChainName and GetChainName methods (#334)
Browse files Browse the repository at this point in the history
* added UpdateChainName and GetChainName methods

* update: updated GetChainName to ChainName & added custom response type to the query

* renamed get_chain_name function to chain_name function
  • Loading branch information
cowboy0015 authored Mar 19, 2024
1 parent 5daccfd commit 67dcd7c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 5 deletions.
4 changes: 4 additions & 0 deletions contracts/os/andromeda-kernel/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ pub fn execute(
kernel_address,
),
ExecuteMsg::Recover {} => execute::recover(execute_env),
ExecuteMsg::UpdateChainName { chain_name } => {
execute::update_chain_name(execute_env, chain_name)
}
ExecuteMsg::Internal(msg) => execute::internal(execute_env, msg),
// Base message
ExecuteMsg::Ownership(ownership_message) => ADOContract::default().execute_ownership(
Expand Down Expand Up @@ -175,6 +178,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result<Binary, ContractErr
}
QueryMsg::ChannelInfo { chain } => encode_binary(&query::channel_info(deps, chain)?),
QueryMsg::Recoveries { addr } => encode_binary(&query::recoveries(deps, addr)?),
QueryMsg::ChainName {} => encode_binary(&query::chain_name(deps)?),
// Base queries
QueryMsg::Version {} => encode_binary(&ADOContract::default().query_version(deps)?),
QueryMsg::Type {} => encode_binary(&ADOContract::default().query_type(deps)?),
Expand Down
24 changes: 22 additions & 2 deletions contracts/os/andromeda-kernel/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use cosmwasm_std::{
use crate::ibc::{generate_transfer_message, PACKET_LIFETIME};
use crate::query;
use crate::state::{
IBCHooksPacketSendState, ADO_OWNER, CHAIN_TO_CHANNEL, CHANNEL_TO_CHAIN, IBC_FUND_RECOVERY,
KERNEL_ADDRESSES, OUTGOING_IBC_HOOKS_PACKETS,
IBCHooksPacketSendState, ADO_OWNER, CHAIN_TO_CHANNEL, CHANNEL_TO_CHAIN, CURR_CHAIN,
IBC_FUND_RECOVERY, KERNEL_ADDRESSES, OUTGOING_IBC_HOOKS_PACKETS,
};

pub fn send(ctx: ExecuteContext, message: AMPMsg) -> Result<Response, ContractError> {
Expand Down Expand Up @@ -322,6 +322,26 @@ pub fn recover(execute_env: ExecuteContext) -> Result<Response, ContractError> {
.add_submessage(sub_msg))
}

pub fn update_chain_name(
execute_env: ExecuteContext,
chain_name: String,
) -> Result<Response, ContractError> {
// Only owner can update CURR_CHAIN
let contract = ADOContract::default();
ensure!(
contract.is_contract_owner(execute_env.deps.storage, execute_env.info.sender.as_str())?,
ContractError::Unauthorized {}
);

// Update CURR_CHAIN
CURR_CHAIN.save(execute_env.deps.storage, &chain_name)?;

Ok(Response::default()
.add_attribute("action", "update_chain_name")
.add_attribute("sender", execute_env.info.sender.as_str())
.add_attribute("chain_name", chain_name))
}

/// Handles a given AMP message and returns a response
///
/// Separated due to common functionality across multiple messages
Expand Down
13 changes: 11 additions & 2 deletions contracts/os/andromeda-kernel/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use andromeda_std::{
amp::ADO_DB_KEY,
error::ContractError,
os::{aos_querier::AOSQuerier, kernel::ChannelInfoResponse},
os::{
aos_querier::AOSQuerier,
kernel::{ChainNameResponse, ChannelInfoResponse},
},
};
use cosmwasm_std::{Addr, Coin, Deps};

use crate::state::{CHAIN_TO_CHANNEL, IBC_FUND_RECOVERY, KERNEL_ADDRESSES};
use crate::state::{CHAIN_TO_CHANNEL, CURR_CHAIN, IBC_FUND_RECOVERY, KERNEL_ADDRESSES};

pub fn key_address(deps: Deps, key: String) -> Result<Addr, ContractError> {
Ok(KERNEL_ADDRESSES.load(deps.storage, &key)?)
Expand Down Expand Up @@ -48,3 +51,9 @@ pub fn recoveries(deps: Deps, addr: Addr) -> Result<Vec<Coin>, ContractError> {
.may_load(deps.storage, &addr)?
.unwrap_or_default())
}

pub fn chain_name(deps: Deps) -> Result<ChainNameResponse, ContractError> {
Ok(ChainNameResponse {
chain_name: CURR_CHAIN.may_load(deps.storage)?.unwrap_or_default(),
})
}
38 changes: 37 additions & 1 deletion contracts/os/andromeda-kernel/src/testing/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
contract::{execute, instantiate},
ibc::PACKET_LIFETIME,
state::{ADO_OWNER, CHAIN_TO_CHANNEL, CHANNEL_TO_CHAIN, KERNEL_ADDRESSES},
state::{ADO_OWNER, CHAIN_TO_CHANNEL, CHANNEL_TO_CHAIN, CURR_CHAIN, KERNEL_ADDRESSES},
};
use andromeda_std::{
amp::{
Expand Down Expand Up @@ -34,6 +34,42 @@ fn proper_initialization() {
assert_eq!(0, res.messages.len());
}

#[test]
fn test_update_chain_name() {
let mut deps = mock_dependencies_custom(&[]);
let info = mock_info("creator", &[]);
let env = mock_env();
instantiate(
deps.as_mut(),
env.clone(),
info.clone(),
InstantiateMsg {
owner: None,
chain_name: "test".to_string(),
},
)
.unwrap();

let chain_name = "other".to_string();
let update_chain_name_msg = ExecuteMsg::UpdateChainName {
chain_name: chain_name.clone(),
};

let fake_info = mock_info("fake", &[]);

let err = execute(
deps.as_mut(),
env.clone(),
fake_info,
update_chain_name_msg.clone(),
)
.unwrap_err();
assert_eq!(err, ContractError::Unauthorized {});

execute(deps.as_mut(), env, info, update_chain_name_msg).unwrap();
assert_eq!(CURR_CHAIN.load(deps.as_ref().storage).unwrap(), chain_name);
}

#[test]
fn test_create_ado() {
let mut deps = mock_dependencies_custom(&[]);
Expand Down
11 changes: 11 additions & 0 deletions packages/std/src/os/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ pub enum ExecuteMsg {
},
/// Recovers funds from failed IBC messages
Recover {},
/// Update Current Chain
UpdateChainName {
chain_name: String,
},
// Only accessible to key contracts
Internal(InternalMsg),
// Base message
Expand Down Expand Up @@ -91,6 +95,11 @@ pub struct ChannelInfoResponse {
pub supported_modules: Vec<String>,
}

#[cw_serde]
pub struct ChainNameResponse {
pub chain_name: String,
}

#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
Expand All @@ -102,6 +111,8 @@ pub enum QueryMsg {
ChannelInfo { chain: String },
#[returns(Vec<::cosmwasm_std::Coin>)]
Recoveries { addr: Addr },
#[returns(ChainNameResponse)]
ChainName {},
// Base queries
#[returns(VersionResponse)]
Version {},
Expand Down

0 comments on commit 67dcd7c

Please sign in to comment.