Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcobb23 committed Nov 7, 2024
1 parent ef5ef51 commit dfc0a7f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 57 deletions.
62 changes: 27 additions & 35 deletions contracts/interchain-token-service/src/contract/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,33 +229,17 @@ pub fn enable_execution(deps: DepsMut) -> Result<Response, Error> {
pub fn register_chains(deps: DepsMut, chains: Vec<msg::ChainConfig>) -> Result<Response, Error> {
chains
.into_iter()
.map(|chain_config| {
register_chain(
deps.storage,
chain_config.chain,
chain_config.its_edge_contract,
chain_config.max_uint,
chain_config.max_target_decimals,
)
})
.map(|chain_config| register_chain(deps.storage, chain_config))
.try_collect::<_, Vec<Response>, _>()?
.then(|_| Ok(Response::new()))
}

pub fn register_chain(
storage: &mut dyn Storage,
chain: ChainNameRaw,
its_address: Address,
max_uint: nonempty::Uint256,
max_target_decimals: u8,
) -> Result<Response, Error> {
match state::may_load_chain_config(storage, &chain).change_context(Error::State)? {
Some(_) => bail!(Error::ChainAlreadyRegistered(chain)),
None => {
state::save_chain_config(storage, &chain, its_address, max_uint, max_target_decimals)
.change_context(Error::State)?
.then(|_| Ok(Response::new()))
}
fn register_chain(storage: &mut dyn Storage, config: msg::ChainConfig) -> Result<Response, Error> {
match state::may_load_chain_config(storage, &config.chain).change_context(Error::State)? {
Some(_) => bail!(Error::ChainAlreadyRegistered(config.chain)),
None => state::save_chain_config(storage, &config.chain.clone(), config)
.change_context(Error::State)?
.then(|_| Ok(Response::new())),
}
}

Expand Down Expand Up @@ -859,18 +843,24 @@ mod tests {
let mut deps = mock_dependencies();
assert_ok!(register_chain(
deps.as_mut().storage,
SOLANA.parse().unwrap(),
ITS_ADDRESS.to_string().try_into().unwrap(),
Uint256::one().try_into().unwrap(),
16u8
msg::ChainConfig {
chain: SOLANA.parse().unwrap(),

its_edge_contract: ITS_ADDRESS.to_string().try_into().unwrap(),
max_uint: Uint256::one().try_into().unwrap(),
max_target_decimals: 16u8
}
));
assert_err_contains!(
register_chain(
deps.as_mut().storage,
SOLANA.parse().unwrap(),
ITS_ADDRESS.to_string().try_into().unwrap(),
Uint256::one().try_into().unwrap(),
16u8
msg::ChainConfig {
chain: SOLANA.parse().unwrap(),

its_edge_contract: ITS_ADDRESS.to_string().try_into().unwrap(),
max_uint: Uint256::one().try_into().unwrap(),
max_target_decimals: 16u8
}
),
Error,
Error::ChainAlreadyRegistered(..)
Expand Down Expand Up @@ -942,10 +932,12 @@ mod tests {
let chain = ChainNameRaw::try_from(chain_name).unwrap();
assert_ok!(register_chain(
deps.as_mut().storage,
chain.clone(),
ITS_ADDRESS.to_string().try_into().unwrap(),
Uint256::one().try_into().unwrap(),
16u8
msg::ChainConfig {
chain: chain.clone(),
its_edge_contract: ITS_ADDRESS.to_string().try_into().unwrap(),
max_uint: Uint256::one().try_into().unwrap(),
max_target_decimals: 16u8
}
));
}
}
Expand Down
50 changes: 28 additions & 22 deletions contracts/interchain-token-service/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cw_storage_plus::{Item, Map};
use error_stack::{report, Result, ResultExt};
use router_api::{Address, ChainNameRaw};

use crate::TokenId;
use crate::{msg, TokenId};

#[derive(thiserror::Error, Debug, IntoContractError)]
pub enum Error {
Expand Down Expand Up @@ -40,6 +40,17 @@ pub struct ChainConfig {
frozen: bool,
}

impl From<msg::ChainConfig> for ChainConfig {
fn from(value: msg::ChainConfig) -> Self {
Self {
max_uint: value.max_uint,
max_target_decimals: value.max_target_decimals,
its_address: value.its_edge_contract,
frozen: false,
}
}
}

#[cw_serde]
pub enum TokenSupply {
/// The total token supply bridged to this chain.
Expand Down Expand Up @@ -144,21 +155,10 @@ pub fn load_chain_config(
pub fn save_chain_config(
storage: &mut dyn Storage,
chain: &ChainNameRaw,
its_contract: Address,
max_uint: nonempty::Uint256,
max_target_decimals: u8,
config: impl Into<ChainConfig>,
) -> Result<(), Error> {
CHAIN_CONFIGS
.save(
storage,
chain,
&ChainConfig {
max_uint,
max_target_decimals,
its_address: its_contract,
frozen: false,
},
)
.save(storage, chain, &config.into())
.change_context(Error::Storage)
}

Expand Down Expand Up @@ -322,17 +322,23 @@ mod tests {

assert_ok!(save_chain_config(
deps.as_mut().storage,
&chain1,
address1.clone(),
Uint256::MAX.try_into().unwrap(),
16u8
&chain1.clone(),
msg::ChainConfig {
chain: chain1.clone(),
its_edge_contract: address1.clone(),
max_uint: Uint256::MAX.try_into().unwrap(),
max_target_decimals: 16u8
}
));
assert_ok!(save_chain_config(
deps.as_mut().storage,
&chain2,
address2.clone(),
Uint256::MAX.try_into().unwrap(),
16u8
&chain2.clone(),
msg::ChainConfig {
chain: chain2.clone(),
its_edge_contract: address2.clone(),
max_uint: Uint256::MAX.try_into().unwrap(),
max_target_decimals: 16u8
}
));
assert_eq!(
assert_ok!(load_its_contract(deps.as_ref().storage, &chain1)),
Expand Down

0 comments on commit dfc0a7f

Please sign in to comment.