Skip to content

Commit

Permalink
feat: assets, nfts & nft fractionalization
Browse files Browse the repository at this point in the history
fix(base): declare WeightInfo for every pallet

style(base): fmt

style(base): undo fmt

fix(base): remove example zombienet file

feat(base): nfts-parachain
  • Loading branch information
al3mart authored and evilrobot-01 committed May 6, 2024
1 parent 33a128e commit 8b998ae
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 13 deletions.
20 changes: 12 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ parachain-template-runtime = { path = "./runtime" }

# Substrate
sc-basic-authorship = "0.37.0"
sc-chain-spec = "30.0.0"
sc-chain-spec = "30.0.1"
sc-cli = "0.39.0"
sc-client-api = "31.0.0"
sc-offchain = "32.0.0"
Expand All @@ -53,17 +53,21 @@ sc-tracing = "31.0.0"
sc-transaction-pool = "31.0.0"
sc-transaction-pool-api = "31.0.0"
frame-benchmarking = { version = "31.0.0", default-features = false }
frame-benchmarking-cli = "35.0.0"
frame-benchmarking-cli = "35.0.1"
frame-executive = { version = "31.0.0", default-features = false }
frame-support = { version = "31.0.0", default-features = false }
frame-system = { version = "31.0.0", default-features = false }
frame-system-benchmarking = { version = "31.0.0", default-features = false }
frame-system-rpc-runtime-api = { version = "29.0.0", default-features = false }
frame-try-runtime = { version = "0.37.0", default-features = false }
pallet-assets = { version = "32.0.0", default-features = false }
pallet-aura = { version = "30.0.0", default-features = false }
pallet-authorship = { version = "31.0.0", default-features = false }
pallet-balances = { version = "31.0.0", default-features = false }
pallet-message-queue = { version = "34.0.0", default-features = false }
pallet-nfts = { version = "25.0.0", default-features = false }
pallet-nfts-runtime-api = { version = "17.0.0", default-features = false }
pallet-nft-fractionalization = { version = "13.0.0", default-features = false }
pallet-session = { version = "31.0.0", default-features = false }
pallet-sudo = { version = "31.0.0", default-features = false }
pallet-timestamp = { version = "30.0.0", default-features = false }
Expand All @@ -90,14 +94,14 @@ sp-transaction-pool = { version = "29.0.0", default-features = false }
sp-version = { version = "32.0.0", default-features = false }

# Polkadot
pallet-xcm = { version = "10.0.0", default-features = false }
pallet-xcm = { version = "10.0.1", default-features = false }
polkadot-cli = "10.0.0"
polkadot-parachain-primitives = { version = "9.0.0", default-features = false }
polkadot-primitives = "10.0.0"
xcm = { package = "staging-xcm", version = "10.0.0", default-features = false }
xcm = { version = "10.0.0", package = "staging-xcm", default-features = false }
polkadot-runtime-common = { version = "10.0.0", default-features = false }
xcm-builder = { package = "staging-xcm-builder", version = "10.0.0", default-features = false }
xcm-executor = { package = "staging-xcm-executor", version = "10.0.0", default-features = false }
xcm-builder = { version = "10.0.0", package = "staging-xcm-builder", default-features = false }
xcm-executor = { version = "10.0.0", package = "staging-xcm-executor", default-features = false }

