Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setup proxy account for module-nft #548

Merged
merged 3 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 15 additions & 5 deletions modules/nft/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use sp_std::prelude::*;
use sp_std::vec;

use frame_benchmarking::{account, benchmarks};
use frame_support::traits::Get;
use frame_system::RawOrigin;
use sp_runtime::traits::UniqueSaturatedInto;
use sp_runtime::traits::{AccountIdConversion, UniqueSaturatedInto};

use module_nft::*;
use orml_traits::BasicCurrencyExtended;
Expand Down Expand Up @@ -47,8 +48,12 @@ benchmarks! {
let base_currency_amount = dollar(1000);
<T as orml_currencies::Trait>::NativeCurrency::update_balance(&caller, base_currency_amount.unique_saturated_into())?;

let module_account: T::AccountId = T::ModuleId::get().into_sub_account(orml_nft::Module::<T>::next_class_id());
module_nft::Module::<T>::create_class(RawOrigin::Signed(caller.clone()).into(), vec![1], Properties(ClassProperty::Transferable | ClassProperty::Burnable))?;
}: _(RawOrigin::Signed(caller), to, 0u32.into(), vec![1], i)
<T as orml_currencies::Trait>::NativeCurrency::update_balance(&module_account, base_currency_amount.unique_saturated_into())?;
}: _(RawOrigin::Signed(module_account), to, 0u32.into(), vec![1], i)

//orml_nft::Module::<T>::next_class_id()
wangjj9219 marked this conversation as resolved.
Show resolved Hide resolved

