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
Change Amount Debug impl to BTC with 8 decimals #414
Conversation
Why not use the Display trait if you want BTC amounts? Right now: |
I think it's easier to read with the decimal point in place. As Steven says, it's still showing exactly the same data, but with a |
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.
utack
My only problem with it is that it's a lot more complexity for a Debug impl, ie if there's some bug in The obvious easy solution would be |
BTW, what about renaming |
I'd prefer to keep I don't fully understand your comment about RGB or Confidential Assets. We've been using rust-elements with CA for quite a while now and this hasn't been a point of difficulty. |
@elichai I do understand your concern. It might be tricky to use the (admittedly suspicious) floating point trickery in a Apart from that, I think external users of Citing
That said, I'd be ok with accepting the status quo. I just experienced that satoshi values are often hard to read. Like |
Do not understand why CI has stuck blocking merging. I do not have an option of restarting GitHub actions |
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.
utACK 6186ee6. This PR was raised before we migrated to actions. So, the CI won't pick it up. A rebase should fixup everything.
How about using Like this: if f.alternate() && self.0 >= 1000000 {
write!(f, "Amount({:.8} BTC)", self.as_btc())
} else {
write!(f, "Amount({} satoshi)", self.as_sat())
} |
I won't pretend this is any cleaner, but if your main concern is the float arithmetic: impl fmt::Debug for Amount {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut str_repr = self.as_sat().to_string();
if str_repr.len() > 8 {
str_repr.insert(str_repr.len() - 8, '.');
} else {
let amt_zeros = 8 - str_repr.len();
str_repr = "0.".chars()
.chain(std::iter::repeat('0').take(amt_zeros))
.chain(str_repr.chars())
.collect::<String>();
}
write!(f, "Amount({} BTC)", str_repr)
}
} (I actually had to use a similar atrocity for thousands separators, that's where I got the idea from 😄 ) |
Please use math, not allocations: write!(f, "Amount({}.", self.as_sat() / 100_000_000);
let fract = self.as_sat() % 100_000_000;
let mut mul = 10_000_000;
while mul > fract && mul > 1 {
write!(f, "0");
mul /= 10;
}
write!("{} BTC)", fract % 100_000_000); I'm leaving optimizing dynamic dispatch as an exercise for the reader. :) |
I've been working a bit with Amounts and the satoshi notation is really confusing if you're dealing with 1 BTC+ amounts. The
Debug
trait is meant to as easily as possible give the developer an idea of the amount, so adding a decimal point gives strictly more information. Like this it always shows 8 digits.This should not be considered a breaking change, IMO. The
Debug
trait is not intended to be parseable.