Skip to content

docs: add amount example for units crate#43

Closed
Zeegaths wants to merge 1 commit intorust-bitcoin:masterfrom
Zeegaths:docs-amount-example
Closed

docs: add amount example for units crate#43
Zeegaths wants to merge 1 commit intorust-bitcoin:masterfrom
Zeegaths:docs-amount-example

Conversation

@Zeegaths
Copy link

@Zeegaths Zeegaths commented Mar 12, 2026

Add cookbook entry for Amount covering the "Amount" section from #4694.

What it covers:

  • Justifies the MAX = 21 million decision
  • Demonstrates creating amounts using constants (MAX, ONE_BTC, ONE_SAT, ZERO, FIFTY_BTC)
  • Explains result handling differences between from_sat_u32 and from_sat
  • Demonstrates parsing amounts from strings using parse
  • Covers formatting with display_in, display_dynamic, and the alloc-gated string methods
  • Shows basic NumOpResult usage with arithmetic and underflow handling

Closes the "Amount" section of #4694.


**Setup**

If using `rust-bitcoin`, `Amount` is re-exported automatically:
Copy link
Member

@apoelstra apoelstra Mar 14, 2026

Choose a reason for hiding this comment

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

In 1ede28e:

I'd drop the word "automatically" and maybe drop the prefix "re-".

@apoelstra
Copy link
Member

Overall this looks amazing, thanks! I just commented a few nits.

@Zeegaths Zeegaths force-pushed the docs-amount-example branch from 1ede28e to e1c8a0c Compare March 15, 2026 13:04
@Zeegaths
Copy link
Author

Overall this looks amazing, thanks! I just commented a few nits.

Thanks for this! made the changes. I could pick up the rest of the docs?

@tcharding
Copy link
Member

Thanks for the contribution.

I could pick up the rest of the docs?

I'll review quite heavily then so that it saves you time on the next ones.

@@ -0,0 +1,168 @@
# Amount

In this section, we will demonstrate different ways of working with Bitcoin amounts using the `Amount` type. The examples in this section will:
Copy link
Member

Choose a reason for hiding this comment

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

I think we should try to use little 'b' for the currency and big 'B' for the network.

let zero = Amount::ZERO;
println!("Zero amount: {} satoshis", zero.to_sat());

// No result handling for small amounts
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// No result handling for small amounts
// No result handling for small amounts.

// Result handling for larger amounts
let large = Amount::from_sat(100_000_000).expect("valid amount");
println!("Large Amount = {}", large);

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps something like this is better?

    // The amount constructor errors if input value is to large.
    let large = Amount::from_sat(100_000_000).expect("valid amount");
    println!("Large Amount = {}", large);

    // Error free construction for amounts under approx 42 BTC ([u32::MAX]).
    let small = Amount::from_sat_u32(50_000);
    println!("Small Amount = {}", small);

let large = Amount::from_sat(100_000_000).expect("valid amount");
println!("Large Amount = {}", large);

// Parsing string type to Amount - result handling needed for potential error
Copy link
Member

Choose a reason for hiding this comment

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

Everything that is code needs back ticks. Here Amount and a bunch of function names elsewhere.

Amount::ONE_BTC.display_in(Denomination::Bitcoin).show_denomination()
);
println!(
"Display in Satoshi with denomination: {}",
Copy link
Member

Choose a reason for hiding this comment

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

Lower case 's' for currency. Also I believe it should be pluralized 'Display in satoshis ...'

println!("Display dynamic: {}", Amount::ONE_SAT.display_dynamic()); // shows in satoshis
println!("Display dynamic: {}", Amount::ONE_BTC.display_dynamic()); // shows in BTC

// to_string_in and to_string_with_denomination require alloc feature
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// to_string_in and to_string_with_denomination require alloc feature
// `to_string_in` and `to_string_with_denomination` both require the `alloc` feature.

**Parsing and Formatting**

When parsing small amounts, result handling is not strictly necessary unless
you want extra caution. We use `.expect()` to handle results for larger amounts.
Copy link
Member

Choose a reason for hiding this comment

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

Did you use an LLM to create these docs or create them yourself?

Comment on lines +146 to +150
When formatting output, the preferred method is `.display_in()`. It can be
combined with `fmt::Formatter` options to precisely control zeros, padding, and
alignment — similar to how floats work in `core`, except that it's more precise,
meaning no rounding occurs. It also works without `alloc`, making it suitable for
`no_std` environments such as hardware wallets or embedded signing devices.
Copy link
Member

Choose a reason for hiding this comment

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

This sounds like LLM output to me and not super useful for a user who is coding at a level that they would read the cookbook.

@@ -0,0 +1,21 @@
Bitcoin is majorly expressed as BTC(bitcoins), mBTC(millibitcoins), sats(satoshis) or bits.
Copy link
Member

@tcharding tcharding Mar 15, 2026

Choose a reason for hiding this comment

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

I'm not keen on this line. Because of the wording: 'majorly express in'. But I rekon better to just mention sats and BTC, but that could be subjective.

Copy link
Member

Choose a reason for hiding this comment

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

I think "usually expressed in" is more idiomotic English.

BTW, these weird expressions make me think this is not LLM output. Though I agree that it often explains things that are too basic to be worth explaining.

Comment on lines +2 to +3
The BTC unit represents a 10^8 value so as to have sub-unit precision instead of large whole numbers.
This allows divisions of 1/10th, 1/100th and so on.
Copy link
Member

Choose a reason for hiding this comment

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

This whole sentence is a bit odd IMO.


The satoshi, named after the original bitcoin creator, Satoshi Nakamoto, is the smallest unit of bitcoin currency representing a hundred millionth of one Bitcoin (0.00000001 BTC).

The Bitcoin source code uses satoshi to specify any Bitcoin amount and all amounts on the blockchain are denominated in satoshi before they get converted for display.
Copy link
Member

Choose a reason for hiding this comment

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

Are we documenting the protocol or the rust-bitcoin lib, its not obvious to me from some of these docs which direction we are going in?

Copy link
Member

Choose a reason for hiding this comment

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

I kinda liked these two sentences. Yes, they're "off-topic" in that they're about the protocol rather than this library, but they're useful context for why we have such a weird and large API for Amount.

@Zeegaths Zeegaths closed this Mar 16, 2026
@apoelstra
Copy link
Member

Sigh. Guess you were right @tcharding

I wonder if it makes sense to move this repo to Forgejo....we hadn't really considered stopping hosting rust-bitcoin.org on Github. On the one hand, self-hosting a website is pretty-much the easiest thing we can do. On the other hand it's still kinda a PITA.

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