From ef6adae7e77d6d8a071c273dacc62e0de3d8fc9d Mon Sep 17 00:00:00 2001 From: lubkoll <11710767+lubkoll@users.noreply.github.com> Date: Tue, 20 Aug 2024 20:51:16 +0200 Subject: [PATCH] Add PoolPair (#789) --- .../osmosis/packages/quasar-types/src/lib.rs | 1 + .../packages/quasar-types/src/pool_pair.rs | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 smart-contracts/osmosis/packages/quasar-types/src/pool_pair.rs diff --git a/smart-contracts/osmosis/packages/quasar-types/src/lib.rs b/smart-contracts/osmosis/packages/quasar-types/src/lib.rs index ac5fb0b86..6e4123a11 100644 --- a/smart-contracts/osmosis/packages/quasar-types/src/lib.rs +++ b/smart-contracts/osmosis/packages/quasar-types/src/lib.rs @@ -5,6 +5,7 @@ pub mod error; pub mod ibc; pub mod ica; pub mod icq; +pub mod pool_pair; pub mod query; pub mod queue; pub mod stride; diff --git a/smart-contracts/osmosis/packages/quasar-types/src/pool_pair.rs b/smart-contracts/osmosis/packages/quasar-types/src/pool_pair.rs new file mode 100644 index 000000000..3a7c37fa8 --- /dev/null +++ b/smart-contracts/osmosis/packages/quasar-types/src/pool_pair.rs @@ -0,0 +1,65 @@ +use cosmwasm_std::Coin; + +#[derive(Debug)] +pub struct PoolPair { + pub base: S, + pub quote: T, +} + +impl PoolPair { + pub fn new(base: S, quote: T) -> Self { + Self { base, quote } + } +} + +pub trait Contains { + fn contains(&self, value: T) -> bool; +} + +impl Contains<&str> for PoolPair { + fn contains(&self, value: &str) -> bool { + value == self.base || value == self.quote + } +} + +impl Contains<&str> for PoolPair { + fn contains(&self, value: &str) -> bool { + value == self.base.denom || value == self.quote.denom + } +} + +#[cfg(test)] +mod tests { + use super::*; + use cosmwasm_std::coin; + + #[test] + fn test_string_pair() { + let base = "base".to_string(); + let quote = "quote".to_string(); + let pair = PoolPair::new(base.clone(), quote.clone()); + assert_eq!(base, pair.base); + assert_eq!(quote, pair.quote); + + assert!(pair.contains(&base)); + assert!(pair.contains("e)); + assert!(pair.contains(base.as_str())); + assert!(!pair.contains(&"other".to_string())); + assert!(!pair.contains("other")); + } + + #[test] + fn test_coin_pair() { + let base = coin(123u128, "base"); + let quote = coin(456u128, "quote"); + let pair = PoolPair::new(base.clone(), quote.clone()); + assert_eq!(base, pair.base); + assert_eq!(quote, pair.quote); + + assert!(pair.contains(&base.denom)); + assert!(pair.contains("e.denom)); + assert!(pair.contains(base.denom.as_str())); + assert!(!pair.contains(&"other".to_string())); + assert!(!pair.contains("other")); + } +}