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

[pallet_broker] Fix adapt_price behaviour at zero #3636

Merged
merged 8 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions prdoc/pr_3636.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: "[pallet_broker] Fix `adapt_price` behaviour at zero"
seadanda marked this conversation as resolved.
Show resolved Hide resolved

doc:
- audience: Runtime Dev
description: |
This fixes the behaviour of `Linear` which is the default implementation of the `AdaptPrice`
trait in the broker pallet. Previously if cores were offered but not sold in only one sale,
the price would be set to zero and due to the logic being purely multiplicative, the price
would stay at 0 indefinitely.

crates:
- name: pallet-broker
19 changes: 15 additions & 4 deletions substrate/frame/broker/src/adapt_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use crate::CoreIndex;
use sp_arithmetic::{traits::One, FixedU64};
use sp_runtime::Saturating;

/// Type for determining how to set price.
pub trait AdaptPrice {
Expand Down Expand Up @@ -49,14 +50,24 @@ impl AdaptPrice for () {
pub struct Linear;
impl AdaptPrice for Linear {
fn leadin_factor_at(when: FixedU64) -> FixedU64 {
FixedU64::from(2) - when
FixedU64::from(2).saturating_sub(when)
}
fn adapt_price(sold: CoreIndex, target: CoreIndex, limit: CoreIndex) -> FixedU64 {
if sold <= target {
FixedU64::from_rational(sold.into(), target.into())
// Range of [0.5, 1.0].
FixedU64::from_rational(1, 2).saturating_add(FixedU64::from_rational(
(sold).into(),
(target.saturating_mul(2)).into(),
seadanda marked this conversation as resolved.
Show resolved Hide resolved
))
} else {
FixedU64::one() +
FixedU64::from_rational((sold - target).into(), (limit - target).into())
// Range of (1.0, 2].

// Unchecked math: In this branch we know that sold < target. The limit must be >= sold
seadanda marked this conversation as resolved.
Show resolved Hide resolved
// by construction, and thus target must be > limit.
seadanda marked this conversation as resolved.
Show resolved Hide resolved
FixedU64::one().saturating_add(FixedU64::from_rational(
(sold - target).into(),
(limit - target).into(),
))
}
}
}
Expand Down
Loading