Skip to content

Commit

Permalink
TokenV4: unit is mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 authored and thesimplekid committed Oct 24, 2024
1 parent 45de44f commit 419b1a3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 25 deletions.
29 changes: 13 additions & 16 deletions crates/cdk/src/nuts/nut00/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Token {
mint_url: MintUrl,
proofs: Proofs,
memo: Option<String>,
unit: Option<CurrencyUnit>,
unit: CurrencyUnit,
) -> Self {
let proofs = proofs
.into_iter()
Expand Down Expand Up @@ -90,10 +90,10 @@ impl Token {
}

/// Unit
pub fn unit(&self) -> &Option<CurrencyUnit> {
pub fn unit(&self) -> Option<CurrencyUnit> {
match self {
Self::TokenV3(token) => token.unit(),
Self::TokenV4(token) => token.unit(),
Self::TokenV4(token) => Some(token.unit()),
}
}

Expand Down Expand Up @@ -219,8 +219,8 @@ impl TokenV3 {
}

#[inline]
fn unit(&self) -> &Option<CurrencyUnit> {
&self.unit
fn unit(&self) -> Option<CurrencyUnit> {
self.unit
}
}

Expand Down Expand Up @@ -257,7 +257,7 @@ impl From<TokenV4> for TokenV3 {
TokenV3 {
token: vec![TokenV3Token::new(mint_url, proofs)],
memo: token.memo,
unit: token.unit,
unit: Some(token.unit),
}
}
}
Expand All @@ -269,14 +269,12 @@ pub struct TokenV4 {
#[serde(rename = "m")]
pub mint_url: MintUrl,
/// Token Unit
#[serde(rename = "u", skip_serializing_if = "Option::is_none")]
pub unit: Option<CurrencyUnit>,
#[serde(rename = "u")]
pub unit: CurrencyUnit,
/// Memo for token
#[serde(rename = "d", skip_serializing_if = "Option::is_none")]
pub memo: Option<String>,
/// Proofs
///
/// Proofs separated by keyset_id
/// Proofs grouped by keyset_id
#[serde(rename = "t")]
pub token: Vec<TokenV4Token>,
}
Expand Down Expand Up @@ -319,8 +317,8 @@ impl TokenV4 {
}

#[inline]
fn unit(&self) -> &Option<CurrencyUnit> {
&self.unit
fn unit(&self) -> CurrencyUnit {
self.unit
}
}

Expand Down Expand Up @@ -374,7 +372,7 @@ impl TryFrom<TokenV3> for TokenV4 {
mint_url: mint_url.to_owned(),
token: proofs,
memo: token.memo,
unit: token.unit,
unit: token.unit.ok_or(Error::UnsupportedUnit)?,
})
}
}
Expand Down Expand Up @@ -469,8 +467,7 @@ mod tests {

assert_eq!(amount, Amount::from(4));

let unit = (*token.unit()).unwrap();

let unit = token.unit().unwrap();
assert_eq!(CurrencyUnit::Sat, unit);

match token {
Expand Down
1 change: 0 additions & 1 deletion crates/cdk/src/wallet/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ impl Wallet {
let mint_url = self.mint_url.clone();
let unit = self.unit;

// If we have a description, we check that the mint supports it.
// If we have a description, we check that the mint supports it.
if description.is_some() {
let mint_method_settings = self
Expand Down
7 changes: 1 addition & 6 deletions crates/cdk/src/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ impl Wallet {
let ys = proofs.ys()?;
self.localstore.reserve_proofs(ys).await?;

Ok(Token::new(
self.mint_url.clone(),
proofs,
memo,
Some(self.unit),
))
Ok(Token::new(self.mint_url.clone(), proofs, memo, self.unit))
}

/// Send
Expand Down
3 changes: 1 addition & 2 deletions crates/cdk/src/wallet/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ pub enum SendKind {
OnlineExact,
/// Prefer offline send if difference is less then tolerance
OnlineTolerance(Amount),
/// Wallet cannot do an online swap and selectedp proof must be exactly send
/// amount
/// Wallet cannot do an online swap and selected proof must be exactly send amount
OfflineExact,
/// Wallet must remain offline but can over pay if below tolerance
OfflineTolerance(Amount),
Expand Down

0 comments on commit 419b1a3

Please sign in to comment.