difftreelog
Merge pull request #463 from UniqueNetwork/feature/remove_const_data_rft
in: master
10 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6311,7 +6311,7 @@
[[package]]
name = "pallet-refungible"
-version = "0.1.2"
+version = "0.2.0"
dependencies = [
"ethereum",
"evm-coder",
@@ -12732,7 +12732,7 @@
[[package]]
name = "up-data-structs"
-version = "0.1.2"
+version = "0.2.0"
dependencies = [
"derivative",
"frame-support",
pallets/refungible/CHANGELOG.mddiffbeforeafterboth--- a/pallets/refungible/CHANGELOG.md
+++ b/pallets/refungible/CHANGELOG.md
@@ -2,6 +2,11 @@
All notable changes to this project will be documented in this file.
+## [v0.2.0] - 2022-08-01
+### Deprecated
+- `ItemData`
+- `TokenData`
+
## [v0.1.2] - 2022-07-14
### Other changes
pallets/refungible/Cargo.tomldiffbeforeafterboth--- a/pallets/refungible/Cargo.toml
+++ b/pallets/refungible/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "pallet-refungible"
-version = "0.1.2"
+version = "0.2.0"
license = "GPLv3"
edition = "2021"
pallets/refungible/src/common.rsdiffbeforeafterboth--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -32,7 +32,7 @@
use crate::{
AccountBalance, Allowance, Balance, Config, Error, Owned, Pallet, RefungibleHandle,
- SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,
+ SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted, TotalSupply,
};
macro_rules! max_weight_of {
@@ -155,7 +155,6 @@
) -> Result<CreateRefungibleExData<T::CrossAccountId>, DispatchError> {
match data {
up_data_structs::CreateItemData::ReFungible(data) => Ok(CreateRefungibleExData {
- const_data: data.const_data,
users: {
let mut out = BTreeMap::new();
out.insert(to.clone(), data.pieces);
@@ -421,7 +420,7 @@
}
fn collection_tokens(&self) -> Vec<TokenId> {
- <TokenData<T>>::iter_prefix((self.id,))
+ <TotalSupply<T>>::iter_prefix((self.id,))
.map(|(id, _)| id)
.collect()
}
pallets/refungible/src/lib.rsdiffbeforeafterboth--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -123,6 +123,7 @@
/// for the convenience of database access. Notably contains the token metadata.
#[struct_versioning::versioned(version = 2, upper)]
#[derive(Encode, Decode, Default, TypeInfo, MaxEncodedLen)]
+#[deprecated(since = "0.2.0", note = "ItemData is no more contains usefull data")]
pub struct ItemData {
pub const_data: BoundedVec<u8, CustomDataLimit>,
@@ -162,7 +163,7 @@
type WeightInfo: WeightInfo;
}
- const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
+ const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);
#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
@@ -180,7 +181,9 @@
StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;
/// Token data, used to partially describe a token.
+ // TODO: remove
#[pallet::storage]
+ #[deprecated(since = "0.2.0", note = "ItemData is no more contains usefull data")]
pub type TokenData<T: Config> = StorageNMap<
Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),
Value = ItemData,
@@ -260,7 +263,11 @@
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
- StorageVersion::new(1).put::<Pallet<T>>();
+ let storage_version = StorageVersion::get::<Pallet<T>>();
+ if storage_version < StorageVersion::new(2) {
+ <TokenData<T>>::remove_all(None);
+ }
+ StorageVersion::new(2).put::<Pallet<T>>();
0
}
@@ -373,7 +380,6 @@
<TokensMinted<T>>::remove(id);
<TokensBurnt<T>>::remove(id);
- <TokenData<T>>::remove_prefix((id,), None);
<TotalSupply<T>>::remove_prefix((id,), None);
<Balance<T>>::remove_prefix((id,), None);
<Allowance<T>>::remove_prefix((id,), None);
@@ -383,7 +389,7 @@
}
fn collection_has_tokens(collection_id: CollectionId) -> bool {
- <TokenData<T>>::iter_prefix((collection_id,))
+ <TotalSupply<T>>::iter_prefix((collection_id,))
.next()
.is_some()
}
@@ -397,7 +403,6 @@
.ok_or(ArithmeticError::Overflow)?;
<TokensBurnt<T>>::insert(collection.id, burnt);
- <TokenData<T>>::remove((collection.id, token_id));
<TokenProperties<T>>::remove((collection.id, token_id));
<TotalSupply<T>>::remove((collection.id, token_id));
<Balance<T>>::remove_prefix((collection.id, token_id), None);
@@ -878,13 +883,6 @@
for (i, data) in data.iter().enumerate() {
let token_id = first_token_id + i as u32 + 1;
<TotalSupply<T>>::insert((collection.id, token_id), totals[i]);
-
- <TokenData<T>>::insert(
- (collection.id, token_id),
- ItemData {
- const_data: data.const_data.clone(),
- },
- );
for (user, amount) in data.users.iter() {
if *amount == 0 {
primitives/data-structs/CHANGELOG.mddiffbeforeafterboth--- a/primitives/data-structs/CHANGELOG.md
+++ b/primitives/data-structs/CHANGELOG.md
@@ -2,6 +2,9 @@
All notable changes to this project will be documented in this file.
+## [v0.2.0] - 2022-08-01
+### Deprecated
+- `CreateReFungibleData::const_data`
## [v0.1.2] - 2022-07-25
### Added
primitives/data-structs/Cargo.tomldiffbeforeafterboth--- a/primitives/data-structs/Cargo.toml
+++ b/primitives/data-structs/Cargo.toml
@@ -6,7 +6,7 @@
license = 'GPLv3'
homepage = "https://unique.network"
repository = 'https://github.com/UniqueNetwork/unique-chain'
-version = '0.1.2'
+version = '0.2.0'
[dependencies]
scale-info = { version = "2.0.1", default-features = false, features = [
primitives/data-structs/src/lib.rsdiffbeforeafterboth--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -780,12 +780,7 @@
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
#[derivative(Debug)]
pub struct CreateReFungibleData {
- /// Immutable metadata of the token
- #[cfg_attr(feature = "serde1", serde(with = "bounded::vec_serde"))]
- #[derivative(Debug(format_with = "bounded::vec_debug"))]
- pub const_data: BoundedVec<u8, CustomDataLimit>,
-
- /// Pieces of created token.
+ /// Number of pieces the RFT is split into
pub pieces: u128,
/// Key-value pairs used to describe the token as metadata
@@ -832,11 +827,6 @@
#[derive(Encode, Decode, MaxEncodedLen, PartialEq, Clone, TypeInfo, Derivative)]
#[derivative(Debug(bound = "CrossAccountId: fmt::Debug + Ord"))]
pub struct CreateRefungibleExData<CrossAccountId> {
- /// Custom data stored in token.
- #[derivative(Debug(format_with = "bounded::vec_debug"))]
- pub const_data: BoundedVec<u8, CustomDataLimit>,
-
- /// Users who will be assigned the specified number of token parts.
#[derivative(Debug(format_with = "bounded::map_debug"))]
pub users: BoundedBTreeMap<CrossAccountId, u128, ConstU32<MAX_ITEMS_PER_BATCH>>,
#[derivative(Debug(format_with = "bounded::vec_debug"))]
@@ -869,16 +859,6 @@
/// Extended data for create ReFungible item in case of
/// single token, which may have many owners
RefungibleMultipleOwners(CreateRefungibleExData<CrossAccountId>),
-}
-
-impl CreateItemData {
- /// Get size of custom data.
- pub fn data_size(&self) -> usize {
- match self {
- CreateItemData::ReFungible(data) => data.const_data.len(),
- _ => 0,
- }
- }
}
impl From<CreateNftData> for CreateItemData {
runtime/common/src/sponsoring.rsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use core::marker::PhantomData;18use up_sponsorship::SponsorshipHandler;19use frame_support::{20 traits::{IsSubType},21 storage::{StorageMap, StorageDoubleMap, StorageNMap},22};23use up_data_structs::{24 CollectionId, FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, NFT_SPONSOR_TRANSFER_TIMEOUT,25 REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT, TokenId, CollectionMode, CreateItemData,26};27use sp_runtime::traits::Saturating;28use pallet_common::{CollectionHandle};29use pallet_evm::account::CrossAccountId;30use pallet_unique::{31 Call as UniqueCall, Config as UniqueConfig, FungibleApproveBasket, RefungibleApproveBasket,32 NftApproveBasket, CreateItemBasket, ReFungibleTransferBasket, FungibleTransferBasket,33 NftTransferBasket, TokenPropertyBasket,34};35use pallet_fungible::Config as FungibleConfig;36use pallet_nonfungible::Config as NonfungibleConfig;37use pallet_refungible::Config as RefungibleConfig;3839pub trait Config: UniqueConfig + FungibleConfig + NonfungibleConfig + RefungibleConfig {}40impl<T> Config for T where T: UniqueConfig + FungibleConfig + NonfungibleConfig + RefungibleConfig {}4142// TODO: permission check?43pub fn withdraw_set_token_property<T: Config>(44 collection: &CollectionHandle<T>,45 who: &T::CrossAccountId,46 item_id: &TokenId,47 data_size: usize,48) -> Option<()> {49 // preliminary sponsoring correctness check50 match collection.mode {51 CollectionMode::NFT => {52 let owner = pallet_nonfungible::TokenData::<T>::get((collection.id, item_id))?.owner;53 if !owner.conv_eq(who) {54 return None;55 }56 }57 CollectionMode::Fungible(_) => {58 // Fungible tokens have no properties59 return None;60 }61 CollectionMode::ReFungible => {62 if !<pallet_refungible::Owned<T>>::get((collection.id, who, item_id)) {63 return None;64 }65 }66 }6768 if data_size > collection.limits.sponsored_data_size() as usize {69 return None;70 }7172 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;73 let limit = collection.limits.sponsored_data_rate_limit()?;7475 if let Some(last_tx_block) = TokenPropertyBasket::<T>::get(collection.id, item_id) {76 let timeout = last_tx_block + limit.into();77 if block_number < timeout {78 return None;79 }80 }8182 <TokenPropertyBasket<T>>::insert(collection.id, item_id, block_number);8384 Some(())85}8687pub fn withdraw_transfer<T: Config>(88 collection: &CollectionHandle<T>,89 who: &T::CrossAccountId,90 item_id: &TokenId,91) -> Option<()> {92 // preliminary sponsoring correctness check93 match collection.mode {94 CollectionMode::NFT => {95 let owner = pallet_nonfungible::TokenData::<T>::get((collection.id, item_id))?.owner;96 if !owner.conv_eq(who) {97 return None;98 }99 }100 CollectionMode::Fungible(_) => {101 if item_id != &TokenId::default() {102 return None;103 }104 if <pallet_fungible::Balance<T>>::get((collection.id, who)) == 0 {105 return None;106 }107 }108 CollectionMode::ReFungible => {109 if !<pallet_refungible::Owned<T>>::get((collection.id, who, item_id)) {110 return None;111 }112 }113 }114115 // sponsor timeout116 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;117 let limit = collection118 .limits119 .sponsor_transfer_timeout(match collection.mode {120 CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,121 CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,122 CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,123 });124125 let last_tx_block = match collection.mode {126 CollectionMode::NFT => <NftTransferBasket<T>>::get(collection.id, item_id),127 CollectionMode::Fungible(_) => {128 <FungibleTransferBasket<T>>::get(collection.id, who.as_sub())129 }130 CollectionMode::ReFungible => {131 <ReFungibleTransferBasket<T>>::get((collection.id, item_id, who.as_sub()))132 }133 };134135 if let Some(last_tx_block) = last_tx_block {136 let timeout = last_tx_block + limit.into();137 if block_number < timeout {138 return None;139 }140 }141142 match collection.mode {143 CollectionMode::NFT => <NftTransferBasket<T>>::insert(collection.id, item_id, block_number),144 CollectionMode::Fungible(_) => {145 <FungibleTransferBasket<T>>::insert(collection.id, who.as_sub(), block_number)146 }147 CollectionMode::ReFungible => <ReFungibleTransferBasket<T>>::insert(148 (collection.id, item_id, who.as_sub()),149 block_number,150 ),151 };152153 Some(())154}155156pub fn withdraw_create_item<T: Config>(157 collection: &CollectionHandle<T>,158 who: &T::CrossAccountId,159 _properties: &CreateItemData,160) -> Option<()> {161 if _properties.data_size() as u32 > collection.limits.sponsored_data_size() {162 return None;163 }164165 // sponsor timeout166 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;167 let limit = collection168 .limits169 .sponsor_transfer_timeout(match _properties {170 CreateItemData::NFT(_) => NFT_SPONSOR_TRANSFER_TIMEOUT,171 CreateItemData::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,172 CreateItemData::ReFungible(_) => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,173 });174175 if let Some(last_tx_block) = <CreateItemBasket<T>>::get((collection.id, who.as_sub())) {176 let timeout = last_tx_block + limit.into();177 if block_number < timeout {178 return None;179 }180 }181182 CreateItemBasket::<T>::insert((collection.id, who.as_sub()), block_number);183184 Some(())185}186187pub fn withdraw_approve<T: Config>(188 collection: &CollectionHandle<T>,189 who: &T::AccountId,190 item_id: &TokenId,191) -> Option<()> {192 // sponsor timeout193 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;194 let limit = collection.limits.sponsor_approve_timeout();195196 let last_tx_block = match collection.mode {197 CollectionMode::NFT => <NftApproveBasket<T>>::get(collection.id, item_id),198 CollectionMode::Fungible(_) => <FungibleApproveBasket<T>>::get(collection.id, who),199 CollectionMode::ReFungible => {200 <RefungibleApproveBasket<T>>::get((collection.id, item_id, who))201 }202 };203204 if let Some(last_tx_block) = last_tx_block {205 let timeout = last_tx_block + limit.into();206 if block_number < timeout {207 return None;208 }209 }210211 match collection.mode {212 CollectionMode::NFT => <NftApproveBasket<T>>::insert(collection.id, item_id, block_number),213 CollectionMode::Fungible(_) => {214 <FungibleApproveBasket<T>>::insert(collection.id, who, block_number)215 }216 CollectionMode::ReFungible => {217 <RefungibleApproveBasket<T>>::insert((collection.id, item_id, who), block_number)218 }219 };220221 Some(())222}223224fn load<T: UniqueConfig>(id: CollectionId) -> Option<(T::AccountId, CollectionHandle<T>)> {225 let collection = CollectionHandle::new(id)?;226 let sponsor = collection.sponsorship.sponsor().cloned()?;227 Some((sponsor, collection))228}229230pub struct UniqueSponsorshipHandler<T>(PhantomData<T>);231impl<T, C> SponsorshipHandler<T::AccountId, C> for UniqueSponsorshipHandler<T>232where233 T: Config,234 C: IsSubType<UniqueCall<T>>,235{236 fn get_sponsor(who: &T::AccountId, call: &C) -> Option<T::AccountId> {237 match IsSubType::<UniqueCall<T>>::is_sub_type(call)? {238 UniqueCall::set_token_properties {239 collection_id,240 token_id,241 properties,242 ..243 } => {244 let (sponsor, collection) = load::<T>(*collection_id)?;245 withdraw_set_token_property(246 &collection,247 &T::CrossAccountId::from_sub(who.clone()),248 &token_id,249 // No overflow may happen, as data larger than usize can't reach here250 properties.iter().map(|p| p.key.len() + p.value.len()).sum(),251 )252 .map(|()| sponsor)253 }254 UniqueCall::create_item {255 collection_id,256 data,257 ..258 } => {259 let (sponsor, collection) = load(*collection_id)?;260 withdraw_create_item::<T>(261 &collection,262 &T::CrossAccountId::from_sub(who.clone()),263 data,264 )265 .map(|()| sponsor)266 }267 UniqueCall::transfer {268 collection_id,269 item_id,270 ..271 } => {272 let (sponsor, collection) = load(*collection_id)?;273 withdraw_transfer::<T>(274 &collection,275 &T::CrossAccountId::from_sub(who.clone()),276 item_id,277 )278 .map(|()| sponsor)279 }280 UniqueCall::transfer_from {281 collection_id,282 item_id,283 from,284 ..285 } => {286 let (sponsor, collection) = load(*collection_id)?;287 withdraw_transfer::<T>(&collection, from, item_id).map(|()| sponsor)288 }289 UniqueCall::approve {290 collection_id,291 item_id,292 ..293 } => {294 let (sponsor, collection) = load(*collection_id)?;295 withdraw_approve::<T>(&collection, who, item_id).map(|()| sponsor)296 }297 _ => None,298 }299 }300}301302pub trait SponsorshipPredict<T: Config> {303 fn predict(collection: CollectionId, account: T::CrossAccountId, token: TokenId) -> Option<u64>304 where305 u64: From<<T as frame_system::Config>::BlockNumber>;306}307308pub struct UniqueSponsorshipPredict<T>(PhantomData<T>);309310impl<T: Config> SponsorshipPredict<T> for UniqueSponsorshipPredict<T> {311 fn predict(collection_id: CollectionId, who: T::CrossAccountId, token: TokenId) -> Option<u64>312 where313 u64: From<<T as frame_system::Config>::BlockNumber>,314 {315 let collection = <CollectionHandle<T>>::try_get(collection_id).ok()?;316 let _ = collection.sponsorship.sponsor()?;317318 // sponsor timeout319 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;320 let limit = collection321 .limits322 .sponsor_transfer_timeout(match collection.mode {323 CollectionMode::NFT => NFT_SPONSOR_TRANSFER_TIMEOUT,324 CollectionMode::Fungible(_) => FUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,325 CollectionMode::ReFungible => REFUNGIBLE_SPONSOR_TRANSFER_TIMEOUT,326 });327328 let last_tx_block = match collection.mode {329 CollectionMode::NFT => <NftTransferBasket<T>>::get(collection.id, token),330 CollectionMode::Fungible(_) => {331 <FungibleTransferBasket<T>>::get(collection.id, who.as_sub())332 }333 CollectionMode::ReFungible => {334 <ReFungibleTransferBasket<T>>::get((collection.id, token, who.as_sub()))335 }336 };337338 if let Some(last_tx_block) = last_tx_block {339 return Some(340 last_tx_block341 .saturating_add(limit.into())342 .saturating_sub(block_number)343 .into(),344 );345 }346347 let token_exists = match collection.mode {348 CollectionMode::NFT => {349 <pallet_nonfungible::TokenData<T>>::contains_key((collection.id, token))350 }351 CollectionMode::Fungible(_) => token == TokenId::default(),352 CollectionMode::ReFungible => {353 <pallet_refungible::TotalSupply<T>>::contains_key((collection.id, token))354 }355 };356357 if token_exists {358 Some(0)359 } else {360 None361 }362 }363}runtime/tests/src/tests.rsdiffbeforeafterboth--- a/runtime/tests/src/tests.rs
+++ b/runtime/tests/src/tests.rs
@@ -62,7 +62,6 @@
fn default_re_fungible_data() -> CreateReFungibleData {
CreateReFungibleData {
- const_data: vec![1, 2, 3].try_into().unwrap(),
pieces: 1023,
properties: vec![Property {
key: b"test-prop".to_vec().try_into().unwrap(),
@@ -298,7 +297,6 @@
let item = <pallet_refungible::TokenData<Test>>::get((collection_id, TokenId(1)));
let balance =
<pallet_refungible::Balance<Test>>::get((collection_id, TokenId(1), account(1)));
- assert_eq!(item.const_data, data.const_data.into_inner());
assert_eq!(balance, 1023);
});
}
@@ -333,7 +331,6 @@
));
let balance =
<pallet_refungible::Balance<Test>>::get((CollectionId(1), TokenId(1), account(1)));
- assert_eq!(item.const_data.to_vec(), data.const_data.into_inner());
assert_eq!(balance, 1023);
}
});
@@ -446,7 +443,6 @@
let data = default_re_fungible_data();
create_test_item(collection_id, &data.clone().into());
let item = <pallet_refungible::TokenData<Test>>::get((collection_id, TokenId(1)));
- assert_eq!(item.const_data, data.const_data.into_inner());
assert_eq!(
<pallet_refungible::AccountBalance<Test>>::get((collection_id, account(1))),
1