Skip to content

Commit

Permalink
Merge pull request #504 from Setheum-Labs/tokens-blend
Browse files Browse the repository at this point in the history
Tokens blend - NFT
  • Loading branch information
balqaasem authored Aug 30, 2021
2 parents 236268e + 7c856b7 commit 39b6dbf
Show file tree
Hide file tree
Showing 7 changed files with 730 additions and 275 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 11 additions & 13 deletions lib-serml/tokens/nft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,29 @@ serde = { version = "1.0.124", optional = true }
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
enumflags2 = { version = "0.6.3" }

# Substrate dependencies
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8", default-features = false, optional = true }
pallet-proxy = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
pallet-timestamp = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
max-encoded-len = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8", default-features = false }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8", default-features = false, optional = true}
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8", default-features = false }
pallet-proxy = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8", default-features = false }

# orml dependencies
orml-traits = { path = "../../../lib-openrml/traits", default-features = false }
orml-nft = { path = "../../../lib-openrml/nft", default-features = false }
orml-traits = { path = "../../../lib-openrml/traits", default-features = false }

# local dependencies
primitives = { package = "setheum-primitives", path = "../../../primitives", default-features = false }
setheum-currencies = { path = "../../tokens/currencies", default-features = false }

[dev-dependencies]
sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
pallet-utility = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.8" }
setheum-currencies = { path = "../../../lib-serml/tokens/currencies", default-features = false }
orml-tokens = { path = "../../../lib-openrml/tokens", default-features = false }
orml-tokens = { path = "../../../lib-openrml/tokens" }
support = { package = "setheum-support", path = "../../support" }
setheum-currencies = { path = "../../tokens/currencies", default-features = false }

[features]
default = ["std"]
Expand All @@ -51,12 +49,12 @@ std = [
"primitives/std",
"orml-traits/std",
"orml-nft/std",
"pallet-timestamp/std",
"setheum-currencies/std",
]
runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"primitives/runtime-benchmarks"
]
try-runtime = ["frame-support/try-runtime"]
139 changes: 65 additions & 74 deletions lib-serml/tokens/nft/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use sp_std::prelude::*;
use sp_std::vec;

use frame_benchmarking::{account, benchmarks};
use frame_support::{traits::Get, weights::DispatchClass};
use frame_support::{dispatch::DispatchErrorWithPostInfo, traits::Get, weights::DispatchClass};
use frame_system::RawOrigin;
use sp_runtime::traits::{AccountIdConversion, StaticLookup, UniqueSaturatedInto};

Expand All @@ -40,14 +40,47 @@ fn dollar(d: u32) -> Balance {
d.saturating_mul(1_000_000_000_000_000_000)
}

fn test_attr() -> Attributes {
let mut attr: Attributes = BTreeMap::new();
for i in 0..30 {
attr.insert(vec![i], vec![0; 64]);
}
attr
}

fn create_token_class<T: Config>(caller: T::AccountId) -> Result<T::AccountId, DispatchErrorWithPostInfo> {
let base_currency_amount = dollar(1000);
<T as module::Config>::Currency::make_free_balance_be(&caller, base_currency_amount.unique_saturated_into());

let module_account: T::AccountId = T::PalletId::get().into_sub_account(orml_nft::Pallet::<T>::next_class_id());
crate::Pallet::<T>::create_class(
RawOrigin::Signed(caller).into(),
vec![1],
Properties(
ClassProperty::Transferable
| ClassProperty::Burnable
| ClassProperty::Mintable
| ClassProperty::ClassPropertiesMutable,
),
test_attr(),
)?;

<T as module::Config>::Currency::make_free_balance_be(
&module_account,
base_currency_amount.unique_saturated_into(),
);

Ok(module_account)
}

benchmarks! {
// create NFT class
create_class {
let caller: T::AccountId = account("caller", 0, SEED);
let base_currency_amount = dollar(1000);

T::Currency::make_free_balance_be(&caller, base_currency_amount.unique_saturated_into());
}: _(RawOrigin::Signed(caller), vec![1], Properties(ClassProperty::Transferable | ClassProperty::Burnable))
<T as module::Config>::Currency::make_free_balance_be(&caller, base_currency_amount.unique_saturated_into());
}: _(RawOrigin::Signed(caller), vec![1], Properties(ClassProperty::Transferable | ClassProperty::Burnable), test_attr())

