From b521e878bbb33eebdb44e1376838803d8b054951 Mon Sep 17 00:00:00 2001 From: ytqaljn <2716693942@qq.com> Date: Wed, 11 Sep 2024 14:13:33 +0800 Subject: [PATCH 1/2] fix: territory calculate price different --- pallets/storage-handler/src/lib.rs | 38 ++++++----------------------- standalone/chain/runtime/src/lib.rs | 2 +- 2 files changed, 8 insertions(+), 32 deletions(-) diff --git a/pallets/storage-handler/src/lib.rs b/pallets/storage-handler/src/lib.rs index 0ab6f929..77dac8be 100644 --- a/pallets/storage-handler/src/lib.rs +++ b/pallets/storage-handler/src/lib.rs @@ -308,7 +308,8 @@ pub mod pallet { TokenId, bool, >; - + + // price / gib / days #[pallet::storage] #[pallet::getter(fn unit_price)] pub(super) type UnitPrice = StorageValue<_, BalanceOf>; @@ -337,7 +338,7 @@ pub mod pallet { #[pallet::genesis_config] pub struct GenesisConfig { - // price / gib / 30days + // price / gib / days pub price: BalanceOf, } @@ -382,16 +383,9 @@ pub mod pallet { ensure!(!>::contains_key(&token), Error::::DuplicateTokens); let space = G_BYTE.checked_mul(gib_count as u128).ok_or(Error::::Overflow)?; - let day_unit_price = >::try_get() - .map_err(|_e| Error::::BugInvalid)? - .checked_div(&30u32.saturated_into()).ok_or(Error::::Overflow)?; Self::storage_territory(token, sender.clone(), space, days, territory_name.clone())?; Self::add_purchased_space(space)?; - let price: BalanceOf = day_unit_price - .checked_mul(&gib_count.saturated_into()) - .ok_or(Error::::Overflow)? - .checked_mul(&days.saturated_into()) - .ok_or(Error::::Overflow)?; + let price = Self::calculate_price(gib_count, days)?; ensure!( ::Currency::can_slash(&sender, price.clone()), @@ -428,9 +422,6 @@ pub mod pallet { ); // The unit price recorded in UnitPrice is the unit price of one month. // Here, the daily unit price is calculated. - let day_unit_price = >::try_get() - .map_err(|_e| Error::::BugInvalid)? - .checked_div(&30u32.saturated_into()).ok_or(Error::::Overflow)?; let space = G_BYTE.checked_mul(gib_count as u128).ok_or(Error::::Overflow)?; //Calculate remaining days. let block_oneday: BlockNumberFor = ::OneDay::get(); @@ -446,13 +437,7 @@ pub mod pallet { .saturated_into(); } //Calculate the final price difference to be made up. - let price: BalanceOf = day_unit_price - .checked_mul(&gib_count.saturated_into()) - .ok_or(Error::::Overflow)? - .checked_mul(&remain_day.saturated_into()) - .ok_or(Error::::Overflow)? - .try_into() - .map_err(|_e| Error::::Overflow)?; + let price = Self::calculate_price(gib_count, remain_day)?; //Judge whether the balance is sufficient ensure!( ::Currency::can_slash(&sender, price.clone()), @@ -490,18 +475,8 @@ pub mod pallet { Error::::StateError, ); - let days_unit_price = >::try_get() - .map_err(|_e| Error::::BugInvalid)? - .checked_div(&30u32.saturated_into()) - .ok_or(Error::::Overflow)?; let gib_count = cur_owned_space.total_space.checked_div(G_BYTE).ok_or(Error::::Overflow)?; - let price: BalanceOf = days_unit_price - .checked_mul(&gib_count.saturated_into()) - .ok_or(Error::::Overflow)? - .checked_mul(&days.saturated_into()) - .ok_or(Error::::Overflow)? - .try_into() - .map_err(|_e| Error::::Overflow)?; + let price = Self::calculate_price(gib_count as u32, days)?; ensure!( ::Currency::can_slash(&sender, price.clone()), @@ -998,6 +973,7 @@ pub mod pallet { impl Pallet { fn calculate_price(gib_count: u32, days: u32) -> Result, DispatchError> { let unit_price: u128 = >::get().unwrap().try_into().map_err(|_| Error::::Overflow)?; + let gib_count: u128 = gib_count.into(); let days: u128 = days.into(); let price = gib_count diff --git a/standalone/chain/runtime/src/lib.rs b/standalone/chain/runtime/src/lib.rs index 9980200a..d7826660 100644 --- a/standalone/chain/runtime/src/lib.rs +++ b/standalone/chain/runtime/src/lib.rs @@ -146,7 +146,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // and set impl_version to 0. If only runtime // implementation changes and behavior does not, then leave spec_version as // is and increment impl_version. - spec_version: 119, + spec_version: 120, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1, From d9a04f22ba67a563cc4c70915bfe083257b61fdd Mon Sep 17 00:00:00 2001 From: ytqaljn <2716693942@qq.com> Date: Wed, 11 Sep 2024 14:29:03 +0800 Subject: [PATCH 2/2] feat: add extrinsic define_update_price --- pallets/storage-handler/src/lib.rs | 12 ++++++++++++ standalone/chain/runtime/src/lib.rs | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pallets/storage-handler/src/lib.rs b/pallets/storage-handler/src/lib.rs index 77dac8be..c8e40f88 100644 --- a/pallets/storage-handler/src/lib.rs +++ b/pallets/storage-handler/src/lib.rs @@ -967,6 +967,18 @@ pub mod pallet { Ok(()) } + + // FOR TEST + #[pallet::call_index(9)] + #[transactional] + #[pallet::weight(Weight::zero())] + pub fn define_update_price(origin: OriginFor, price: u128) -> DispatchResult { + let _ = ensure_root(origin)?; + let default_price: BalanceOf = price.try_into().map_err(|_| Error::::Overflow)?; + UnitPrice::::put(default_price); + + Ok(()) + } } } diff --git a/standalone/chain/runtime/src/lib.rs b/standalone/chain/runtime/src/lib.rs index d7826660..9fea3659 100644 --- a/standalone/chain/runtime/src/lib.rs +++ b/standalone/chain/runtime/src/lib.rs @@ -146,7 +146,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // and set impl_version to 0. If only runtime // implementation changes and behavior does not, then leave spec_version as // is and increment impl_version. - spec_version: 120, + spec_version: 121, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1,