Skip to content

Commit

Permalink
add OracleKey type
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Jun 2, 2023
1 parent 6b386da commit fa71840
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 26 deletions.
2 changes: 1 addition & 1 deletion libs/mocks/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub mod pallet {
}

pub fn mock_feed_value(
f: impl Fn(T::DataId, T::DataElem, T::AccountId) -> DispatchResult + 'static,
f: impl Fn(T::AccountId, T::DataId, T::DataElem) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}
Expand Down
3 changes: 0 additions & 3 deletions libs/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,6 @@ pub mod types {

/// A representation of a loan identifier
pub type LoanId = u64;

/// A representation of a price identifier
pub type PriceId = u64;
}

/// Common constants for all runtimes
Expand Down
1 change: 1 addition & 0 deletions libs/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub mod fixed_point;
pub mod ids;
pub mod investments;
pub mod locations;
pub mod oracles;
pub mod orders;
pub mod permissions;
pub mod time;
Expand Down
36 changes: 36 additions & 0 deletions libs/types/src/oracles.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::RuntimeDebug;
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};

/// [ISIN](https://en.wikipedia.org/wiki/International_Securities_Identification_Number) format.
pub type Isin = [u8; 12];

/// A representation of an oracle price identifier
#[derive(
Encode,
Decode,
Clone,
Copy,
PartialOrd,
Ord,
PartialEq,
Eq,
TypeInfo,
RuntimeDebug,
MaxEncodedLen,
)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum OracleKey {
Isin(Isin),
}

#[cfg(feature = "runtime-benchmarks")]
impl From<u32> for OracleKey {
fn from(value: u32) -> Self {
// Any u32 value always fits into 12 bytes
let isin = Isin::try_from(&(value as u128).to_be_bytes()[0..12]).unwrap();
OracleKey::Isin(isin)
}
}
4 changes: 2 additions & 2 deletions pallets/loans-ref/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,10 @@ where
fn max_benchmark_loans() -> u32 {
// Maximum loans used for benchmaks.
// This can be smaller than `MaxActiveLoansPerPool`,
// to avoid calculate weights during hours if this the loan numbers is too
// to avoid calculate weights during hours if the loan numbers is too
// large. It should be large enough to be able to represent a consistent weight
// function for any number of loans.
T::MaxActiveLoansPerPool::get().max(50)
T::MaxActiveLoansPerPool::get().min(50)
}
}

