difftreelog
feat(rpc) adminlist/allowlist/last_token_id calls
in: master
10 files changed
client/rpc/src/lib.rsdiffbeforeafterboth1use std::sync::Arc;1use std::sync::Arc;223use codec::Decode;3use jsonrpc_core::{Error as RpcError, ErrorCode, Result};4use jsonrpc_core::{Error as RpcError, ErrorCode, Result};4use jsonrpc_derive::rpc;5use jsonrpc_derive::rpc;5use nft_data_structs::{CollectionId, TokenId};6use nft_data_structs::{CollectionId, TokenId};73 at: Option<BlockHash>,74 at: Option<BlockHash>,74 ) -> Result<u128>;75 ) -> Result<u128>;7677 #[rpc(name = "nft_adminlist")]78 fn adminlist(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<Vec<AccountId>>;79 #[rpc(name = "nft_allowlist")]80 fn allowlist(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<Vec<AccountId>>;81 #[rpc(name = "nft_lastTokenId")]82 fn last_token_id(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<TokenId>;75}83}768477pub struct Nft<C, P> {85pub struct Nft<C, P> {125 for Nft<C, Block>133 for Nft<C, Block>126where134where127 Block: BlockT,135 Block: BlockT,136 AccountId: Decode,128 C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,137 C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,129 C::Api: NftRuntimeApi<Block, CrossAccountId, AccountId>,138 C::Api: NftRuntimeApi<Block, CrossAccountId, AccountId>,130 CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,139 CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,139 pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128);148 pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128);140 pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> u128);149 pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> u128);150151 pass_method!(adminlist(collection: CollectionId) -> Vec<AccountId>);152 pass_method!(allowlist(collection: CollectionId) -> Vec<AccountId>);153 pass_method!(last_token_id(collection: CollectionId) -> TokenId);141}154}142155pallets/common/src/lib.rsdiffbeforeafterboth109 pub fn ignores_owned_amount(&self, user: &T::CrossAccountId) -> Result<bool, DispatchError> {109 pub fn ignores_owned_amount(&self, user: &T::CrossAccountId) -> Result<bool, DispatchError> {110 Ok(self.limits.owner_can_transfer && self.is_owner_or_admin(user)?)110 Ok(self.limits.owner_can_transfer && self.is_owner_or_admin(user)?)111 }111 }112 pub fn check_whitelist(&self, user: &T::CrossAccountId) -> DispatchResult {112 pub fn check_allowlist(&self, user: &T::CrossAccountId) -> DispatchResult {113 self.consume_sload()?;113 self.consume_sload()?;114114115 ensure!(115 ensure!(116 <WhiteList<T>>::get((self.id, user.as_sub())),116 <Allowlist<T>>::get((self.id, user.as_sub())),117 <Error<T>>::AddressNotInWhiteList117 <Error<T>>::AddressNotInAllowlist118 );118 );119 Ok(())119 Ok(())120 }120 }252 /// Collection is not in mint mode.252 /// Collection is not in mint mode.253 PublicMintingNotAllowed,253 PublicMintingNotAllowed,254 /// Address is not in white list.254 /// Address is not in white list.255 AddressNotInWhiteList,255 AddressNotInAllowlist,256256257 /// Collection name can not be longer than 63 char.257 /// Collection name can not be longer than 63 char.258 CollectionNameLimitExceeded,258 CollectionNameLimitExceeded,315 QueryKind = ValueQuery,315 QueryKind = ValueQuery,316 >;316 >;317317318 /// Whitelisted collection users318 /// Allowlisted collection users319 #[pallet::storage]319 #[pallet::storage]320 pub type WhiteList<T: Config> = StorageNMap<320 pub type Allowlist<T: Config> = StorageNMap<321 Key = (321 Key = (322 Key<Blake2_128Concat, CollectionId>,322 Key<Blake2_128Concat, CollectionId>,323 Key<Blake2_128Concat, T::AccountId>,323 Key<Blake2_128Concat, T::AccountId>,417 <DestroyedCollectionCount<T>>::put(destroyed_collections);418 <DestroyedCollectionCount<T>>::put(destroyed_collections);418 <CollectionById<T>>::remove(collection.id);419 <CollectionById<T>>::remove(collection.id);419 <IsAdmin<T>>::remove_prefix((collection.id,), None);420 <IsAdmin<T>>::remove_prefix((collection.id,), None);420 <WhiteList<T>>::remove_prefix((collection.id,), None);421 <Allowlist<T>>::remove_prefix((collection.id,), None);421 Ok(())422 Ok(())422 }423 }423424424 pub fn toggle_whitelist(425 pub fn toggle_allowlist(425 collection: &CollectionHandle<T>,426 collection: &CollectionHandle<T>,426 sender: &T::CrossAccountId,427 sender: &T::CrossAccountId,427 user: &T::CrossAccountId,428 user: &T::CrossAccountId,432 // =========433 // =========433434434 if allowed {435 if allowed {435 <WhiteList<T>>::insert((collection.id, user.as_sub()), true);436 <Allowlist<T>>::insert((collection.id, user.as_sub()), true);436 } else {437 } else {437 <WhiteList<T>>::remove((collection.id, user.as_sub()));438 <Allowlist<T>>::remove((collection.id, user.as_sub()));438 }439 }439440440 Ok(())441 Ok(())511512512 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId>;513 fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId>;513 fn token_exists(&self, token: TokenId) -> bool;514 fn token_exists(&self, token: TokenId) -> bool;515 fn last_token_id(&self) -> TokenId;514516515 fn token_owner(&self, token: TokenId) -> T::CrossAccountId;517 fn token_owner(&self, token: TokenId) -> T::CrossAccountId;516 fn const_metadata(&self, token: TokenId) -> Vec<u8>;518 fn const_metadata(&self, token: TokenId) -> Vec<u8>;pallets/fungible/src/common.rsdiffbeforeafterboth177 token == TokenId::default()177 token == TokenId::default()178 }178 }179180 fn last_token_id(&self) -> TokenId {181 TokenId::default()182 }179183180 fn token_owner(&self, _token: TokenId) -> T::CrossAccountId {184 fn token_owner(&self, _token: TokenId) -> T::CrossAccountId {181 T::CrossAccountId::default()185 T::CrossAccountId::default()pallets/fungible/src/lib.rsdiffbeforeafterboth123 .ok_or(<CommonError<T>>::TokenValueTooLow)?;123 .ok_or(<CommonError<T>>::TokenValueTooLow)?;124124125 if collection.access == AccessMode::WhiteList {125 if collection.access == AccessMode::WhiteList {126 collection.check_whitelist(owner)?;126 collection.check_allowlist(owner)?;127 }127 }128128129 // =========129 // =========161 );161 );162162163 if collection.access == AccessMode::WhiteList {163 if collection.access == AccessMode::WhiteList {164 collection.check_whitelist(from)?;164 collection.check_allowlist(from)?;165 collection.check_whitelist(to)?;165 collection.check_allowlist(to)?;166 }166 }167 <PalletCommon<T>>::ensure_correct_receiver(to)?;167 <PalletCommon<T>>::ensure_correct_receiver(to)?;168168222 collection.mint_mode,222 collection.mint_mode,223 <CommonError<T>>::PublicMintingNotAllowed223 <CommonError<T>>::PublicMintingNotAllowed224 );224 );225 collection.check_whitelist(sender)?;225 collection.check_allowlist(sender)?;226226227 for (owner, _) in data.iter() {227 for (owner, _) in data.iter() {228 collection.check_whitelist(owner)?;228 collection.check_allowlist(owner)?;229 }229 }230 }230 }231231305 amount: u128,305 amount: u128,306 ) -> DispatchResult {306 ) -> DispatchResult {307 if collection.access == AccessMode::WhiteList {307 if collection.access == AccessMode::WhiteList {308 collection.check_whitelist(&owner)?;308 collection.check_allowlist(&owner)?;309 collection.check_whitelist(&spender)?;309 collection.check_allowlist(&spender)?;310 }310 }311311312 if <Balance<T>>::get((collection.id, owner.as_sub())) < amount {312 if <Balance<T>>::get((collection.id, owner.as_sub())) < amount {334 }334 }335 if collection.access == AccessMode::WhiteList {335 if collection.access == AccessMode::WhiteList {336 // `from`, `to` checked in [`transfer`]336 // `from`, `to` checked in [`transfer`]337 collection.check_whitelist(spender)?;337 collection.check_allowlist(spender)?;338 }338 }339339340 let allowance = <Allowance<T>>::get((collection.id, from.as_sub(), spender.as_sub()))340 let allowance = <Allowance<T>>::get((collection.id, from.as_sub(), spender.as_sub()))pallets/nft/src/lib.rsdiffbeforeafterboth41};41};42use pallet_common::{42use pallet_common::{43 account::CrossAccountId, CollectionHandle, IsAdmin, Pallet as PalletCommon,43 account::CrossAccountId, CollectionHandle, IsAdmin, Pallet as PalletCommon,44 Error as CommonError, CommonWeightInfo,44 Error as CommonError, CommonWeightInfo, Allowlist,45};45};46use pallet_refungible::{Pallet as PalletRefungible, RefungibleHandle};46use pallet_refungible::{Pallet as PalletRefungible, RefungibleHandle};47use pallet_fungible::{Pallet as PalletFungible, FungibleHandle};47use pallet_fungible::{Pallet as PalletFungible, FungibleHandle};311 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);311 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);312 let collection = <CollectionHandle<T>>::try_get(collection_id)?;312 let collection = <CollectionHandle<T>>::try_get(collection_id)?;313313314 <PalletCommon<T>>::toggle_whitelist(314 <PalletCommon<T>>::toggle_allowlist(315 &collection,315 &collection,316 &sender,316 &sender,317 &address,317 &address,340 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);340 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);341 let collection = <CollectionHandle<T>>::try_get(collection_id)?;341 let collection = <CollectionHandle<T>>::try_get(collection_id)?;342342343 <PalletCommon<T>>::toggle_whitelist(343 <PalletCommon<T>>::toggle_allowlist(344 &collection,344 &collection,345 &sender,345 &sender,346 &address,346 &address,924 }924 }925}925}926927// TODO: limit returned entries?928impl<T: Config> Pallet<T> {929 pub fn adminlist(collection: CollectionId) -> Vec<T::AccountId> {930 <IsAdmin<T>>::iter_prefix((collection,))931 .map(|(a, _)| a)932 .collect()933 }934 pub fn allowlist(collection: CollectionId) -> Vec<T::AccountId> {935 <Allowlist<T>>::iter_prefix((collection,))936 .map(|(a, _)| a)937 .collect()938 }939}926940pallets/nonfungible/src/common.rsdiffbeforeafterboth185 <Pallet<T>>::token_exists(self, token)185 <Pallet<T>>::token_exists(self, token)186 }186 }187188 fn last_token_id(&self) -> TokenId {189 TokenId(<TokensMinted<T>>::get(self.id))190 }187191188 fn token_owner(&self, token: TokenId) -> T::CrossAccountId {192 fn token_owner(&self, token: TokenId) -> T::CrossAccountId {189 <Owner<T>>::get((self.id, token))193 <Owner<T>>::get((self.id, token))pallets/nonfungible/src/lib.rsdiffbeforeafterboth198 );198 );199199200 if collection.access == AccessMode::WhiteList {200 if collection.access == AccessMode::WhiteList {201 collection.check_whitelist(sender)?;201 collection.check_allowlist(sender)?;202 }202 }203203204 let burnt = <TokensBurnt<T>>::get(collection.id)204 let burnt = <TokensBurnt<T>>::get(collection.id)246 );246 );247247248 if collection.access == AccessMode::WhiteList {248 if collection.access == AccessMode::WhiteList {249 collection.check_whitelist(from)?;249 collection.check_allowlist(from)?;250 collection.check_whitelist(to)?;250 collection.check_allowlist(to)?;251 }251 }252 <PalletCommon<T>>::ensure_correct_receiver(to)?;252 <PalletCommon<T>>::ensure_correct_receiver(to)?;253253314 collection.mint_mode,314 collection.mint_mode,315 <CommonError<T>>::PublicMintingNotAllowed315 <CommonError<T>>::PublicMintingNotAllowed316 );316 );317 collection.check_whitelist(sender)?;317 collection.check_allowlist(sender)?;318318319 for item in data.iter() {319 for item in data.iter() {320 collection.check_whitelist(&item.owner)?;320 collection.check_allowlist(&item.owner)?;321 }321 }322 }322 }323323453 spender: Option<&T::CrossAccountId>,453 spender: Option<&T::CrossAccountId>,454 ) -> DispatchResult {454 ) -> DispatchResult {455 if collection.access == AccessMode::WhiteList {455 if collection.access == AccessMode::WhiteList {456 collection.check_whitelist(&sender)?;456 collection.check_allowlist(&sender)?;457 if let Some(spender) = spender {457 if let Some(spender) = spender {458 collection.check_whitelist(&spender)?;458 collection.check_allowlist(&spender)?;459 }459 }460 }460 }461461488 }488 }489 if collection.access == AccessMode::WhiteList {489 if collection.access == AccessMode::WhiteList {490 // `from`, `to` checked in [`transfer`]490 // `from`, `to` checked in [`transfer`]491 collection.check_whitelist(spender)?;491 collection.check_allowlist(spender)?;492 }492 }493493494 if <Allowance<T>>::get((collection.id, token)).as_ref() != Some(spender) {494 if <Allowance<T>>::get((collection.id, token)).as_ref() != Some(spender) {pallets/refungible/src/lib.rsdiffbeforeafterboth268 );268 );269269270 if collection.access == AccessMode::WhiteList {270 if collection.access == AccessMode::WhiteList {271 collection.check_whitelist(from)?;271 collection.check_allowlist(from)?;272 collection.check_whitelist(to)?;272 collection.check_allowlist(to)?;273 }273 }274 <PalletCommon<T>>::ensure_correct_receiver(to)?;274 <PalletCommon<T>>::ensure_correct_receiver(to)?;275275361 collection.mint_mode,361 collection.mint_mode,362 <CommonError<T>>::PublicMintingNotAllowed362 <CommonError<T>>::PublicMintingNotAllowed363 );363 );364 collection.check_whitelist(sender)?;364 collection.check_allowlist(sender)?;365365366 for item in data.iter() {366 for item in data.iter() {367 for (user, _) in &item.users {367 for (user, _) in &item.users {368 collection.check_whitelist(&user)?;368 collection.check_allowlist(&user)?;369 }369 }370 }370 }371 }371 }485 amount: u128,485 amount: u128,486 ) -> DispatchResult {486 ) -> DispatchResult {487 if collection.access == AccessMode::WhiteList {487 if collection.access == AccessMode::WhiteList {488 collection.check_whitelist(&sender)?;488 collection.check_allowlist(&sender)?;489 collection.check_whitelist(&spender)?;489 collection.check_allowlist(&spender)?;490 }490 }491491492 <PalletCommon<T>>::ensure_correct_receiver(spender)?;492 <PalletCommon<T>>::ensure_correct_receiver(spender)?;517 }517 }518 if collection.access == AccessMode::WhiteList {518 if collection.access == AccessMode::WhiteList {519 // `from`, `to` checked in [`transfer`]519 // `from`, `to` checked in [`transfer`]520 collection.check_whitelist(spender)?;520 collection.check_allowlist(spender)?;521 }521 }522522523 let allowance = <Allowance<T>>::get((collection.id, token, from.as_sub(), &spender))523 let allowance = <Allowance<T>>::get((collection.id, token, from.as_sub(), &spender))primitives/rpc/src/lib.rsdiffbeforeafterboth3use nft_data_structs::{CollectionId, TokenId};3use nft_data_structs::{CollectionId, TokenId};4use sp_std::vec::Vec;4use sp_std::vec::Vec;5use sp_core::H160;5use sp_core::H160;6use codec::Decode;677sp_api::decl_runtime_apis! {8sp_api::decl_runtime_apis! {8 pub trait NftApi<CrossAccountId, AccountId> where9 pub trait NftApi<CrossAccountId, AccountId> where10 AccountId: Decode,9 CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,11 CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,10 {12 {11 fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>;13 fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>;28 /// Used for ethereum integration30 /// Used for ethereum integration29 fn eth_contract_code(account: H160) -> Option<Vec<u8>>;31 fn eth_contract_code(account: H160) -> Option<Vec<u8>>;3233 fn adminlist(collection: CollectionId) -> Vec<AccountId>;34 fn allowlist(collection: CollectionId) -> Vec<AccountId>;35 fn last_token_id(collection: CollectionId) -> TokenId;30 }36 }31}37}3238runtime/src/lib.rsdiffbeforeafterboth972 fn eth_contract_code(account: H160) -> Option<Vec<u8>> {972 fn eth_contract_code(account: H160) -> Option<Vec<u8>> {973 <pallet_nft::NftErcSupport<Runtime>>::get_code(&account)973 <pallet_nft::NftErcSupport<Runtime>>::get_code(&account)974 }974 }975 fn adminlist(collection: CollectionId) -> Vec<AccountId> {976 <pallet_nft::Pallet<Runtime>>::adminlist(collection)977 }978 fn allowlist(collection: CollectionId) -> Vec<AccountId> {979 <pallet_nft::Pallet<Runtime>>::allowlist(collection)980 }981 fn last_token_id(collection: CollectionId) -> TokenId {982 dispatch_nft_runtime!(collection.last_token_id())983 }975 }984 }976985977 impl sp_api::Core<Block> for Runtime {986 impl sp_api::Core<Block> for Runtime {