Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into 1025-rc1
Browse files Browse the repository at this point in the history
  • Loading branch information
wischli committed Feb 29, 2024
2 parents 947f61b + 10aed20 commit 013d7f1
Show file tree
Hide file tree
Showing 19 changed files with 602 additions and 104 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions libs/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pallet-aura = { workspace = true }
pallet-timestamp = { workspace = true }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
sp-arithmetic = { workspace = true }
sp-consensus-aura = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
Expand All @@ -30,6 +31,7 @@ std = [
"frame-support/std",
"frame-system/std",
"sp-runtime/std",
"sp-arithmetic/std",
"sp-std/std",
"pallet-timestamp/std",
"pallet-aura/std",
Expand Down
95 changes: 95 additions & 0 deletions libs/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,101 @@ pub fn decode_var_source<const EXPECTED_SOURCE_ADDRESS_SIZE: usize>(
}
}

pub mod math {
use sp_arithmetic::{
traits::{BaseArithmetic, EnsureFixedPointNumber},
ArithmeticError, FixedPointOperand, FixedU128,
};

/// Returns the coordinate `y` for coordinate `x`,
/// in a function given by 2 points: (x1, y1) and (x2, y2)
pub fn y_coord_in_rect<X, Y>(
(x1, y1): (X, Y),
(x2, y2): (X, Y),
x: X,
) -> Result<Y, ArithmeticError>
where
X: BaseArithmetic + FixedPointOperand,
Y: BaseArithmetic + FixedPointOperand,
{
// From the equation: (x - x1) / (x2 - x1) == (y - y1) / (y2 - y1) we solve y:
//
// NOTE: With rects that have x or y negative directions, we emulate a
// symmetry in those axis to avoid unsigned underflows in substractions. It
// means, we first "convert" the rect into an increasing rect, and in such rect,
// we find the y coordinate.

let left = if x1 <= x2 {
FixedU128::ensure_from_rational(x.ensure_sub(x1)?, x2.ensure_sub(x1)?)?
} else {
// X symmetry emulation
FixedU128::ensure_from_rational(x1.ensure_sub(x)?, x1.ensure_sub(x2)?)?
};

if y1 <= y2 {
left.ensure_mul_int(y2.ensure_sub(y1)?)?.ensure_add(y1)
} else {
// Y symmetry emulation
y1.ensure_sub(left.ensure_mul_int(y1.ensure_sub(y2)?)?)
}
}

#[cfg(test)]
mod test_y_coord_in_function_with_2_points {
use super::*;

#[test]
fn start_point() {
assert_eq!(y_coord_in_rect::<u32, u32>((3, 12), (7, 24), 3), Ok(12));
}

#[test]
fn end_point() {
assert_eq!(y_coord_in_rect::<u32, u32>((3, 12), (7, 24), 7), Ok(24));
}

// Rect defined as:
// (x2, y2)
// /
// /
// (x1, y1)
#[test]
fn inner_point() {
assert_eq!(y_coord_in_rect::<u32, u32>((3, 12), (7, 24), 4), Ok(15));
}

// Rect defined as:
// (x2, y2)
// \
// \
// (x1, y1)
#[test]
fn inner_point_with_greater_x1() {
assert_eq!(y_coord_in_rect::<u32, u32>((7, 12), (3, 24), 4), Ok(21));
}

// Rect defined as:
// (x1, y1)
// \
// \
// (x2, y2)
#[test]
fn inner_point_with_greater_y1() {
assert_eq!(y_coord_in_rect::<u32, u32>((3, 24), (7, 12), 4), Ok(21));
}

// Rect defined as:
// (x1, y1)
// /
// /
// (x2, y2)
#[test]
fn inner_point_with_greater_x1y1() {
assert_eq!(y_coord_in_rect::<u32, u32>((7, 24), (3, 12), 4), Ok(15));
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 4 additions & 0 deletions pallets/loans/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sp-std = { workspace = true }
cfg-primitives = { workspace = true }
cfg-traits = { workspace = true }
cfg-types = { workspace = true }
cfg-utils = { workspace = true }
orml-traits = { workspace = true }

strum = { workspace = true }
Expand Down Expand Up @@ -56,6 +57,7 @@ std = [
"cfg-primitives/std",
"cfg-traits/std",
"cfg-types/std",
"cfg-utils/std",
"frame-benchmarking/std",
"strum/std",
"orml-traits/std",
Expand All @@ -68,6 +70,7 @@ runtime-benchmarks = [
"cfg-primitives/runtime-benchmarks",
"cfg-traits/runtime-benchmarks",
"cfg-types/runtime-benchmarks",
"cfg-utils/runtime-benchmarks",
"pallet-uniques/runtime-benchmarks",
"cfg-mocks/runtime-benchmarks",
]
Expand All @@ -78,6 +81,7 @@ try-runtime = [
"cfg-primitives/try-runtime",
"cfg-traits/try-runtime",
"cfg-types/try-runtime",
"cfg-utils/try-runtime",
"cfg-mocks/try-runtime",
"sp-runtime/try-runtime",
]
1 change: 1 addition & 0 deletions pallets/loans/docs/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ package pricing {
outstanding_quantity: Rate,
interest: ActiveInterestRate
latest_settlement_price: Balance,
settlement_price_updated: Seconds,
}
ExternalActivePricing *-r-> ActiveInterestRate
Expand Down
10 changes: 9 additions & 1 deletion pallets/loans/src/entities/input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use frame_support::RuntimeDebugNoBound;
use frame_support::{storage::bounded_btree_map::BoundedBTreeMap, RuntimeDebugNoBound};
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::{ArithmeticError, DispatchError};
Expand Down Expand Up @@ -56,3 +56,11 @@ impl<T: Config> RepaidInput<T> {
})
}
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, RuntimeDebugNoBound, MaxEncodedLen)]
#[scale_info(skip_type_params(T))]
pub enum PriceCollectionInput<T: Config> {
Empty,
Custom(BoundedBTreeMap<T::PriceId, T::Balance, T::MaxActiveLoansPerPool>),
FromRegistry,
}
Loading

0 comments on commit 013d7f1

Please sign in to comment.