Skip to content

Commit

Permalink
Oracle V2 implementation (#1629)
Browse files Browse the repository at this point in the history
* impl pallet-oracle-feed

* impl pallet-oracle-data-collection

* tech debt: PayFee docs

* attach both pallets to development runtime

* runtime change support for changing feeders

* add AggregationProvider

* fix clippy

* mock for pallet-oracle-feed

* add pallet-oracle-feed tests

* add mock for pallet-oracle-data-collection

* prepare testing for pallet-oracle-data-collection

* add pallet-oracle-data-collection testing

* fix docs

* add benchmarking for pallet_oracle_feed

* add altair & centrifuge runtimes

* complete pallet-oracle-benchmarks

* Benchmarks for pallet-oracle-data-collection

* set maximum weights to oracle-data

* add weights

* add satisfy() method to PreConditions

* fix change guard for benchmarks

* prepare provider initialization

* oracle provider with initial state for benchmarking

* taplo fmt

* benchmark for oracle-data-collection working

* try-runtime support

* fix clippy

* fix docs

* add root support for feeders

* RuntimeOrigin as a feeder source

* last_change_id_for out of benchmark line

* doc Willian suggestions

* testing events

* deposit taken from storage size

* add sp_std dependency to oracle feed

* size deposit to all runtimes

* remove TODO comment
  • Loading branch information
lemunozm authored Dec 21, 2023
1 parent 3191ed1 commit 1552ccf
Show file tree
Hide file tree
Showing 45 changed files with 2,490 additions and 77 deletions.
41 changes: 41 additions & 0 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ members = [
"pallets/migration",
"pallets/nft",
"pallets/nft-sales",
"pallets/oracle-feed",
"pallets/oracle-data-collection",
"pallets/order-book",
"pallets/permissions",
"pallets/pool-system",
Expand Down Expand Up @@ -60,7 +62,6 @@ hex-literal = { version = "0.3.4" }
hex = { version = "0.4.3", default_features = false }
smallvec = "1.6.1"
serde = { version = "1.0.119", features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.0", default-features = false, features = ["derive"] }
parity-scale-codec = { version = "3.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.3.0", default-features = false, features = ["derive"] }
log = "0.4"
Expand Down Expand Up @@ -268,6 +269,8 @@ pallet-loans = { path = "pallets/loans", default-features = false }
pallet-migration-manager = { path = "pallets/migration", default-features = false }
pallet-nft = { path = "pallets/nft", default-features = false }
pallet-nft-sales = { path = "pallets/nft-sales", default-features = false }
pallet-oracle-feed = { path = "pallets/oracle-feed", default-features = false }
pallet-oracle-data-collection = { path = "pallets/oracle-data-collection", default-features = false }
pallet-order-book = { path = "pallets/order-book", default-features = false }
pallet-permissions = { path = "pallets/permissions", default-features = false }
pallet-pool-registry = { path = "pallets/pool-registry", default-features = false }
Expand Down
10 changes: 9 additions & 1 deletion libs/mocks/src/change_guard.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[frame_support::pallet]
pub mod pallet_mock_change_guard {
pub mod pallet {
use cfg_traits::changes::ChangeGuard;
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};
Expand Down Expand Up @@ -49,4 +49,12 @@ pub mod pallet_mock_change_guard {
execute_call!((a, b))
}
}

#[cfg(feature = "runtime-benchmarks")]
impl<T: Config> cfg_traits::benchmarking::PoolBenchmarkHelper for Pallet<T> {
type AccountId = T::AccountId;
type PoolId = T::PoolId;

fn bench_create_pool(_: Self::PoolId, _: &Self::AccountId) {}
}
}
38 changes: 22 additions & 16 deletions libs/mocks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
mod change_guard;
mod currency_conversion;
mod data;
mod fees;
mod investment;
mod liquidity_pools;
mod liquidity_pools_gateway_routers;
mod permissions;
mod pools;
mod rewards;
mod status_notification;
mod time;
mod token_swaps;
mod try_convert;
mod write_off_policy;
pub mod change_guard;
pub mod currency_conversion;
pub mod data;
pub mod fees;
pub mod investment;
pub mod liquidity_pools;
pub mod liquidity_pools_gateway_routers;
pub mod pay_fee;
pub mod permissions;
pub mod pools;
pub mod pre_conditions;
pub mod rewards;
pub mod status_notification;
pub mod time;
pub mod token_swaps;
pub mod try_convert;
pub mod value_provider;
pub mod write_off_policy;

pub use change_guard::pallet_mock_change_guard;
pub use change_guard::pallet as pallet_mock_change_guard;
pub use currency_conversion::pallet as pallet_mock_currency_conversion;
pub use data::pallet as pallet_mock_data;
pub use fees::pallet as pallet_mock_fees;
pub use investment::pallet as pallet_mock_investment;
pub use liquidity_pools::{pallet as pallet_mock_liquidity_pools, MessageMock};
pub use liquidity_pools_gateway_routers::{pallet as pallet_mock_routers, RouterMock};
pub use pay_fee::pallet as pallet_mock_pay_fee;
pub use permissions::pallet as pallet_mock_permissions;
pub use pools::pallet as pallet_mock_pools;
pub use pre_conditions::pallet as pallet_mock_pre_conditions;
pub use rewards::pallet as pallet_mock_rewards;
pub use status_notification::pallet as pallet_mock_status_notification;
pub use time::pallet as pallet_mock_time;
pub use token_swaps::pallet as pallet_mock_token_swaps;
pub use try_convert::pallet as pallet_mock_try_convert;
pub use value_provider::pallet as pallet_mock_value_provider;
pub use write_off_policy::pallet as pallet_mock_write_off_policy;

#[cfg(test)]
Expand Down
32 changes: 32 additions & 0 deletions libs/mocks/src/pay_fee.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#[frame_support::pallet]
pub mod pallet {
use cfg_traits::fees::PayFee;
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};

#[pallet::config]
pub trait Config: frame_system::Config {}

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::storage]
pub(super) type CallIds<T: Config> = StorageMap<
_,
Blake2_128Concat,
<Blake2_128 as frame_support::StorageHasher>::Output,
mock_builder::CallId,
>;

impl<T: Config> Pallet<T> {
pub fn mock_pay(f: impl Fn(&T::AccountId) -> DispatchResult + 'static) {
register_call!(f);
}
}

impl<T: Config> PayFee<T::AccountId> for Pallet<T> {
fn pay(a: &T::AccountId) -> DispatchResult {
execute_call!(a)
}
}
}
8 changes: 8 additions & 0 deletions libs/mocks/src/pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ pub mod pallet {
}
}

