diff --git a/contracts/os/andromeda-kernel/src/contract.rs b/contracts/os/andromeda-kernel/src/contract.rs index 536257f3d..6bff73b09 100644 --- a/contracts/os/andromeda-kernel/src/contract.rs +++ b/contracts/os/andromeda-kernel/src/contract.rs @@ -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( @@ -175,6 +178,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> Result encode_binary(&query::channel_info(deps, chain)?), QueryMsg::Recoveries { addr } => encode_binary(&query::recoveries(deps, addr)?), + QueryMsg::GetChainName {} => encode_binary(&query::get_chain_name(deps)?), // Base queries QueryMsg::Version {} => encode_binary(&ADOContract::default().query_version(deps)?), QueryMsg::Type {} => encode_binary(&ADOContract::default().query_type(deps)?), diff --git a/contracts/os/andromeda-kernel/src/execute.rs b/contracts/os/andromeda-kernel/src/execute.rs index 197a996cb..2abe24a91 100644 --- a/contracts/os/andromeda-kernel/src/execute.rs +++ b/contracts/os/andromeda-kernel/src/execute.rs @@ -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 { @@ -322,6 +322,26 @@ pub fn recover(execute_env: ExecuteContext) -> Result { .add_submessage(sub_msg)) } +pub fn update_chain_name( + execute_env: ExecuteContext, + chain_name: String, +) -> Result { + // 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 diff --git a/contracts/os/andromeda-kernel/src/query.rs b/contracts/os/andromeda-kernel/src/query.rs index 5c4b54e4d..01c158990 100644 --- a/contracts/os/andromeda-kernel/src/query.rs +++ b/contracts/os/andromeda-kernel/src/query.rs @@ -5,7 +5,7 @@ use andromeda_std::{ }; 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 { Ok(KERNEL_ADDRESSES.load(deps.storage, &key)?) @@ -48,3 +48,7 @@ pub fn recoveries(deps: Deps, addr: Addr) -> Result, ContractError> { .may_load(deps.storage, &addr)? .unwrap_or_default()) } + +pub fn get_chain_name(deps: Deps) -> Result { + Ok(CURR_CHAIN.may_load(deps.storage)?.unwrap_or_default()) +} diff --git a/contracts/os/andromeda-kernel/src/testing/tests.rs b/contracts/os/andromeda-kernel/src/testing/tests.rs index be291bf63..e10cc825b 100644 --- a/contracts/os/andromeda-kernel/src/testing/tests.rs +++ b/contracts/os/andromeda-kernel/src/testing/tests.rs @@ -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::{ @@ -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(&[]); diff --git a/packages/std/src/os/kernel.rs b/packages/std/src/os/kernel.rs index 18ce3b704..a16fce401 100644 --- a/packages/std/src/os/kernel.rs +++ b/packages/std/src/os/kernel.rs @@ -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 @@ -102,6 +106,8 @@ pub enum QueryMsg { ChannelInfo { chain: String }, #[returns(Vec<::cosmwasm_std::Coin>)] Recoveries { addr: Addr }, + #[returns(String)] + GetChainName {}, // Base queries #[returns(VersionResponse)] Version {},