Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Make pallet Assets instantiable
Browse files Browse the repository at this point in the history
  • Loading branch information
olanod committed Apr 2, 2021
1 parent 3dc169d commit 080dcf0
Show file tree
Hide file tree
Showing 6 changed files with 1,977 additions and 1,879 deletions.
137 changes: 70 additions & 67 deletions frame/assets/src/extra_mutator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,81 +25,84 @@ 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> {
id: T::AssetId,
who: T::AccountId,
original: T::Extra,
pending: Option<T::Extra>,
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> {
fn drop(&mut self) {
debug_assert!(self.commit().is_ok(), "attempt to write to non-existent asset account");
}
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"
);
}
}

impl<T: Config> sp_std::ops::Deref for ExtraMutator<T> {
type Target = T::Extra;
fn deref(&self) -> &T::Extra {
match self.pending {
Some(ref value) => value,
None => &self.original,
}
}
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 {
Some(ref value) => value,
None => &self.original,
}
}
}

impl<T: Config> sp_std::ops::DerefMut for ExtraMutator<T> {
fn deref_mut(&mut self) -> &mut T::Extra {
if self.pending.is_none() {
self.pending = Some(self.original.clone());
}
self.pending.as_mut().unwrap()
}
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());
}
self.pending.as_mut().unwrap()
}
}

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> {
id,
who: who.borrow().clone(),
original: Account::<T>::get(id, who.borrow()).extra,
pending: None,
})
} else {
None
}
}
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, I>::get(id, who.borrow()).extra,
pending: None,
})
} else {
None
}
}

/// Commit any changes to storage.
pub fn commit(&mut self) -> Result<(), ()> {
if let Some(extra) = self.pending.take() {
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(())
}
}

/// 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|
if let Some(ref mut account) = maybe_account {
account.extra = extra;
Ok(())
} else {
Err(())
}
)
} else {
Ok(())
}
}

/// 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|
if let Some(ref mut account) = maybe_account {
account.extra = self.original.clone();
Ok(())
} else {
Err(())
}
)
}
/// Revert any changes, even those already committed by `self` and drop self.
pub fn revert(mut self) -> Result<(), ()> {
self.pending = None;
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(())
}
})
}
}
Loading

0 comments on commit 080dcf0

Please sign in to comment.