This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Refactor nft fractionalisation pallet #13008
Merged
jsidorenko
merged 24 commits into
li/sft-main-branch
from
js/refactor-nft-fractionalisation
Jan 27, 2023
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
3390a6f
Refactoring
jsidorenko 73e717e
Merge branch 'li/sft-main-branch' into js/refactor-nft-fractionalisation
jsidorenko 94b8abc
Make it compile
jsidorenko fa5f494
Add tests
jsidorenko df750b6
Rename
jsidorenko 6eb4f75
Rework nfts locking
jsidorenko b212686
Merge branch 'li/sft-main-branch' into js/refactor-nft-fractionalisation
jsidorenko 6813ae2
Update cargo.lock
jsidorenko 39ddaf4
Connect the latest changes to the runtime-kitchensink
jsidorenko c483867
Add benchmarks, fix other issues
jsidorenko 6af1edf
Chore
jsidorenko d8e4648
Chore 2
jsidorenko dcf60a6
Chore 3
jsidorenko 577290c
Add runtime-benchmarks
jsidorenko d22ce73
Merge branch 'li/sft-main-branch' into js/refactor-nft-fractionalisation
jsidorenko 325bc27
Rename
jsidorenko 6fbe7d3
Set metadata
jsidorenko d861c36
Make fields public
jsidorenko 85f9773
Merge branch 'li/sft-main-branch' into js/refactor-nft-fractionalisation
jsidorenko 9a0f3e0
Chore
jsidorenko 2fc5f6e
Created asset shouldn't be sufficient
jsidorenko bdd59c6
Add documentation
lana-shanghai 69e8871
minor edit to docs
lana-shanghai 72233b7
Minor corrections
lana-shanghai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,17 +40,21 @@ pub mod pallet { | |
use super::*; | ||
use frame_support::pallet_prelude::*; | ||
use frame_system::pallet_prelude::*; | ||
use sp_runtime::traits::One; | ||
use sp_runtime::traits::{One, Zero}; | ||
use std::fmt::Display; | ||
|
||
use frame_support::{ | ||
dispatch::DispatchResult, | ||
sp_runtime::traits::{AccountIdConversion, AtLeast32BitUnsigned, StaticLookup}, | ||
traits::{ | ||
fungibles::{metadata::Mutate as MutateMetadata, Create, Destroy, Inspect, Mutate}, | ||
fungibles::{ | ||
metadata::{CalcMetadataDeposit, Mutate as MutateMetadata}, | ||
Create, Destroy, Inspect, Mutate, | ||
}, | ||
tokens::nonfungibles_v2::{ | ||
Inspect as NonFungiblesInspect, LockableNonfungible, Transfer, | ||
}, | ||
Currency, | ||
Currency, ExistenceRequirement, | ||
}, | ||
PalletId, | ||
}; | ||
|
@@ -73,11 +77,11 @@ pub mod pallet { | |
/// The currency mechanism, used for paying for deposits. | ||
type Currency: Currency<Self::AccountId>; | ||
|
||
/// Identifier for the collection of nft. | ||
type NftCollectionId: Member + Parameter + MaxEncodedLen + Copy; | ||
/// Identifier for the collection of NFT. | ||
type NftCollectionId: Member + Parameter + MaxEncodedLen + Copy + Display; | ||
|
||
/// The type used to identify an nft within a collection. | ||
type NftId: Member + Parameter + MaxEncodedLen + Copy; | ||
/// The type used to identify an NFT within a collection. | ||
type NftId: Member + Parameter + MaxEncodedLen + Copy + Display; | ||
|
||
/// The type used to describe the amount of fractions converted into assets. | ||
type AssetBalance: AtLeast32BitUnsigned | ||
|
@@ -104,16 +108,17 @@ pub mod pallet { | |
+ Create<Self::AccountId> | ||
+ Destroy<Self::AccountId> | ||
+ Mutate<Self::AccountId> | ||
+ MutateMetadata<Self::AccountId>; | ||
+ MutateMetadata<Self::AccountId> | ||
+ CalcMetadataDeposit<<Self::Currency as Currency<Self::AccountId>>::Balance>; | ||
|
||
/// Registry for minted nfts. | ||
/// Registry for minted NFTs. | ||
type Nfts: NonFungiblesInspect< | ||
Self::AccountId, | ||
ItemId = Self::NftId, | ||
CollectionId = Self::NftCollectionId, | ||
> + Transfer<Self::AccountId>; | ||
|
||
/// Locker trait to enable Nft Locking. | ||
/// Locker trait to enable NFT's locking. | ||
type NftLocker: LockableNonfungible<Self::NftCollectionId, Self::NftId>; | ||
|
||
/// The pallet's id, used for deriving its sovereign account ID. | ||
|
@@ -185,10 +190,10 @@ pub mod pallet { | |
ensure!(nft_owner == who, Error::<T>::NoPermission); | ||
|
||
let pallet_account = Self::get_pallet_account(); | ||
|
||
Self::do_lock_nft(nft_collection_id, nft_id)?; | ||
Self::do_create_asset(asset_id, pallet_account)?; | ||
Self::do_create_asset(asset_id, pallet_account.clone())?; | ||
Self::do_mint_asset(asset_id, &beneficiary, fractions)?; | ||
Self::do_set_metadata(asset_id, &who, &pallet_account, &nft_collection_id, &nft_id)?; | ||
|
||
NftToAsset::<T>::insert( | ||
(nft_collection_id, nft_id), | ||
|
@@ -285,5 +290,27 @@ pub mod pallet { | |
T::Assets::burn_from(asset_id, account, amount)?; | ||
T::Assets::start_destroy(asset_id, None) | ||
jsidorenko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// Set the metadata for the newly created asset. | ||
fn do_set_metadata( | ||
asset_id: AssetIdOf<T>, | ||
depositor: &T::AccountId, | ||
pallet_account: &T::AccountId, | ||
nft_collection_id: &T::NftCollectionId, | ||
nft_id: &T::NftId, | ||
) -> DispatchResult { | ||
let symbol = "FRAC"; | ||
let name = format!("Frac {nft_collection_id}-{nft_id}"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if you want to pad these ids so that its always the same cost? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be less readable on the UI if we pad them |
||
let deposit = T::Assets::calc(&name.clone().into(), &symbol.into()); | ||
if deposit != Zero::zero() { | ||
T::Currency::transfer( | ||
&depositor, | ||
&pallet_account, | ||
deposit, | ||
ExistenceRequirement::KeepAlive, | ||
)?; | ||
} | ||
T::Assets::set(asset_id, &pallet_account, name.into(), symbol.into(), 0) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should do_create_asset be taking a reference to pallet_account rather than ownership?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then we would need to deref it inside because of
T::Assets::create()