// mint NFT token
mint {
Expand All @@ -57,13 +90,8 @@ benchmarks! {
let to: T::AccountId = account("to", 0, SEED);
let to_lookup = T::Lookup::unlookup(to);

let base_currency_amount = dollar(1000);
T::Currency::make_free_balance_be(&caller, base_currency_amount.unique_saturated_into());

let module_account: T::AccountId = T::PalletId::get().into_sub_account(orml_nft::Pallet::<T>::next_class_id());
crate::Pallet::<T>::create_class(RawOrigin::Signed(caller).into(), vec![1], Properties(ClassProperty::Transferable | ClassProperty::Burnable))?;
T::Currency::make_free_balance_be(&module_account, base_currency_amount.unique_saturated_into());
}: _(RawOrigin::Signed(module_account), to_lookup, 0u32.into(), vec![1], i)
let module_account = create_token_class::<T>(caller)?;
}: _(RawOrigin::Signed(module_account), to_lookup, 0u32.into(), vec![1], test_attr(), i)

// transfer NFT token to another account
transfer {
Expand All @@ -72,13 +100,9 @@ benchmarks! {
let to: T::AccountId = account("to", 0, SEED);
let to_lookup = T::Lookup::unlookup(to.clone());

let base_currency_amount = dollar(1000);
T::Currency::make_free_balance_be(&caller, base_currency_amount.unique_saturated_into());
let module_account = create_token_class::<T>(caller)?;

let module_account: T::AccountId = T::PalletId::get().into_sub_account(orml_nft::Pallet::<T>::next_class_id());
crate::Pallet::<T>::create_class(RawOrigin::Signed(caller).into(), vec![1], Properties(ClassProperty::Transferable | ClassProperty::Burnable))?;
T::Currency::make_free_balance_be(&module_account, base_currency_amount.unique_saturated_into());
crate::Pallet::<T>::mint(RawOrigin::Signed(module_account).into(), to_lookup, 0u32.into(), vec![1], 1)?;
crate::Pallet::<T>::mint(RawOrigin::Signed(module_account).into(), to_lookup, 0u32.into(), vec![1], test_attr(), 1)?;
}: _(RawOrigin::Signed(to), caller_lookup, (0u32.into(), 0u32.into()))

// burn NFT token
Expand All @@ -87,13 +111,9 @@ benchmarks! {
let to: T::AccountId = account("to", 0, SEED);
let to_lookup = T::Lookup::unlookup(to.clone());

let base_currency_amount = dollar(1000);
T::Currency::make_free_balance_be(&caller, base_currency_amount.unique_saturated_into());
let module_account = create_token_class::<T>(caller)?;

let module_account: T::AccountId = T::PalletId::get().into_sub_account(orml_nft::Pallet::<T>::next_class_id());
crate::Pallet::<T>::create_class(RawOrigin::Signed(caller).into(), vec![1], Properties(ClassProperty::Transferable | ClassProperty::Burnable))?;
T::Currency::make_free_balance_be(&module_account, base_currency_amount.unique_saturated_into());
crate::Pallet::<T>::mint(RawOrigin::Signed(module_account).into(), to_lookup, 0u32.into(), vec![1], 1)?;
crate::Pallet::<T>::mint(RawOrigin::Signed(module_account).into(), to_lookup, 0u32.into(), vec![1], test_attr(), 1)?;
}: _(RawOrigin::Signed(to), (0u32.into(), 0u32.into()))

// burn NFT token with remark
Expand All @@ -104,13 +124,9 @@ benchmarks! {
let to: T::AccountId = account("to", 0, SEED);
let to_lookup = T::Lookup::unlookup(to.clone());

let base_currency_amount = dollar(1000);
T::Currency::make_free_balance_be(&caller, base_currency_amount.unique_saturated_into());
let module_account = create_token_class::<T>(caller)?;

let module_account: T::AccountId = T::PalletId::get().into_sub_account(orml_nft::Pallet::<T>::next_class_id());
crate::Pallet::<T>::create_class(RawOrigin::Signed(caller).into(), vec![1], Properties(ClassProperty::Transferable | ClassProperty::Burnable))?;
T::Currency::make_free_balance_be(&module_account, base_currency_amount.unique_saturated_into());
crate::Pallet::<T>::mint(RawOrigin::Signed(module_account).into(), to_lookup, 0u32.into(), vec![1], 1)?;
crate::Pallet::<T>::mint(RawOrigin::Signed(module_account).into(), to_lookup, 0u32.into(), vec![1], test_attr(), 1)?;
}: _(RawOrigin::Signed(to), (0u32.into(), 0u32.into()), remark_message)

// destroy NFT class
Expand All @@ -120,11 +136,17 @@ benchmarks! {

let base_currency_amount = dollar(1000);

T::Currency::make_free_balance_be(&caller, base_currency_amount.unique_saturated_into());
let module_account = create_token_class::<T>(caller)?;

let module_account: T::AccountId = T::PalletId::get().into_sub_account(orml_nft::Pallet::<T>::next_class_id());
crate::Pallet::<T>::create_class(RawOrigin::Signed(caller).into(), vec![1], Properties(ClassProperty::Transferable | ClassProperty::Burnable))?;
}: _(RawOrigin::Signed(module_account), 0u32.into(), caller_lookup)

update_class_properties {
let caller: T::AccountId = account("caller", 0, SEED);
let to: T::AccountId = account("to", 0, SEED);
let to_lookup = T::Lookup::unlookup(to);

let module_account = create_token_class::<T>(caller)?;
}: _(RawOrigin::Signed(module_account), 0u32.into(), Properties(ClassProperty::Transferable.into()))
}

