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

Refactor nft fractionalisation pallet #13008

Merged
merged 24 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,886 changes: 997 additions & 889 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ members = [
"frame/proxy",
"frame/message-queue",
"frame/nfts",
"frame/nft-fractionalisation",
"frame/nft-fractionalization",
"frame/nomination-pools",
"frame/nomination-pools/fuzzer",
"frame/nomination-pools/benchmarking",
Expand Down
7 changes: 4 additions & 3 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pallet-message-queue = { version = "7.0.0-dev", default-features = false, path =
pallet-mmr = { version = "4.0.0-dev", default-features = false, path = "../../../frame/merkle-mountain-range" }
pallet-multisig = { version = "4.0.0-dev", default-features = false, path = "../../../frame/multisig" }
pallet-nfts = { version = "4.0.0-dev", default-features = false, path = "../../../frame/nfts" }
pallet-nft-fractionalisation = { version = "4.0.0-dev", default-features = false, path = "../../../frame/nft-fractionalisation" }
pallet-nft-fractionalization = { version = "4.0.0-dev", default-features = false, path = "../../../frame/nft-fractionalization" }
pallet-nomination-pools = { version = "1.0.0", default-features = false, path = "../../../frame/nomination-pools"}
pallet-nomination-pools-benchmarking = { version = "1.0.0", default-features = false, optional = true, path = "../../../frame/nomination-pools/benchmarking" }
pallet-nomination-pools-runtime-api = { version = "1.0.0-dev", default-features = false, path = "../../../frame/nomination-pools/runtime-api" }
Expand Down Expand Up @@ -200,7 +200,7 @@ std = [
"pallet-recovery/std",
"pallet-uniques/std",
"pallet-nfts/std",
"pallet-nft-fractionalisation/std",
"pallet-nft-fractionalization/std",
"pallet-vesting/std",
"log/std",
"frame-try-runtime?/std",
Expand Down Expand Up @@ -258,6 +258,7 @@ runtime-benchmarks = [
"pallet-utility/runtime-benchmarks",
"pallet-uniques/runtime-benchmarks",
"pallet-nfts/runtime-benchmarks",
"pallet-nft-fractionalization/runtime-benchmarks",
"pallet-vesting/runtime-benchmarks",
"pallet-whitelist/runtime-benchmarks",
"frame-system-benchmarking/runtime-benchmarks",
Expand Down Expand Up @@ -318,7 +319,7 @@ try-runtime = [
"pallet-transaction-storage/try-runtime",
"pallet-uniques/try-runtime",
"pallet-nfts/try-runtime",
"pallet-nft-fractionalisation/try-runtime",
"pallet-nft-fractionalization/try-runtime",
"pallet-vesting/try-runtime",
"pallet-whitelist/try-runtime",
]
64 changes: 47 additions & 17 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ use frame_support::{
pallet_prelude::Get,
parameter_types,
traits::{
fungible::ItemOf, AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU16, ConstU32,
Currency, EitherOfDiverse, EqualPrivilegeOnly, Everything, Imbalance, InstanceFilter,
KeyOwnerProofSystem, LockIdentifier, Nothing, OnUnbalanced, U128CurrencyToVote,
WithdrawReasons,
fungible::ItemOf,
tokens::{
nonfungibles_v2::{Inspect, LockableNonfungible, Mutate},
AttributeNamespace,
},
AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU16, ConstU32, Currency, EitherOfDiverse,
EqualPrivilegeOnly, Everything, Imbalance, InstanceFilter, KeyOwnerProofSystem,
LockIdentifier, Locker, Nothing, OnUnbalanced, U128CurrencyToVote, WithdrawReasons,
},
weights::{
constants::{
Expand All @@ -56,7 +60,7 @@ use pallet_grandpa::{
fg_primitives, AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList,
};
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use pallet_nfts::PalletFeatures;
use pallet_nfts::{ItemConfig, PalletFeatures};
use pallet_nis::WithMaximumOf;
use pallet_session::historical::{self as pallet_session_historical};
pub use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustment};
Expand All @@ -74,7 +78,8 @@ use sp_runtime::{
SaturatedConversion, StaticLookup,
},
transaction_validity::{TransactionPriority, TransactionSource, TransactionValidity},
ApplyExtrinsicResult, FixedPointNumber, FixedU128, Perbill, Percent, Permill, Perquintill,
ApplyExtrinsicResult, DispatchResult, FixedPointNumber, FixedU128, Perbill, Percent, Permill,
Perquintill,
};
use sp_std::prelude::*;
#[cfg(any(feature = "std", test))]
Expand All @@ -93,8 +98,6 @@ pub use pallet_sudo::Call as SudoCall;
#[cfg(any(feature = "std", test))]
pub use sp_runtime::BuildStorage;

pub use pallet_nft_fractionalisation;

/// Implementations of some helper traits passed into runtime modules as associated types.
pub mod impls;
#[cfg(not(feature = "runtime-benchmarks"))]
Expand Down Expand Up @@ -1562,6 +1565,17 @@ parameter_types! {
pub Features: PalletFeatures = PalletFeatures::all_enabled();
}

const LOCKED_NFT_KEY: &[u8; 6] = b"locked";
type ItemId = <Runtime as pallet_nfts::Config>::ItemId;
type CollectionId = <Runtime as pallet_nfts::Config>::CollectionId;

pub struct NftLocker;
impl Locker<CollectionId, ItemId> for NftLocker {
fn is_locked(collection: CollectionId, item: ItemId) -> bool {
Nfts::attribute(&collection, &item, &AttributeNamespace::Pallet, LOCKED_NFT_KEY).is_some()
}
}

impl pallet_nfts::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type CollectionId = u32;
Expand All @@ -1585,25 +1599,40 @@ impl pallet_nfts::Config for Runtime {
#[cfg(feature = "runtime-benchmarks")]
type Helper = ();
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<AccountId>>;
type Locker = ();
type Locker = NftLocker;
}

parameter_types! {
pub const NftFractionsPalletId: PalletId = PalletId(*b"fraction");
pub const BuybackThreshold: u32 = 1;
}

impl pallet_nft_fractionalisation::Config for Runtime {
pub struct RuntimeLockableNonfungible;
impl LockableNonfungible<CollectionId, ItemId> for RuntimeLockableNonfungible {
fn lock(collection: &CollectionId, item: &ItemId) -> DispatchResult {
<Nfts as Mutate<AccountId, ItemConfig>>::set_attribute(
collection,
item,
LOCKED_NFT_KEY,
&[1],
)
}
fn unlock(collection: &CollectionId, item: &ItemId) -> DispatchResult {
<Nfts as Mutate<AccountId, ItemConfig>>::clear_attribute(collection, item, LOCKED_NFT_KEY)
}
}

impl pallet_nft_fractionalization::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type PalletId = NftFractionsPalletId;
type Currency = Balances;
type BuybackThreshold = BuybackThreshold;
type CollectionId = <Self as pallet_nfts::Config>::CollectionId;
type ItemId = <Self as pallet_nfts::Config>::ItemId;
type NftCollectionId = <Self as pallet_nfts::Config>::CollectionId;
type NftId = <Self as pallet_nfts::Config>::ItemId;
type AssetBalance = <Self as pallet_balances::Config>::Balance;
type Assets = Assets;
type Items = Nfts;
type AssetId = <Self as pallet_assets::Config>::AssetId;
type Assets = Assets;
type Nfts = Nfts;
type NftLocker = RuntimeLockableNonfungible;
type WeightInfo = pallet_nft_fractionalization::weights::SubstrateWeight<Runtime>;
}

impl pallet_transaction_storage::Config for Runtime {
Expand Down Expand Up @@ -1761,7 +1790,7 @@ construct_runtime!(
Nis: pallet_nis,
Uniques: pallet_uniques,
Nfts: pallet_nfts,
NftFractions: pallet_nft_fractionalisation,
NftFractions: pallet_nft_fractionalization,
TransactionStorage: pallet_transaction_storage,
VoterList: pallet_bags_list::<Instance1>,
StateTrieMigration: pallet_state_trie_migration,
Expand Down Expand Up @@ -1894,6 +1923,7 @@ mod benches {
[pallet_treasury, Treasury]
[pallet_uniques, Uniques]
[pallet_nfts, Nfts]
[pallet_nft_fractionalization, NftFractions]
[pallet_utility, Utility]
[pallet_vesting, Vesting]
[pallet_whitelist, Whitelist]
Expand Down
14 changes: 11 additions & 3 deletions frame/assets/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,9 +899,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
ensure!(metadata.as_ref().map_or(true, |m| !m.is_frozen), Error::<T, I>::NoPermission);

let old_deposit = metadata.take().map_or(Zero::zero(), |m| m.deposit);
let new_deposit = T::MetadataDepositPerByte::get()
.saturating_mul(((name.len() + symbol.len()) as u32).into())
.saturating_add(T::MetadataDepositBase::get());
let new_deposit = Self::calc_metadata_deposit(&name, &symbol);

if new_deposit > old_deposit {
T::Currency::reserve(from, new_deposit - old_deposit)?;
Expand All @@ -927,4 +925,14 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
Ok(())
})
}

/// Calculate the metadata deposit for the provided data.
pub(super) fn calc_metadata_deposit(
name: &Vec<u8>,
symbol: &Vec<u8>,
) -> DepositBalanceOf<T, I> {
T::MetadataDepositPerByte::get()
.saturating_mul(((name.len() + symbol.len()) as u32).into())
.saturating_add(T::MetadataDepositBase::get())
}
}
13 changes: 13 additions & 0 deletions frame/assets/src/impl_fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,19 @@ impl<T: Config<I>, I: 'static> fungibles::metadata::Mutate<<T as SystemConfig>::
}
}

impl<T: Config<I>, I: 'static>
fungibles::metadata::CalcMetadataDeposit<
<T::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance,
> for Pallet<T, I>
{
fn calc(
name: &Vec<u8>,
symbol: &Vec<u8>,
) -> <T::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance {
Self::calc_metadata_deposit(name, symbol)
}
}

impl<T: Config<I>, I: 'static> fungibles::approvals::Inspect<<T as SystemConfig>::AccountId>
for Pallet<T, I>
{
Expand Down
4 changes: 0 additions & 4 deletions frame/nft-fractionalisation/README.md

This file was deleted.

Loading