# Cumulus
cumulus-pallet-aura-ext = { version = "0.10.0", default-features = false }
Expand All @@ -109,9 +113,9 @@ cumulus-primitives-aura = { version = "0.10.0", default-features = false }
cumulus-primitives-core = { version = "0.10.0", default-features = false }
cumulus-primitives-utility = { version = "0.10.0", default-features = false }
cumulus-primitives-storage-weight-reclaim = { version = "1.0.0", default-features = false }
pallet-collator-selection = { version = "12.0.0", default-features = false }
pallet-collator-selection = { version = "12.0.1", default-features = false }
parachains-common = { version = "10.0.0", default-features = false }
parachain-info = { package = "staging-parachain-info", version = "0.10.0", default-features = false }
parachain-info = { version = "0.10.0", package = "staging-parachain-info", default-features = false }
cumulus-primitives-parachain-inherent = "0.10.0"
cumulus-relay-chain-interface = "0.10.0"
color-print = "0.3.4"
Expand Down
14 changes: 14 additions & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ frame-system = { workspace = true }
frame-system-benchmarking = { workspace = true }
frame-system-rpc-runtime-api = { workspace = true }
frame-try-runtime = { workspace = true }
pallet-assets = { workspace = true }
pallet-aura = { workspace = true }
pallet-authorship = { workspace = true }
pallet-balances = { workspace = true }
pallet-message-queue = { workspace = true }
pallet-nfts = { workspace = true }
pallet-nfts-runtime-api = { workspace = true }
pallet-nft-fractionalization = { workspace = true }
pallet-session = { workspace = true }
pallet-sudo = { workspace = true }
pallet-timestamp = { workspace = true }
Expand Down Expand Up @@ -95,11 +99,15 @@ std = [
"frame-system/std",
"frame-try-runtime/std",
"log/std",
"pallet-assets/std",
"pallet-aura/std",
"pallet-authorship/std",
"pallet-balances/std",
"pallet-collator-selection/std",
"pallet-message-queue/std",
"pallet-nfts/std",
"pallet-nfts-runtime-api/std",
"pallet-nft-fractionalization/std",
"pallet-session/std",
"pallet-sudo/std",
"pallet-timestamp/std",
Expand Down Expand Up @@ -138,9 +146,12 @@ runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"frame-system-benchmarking/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-assets/runtime-benchmarks",
"pallet-balances/runtime-benchmarks",
"pallet-collator-selection/runtime-benchmarks",
"pallet-message-queue/runtime-benchmarks",
"pallet-nfts/runtime-benchmarks",
"pallet-nft-fractionalization/runtime-benchmarks",
"pallet-sudo/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
Expand All @@ -161,11 +172,14 @@ try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"frame-try-runtime/try-runtime",
"pallet-assets/try-runtime",
"pallet-aura/try-runtime",
"pallet-authorship/try-runtime",
"pallet-balances/try-runtime",
"pallet-collator-selection/try-runtime",
"pallet-message-queue/try-runtime",
"pallet-nfts/try-runtime",
"pallet-nft-fractionalization/try-runtime",
"pallet-session/try-runtime",
"pallet-sudo/try-runtime",
"pallet-timestamp/try-runtime",
Expand Down
171 changes: 166 additions & 5 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,26 @@ use frame_support::{
dispatch::DispatchClass,
genesis_builder_helper::{build_config, create_default_config},
parameter_types,
traits::{ConstBool, ConstU32, ConstU64, ConstU8, EitherOfDiverse, TransformOrigin},
traits::{
tokens::nonfungibles_v2::Inspect, AsEnsureOriginWithArg, ConstBool, ConstU32, ConstU64,
ConstU8, EitherOfDiverse, TransformOrigin,
},
weights::{
constants::WEIGHT_REF_TIME_PER_SECOND, ConstantMultiplier, Weight, WeightToFeeCoefficient,
WeightToFeeCoefficients, WeightToFeePolynomial,
},
PalletId,
BoundedVec, PalletId,
};
use frame_system::{
limits::{BlockLength, BlockWeights},
EnsureRoot,
EnsureRoot, EnsureSigned,
};
use pallet_nfts::PalletFeatures;
use pallet_xcm::{EnsureXcm, IsVoiceOfBody};
use parachains_common::message_queue::{NarrowOriginToSibling, ParaIdToSibling};
use parachains_common::{
message_queue::{NarrowOriginToSibling, ParaIdToSibling},
AssetIdForTrustBackedAssets, CollectionId, ItemId,
};
pub use sp_consensus_aura::sr25519::AuthorityId as AuraId;
pub use sp_runtime::{MultiAddress, Perbill, Permill};
use xcm_config::{RelayLocation, XcmOriginToTransactDispatchOrigin};
Expand Down Expand Up @@ -209,6 +216,10 @@ pub const CENTIUNIT: Balance = 10_000_000_000;
pub const MILLIUNIT: Balance = 1_000_000_000;
pub const MICROUNIT: Balance = 1_000_000;

// Deposits
const fn deposit(items: u32, bytes: u32) -> Balance {
(items as Balance * UNIT + (bytes as Balance) * (5 * MILLIUNIT / 100)) / 10
}
/// The existential deposit. Set to 1/10 of the Connected Relay Chain.
pub const EXISTENTIAL_DEPOSIT: Balance = MILLIUNIT;

Expand Down Expand Up @@ -396,7 +407,6 @@ impl parachain_info::Config for Runtime {}
parameter_types! {
pub MessageQueueServiceWeight: Weight = Perbill::from_percent(35) * RuntimeBlockWeights::get().max_block;
}

impl pallet_message_queue::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = (); // Configure based on benchmarking results.
Expand Down Expand Up @@ -505,6 +515,108 @@ impl pallet_collator_selection::Config for Runtime {
type WeightInfo = (); // Configure based on benchmarking results.
}

parameter_types! {
pub const NftFractionalizationPalletId: PalletId = PalletId(*b"fraction");
pub NewAssetSymbol: BoundedVec<u8, AssetsStringLimit> = (*b"FRAC").to_vec().try_into().unwrap();
pub NewAssetName: BoundedVec<u8, AssetsStringLimit> = (*b"Frac").to_vec().try_into().unwrap();
}

impl pallet_nft_fractionalization::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Deposit = AssetDeposit;
type Currency = Balances;
type NewAssetSymbol = NewAssetSymbol;
type NewAssetName = NewAssetName;
type StringLimit = AssetsStringLimit;
type NftCollectionId = <Self as pallet_nfts::Config>::CollectionId;
type NftId = <Self as pallet_nfts::Config>::ItemId;
type AssetBalance = <Self as pallet_assets::Config<TrustBackedAssets>>::Balance;
type AssetId = <Self as pallet_assets::Config<TrustBackedAssets>>::AssetId;
type Assets = Assets;
type Nfts = Nfts;
type PalletId = NftFractionalizationPalletId;
type WeightInfo = pallet_nft_fractionalization::weights::SubstrateWeight<Runtime>;
type RuntimeHoldReason = RuntimeHoldReason;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

parameter_types! {
pub NftsPalletFeatures: PalletFeatures = PalletFeatures::all_enabled();
pub const NftsCollectionDeposit: Balance = 10 * UNIT;
pub const NftsItemDeposit: Balance = UNIT / 100;
pub const NftsMetadataDepositBase: Balance = deposit(1, 129);
pub const NftsAttributeDepositBase: Balance = deposit(1, 0);
pub const NftsDepositPerByte: Balance = deposit(0, 1);
pub const NftsMaxDeadlineDuration: BlockNumber = 12 * 30 * DAYS;
}
impl pallet_nfts::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type CollectionId = CollectionId;
type ItemId = ItemId;
type Currency = Balances;
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<AccountId>>;
type ForceOrigin = AssetsForceOrigin;
type Locker = ();
type CollectionDeposit = NftsCollectionDeposit;
type ItemDeposit = NftsItemDeposit;
type MetadataDepositBase = NftsMetadataDepositBase;
type AttributeDepositBase = NftsAttributeDepositBase;
type DepositPerByte = NftsDepositPerByte;
type StringLimit = ConstU32<256>;
type KeyLimit = ConstU32<64>;
type ValueLimit = ConstU32<256>;
type ApprovalsLimit = ConstU32<20>;
type ItemAttributesApprovalsLimit = ConstU32<30>;
type MaxTips = ConstU32<10>;
type MaxDeadlineDuration = NftsMaxDeadlineDuration;
type MaxAttributesPerCall = ConstU32<10>;
type Features = NftsPalletFeatures;
type OffchainSignature = Signature;
type OffchainPublic = <Signature as Verify>::Signer;
type WeightInfo = pallet_nfts::weights::SubstrateWeight<Runtime>;
#[cfg(feature = "runtime-benchmarks")]
type Helper = ();
}

