difftreelog
Test all collection for total_pieces
in: master
10 files changed
client/rpc/src/lib.rsdiffbeforeafterboth--- a/client/rpc/src/lib.rs
+++ b/client/rpc/src/lib.rs
@@ -198,7 +198,7 @@
collection_id: CollectionId,
token_id: TokenId,
at: Option<BlockHash>,
- ) -> Result<u128>;
+ ) -> Result<Option<u128>>;
}
mod rmrk_unique_rpc {
@@ -472,7 +472,7 @@
pass_method!(collection_stats() -> CollectionStats, unique_api);
pass_method!(next_sponsored(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Option<u64>, unique_api);
pass_method!(effective_collection_limits(collection_id: CollectionId) -> Option<CollectionLimits>, unique_api);
- pass_method!(total_pieces(collection_id: CollectionId, token_id: TokenId) -> u128, unique_api);
+ pass_method!(total_pieces(collection_id: CollectionId, token_id: TokenId) -> Option<u128>, unique_api);
}
#[allow(deprecated)]
pallets/common/src/lib.rsdiffbeforeafterboth--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -1383,7 +1383,7 @@
/// Amount of specific token account have (Applicable to fungible/refungible)
fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128;
/// Amount of token pieces
- fn total_pieces(&self, token: TokenId) -> u128;
+ fn total_pieces(&self, token: TokenId) -> Option<u128>;
fn allowance(
&self,
sender: T::CrossAccountId,
pallets/fungible/src/common.rsdiffbeforeafterboth--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -25,7 +25,8 @@
use up_data_structs::{Property, PropertyKey, PropertyValue, PropertyKeyPermission};
use crate::{
- Allowance, Balance, Config, Error, FungibleHandle, Pallet, SelfWeightOf, weights::WeightInfo,
+ Allowance, TotalSupply, Balance, Config, Error, FungibleHandle, Pallet, SelfWeightOf,
+ weights::WeightInfo,
};
pub struct CommonWeights<T: Config>(PhantomData<T>);
@@ -404,7 +405,10 @@
None
}
- fn total_pieces(&self, _token: TokenId) -> u128 {
- 0
+ fn total_pieces(&self, token: TokenId) -> Option<u128> {
+ if token != TokenId::default() {
+ return None;
+ }
+ <TotalSupply<T>>::try_get(self.id).ok()
}
}
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -473,7 +473,7 @@
None
}
- fn total_pieces(&self, _token: TokenId) -> u128 {
- 1
+ fn total_pieces(&self, _token: TokenId) -> Option<u128> {
+ Some(1)
}
}
pallets/refungible/src/common.rsdiffbeforeafterboth--- a/pallets/refungible/src/common.rs
+++ b/pallets/refungible/src/common.rs
@@ -410,7 +410,7 @@
Some(self)
}
- fn total_pieces(&self, token: TokenId) -> u128 {
+ fn total_pieces(&self, token: TokenId) -> Option<u128> {
<Pallet<T>>::total_pieces(self.id, token)
}
}
pallets/refungible/src/lib.rsdiffbeforeafterboth--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -711,7 +711,7 @@
Ok(())
}
- fn total_pieces(collection_id: CollectionId, token_id: TokenId) -> u128 {
- <TotalSupply<T>>::get((collection_id, token_id))
+ fn total_pieces(collection_id: CollectionId, token_id: TokenId) -> Option<u128> {
+ <TotalSupply<T>>::try_get((collection_id, token_id)).ok()
}
}
primitives/rpc/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 up_data_structs::{20 CollectionId, TokenId, RpcCollection, CollectionStats, CollectionLimits, Property,21 PropertyKeyPermission, TokenData, TokenChild,22};23use sp_std::vec::Vec;24use codec::Decode;25use sp_runtime::DispatchError;2627type Result<T> = core::result::Result<T, DispatchError>;2829sp_api::decl_runtime_apis! {30 #[api_version(2)]31 pub trait UniqueApi<CrossAccountId, AccountId> where32 AccountId: Decode,33 CrossAccountId: pallet_evm::account::CrossAccountId<AccountId>,34 {35 #[changed_in(2)]36 fn token_owner(collection: CollectionId, token: TokenId) -> Result<CrossAccountId>;3738 fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Result<Vec<TokenId>>;39 fn collection_tokens(collection: CollectionId) -> Result<Vec<TokenId>>;40 fn token_exists(collection: CollectionId, token: TokenId) -> Result<bool>;4142 fn token_owner(collection: CollectionId, token: TokenId) -> Result<Option<CrossAccountId>>;43 fn topmost_token_owner(collection: CollectionId, token: TokenId) -> Result<Option<CrossAccountId>>;44 fn token_children(collection: CollectionId, token: TokenId) -> Result<Vec<TokenChild>>;4546 fn collection_properties(collection: CollectionId, properties: Option<Vec<Vec<u8>>>) -> Result<Vec<Property>>;4748 fn token_properties(49 collection: CollectionId,50 token_id: TokenId,51 properties: Option<Vec<Vec<u8>>>52 ) -> Result<Vec<Property>>;5354 fn property_permissions(55 collection: CollectionId,56 properties: Option<Vec<Vec<u8>>>57 ) -> Result<Vec<PropertyKeyPermission>>;5859 fn token_data(60 collection: CollectionId,61 token_id: TokenId,62 keys: Option<Vec<Vec<u8>>>63 ) -> Result<TokenData<CrossAccountId>>;6465 fn total_supply(collection: CollectionId) -> Result<u32>;66 fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32>;67 fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<u128>;68 fn allowance(69 collection: CollectionId,70 sender: CrossAccountId,71 spender: CrossAccountId,72 token: TokenId,73 ) -> Result<u128>;7475 fn adminlist(collection: CollectionId) -> Result<Vec<CrossAccountId>>;76 fn allowlist(collection: CollectionId) -> Result<Vec<CrossAccountId>>;77 fn allowed(collection: CollectionId, user: CrossAccountId) -> Result<bool>;78 fn last_token_id(collection: CollectionId) -> Result<TokenId>;79 fn collection_by_id(collection: CollectionId) -> Result<Option<RpcCollection<AccountId>>>;80 fn collection_stats() -> Result<CollectionStats>;81 fn next_sponsored(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<Option<u64>>;82 fn effective_collection_limits(collection_id: CollectionId) -> Result<Option<CollectionLimits>>;83 fn total_pieces(collection_id: CollectionId, token_id: TokenId) -> Result<u128>;84 }85}1// 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 up_data_structs::{20 CollectionId, TokenId, RpcCollection, CollectionStats, CollectionLimits, Property,21 PropertyKeyPermission, TokenData, TokenChild,22};23use sp_std::vec::Vec;24use codec::Decode;25use sp_runtime::DispatchError;2627type Result<T> = core::result::Result<T, DispatchError>;2829sp_api::decl_runtime_apis! {30 #[api_version(2)]31 pub trait UniqueApi<CrossAccountId, AccountId> where32 AccountId: Decode,33 CrossAccountId: pallet_evm::account::CrossAccountId<AccountId>,34 {35 #[changed_in(2)]36 fn token_owner(collection: CollectionId, token: TokenId) -> Result<CrossAccountId>;3738 fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Result<Vec<TokenId>>;39 fn collection_tokens(collection: CollectionId) -> Result<Vec<TokenId>>;40 fn token_exists(collection: CollectionId, token: TokenId) -> Result<bool>;4142 fn token_owner(collection: CollectionId, token: TokenId) -> Result<Option<CrossAccountId>>;43 fn topmost_token_owner(collection: CollectionId, token: TokenId) -> Result<Option<CrossAccountId>>;44 fn token_children(collection: CollectionId, token: TokenId) -> Result<Vec<TokenChild>>;4546 fn collection_properties(collection: CollectionId, properties: Option<Vec<Vec<u8>>>) -> Result<Vec<Property>>;4748 fn token_properties(49 collection: CollectionId,50 token_id: TokenId,51 properties: Option<Vec<Vec<u8>>>52 ) -> Result<Vec<Property>>;5354 fn property_permissions(55 collection: CollectionId,56 properties: Option<Vec<Vec<u8>>>57 ) -> Result<Vec<PropertyKeyPermission>>;5859 fn token_data(60 collection: CollectionId,61 token_id: TokenId,62 keys: Option<Vec<Vec<u8>>>63 ) -> Result<TokenData<CrossAccountId>>;6465 fn total_supply(collection: CollectionId) -> Result<u32>;66 fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32>;67 fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<u128>;68 fn allowance(69 collection: CollectionId,70 sender: CrossAccountId,71 spender: CrossAccountId,72 token: TokenId,73 ) -> Result<u128>;7475 fn adminlist(collection: CollectionId) -> Result<Vec<CrossAccountId>>;76 fn allowlist(collection: CollectionId) -> Result<Vec<CrossAccountId>>;77 fn allowed(collection: CollectionId, user: CrossAccountId) -> Result<bool>;78 fn last_token_id(collection: CollectionId) -> Result<TokenId>;79 fn collection_by_id(collection: CollectionId) -> Result<Option<RpcCollection<AccountId>>>;80 fn collection_stats() -> Result<CollectionStats>;81 fn next_sponsored(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<Option<u64>>;82 fn effective_collection_limits(collection_id: CollectionId) -> Result<Option<CollectionLimits>>;83 fn total_pieces(collection_id: CollectionId, token_id: TokenId) -> Result<Option<u128>>;84 }85}runtime/common/src/runtime_apis.rsdiffbeforeafterboth--- a/runtime/common/src/runtime_apis.rs
+++ b/runtime/common/src/runtime_apis.rs
@@ -143,7 +143,7 @@
Ok(<pallet_common::Pallet<Runtime>>::effective_collection_limits(collection))
}
- fn total_pieces(collection: CollectionId, token_id: TokenId) -> Result<u128, DispatchError> {
+ fn total_pieces(collection: CollectionId, token_id: TokenId) -> Result<Option<u128>, DispatchError> {
dispatch_unique_runtime!(collection.total_pieces(token_id))
}
}
tests/src/createItem.test.tsdiffbeforeafterboth--- a/tests/src/createItem.test.ts
+++ b/tests/src/createItem.test.ts
@@ -24,6 +24,8 @@
createCollectionWithPropsExpectSuccess,
createItemWithPropsExpectSuccess,
createItemWithPropsExpectFailure,
+ createCollection,
+ transferExpectSuccess,
} from './util/helpers';
const expect = chai.expect;
@@ -95,6 +97,68 @@
await createItemWithPropsExpectSuccess(alice, newCollectionID, createMode, [{key: 'key1', value: 'val1'}]);
});
+
+ it.only('Check total pieces of Fungible token', async () => {
+ await usingApi(async api => {
+ const createMode = 'Fungible';
+ const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});
+ const tokenId = await createItemExpectSuccess(alice, collectionId, createMode, bob.address);
+ {
+ const totalPieces = await api.rpc.unique.totalPieces(collectionId, tokenId);
+ expect(totalPieces.isSome).to.be.true;
+ expect(totalPieces.unwrap().toBigInt()).to.be.eq(10n);
+ }
+
+ await transferExpectSuccess(collectionId, tokenId, bob, alice, 1, createMode);
+ {
+ const totalPieces = await api.rpc.unique.totalPieces(collectionId, tokenId);
+ expect(totalPieces.isSome).to.be.true;
+ expect(totalPieces.unwrap().toBigInt()).to.be.eq(10n);
+ }
+ });
+ });
+
+ it.only('Check total pieces of NFT token', async () => {
+ await usingApi(async api => {
+ const createMode = 'NFT';
+ const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});
+ const tokenId = await createItemExpectSuccess(alice, collectionId, createMode, bob.address);
+ {
+ const totalPieces = await api.rpc.unique.totalPieces(collectionId, tokenId);
+ expect(totalPieces.isSome).to.be.true;
+ expect(totalPieces.unwrap().toBigInt()).to.be.eq(1n);
+ }
+
+ await transferExpectSuccess(collectionId, tokenId, bob, alice, 1, createMode);
+ {
+ const totalPieces = await api.rpc.unique.totalPieces(collectionId, tokenId);
+ expect(totalPieces.isSome).to.be.true;
+ expect(totalPieces.unwrap().toBigInt()).to.be.eq(1n);
+ }
+ });
+ });
+
+ it.only('Check total pieces of ReFungible token', async () => {
+ await usingApi(async api => {
+ const createMode = 'ReFungible';
+ const createCollectionResult = await createCollection(api, alice, {mode: {type: createMode}});
+ const collectionId = createCollectionResult.collectionId;
+ const amountPieces = 100n;
+ const tokenId = await createItemExpectSuccess(alice, collectionId, createMode, bob.address);
+ {
+ const totalPieces = await api.rpc.unique.totalPieces(collectionId, tokenId);
+ expect(totalPieces.isSome).to.be.true;
+ expect(totalPieces.unwrap().toBigInt()).to.be.eq(amountPieces);
+ }
+
+ await transferExpectSuccess(collectionId, tokenId, bob, alice, 60n, createMode);
+ {
+ const totalPieces = await api.rpc.unique.totalPieces(collectionId, tokenId);
+ expect(totalPieces.isSome).to.be.true;
+ expect(totalPieces.unwrap().toBigInt()).to.be.eq(amountPieces);
+ }
+ });
+ });
});
describe('Negative integration test: ext. createItem():', () => {
tests/src/refungible.test.tsdiffbeforeafterboth--- a/tests/src/refungible.test.ts
+++ b/tests/src/refungible.test.ts
@@ -68,27 +68,6 @@
});
});
- it.only('Check total pieces of token', async () => {
- await usingApi(async api => {
- const createCollectionResult = await createCollection(api, alice, {mode: {type: 'ReFungible'}});
- expect(createCollectionResult.success).to.be.true;
- const collectionId = createCollectionResult.collectionId;
- const amountPieces = 100n;
- const result = await createRefungibleToken(api, alice, collectionId, amountPieces);
- expect(result.success).to.be.true;
- {
- const totalPieces = await api.rpc.unique.totalPieces(collectionId, result.itemId);
- expect(totalPieces.toBigInt()).to.be.eq(amountPieces);
- }
-
- await transfer(api, collectionId, result.itemId, alice, bob, 60n);
- {
- const totalPieces = await api.rpc.unique.totalPieces(collectionId, result.itemId);
- expect(totalPieces.toBigInt()).to.be.eq(amountPieces);
- }
- });
- });
-
it('Transfer token pieces', async () => {
await usingApi(async api => {
const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;