git.delta.rocks / unique-network / refs/commits / d54d8c2c28b6

difftreelog

refactor(refungible) merge TokenData

Yaroslav Bolyukin2021-10-22parent: #ed4e1ec.patch.diff
in: master

2 files changed

modifiedpallets/refungible/src/common.rsdiffbeforeafterboth
10use sp_std::vec::Vec;10use sp_std::vec::Vec;
1111
12use crate::{12use crate::{
13 AccountBalance, Allowance, Balance, Config, CreateItemData, DataKind, Error, Owned, Pallet,13 AccountBalance, Allowance, Balance, Config, CreateItemData, Error, Owned, Pallet,
14 RefungibleHandle, SelfWeightOf, TokenData, weights::WeightInfo,14 RefungibleHandle, SelfWeightOf, TokenData, weights::WeightInfo, TokensMinted,
15};15};
1616
17pub struct CommonWeights<T: Config>(PhantomData<T>);17pub struct CommonWeights<T: Config>(PhantomData<T>);
168 <Pallet<T>>::token_exists(self, token)168 <Pallet<T>>::token_exists(self, token)
169 }169 }
170
171 fn last_token_id(&self) -> TokenId {
172 TokenId(<TokensMinted<T>>::get(self.id))
173 }
170174
171 fn token_owner(&self, _token: TokenId) -> T::CrossAccountId {175 fn token_owner(&self, _token: TokenId) -> T::CrossAccountId {
172 T::CrossAccountId::default()176 T::CrossAccountId::default()
173 }177 }
174 fn const_metadata(&self, token: TokenId) -> Vec<u8> {178 fn const_metadata(&self, token: TokenId) -> Vec<u8> {
175 <TokenData<T>>::get((self.id, token, DataKind::Constant))179 <TokenData<T>>::get((self.id, token)).const_data
176 }180 }
177 fn variable_metadata(&self, token: TokenId) -> Vec<u8> {181 fn variable_metadata(&self, token: TokenId) -> Vec<u8> {
178 <TokenData<T>>::get((self.id, token, DataKind::Variable))182 <TokenData<T>>::get((self.id, token)).variable_data
179 }183 }
180184
181 fn collection_tokens(&self) -> u32 {185 fn collection_tokens(&self) -> u32 {
modifiedpallets/refungible/src/lib.rsdiffbeforeafterboth
11use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};11use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};
12use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap};12use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap};
13use core::ops::Deref;13use core::ops::Deref;
14use codec::{Encode, Decode};
1415
15pub use pallet::*;16pub use pallet::*;
16pub mod benchmarking;17pub mod benchmarking;
24}25}
25pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;26pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;
27
28#[derive(Encode, Decode, Default)]
29pub struct ItemData {
30 pub const_data: Vec<u8>,
31 pub variable_data: Vec<u8>,
32}
2633
27#[frame_support::pallet]34#[frame_support::pallet]
28pub mod pallet {35pub mod pallet {
36 use super::*;
29 use frame_support::{Blake2_128, Blake2_128Concat, Twox64Concat, pallet_prelude::*, storage::Key};37 use frame_support::{Blake2_128, Blake2_128Concat, Twox64Concat, pallet_prelude::*, storage::Key};
30 use sp_std::vec::Vec;
31 use nft_data_structs::{CollectionId, TokenId};38 use nft_data_structs::{CollectionId, TokenId};
32 use super::weights::WeightInfo;39 use super::weights::WeightInfo;
3340
55 pub(super) type TokensBurnt<T: Config> =62 pub(super) type TokensBurnt<T: Config> =
56 StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;63 StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;
57
58 #[derive(Encode, Decode)]
59 pub enum DataKind {
60 Constant,
61 Variable,
62 }
6364
64 #[pallet::storage]65 #[pallet::storage]
65 pub(super) type TokenData<T: Config> = StorageNMap<66 pub(super) type TokenData<T: Config> = StorageNMap<
66 Key = (67 Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),
67 Key<Twox64Concat, CollectionId>,
68 Key<Twox64Concat, TokenId>,
69 Key<Identity, DataKind>,
70 ),
71 Value = Vec<u8>,68 Value = ItemData,
72 QueryKind = ValueQuery,69 QueryKind = ValueQuery,
73 >;70 >;
7471
185 .ok_or(ArithmeticError::Overflow)?;182 .ok_or(ArithmeticError::Overflow)?;
186183
187 <TokensBurnt<T>>::insert(collection.id, burnt);184 <TokensBurnt<T>>::insert(collection.id, burnt);
188 <TokenData<T>>::remove_prefix((collection.id, token_id), None);185 <TokenData<T>>::remove((collection.id, token_id));
189 <TotalSupply<T>>::remove((collection.id, token_id));186 <TotalSupply<T>>::remove((collection.id, token_id));
190 <Balance<T>>::remove_prefix((collection.id, token_id), None);187 <Balance<T>>::remove_prefix((collection.id, token_id), None);
191 <Allowance<T>>::remove_prefix((collection.id, token_id), None);188 <Allowance<T>>::remove_prefix((collection.id, token_id), None);
210 <Balance<T>>::get((collection.id, token, owner.as_sub())) == amount,207 <Balance<T>>::get((collection.id, token, owner.as_sub())) == amount,
211 <CommonError<T>>::TokenValueTooLow208 <CommonError<T>>::TokenValueTooLow
212 );209 );
210 let account_balance = <AccountBalance<T>>::get((collection.id, owner.as_sub()))
211 .checked_sub(1)
212 // Should not occur
213 .ok_or(ArithmeticError::Underflow)?;
213214
214 // =========215 // =========
215216
216 <Owned<T>>::remove((collection.id, owner.as_sub(), token));217 <Owned<T>>::remove((collection.id, owner.as_sub(), token));
218 <AccountBalance<T>>::insert((collection.id, owner.as_sub()), account_balance);
217 Self::burn_token(collection, token)?;219 Self::burn_token(collection, token)?;
218 <PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(220 <PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(
219 collection.id,221 collection.id,
239 // =========241 // =========
240242
241 if balance == 0 {243 if balance == 0 {
244 <Owned<T>>::remove((collection.id, owner.as_sub(), token));
242 <Balance<T>>::remove((collection.id, token, owner.as_sub()));245 <Balance<T>>::remove((collection.id, token, owner.as_sub()));
243 <AccountBalance<T>>::insert((collection.id, owner.as_sub()), account_balance);246 <AccountBalance<T>>::insert((collection.id, owner.as_sub()), account_balance);
244 } else {247 } else {
330 <Balance<T>>::insert((collection.id, token, to.as_sub()), balance_to);333 <Balance<T>>::insert((collection.id, token, to.as_sub()), balance_to);
331 if let Some(account_balance_from) = account_balance_from {334 if let Some(account_balance_from) = account_balance_from {
332 <AccountBalance<T>>::insert((collection.id, from.as_sub()), account_balance_from);335 <AccountBalance<T>>::insert((collection.id, from.as_sub()), account_balance_from);
336 <Owned<T>>::remove((collection.id, from.as_sub(), token));
333 }337 }
334 if let Some(account_balance_to) = account_balance_to {338 if let Some(account_balance_to) = account_balance_to {
335 <AccountBalance<T>>::insert((collection.id, to.as_sub()), account_balance_to);
336 }
337
338 <Owned<T>>::remove((collection.id, from.as_sub(), token));339 <AccountBalance<T>>::insert((collection.id, to.as_sub()), account_balance_to);
339 <Owned<T>>::insert((collection.id, to.as_sub(), token), true);340 <Owned<T>>::insert((collection.id, to.as_sub(), token), true);
341 }
340 }342 }
341343
342 // TODO: ERC20 transfer event344 // TODO: ERC20 transfer event
426 <AccountBalance<T>>::insert((collection.id, account), balance);428 <AccountBalance<T>>::insert((collection.id, account), balance);
427 }429 }
428 for (i, token) in data.into_iter().enumerate() {430 for (i, token) in data.into_iter().enumerate() {
429 let token_id = first_token_id + i as u32;431 let token_id = first_token_id + i as u32 + 1;
430 <TotalSupply<T>>::insert((collection.id, token_id), totals[i]);432 <TotalSupply<T>>::insert((collection.id, token_id), totals[i]);
431433
432 if !token.const_data.is_empty() {
433 <TokenData<T>>::insert(
434 (collection.id, token_id, DataKind::Constant),
435 token.const_data,
436 );
437 }
438 if !token.variable_data.is_empty() {
439 <TokenData<T>>::insert(434 <TokenData<T>>::insert(
440 (collection.id, token_id, DataKind::Variable),435 (collection.id, token_id),
436 ItemData {
437 const_data: token.const_data.into(),
441 token.variable_data,438 variable_data: token.variable_data.into(),
439 },
442 );440 );
443 }
444 for (user, amount) in token.users.into_iter() {441 for (user, amount) in token.users.into_iter() {
445 if amount == 0 {442 if amount == 0 {
446 continue;443 continue;
554 )?;551 )?;
555552
556 collection.consume_sstore()?;553 collection.consume_sstore()?;
554 let token_data = <TokenData<T>>::get((collection.id, token));
557555
558 // =========556 // =========
559557
560 <TokenData<T>>::insert((collection.id, token, DataKind::Variable), data);558 <TokenData<T>>::insert(
559 (collection.id, token),
560 ItemData {
561 variable_data: data,
562 ..token_data
563 },
564 );
561 Ok(())565 Ok(())
562 }566 }