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

Commit 13bea77

Browse files
gavofyorkbkchrshawntabrizi
authored
Implement fungible::* for Balances (#8454)
* Reservable, Transferrable Fungible(s), plus adapters. * Repot into new dir * Imbalances for Fungibles * Repot and balanced fungible. * Clean up names and bridge-over Imbalanced. * Repot frame_support::trait. Finally. * Make build. * Docs * Good errors * Fix tests. Implement fungible::Inspect for Balances. * Implement additional traits for Balances. * Revert UI test "fixes" * Fix UI error * Fix UI test * More work on fungibles * Fixes * More work. * Update lock * Make fungible::reserved work for Balances * Introduce Freezer to Assets, ready for a reserve & locks pallet. Some renaming/refactoring. * Cleanup errors * Imbalances working with Assets * Test for freezer. * Grumbles * Grumbles * Fixes * Extra "side-car" data for a user's asset balance. * Fix * Fix test * Fixes * Line lengths * Comments * Update frame/assets/src/tests.rs Co-authored-by: Shawn Tabrizi <[email protected]> * Update frame/support/src/traits/tokens/fungibles.rs Co-authored-by: Shawn Tabrizi <[email protected]> * Update frame/assets/src/lib.rs Co-authored-by: Shawn Tabrizi <[email protected]> * Update frame/support/src/traits/tokens/fungible.rs Co-authored-by: Shawn Tabrizi <[email protected]> * Introduce `transfer_reserved` * Rename fungible Reserve -> Hold, add flag structs * Avoid the `melted` API - its too complex and gives little help * Repot Assets pallet Co-authored-by: Bastian Köcher <[email protected]> Co-authored-by: Shawn Tabrizi <[email protected]>
1 parent 505a8d6 commit 13bea77

File tree

17 files changed

+1531
-548
lines changed

17 files changed

+1531
-548
lines changed

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node/runtime/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,8 @@ impl pallet_assets::Config for Runtime {
10451045
type MetadataDepositPerByte = MetadataDepositPerByte;
10461046
type ApprovalDeposit = ApprovalDeposit;
10471047
type StringLimit = StringLimit;
1048+
type Freezer = ();
1049+
type Extra = ();
10481050
type WeightInfo = pallet_assets::weights::SubstrateWeight<Runtime>;
10491051
}
10501052

frame/assets/src/extra_mutator.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
//! Datatype for easy mutation of the extra "sidecar" data.
19+
20+
use super::*;
21+
22+
/// A mutator type allowing inspection and possible modification of the extra "sidecar" data.
23+
///
24+
/// This may be used as a `Deref` for the pallet's extra data. If mutated (using `DerefMut`), then
25+
/// any uncommitted changes (see `commit` function) will be automatically committed to storage when
26+
/// dropped. Changes, even after committed, may be reverted to their original values with the
27+
/// `revert` function.
28+
pub struct ExtraMutator<T: Config> {
29+
id: T::AssetId,
30+
who: T::AccountId,
31+
original: T::Extra,
32+
pending: Option<T::Extra>,
33+
}
34+
35+
impl<T: Config> Drop for ExtraMutator<T> {
36+
fn drop(&mut self) {
37+
debug_assert!(self.commit().is_ok(), "attempt to write to non-existent asset account");
38+
}
39+
}
40+
41+
impl<T: Config> sp_std::ops::Deref for ExtraMutator<T> {
42+
type Target = T::Extra;
43+
fn deref(&self) -> &T::Extra {
44+
match self.pending {
45+
Some(ref value) => value,
46+
None => &self.original,
47+
}
48+
}
49+
}
50+
51+
impl<T: Config> sp_std::ops::DerefMut for ExtraMutator<T> {
52+
fn deref_mut(&mut self) -> &mut T::Extra {
53+
if self.pending.is_none() {
54+
self.pending = Some(self.original.clone());
55+
}
56+
self.pending.as_mut().unwrap()
57+
}
58+
}
59+
60+
impl<T: Config> ExtraMutator<T> {
61+
pub(super) fn maybe_new(id: T::AssetId, who: impl sp_std::borrow::Borrow<T::AccountId>)
62+
-> Option<ExtraMutator<T>>
63+
{
64+
if Account::<T>::contains_key(id, who.borrow()) {
65+
Some(ExtraMutator::<T> {
66+
id,
67+
who: who.borrow().clone(),
68+
original: Account::<T>::get(id, who.borrow()).extra,
69+
pending: None,
70+
})
71+
} else {
72+
None
73+
}
74+
}
75+
76+
77+
/// Commit any changes to storage.
78+
pub fn commit(&mut self) -> Result<(), ()> {
79+
if let Some(extra) = self.pending.take() {
80+
Account::<T>::try_mutate_exists(self.id, self.who.borrow(), |maybe_account|
81+
if let Some(ref mut account) = maybe_account {
82+
account.extra = extra;
83+
Ok(())
84+
} else {
85+
Err(())
86+
}
87+
)
88+
} else {
89+
Ok(())
90+
}
91+
}
92+
93+
/// Revert any changes, even those already committed by `self` and drop self.
94+
pub fn revert(mut self) -> Result<(), ()> {
95+
self.pending = None;
96+
Account::<T>::try_mutate_exists(self.id, self.who.borrow(), |maybe_account|
97+
if let Some(ref mut account) = maybe_account {
98+
account.extra = self.original.clone();
99+
Ok(())
100+
} else {
101+
Err(())
102+
}
103+
)
104+
}
105+
}

0 commit comments

Comments
 (0)