Skip to content

Conversation

@ma2bd
Copy link
Contributor

@ma2bd ma2bd commented Nov 15, 2025

Motivation

The Amount can be multiplied by u128 using saturating_mul but we don't have an inverse operation. The existing saturating_div compute a u128 ratio between two amounts.

Proposal

  • rename Amount::saturating_div into saturating_ratio
  • create a new Amount::saturating_div that takes a u128 similar to saturating_mul

Test Plan

CI

Release Plan

  • These changes should be backported to the latest testnet branch, then
    • be released in a new SDK,

Co-authored-by: Andreas Fackler <[email protected]>
Signed-off-by: Mathieu Baudet <[email protected]>
@ma2bd ma2bd enabled auto-merge November 16, 2025 17:07
@ma2bd ma2bd added this pull request to the merge queue Nov 16, 2025
Merged via the queue into linera-io:main with commit 126f2a1 Nov 16, 2025
32 checks passed
@ma2bd ma2bd deleted the saturating_ratio branch November 16, 2025 18:06
ma2bd added a commit that referenced this pull request Nov 17, 2025
## Motivation

The `Amount` can be multiplied by `u128` using `saturating_mul` but we
don't have an inverse operation. The existing `saturating_div` compute a
`u128` ratio between two amounts.

## Proposal

* rename `Amount::saturating_div` into `saturating_ratio`
* create a new `Amount::saturating_div` that takes a u128 similar to
`saturating_mul`

## Test Plan

CI

## Release Plan

- These changes should be backported to the latest `testnet` branch,
then
    - be released in a new SDK,

---------

Signed-off-by: Mathieu Baudet <[email protected]>
Co-authored-by: Andreas Fackler <[email protected]>
github-merge-queue bot pushed a commit that referenced this pull request Nov 18, 2025
## Motivation

* Fixes #841
* Demonstrate how to embed parameters in the "extra" field of `View`s to
help with code structure. (I mentioned this possibility to @kikakkz once
but we had no example to show.)

## Proposal

Allow creators of a market to specify a fixed decimal precision for
prices.
* The amount to be paid for an order is: `amount = quantity *
price/10^precision`.
* If `quantity` (expressed in "attos", the lowest precision for
`Amount`) is not a multiple of 10^precision, then the order is simply
invalid.

We modify the code in several steps:
* add application parameters to storage context
* nit: rename `next_order_number` into `next_order_id`
* rename "amount" into "quantity" + add integration tests
* reorganize the code (moving methods closer to their main object)
* finally, add the notion of "price decimals"

Discussion:
* AFAICT it is very standard for market prices to have fixed precision.
Eventually, `Amount`s should probably have a configurable (reduced)
precision as well.
* Parameters being immutable, copying them in the `extra` field is
pretty harmless. One could also embed the entire runtime using
`Arc<Mutex<ContractRuntime<MyContractState>>` but this is arguably a
tradeoff (e.g. any view can now schedule outgoing messages).

stacked with #4971 and #4972

## Test Plan

CI

## Release Plan

- These changes should be backported to the latest `testnet` branch,
then
    - be released in a new SDK,
ma2bd added a commit to ma2bd/linera-protocol that referenced this pull request Nov 18, 2025
* Fixes linera-io#841
* Demonstrate how to embed parameters in the "extra" field of `View`s to
help with code structure. (I mentioned this possibility to @kikakkz once
but we had no example to show.)

Allow creators of a market to specify a fixed decimal precision for
prices.
* The amount to be paid for an order is: `amount = quantity *
price/10^precision`.
* If `quantity` (expressed in "attos", the lowest precision for
`Amount`) is not a multiple of 10^precision, then the order is simply
invalid.

We modify the code in several steps:
* add application parameters to storage context
* nit: rename `next_order_number` into `next_order_id`
* rename "amount" into "quantity" + add integration tests
* reorganize the code (moving methods closer to their main object)
* finally, add the notion of "price decimals"

Discussion:
* AFAICT it is very standard for market prices to have fixed precision.
Eventually, `Amount`s should probably have a configurable (reduced)
precision as well.
* Parameters being immutable, copying them in the `extra` field is
pretty harmless. One could also embed the entire runtime using
`Arc<Mutex<ContractRuntime<MyContractState>>` but this is arguably a
tradeoff (e.g. any view can now schedule outgoing messages).

stacked with linera-io#4971 and linera-io#4972

CI

- These changes should be backported to the latest `testnet` branch,
then
    - be released in a new SDK,
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.

2 participants