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

Loans: Add oracles to development runtime #1377

Merged
merged 25 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
8 changes: 8 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions libs/mocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ sp-std = { git = "https://github.com/paritytech/substrate", default-features = f
cfg-primitives = { path = "../primitives", default-features = false }
cfg-traits = { path = "../traits", default-features = false }
cfg-types = { path = "../types", default-features = false }
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", default-features = false, branch = "polkadot-v0.9.38" }

mock-builder = { path = "../../libs/mock-builder" }

[features]
Expand All @@ -39,6 +41,7 @@ std = [
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"orml-traits/std",
]
runtime-benchmarks = [
"frame-support/runtime-benchmarks",
Expand Down
20 changes: 20 additions & 0 deletions libs/mocks/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ pub mod pallet {
use cfg_traits::data::{DataCollection, DataRegistry};
use frame_support::pallet_prelude::*;
use mock_builder::{execute_call, register_call};
use orml_traits::{DataFeeder, DataProvider};

#[pallet::config]
pub trait Config: frame_system::Config {
type DataId;
type CollectionId;
type Collection: DataCollection<Self::DataId>;
type Data;
type DataElem;
#[cfg(feature = "runtime-benchmarks")]
type MaxCollectionSize: Get<u32>;
}
Expand Down Expand Up @@ -46,6 +48,12 @@ pub mod pallet {
) {
register_call!(move |(a, b)| f(a, b));
}

pub fn mock_feed_value(
f: impl Fn(T::AccountId, T::DataId, T::DataElem) -> DispatchResult + 'static,
) {
register_call!(move |(a, b, c)| f(a, b, c));
}
}

impl<T: Config> DataRegistry<T::DataId, T::CollectionId> for Pallet<T> {
Expand All @@ -71,6 +79,18 @@ pub mod pallet {
}
}

impl<T: Config> DataProvider<T::DataId, T::DataElem> for Pallet<T> {
fn get(a: &T::DataId) -> Option<T::DataElem> {
execute_call!(a)
}
}

impl<T: Config> DataFeeder<T::DataId, T::DataElem, T::AccountId> for Pallet<T> {
fn feed_value(a: T::AccountId, b: T::DataId, c: T::DataElem) -> DispatchResult {
execute_call!((a, b, c))
}
}

#[cfg(feature = "std")]
pub mod util {
use super::*;
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/traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub mod ops;
/// Traits related to rewards.
pub mod rewards;

/// Traits related to data registry & collections.
pub mod data;

/// A trait used for loosely coupling the claim pallet with a reward mechanism.
Expand Down
2 changes: 1 addition & 1 deletion libs/types/src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ impl<InvestmentId> TypeId for InvestmentAccount<InvestmentId> {

// Pallet-Ids that define pallets accounts
pub const POOLS_PALLET_ID: PalletId = PalletId(*b"roc/pool");
pub const LOANS_PALLET_ID: PalletId = PalletId(*b"roc/loan");
pub const CHAIN_BRIDGE_PALLET_ID: PalletId = PalletId(*b"chnbrdge");
pub const CLAIMS_PALLET_ID: PalletId = PalletId(*b"p/claims");
pub const CROWDLOAN_REWARD_PALLET_ID: PalletId = PalletId(*b"cc/rewrd");
Expand All @@ -33,6 +32,7 @@ pub const TREASURY_PALLET_ID: PalletId = PalletId(*b"py/trsry");
pub const NFT_SALES_PALLET_ID: PalletId = PalletId(*b"pal/nfts");
pub const STAKE_POT_PALLET_ID: PalletId = PalletId(*b"PotStake");
pub const BLOCK_REWARDS_PALLET_ID: PalletId = PalletId(*b"cfg/blrw");
pub const PRICE_ORACLE_PALLET_ID: PalletId = PalletId(*b"or/price");

// Other ids
pub const CHAIN_BRIDGE_HASH_ID: [u8; 13] = *b"cent_nft_hash";
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)
}
}
14 changes: 14 additions & 0 deletions pallets/data-collector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-li

cfg-traits = { path = "../../libs/traits", default-features = false }

# Optionals for benchmarking
frame-benchmarking = { git = "https://github.com/paritytech/substrate", default-features = false, optional = true, branch = "polkadot-v0.9.38" }

[dev-dependencies]
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
Expand All @@ -43,7 +46,18 @@ std = [
"sp-std/std",
"cfg-traits/std",
"orml-traits/std",
"frame-benchmarking/std",
]
runtime-benchmarks = [
"frame-benchmarking",
"cfg-traits/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
"cfg-traits/try-runtime",
]
38 changes: 32 additions & 6 deletions pallets/data-collector/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ mod tests;
pub mod pallet {
use cfg_traits::data::{DataCollection, DataRegistry};
use frame_support::{pallet_prelude::*, storage::bounded_btree_map::BoundedBTreeMap};
use orml_traits::{DataProviderExtended, OnNewData};
use orml_traits::{DataFeeder, DataProvider, DataProviderExtended, OnNewData};
use sp_runtime::{
traits::{EnsureAddAssign, EnsureSubAssign},
DispatchError,
DispatchError, DispatchResult,
};

type DataValueOf<T, I> = (<T as Config<I>>::Data, <T as Config<I>>::Moment);
Expand Down Expand Up @@ -130,8 +130,10 @@ pub mod pallet {
.map_err(|_| Error::<T, I>::MaxCollectionNumber)?;

Collection::<T, I>::try_mutate(collection_id, |collection| {
let data =
<Self as DataRegistry<T::DataId, T::CollectionId>>::get(data_id)?;
collection
.try_insert(data_id.clone(), Self::get(data_id)?)
.try_insert(data_id.clone(), data)
.map(|_| ())
.map_err(|_| Error::<T, I>::MaxCollectionSize.into())
})
Expand Down Expand Up @@ -166,16 +168,40 @@ pub mod pallet {
// for Data values.
for collection_id in Listening::<T, I>::get(data_id).keys() {
Collection::<T, I>::mutate(collection_id, |collection| {
if let (Some(value), Ok(new_value)) =
(collection.get_mut(data_id), Self::get(data_id))
{
let data = <Self as DataRegistry<T::DataId, T::CollectionId>>::get(data_id);
if let (Some(value), Ok(new_value)) = (collection.get_mut(data_id), data) {
*value = new_value;
}
});
}
}
}

// This implementation can be removed once:
// <https://github.com/open-web3-stack/open-runtime-module-library/pull/920> be merged,
// and consecuently, get() call methods simplified.
impl<T: Config<I>, I: 'static> DataProvider<T::DataId, T::Data> for Pallet<T, I>
where
T::DataProvider: DataProvider<T::DataId, T::Data>,
{
fn get(key: &T::DataId) -> Option<T::Data> {
T::DataProvider::get(key)
}
}

impl<T: Config<I>, I: 'static> DataFeeder<T::DataId, T::Data, T::AccountId> for Pallet<T, I>
where
T::DataProvider: DataFeeder<T::DataId, T::Data, T::AccountId>,
{
fn feed_value(
account_id: T::AccountId,
data_id: T::DataId,
data: T::Data,
) -> DispatchResult {
T::DataProvider::feed_value(account_id, data_id, data)
}
}

/// A collection cached in memory
pub struct CachedCollection<T: Config<I>, I: 'static = ()>(
BoundedBTreeMap<T::DataId, DataValueOf<T, I>, T::MaxCollectionSize>,
Expand Down
5 changes: 4 additions & 1 deletion pallets/loans-ref/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ sp-std = { git = "https://github.com/paritytech/substrate", default-features = f
cfg-primitives = { path = "../../libs/primitives", default-features = false }
cfg-traits = { path = "../../libs/traits", default-features = false }
cfg-types = { path = "../../libs/types", default-features = false }
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", default-features = false, branch = "polkadot-v0.9.38" }

strum = { version = "0.24", default-features = false, features = ["derive"] }

# Optionals for benchamarking
# Optionals for benchmarking
frame-benchmarking = { git = "https://github.com/paritytech/substrate", default-features = false, optional = true, branch = "polkadot-v0.9.38" }

# Used for migrations (no longer needed once migratios is done)
Expand Down Expand Up @@ -61,6 +62,7 @@ std = [
"frame-benchmarking/std",
"sp-io/std",
"strum/std",
"orml-traits/std",
]
runtime-benchmarks = [
"frame-benchmarking",
Expand All @@ -76,6 +78,7 @@ runtime-benchmarks = [
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"sp-runtime/try-runtime",
"cfg-primitives/try-runtime",
"cfg-traits/try-runtime",
"cfg-types/try-runtime",
Expand Down
Loading