Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple active keysets and return active keyset with lowest fee #448

Merged
merged 5 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/cdk/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ pub enum Error {
/// No active keyset
#[error("No active keyset")]
NoActiveKeyset,
/// No active keyset
#[error("No active keyset with lowest fee")]
NoActiveKeysetWithLowestFee,
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
/// Incorrect quote amount
#[error("Incorrect quote amount")]
IncorrectQuoteAmount,
Expand Down
19 changes: 17 additions & 2 deletions crates/cdk/src/wallet/keysets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeySetInfo, Error> {
pub async fn get_active_mint_keysets(&self) -> Result<Vec<KeySetInfo>, Error> {
let keysets = self.client.get_mint_keysets(self.mint_url.clone()).await?;
let keysets = keysets.keysets;

Expand Down Expand Up @@ -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<KeySetInfo, Error> {
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::NoActiveKeysetWithLowestFee)?;
mubarak23 marked this conversation as resolved.
Show resolved Hide resolved
Ok(keyset_with_lowest_fee)
}
}
Loading