-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Make Assets Pallet's Privileged Roles Option #12579
Changes from 10 commits
c3cb0b4
83189f6
935fa9d
e35b40f
789aa6e
f9799f4
9466b5d
2eb1537
2e0b828
57cb27c
b26bd66
446e256
43eabc4
ae72aae
0de20ab
32c1059
d8a301a
c86a853
8bcedb0
bdbbcdd
a785a08
e155cda
2248192
f2580be
1244d20
aad17d7
9bb7354
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -345,10 +345,10 @@ pub mod pallet { | |
| Asset::<T, I>::insert( | ||
| id, | ||
| AssetDetails { | ||
| owner: owner.clone(), | ||
| issuer: owner.clone(), | ||
| admin: owner.clone(), | ||
| freezer: owner.clone(), | ||
| owner: Some(owner.clone()), | ||
| issuer: Some(owner.clone()), | ||
| admin: Some(owner.clone()), | ||
| freezer: Some(owner.clone()), | ||
| supply: Zero::zero(), | ||
| deposit: Zero::zero(), | ||
| min_balance: *min_balance, | ||
|
|
@@ -562,10 +562,10 @@ pub mod pallet { | |
| Asset::<T, I>::insert( | ||
| id, | ||
| AssetDetails { | ||
| owner: owner.clone(), | ||
| issuer: admin.clone(), | ||
| admin: admin.clone(), | ||
| freezer: admin.clone(), | ||
| owner: Some(owner.clone()), | ||
| issuer: Some(admin.clone()), | ||
| admin: Some(admin.clone()), | ||
| freezer: Some(admin.clone()), | ||
| supply: Zero::zero(), | ||
| deposit, | ||
| min_balance, | ||
|
|
@@ -874,7 +874,7 @@ pub mod pallet { | |
| d.status == AssetStatus::Live || d.status == AssetStatus::Frozen, | ||
| Error::<T, I>::AssetNotLive | ||
| ); | ||
| ensure!(origin == d.freezer, Error::<T, I>::NoPermission); | ||
| ensure!(origin == d.freezer.unwrap(), Error::<T, I>::NoPermission); | ||
|
jsidorenko marked this conversation as resolved.
Outdated
|
||
| let who = T::Lookup::lookup(who)?; | ||
|
|
||
| Account::<T, I>::try_mutate(id, &who, |maybe_account| -> DispatchResult { | ||
|
|
@@ -909,7 +909,7 @@ pub mod pallet { | |
| details.status == AssetStatus::Live || details.status == AssetStatus::Frozen, | ||
| Error::<T, I>::AssetNotLive | ||
| ); | ||
| ensure!(origin == details.admin, Error::<T, I>::NoPermission); | ||
| ensure!(origin == details.admin.unwrap(), Error::<T, I>::NoPermission); | ||
| let who = T::Lookup::lookup(who)?; | ||
|
|
||
| Account::<T, I>::try_mutate(id, &who, |maybe_account| -> DispatchResult { | ||
|
|
@@ -940,7 +940,7 @@ pub mod pallet { | |
| Asset::<T, I>::try_mutate(id, |maybe_details| { | ||
| let d = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?; | ||
| ensure!(d.status == AssetStatus::Live, Error::<T, I>::AssetNotLive); | ||
| ensure!(origin == d.freezer, Error::<T, I>::NoPermission); | ||
| ensure!(origin == d.freezer.clone().unwrap(), Error::<T, I>::NoPermission); | ||
|
|
||
| d.status = AssetStatus::Frozen; | ||
|
|
||
|
|
@@ -967,7 +967,7 @@ pub mod pallet { | |
|
|
||
| Asset::<T, I>::try_mutate(id, |maybe_details| { | ||
| let d = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?; | ||
| ensure!(origin == d.admin, Error::<T, I>::NoPermission); | ||
| ensure!(origin == d.admin.clone().unwrap(), Error::<T, I>::NoPermission); | ||
| ensure!(d.status == AssetStatus::Frozen, Error::<T, I>::NotFrozen); | ||
|
|
||
| d.status = AssetStatus::Live; | ||
|
|
@@ -999,18 +999,25 @@ pub mod pallet { | |
| Asset::<T, I>::try_mutate(id, |maybe_details| { | ||
| let details = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?; | ||
| ensure!(details.status == AssetStatus::Live, Error::<T, I>::LiveAsset); | ||
| ensure!(origin == details.owner, Error::<T, I>::NoPermission); | ||
| if details.owner == owner { | ||
| ensure!(origin == details.owner.clone().unwrap(), Error::<T, I>::NoPermission); | ||
| if details.owner == Some(owner.clone()) { | ||
| return Ok(()) | ||
| } | ||
|
|
||
| let metadata_deposit = Metadata::<T, I>::get(id).deposit; | ||
| let deposit = details.deposit + metadata_deposit; | ||
|
|
||
| // Move the deposit to the new owner. | ||
| T::Currency::repatriate_reserved(&details.owner, &owner, deposit, Reserved)?; | ||
| if details.owner.is_some() { | ||
| T::Currency::repatriate_reserved( | ||
| &details.owner.as_ref().unwrap(), | ||
| &owner, | ||
| deposit, | ||
| Reserved, | ||
| )?; | ||
| } | ||
|
Comment on lines
+1054
to
+1086
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should try to #12951 should help with the possibility of changing deposit rates. |
||
|
|
||
| details.owner = owner.clone(); | ||
| details.owner = Some(owner.clone()); | ||
|
|
||
| Self::deposit_event(Event::OwnerChanged { asset_id: id, owner }); | ||
| Ok(()) | ||
|
|
@@ -1045,11 +1052,11 @@ pub mod pallet { | |
| Asset::<T, I>::try_mutate(id, |maybe_details| { | ||
| let details = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?; | ||
| ensure!(details.status == AssetStatus::Live, Error::<T, I>::AssetNotLive); | ||
| ensure!(origin == details.owner, Error::<T, I>::NoPermission); | ||
| ensure!(origin == details.owner.clone().unwrap(), Error::<T, I>::NoPermission); | ||
|
|
||
| details.issuer = issuer.clone(); | ||
| details.admin = admin.clone(); | ||
| details.freezer = freezer.clone(); | ||
| details.issuer = Some(issuer.clone()); | ||
| details.admin = Some(admin.clone()); | ||
| details.freezer = Some(freezer.clone()); | ||
|
|
||
| Self::deposit_event(Event::TeamChanged { asset_id: id, issuer, admin, freezer }); | ||
| Ok(()) | ||
|
|
@@ -1104,11 +1111,14 @@ pub mod pallet { | |
|
|
||
| let d = Asset::<T, I>::get(id).ok_or(Error::<T, I>::Unknown)?; | ||
| ensure!(d.status == AssetStatus::Live, Error::<T, I>::AssetNotLive); | ||
| ensure!(origin == d.owner, Error::<T, I>::NoPermission); | ||
| ensure!(origin == d.owner.clone().unwrap(), Error::<T, I>::NoPermission); | ||
|
|
||
| Metadata::<T, I>::try_mutate_exists(id, |metadata| { | ||
| let deposit = metadata.take().ok_or(Error::<T, I>::Unknown)?.deposit; | ||
| T::Currency::unreserve(&d.owner, deposit); | ||
|
|
||
| if d.owner.is_some() { | ||
| T::Currency::unreserve(&d.owner.unwrap(), deposit); | ||
| } | ||
| Self::deposit_event(Event::MetadataCleared { asset_id: id }); | ||
| Ok(()) | ||
| }) | ||
|
|
@@ -1188,7 +1198,9 @@ pub mod pallet { | |
| let d = Asset::<T, I>::get(id).ok_or(Error::<T, I>::Unknown)?; | ||
| Metadata::<T, I>::try_mutate_exists(id, |metadata| { | ||
| let deposit = metadata.take().ok_or(Error::<T, I>::Unknown)?.deposit; | ||
| T::Currency::unreserve(&d.owner, deposit); | ||
| if d.owner.is_some() { | ||
| T::Currency::unreserve(&d.owner.unwrap(), deposit); | ||
| } | ||
| Self::deposit_event(Event::MetadataCleared { asset_id: id }); | ||
| Ok(()) | ||
| }) | ||
|
|
@@ -1233,10 +1245,10 @@ pub mod pallet { | |
| Asset::<T, I>::try_mutate(id, |maybe_asset| { | ||
| let mut asset = maybe_asset.take().ok_or(Error::<T, I>::Unknown)?; | ||
| ensure!(asset.status != AssetStatus::Destroying, Error::<T, I>::AssetNotLive); | ||
| asset.owner = T::Lookup::lookup(owner)?; | ||
| asset.issuer = T::Lookup::lookup(issuer)?; | ||
| asset.admin = T::Lookup::lookup(admin)?; | ||
| asset.freezer = T::Lookup::lookup(freezer)?; | ||
| asset.owner = Some(T::Lookup::lookup(owner)?); | ||
| asset.issuer = Some(T::Lookup::lookup(issuer)?); | ||
| asset.admin = Some(T::Lookup::lookup(admin)?); | ||
| asset.freezer = Some(T::Lookup::lookup(freezer)?); | ||
| asset.min_balance = min_balance; | ||
| asset.is_sufficient = is_sufficient; | ||
| if is_frozen { | ||
|
|
@@ -1343,7 +1355,7 @@ pub mod pallet { | |
| .map(|_| ()) | ||
| .or_else(|origin| -> DispatchResult { | ||
| let origin = ensure_signed(origin)?; | ||
| ensure!(origin == d.admin, Error::<T, I>::NoPermission); | ||
| ensure!(Some(origin) == d.admin, Error::<T, I>::NoPermission); | ||
| Ok(()) | ||
| })?; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,10 +44,10 @@ pub mod v1 { | |
| let status = if self.is_frozen { AssetStatus::Frozen } else { AssetStatus::Live }; | ||
|
|
||
| AssetDetails { | ||
| owner: self.owner, | ||
| issuer: self.issuer, | ||
| admin: self.admin, | ||
| freezer: self.freezer, | ||
| owner: Some(self.owner), | ||
| issuer: Some(self.issuer), | ||
| admin: Some(self.admin), | ||
| freezer: Some(self.freezer), | ||
|
Comment on lines
+69
to
+72
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not |
||
| supply: self.supply, | ||
| deposit: self.deposit, | ||
| min_balance: self.min_balance, | ||
|
|
@@ -120,3 +120,100 @@ pub mod v1 { | |
| } | ||
| } | ||
| } | ||
|
|
||
| pub mod v2 { | ||
| use super::*; | ||
| use frame_support::{pallet_prelude::*, weights::Weight}; | ||
|
|
||
| #[derive(Decode)] | ||
| pub struct OldAssetDetails<Balance, AccountId, DepositBalance> { | ||
| pub owner: AccountId, | ||
| pub issuer: AccountId, | ||
| pub admin: AccountId, | ||
| pub freezer: AccountId, | ||
| pub supply: Balance, | ||
| pub deposit: DepositBalance, | ||
| pub min_balance: Balance, | ||
| pub is_sufficient: bool, | ||
| pub accounts: u32, | ||
| pub sufficients: u32, | ||
| pub approvals: u32, | ||
| pub status: AssetStatus, | ||
| } | ||
|
|
||
| impl<Balance, AccountId, DepositBalance> OldAssetDetails<Balance, AccountId, DepositBalance> { | ||
| fn migrate_to_v2(self) -> AssetDetails<Balance, AccountId, DepositBalance> { | ||
| AssetDetails { | ||
| owner: Some(self.owner), | ||
| issuer: Some(self.issuer), | ||
| admin: Some(self.admin), | ||
| freezer: Some(self.freezer), | ||
| supply: self.supply, | ||
| deposit: self.deposit, | ||
| min_balance: self.min_balance, | ||
| is_sufficient: self.is_sufficient, | ||
| accounts: self.accounts, | ||
| sufficients: self.sufficients, | ||
| approvals: self.approvals, | ||
| status: self.status, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub struct MigrateToV2<T>(sp_std::marker::PhantomData<T>); | ||
| impl<T: Config> OnRuntimeUpgrade for MigrateToV2<T> { | ||
| fn on_runtime_upgrade() -> Weight { | ||
| let current_version = Pallet::<T>::current_storage_version(); | ||
| let onchain_version = Pallet::<T>::on_chain_storage_version(); | ||
| if onchain_version == 0 && current_version == 2 { | ||
| let mut translated = 0u64; | ||
| Asset::<T>::translate::< | ||
| OldAssetDetails<T::Balance, T::AccountId, DepositBalanceOf<T>>, | ||
| _, | ||
| >(|_key, old_value| { | ||
| translated.saturating_inc(); | ||
| Some(old_value.migrate_to_v2()) | ||
| }); | ||
| current_version.put::<Pallet<T>>(); | ||
| log::info!(target: "runtime::assets", "Upgraded {} pools, storage to version {:?}", translated, current_version); | ||
|
jsidorenko marked this conversation as resolved.
|
||
| T::DbWeight::get().reads_writes(translated + 1, translated + 1) | ||
| } else { | ||
| log::info!(target: "runtime::assets", "Migration did not execute. This probably should be removed"); | ||
| T::DbWeight::get().reads(1) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "try-runtime")] | ||
| fn pre_upgrade() -> Result<Vec<u8>, &'static str> { | ||
| frame_support::ensure!( | ||
| Pallet::<T>::on_chain_storage_version() == 0, | ||
| "must upgrade linearly" | ||
| ); | ||
| let prev_count = Asset::<T>::iter().count(); | ||
| Ok((prev_count as u32).encode()) | ||
| } | ||
|
|
||
| #[cfg(feature = "try-runtime")] | ||
| fn post_upgrade(prev_count: Vec<u8>) -> Result<(), &'static str> { | ||
| let prev_count: u32 = Decode::decode(&mut prev_count.as_slice()).expect( | ||
| "the state parameter should be something that was generated by pre_upgrade", | ||
| ); | ||
| let post_count = Asset::<T>::iter().count() as u32; | ||
| assert_eq!( | ||
| prev_count, post_count, | ||
| "the asset count before and after the migration should be the same" | ||
| ); | ||
|
|
||
| let current_version = Pallet::<T>::current_storage_version(); | ||
| let onchain_version = Pallet::<T>::on_chain_storage_version(); | ||
|
|
||
| frame_support::ensure!(current_version == 1, "must_upgrade"); | ||
| assert_eq!( | ||
| current_version, onchain_version, | ||
| "after migration, the current_version and onchain_version should be the same" | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.