/// We allow root to execute privileged asset operations.
pub type AssetsForceOrigin = EnsureRoot<AccountId>;

parameter_types! {
pub const AssetDeposit: Balance = 10 * UNIT;
pub const AssetAccountDeposit: Balance = deposit(1, 16);
pub const ApprovalDeposit: Balance = EXISTENTIAL_DEPOSIT;
pub const AssetsStringLimit: u32 = 50;
/// Key = 32 bytes, Value = 36 bytes (32+1+1+1+1)
// https://github.com/paritytech/substrate/blob/069917b/frame/assets/src/lib.rs#L257L271
pub const MetadataDepositBase: Balance = deposit(1, 68);
pub const MetadataDepositPerByte: Balance = deposit(0, 1);
}

pub type TrustBackedAssets = pallet_assets::Instance1;
impl pallet_assets::Config<TrustBackedAssets> for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type AssetId = AssetIdForTrustBackedAssets;
type AssetIdParameter = codec::Compact<AssetIdForTrustBackedAssets>;
type Currency = Balances;
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<AccountId>>;
type ForceOrigin = AssetsForceOrigin;
type AssetDeposit = AssetDeposit;
type MetadataDepositBase = MetadataDepositBase;
type MetadataDepositPerByte = MetadataDepositPerByte;
type ApprovalDeposit = ApprovalDeposit;
type StringLimit = AssetsStringLimit;
type Freezer = ();
type Extra = ();
type WeightInfo = pallet_assets::weights::SubstrateWeight<Runtime>;
type CallbackHandle = ();
type AssetAccountDeposit = AssetAccountDeposit;
type RemoveItemsLimit = ConstU32<1000>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

