Skip to content

Prefer using AssetAmount instead of u64 for asset amounts #2781

@PhilippGackstatter

Description

@PhilippGackstatter

We have recently introduced AssetAmount, but we're not using it to its full extent yet. A couple of improvements we can make:

  • impl From<AssetAmount> for Felt so users can infallibly do this conversion without having to expect.
  • We can simplify logic like this then:
    pub fn to_value_word(&self) -> Word {
    Word::new([
    Felt::try_from(u64::from(self.amount))
    .expect("fungible asset should only allow amounts that fit into a felt"),
  • The FungibleAsset::amount API should return AssetAmount instead of u64 so users can take advantage of the guarantees of AssetAmount. This should have a lot of ripple effects to also convert other APIs that deal with asset amounts to use AssetAmount.
  • After PSwap Contract #2636:
    • PswapNote::create_args(account_fill: u64, note_fill: u64) which deals with asset amounts.
    • PswapNote::calculate_output_amount(offered_total: u64, requested_total: u64, fill_amount: u64) -> u64 can use AssetAmount.

These are just examples. We should explore the codebase to see where similar patterns can be improved / simplified.

Metadata

Metadata

Assignees

No one assigned

    Labels

    rustIssues that affect or pull requests that update Rust codestandardsRelated to standard note scripts or account components

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions