Skip to content

Commit

Permalink
validate the denom of each new limiter to be part of the pool assets
Browse files Browse the repository at this point in the history
  • Loading branch information
iboss-ptk committed Sep 26, 2023
1 parent c5249a4 commit a4cfd53
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
30 changes: 30 additions & 0 deletions contracts/transmuter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ impl Transmuter<'_> {
// only admin can register limiter
ensure_admin_authority!(info.sender, self.role.admin, deps.as_ref());

// ensure pool has the specified denom
let pool = self.pool.load(deps.storage)?;
ensure!(
pool.has_denom(&denom),
ContractError::InvalidPoolAssetDenom { denom }
);

let base_attrs = vec![
("method", "register_limiter"),
("denom", &denom),
Expand Down Expand Up @@ -1550,6 +1557,29 @@ mod tests {

assert_eq!(res.attributes, attrs);

// denom that is not in the pool can't be registered
let err = execute(
deps.as_mut(),
mock_env(),
mock_info(admin, &[]),
ContractExecMsg::Transmuter(ExecMsg::RegisterLimiter {
denom: "invalid_denom".to_string(),
label: "1h".to_string(),
limiter_params: LimiterParams::ChangeLimiter {
window_config: window_config_1h.clone(),
boundary_offset: Decimal::percent(1),
},
}),
)
.unwrap_err();

assert_eq!(
err,
ContractError::InvalidPoolAssetDenom {
denom: "invalid_denom".to_string(),
}
);

// Query the list of limiters
let query_msg = ContractQueryMsg::Transmuter(QueryMsg::ListLimiters {});
let res = query(deps.as_ref(), mock_env(), query_msg).unwrap();
Expand Down
31 changes: 31 additions & 0 deletions contracts/transmuter/src/transmuter_pool/has_denom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use super::TransmuterPool;

impl TransmuterPool {
/// Check if the pool has the specified denom
pub fn has_denom(&self, denom: &str) -> bool {
self.pool_assets
.iter()
.any(|pool_asset| pool_asset.denom == denom)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::denom::Denom;

#[test]
fn test_has_denom() {
let pool_assets = vec![
Denom::unchecked("asset1"),
Denom::unchecked("asset2"),
Denom::unchecked("asset3"),
];
let pool = TransmuterPool::new(&pool_assets).unwrap();

assert!(pool.has_denom("asset1"));
assert!(pool.has_denom("asset2"));
assert!(pool.has_denom("asset3"));
assert!(!pool.has_denom("asset4"));
}
}
1 change: 1 addition & 0 deletions contracts/transmuter/src/transmuter_pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod exit_pool;
mod join_pool;
mod transmute;
mod weight;
mod has_denom;

use cosmwasm_schema::cw_serde;
use cosmwasm_std::{ensure, Coin, Uint64};
Expand Down

0 comments on commit a4cfd53

Please sign in to comment.