use super::*;
use crate::macros::*;
use alloc::{vec, vec::Vec};
use codec::EncodeLike;
use enumflags2::{bitflags, BitFlags};
use frame_support::{
pallet_prelude::{BoundedVec, MaxEncodedLen},
traits::Get,
BoundedBTreeMap, BoundedBTreeSet,
};
use frame_system::pallet_prelude::BlockNumberFor;
use scale_info::{build::Fields, meta_type, Path, Type, TypeInfo, TypeParameter};
pub type DepositBalanceOf<T, I = ()> =
<<T as Config<I>>::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance;
pub type CollectionDetailsFor<T, I> =
CollectionDetails<<T as SystemConfig>::AccountId, DepositBalanceOf<T, I>>;
pub type ApprovalsOf<T, I = ()> = BoundedBTreeMap<
<T as SystemConfig>::AccountId,
Option<BlockNumberFor<T>>,
<T as Config<I>>::ApprovalsLimit,
>;
pub type ItemAttributesApprovals<T, I = ()> =
BoundedBTreeSet<<T as SystemConfig>::AccountId, <T as Config<I>>::ItemAttributesApprovalsLimit>;
pub type ItemDepositOf<T, I> = ItemDeposit<DepositBalanceOf<T, I>, <T as SystemConfig>::AccountId>;
pub type AttributeDepositOf<T, I> =
AttributeDeposit<DepositBalanceOf<T, I>, <T as SystemConfig>::AccountId>;
pub type ItemMetadataDepositOf<T, I> =
ItemMetadataDeposit<DepositBalanceOf<T, I>, <T as SystemConfig>::AccountId>;
pub type ItemDetailsFor<T, I> =
ItemDetails<<T as SystemConfig>::AccountId, ItemDepositOf<T, I>, ApprovalsOf<T, I>>;
pub type BalanceOf<T, I = ()> =
<<T as Config<I>>::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance;
pub type ItemPrice<T, I = ()> = BalanceOf<T, I>;
pub type ItemTipOf<T, I = ()> = ItemTip<
<T as Config<I>>::CollectionId,
<T as Config<I>>::ItemId,
<T as SystemConfig>::AccountId,
BalanceOf<T, I>,
>;
pub type CollectionConfigFor<T, I = ()> =
CollectionConfig<BalanceOf<T, I>, BlockNumberFor<T>, <T as Config<I>>::CollectionId>;
pub type PreSignedMintOf<T, I = ()> = PreSignedMint<
<T as Config<I>>::CollectionId,
<T as Config<I>>::ItemId,
<T as SystemConfig>::AccountId,
BlockNumberFor<T>,
BalanceOf<T, I>,
>;
pub type PreSignedAttributesOf<T, I = ()> = PreSignedAttributes<
<T as Config<I>>::CollectionId,
<T as Config<I>>::ItemId,
<T as SystemConfig>::AccountId,
BlockNumberFor<T>,
>;
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct CollectionDetails<AccountId, DepositBalance> {
pub owner: AccountId,
pub owner_deposit: DepositBalance,
pub items: u32,
pub item_metadatas: u32,
pub item_configs: u32,
pub attributes: u32,
}
#[derive(Copy, Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct DestroyWitness {
#[codec(compact)]
pub item_metadatas: u32,
#[codec(compact)]
pub item_configs: u32,
#[codec(compact)]
pub attributes: u32,
}
impl<AccountId, DepositBalance> CollectionDetails<AccountId, DepositBalance> {
pub fn destroy_witness(&self) -> DestroyWitness {
DestroyWitness {
item_metadatas: self.item_metadatas,
item_configs: self.item_configs,
attributes: self.attributes,
}
}
}
#[derive(Clone, Encode, Decode, Default, Eq, PartialEq, RuntimeDebug, TypeInfo)]
pub struct MintWitness<ItemId, Balance> {
pub owned_item: Option<ItemId>,
pub mint_price: Option<Balance>,
}
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default, TypeInfo, MaxEncodedLen)]
pub struct ItemDetails<AccountId, Deposit, Approvals> {
pub owner: AccountId,
pub approvals: Approvals,
pub deposit: Deposit,
}
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct ItemDeposit<DepositBalance, AccountId> {
pub account: AccountId,
pub amount: DepositBalance,
}
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(StringLimit))]
#[codec(mel_bound(Deposit: MaxEncodedLen))]
pub struct CollectionMetadata<Deposit, StringLimit: Get<u32>> {
pub deposit: Deposit,
pub data: BoundedVec<u8, StringLimit>,
}
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default, TypeInfo, MaxEncodedLen)]
#[scale_info(skip_type_params(StringLimit))]
pub struct ItemMetadata<Deposit, StringLimit: Get<u32>> {
pub deposit: Deposit,
pub data: BoundedVec<u8, StringLimit>,
}
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct ItemTip<CollectionId, ItemId, AccountId, Amount> {
pub collection: CollectionId,
pub item: ItemId,
pub receiver: AccountId,
pub amount: Amount,
}
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, Default, TypeInfo, MaxEncodedLen)]
pub struct PendingSwap<CollectionId, ItemId, ItemPriceWithDirection, Deadline> {
pub desired_collection: CollectionId,
pub desired_item: Option<ItemId>,
pub price: Option<ItemPriceWithDirection>,
pub deadline: Deadline,
}
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct AttributeDeposit<DepositBalance, AccountId> {
pub account: Option<AccountId>,
pub amount: DepositBalance,
}
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct ItemMetadataDeposit<DepositBalance, AccountId> {
pub account: Option<AccountId>,
pub amount: DepositBalance,
}
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub enum PriceDirection {
Send,
Receive,
}
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct PriceWithDirection<Amount> {
pub amount: Amount,
pub direction: PriceDirection,
}
#[bitflags]
#[repr(u64)]
#[derive(Copy, Clone, RuntimeDebug, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub enum CollectionSetting {
TransferableItems,
UnlockedMetadata,
UnlockedAttributes,
UnlockedMaxSupply,
DepositRequired,
}
#[derive(Clone, Copy, PartialEq, Eq, Default, RuntimeDebug)]
pub struct CollectionSettings(pub BitFlags<CollectionSetting>);
impl CollectionSettings {
pub fn all_enabled() -> Self {
Self(BitFlags::EMPTY)
}
pub fn get_disabled(&self) -> BitFlags<CollectionSetting> {
self.0
}
pub fn is_disabled(&self, setting: CollectionSetting) -> bool {
self.0.contains(setting)
}
pub fn from_disabled(settings: BitFlags<CollectionSetting>) -> Self {
Self(settings)
}
}
impl_codec_bitflags!(CollectionSettings, u64, CollectionSetting);
#[derive(Clone, Copy, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub enum MintType<CollectionId> {
Issuer,
Public,
HolderOf(CollectionId),
}
#[derive(Clone, Copy, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct MintSettings<Price, BlockNumber, CollectionId> {
pub mint_type: MintType<CollectionId>,
pub price: Option<Price>,
pub start_block: Option<BlockNumber>,
pub end_block: Option<BlockNumber>,
pub default_item_settings: ItemSettings,
}
impl<Price, BlockNumber, CollectionId> Default for MintSettings<Price, BlockNumber, CollectionId> {
fn default() -> Self {
Self {
mint_type: MintType::Issuer,
price: None,
start_block: None,
end_block: None,
default_item_settings: ItemSettings::all_enabled(),
}
}
}
#[derive(
Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, scale_info::TypeInfo, MaxEncodedLen,
)]
pub enum AttributeNamespace<AccountId> {
Pallet,
CollectionOwner,
ItemOwner,
Account(AccountId),
}
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo)]
pub struct CancelAttributesApprovalWitness {
pub account_attributes: u32,
}
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub enum PalletAttributes<CollectionId> {
UsedToClaim(CollectionId),
TransferDisabled,
}
#[derive(
Clone, Copy, Decode, Default, Encode, MaxEncodedLen, PartialEq, RuntimeDebug, TypeInfo,
)]
pub struct CollectionConfig<Price, BlockNumber, CollectionId> {
pub settings: CollectionSettings,
pub max_supply: Option<u32>,
pub mint_settings: MintSettings<Price, BlockNumber, CollectionId>,
}
impl<Price, BlockNumber, CollectionId> CollectionConfig<Price, BlockNumber, CollectionId> {
pub fn is_setting_enabled(&self, setting: CollectionSetting) -> bool {
!self.settings.is_disabled(setting)
}
pub fn has_disabled_setting(&self, setting: CollectionSetting) -> bool {
self.settings.is_disabled(setting)
}
pub fn enable_setting(&mut self, setting: CollectionSetting) {
self.settings.0.remove(setting);
}
pub fn disable_setting(&mut self, setting: CollectionSetting) {
self.settings.0.insert(setting);
}
}
#[bitflags]
#[repr(u64)]
#[derive(Copy, Clone, RuntimeDebug, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub enum ItemSetting {
Transferable,
UnlockedMetadata,
UnlockedAttributes,
}
#[derive(Clone, Copy, PartialEq, Eq, Default, RuntimeDebug)]
pub struct ItemSettings(pub BitFlags<ItemSetting>);
impl ItemSettings {
pub fn all_enabled() -> Self {
Self(BitFlags::EMPTY)
}
pub fn get_disabled(&self) -> BitFlags<ItemSetting> {
self.0
}
pub fn is_disabled(&self, setting: ItemSetting) -> bool {
self.0.contains(setting)
}
pub fn from_disabled(settings: BitFlags<ItemSetting>) -> Self {
Self(settings)
}
}
impl_codec_bitflags!(ItemSettings, u64, ItemSetting);
#[derive(
Encode, Decode, Default, PartialEq, RuntimeDebug, Clone, Copy, MaxEncodedLen, TypeInfo,
)]
pub struct ItemConfig {
pub settings: ItemSettings,
}
impl ItemConfig {
pub fn is_setting_enabled(&self, setting: ItemSetting) -> bool {
!self.settings.is_disabled(setting)
}
pub fn has_disabled_setting(&self, setting: ItemSetting) -> bool {
self.settings.is_disabled(setting)
}
pub fn has_disabled_settings(&self) -> bool {
!self.settings.get_disabled().is_empty()
}
pub fn enable_setting(&mut self, setting: ItemSetting) {
self.settings.0.remove(setting);
}
pub fn disable_setting(&mut self, setting: ItemSetting) {
self.settings.0.insert(setting);
}
}
#[bitflags]
#[repr(u64)]
#[derive(Copy, Clone, RuntimeDebug, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub enum PalletFeature {
Trading,
Attributes,
Approvals,
Swaps,
}
#[derive(Default, RuntimeDebug)]
pub struct PalletFeatures(pub BitFlags<PalletFeature>);
impl PalletFeatures {
pub fn all_enabled() -> Self {
Self(BitFlags::EMPTY)
}
pub fn from_disabled(features: BitFlags<PalletFeature>) -> Self {
Self(features)
}
pub fn is_enabled(&self, feature: PalletFeature) -> bool {
!self.0.contains(feature)
}
}
impl_codec_bitflags!(PalletFeatures, u64, PalletFeature);
#[bitflags]
#[repr(u8)]
#[derive(Copy, Clone, RuntimeDebug, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub enum CollectionRole {
Issuer,
Freezer,
Admin,
}
#[derive(Clone, Copy, PartialEq, Eq, Default, RuntimeDebug)]
pub struct CollectionRoles(pub BitFlags<CollectionRole>);
impl CollectionRoles {
pub fn none() -> Self {
Self(BitFlags::EMPTY)
}
pub fn has_role(&self, role: CollectionRole) -> bool {
self.0.contains(role)
}
pub fn add_role(&mut self, role: CollectionRole) {
self.0.insert(role);
}
pub fn max_roles() -> u8 {
let all: BitFlags<CollectionRole> = BitFlags::all();
all.len() as u8
}
}
impl_codec_bitflags!(CollectionRoles, u8, CollectionRole);
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct PreSignedMint<CollectionId, ItemId, AccountId, Deadline, Balance> {
pub collection: CollectionId,
pub item: ItemId,
pub attributes: Vec<(Vec<u8>, Vec<u8>)>,
pub metadata: Vec<u8>,
pub only_account: Option<AccountId>,
pub deadline: Deadline,
pub mint_price: Option<Balance>,
}
#[derive(Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct PreSignedAttributes<CollectionId, ItemId, AccountId, Deadline> {
pub collection: CollectionId,
pub item: ItemId,
pub attributes: Vec<(Vec<u8>, Vec<u8>)>,
pub namespace: AttributeNamespace<AccountId>,
pub deadline: Deadline,
}