-
Notifications
You must be signed in to change notification settings - Fork 35
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
Better amount inputs #246
Better amount inputs #246
Conversation
Preview URL: https://mutiny-testnet-776l5z5xs-paulteam.vercel.app |
can't say this was fun but it did end up battle testing the rust conversion function so that's nice |
if (typeof parsedAmount === "number" && parsedAmount !== 0 && price) { | ||
// If the currency is set to usd, we need to convert the input amount to sats and store that in setAmount | ||
if (currency === Currency.USD) { | ||
setAmount(usdToSats(parsedAmount, price)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be?
setAmount(satsToUSD(parsedAmount, price))
This is for where the input is formatting for USD when the Currency.USD option is selected correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is correct I think this is what is causing the input to not be able to have a pre-switched value of less than $1 or just cents in general
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah it's correct, the "source of truth" is sats so we always need to set that.
yeah I gave up before I could manage to support less than $1. maybe a good follow-on pr?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks a lot cleaner 👍
// rust bitcoin doesn't like extra precision in the float | ||
// so we round to the nearest satoshi | ||
// explained here: https://stackoverflow.com/questions/28655362/how-does-one-round-a-floating-point-number-to-a-specified-number-of-digits | ||
let truncated = 10i32.pow(8) as f64; | ||
let btc = (btc * truncated).round() / truncated; | ||
if let Ok(amount) = bitcoin::Amount::from_btc(btc as f64) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oof this does not sound fun.
builds on @benalleng 's work in #242
switching back and forth between usd and sats was more annoying to code than I anticipated but finally happy with how it works. keeps a source of truth in sats for minimal infinite loop opportunities