// transfer NFT token to another account
transfer {
Expand All @@ -58,8 +63,10 @@ benchmarks! {
let base_currency_amount = dollar(1000);
<T as orml_currencies::Trait>::NativeCurrency::update_balance(&caller, base_currency_amount.unique_saturated_into())?;

let module_account: T::AccountId = T::ModuleId::get().into_sub_account(orml_nft::Module::<T>::next_class_id());
module_nft::Module::<T>::create_class(RawOrigin::Signed(caller.clone()).into(), vec![1], Properties(ClassProperty::Transferable | ClassProperty::Burnable))?;
module_nft::Module::<T>::mint(RawOrigin::Signed(caller.clone()).into(), to.clone(), 0u32.into(), vec![1], 1)?;
<T as orml_currencies::Trait>::NativeCurrency::update_balance(&module_account, base_currency_amount.unique_saturated_into())?;
module_nft::Module::<T>::mint(RawOrigin::Signed(module_account.clone()).into(), to.clone(), 0u32.into(), vec![1], 1)?;
}: _(RawOrigin::Signed(to), caller, (0u32.into(), 0u32.into()))

// burn NFT token
Expand All @@ -70,8 +77,10 @@ benchmarks! {
let base_currency_amount = dollar(1000);
<T as orml_currencies::Trait>::NativeCurrency::update_balance(&caller, base_currency_amount.unique_saturated_into())?;

let module_account: T::AccountId = T::ModuleId::get().into_sub_account(orml_nft::Module::<T>::next_class_id());
module_nft::Module::<T>::create_class(RawOrigin::Signed(caller.clone()).into(), vec![1], Properties(ClassProperty::Transferable | ClassProperty::Burnable))?;
module_nft::Module::<T>::mint(RawOrigin::Signed(caller).into(), to.clone(), 0u32.into(), vec![1], 1)?;
<T as orml_currencies::Trait>::NativeCurrency::update_balance(&module_account, base_currency_amount.unique_saturated_into())?;
module_nft::Module::<T>::mint(RawOrigin::Signed(module_account.clone()).into(), to.clone(), 0u32.into(), vec![1], 1)?;
}: _(RawOrigin::Signed(to), (0u32.into(), 0u32.into()))

// destroy NFT class
Expand All @@ -82,8 +91,9 @@ benchmarks! {
let base_currency_amount = dollar(1000);
<T as orml_currencies::Trait>::NativeCurrency::update_balance(&caller, base_currency_amount.unique_saturated_into())?;

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

#[cfg(test)]
Expand Down
30 changes: 19 additions & 11 deletions modules/nft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
use codec::{Decode, Encode};
use enumflags2::BitFlags;
use frame_support::{
decl_error, decl_event, decl_module, ensure, traits::Get, transactional, weights::Weight, IterableStorageDoubleMap,
decl_error, decl_event, decl_module, ensure,
traits::{Currency, ExistenceRequirement::KeepAlive, Get, ReservableCurrency},
transactional,
weights::Weight,
IterableStorageDoubleMap, StorageMap,
};
use frame_system::ensure_signed;
use orml_traits::{BasicCurrency, BasicReservableCurrency, NFT};
Expand Down Expand Up @@ -170,13 +174,16 @@ decl_module! {
// `owner` must be a new account, so the free balance will reserve `NewAccountDeposit`.
// use `free_balance(owner)` instead of `deposit`
<T as Trait>::Currency::reserve(&owner, <T as Trait>::Currency::free_balance(&owner))?;
// Depends on https://github.com/paritytech/substrate/issues/7139
// For now, use origin as owner and skip the proxy part
// pallet_proxy::Module<T>::add_proxy(owner, origin, Default::default(), 0)

// owner add proxy delegate to origin
let proxy_deposit = <pallet_proxy::Module<T>>::deposit(1u32);
wangjj9219 marked this conversation as resolved.
Show resolved Hide resolved
<T as pallet_proxy::Trait>::Currency::transfer(&who, &owner, proxy_deposit, KeepAlive)?;
<pallet_proxy::Module<T>>::add_proxy_delegate(&owner, who, Default::default(), 0u32.into())?;
wangjj9219 marked this conversation as resolved.
Show resolved Hide resolved

let data = ClassData { deposit, properties };
orml_nft::Module::<T>::create_class(&who, metadata, data)?;
orml_nft::Module::<T>::create_class(&owner, metadata, data)?;

Self::deposit_event(RawEvent::CreatedClass(who, next_id));
Self::deposit_event(RawEvent::CreatedClass(owner, next_id));
}

/// Mint NFT token
Expand Down Expand Up @@ -205,10 +212,8 @@ decl_module! {
let class_info = orml_nft::Module::<T>::classes(class_id).ok_or(Error::<T>::ClassIdNotFound)?;
ensure!(who == class_info.owner, Error::<T>::NoPermission);
let deposit = T::CreateTokenDeposit::get();
let owner: T::AccountId = T::ModuleId::get().into_sub_account(class_id);
let total_deposit = deposit * (quantity as u128);
<T as Trait>::Currency::transfer(&who, &owner, total_deposit)?;
<T as Trait>::Currency::reserve(&owner, total_deposit)?;
<T as Trait>::Currency::reserve(&class_info.owner, total_deposit)?;

let data = TokenData { deposit };
for _ in 0..quantity {
Expand Down Expand Up @@ -308,8 +313,11 @@ decl_module! {
<T as Trait>::Currency::unreserve(&owner, data.deposit);
<T as Trait>::Currency::transfer(&owner, &dest, data.deposit)?;

// Skip two steps until pallet_proxy is accessable
// pallet_proxy::Module<T>::remove_proxies(owner)
// remove all proxies of class owner
// `remove_proxies` of pallet_proxy is still private
let (_, proxy_deposit) = pallet_proxy::Proxies::<T>::take(&class_info.owner);
<T as pallet_proxy::Trait>::Currency::unreserve(&class_info.owner, proxy_deposit);
wangjj9219 marked this conversation as resolved.
Show resolved Hide resolved

// transfer all free from origin to dest
orml_nft::Module::<T>::destroy_class(&who, class_id)?;

Expand Down
4 changes: 2 additions & 2 deletions modules/nft/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ impl pallet_proxy::Trait for Runtime {
}

type System = frame_system::Module<Runtime>;
type Balances = pallet_balances::Module<Runtime>;
pub type Balances = pallet_balances::Module<Runtime>;
type Utility = pallet_utility::Module<Runtime>;
type Proxy = pallet_proxy::Module<Runtime>;
pub type Proxy = pallet_proxy::Module<Runtime>;

pub type NativeCurrency = orml_currencies::BasicCurrencyAdapter<Runtime, Balances, Amount, BlockNumber>;

Expand Down
Loading