From 7cc4d3511bcdc9f0171fcf9626f57e6caab6c0d3 Mon Sep 17 00:00:00 2001 From: benthecarman Date: Tue, 6 Dec 2022 01:25:18 -0600 Subject: [PATCH] Sort transactions in rust rather than in javascript --- frontend/src/routes/OnChain.tsx | 15 +-------------- node-manager/src/nodemanager.rs | 12 +++++++++++- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/frontend/src/routes/OnChain.tsx b/frontend/src/routes/OnChain.tsx index fe454d38c..f82a3df06 100644 --- a/frontend/src/routes/OnChain.tsx +++ b/frontend/src/routes/OnChain.tsx @@ -45,19 +45,6 @@ const SingleTransaction = ({ tx, network }: { tx: OnChainTx, network?: string }) ) } -// Sort by timestamp, but if there's no timestamp put it first -function sortTx(a: OnChainTx, b: OnChainTx) { - if (b.confirmation_time && a.confirmation_time) { - return b.confirmation_time.timestamp - a.confirmation_time.timestamp - } else if (a.confirmation_time) { - return 1 - } else if (b.confirmation_time) { - return -1 - } else { - return a.txid.localeCompare(b.txid) - } -} - function OnChain() { const { nodeManager } = useContext(NodeManagerContext); @@ -80,7 +67,7 @@ function OnChain() { diff --git a/node-manager/src/nodemanager.rs b/node-manager/src/nodemanager.rs index 1d79bb9f2..0e07dd446 100644 --- a/node-manager/src/nodemanager.rs +++ b/node-manager/src/nodemanager.rs @@ -489,7 +489,17 @@ impl NodeManager { #[wasm_bindgen] pub async fn list_onchain(&self) -> Result { - let txs = self.wallet.list_transactions(false).await?; + let mut txs = self.wallet.list_transactions(false).await?; + + // Sort by timestamp, but if there's no timestamp put it first, + // if timestamps are equal, compare by txid + txs.sort_by(|a, b| { + a.confirmation_time + .as_ref() + .map(|c| c.timestamp) + .cmp(&b.confirmation_time.as_ref().map(|c| c.timestamp)) + .then_with(|| a.txid.cmp(&b.txid)) + }); Ok(serde_wasm_bindgen::to_value(&txs)?) }