mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 17:31:05 +00:00
Make pallet Assets instantiable (#8483)
* Make pallet Assets instantiable * use instantiable benchmarks Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
@@ -25,20 +25,23 @@ use super::*;
|
||||
/// any uncommitted changes (see `commit` function) will be automatically committed to storage when
|
||||
/// dropped. Changes, even after committed, may be reverted to their original values with the
|
||||
/// `revert` function.
|
||||
pub struct ExtraMutator<T: Config> {
|
||||
pub struct ExtraMutator<T: Config<I>, I: 'static = ()> {
|
||||
id: T::AssetId,
|
||||
who: T::AccountId,
|
||||
original: T::Extra,
|
||||
pending: Option<T::Extra>,
|
||||
}
|
||||
|
||||
impl<T: Config> Drop for ExtraMutator<T> {
|
||||
impl<T: Config<I>, I: 'static> Drop for ExtraMutator<T, I> {
|
||||
fn drop(&mut self) {
|
||||
debug_assert!(self.commit().is_ok(), "attempt to write to non-existent asset account");
|
||||
debug_assert!(
|
||||
self.commit().is_ok(),
|
||||
"attempt to write to non-existent asset account"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config> sp_std::ops::Deref for ExtraMutator<T> {
|
||||
impl<T: Config<I>, I: 'static> sp_std::ops::Deref for ExtraMutator<T, I> {
|
||||
type Target = T::Extra;
|
||||
fn deref(&self) -> &T::Extra {
|
||||
match self.pending {
|
||||
@@ -48,7 +51,7 @@ impl<T: Config> sp_std::ops::Deref for ExtraMutator<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config> sp_std::ops::DerefMut for ExtraMutator<T> {
|
||||
impl<T: Config<I>, I: 'static> sp_std::ops::DerefMut for ExtraMutator<T, I> {
|
||||
fn deref_mut(&mut self) -> &mut T::Extra {
|
||||
if self.pending.is_none() {
|
||||
self.pending = Some(self.original.clone());
|
||||
@@ -57,15 +60,16 @@ impl<T: Config> sp_std::ops::DerefMut for ExtraMutator<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config> ExtraMutator<T> {
|
||||
pub(super) fn maybe_new(id: T::AssetId, who: impl sp_std::borrow::Borrow<T::AccountId>)
|
||||
-> Option<ExtraMutator<T>>
|
||||
{
|
||||
if Account::<T>::contains_key(id, who.borrow()) {
|
||||
Some(ExtraMutator::<T> {
|
||||
impl<T: Config<I>, I: 'static> ExtraMutator<T, I> {
|
||||
pub(super) fn maybe_new(
|
||||
id: T::AssetId,
|
||||
who: impl sp_std::borrow::Borrow<T::AccountId>,
|
||||
) -> Option<ExtraMutator<T, I>> {
|
||||
if Account::<T, I>::contains_key(id, who.borrow()) {
|
||||
Some(ExtraMutator::<T, I> {
|
||||
id,
|
||||
who: who.borrow().clone(),
|
||||
original: Account::<T>::get(id, who.borrow()).extra,
|
||||
original: Account::<T, I>::get(id, who.borrow()).extra,
|
||||
pending: None,
|
||||
})
|
||||
} else {
|
||||
@@ -73,18 +77,17 @@ impl<T: Config> ExtraMutator<T> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Commit any changes to storage.
|
||||
pub fn commit(&mut self) -> Result<(), ()> {
|
||||
if let Some(extra) = self.pending.take() {
|
||||
Account::<T>::try_mutate_exists(self.id, self.who.borrow(), |maybe_account|
|
||||
Account::<T, I>::try_mutate_exists(self.id, self.who.borrow(), |maybe_account| {
|
||||
if let Some(ref mut account) = maybe_account {
|
||||
account.extra = extra;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
)
|
||||
})
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
@@ -93,13 +96,13 @@ impl<T: Config> ExtraMutator<T> {
|
||||
/// Revert any changes, even those already committed by `self` and drop self.
|
||||
pub fn revert(mut self) -> Result<(), ()> {
|
||||
self.pending = None;
|
||||
Account::<T>::try_mutate_exists(self.id, self.who.borrow(), |maybe_account|
|
||||
Account::<T, I>::try_mutate_exists(self.id, self.who.borrow(), |maybe_account| {
|
||||
if let Some(ref mut account) = maybe_account {
|
||||
account.extra = self.original.clone();
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user