Skip to content

Commit

Permalink
Add MinAnswerBound in pallet-oracle #PICA-324 (#3389)
Browse files Browse the repository at this point in the history
  • Loading branch information
obsessed-cake authored Apr 14, 2023
1 parent 96f88cb commit 6631eb8
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 84 deletions.
1 change: 1 addition & 0 deletions code/parachain/frame/composable-traits/src/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub trait Oracle {
type Balance: From<u64>;
type Timestamp;
type LocalAssets: LocalAssets<Self::AssetId>;
type MinAnswerBound: Get<u32>;
type MaxAnswerBound: Get<u32>;
/// Number of prices from history for calculating TWAP and get weighted price.
type TwapWindow: Get<u16>;
Expand Down
26 changes: 14 additions & 12 deletions code/parachain/frame/lending/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,26 @@ smallvec = "1.7.0"
[features]
default = ["std"]
std = [
"serde",
"codec/std",
"log/std",
"composable-traits/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"sp-runtime/std",
"sp-io/std",
"sp-core/std",
"sp-std/std",
"sp-arithmetic/std",
"composable-traits/std",
"log/std",
"orml-tokens/std",
"pallet-balances/std",
"pallet-oracle/std",
"pallet-vault/std",
"scale-info/std",
"pallet-oracle/std",
"xcm/std",
"sp-keystore",
"serde",
"sp-application-crypto",
"frame-benchmarking/std",
"sp-arithmetic/std",
"sp-core/std",
"sp-io/std",
"sp-keystore",
"sp-runtime/std",
"sp-std/std",
"xcm/std",
]

runtime-benchmarks = [
Expand Down
1 change: 1 addition & 0 deletions code/parachain/frame/lending/src/mocks/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ impl pallet_oracle::Config for Runtime {
type StakeLock = MinU64;
type StalePrice = MinU64;
type AddOracle = EnsureSignedBy<RootAccount, AccountId>;
type MinAnswerBound = MinU32;
type MaxAnswerBound = MinU32;
type MaxAssetsCount = MinU32;
type MaxHistory = MinU32;
Expand Down
1 change: 1 addition & 0 deletions code/parachain/frame/lending/src/mocks/offchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ impl pallet_oracle::Config for Runtime {
type StakeLock = MinU64;
type StalePrice = MinU64;
type AddOracle = EnsureSignedBy<RootAccount, AccountId>;
type MinAnswerBound = MinU32;
type MaxAnswerBound = MinU32;
type MaxAssetsCount = MinU32;
type MaxHistory = MinU32;
Expand Down
1 change: 1 addition & 0 deletions code/parachain/frame/oracle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ std = [
"lite-json/std",
"frame-support/std",
"frame-system/std",
"pallet-balances/std",
"sp-runtime/std",
"sp-io/std",
"sp-core/std",
Expand Down
4 changes: 2 additions & 2 deletions code/parachain/frame/oracle/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ benchmarks! {
add_asset_and_info {
let asset_id = 1;
let threshold = Validated::new(Percent::from_percent(80)).unwrap();
let min_answers = Validated::new(3).unwrap();
let max_answers = Validated::new(5).unwrap();
let min_answers = Validated::new(7).unwrap();
let max_answers = Validated::new(15).unwrap();
let block_interval = Validated::<T::BlockNumber, ValidBlockInterval<T::StalePrice>>::new(T::StalePrice::get() + 1u32.into()).unwrap();
let reward: BalanceOf<T> = T::Currency::minimum_balance();
let slash: BalanceOf<T> = T::Currency::minimum_balance();
Expand Down
12 changes: 11 additions & 1 deletion code/parachain/frame/oracle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ pub mod pallet {
type AddOracle: EnsureOrigin<Self::RuntimeOrigin>;
/// Origin to manage rewards
type RewardOrigin: EnsureOrigin<Self::RuntimeOrigin>;
/// Lower bound for min answers for a price
type MinAnswerBound: Get<u32>;
/// Upper bound for max answers for a price
type MaxAnswerBound: Get<u32>;
/// Upper bound for total assets available for the oracle
Expand Down Expand Up @@ -432,6 +434,7 @@ pub mod pallet {
type Balance = T::PriceValue;
type Timestamp = <T as frame_system::Config>::BlockNumber;
type LocalAssets = T::LocalAssets;
type MinAnswerBound = T::MinAnswerBound;
type MaxAnswerBound = T::MaxAnswerBound;
type TwapWindow = T::TwapWindow;

Expand Down Expand Up @@ -517,12 +520,13 @@ pub mod pallet {
/// - `emit_price_changes`: emit PriceChanged event when asset price changes
///
/// Emits `DepositEvent` event when successful.
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::add_asset_and_info())]
pub fn add_asset_and_info(
origin: OriginFor<T>,
asset_id: T::AssetId,
threshold: Validated<Percent, ValidThreshold>,
min_answers: Validated<u32, ValidMinAnswers>,
min_answers: Validated<u32, ValidMinAnswers<T::MinAnswerBound>>,
max_answers: Validated<u32, ValidMaxAnswer<T::MaxAnswerBound>>,
block_interval: Validated<T::BlockNumber, ValidBlockInterval<T::StalePrice>>,
reward_weight: BalanceOf<T>,
Expand Down Expand Up @@ -577,6 +581,7 @@ pub mod pallet {
/// - `signer`: signer to tie controller to
///
/// Emits `SignerSet` and `StakeAdded` events when successful.
#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::set_signer())]
pub fn set_signer(
origin: OriginFor<T>,
Expand Down Expand Up @@ -604,6 +609,7 @@ pub mod pallet {
/// actual ideal number so that the Oracles make a profit under ideal conditions.
///
/// Emits `RewardRateSet` event when successful.
#[pallet::call_index(2)]
#[pallet::weight(T::WeightInfo::adjust_rewards())]
pub fn adjust_rewards(
origin: OriginFor<T>,
Expand Down Expand Up @@ -645,6 +651,7 @@ pub mod pallet {
/// - `stake`: amount to add to stake
///
/// Emits `StakeAdded` event when successful.
#[pallet::call_index(3)]
#[pallet::weight(T::WeightInfo::add_stake())]
pub fn add_stake(origin: OriginFor<T>, stake: BalanceOf<T>) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
Expand All @@ -658,6 +665,7 @@ pub mod pallet {
/// Call to put in a claim to remove stake, called from controller
///
/// Emits `StakeRemoved` event when successful.
#[pallet::call_index(4)]
#[pallet::weight(T::WeightInfo::remove_stake())]
pub fn remove_stake(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
Expand All @@ -676,6 +684,7 @@ pub mod pallet {
/// Call to reclaim stake after proper time has passed, called from controller
///
/// Emits `StakeReclaimed` event when successful.
#[pallet::call_index(5)]
#[pallet::weight(T::WeightInfo::reclaim_stake())]
pub fn reclaim_stake(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
Expand Down Expand Up @@ -703,6 +712,7 @@ pub mod pallet {
/// - `asset_id`: id for the asset
///
/// Emits `PriceSubmitted` event when successful.
#[pallet::call_index(6)]
#[pallet::weight((T::WeightInfo::submit_price(T::MaxAnswerBound::get()), DispatchClass::Operational))]
pub fn submit_price(
origin: OriginFor<T>,
Expand Down
16 changes: 10 additions & 6 deletions code/parachain/frame/oracle/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use frame_support::{
ord_parameter_types,
pallet_prelude::ConstU32,
parameter_types,
traits::{EnsureOneOf, Everything},
traits::{EitherOfDiverse, Everything},
PalletId,
};
use frame_system as system;
Expand Down Expand Up @@ -105,10 +105,11 @@ parameter_types! {
pub const StakeLock: u64 = 1;
pub const MinStake: Balance = 1;
pub const StalePrice: u64 = 2;
pub const MaxAnswerBound: u32 = 5;
pub const MaxAssetsCount: u32 = 2;
pub const MinAnswerBound: u32 = 1;
pub const MaxAnswerBound: u32 = 15;
pub const MaxAssetsCount: u32 = 15;
pub const MaxHistory: u32 = 3;
pub const MaxPrePrices: u32 = 12;
pub const MaxPrePrices: u32 = 20;
pub const TwapWindow: u16 = 3;
}

Expand Down Expand Up @@ -165,8 +166,11 @@ impl pallet_oracle::Config for Test {
type StakeLock = StakeLock;
type StalePrice = StalePrice;
type MinStake = MinStake;
type AddOracle =
EnsureOneOf<EnsureSignedBy<RootAccount, sp_core::sr25519::Public>, EnsureRoot<AccountId>>;
type AddOracle = EitherOfDiverse<
EnsureSignedBy<RootAccount, sp_core::sr25519::Public>,
EnsureRoot<AccountId>,
>;
type MinAnswerBound = MinAnswerBound;
type MaxAnswerBound = MaxAnswerBound;
type MaxAssetsCount = MaxAssetsCount;
type MaxHistory = MaxHistory;
Expand Down
Loading

0 comments on commit 6631eb8

Please sign in to comment.