Skip to content

Commit

Permalink
fix: send proofs with conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Aug 1, 2024
1 parent ed97625 commit 72bef1b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
8 changes: 6 additions & 2 deletions crates/cdk/examples/p2pk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ async fn main() -> Result<(), Error> {

let spending_conditions = SpendingConditions::new_p2pk(secret.public_key(), None);

let bal = wallet.total_balance().await.unwrap();

println!("{}", bal);

let token = wallet
.send(
amount,
None,
Some(spending_conditions),
&SplitTarget::None,
&SplitTarget::default(),
&SendKind::default(),
false,
)
Expand All @@ -67,7 +71,7 @@ async fn main() -> Result<(), Error> {
.await
.unwrap();

println!("Redeamed locked token worth: {}", u64::from(amount));
println!("Redeemed locked token worth: {}", u64::from(amount));

Ok(())
}
38 changes: 35 additions & 3 deletions crates/cdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,10 +1029,42 @@ impl Wallet {
(acc1, acc2)
},
);
let available_proofs = if proofs_sum < amount {
match &conditions {
Some(conditions) => {
let available_proofs = self
.localstore
.get_proofs(
Some(mint_url.clone()),
Some(*unit),
Some(vec![State::Unspent]),
None,
)
.await?;

if proofs_sum < amount {
return Err(Error::InsufficientFunds);
}
let available_proofs = available_proofs.into_iter().map(|p| p.proof).collect();

let proofs_to_swap =
self.select_proofs_to_swap(amount, available_proofs).await?;

let proofs_with_conditions = self
.swap(
Some(amount),
SplitTarget::default(),
proofs_to_swap,
Some(conditions.clone()),
include_fees,
)
.await?;
proofs_with_conditions.ok_or(Error::InsufficientFunds)?
}
None => {
return Err(Error::InsufficientFunds);
}
}
} else {
available_proofs
};

let selected = self
.select_proofs_to_send(amount, available_proofs, include_fees)
Expand Down

0 comments on commit 72bef1b

Please sign in to comment.