Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Apr 10, 2024
1 parent b835fee commit d7aae94
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
21 changes: 14 additions & 7 deletions moksha-cli/src/bin/moksha-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ async fn main() -> anyhow::Result<()> {
Command::Receive { token } => {
let token: TokenV3 = TokenV3::from_str(&token)?;
let mint_urls = wallet.get_mint_urls().await?;
let currency = match &token.currency_unit {
Some(currency) => currency,
None => &CurrencyUnit::Sat,
};

let token_mint_url = match token.mint() {
Some(url) => url,
Expand Down Expand Up @@ -152,14 +156,15 @@ async fn main() -> anyhow::Result<()> {

let wallet_keysets = wallet.get_wallet_keysets().await?;
let wallet_keyset = wallet_keysets
.get_active(&token_mint_url, &CurrencyUnit::Sat)
.get_active(&token_mint_url, currency)
.expect("no active keyset found");

wallet.receive_tokens(wallet_keyset, &token).await?;
cli::show_total_balance(&wallet).await?;
}
Command::Send { amount } => {
let mint_url = choose_mint(&wallet, &CurrencyUnit::Sat).await?;
let currency_unit = CurrencyUnit::Sat;
let mint_url = choose_mint(&wallet, &currency_unit).await?;

if mint_url.1 < amount {
term.write_line("Error: Not enough tokens in selected mint")?;
Expand All @@ -170,7 +175,7 @@ async fn main() -> anyhow::Result<()> {

let wallet_keysets = wallet.get_wallet_keysets().await?;
let wallet_keyset = wallet_keysets
.get_active(&mint_url, &CurrencyUnit::Sat)
.get_active(&mint_url, &currency_unit)
.expect("no active keyset found");

term.write_line(&format!("Using tokens from mint: {mint_url}"))?;
Expand Down Expand Up @@ -243,7 +248,8 @@ async fn main() -> anyhow::Result<()> {
}
Command::PayOnchain { address, amount } => {
// FIXME remove redundant code
let mint_url = choose_mint(&wallet, &CurrencyUnit::Sat).await?;
let currency = CurrencyUnit::Sat;
let mint_url = choose_mint(&wallet, &currency).await?;

if mint_url.1 < amount {
term.write_line("Error: Not enough tokens in selected mint")?;
Expand All @@ -253,7 +259,7 @@ async fn main() -> anyhow::Result<()> {

let wallet_keysets = wallet.get_wallet_keysets().await?;
let wallet_keyset = wallet_keysets
.get_active(&mint_url, &CurrencyUnit::Sat)
.get_active(&mint_url, &currency)
.expect("Keyset not found");

let info = wallet.get_mint_info(&mint_url).await?;
Expand Down Expand Up @@ -309,7 +315,8 @@ async fn main() -> anyhow::Result<()> {
}
}
Command::Mint { amount } => {
let mint_url = choose_mint(&wallet, &CurrencyUnit::Sat).await?.0;
let currency = CurrencyUnit::Sat;
let mint_url = choose_mint(&wallet, &currency).await?.0;

let info = wallet.get_mint_info(&mint_url).await?;

Expand Down Expand Up @@ -396,7 +403,7 @@ async fn main() -> anyhow::Result<()> {

let wallet_keysets = wallet.get_wallet_keysets().await?;
let wallet_keyset = wallet_keysets
.get_active(&mint_url, &CurrencyUnit::Sat)
.get_active(&mint_url, &currency)
.expect("Keyset not found");

let progress_bar = cli::progress_bar()?;
Expand Down
17 changes: 10 additions & 7 deletions moksha-core/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ where
pub struct TokenV3 {
#[serde(rename = "token")]
pub tokens: Vec<Token>,
pub unit: Option<CurrencyUnit>,
#[serde(rename = "unit")]
pub currency_unit: Option<CurrencyUnit>,
pub memo: Option<String>,
}

Expand All @@ -62,15 +63,15 @@ impl TokenV3 {
Self {
tokens: vec![token],
memo: None,
unit: None,
currency_unit: None,
}
}

pub const fn empty() -> Self {
Self {
tokens: vec![],
memo: None,
unit: None,
currency_unit: None,
}
}

Expand Down Expand Up @@ -160,7 +161,7 @@ impl From<(Url, Proofs)> for TokenV3 {
proofs: from.1,
}],
memo: None,
unit: None,
currency_unit: None,
}
}
}
Expand All @@ -173,7 +174,7 @@ impl From<(Url, CurrencyUnit, Proofs)> for TokenV3 {
proofs: from.2,
}],
memo: None,
unit: Some(from.1),
currency_unit: Some(from.1),
}
}
}
Expand Down Expand Up @@ -227,7 +228,9 @@ mod tests {
Some(Url::parse("https://8333.space:3338")?)
);
assert_eq!(token.tokens[0].proofs.len(), 2);
assert_eq!(token.unit, Some(CurrencyUnit::Sat));
assert_eq!(token.currency_unit, Some(CurrencyUnit::Sat));
assert_eq!(token.memo, Some("Thank you.".to_string()));
assert_eq!(token.total_amount(), 10);

let token_serialized = token.serialize()?;
let fixture = read_fixture("token_nut_example.cashu")?;
Expand Down Expand Up @@ -284,7 +287,7 @@ mod tests {
let tokens = super::TokenV3 {
tokens: vec![token],
memo: Some("my memo".to_string()),
unit: None,
currency_unit: None,
};

let serialized: String = tokens.try_into()?;
Expand Down
4 changes: 2 additions & 2 deletions moksha-wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,12 +1034,12 @@ mod tests {

let first = result.0;

assert_eq!(CurrencyUnit::Sat, first.clone().unit.unwrap());
assert_eq!(CurrencyUnit::Sat, first.clone().currency_unit.unwrap());
assert_eq!(24, first.total_amount());

let second = result.1;

assert_eq!(CurrencyUnit::Sat, second.clone().unit.unwrap());
assert_eq!(CurrencyUnit::Sat, second.clone().currency_unit.unwrap());
assert_eq!(40, second.total_amount());
Ok(())
}
Expand Down

0 comments on commit d7aae94

Please sign in to comment.