difftreelog
CORE-390 Add read only flag
in: master
8 files changed
pallets/common/src/erc.rsdiffbeforeafterboth--- a/pallets/common/src/erc.rs
+++ b/pallets/common/src/erc.rs
@@ -87,18 +87,16 @@
check_is_owner(caller, self)?;
let sponsor = T::CrossAccountId::from_eth(sponsor);
- self.set_sponsor(sponsor.as_sub().clone());
- save(self);
- Ok(())
+ self.set_sponsor(sponsor.as_sub().clone()).map_err(dispatch_to_evm::<T>)?;
+ save(self)
}
fn confirm_collection_sponsorship(&mut self, caller: caller) -> Result<void> {
let caller = T::CrossAccountId::from_eth(caller);
- if !self.confirm_sponsorship(caller.as_sub()) {
+ if !self.confirm_sponsorship(caller.as_sub()).map_err(dispatch_to_evm::<T>)? {
return Err(Error::Revert("Caller is not set as sponsor".into()));
}
- save(self);
- Ok(())
+ save(self)
}
#[solidity(rename_selector = "setCollectionLimit")]
@@ -134,8 +132,7 @@
}
self.limits = <Pallet<T>>::clamp_limits(self.mode.clone(), &self.limits, limits)
.map_err(dispatch_to_evm::<T>)?;
- save(self);
- Ok(())
+ save(self)
}
#[solidity(rename_selector = "setCollectionLimit")]
@@ -162,8 +159,7 @@
}
self.limits = <Pallet<T>>::clamp_limits(self.mode.clone(), &self.limits, limits)
.map_err(dispatch_to_evm::<T>)?;
- save(self);
- Ok(())
+ save(self)
}
fn contract_address(&self, _caller: caller) -> Result<address> {
@@ -296,7 +292,7 @@
}
}
-fn check_is_owner<T: Config>(caller: caller, collection: &CollectionHandle<T>) -> Result<()> {
+fn check_is_owner<T: Config>(caller: caller, collection: &CollectionHandle<T>) -> Result<void> {
let caller = T::CrossAccountId::from_eth(caller);
collection
.check_is_owner(&caller)
@@ -315,8 +311,10 @@
Ok(caller)
}
-fn save<T: Config>(collection: &CollectionHandle<T>) {
+fn save<T: Config>(collection: &CollectionHandle<T>) -> Result<void> {
+ collection.check_is_read_only().map_err(dispatch_to_evm::<T>)?;
<crate::CollectionById<T>>::insert(collection.id, collection.collection.clone());
+ Ok(())
}
pub fn token_uri_key() -> up_data_structs::PropertyKey {
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -148,23 +148,35 @@
.saturating_mul(writes),
))
}
-
- pub fn save(self) -> DispatchResult {
+ pub fn save(self) -> Result<(), DispatchError> {
+ self.check_is_read_only()?;
<CollectionById<T>>::insert(self.id, self.collection);
Ok(())
}
- pub fn set_sponsor(&mut self, sponsor: T::AccountId) {
+ pub fn set_sponsor(&mut self, sponsor: T::AccountId) -> DispatchResult {
+ self.check_is_read_only()?;
self.collection.sponsorship = SponsorshipState::Unconfirmed(sponsor);
+ Ok(())
}
- pub fn confirm_sponsorship(&mut self, sender: &T::AccountId) -> bool {
+ pub fn confirm_sponsorship(&mut self, sender: &T::AccountId) -> Result<bool, DispatchError> {
+ self.check_is_read_only()?;
+
if self.collection.sponsorship.pending_sponsor() != Some(sender) {
- return false;
- };
+ return Ok(false);
+ }
self.collection.sponsorship = SponsorshipState::Confirmed(sender.clone());
- true
+ Ok(true)
+ }
+
+ pub fn check_is_read_only(&self) -> DispatchResult {
+ if self.read_only {
+ return Err(<Error<T>>::CollectionNotFound)?;
+ }
+
+ Ok(())
}
}
@@ -434,6 +446,9 @@
/// Empty property keys are forbidden
EmptyPropertyKey,
+
+ /// Collection is read only
+ CollectionIsReadOnly,
}
#[pallet::storage]
@@ -669,6 +684,7 @@
sponsorship,
limits,
permissions,
+ read_only,
} = <CollectionById<T>>::get(collection)?;
let token_property_permissions = <CollectionPropertyPermissions<T>>::get(collection)
@@ -698,6 +714,7 @@
permissions,
token_property_permissions,
properties,
+ read_only,
})
}
}
@@ -778,6 +795,7 @@
Self::clamp_permissions(data.mode.clone(), &Default::default(), permissions)
})
.unwrap_or_else(|| Ok(CollectionPermissions::default()))?,
+ read_only: false,
};
let mut collection_properties = up_data_structs::CollectionProperties::get();
@@ -834,6 +852,7 @@
collection: CollectionHandle<T>,
sender: &T::CrossAccountId,
) -> DispatchResult {
+ collection.check_is_read_only()?;
ensure!(
collection.limits.owner_can_destroy(),
<Error<T>>::NoPermission,
@@ -863,6 +882,7 @@
sender: &T::CrossAccountId,
property: Property,
) -> DispatchResult {
+ collection.check_is_read_only()?;
collection.check_is_owner_or_admin(sender)?;
CollectionProperties::<T>::try_mutate(collection.id, |properties| {
@@ -908,6 +928,8 @@
sender: &T::CrossAccountId,
properties: Vec<Property>,
) -> DispatchResult {
+ collection.check_is_read_only()?;
+
for property in properties {
Self::set_collection_property(collection, sender, property)?;
}
@@ -920,6 +942,7 @@
sender: &T::CrossAccountId,
property_key: PropertyKey,
) -> DispatchResult {
+ collection.check_is_read_only()?;
collection.check_is_owner_or_admin(sender)?;
CollectionProperties::<T>::try_mutate(collection.id, |properties| {
@@ -941,6 +964,8 @@
sender: &T::CrossAccountId,
property_keys: Vec<PropertyKey>,
) -> DispatchResult {
+ collection.check_is_read_only()?;
+
for key in property_keys {
Self::delete_collection_property(collection, sender, key)?;
}
@@ -965,6 +990,7 @@
sender: &T::CrossAccountId,
property_permission: PropertyKeyPermission,
) -> DispatchResult {
+ collection.check_is_read_only()?;
collection.check_is_owner_or_admin(sender)?;
let all_permissions = CollectionPropertyPermissions::<T>::get(collection.id);
@@ -996,6 +1022,8 @@
sender: &T::CrossAccountId,
property_permissions: Vec<PropertyKeyPermission>,
) -> DispatchResult {
+ collection.check_is_read_only()?;
+
for prop_pemission in property_permissions {
Self::set_property_permission(collection, sender, prop_pemission)?;
}
@@ -1083,6 +1111,7 @@
user: &T::CrossAccountId,
allowed: bool,
) -> DispatchResult {
+ collection.check_is_read_only()?;
collection.check_is_owner_or_admin(sender)?;
// =========
@@ -1102,6 +1131,7 @@
user: &T::CrossAccountId,
admin: bool,
) -> DispatchResult {
+ collection.check_is_read_only()?;
collection.check_is_owner_or_admin(sender)?;
let was_admin = <IsAdmin<T>>::get((collection.id, user));
pallets/fungible/src/lib.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/>.1617#![cfg_attr(not(feature = "std"), no_std)]1819use core::ops::Deref;20use evm_coder::ToLog;21use frame_support::{ensure};22use pallet_evm::account::CrossAccountId;23use up_data_structs::{24 AccessMode, CollectionId, TokenId, CreateCollectionData, mapping::TokenAddressMapping,25 budget::Budget,26};27use pallet_common::{28 Error as CommonError, Event as CommonEvent, Pallet as PalletCommon,29 eth::collection_id_to_address,30};31use pallet_evm::Pallet as PalletEvm;32use pallet_structure::Pallet as PalletStructure;33use pallet_evm_coder_substrate::WithRecorder;34use sp_core::H160;35use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};36use sp_std::{collections::btree_map::BTreeMap};3738pub use pallet::*;3940use crate::erc::ERC20Events;41#[cfg(feature = "runtime-benchmarks")]42pub mod benchmarking;43pub mod common;44pub mod erc;45pub mod weights;4647pub type CreateItemData<T> = (<T as pallet_evm::account::Config>::CrossAccountId, u128);48pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;4950#[frame_support::pallet]51pub mod pallet {52 use frame_support::{Blake2_128, Blake2_128Concat, Twox64Concat, pallet_prelude::*, storage::Key};53 use up_data_structs::CollectionId;54 use super::weights::WeightInfo;5556 #[pallet::error]57 pub enum Error<T> {58 /// Not Fungible item data used to mint in Fungible collection.59 NotFungibleDataUsedToMintFungibleCollectionToken,60 /// Not default id passed as TokenId argument61 FungibleItemsHaveNoId,62 /// Tried to set data for fungible item63 FungibleItemsDontHaveData,64 /// Fungible token does not support nested65 FungibleDisallowsNesting,66 /// Setting item properties is not allowed67 SettingPropertiesNotAllowed,68 }6970 #[pallet::config]71 pub trait Config:72 frame_system::Config + pallet_common::Config + pallet_structure::Config + pallet_evm::Config73 {74 type WeightInfo: WeightInfo;75 }7677 #[pallet::pallet]78 #[pallet::generate_store(pub(super) trait Store)]79 pub struct Pallet<T>(_);8081 #[pallet::storage]82 pub type TotalSupply<T: Config> =83 StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u128, QueryKind = ValueQuery>;8485 #[pallet::storage]86 pub type Balance<T: Config> = StorageNMap<87 Key = (88 Key<Twox64Concat, CollectionId>,89 Key<Blake2_128Concat, T::CrossAccountId>,90 ),91 Value = u128,92 QueryKind = ValueQuery,93 >;9495 #[pallet::storage]96 pub type Allowance<T: Config> = StorageNMap<97 Key = (98 Key<Twox64Concat, CollectionId>,99 Key<Blake2_128, T::CrossAccountId>,100 Key<Blake2_128Concat, T::CrossAccountId>,101 ),102 Value = u128,103 QueryKind = ValueQuery,104 >;105}106107pub struct FungibleHandle<T: Config>(pallet_common::CollectionHandle<T>);108impl<T: Config> FungibleHandle<T> {109 pub fn cast(inner: pallet_common::CollectionHandle<T>) -> Self {110 Self(inner)111 }112 pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {113 self.0114 }115 pub fn common_mut(&mut self) -> &mut pallet_common::CollectionHandle<T> {116 &mut self.0117 }118}119impl<T: Config> WithRecorder<T> for FungibleHandle<T> {120 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {121 self.0.recorder()122 }123 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {124 self.0.into_recorder()125 }126}127impl<T: Config> Deref for FungibleHandle<T> {128 type Target = pallet_common::CollectionHandle<T>;129130 fn deref(&self) -> &Self::Target {131 &self.0132 }133}134135impl<T: Config> Pallet<T> {136 pub fn init_collection(137 owner: T::CrossAccountId,138 data: CreateCollectionData<T::AccountId>,139 ) -> Result<CollectionId, DispatchError> {140 <PalletCommon<T>>::init_collection(owner, data)141 }142 pub fn destroy_collection(143 collection: FungibleHandle<T>,144 sender: &T::CrossAccountId,145 ) -> DispatchResult {146 let id = collection.id;147148 if Self::collection_has_tokens(id) {149 return Err(<CommonError<T>>::CantDestroyNotEmptyCollection.into());150 }151152 // =========153154 PalletCommon::destroy_collection(collection.0, sender)?;155156 <TotalSupply<T>>::remove(id);157 <Balance<T>>::remove_prefix((id,), None);158 <Allowance<T>>::remove_prefix((id,), None);159 Ok(())160 }161162 fn collection_has_tokens(collection_id: CollectionId) -> bool {163 <TotalSupply<T>>::get(collection_id) != 0164 }165166 pub fn burn(167 collection: &FungibleHandle<T>,168 owner: &T::CrossAccountId,169 amount: u128,170 ) -> DispatchResult {171 let total_supply = <TotalSupply<T>>::get(collection.id)172 .checked_sub(amount)173 .ok_or(<CommonError<T>>::TokenValueTooLow)?;174175 let balance = <Balance<T>>::get((collection.id, owner))176 .checked_sub(amount)177 .ok_or(<CommonError<T>>::TokenValueTooLow)?;178179 if collection.permissions.access() == AccessMode::AllowList {180 collection.check_allowlist(owner)?;181 }182183 // =========184185 if balance == 0 {186 <Balance<T>>::remove((collection.id, owner));187 <PalletStructure<T>>::unnest_if_nested(owner, collection.id, TokenId::default());188 } else {189 <Balance<T>>::insert((collection.id, owner), balance);190 }191 <TotalSupply<T>>::insert(collection.id, total_supply);192193 <PalletEvm<T>>::deposit_log(194 ERC20Events::Transfer {195 from: *owner.as_eth(),196 to: H160::default(),197 value: amount.into(),198 }199 .to_log(collection_id_to_address(collection.id)),200 );201 <PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(202 collection.id,203 TokenId::default(),204 owner.clone(),205 amount,206 ));207 Ok(())208 }209210 pub fn transfer(211 collection: &FungibleHandle<T>,212 from: &T::CrossAccountId,213 to: &T::CrossAccountId,214 amount: u128,215 nesting_budget: &dyn Budget,216 ) -> DispatchResult {217 ensure!(218 collection.limits.transfers_enabled(),219 <CommonError<T>>::TransferNotAllowed,220 );221222 if collection.permissions.access() == AccessMode::AllowList {223 collection.check_allowlist(from)?;224 collection.check_allowlist(to)?;225 }226 <PalletCommon<T>>::ensure_correct_receiver(to)?;227228 let balance_from = <Balance<T>>::get((collection.id, from))229 .checked_sub(amount)230 .ok_or(<CommonError<T>>::TokenValueTooLow)?;231 let balance_to = if from != to {232 Some(233 <Balance<T>>::get((collection.id, to))234 .checked_add(amount)235 .ok_or(ArithmeticError::Overflow)?,236 )237 } else {238 None239 };240241 // =========242243 <PalletStructure<T>>::nest_if_sent_to_token(244 from.clone(),245 to,246 collection.id,247 TokenId::default(),248 nesting_budget,249 )?;250251 if let Some(balance_to) = balance_to {252 // from != to253 if balance_from == 0 {254 <Balance<T>>::remove((collection.id, from));255 <PalletStructure<T>>::unnest_if_nested(from, collection.id, TokenId::default());256 } else {257 <Balance<T>>::insert((collection.id, from), balance_from);258 }259 <Balance<T>>::insert((collection.id, to), balance_to);260 }261262 <PalletEvm<T>>::deposit_log(263 ERC20Events::Transfer {264 from: *from.as_eth(),265 to: *to.as_eth(),266 value: amount.into(),267 }268 .to_log(collection_id_to_address(collection.id)),269 );270 <PalletCommon<T>>::deposit_event(CommonEvent::Transfer(271 collection.id,272 TokenId::default(),273 from.clone(),274 to.clone(),275 amount,276 ));277 Ok(())278 }279280 pub fn create_multiple_items(281 collection: &FungibleHandle<T>,282 sender: &T::CrossAccountId,283 data: BTreeMap<T::CrossAccountId, u128>,284 nesting_budget: &dyn Budget,285 ) -> DispatchResult {286 if !collection.is_owner_or_admin(sender) {287 ensure!(288 collection.permissions.mint_mode(),289 <CommonError<T>>::PublicMintingNotAllowed290 );291 collection.check_allowlist(sender)?;292293 for (owner, _) in data.iter() {294 collection.check_allowlist(owner)?;295 }296 }297298 let total_supply = data299 .iter()300 .map(|(_, v)| *v)301 .try_fold(<TotalSupply<T>>::get(collection.id), |acc, v| {302 acc.checked_add(v)303 })304 .ok_or(ArithmeticError::Overflow)?;305306 let mut balances = data;307 for (k, v) in balances.iter_mut() {308 *v = <Balance<T>>::get((collection.id, &k))309 .checked_add(*v)310 .ok_or(ArithmeticError::Overflow)?;311 }312313 for (to, _) in balances.iter() {314 <PalletStructure<T>>::check_nesting(315 sender.clone(),316 to,317 collection.id,318 TokenId::default(),319 nesting_budget,320 )?;321 }322323 // =========324325 <TotalSupply<T>>::insert(collection.id, total_supply);326 for (user, amount) in balances {327 <Balance<T>>::insert((collection.id, &user), amount);328 <PalletStructure<T>>::nest_if_sent_to_token_unchecked(329 &user,330 collection.id,331 TokenId::default(),332 );333 <PalletEvm<T>>::deposit_log(334 ERC20Events::Transfer {335 from: H160::default(),336 to: *user.as_eth(),337 value: amount.into(),338 }339 .to_log(collection_id_to_address(collection.id)),340 );341 <PalletCommon<T>>::deposit_event(CommonEvent::ItemCreated(342 collection.id,343 TokenId::default(),344 user.clone(),345 amount,346 ));347 }348349 Ok(())350 }351352 fn set_allowance_unchecked(353 collection: &FungibleHandle<T>,354 owner: &T::CrossAccountId,355 spender: &T::CrossAccountId,356 amount: u128,357 ) {358 if amount == 0 {359 <Allowance<T>>::remove((collection.id, owner, spender));360 } else {361 <Allowance<T>>::insert((collection.id, owner, spender), amount);362 }363364 <PalletEvm<T>>::deposit_log(365 ERC20Events::Approval {366 owner: *owner.as_eth(),367 spender: *spender.as_eth(),368 value: amount.into(),369 }370 .to_log(collection_id_to_address(collection.id)),371 );372 <PalletCommon<T>>::deposit_event(CommonEvent::Approved(373 collection.id,374 TokenId(0),375 owner.clone(),376 spender.clone(),377 amount,378 ));379 }380381 pub fn set_allowance(382 collection: &FungibleHandle<T>,383 owner: &T::CrossAccountId,384 spender: &T::CrossAccountId,385 amount: u128,386 ) -> DispatchResult {387 if collection.permissions.access() == AccessMode::AllowList {388 collection.check_allowlist(owner)?;389 collection.check_allowlist(spender)?;390 }391392 if <Balance<T>>::get((collection.id, owner)) < amount {393 ensure!(394 collection.ignores_owned_amount(owner),395 <CommonError<T>>::CantApproveMoreThanOwned396 );397 }398399 // =========400401 Self::set_allowance_unchecked(collection, owner, spender, amount);402 Ok(())403 }404405 fn check_allowed(406 collection: &FungibleHandle<T>,407 spender: &T::CrossAccountId,408 from: &T::CrossAccountId,409 amount: u128,410 nesting_budget: &dyn Budget,411 ) -> Result<Option<u128>, DispatchError> {412 if spender.conv_eq(from) {413 return Ok(None);414 }415 if collection.permissions.access() == AccessMode::AllowList {416 // `from`, `to` checked in [`transfer`]417 collection.check_allowlist(spender)?;418 }419 if let Some(source) = T::CrossTokenAddressMapping::address_to_token(from) {420 // TODO: should collection owner be allowed to perform this transfer?421 ensure!(422 <PalletStructure<T>>::check_indirectly_owned(423 spender.clone(),424 source.0,425 source.1,426 None,427 nesting_budget428 )?,429 <CommonError<T>>::ApprovedValueTooLow,430 );431 return Ok(None);432 }433 let allowance = <Allowance<T>>::get((collection.id, from, spender)).checked_sub(amount);434 if allowance.is_none() {435 ensure!(436 collection.ignores_allowance(spender),437 <CommonError<T>>::ApprovedValueTooLow438 );439 }440441 Ok(allowance)442 }443444 pub fn transfer_from(445 collection: &FungibleHandle<T>,446 spender: &T::CrossAccountId,447 from: &T::CrossAccountId,448 to: &T::CrossAccountId,449 amount: u128,450 nesting_budget: &dyn Budget,451 ) -> DispatchResult {452 let allowance = Self::check_allowed(collection, spender, from, amount, nesting_budget)?;453454 // =========455456 Self::transfer(collection, from, to, amount, nesting_budget)?;457 if let Some(allowance) = allowance {458 Self::set_allowance_unchecked(collection, from, spender, allowance);459 }460 Ok(())461 }462463 pub fn burn_from(464 collection: &FungibleHandle<T>,465 spender: &T::CrossAccountId,466 from: &T::CrossAccountId,467 amount: u128,468 nesting_budget: &dyn Budget,469 ) -> DispatchResult {470 let allowance = Self::check_allowed(collection, spender, from, amount, nesting_budget)?;471472 // =========473474 Self::burn(collection, from, amount)?;475 if let Some(allowance) = allowance {476 Self::set_allowance_unchecked(collection, from, spender, allowance);477 }478 Ok(())479 }480481 /// Delegated to `create_multiple_items`482 pub fn create_item(483 collection: &FungibleHandle<T>,484 sender: &T::CrossAccountId,485 data: CreateItemData<T>,486 nesting_budget: &dyn Budget,487 ) -> DispatchResult {488 Self::create_multiple_items(489 collection,490 sender,491 [(data.0, data.1)].into_iter().collect(),492 nesting_budget,493 )494 }495}pallets/nonfungible/src/lib.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -336,6 +336,8 @@
sender: &T::CrossAccountId,
token: TokenId,
) -> DispatchResult {
+ collection.check_is_read_only()?;
+
let token_data =
<TokenData<T>>::get((collection.id, token)).ok_or(<CommonError<T>>::TokenNotFound)?;
ensure!(
@@ -456,6 +458,7 @@
&property.key,
is_token_create,
)?;
+ collection.check_is_read_only()?;
<TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {
let property = property.clone();
@@ -494,6 +497,7 @@
property_key: PropertyKey,
) -> DispatchResult {
Self::check_token_change_permission(collection, sender, token_id, &property_key, false)?;
+ collection.check_is_read_only()?;
<TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {
properties.remove(&property_key)
@@ -570,6 +574,8 @@
token_id: TokenId,
property_keys: Vec<PropertyKey>,
) -> DispatchResult {
+ collection.check_is_read_only()?;
+
for key in property_keys {
Self::delete_token_property(collection, sender, token_id, key)?;
}
@@ -616,6 +622,8 @@
token: TokenId,
nesting_budget: &dyn Budget,
) -> DispatchResult {
+ collection.check_is_read_only()?;
+
ensure!(
collection.limits.transfers_enabled(),
<CommonError<T>>::TransferNotAllowed
@@ -894,6 +902,8 @@
token: TokenId,
spender: Option<&T::CrossAccountId>,
) -> DispatchResult {
+ collection.check_is_read_only()?;
+
if collection.permissions.access() == AccessMode::AllowList {
collection.check_allowlist(sender)?;
if let Some(spender) = spender {
pallets/refungible/src/lib.rsdiffbeforeafterboth--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -234,6 +234,7 @@
}
pub fn burn_token(collection: &RefungibleHandle<T>, token_id: TokenId) -> DispatchResult {
+ collection.check_is_read_only()?;
let burnt = <TokensBurnt<T>>::get(collection.id)
.checked_add(1)
.ok_or(ArithmeticError::Overflow)?;
@@ -253,6 +254,7 @@
token: TokenId,
amount: u128,
) -> DispatchResult {
+ collection.check_is_read_only()?;
let total_supply = <TotalSupply<T>>::get((collection.id, token))
.checked_sub(amount)
.ok_or(<CommonError<T>>::TokenValueTooLow)?;
@@ -325,6 +327,7 @@
amount: u128,
nesting_budget: &dyn Budget,
) -> DispatchResult {
+ collection.check_is_read_only()?;
ensure!(
collection.limits.transfers_enabled(),
<CommonError<T>>::TransferNotAllowed
@@ -573,6 +576,7 @@
token: TokenId,
amount: u128,
) -> DispatchResult {
+ collection.check_is_read_only()?;
if collection.permissions.access() == AccessMode::AllowList {
collection.check_allowlist(sender)?;
collection.check_allowlist(spender)?;
pallets/unique/src/lib.rsdiffbeforeafterboth--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -304,6 +304,7 @@
pub fn destroy_collection(origin, collection_id: CollectionId) -> DispatchResult {
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
let collection = <CollectionHandle<T>>::try_get(collection_id)?;
+ collection.check_is_read_only()?;
// =========
@@ -406,6 +407,7 @@
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
+ target_collection.check_is_read_only()?;
target_collection.check_is_owner(&sender)?;
target_collection.owner = new_owner.clone();
@@ -487,7 +489,7 @@
let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
target_collection.check_is_owner(&sender)?;
- target_collection.set_sponsor(new_sponsor.clone());
+ target_collection.set_sponsor(new_sponsor.clone())?;
<Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorSet(
collection_id,
@@ -511,7 +513,7 @@
let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
ensure!(
- target_collection.confirm_sponsorship(&sender),
+ target_collection.confirm_sponsorship(&sender)?,
Error::<T>::ConfirmUnsetSponsorFail
);
primitives/data-structs/src/lib.rsdiffbeforeafterboth--- a/primitives/data-structs/src/lib.rs
+++ b/primitives/data-structs/src/lib.rs
@@ -316,6 +316,9 @@
#[version(2.., upper(Default::default()))]
pub permissions: CollectionPermissions,
+ #[version(2.., upper(false))]
+ pub read_only: bool,
+
#[version(..2)]
pub variable_on_chain_schema: BoundedVec<u8, ConstU32<VARIABLE_ON_CHAIN_SCHEMA_LIMIT>>,
@@ -340,6 +343,7 @@
pub permissions: CollectionPermissions,
pub token_property_permissions: Vec<PropertyKeyPermission>,
pub properties: Vec<Property>,
+ pub read_only: bool,
}
#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, Derivative, MaxEncodedLen)]
tests/src/createCollection.test.tsdiffbeforeafterboth--- a/tests/src/createCollection.test.ts
+++ b/tests/src/createCollection.test.ts
@@ -87,6 +87,20 @@
expect(collection.limits.accountTokenOwnershipLimit.unwrap().toNumber()).to.equal(3);
});
});
+
+ it('Create new collection is not read only', async () => {
+ await usingApi(async api => {
+ const alice = privateKey('//Alice');
+ const tx = api.tx.unique.createCollectionEx({
+ readOnly: true
+ });
+ const events = await submitTransactionAsync(alice, tx);
+ const result = getCreateCollectionResult(events);
+
+ const collection = (await getDetailedCollectionInfo(api, result.collectionId))!;
+ expect(collection.readOnly.toHuman()).to.be.false;
+ });
+ });
});
describe('(!negative test!) integration test: ext. createCollection():', () => {