#[cfg(feature = "runtime-benchmarks")]
impl<T: Config> cfg_traits::benchmarking::PoolBenchmarkHelper for Pallet<T> {
type AccountId = T::AccountId;
type PoolId = T::PoolId;

fn bench_create_pool(_: Self::PoolId, _: &Self::AccountId) {}
}

#[cfg(feature = "runtime-benchmarks")]
impl<T: Config> cfg_traits::benchmarking::FundedPoolBenchmarkHelper for Pallet<T> {
type AccountId = T::AccountId;
Expand Down
37 changes: 37 additions & 0 deletions libs/mocks/src/pre_conditions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#[frame_support::pallet]
pub mod pallet {
use cfg_traits::PreConditions;
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call_instance, register_call_instance};

#[pallet::config]
pub trait Config<I: 'static = ()>: frame_system::Config {
type Conditions;
type Result;
}

#[pallet::pallet]
pub struct Pallet<T, I = ()>(_);

#[pallet::storage]
pub(super) type CallIds<T: Config<I>, I: 'static = ()> = StorageMap<
_,
Blake2_128Concat,
<Blake2_128 as frame_support::StorageHasher>::Output,
mock_builder::CallId,
>;

impl<T: Config<I>, I: 'static> Pallet<T, I> {
pub fn mock_check(f: impl Fn(T::Conditions) -> T::Result + 'static) {
register_call_instance!(f);
}
}

