From e5c81d3848be135771d369ec21a2c7fe16eafa9f Mon Sep 17 00:00:00 2001 From: Mubarak Muhammad Aminu Date: Tue, 12 Nov 2024 14:18:58 +0100 Subject: [PATCH] multiple active keysets and return active keyset with lowest fee (#448) --- crates/cdk/src/wallet/keysets.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/cdk/src/wallet/keysets.rs b/crates/cdk/src/wallet/keysets.rs index 9e5babc51..546c17a51 100644 --- a/crates/cdk/src/wallet/keysets.rs +++ b/crates/cdk/src/wallet/keysets.rs @@ -50,7 +50,7 @@ impl Wallet { /// Queries mint for current keysets then gets [`Keys`] for any unknown /// keysets #[instrument(skip(self))] - pub async fn get_active_mint_keyset(&self) -> Result { + pub async fn get_active_mint_keysets(&self) -> Result, Error> { let keysets = self.client.get_mint_keysets(self.mint_url.clone()).await?; let keysets = keysets.keysets; @@ -86,6 +86,21 @@ impl Wallet { } } - active_keysets.first().ok_or(Error::NoActiveKeyset).cloned() + Ok(active_keysets) + } + + /// Get active keyset for mint with the lowest fees + /// + /// Queries mint for current keysets then gets [`Keys`] for any unknown + /// keysets + #[instrument(skip(self))] + pub async fn get_active_mint_keyset(&self) -> Result { + let active_keysets = self.get_active_mint_keysets().await?; + + let keyset_with_lowest_fee = active_keysets + .into_iter() + .min_by_key(|key| key.input_fee_ppk) + .ok_or(Error::NoActiveKeyset)?; + Ok(keyset_with_lowest_fee) } }