feat: copy on unit

This commit is contained in:
thesimplekid
2024-07-07 11:41:14 +01:00
parent 46f8689b1f
commit 0ff936d3b7
10 changed files with 42 additions and 68 deletions

View File

@@ -33,7 +33,7 @@ impl JsMeltQuote {
#[wasm_bindgen(getter)]
pub fn unit(&self) -> JsCurrencyUnit {
self.inner.unit.clone().into()
self.inner.unit.into()
}
#[wasm_bindgen(getter)]

View File

@@ -33,7 +33,7 @@ impl JsMintQuote {
#[wasm_bindgen(getter)]
pub fn unit(&self) -> JsCurrencyUnit {
self.inner.unit.clone().into()
self.inner.unit.into()
}
#[wasm_bindgen(getter)]

View File

@@ -44,7 +44,7 @@ async fn main() {
let localstore = WalletMemoryDatabase::default();
let wallet = Wallet::new(mint_url, unit.clone(), Arc::new(localstore), &seed);
let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed);
let quote = wallet.mint_quote(amount).await.unwrap();

View File

@@ -19,7 +19,7 @@ async fn main() -> Result<(), Error> {
let unit = CurrencyUnit::Sat;
let amount = Amount::from(10);
let wallet = Wallet::new(mint_url, unit.clone(), Arc::new(localstore), &seed);
let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed);
let quote = wallet.mint_quote(amount).await.unwrap();

View File

@@ -21,7 +21,7 @@ async fn main() {
let localstore = WalletMemoryDatabase::default();
let wallet = Wallet::new(mint_url, unit.clone(), Arc::new(localstore), &seed);
let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), &seed);
let quote = wallet.mint_quote(amount).await.unwrap();

View File

@@ -124,14 +124,7 @@ impl Mint {
expiry: u64,
ln_lookup: String,
) -> Result<MintQuote, Error> {
let quote = MintQuote::new(
mint_url,
request,
unit.clone(),
amount,
expiry,
ln_lookup.clone(),
);
let quote = MintQuote::new(mint_url, request, unit, amount, expiry, ln_lookup.clone());
tracing::debug!(
"New mint quote {} for {} {} with request id {}",
quote.id,
@@ -319,13 +312,8 @@ impl Mint {
derivation_path: DerivationPath,
max_order: u8,
) -> Result<(), Error> {
let (keyset, keyset_info) = create_new_keyset(
&self.secp_ctx,
self.xpriv,
derivation_path,
unit.clone(),
max_order,
);
let (keyset, keyset_info) =
create_new_keyset(&self.secp_ctx, self.xpriv, derivation_path, unit, max_order);
let id = keyset_info.id;
self.localstore.add_keyset_info(keyset_info).await?;
self.localstore.add_active_keyset(unit, id).await?;
@@ -962,7 +950,7 @@ fn create_new_keyset<C: secp256k1::Signing>(
);
let keyset_info = MintKeySetInfo {
id: keyset.id,
unit: keyset.unit.clone(),
unit: keyset.unit,
active: true,
valid_from: unix_time(),
valid_to: None,

View File

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

View File

@@ -508,7 +508,7 @@ mod tests {
token.token[0].proofs[0].clone().keyset_id,
Id::from_str("009a1f293253e41e").unwrap()
);
assert_eq!(token.unit.clone().unwrap(), CurrencyUnit::Sat);
assert_eq!(token.unit.unwrap(), CurrencyUnit::Sat);
let encoded = &token.to_string();

View File

@@ -78,7 +78,7 @@ impl Wallet {
.localstore
.get_proofs(
Some(self.mint_url.clone()),
Some(self.unit.clone()),
Some(self.unit),
Some(vec![State::Unspent]),
None,
)
@@ -101,7 +101,7 @@ impl Wallet {
.localstore
.get_proofs(
Some(self.mint_url.clone()),
Some(self.unit.clone()),
Some(self.unit),
Some(vec![State::Pending]),
None,
)
@@ -127,7 +127,7 @@ impl Wallet {
.localstore
.get_proofs(
Some(self.mint_url.clone()),
Some(self.unit.clone()),
Some(self.unit),
Some(vec![State::Reserved]),
None,
)
@@ -164,7 +164,7 @@ impl Wallet {
.localstore
.get_proofs(
Some(self.mint_url.clone()),
Some(self.unit.clone()),
Some(self.unit),
Some(vec![State::Unspent]),
None,
)
@@ -179,7 +179,7 @@ impl Wallet {
.localstore
.get_proofs(
Some(self.mint_url.clone()),
Some(self.unit.clone()),
Some(self.unit),
Some(vec![State::Pending]),
None,
)
@@ -195,7 +195,7 @@ impl Wallet {
.localstore
.get_proofs(
Some(self.mint_url.clone()),
Some(self.unit.clone()),
Some(self.unit),
Some(vec![State::Reserved]),
None,
)
@@ -435,7 +435,7 @@ impl Wallet {
.localstore
.get_proofs(
Some(self.mint_url.clone()),
Some(self.unit.clone()),
Some(self.unit),
Some(vec![State::Pending, State::Reserved]),
None,
)
@@ -474,17 +474,17 @@ impl Wallet {
#[instrument(skip(self))]
pub async fn mint_quote(&self, amount: Amount) -> Result<MintQuote, Error> {
let mint_url = self.mint_url.clone();
let unit = self.unit.clone();
let unit = self.unit;
let quote_res = self
.client
.post_mint_quote(mint_url.clone().try_into()?, amount, unit.clone())
.post_mint_quote(mint_url.clone().try_into()?, amount, unit)
.await?;
let quote = MintQuote {
mint_url,
id: quote_res.quote.clone(),
amount,
unit: unit.clone(),
unit,
request: quote_res.request,
state: quote_res.state,
expiry: quote_res.expiry.unwrap_or(0),
@@ -643,7 +643,7 @@ impl Wallet {
proof,
self.mint_url.clone(),
State::Unspent,
quote_info.unit.clone(),
quote_info.unit,
)
})
.collect();
@@ -735,7 +735,7 @@ impl Wallet {
.clone()
.into_iter()
.flat_map(|proof| {
ProofInfo::new(proof, mint_url.clone(), State::Reserved, unit.clone())
ProofInfo::new(proof, mint_url.clone(), State::Reserved, *unit)
})
.collect();
@@ -758,9 +758,7 @@ impl Wallet {
if let Some(proofs) = proofs_to_send.clone() {
let send_proofs = proofs
.into_iter()
.flat_map(|proof| {
ProofInfo::new(proof, mint_url.clone(), State::Reserved, unit.clone())
})
.flat_map(|proof| ProofInfo::new(proof, mint_url.clone(), State::Reserved, *unit))
.collect();
self.localstore.add_proofs(send_proofs).await?;
@@ -768,7 +766,7 @@ impl Wallet {
let keep_proofs = keep_proofs
.into_iter()
.flat_map(|proof| ProofInfo::new(proof, mint_url.clone(), State::Unspent, unit.clone()))
.flat_map(|proof| ProofInfo::new(proof, mint_url.clone(), State::Unspent, *unit))
.collect();
self.localstore.add_proofs(keep_proofs).await?;
@@ -939,7 +937,7 @@ impl Wallet {
.await?;
}
Ok(Token::new(mint_url.clone(), send_proofs, memo, Some(unit.clone())).to_string())
Ok(Token::new(mint_url.clone(), send_proofs, memo, Some(*unit)).to_string())
}
/// Melt Quote
@@ -963,12 +961,7 @@ impl Wallet {
let quote_res = self
.client
.post_melt_quote(
self.mint_url.clone().try_into()?,
self.unit.clone(),
invoice,
mpp,
)
.post_melt_quote(self.mint_url.clone().try_into()?, self.unit, invoice, mpp)
.await?;
if quote_res.amount != amount {
@@ -979,7 +972,7 @@ impl Wallet {
id: quote_res.quote,
amount,
request,
unit: self.unit.clone(),
unit: self.unit,
fee_reserve: quote_res.fee_reserve,
state: quote_res.state,
expiry: quote_res.expiry,
@@ -1146,7 +1139,7 @@ impl Wallet {
proof,
self.mint_url.clone(),
State::Unspent,
quote_info.unit.clone(),
quote_info.unit,
)
})
.collect();
@@ -1176,7 +1169,7 @@ impl Wallet {
.localstore
.get_proofs(
Some(mint_url.clone()),
Some(self.unit.clone()),
Some(self.unit),
Some(vec![State::Unspent]),
conditions,
)
@@ -1194,7 +1187,7 @@ impl Wallet {
let (active, inactive): (HashSet<KeySetInfo>, HashSet<KeySetInfo>) = mint_keysets
.into_iter()
.filter(|p| p.unit.eq(&self.unit.clone()))
.filter(|p| p.unit.eq(&self.unit))
.partition(|x| x.active);
let active: HashSet<Id> = active.iter().map(|k| k.id).collect();
@@ -1236,7 +1229,7 @@ impl Wallet {
.localstore
.get_proofs(
Some(mint_url.clone()),
Some(self.unit.clone()),
Some(self.unit),
Some(vec![State::Unspent]),
None,
)
@@ -1410,9 +1403,7 @@ impl Wallet {
total_amount += proofs.iter().map(|p| p.amount).sum();
let proofs = proofs
.into_iter()
.flat_map(|proof| {
ProofInfo::new(proof, mint.clone(), State::Unspent, self.unit.clone())
})
.flat_map(|proof| ProofInfo::new(proof, mint.clone(), State::Unspent, self.unit))
.collect();
self.localstore.add_proofs(proofs).await?;
}
@@ -1431,7 +1422,7 @@ impl Wallet {
) -> Result<Amount, Error> {
let token_data = Token::from_str(encoded_token)?;
let unit = token_data.unit().clone().unwrap_or_default();
let unit = token_data.unit().unwrap_or_default();
if unit != self.unit {
return Err(Error::UnitNotSupported);
@@ -1551,12 +1542,7 @@ impl Wallet {
let unspent_proofs = unspent_proofs
.into_iter()
.flat_map(|proof| {
ProofInfo::new(
proof,
self.mint_url.clone(),
State::Unspent,
keyset.unit.clone(),
)
ProofInfo::new(proof, self.mint_url.clone(), State::Unspent, keyset.unit)
})
.collect();

View File

@@ -52,7 +52,7 @@ impl MultiMintWallet {
wallets: Arc::new(Mutex::new(
wallets
.into_iter()
.map(|w| (WalletKey::new(w.mint_url.clone(), w.unit.clone()), w))
.map(|w| (WalletKey::new(w.mint_url.clone(), w.unit), w))
.collect(),
)),
}
@@ -61,7 +61,7 @@ impl MultiMintWallet {
/// Add wallet to MultiMintWallet
#[instrument(skip(self, wallet))]
pub async fn add_wallet(&self, wallet: Wallet) {
let wallet_key = WalletKey::new(wallet.mint_url.clone(), wallet.unit.clone());
let wallet_key = WalletKey::new(wallet.mint_url.clone(), wallet.unit);
let mut wallets = self.wallets.lock().await;
@@ -164,14 +164,14 @@ impl MultiMintWallet {
.ok_or(Error::UnknownWallet(wallet_key.to_string()))?;
let amount = wallet.check_all_mint_quotes().await?;
amount_minted.insert(wallet.unit.clone(), amount);
amount_minted.insert(wallet.unit, amount);
}
None => {
for (_, wallet) in self.wallets.lock().await.iter() {
let amount = wallet.check_all_mint_quotes().await?;
amount_minted
.entry(wallet.unit.clone())
.entry(wallet.unit)
.and_modify(|b| *b += amount)
.or_insert(amount);
}
@@ -208,7 +208,7 @@ impl MultiMintWallet {
preimages: &[String],
) -> Result<Amount, Error> {
let token_data = Token::from_str(encoded_token)?;
let unit = token_data.unit().clone().unwrap_or_default();
let unit = token_data.unit().unwrap_or_default();
let mint_proofs = token_data.proofs();
@@ -216,12 +216,12 @@ impl MultiMintWallet {
// Check that all mints in tokes have wallets
for (mint_url, proofs) in mint_proofs {
let wallet_key = WalletKey::new(mint_url.clone(), unit.clone());
let wallet_key = WalletKey::new(mint_url.clone(), unit);
if !self.has(&wallet_key).await {
return Err(Error::UnknownWallet(wallet_key.to_string()));
}
let wallet_key = WalletKey::new(mint_url, unit.clone());
let wallet_key = WalletKey::new(mint_url, unit);
let wallet = self
.get_wallet(&wallet_key)
.await