feat: use BTreeMap for wallet list

This commit is contained in:
thesimplekid
2024-08-24 09:48:38 +03:00
parent 4b3eca23e9
commit 9a009ef125
3 changed files with 9 additions and 9 deletions

View File

@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::BTreeMap;
use anyhow::Result;
use cdk::mint_url::MintUrl;
@@ -15,9 +15,9 @@ pub async fn mint_balances(
multi_mint_wallet: &MultiMintWallet,
unit: &CurrencyUnit,
) -> Result<Vec<(MintUrl, Amount)>> {
let wallets: HashMap<MintUrl, Amount> = multi_mint_wallet.get_balances(unit).await?;
let wallets: BTreeMap<MintUrl, Amount> = multi_mint_wallet.get_balances(unit).await?;
let mut wallets_vec = Vec::with_capacity(wallets.capacity());
let mut wallets_vec = Vec::with_capacity(wallets.len());
for (i, (mint_url, amount)) in wallets.iter().enumerate() {
let mint_url = mint_url.clone();

View File

@@ -312,7 +312,7 @@ where
}
/// Currency Unit
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub enum CurrencyUnit {
/// Sat
#[default]

View File

@@ -2,7 +2,7 @@
//!
//! Wrapper around core [`Wallet`] that enables the use of multiple mint unit pairs
use std::collections::HashMap;
use std::collections::{BTreeMap, HashMap};
use std::fmt;
use std::str::FromStr;
use std::sync::Arc;
@@ -24,11 +24,11 @@ use crate::{Amount, Wallet};
#[derive(Debug, Clone)]
pub struct MultiMintWallet {
/// Wallets
pub wallets: Arc<Mutex<HashMap<WalletKey, Wallet>>>,
pub wallets: Arc<Mutex<BTreeMap<WalletKey, Wallet>>>,
}
/// Wallet Key
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct WalletKey {
mint_url: MintUrl,
unit: CurrencyUnit,
@@ -103,8 +103,8 @@ impl MultiMintWallet {
pub async fn get_balances(
&self,
unit: &CurrencyUnit,
) -> Result<HashMap<MintUrl, Amount>, Error> {
let mut balances = HashMap::new();
) -> Result<BTreeMap<MintUrl, Amount>, Error> {
let mut balances = BTreeMap::new();
for (WalletKey { mint_url, unit: u }, wallet) in self.wallets.lock().await.iter() {
if unit == u {