impl<T: Config<I>, I: 'static> PreConditions<T::Conditions> for Pallet<T, I> {
type Result = T::Result;

fn check(a: T::Conditions) -> T::Result {
execute_call_instance!(a)
}
}
}
40 changes: 40 additions & 0 deletions libs/mocks/src/value_provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#[frame_support::pallet]
pub mod pallet {
use cfg_traits::ValueProvider;
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};

#[pallet::config]
pub trait Config: frame_system::Config {
type Source;
type Key;
type Value;
}

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::storage]
pub(super) type CallIds<T: Config> = StorageMap<
_,
Blake2_128Concat,
<Blake2_128 as frame_support::StorageHasher>::Output,
mock_builder::CallId,
>;

impl<T: Config> Pallet<T> {
pub fn mock_get(
f: impl Fn(&T::Source, &T::Key) -> Result<Option<T::Value>, DispatchError> + 'static,
) {
register_call!(move |(a, b)| f(a, b));
}
}

impl<T: Config> ValueProvider<T::Source, T::Key> for Pallet<T> {
type Value = T::Value;

fn get(a: &T::Source, b: &T::Key) -> Result<Option<Self::Value>, DispatchError> {
execute_call!((a, b))
}
}
}
41 changes: 41 additions & 0 deletions libs/primitives/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use sp_arithmetic::{
ArithmeticError, FixedPointNumber,
};
use sp_runtime::traits::EnsureInto;
use sp_std::cmp::Ordering;

/// Transform a fixed point number to a Balance.
/// The resulting Balance will be represented with the `decimals` given.
Expand Down Expand Up @@ -69,6 +70,46 @@ pub fn fixed_point_to_balance<
new_integer_part.ensure_add(new_frac_part)
}

/// Transform a balance to a fixed point number.
/// Inverse operation of [`fixed_point_to_balance`]
///
/// ```rust
/// use cfg_primitives::conversion::balance_to_fixed_point;
/// # use frame_support::assert_ok;
/// # use sp_arithmetic::fixed_point::FixedU64;
///
/// assert_ok!(balance_to_fixed_point(2_123_456_789, 0), FixedU64::from_inner(2_123_456_789_000_000_000));
/// assert_ok!(balance_to_fixed_point(2_123_456_789, 3), FixedU64::from_inner(2_123_456_789_000_000));
/// assert_ok!(balance_to_fixed_point(2_123_456_789, 6), FixedU64::from_inner(2_123_456_789_000));
/// assert_ok!(balance_to_fixed_point(2_123_456_789, 9), FixedU64::from_inner(2_123_456_789));
/// assert_ok!(balance_to_fixed_point(2_123_456_789, 12), FixedU64::from_inner(2_123_456));
/// assert_ok!(balance_to_fixed_point(2_123_456_789, 15), FixedU64::from_inner(2_123));
/// assert_ok!(balance_to_fixed_point(2_123_456_789, 18), FixedU64::from_inner(2));
/// ```
pub fn balance_to_fixed_point<
FixedPoint: FixedPointNumber<Inner = IntoBalance>,
IntoBalance: BaseArithmetic + Copy,
>(
balance: IntoBalance,
decimals: usize,
) -> Result<FixedPoint, ArithmeticError> {
let magnitude = ensure_pow(IntoBalance::from(10), decimals)?;

let balance = match FixedPoint::DIV.cmp(&magnitude) {
Ordering::Greater => {
let extra = FixedPoint::DIV.ensure_div(magnitude)?;
balance.ensure_mul(extra)?
}
Ordering::Less => {
let extra = magnitude.ensure_div(FixedPoint::DIV)?;
balance.ensure_div(extra)?
}
Ordering::Equal => balance,
};

Ok(FixedPoint::from_inner(balance))
}

/// Converts a `uint` `Balance` of one precision into a `Balance` of another
/// precision i.e:
/// ```rust
Expand Down
Loading

0 comments on commit 1552ccf

Please sign in to comment.