difftreelog
feat(rpc) adminlist/allowlist/last_token_id calls
in: master
10 files changed
client/rpc/src/lib.rsdiffbeforeafterboth--- a/client/rpc/src/lib.rs
+++ b/client/rpc/src/lib.rs
@@ -1,5 +1,6 @@
use std::sync::Arc;
+use codec::Decode;
use jsonrpc_core::{Error as RpcError, ErrorCode, Result};
use jsonrpc_derive::rpc;
use nft_data_structs::{CollectionId, TokenId};
@@ -72,6 +73,13 @@
token: TokenId,
at: Option<BlockHash>,
) -> Result<u128>;
+
+ #[rpc(name = "nft_adminlist")]
+ fn adminlist(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<Vec<AccountId>>;
+ #[rpc(name = "nft_allowlist")]
+ fn allowlist(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<Vec<AccountId>>;
+ #[rpc(name = "nft_lastTokenId")]
+ fn last_token_id(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<TokenId>;
}
pub struct Nft<C, P> {
@@ -125,6 +133,7 @@
for Nft<C, Block>
where
Block: BlockT,
+ AccountId: Decode,
C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
C::Api: NftRuntimeApi<Block, CrossAccountId, AccountId>,
CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,
@@ -138,4 +147,8 @@
pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);
pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128);
pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> u128);
+
+ pass_method!(adminlist(collection: CollectionId) -> Vec<AccountId>);
+ pass_method!(allowlist(collection: CollectionId) -> Vec<AccountId>);
+ pass_method!(last_token_id(collection: CollectionId) -> TokenId);
}
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -109,12 +109,12 @@
pub fn ignores_owned_amount(&self, user: &T::CrossAccountId) -> Result<bool, DispatchError> {
Ok(self.limits.owner_can_transfer && self.is_owner_or_admin(user)?)
}
- pub fn check_whitelist(&self, user: &T::CrossAccountId) -> DispatchResult {
+ pub fn check_allowlist(&self, user: &T::CrossAccountId) -> DispatchResult {
self.consume_sload()?;
ensure!(
- <WhiteList<T>>::get((self.id, user.as_sub())),
- <Error<T>>::AddressNotInWhiteList
+ <Allowlist<T>>::get((self.id, user.as_sub())),
+ <Error<T>>::AddressNotInAllowlist
);
Ok(())
}
@@ -252,7 +252,7 @@
/// Collection is not in mint mode.
PublicMintingNotAllowed,
/// Address is not in white list.
- AddressNotInWhiteList,
+ AddressNotInAllowlist,
/// Collection name can not be longer than 63 char.
CollectionNameLimitExceeded,
@@ -315,9 +315,9 @@
QueryKind = ValueQuery,
>;
- /// Whitelisted collection users
+ /// Allowlisted collection users
#[pallet::storage]
- pub type WhiteList<T: Config> = StorageNMap<
+ pub type Allowlist<T: Config> = StorageNMap<
Key = (
Key<Blake2_128Concat, CollectionId>,
Key<Blake2_128Concat, T::AccountId>,
@@ -398,6 +398,7 @@
<CollectionById<T>>::insert(id, data);
Ok(id)
}
+
pub fn destroy_collection(
collection: CollectionHandle<T>,
sender: &T::CrossAccountId,
@@ -417,11 +418,11 @@
<DestroyedCollectionCount<T>>::put(destroyed_collections);
<CollectionById<T>>::remove(collection.id);
<IsAdmin<T>>::remove_prefix((collection.id,), None);
- <WhiteList<T>>::remove_prefix((collection.id,), None);
+ <Allowlist<T>>::remove_prefix((collection.id,), None);
Ok(())
}
- pub fn toggle_whitelist(
+ pub fn toggle_allowlist(
collection: &CollectionHandle<T>,
sender: &T::CrossAccountId,
user: &T::CrossAccountId,
@@ -432,9 +433,9 @@
// =========
if allowed {
- <WhiteList<T>>::insert((collection.id, user.as_sub()), true);
+ <Allowlist<T>>::insert((collection.id, user.as_sub()), true);
} else {
- <WhiteList<T>>::remove((collection.id, user.as_sub()));
+ <Allowlist<T>>::remove((collection.id, user.as_sub()));
}
Ok(())
@@ -511,6 +512,7 @@
fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId>;
fn token_exists(&self, token: TokenId) -> bool;
+ fn last_token_id(&self) -> TokenId;
fn token_owner(&self, token: TokenId) -> T::CrossAccountId;
fn const_metadata(&self, token: TokenId) -> Vec<u8>;
pallets/fungible/src/common.rsdiffbeforeafterboth--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -177,6 +177,10 @@
token == TokenId::default()
}
+ fn last_token_id(&self) -> TokenId {
+ TokenId::default()
+ }
+
fn token_owner(&self, _token: TokenId) -> T::CrossAccountId {
T::CrossAccountId::default()
}
pallets/fungible/src/lib.rsdiffbeforeafterboth1#![cfg_attr(not(feature = "std"), no_std)]23use core::ops::Deref;4use frame_support::{ensure};5use nft_data_structs::{AccessMode, Collection, CollectionId, TokenId};6use pallet_common::{7 Error as CommonError, Event as CommonEvent, Pallet as PalletCommon, account::CrossAccountId,8};9use sp_core::H160;10use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};11use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap};1213pub use pallet::*;1415use crate::erc::ERC20Events;16pub mod benchmarking;17pub mod common;18pub mod erc;19pub mod weights;2021pub type CreateItemData<T> = (<T as pallet_common::Config>::CrossAccountId, u128);22pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;2324#[frame_support::pallet]25pub mod pallet {26 use frame_support::{Blake2_128, Blake2_128Concat, Twox64Concat, pallet_prelude::*, storage::Key};27 use nft_data_structs::CollectionId;28 use super::weights::WeightInfo;2930 #[pallet::error]31 pub enum Error<T> {32 /// Not Fungible item data used to mint in Fungible collection.33 NotFungibleDataUsedToMintFungibleCollectionToken,34 /// Not default id passed as TokenId argument35 FungibleItemsHaveNoId,36 /// Tried to set data for fungible item37 FungibleItemsHaveData,38 }3940 #[pallet::config]41 pub trait Config: frame_system::Config + pallet_common::Config {42 type WeightInfo: WeightInfo;43 }4445 #[pallet::pallet]46 #[pallet::generate_store(pub(super) trait Store)]47 pub struct Pallet<T>(_);4849 #[pallet::storage]50 pub(super) type TotalSupply<T: Config> =51 StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u128, QueryKind = ValueQuery>;5253 #[pallet::storage]54 pub(super) type Balance<T: Config> = StorageNMap<55 Key = (56 Key<Twox64Concat, CollectionId>,57 Key<Blake2_128Concat, T::AccountId>,58 ),59 Value = u128,60 QueryKind = ValueQuery,61 >;6263 #[pallet::storage]64 pub(super) type Allowance<T: Config> = StorageNMap<65 Key = (66 Key<Twox64Concat, CollectionId>,67 Key<Blake2_128, T::AccountId>,68 Key<Blake2_128Concat, T::AccountId>,69 ),70 Value = u128,71 QueryKind = ValueQuery,72 >;73}7475pub struct FungibleHandle<T: Config>(pallet_common::CollectionHandle<T>);76impl<T: Config> FungibleHandle<T> {77 pub fn cast(inner: pallet_common::CollectionHandle<T>) -> Self {78 Self(inner)79 }80 pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {81 self.082 }83}84impl<T: Config> Deref for FungibleHandle<T> {85 type Target = pallet_common::CollectionHandle<T>;8687 fn deref(&self) -> &Self::Target {88 &self.089 }90}9192impl<T: Config> Pallet<T> {93 pub fn init_collection(data: Collection<T>) -> Result<CollectionId, DispatchError> {94 PalletCommon::init_collection(data)95 }96 pub fn destroy_collection(97 collection: FungibleHandle<T>,98 sender: &T::CrossAccountId,99 ) -> DispatchResult {100 let id = collection.id;101102 // =========103104 PalletCommon::destroy_collection(collection.0, sender)?;105106 <TotalSupply<T>>::remove(id);107 <Balance<T>>::remove_prefix((id,), None);108 <Allowance<T>>::remove_prefix((id,), None);109 Ok(())110 }111112 pub fn burn(113 collection: &FungibleHandle<T>,114 owner: &T::CrossAccountId,115 amount: u128,116 ) -> DispatchResult {117 let total_supply = <TotalSupply<T>>::get(collection.id)118 .checked_sub(amount)119 .ok_or(<CommonError<T>>::TokenValueTooLow)?;120121 let balance = <Balance<T>>::get((collection.id, owner.as_sub()))122 .checked_sub(amount)123 .ok_or(<CommonError<T>>::TokenValueTooLow)?;124125 if collection.access == AccessMode::WhiteList {126 collection.check_whitelist(owner)?;127 }128129 // =========130131 if balance == 0 {132 <Balance<T>>::remove((collection.id, owner.as_sub()));133 } else {134 <Balance<T>>::insert((collection.id, owner.as_sub()), balance);135 }136 <TotalSupply<T>>::insert(collection.id, total_supply);137138 collection.log_infallible(ERC20Events::Transfer {139 from: *owner.as_eth(),140 to: H160::default(),141 value: amount.into(),142 });143 <PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(144 collection.id,145 TokenId::default(),146 owner.clone(),147 amount,148 ));149 Ok(())150 }151152 pub fn transfer(153 collection: &FungibleHandle<T>,154 from: &T::CrossAccountId,155 to: &T::CrossAccountId,156 amount: u128,157 ) -> DispatchResult {158 ensure!(159 collection.transfers_enabled,160 <CommonError<T>>::TransferNotAllowed161 );162163 if collection.access == AccessMode::WhiteList {164 collection.check_whitelist(from)?;165 collection.check_whitelist(to)?;166 }167 <PalletCommon<T>>::ensure_correct_receiver(to)?;168169 let balance_from = <Balance<T>>::get((collection.id, from.as_sub()))170 .checked_sub(amount)171 .ok_or(<CommonError<T>>::TokenValueTooLow)?;172 let balance_to = if from != to {173 Some(174 <Balance<T>>::get((collection.id, to.as_sub()))175 .checked_add(amount)176 .ok_or(ArithmeticError::Overflow)?,177 )178 } else {179 None180 };181182 collection.consume_sstore()?;183 collection.consume_sstore()?;184 collection.consume_log(2, 32)?;185 collection.consume_sstore()?;186187 // =========188189 if let Some(balance_to) = balance_to {190 // from != to191 if balance_from == 0 {192 <Balance<T>>::remove((collection.id, from.as_sub()));193 } else {194 <Balance<T>>::insert((collection.id, from.as_sub()), balance_from);195 }196 <Balance<T>>::insert((collection.id, to.as_sub()), balance_to);197 }198199 collection.log_infallible(ERC20Events::Transfer {200 from: *from.as_eth(),201 to: *to.as_eth(),202 value: amount.into(),203 });204 <PalletCommon<T>>::deposit_event(CommonEvent::Transfer(205 collection.id,206 TokenId::default(),207 from.clone(),208 to.clone(),209 amount,210 ));211 Ok(())212 }213214 pub fn create_multiple_items(215 collection: &FungibleHandle<T>,216 sender: &T::CrossAccountId,217 data: Vec<CreateItemData<T>>,218 ) -> DispatchResult {219 let unrestricted_minting = collection.is_owner_or_admin(sender)?;220 if !unrestricted_minting {221 ensure!(222 collection.mint_mode,223 <CommonError<T>>::PublicMintingNotAllowed224 );225 collection.check_whitelist(sender)?;226227 for (owner, _) in data.iter() {228 collection.check_whitelist(owner)?;229 }230 }231232 let mut balances = BTreeMap::new();233234 let total_supply = data235 .iter()236 .map(|u| u.1)237 .try_fold(0u128, |acc, v| acc.checked_add(v))238 .ok_or(ArithmeticError::Overflow)?;239240 for (user, amount) in data.into_iter() {241 collection.consume_sload()?;242 let balance = balances243 .entry(user.clone())244 .or_insert_with(|| <Balance<T>>::get((collection.id, user.as_sub())));245 *balance = (*balance)246 .checked_add(amount)247 .ok_or(ArithmeticError::Overflow)?;248 }249250 collection.consume_sstore()?;251 for _ in &balances {252 collection.consume_sstore()?;253 collection.consume_log(2, 32)?;254 collection.consume_sstore()?;255 }256257 // =========258259 <TotalSupply<T>>::insert(collection.id, total_supply);260 for (user, amount) in balances {261 <Balance<T>>::insert((collection.id, user.as_sub()), amount);262263 collection.log_infallible(ERC20Events::Transfer {264 from: H160::default(),265 to: *user.as_eth(),266 value: amount.into(),267 });268 <PalletCommon<T>>::deposit_event(CommonEvent::ItemCreated(269 collection.id,270 TokenId::default(),271 user.clone(),272 amount,273 ));274 }275276 Ok(())277 }278279 fn set_allowance_unchecked(280 collection: &FungibleHandle<T>,281 owner: &T::CrossAccountId,282 spender: &T::CrossAccountId,283 amount: u128,284 ) {285 <Allowance<T>>::insert((collection.id, owner.as_sub(), spender.as_sub()), amount);286287 collection.log_infallible(ERC20Events::Approval {288 owner: *owner.as_eth(),289 spender: *spender.as_eth(),290 value: amount.into(),291 });292 <PalletCommon<T>>::deposit_event(CommonEvent::Approved(293 collection.id,294 TokenId(0),295 owner.clone(),296 spender.clone(),297 amount,298 ));299 }300301 pub fn set_allowance(302 collection: &FungibleHandle<T>,303 owner: &T::CrossAccountId,304 spender: &T::CrossAccountId,305 amount: u128,306 ) -> DispatchResult {307 if collection.access == AccessMode::WhiteList {308 collection.check_whitelist(&owner)?;309 collection.check_whitelist(&spender)?;310 }311312 if <Balance<T>>::get((collection.id, owner.as_sub())) < amount {313 ensure!(314 collection.ignores_owned_amount(owner)?,315 <CommonError<T>>::CantApproveMoreThanOwned316 );317 }318319 // =========320321 Self::set_allowance_unchecked(collection, owner, spender, amount);322 Ok(())323 }324325 pub fn transfer_from(326 collection: &FungibleHandle<T>,327 spender: &T::CrossAccountId,328 from: &T::CrossAccountId,329 to: &T::CrossAccountId,330 amount: u128,331 ) -> DispatchResult {332 if spender == from {333 return Self::transfer(collection, from, to, amount);334 }335 if collection.access == AccessMode::WhiteList {336 // `from`, `to` checked in [`transfer`]337 collection.check_whitelist(spender)?;338 }339340 let allowance = <Allowance<T>>::get((collection.id, from.as_sub(), spender.as_sub()))341 .checked_sub(amount);342 if allowance.is_none() {343 ensure!(344 collection.ignores_allowance(spender)?,345 <CommonError<T>>::TokenValueNotEnough346 );347 }348349 // =========350351 Self::transfer(collection, from, to, amount)?;352 if let Some(allowance) = allowance {353 Self::set_allowance_unchecked(collection, from, spender, allowance);354 }355 Ok(())356 }357358 /// Delegated to `create_multiple_items`359 pub fn create_item(360 collection: &FungibleHandle<T>,361 sender: &T::CrossAccountId,362 data: CreateItemData<T>,363 ) -> DispatchResult {364 Self::create_multiple_items(collection, sender, vec![data])365 }366}pallets/nft/src/lib.rsdiffbeforeafterboth--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -41,7 +41,7 @@
};
use pallet_common::{
account::CrossAccountId, CollectionHandle, IsAdmin, Pallet as PalletCommon,
- Error as CommonError, CommonWeightInfo,
+ Error as CommonError, CommonWeightInfo, Allowlist,
};
use pallet_refungible::{Pallet as PalletRefungible, RefungibleHandle};
use pallet_fungible::{Pallet as PalletFungible, FungibleHandle};
@@ -311,7 +311,7 @@
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
let collection = <CollectionHandle<T>>::try_get(collection_id)?;
- <PalletCommon<T>>::toggle_whitelist(
+ <PalletCommon<T>>::toggle_allowlist(
&collection,
&sender,
&address,
@@ -340,7 +340,7 @@
let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
let collection = <CollectionHandle<T>>::try_get(collection_id)?;
- <PalletCommon<T>>::toggle_whitelist(
+ <PalletCommon<T>>::toggle_allowlist(
&collection,
&sender,
&address,
@@ -923,3 +923,17 @@
}
}
}
+
+// TODO: limit returned entries?
+impl<T: Config> Pallet<T> {
+ pub fn adminlist(collection: CollectionId) -> Vec<T::AccountId> {
+ <IsAdmin<T>>::iter_prefix((collection,))
+ .map(|(a, _)| a)
+ .collect()
+ }
+ pub fn allowlist(collection: CollectionId) -> Vec<T::AccountId> {
+ <Allowlist<T>>::iter_prefix((collection,))
+ .map(|(a, _)| a)
+ .collect()
+ }
+}
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -185,6 +185,10 @@
<Pallet<T>>::token_exists(self, token)
}
+ fn last_token_id(&self) -> TokenId {
+ TokenId(<TokensMinted<T>>::get(self.id))
+ }
+
fn token_owner(&self, token: TokenId) -> T::CrossAccountId {
<Owner<T>>::get((self.id, token))
}
pallets/nonfungible/src/lib.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -198,7 +198,7 @@
);
if collection.access == AccessMode::WhiteList {
- collection.check_whitelist(sender)?;
+ collection.check_allowlist(sender)?;
}
let burnt = <TokensBurnt<T>>::get(collection.id)
@@ -246,8 +246,8 @@
);
if collection.access == AccessMode::WhiteList {
- collection.check_whitelist(from)?;
- collection.check_whitelist(to)?;
+ collection.check_allowlist(from)?;
+ collection.check_allowlist(to)?;
}
<PalletCommon<T>>::ensure_correct_receiver(to)?;
@@ -314,10 +314,10 @@
collection.mint_mode,
<CommonError<T>>::PublicMintingNotAllowed
);
- collection.check_whitelist(sender)?;
+ collection.check_allowlist(sender)?;
for item in data.iter() {
- collection.check_whitelist(&item.owner)?;
+ collection.check_allowlist(&item.owner)?;
}
}
@@ -453,9 +453,9 @@
spender: Option<&T::CrossAccountId>,
) -> DispatchResult {
if collection.access == AccessMode::WhiteList {
- collection.check_whitelist(&sender)?;
+ collection.check_allowlist(&sender)?;
if let Some(spender) = spender {
- collection.check_whitelist(&spender)?;
+ collection.check_allowlist(&spender)?;
}
}
@@ -488,7 +488,7 @@
}
if collection.access == AccessMode::WhiteList {
// `from`, `to` checked in [`transfer`]
- collection.check_whitelist(spender)?;
+ collection.check_allowlist(spender)?;
}
if <Allowance<T>>::get((collection.id, token)).as_ref() != Some(spender) {
pallets/refungible/src/lib.rsdiffbeforeafterboth--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -268,8 +268,8 @@
);
if collection.access == AccessMode::WhiteList {
- collection.check_whitelist(from)?;
- collection.check_whitelist(to)?;
+ collection.check_allowlist(from)?;
+ collection.check_allowlist(to)?;
}
<PalletCommon<T>>::ensure_correct_receiver(to)?;
@@ -361,11 +361,11 @@
collection.mint_mode,
<CommonError<T>>::PublicMintingNotAllowed
);
- collection.check_whitelist(sender)?;
+ collection.check_allowlist(sender)?;
for item in data.iter() {
for (user, _) in &item.users {
- collection.check_whitelist(&user)?;
+ collection.check_allowlist(&user)?;
}
}
}
@@ -485,8 +485,8 @@
amount: u128,
) -> DispatchResult {
if collection.access == AccessMode::WhiteList {
- collection.check_whitelist(&sender)?;
- collection.check_whitelist(&spender)?;
+ collection.check_allowlist(&sender)?;
+ collection.check_allowlist(&spender)?;
}
<PalletCommon<T>>::ensure_correct_receiver(spender)?;
@@ -517,7 +517,7 @@
}
if collection.access == AccessMode::WhiteList {
// `from`, `to` checked in [`transfer`]
- collection.check_whitelist(spender)?;
+ collection.check_allowlist(spender)?;
}
let allowance = <Allowance<T>>::get((collection.id, token, from.as_sub(), &spender))
primitives/rpc/src/lib.rsdiffbeforeafterboth--- a/primitives/rpc/src/lib.rs
+++ b/primitives/rpc/src/lib.rs
@@ -3,9 +3,11 @@
use nft_data_structs::{CollectionId, TokenId};
use sp_std::vec::Vec;
use sp_core::H160;
+use codec::Decode;
sp_api::decl_runtime_apis! {
pub trait NftApi<CrossAccountId, AccountId> where
+ AccountId: Decode,
CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,
{
fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>;
@@ -27,5 +29,9 @@
/// Used for ethereum integration
fn eth_contract_code(account: H160) -> Option<Vec<u8>>;
+
+ fn adminlist(collection: CollectionId) -> Vec<AccountId>;
+ fn allowlist(collection: CollectionId) -> Vec<AccountId>;
+ fn last_token_id(collection: CollectionId) -> TokenId;
}
}
runtime/src/lib.rsdiffbeforeafterboth--- a/runtime/src/lib.rs
+++ b/runtime/src/lib.rs
@@ -972,6 +972,15 @@
fn eth_contract_code(account: H160) -> Option<Vec<u8>> {
<pallet_nft::NftErcSupport<Runtime>>::get_code(&account)
}
+ fn adminlist(collection: CollectionId) -> Vec<AccountId> {
+ <pallet_nft::Pallet<Runtime>>::adminlist(collection)
+ }
+ fn allowlist(collection: CollectionId) -> Vec<AccountId> {
+ <pallet_nft::Pallet<Runtime>>::allowlist(collection)
+ }
+ fn last_token_id(collection: CollectionId) -> TokenId {
+ dispatch_nft_runtime!(collection.last_token_id())
+ }
}
impl sp_api::Core<Block> for Runtime {