#[cfg(test)]
Expand Down Expand Up @@ -182,6 +204,7 @@ mod mock {
}
parameter_types! {
pub const ExistentialDeposit: u64 = 1;
pub const MaxReserves: u32 = 50;
}
impl pallet_balances::Config for Runtime {
type Balance = Balance;
Expand All @@ -190,6 +213,8 @@ mod mock {
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = frame_system::Pallet<Runtime>;
type MaxLocks = ();
type MaxReserves = MaxReserves;
type ReserveIdentifier = ReserveIdentifier;
type WeightInfo = ();
}
impl pallet_utility::Config for Runtime {
Expand Down Expand Up @@ -257,13 +282,19 @@ mod mock {
parameter_types! {
pub const CreateClassDeposit: Balance = 200;
pub const CreateTokenDeposit: Balance = 100;
pub const DataDepositPerByte: Balance = 10;
pub const NftPalletId: PalletId = PalletId(*b"set/sNFT");
pub MaxAttributesBytes: u32 = 2048;
}

impl crate::Config for Runtime {
type Event = ();
type Currency = Balances;
type CreateClassDeposit = CreateClassDeposit;
type CreateTokenDeposit = CreateTokenDeposit;
type DataDepositPerByte = DataDepositPerByte;
type PalletId = NftPalletId;
type MaxAttributesBytes = MaxAttributesBytes;
type WeightInfo = ();
}

Expand Down Expand Up @@ -314,49 +345,9 @@ mod mock {

#[cfg(test)]
mod tests {
use super::mock::*;
use super::*;
use frame_support::assert_ok;
use mock::{new_test_ext, Runtime};

#[test]
fn test_create_class() {
new_test_ext().execute_with(|| {
assert_ok!(test_benchmark_create_class::<Runtime>());
});
}

#[test]
fn test_mint() {
new_test_ext().execute_with(|| {
assert_ok!(test_benchmark_mint::<Runtime>());
});
}
use frame_benchmarking::impl_benchmark_test_suite;

#[test]
fn test_transfer() {
new_test_ext().execute_with(|| {
assert_ok!(test_benchmark_transfer::<Runtime>());
});
}

#[test]
fn test_burn() {
new_test_ext().execute_with(|| {
assert_ok!(test_benchmark_burn::<Runtime>());
});
}

#[test]
fn test_burn_with_remark() {
new_test_ext().execute_with(|| {
assert_ok!(test_benchmark_burn_with_remark::<Runtime>());
});
}

#[test]
fn test_destroy_class() {
new_test_ext().execute_with(|| {
assert_ok!(test_benchmark_destroy_class::<Runtime>());
});
}
impl_benchmark_test_suite!(Pallet, super::new_test_ext(), super::Runtime,);
}
Loading

0 comments on commit 39b6dbf

Please sign in to comment.