git.delta.rocks / unique-network / refs/commits / 0a49946aa7b2

difftreelog

fix forbid non-1 tokenid in native fungibles (#1015)

Daniel Shiposha2023-10-16parent: #0cf3df0.patch.diff
in: master
* fix: native fungibles collection

* refactor: move FungibleItemsHaveNoId to pallet-common

6 files changed

modifiedpallets/balances-adapter/src/common.rsdiffbeforeafterboth
1use alloc::{vec, vec::Vec};1use alloc::{vec, vec::Vec};
2use core::marker::PhantomData;2use core::marker::PhantomData;
33
4use frame_support::{fail, weights::Weight};4use frame_support::{ensure, fail, weights::Weight};
5use pallet_balances::{weights::SubstrateWeight as BalancesWeight, WeightInfo};5use pallet_balances::{weights::SubstrateWeight as BalancesWeight, WeightInfo};
6use pallet_common::{CommonCollectionOperations, CommonWeightInfo};6use pallet_common::{CommonCollectionOperations, CommonWeightInfo, Error as CommonError};
7use up_data_structs::TokenId;7use up_data_structs::TokenId;
88
9use crate::{Config, NativeFungibleHandle, Pallet};9use crate::{Config, NativeFungibleHandle, Pallet};
77 _data: up_data_structs::CreateItemData,77 _data: up_data_structs::CreateItemData,
78 _nesting_budget: &dyn up_data_structs::budget::Budget,78 _nesting_budget: &dyn up_data_structs::budget::Budget,
79 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {79 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
80 fail!(<pallet_common::Error<T>>::UnsupportedOperation);80 fail!(<CommonError<T>>::UnsupportedOperation);
81 }81 }
8282
83 fn create_multiple_items(83 fn create_multiple_items(
87 _data: Vec<up_data_structs::CreateItemData>,87 _data: Vec<up_data_structs::CreateItemData>,
88 _nesting_budget: &dyn up_data_structs::budget::Budget,88 _nesting_budget: &dyn up_data_structs::budget::Budget,
89 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {89 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
90 fail!(<pallet_common::Error<T>>::UnsupportedOperation);90 fail!(<CommonError<T>>::UnsupportedOperation);
91 }91 }
9292
93 fn create_multiple_items_ex(93 fn create_multiple_items_ex(
96 _data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,96 _data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,
97 _nesting_budget: &dyn up_data_structs::budget::Budget,97 _nesting_budget: &dyn up_data_structs::budget::Budget,
98 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {98 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
99 fail!(<pallet_common::Error<T>>::UnsupportedOperation);99 fail!(<CommonError<T>>::UnsupportedOperation);
100 }100 }
101101
102 fn burn_item(102 fn burn_item(
105 _token: TokenId,105 _token: TokenId,
106 _amount: u128,106 _amount: u128,
107 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {107 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
108 fail!(<pallet_common::Error<T>>::UnsupportedOperation);108 fail!(<CommonError<T>>::UnsupportedOperation);
109 }109 }
110110
111 fn set_collection_properties(111 fn set_collection_properties(
112 &self,112 &self,
113 _sender: <T>::CrossAccountId,113 _sender: <T>::CrossAccountId,
114 _properties: Vec<up_data_structs::Property>,114 _properties: Vec<up_data_structs::Property>,
115 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {115 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
116 fail!(<pallet_common::Error<T>>::UnsupportedOperation);116 fail!(<CommonError<T>>::UnsupportedOperation);
117 }117 }
118118
119 fn delete_collection_properties(119 fn delete_collection_properties(
120 &self,120 &self,
121 _sender: &<T>::CrossAccountId,121 _sender: &<T>::CrossAccountId,
122 _property_keys: Vec<up_data_structs::PropertyKey>,122 _property_keys: Vec<up_data_structs::PropertyKey>,
123 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {123 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
124 fail!(<pallet_common::Error<T>>::UnsupportedOperation);124 fail!(<CommonError<T>>::UnsupportedOperation);
125 }125 }
126126
127 fn set_token_properties(127 fn set_token_properties(
131 _properties: Vec<up_data_structs::Property>,131 _properties: Vec<up_data_structs::Property>,
132 _budget: &dyn up_data_structs::budget::Budget,132 _budget: &dyn up_data_structs::budget::Budget,
133 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {133 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
134 fail!(<pallet_common::Error<T>>::UnsupportedOperation);134 fail!(<CommonError<T>>::UnsupportedOperation);
135 }135 }
136136
137 fn delete_token_properties(137 fn delete_token_properties(
141 _property_keys: Vec<up_data_structs::PropertyKey>,141 _property_keys: Vec<up_data_structs::PropertyKey>,
142 _budget: &dyn up_data_structs::budget::Budget,142 _budget: &dyn up_data_structs::budget::Budget,
143 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {143 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
144 fail!(<pallet_common::Error<T>>::UnsupportedOperation);144 fail!(<CommonError<T>>::UnsupportedOperation);
145 }145 }
146146
147 fn get_token_properties_raw(147 fn get_token_properties_raw(
161 _sender: &<T>::CrossAccountId,161 _sender: &<T>::CrossAccountId,
162 _property_permissions: Vec<up_data_structs::PropertyKeyPermission>,162 _property_permissions: Vec<up_data_structs::PropertyKeyPermission>,
163 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {163 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
164 fail!(<pallet_common::Error<T>>::UnsupportedOperation);164 fail!(<CommonError<T>>::UnsupportedOperation);
165 }165 }
166166
167 fn transfer(167 fn transfer(
168 &self,168 &self,
169 sender: <T>::CrossAccountId,169 sender: <T>::CrossAccountId,
170 to: <T>::CrossAccountId,170 to: <T>::CrossAccountId,
171 _token: TokenId,171 token: TokenId,
172 amount: u128,172 amount: u128,
173 budget: &dyn up_data_structs::budget::Budget,173 _budget: &dyn up_data_structs::budget::Budget,
174 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {174 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
175 ensure!(
176 token == TokenId::default(),
177 <CommonError<T>>::FungibleItemsHaveNoId
178 );
179
175 <Pallet<T>>::transfer(self, &sender, &to, amount, budget)180 <Pallet<T>>::transfer(&sender, &to, amount)
176 }181 }
177182
178 fn approve(183 fn approve(
182 _token: TokenId,187 _token: TokenId,
183 _amount: u128,188 _amount: u128,
184 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {189 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
185 fail!(<pallet_common::Error<T>>::UnsupportedOperation);190 fail!(<CommonError<T>>::UnsupportedOperation);
186 }191 }
187192
188 fn approve_from(193 fn approve_from(
193 _token: TokenId,198 _token: TokenId,
194 _amount: u128,199 _amount: u128,
195 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {200 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
196 fail!(<pallet_common::Error<T>>::UnsupportedOperation);201 fail!(<CommonError<T>>::UnsupportedOperation);
197 }202 }
198203
199 fn transfer_from(204 fn transfer_from(
200 &self,205 &self,
201 sender: <T>::CrossAccountId,206 sender: <T>::CrossAccountId,
202 from: <T>::CrossAccountId,207 from: <T>::CrossAccountId,
203 to: <T>::CrossAccountId,208 to: <T>::CrossAccountId,
204 _token: TokenId,209 token: TokenId,
205 amount: u128,210 amount: u128,
206 budget: &dyn up_data_structs::budget::Budget,211 budget: &dyn up_data_structs::budget::Budget,
207 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {212 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
213 ensure!(
214 token == TokenId::default(),
215 <CommonError<T>>::FungibleItemsHaveNoId
216 );
217
208 <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, budget)218 <Pallet<T>>::transfer_from(&sender, &from, &to, amount, budget)
209 }219 }
210220
211 fn burn_from(221 fn burn_from(
216 _amount: u128,226 _amount: u128,
217 _budget: &dyn up_data_structs::budget::Budget,227 _budget: &dyn up_data_structs::budget::Budget,
218 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {228 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
219 fail!(<pallet_common::Error<T>>::UnsupportedOperation);229 fail!(<CommonError<T>>::UnsupportedOperation);
220 }230 }
221231
222 fn check_nesting(232 fn check_nesting(
226 _under: TokenId,236 _under: TokenId,
227 _budget: &dyn up_data_structs::budget::Budget,237 _budget: &dyn up_data_structs::budget::Budget,
228 ) -> frame_support::sp_runtime::DispatchResult {238 ) -> frame_support::sp_runtime::DispatchResult {
229 fail!(<pallet_common::Error<T>>::UnsupportedOperation);239 fail!(<CommonError<T>>::UnsupportedOperation);
230 }240 }
231241
232 fn nest(&self, _under: TokenId, _to_nest: (up_data_structs::CollectionId, TokenId)) {}242 fn nest(&self, _under: TokenId, _to_nest: (up_data_structs::CollectionId, TokenId)) {}
332 _operator: <T>::CrossAccountId,342 _operator: <T>::CrossAccountId,
333 _approve: bool,343 _approve: bool,
334 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {344 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
335 fail!(<pallet_common::Error<T>>::UnsupportedOperation);345 fail!(<CommonError<T>>::UnsupportedOperation);
336 }346 }
337347
338 fn allowance_for_all(348 fn allowance_for_all(
347 &self,357 &self,
348 _token: TokenId,358 _token: TokenId,
349 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {359 ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
350 fail!(<pallet_common::Error<T>>::UnsupportedOperation);360 fail!(<CommonError<T>>::UnsupportedOperation);
351 }361 }
352}362}
353363
modifiedpallets/balances-adapter/src/erc.rsdiffbeforeafterboth
58 let caller = T::CrossAccountId::from_eth(caller);58 let caller = T::CrossAccountId::from_eth(caller);
59 let to = T::CrossAccountId::from_eth(to);59 let to = T::CrossAccountId::from_eth(to);
60 let amount = amount.try_into().map_err(|_| "amount overflow")?;60 let amount = amount.try_into().map_err(|_| "amount overflow")?;
61 let budget = self
62 .recorder()
63 .weight_calls_budget(<StructureWeight<T>>::find_parent());
6461
65 <Pallet<T>>::transfer(self, &caller, &to, amount, &budget)62 <Pallet<T>>::transfer(&caller, &to, amount).map_err(|e| dispatch_to_evm::<T>(e.error))?;
66 .map_err(|e| dispatch_to_evm::<T>(e.error))?;
67 Ok(true)63 Ok(true)
68 }64 }
83 .recorder()79 .recorder()
84 .weight_calls_budget(<StructureWeight<T>>::find_parent());80 .weight_calls_budget(<StructureWeight<T>>::find_parent());
8581
86 <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)82 <Pallet<T>>::transfer_from(&caller, &from, &to, amount, &budget)
87 .map_err(|e| dispatch_to_evm::<T>(e.error))?;83 .map_err(|e| dispatch_to_evm::<T>(e.error))?;
88 Ok(true)84 Ok(true)
89 }85 }
106 let caller = T::CrossAccountId::from_eth(caller);102 let caller = T::CrossAccountId::from_eth(caller);
107 let to = to.into_sub_cross_account::<T>()?;103 let to = to.into_sub_cross_account::<T>()?;
108 let amount = amount.try_into().map_err(|_| "amount overflow")?;104 let amount = amount.try_into().map_err(|_| "amount overflow")?;
109 let budget = self
110 .recorder()
111 .weight_calls_budget(<StructureWeight<T>>::find_parent());
112105
113 <Pallet<T>>::transfer(self, &caller, &to, amount, &budget)106 <Pallet<T>>::transfer(&caller, &to, amount).map_err(|e| dispatch_to_evm::<T>(e.error))?;
114 .map_err(|e| dispatch_to_evm::<T>(e.error))?;
115107
116 Ok(true)108 Ok(true)
137 .recorder()129 .recorder()
138 .weight_calls_budget(<StructureWeight<T>>::find_parent());130 .weight_calls_budget(<StructureWeight<T>>::find_parent());
139131
140 <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)132 <Pallet<T>>::transfer_from(&caller, &from, &to, amount, &budget)
141 .map_err(|e| dispatch_to_evm::<T>(e.error))?;133 .map_err(|e| dispatch_to_evm::<T>(e.error))?;
142134
143 Ok(true)135 Ok(true)
modifiedpallets/balances-adapter/src/lib.rsdiffbeforeafterboth
155 Ok(Self::balance_of(from))155 Ok(Self::balance_of(from))
156 }156 }
157157
158 /// Transfers the specified amount of tokens. Will check that158 /// Transfers the specified amount of tokens.
159 /// the transfer is allowed for the token.
160 ///159 ///
161 /// - `collection`: Collection that contains the token.
162 /// - `from`: Owner of tokens to transfer.160 /// - `from`: Owner of tokens to transfer.
163 /// - `to`: Recepient of transfered tokens.161 /// - `to`: Recepient of transfered tokens.
164 /// - `amount`: Amount of tokens to transfer.162 /// - `amount`: Amount of tokens to transfer.
165 /// - `nesting_budget`: Limit for searching parents in-depth to check ownership.
166 pub fn transfer(163 pub fn transfer(
167 _collection: &NativeFungibleHandle<T>,
168 from: &T::CrossAccountId,164 from: &T::CrossAccountId,
169 to: &T::CrossAccountId,165 to: &T::CrossAccountId,
170 amount: u128,166 amount: u128,
171 _nesting_budget: &dyn Budget,
172 ) -> DispatchResultWithPostInfo {167 ) -> DispatchResultWithPostInfo {
173 <PalletCommon<T>>::ensure_correct_receiver(to)?;168 <PalletCommon<T>>::ensure_correct_receiver(to)?;
174169
185 })180 })
186 }181 }
187182
188 /// Transfer NFT token from one account to another.183 /// Transfer tokens from one account to another.
189 ///184 ///
190 /// Same as the [`Self::transfer`] but spender doesn't needs to be the owner of the token.185 /// Same as the [`Self::transfer`] but the spender doesn't needs to be the direct owner of the token.
191 /// The owner should set allowance for the spender to transfer token.186 /// The spender must be allowed to transfer token.
187 /// If the tokens are nested in an NFT and the spender owns the NFT, the allowance is considered to be set.
192 ///188 ///
193 /// - `collection`: Collection that contains the token.
194 /// - `spender`: Account that spend the money.189 /// - `spender`: Account that spend the money.
195 /// - `from`: Owner of tokens to transfer.190 /// - `from`: Owner of tokens to transfer.
196 /// - `to`: Recepient of transfered tokens.191 /// - `to`: Recepient of transfered tokens.
197 /// - `amount`: Amount of tokens to transfer.192 /// - `amount`: Amount of tokens to transfer.
198 /// - `nesting_budget`: Limit for searching parents in-depth to check ownership.193 /// - `nesting_budget`: Limit for searching parents in-depth to check ownership.
199 pub fn transfer_from(194 pub fn transfer_from(
200 collection: &NativeFungibleHandle<T>,
201 spender: &T::CrossAccountId,195 spender: &T::CrossAccountId,
202 from: &T::CrossAccountId,196 from: &T::CrossAccountId,
203 to: &T::CrossAccountId,197 to: &T::CrossAccountId,
208 if allowance < amount {202 if allowance < amount {
209 return Err(<CommonError<T>>::ApprovedValueTooLow.into());203 return Err(<CommonError<T>>::ApprovedValueTooLow.into());
210 }204 }
211 Self::transfer(collection, from, to, amount, nesting_budget)205 Self::transfer(from, to, amount)
212 }206 }
213 }207 }
214}208}
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
784 /// The user is not an administrator.784 /// The user is not an administrator.
785 UserIsNotCollectionAdmin,785 UserIsNotCollectionAdmin,
786
787 /// Fungible tokens hold no ID, and the default value of TokenId for a fungible collection is 0.
788 FungibleItemsHaveNoId,
786 }789 }
787790
788 /// Storage of the count of created collections. Essentially contains the last collection ID.791 /// Storage of the count of created collections. Essentially contains the last collection ID.
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
19use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight};19use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight};
20use pallet_common::{20use pallet_common::{
21 weights::WeightInfo as _, with_weight, CommonCollectionOperations, CommonWeightInfo,21 weights::WeightInfo as _, with_weight, CommonCollectionOperations, CommonWeightInfo,
22 RefungibleExtensions, SelfWeightOf as PalletCommonWeightOf,22 Error as CommonError, RefungibleExtensions, SelfWeightOf as PalletCommonWeightOf,
23};23};
24use sp_runtime::{ArithmeticError, DispatchError};24use sp_runtime::{ArithmeticError, DispatchError};
25use sp_std::{vec, vec::Vec};25use sp_std::{vec, vec::Vec};
169 ) -> DispatchResultWithPostInfo {169 ) -> DispatchResultWithPostInfo {
170 ensure!(170 ensure!(
171 token == TokenId::default(),171 token == TokenId::default(),
172 <Error<T>>::FungibleItemsHaveNoId172 <CommonError<T>>::FungibleItemsHaveNoId
173 );173 );
174174
175 with_weight(175 with_weight(
188 ) -> DispatchResultWithPostInfo {188 ) -> DispatchResultWithPostInfo {
189 ensure!(189 ensure!(
190 token == TokenId::default(),190 token == TokenId::default(),
191 <Error<T>>::FungibleItemsHaveNoId191 <CommonError<T>>::FungibleItemsHaveNoId
192 );192 );
193193
194 <Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget)194 <Pallet<T>>::transfer(self, &from, &to, amount, nesting_budget)
203 ) -> DispatchResultWithPostInfo {203 ) -> DispatchResultWithPostInfo {
204 ensure!(204 ensure!(
205 token == TokenId::default(),205 token == TokenId::default(),
206 <Error<T>>::FungibleItemsHaveNoId206 <CommonError<T>>::FungibleItemsHaveNoId
207 );207 );
208208
209 with_weight(209 with_weight(
222 ) -> DispatchResultWithPostInfo {222 ) -> DispatchResultWithPostInfo {
223 ensure!(223 ensure!(
224 token == TokenId::default(),224 token == TokenId::default(),
225 <Error<T>>::FungibleItemsHaveNoId225 <CommonError<T>>::FungibleItemsHaveNoId
226 );226 );
227227
228 with_weight(228 with_weight(
242 ) -> DispatchResultWithPostInfo {242 ) -> DispatchResultWithPostInfo {
243 ensure!(243 ensure!(
244 token == TokenId::default(),244 token == TokenId::default(),
245 <Error<T>>::FungibleItemsHaveNoId245 <CommonError<T>>::FungibleItemsHaveNoId
246 );246 );
247247
248 <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget)248 <Pallet<T>>::transfer_from(self, &sender, &from, &to, amount, nesting_budget)
258 ) -> DispatchResultWithPostInfo {258 ) -> DispatchResultWithPostInfo {
259 ensure!(259 ensure!(
260 token == TokenId::default(),260 token == TokenId::default(),
261 <Error<T>>::FungibleItemsHaveNoId261 <CommonError<T>>::FungibleItemsHaveNoId
262 );262 );
263263
264 with_weight(264 with_weight(
modifiedpallets/fungible/src/lib.rsdiffbeforeafterboth
123 pub enum Error<T> {123 pub enum Error<T> {
124 /// Not Fungible item data used to mint in Fungible collection.124 /// Not Fungible item data used to mint in Fungible collection.
125 NotFungibleDataUsedToMintFungibleCollectionToken,125 NotFungibleDataUsedToMintFungibleCollectionToken,
126 /// Fungible tokens hold no ID, and the default value of TokenId for Fungible collection is 0.
127 FungibleItemsHaveNoId,
128 /// Tried to set data for fungible item.126 /// Tried to set data for fungible item.
129 FungibleItemsDontHaveData,127 FungibleItemsDontHaveData,
130 /// Fungible token does not support nesting.128 /// Fungible token does not support nesting.