Expand Down
1 change: 0 additions & 1 deletion pallets/loans-ref/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ pub mod pallet {
type PriceId: Parameter
+ Member
+ MaybeSerializeDeserialize
+ Default
+ TypeInfo
+ Copy
+ MaxEncodedLen;
Expand Down
2 changes: 1 addition & 1 deletion runtime/altair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@ impl pallet_loans_ref::Config for Runtime {
type NonFungible = Uniques;
type Permissions = Permissions;
type Pool = PoolSystem;
type PriceId = PriceId;
type PriceId = u32;
type PriceRegistry = pallet_loans_ref::util::NoPriceRegistry<Runtime>;
type Rate = Rate;
type RuntimeEvent = RuntimeEvent;
Expand Down
2 changes: 1 addition & 1 deletion runtime/centrifuge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1575,7 +1575,7 @@ impl pallet_loans_ref::Config for Runtime {
type NonFungible = Uniques;
type Permissions = Permissions;
type Pool = PoolSystem;
type PriceId = PriceId;
type PriceId = u32;
type PriceRegistry = pallet_loans_ref::util::NoPriceRegistry<Runtime>;
type Rate = Rate;
type RuntimeEvent = RuntimeEvent;
Expand Down
29 changes: 15 additions & 14 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ pub mod xcm {
}

pub mod oracle {
use cfg_primitives::types::{AccountId, Balance, Moment, PriceId};
use cfg_primitives::types::{AccountId, Balance, Moment};
use cfg_types::oracles::OracleKey;
use orml_traits::{CombineData, DataFeeder, DataProvider, DataProviderExtended};
use sp_runtime::DispatchResult;
use sp_std::{marker::PhantomData, vec::Vec};
Expand All @@ -266,9 +267,9 @@ pub mod oracle {
pub struct LastOracleValue;

#[cfg(not(feature = "runtime-benchmarks"))]
impl CombineData<PriceId, OracleValue> for LastOracleValue {
impl CombineData<OracleKey, OracleValue> for LastOracleValue {
fn combine_data(
_: &PriceId,
_: &OracleKey,
values: Vec<OracleValue>,
_: Option<OracleValue>,
) -> Option<OracleValue> {
Expand All @@ -283,14 +284,14 @@ pub mod oracle {
/// and can be removed once they fix this.
pub struct DataProviderBridge<OrmlOracle>(PhantomData<OrmlOracle>);

impl<OrmlOracle: DataProviderExtended<PriceId, OracleValue>>
DataProviderExtended<PriceId, (Balance, Moment)> for DataProviderBridge<OrmlOracle>
impl<OrmlOracle: DataProviderExtended<OracleKey, OracleValue>>
DataProviderExtended<OracleKey, (Balance, Moment)> for DataProviderBridge<OrmlOracle>
{
fn get_no_op(key: &PriceId) -> Option<(Balance, Moment)> {
fn get_no_op(key: &OracleKey) -> Option<(Balance, Moment)> {
OrmlOracle::get_no_op(key).map(|OracleValue { value, timestamp }| (value, timestamp))
}

fn get_all_values() -> Vec<(PriceId, Option<(Balance, Moment)>)> {
fn get_all_values() -> Vec<(OracleKey, Option<(Balance, Moment)>)> {
OrmlOracle::get_all_values()
.into_iter()
.map(|elem| {
Expand All @@ -304,18 +305,18 @@ pub mod oracle {
}
}

impl<OrmlOracle: DataProvider<PriceId, Balance>> DataProvider<PriceId, Balance>
impl<OrmlOracle: DataProvider<OracleKey, Balance>> DataProvider<OracleKey, Balance>
for DataProviderBridge<OrmlOracle>
{
fn get(key: &PriceId) -> Option<Balance> {
fn get(key: &OracleKey) -> Option<Balance> {
OrmlOracle::get(key)
}
}

impl<OrmlOracle: DataFeeder<PriceId, Balance, AccountId>>
DataFeeder<PriceId, Balance, AccountId> for DataProviderBridge<OrmlOracle>
impl<OrmlOracle: DataFeeder<OracleKey, Balance, AccountId>>
DataFeeder<OracleKey, Balance, AccountId> for DataProviderBridge<OrmlOracle>
{
fn feed_value(who: AccountId, key: PriceId, value: Balance) -> DispatchResult {
fn feed_value(who: AccountId, key: OracleKey, value: Balance) -> DispatchResult {
OrmlOracle::feed_value(who, key, value)
}
}
Expand All @@ -330,9 +331,9 @@ pub mod oracle {

use super::*;

impl CombineData<PriceId, OracleValue> for LastOracleValue {
impl CombineData<OracleKey, OracleValue> for LastOracleValue {
fn combine_data(
_: &PriceId,
_: &OracleKey,
_: Vec<OracleValue>,
_: Option<OracleValue>,
) -> Option<OracleValue> {
Expand Down
7 changes: 4 additions & 3 deletions runtime/development/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use cfg_types::{
fixed_point::Rate,
ids::PRICE_ORACLE_PALLET_ID,
locations::Location,
oracles::OracleKey,
permissions::{
PermissionRoles, PermissionScope, PermissionedCurrencyRole, PoolRole, Role, UNION,
},
Expand Down Expand Up @@ -1332,7 +1333,7 @@ impl orml_oracle::Config for Runtime {
#[cfg(feature = "runtime-benchmarks")]
type Members = runtime_common::oracle::benchmarks_util::Members;
type OnNewData = PriceCollector;
type OracleKey = PriceId;
type OracleKey = OracleKey;
type OracleValue = Balance;
type RootOperatorAccountId = RootOperatorOraclePrice;
type RuntimeEvent = RuntimeEvent;
Expand All @@ -1343,7 +1344,7 @@ impl orml_oracle::Config for Runtime {
impl pallet_data_collector::Config for Runtime {
type CollectionId = PoolId;
type Data = Balance;
type DataId = PriceId;
type DataId = OracleKey;
type DataProvider = runtime_common::oracle::DataProviderBridge<PriceOracle>;
type MaxCollectionSize = MaxActiveLoansPerPool;
type MaxCollections = MaxPoolsWithExternalPrices;
Expand All @@ -1362,7 +1363,7 @@ impl pallet_loans_ref::Config for Runtime {
type NonFungible = Uniques;
type Permissions = Permissions;
type Pool = PoolSystem;
type PriceId = PriceId;
type PriceId = OracleKey;
type PriceRegistry = PriceCollector;
type Rate = Rate;
type RuntimeEvent = RuntimeEvent;
Expand Down

0 comments on commit fa71840

Please sign in to comment.