Skip to content

Commit

Permalink
Add PoolPair (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
lubkoll authored Aug 20, 2024
1 parent ae82da8 commit ef6adae
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions smart-contracts/osmosis/packages/quasar-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
65 changes: 65 additions & 0 deletions smart-contracts/osmosis/packages/quasar-types/src/pool_pair.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use cosmwasm_std::Coin;

#[derive(Debug)]
pub struct PoolPair<S, T> {
pub base: S,
pub quote: T,
}

impl<S, T> PoolPair<S, T> {
pub fn new(base: S, quote: T) -> Self {
Self { base, quote }
}
}

pub trait Contains<T> {
fn contains(&self, value: T) -> bool;
}

impl Contains<&str> for PoolPair<String, String> {
fn contains(&self, value: &str) -> bool {
value == self.base || value == self.quote
}
}

impl Contains<&str> for PoolPair<Coin, Coin> {
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(&quote));
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(&quote.denom));
assert!(pair.contains(base.denom.as_str()));
assert!(!pair.contains(&"other".to_string()));
assert!(!pair.contains("other"));
}
}

0 comments on commit ef6adae

Please sign in to comment.