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

fix asset count to change an existing asset #481

Merged
merged 6 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
27 changes: 17 additions & 10 deletions frame/oracle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ pub mod pallet {
type StalePrice: Get<Self::BlockNumber>;
/// Origin to add new price types
type AddOracle: EnsureOrigin<Self::Origin>;
/// Slash for an incorrect answer
type SlashAmount: Get<BalanceOf<Self>>;
/// 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 @@ -269,7 +267,7 @@ pub mod pallet {
Blake2_128Concat,
T::AssetId,
AssetInfo<Percent, T::BlockNumber, BalanceOf<T>>,
ValueQuery,
OptionQuery,
>;

#[pallet::event]
Expand Down Expand Up @@ -427,8 +425,13 @@ pub mod pallet {
);
let asset_info =
AssetInfo { threshold, min_answers, max_answers, block_interval, reward, slash };

let current_asset_info = Self::asset_info(asset_id);
if current_asset_info.is_none() {
AssetsCount::<T>::mutate(|a| *a += 1);
}

AssetsInfo::<T>::insert(asset_id, asset_info);
AssetsCount::<T>::mutate(|a| *a += 1);
Self::deposit_event(Event::AssetInfoChange(
asset_id,
threshold,
Expand Down Expand Up @@ -550,7 +553,7 @@ pub mod pallet {
block: frame_system::Pallet::<T>::block_number(),
who: who.clone(),
};
let asset_info = AssetsInfo::<T>::get(asset_id);
let asset_info = Self::asset_info(asset_id).ok_or(Error::<T>::InvalidAssetId)?;
PrePrices::<T>::try_mutate(asset_id, |current_prices| -> Result<(), DispatchError> {
// There can convert current_prices.len() to u32 safely
// because current_prices.len() limited by u32
Expand Down Expand Up @@ -612,15 +615,15 @@ pub mod pallet {
price: T::PriceValue,
asset_id: T::AssetId,
) {
let asset_info = Self::asset_info(asset_id);
let asset_info = Self::asset_info(asset_id).unwrap_or_default();
hussein-aitlahcen marked this conversation as resolved.
Show resolved Hide resolved
for answer in pre_prices {
let accuracy: Percent = if answer.price < price {
PerThing::from_rational(answer.price, price)
} else {
let adjusted_number = price.saturating_sub(answer.price - price);
PerThing::from_rational(adjusted_number, price)
};
let min_accuracy = AssetsInfo::<T>::get(asset_id).threshold;
let min_accuracy = asset_info.threshold;
if accuracy < min_accuracy {
let slash_amount = asset_info.slash;
let try_slash = T::Currency::can_slash(&answer.who, slash_amount);
Expand Down Expand Up @@ -800,11 +803,15 @@ pub mod pallet {
let last_update = Self::prices(price_id);
let current_block = frame_system::Pallet::<T>::block_number();
let asset_info = Self::asset_info(price_id);
last_update.block + asset_info.block_interval < current_block
if asset_info.is_none() {
false
} else {
last_update.block + asset_info.unwrap_or_default().block_interval < current_block
}
}

pub fn remove_price_in_transit(asset_id: &T::AssetId, who: &T::AccountId) {
let asset_info = AssetsInfo::<T>::get(asset_id);
let asset_info = Self::asset_info(asset_id).unwrap_or_default();
hussein-aitlahcen marked this conversation as resolved.
Show resolved Hide resolved
AnswerInTransit::<T>::mutate(&who, |transit| {
*transit = Some(transit.unwrap_or_else(Zero::zero).saturating_sub(asset_info.slash))
});
Expand Down Expand Up @@ -877,7 +884,7 @@ pub mod pallet {
let mut to32 = AccountId32::as_ref(&account);
let address: T::AccountId = T::AccountId::decode(&mut to32).unwrap_or_default();

if prices.len() as u32 >= Self::asset_info(price_id).max_answers {
if prices.len() as u32 >= Self::asset_info(price_id).unwrap_or_default().max_answers {
hussein-aitlahcen marked this conversation as resolved.
Show resolved Hide resolved
log::info!("Max answers reached");
return Err("Max answers reached")
}
Expand Down
2 changes: 0 additions & 2 deletions frame/oracle/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ parameter_types! {
pub const StakeLock: u64 = 1;
pub const MinStake: u64 = 1;
pub const StalePrice: u64 = 2;
pub const SlashAmount: u64 = 5;
pub const MaxAnswerBound: u32 = 5;
pub const MaxAssetsCount: u32 = 2;
pub const MaxHistory: u32 = 3;
Expand Down Expand Up @@ -128,7 +127,6 @@ impl pallet_oracle::Config for Test {
type StalePrice = StalePrice;
type MinStake = MinStake;
type AddOracle = EnsureSignedBy<RootAccount, sp_core::sr25519::Public>;
type SlashAmount = SlashAmount;
type MaxAnswerBound = MaxAnswerBound;
type MaxAssetsCount = MaxAssetsCount;
type MaxHistory = MaxHistory;
Expand Down
18 changes: 16 additions & 2 deletions frame/oracle/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ fn add_asset_and_info() {
SLASH
));

// does not increment if exists
assert_ok!(Oracle::add_asset_and_info(
Origin::signed(account_2),
ASSET_ID,
THRESHOLD,
MIN_ANSWERS,
MAX_ANSWERS,
BLOCK_INTERVAL,
REWARD,
SLASH
));
assert_eq!(Oracle::assets_count(), 1);

assert_ok!(Oracle::add_asset_and_info(
Origin::signed(account_2),
ASSET_ID + 1,
Expand All @@ -62,8 +75,9 @@ fn add_asset_and_info() {
slash: SLASH,
};
// id now activated and count incremented
assert_eq!(Oracle::asset_info(1), asset_info);
assert_eq!(Oracle::asset_info(1), Some(asset_info));
assert_eq!(Oracle::assets_count(), 2);

// fails with non permission
let account_1: AccountId = Default::default();
assert_noop!(
Expand Down Expand Up @@ -345,7 +359,7 @@ fn add_price() {
// non existent asset_id
assert_noop!(
Oracle::submit_price(Origin::signed(account_1), 100_u128, 10_u128),
Error::<Test>::MaxPrices
Error::<Test>::PriceNotRequested
);
});
}
Expand Down
3 changes: 0 additions & 3 deletions runtime/dali/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,6 @@ parameter_types! {

/// TODO: discuss with omar/cosmin
pub MinStake: Balance = 1000 * CurrencyId::PICA.unit::<Balance>();
// Shouldn't this be a ratio based on locked amount?
pub const SlashAmount: Balance = 5;
pub const MaxAnswerBound: u32 = 25;
pub const MaxAssetsCount: u32 = 100_000;
pub const MaxHistory: u32 = 20;
Expand All @@ -436,7 +434,6 @@ impl oracle::Config for Runtime {
type MinStake = MinStake;
type StalePrice = StalePrice;
type AddOracle = EnsureRootOrHalfCouncil;
type SlashAmount = SlashAmount;
type MaxAnswerBound = MaxAnswerBound;
type MaxAssetsCount = MaxAssetsCount;
type MaxHistory = MaxHistory;
Expand Down
2 changes: 0 additions & 2 deletions runtime/picasso/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,6 @@ parameter_types! {

/// TODO: discuss with omar/cosmin
pub MinStake: Balance = 1000 * CurrencyId::PICA.unit::<Balance>();
// Shouldn't this be a ratio based on locked amount?
pub const SlashAmount: Balance = 5;
pub const MaxAnswerBound: u32 = 25;
pub const MaxAssetsCount: u32 = 100_000;
pub const MaxHistory: u32 = 20;
Expand Down