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

Better amount inputs #246

Merged
merged 3 commits into from
Dec 27, 2022
Merged

Better amount inputs #246

merged 3 commits into from
Dec 27, 2022

Conversation

futurepaul
Copy link
Contributor

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

Screen Shot 2022-12-19 at 5 54 41 PM
Screen Shot 2022-12-19 at 5 54 34 PM
Screen Shot 2022-12-19 at 5 54 05 PM

@github-actions
Copy link

github-actions bot commented Dec 20, 2022

@futurepaul
Copy link
Contributor Author

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))
Copy link
Collaborator

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?

Copy link
Collaborator

@benalleng benalleng Dec 20, 2022

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

Copy link
Contributor Author

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?

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.

This looks a lot cleaner 👍

Comment on lines +924 to +929
// 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) {
Copy link
Collaborator

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.

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.

3 participants