// Create the runtime by composing the FRAME pallets that were previously configured.
construct_runtime!(
// While this macro defines the pallets conforming the runtime,
Expand Down Expand Up @@ -535,6 +647,11 @@ construct_runtime!(
PolkadotXcm: pallet_xcm = 31,
CumulusXcm: cumulus_pallet_xcm = 32,
MessageQueue: pallet_message_queue = 33,

// Assets
Nfts: pallet_nfts = 40,
NftFractionalization: pallet_nft_fractionalization = 41,
Assets: pallet_assets::<Instance1> = 42,
}
);

Expand Down Expand Up @@ -707,6 +824,50 @@ impl_runtime_apis! {
}
}

impl pallet_nfts_runtime_api::NftsApi<Block, AccountId, u32, u32> for Runtime {
fn owner(collection: u32, item: u32) -> Option<AccountId> {
<Nfts as Inspect<AccountId>>::owner(&collection, &item)
}

fn collection_owner(collection: u32) -> Option<AccountId> {
<Nfts as Inspect<AccountId>>::collection_owner(&collection)
}

fn attribute(
collection: u32,
item: u32,
key: Vec<u8>,
) -> Option<Vec<u8>> {
<Nfts as Inspect<AccountId>>::attribute(&collection, &item, &key)
}

fn custom_attribute(
account: AccountId,
collection: u32,
item: u32,
key: Vec<u8>,
) -> Option<Vec<u8>> {
<Nfts as Inspect<AccountId>>::custom_attribute(
&account,
&collection,
&item,
&key,
)
}

fn system_attribute(
collection: u32,
item: Option<u32>,
key: Vec<u8>,
) -> Option<Vec<u8>> {
<Nfts as Inspect<AccountId>>::system_attribute(&collection, item.as_ref(), &key)
}

fn collection_attribute(collection: u32, key: Vec<u8>) -> Option<Vec<u8>> {
<Nfts as Inspect<AccountId>>::collection_attribute(&collection, &key)
}
}

#[cfg(feature = "try-runtime")]
impl frame_try_runtime::TryRuntime<Block> for Runtime {
fn on_runtime_upgrade(checks: frame_try_runtime::UpgradeCheckSelect) -> (Weight, Weight) {
Expand Down

0 comments on commit 8b998ae

Please sign in to comment.