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

add cashu token melting to mutiny-node #1026

Merged
merged 2 commits into from
Feb 22, 2024

Conversation

elnosh
Copy link
Contributor

@elnosh elnosh commented Feb 12, 2024

following up on @oleonardolima outline on #1021.

mutiny-core/src/error.rs Outdated Show resolved Hide resolved
mutiny-core/src/error.rs Outdated Show resolved Hide resolved
mutiny-core/src/lib.rs Outdated Show resolved Hide resolved
mutiny-core/src/lib.rs Outdated Show resolved Hide resolved
mutiny-wasm/Cargo.toml Outdated Show resolved Hide resolved
mutiny-wasm/src/error.rs Show resolved Hide resolved
@elnosh elnosh marked this pull request as ready for review February 12, 2024 18:14
Copy link
Collaborator

@benthecarman benthecarman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really close, just a couple things

mutiny-core/src/lib.rs Outdated Show resolved Hide resolved
mutiny-core/src/lib.rs Show resolved Hide resolved
Copy link

@oleonardolima oleonardolima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

Copy link
Collaborator

@benthecarman benthecarman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you squash the commits?

Just some nits, otherwise lgtm

Comment on lines 2258 to 2315
.clone()
.bolt11
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: if you do .bolt11.clone() instead of .clone().bolt11 then it'll only clone the invoice instead of the whole struct.

let invoice_amount = token.proofs.total_amount() as f64 * (1.0 - 3.0 / 100.0);

let mut mutiny_invoice = self
.create_invoice(invoice_amount as u64, vec!["Cashu Token Melt".to_string()])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make "Cashu Token Melt" a const at the top of this file, we have one for SWAP as well

mutiny-core/src/lib.rs Outdated Show resolved Hide resolved
Comment on lines 921 to 922
Ok(JsValue::from_serde(
&self.inner.melt_cashu_token(token).await?,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to convert this into mutiny-wasm's version of MutinyInvoice

Have to do something like this.

let result = self.inner.melt_cashu_token(token).await?;
let invoices: Vec<MutinyInvoice> = result.into_iter().map(|i| i.into()).collect();
Ok(JsValue::from_serde(&invoices)?)

Comment on lines 2330 to 2338
log_debug!(
self.logger,
"will need to add extra outputs for overpaid fees"
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, can we log the amount here as well.

should be something like

let amt = post_melt_bolt11_response.change.iter().map(|s|s.amount).sum();

you can add it to the log by just putting "{amt}"

Copy link
Collaborator

@benthecarman benthecarman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Cargo.lock Outdated
]

[[package]]
name = "libsqlite3-sys"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eww, this adds another 4MB? Does mocha really need to pull in sqlite?

Cargo.lock Outdated
]

[[package]]
name = "moksha-wallet"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pulls in a lot of dependencies. What is in moksha wallet that isn't in core that we need?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moksha-core is providing the basic crypto implementation and primitives. Moksha-wallet adds a higher abstraction layer for working with a mint and provides methods for minting, melting, exporting etc. Since v1 cashu is using quotes for all the apis. The wallet hides this complexity and also adds multiple implementations for desktop/web httpclients and databases for storing the tokens. If you are not interested in storing tokens and just want to redeem them for a bolt11 invoice, using moksha-core might be the right choice.

&self,
token_v3: TokenV3,
) -> Result<Vec<MutinyInvoice>, MutinyError> {
let mint_client = CrossPlatformHttpClient::new();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If moksha_wallet is just for an HTTP client, can we get rid of it by just pulling in our current http library?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right. I think we can get rid of moksha-wallet and make the http calls to the mint without it.

None => return Err(MutinyError::EmptyMintURLError),
};

let invoice_amount = token.proofs.total_amount() as f64 * (1.0 - 3.0 / 100.0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of 1.0 - 3.0 / 100.0 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the amount for the invoice needs to be a bit lower than the actual amount of the cashu token to account for potential fees. Not sure on best way to calculate it but landed on that number after discussing with @benthecarman

Comment on lines 2335 to 2338
log_debug!(
self.logger,
"will need to add extra outputs for {amt} for overpaid fees"
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused why this debug is here. Should this be a warning? And does it always occur?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benthecarman commented to add that log that could be for a follow-up. Follow up could be to add outputs param to the post_melt_bolt call in order to get the overpaid fees if any. Or if we create an invoice for "right" amount, we could get rid of this.

mutiny-core/src/lib.rs Outdated Show resolved Hide resolved
mutiny-core/src/lib.rs Outdated Show resolved Hide resolved
mutiny-core/src/lib.rs Show resolved Hide resolved
@benthecarman
Copy link
Collaborator

looks like you have some rebase conflicts in the cargo.lock, I would just revert the changes in there and then compile again, let cargo fix it for you

Comment on lines 2340 to 2343
.mint_post(
&mint_url.join("/v1/melt/quote/bolt11")?,
json!(quote_request),
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can make this a dedicated function on the cashu client. This way you can just pass the mint url and a PostMeltQuoteBolt11Request. Will make things easier to use in the future and we could eventually mock it for tests too

Comment on lines 2360 to 2422
let post_melt_bolt11_response: PostMeltBolt11Response = self
.cashu_client
.mint_post(&mint_url.join("v1/melt/bolt11")?, json!(melt_request))
.await?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should do the same for this function


let total_proofs_amount = token.proofs.total_amount();
// create invoice for 1% less than proofs amount
let mut invoice_amount = total_proofs_amount as f64 * (1.0 - 1.0 / 100.0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would be more readable as
let mut invoice_amount = total_proofs_amount as f64 * 0.99;

Copy link
Contributor

@TonyGiorgio TonyGiorgio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really good! Nice work.

The only question/comment I have on is around the multiple token thing. Is that common? If so, I wonder if all of the errors in the for loop should just result in a continue and if a new PartialCashuError could be introduced. If it's not very common, then I think it's fine to leave it as is.

@elnosh
Copy link
Contributor Author

elnosh commented Feb 20, 2024

thanks! I believe the EmptyMintURLError in the for loop should not be very common. In NUT-00 https://github.com/cashubtc/nuts/blob/main/00.md https://github.com/cashubtc/nuts/blob/main/00.md#03---methods the mint url is not optional in the token so I'm thinking it should always be there.

The only question/comment I have on is around the multiple token thing. Is that common? If so, I wonder if all of the errors in the for loop should just result in a continue and if a new PartialCashuError could be introduced. If it's not very common, then I think it's fine to leave it as is.

@@ -56,6 +56,7 @@ fedimint-mint-client = { git = "https://github.com/fedimint/fedimint", rev = "6a
fedimint-ln-client = { git = "https://github.com/fedimint/fedimint", rev = "6a923ee10c3a578cd835044e3fdd94aa5123735a" }
fedimint-bip39 = { git = "https://github.com/fedimint/fedimint", rev = "6a923ee10c3a578cd835044e3fdd94aa5123735a" }
fedimint-ln-common = { git = "https://github.com/fedimint/fedimint", rev = "6a923ee10c3a578cd835044e3fdd94aa5123735a" }
moksha-core = { git = "https://github.com/ngutech21/moksha", rev = "0b9a32d216ba0782a4aa2cdb19a87589e16d9089" }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated waila to the commit 18d99977965662d46ccec29fecdb0ce493745917, can you do the same here so their in sync, then we should be good to go

@benthecarman benthecarman merged commit a9816e9 into MutinyWallet:master Feb 22, 2024
9 checks passed
@elnosh elnosh deleted the receive-cashu branch April 12, 2024 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants