Skip to content

Commit

Permalink
Requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanoroshiba committed Sep 27, 2024
1 parent 0e50c5e commit fb7110f
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
87 changes: 87 additions & 0 deletions crates/astria-core/src/primitive/v1/asset/denom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,29 @@ impl FromStr for Denom {
}
}

impl PartialOrd for Denom {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl Ord for Denom {
/// If the denoms are the same type, returns their comparison. Otherwise, returns IBC prefixed
/// denoms as less than trace prefixed denoms.
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self {
Self::TracePrefixed(lhs) => match other {
Self::TracePrefixed(rhs) => lhs.cmp(rhs),
Self::IbcPrefixed(_) => std::cmp::Ordering::Greater,
},
Self::IbcPrefixed(lhs) => match other {
Self::IbcPrefixed(rhs) => lhs.cmp(rhs),
Self::TracePrefixed(_) => std::cmp::Ordering::Less,
},
}
}
}

#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct ParseDenomError(ParseDenomErrorKind);
Expand Down Expand Up @@ -284,6 +307,21 @@ impl TracePrefixed {
}
}

impl PartialOrd for TracePrefixed {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl Ord for TracePrefixed {
/// Returns trace comparison if not equal, otherwise returns base denom comparison.
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.trace
.cmp(&other.trace)
.then_with(|| self.base_denom.cmp(&other.base_denom))
}
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
struct TraceSegments {
inner: VecDeque<PortAndChannel>,
Expand Down Expand Up @@ -354,6 +392,28 @@ impl FromStr for TraceSegments {
Ok(parsed_segments)
}
}

impl PartialOrd for TraceSegments {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl Ord for TraceSegments {
/// Returns the first non-equal comparison between the two trace segments. If one doesn't exist,
/// returns the shortest, and if they are equal, returns equal.
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.inner
.iter()
.zip(other.inner.iter())
.find_map(|(self_segment, other_segment)| {
Some(self_segment.cmp(other_segment))
.filter(|&cmp| cmp != std::cmp::Ordering::Equal)
})
.unwrap_or(self.inner.len().cmp(&other.inner.len()))
}
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct PortAndChannel {
port: String,
Expand All @@ -372,6 +432,21 @@ impl PortAndChannel {
}
}

impl PartialOrd for PortAndChannel {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl Ord for PortAndChannel {
/// Returns port comparison if not equal, otherwise returns channel comparison.
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.port
.cmp(&other.port)
.then_with(|| self.channel.cmp(&other.channel))
}
}

impl std::fmt::Display for TracePrefixed {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for segment in &self.trace.inner {
Expand Down Expand Up @@ -549,6 +624,18 @@ impl FromStr for IbcPrefixed {
}
}

impl PartialOrd for IbcPrefixed {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl Ord for IbcPrefixed {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.id.cmp(&other.id)
}
}

#[cfg(feature = "serde")]
mod serde_impl {
use serde::{
Expand Down
7 changes: 5 additions & 2 deletions crates/astria-sequencer/src/accounts/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ async fn get_trace_prefixed_account_balances<S: StateRead>(
stream.try_collect::<Vec<_>>().await
}

/// Returns a list of `AssetBalance`s for the provided address. `AssetBalance`s are sorted first in
/// descending order by balance, then in ascending order by denom. IBC prefixed denoms are treated
/// as less than trace prefixed denoms.
pub(crate) async fn balance_request(
storage: Storage,
request: request::Query,
Expand Down Expand Up @@ -228,13 +231,13 @@ fn compare_asset_balances(lhs: &AssetBalance, rhs: &AssetBalance) -> std::cmp::O
use std::cmp::Ordering;

match lhs.balance.cmp(&rhs.balance) {
// Denoms should never have the same display name
Ordering::Equal => lhs.denom.to_string().cmp(&rhs.denom.to_string()),
Ordering::Equal => lhs.denom.cmp(&rhs.denom),
Ordering::Less => Ordering::Greater,
Ordering::Greater => Ordering::Less,
}
}

#[cfg(test)]
mod test {
#[test]
fn compare_asset_balances_sorts_asset_balances_as_expected() {
Expand Down

0 comments on commit fb7110f

Please sign in to comment.