Skip to content

Commit

Permalink
fix hold reason in restricted tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed May 13, 2024
1 parent 056d942 commit 5940897
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 107 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ members = [
#"pallets/pool-fees",
#"pallets/pool-system",
#"pallets/pool-registry",
#"pallets/restricted-tokens",
"pallets/restricted-tokens",
#"pallets/restricted-xtokens",
"pallets/rewards",
"pallets/swaps",
Expand Down
2 changes: 1 addition & 1 deletion pallets/restricted-tokens/src/impl_fungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub enum FungibleInspectHoldEffects<AccountId, Balance> {
}

impl<T: Config> InspectHold<T::AccountId> for Pallet<T> {
type Reason = ();
type Reason = <T::NativeFungible as fungible::InspectHold<T::AccountId>>::Reason;

fn total_balance_on_hold(who: &T::AccountId) -> Self::Balance {
<T::NativeFungible as InspectHold<T::AccountId>>::total_balance_on_hold(who)
Expand Down
28 changes: 18 additions & 10 deletions pallets/restricted-tokens/src/impl_fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ pub enum FungiblesInspectHoldEffects<AssetId, AccountId, Balance> {
}

impl<T: Config> InspectHold<T::AccountId> for Pallet<T> {
type Reason = ();
type Reason = <T::NativeFungible as fungible::InspectHold<T::AccountId>>::Reason;

fn total_balance_on_hold(asset: Self::AssetId, who: &T::AccountId) -> Self::Balance {
if asset == T::NativeToken::get() {
Expand Down Expand Up @@ -205,7 +205,7 @@ impl<T: Config> InspectHold<T::AccountId> for Pallet<T> {
if asset == T::NativeToken::get() {
<Self as fungible::InspectHold<T::AccountId>>::balance_on_hold(reason, who)
} else {
<T::Fungibles as InspectHold<T::AccountId>>::balance_on_hold(asset, reason, who)
<T::Fungibles as InspectHold<T::AccountId>>::balance_on_hold(asset, &(), who)
}
}

Expand All @@ -214,7 +214,7 @@ impl<T: Config> InspectHold<T::AccountId> for Pallet<T> {
<Self as fungible::InspectHold<T::AccountId>>::hold_available(reason, who)
} else {
let hold_available =
<T::Fungibles as InspectHold<T::AccountId>>::hold_available(asset, reason, who);
<T::Fungibles as InspectHold<T::AccountId>>::hold_available(asset, &(), who);

T::PreFungiblesInspectHold::check(FungiblesInspectHoldEffects::HoldAvailable(
asset,
Expand All @@ -234,7 +234,7 @@ impl<T: Config> InspectHold<T::AccountId> for Pallet<T> {
<Self as fungible::InspectHold<T::AccountId>>::can_hold(reason, who, amount)
} else {
let can_hold =
<T::Fungibles as InspectHold<T::AccountId>>::can_hold(asset, reason, who, amount);
<T::Fungibles as InspectHold<T::AccountId>>::can_hold(asset, &(), who, amount);

T::PreFungiblesInspectHold::check(FungiblesInspectHoldEffects::CanHold(
asset,
Expand Down Expand Up @@ -399,7 +399,10 @@ impl<T: Config> fungibles::hold::Unbalanced<T::AccountId> for Pallet<T> {
);

<T::Fungibles as fungibles::hold::Unbalanced<T::AccountId>>::set_balance_on_hold(
asset, reason, who, amount,
asset,
&(),
who,
amount,
)
}
}
Expand All @@ -424,7 +427,7 @@ impl<T: Config> MutateHold<T::AccountId> for Pallet<T> {
Error::<T>::PreConditionsNotMet
);

<T::Fungibles as MutateHold<T::AccountId>>::hold(asset, reason, who, amount)
<T::Fungibles as MutateHold<T::AccountId>>::hold(asset, &(), who, amount)
}
}

Expand All @@ -448,9 +451,7 @@ impl<T: Config> MutateHold<T::AccountId> for Pallet<T> {
Error::<T>::PreConditionsNotMet
);

<T::Fungibles as MutateHold<T::AccountId>>::release(
asset, reason, who, amount, precision,
)
<T::Fungibles as MutateHold<T::AccountId>>::release(asset, &(), who, amount, precision)
}
}

Expand Down Expand Up @@ -482,7 +483,14 @@ impl<T: Config> MutateHold<T::AccountId> for Pallet<T> {
);

<T::Fungibles as MutateHold<T::AccountId>>::transfer_on_hold(
asset, reason, source, dest, amount, precision, mode, force,
asset,
&(),
source,
dest,
amount,
precision,
mode,
force,
)
}
}
Expand Down
31 changes: 1 addition & 30 deletions pallets/restricted-tokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ pub mod pallet {
+ LockableCurrency<Self::AccountId>
+ ReservableCurrency<Self::AccountId>
+ fungible::Inspect<Self::AccountId, Balance = Self::Balance>
+ fungible::InspectHold<Self::AccountId, Reason = ()>
+ fungible::InspectHold<Self::AccountId>
+ fungible::Mutate<Self::AccountId>
+ fungible::MutateHold<Self::AccountId>;

Expand Down Expand Up @@ -502,15 +502,6 @@ pub mod pallet {
.ok_or(ArithmeticError::Overflow)?;

let token = if T::NativeToken::get() == currency_id {
let old_reserved =
<Self as fungible::InspectHold<T::AccountId>>::balance_on_hold(&(), &who);

<Self as fungible::MutateHold<T::AccountId>>::release(
&(),
&who,
old_reserved,
Precision::Exact,
)?;
let to_burn = <Self as fungible::Inspect<T::AccountId>>::balance(&who);
<Self as fungible::Mutate<T::AccountId>>::burn_from(
&who,
Expand All @@ -519,23 +510,9 @@ pub mod pallet {
Fortitude::Force,
)?;
<Self as fungible::Mutate<T::AccountId>>::mint_into(&who, new_total)?;
<Self as fungible::MutateHold<T::AccountId>>::hold(&(), &who, new_reserved)?;

TokenType::Native
} else {
let old_reserved =
<T::Fungibles as fungibles::InspectHold<T::AccountId>>::balance_on_hold(
currency_id,
&(),
&who,
);
<T::Fungibles as fungibles::MutateHold<T::AccountId>>::release(
currency_id,
&(),
&who,
old_reserved,
Precision::Exact,
)?;
let to_burn =
<T::Fungibles as fungibles::Inspect<T::AccountId>>::balance(currency_id, &who);
<T::Fungibles as fungibles::Mutate<T::AccountId>>::burn_from(
Expand All @@ -550,12 +527,6 @@ pub mod pallet {
&who,
new_total,
)?;
<T::Fungibles as fungibles::MutateHold<T::AccountId>>::hold(
currency_id,
&(),
&who,
new_reserved,
)?;

TokenType::Other
};
Expand Down
25 changes: 11 additions & 14 deletions pallets/restricted-tokens/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
// GNU General Public License for more details.

use cfg_traits::PreConditions;
use frame_support::traits::VariantCount;
use frame_support::{derive_impl, parameter_types};
use orml_traits::parameter_type_with_key;
use pallet_restricted_tokens::TransferDetails;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_runtime::{traits::ConstU32, BuildStorage};
Expand Down Expand Up @@ -349,17 +352,7 @@ mod filter {
}

#[derive(
parity_scale_codec::Encode,
parity_scale_codec::Decode,
Clone,
Copy,
Debug,
PartialOrd,
Ord,
PartialEq,
Eq,
scale_info::TypeInfo,
parity_scale_codec::MaxEncodedLen,
Encode, Decode, Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq, TypeInfo, MaxEncodedLen,
)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum CurrencyId {
Expand Down Expand Up @@ -389,13 +382,17 @@ parameter_types! {
pub const ExistentialDeposit: u64 = 1;
}

#[derive(Encode, Decode, TypeInfo, MaxEncodedLen, Debug, Copy, PartialEq, Eq, Clone)]
pub struct HoldReason;
impl VariantCount for HoldReason {
const VARIANT_COUNT: u32 = 1;
}

#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig as pallet_balances::DefaultConfig)]
impl pallet_balances::Config for Runtime {
type AccountStore = System;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type MaxHolds = ConstU32<1>;
type RuntimeHoldReason = ();
type RuntimeHoldReason = HoldReason;
}

parameter_type_with_key! {
Expand Down
Loading

0 comments on commit 5940897

Please sign in to comment.