--- a/client/rpc/src/lib.rs +++ b/client/rpc/src/lib.rs @@ -198,7 +198,7 @@ collection_id: CollectionId, token_id: TokenId, at: Option, - ) -> Result; + ) -> Result>; } 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, unique_api); pass_method!(effective_collection_limits(collection_id: CollectionId) -> Option, 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, unique_api); } #[allow(deprecated)] --- 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; fn allowance( &self, sender: T::CrossAccountId, --- 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(PhantomData); @@ -404,7 +405,10 @@ None } - fn total_pieces(&self, _token: TokenId) -> u128 { - 0 + fn total_pieces(&self, token: TokenId) -> Option { + if token != TokenId::default() { + return None; + } + >::try_get(self.id).ok() } } --- 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 { + Some(1) } } --- 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 { >::total_pieces(self.id, token) } } --- 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 { - >::get((collection_id, token_id)) + fn total_pieces(collection_id: CollectionId, token_id: TokenId) -> Option { + >::try_get((collection_id, token_id)).ok() } } --- a/primitives/rpc/src/lib.rs +++ b/primitives/rpc/src/lib.rs @@ -80,6 +80,6 @@ fn collection_stats() -> Result; fn next_sponsored(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result>; fn effective_collection_limits(collection_id: CollectionId) -> Result>; - fn total_pieces(collection_id: CollectionId, token_id: TokenId) -> Result; + fn total_pieces(collection_id: CollectionId, token_id: TokenId) -> Result>; } } --- a/runtime/common/src/runtime_apis.rs +++ b/runtime/common/src/runtime_apis.rs @@ -143,7 +143,7 @@ Ok(>::effective_collection_limits(collection)) } - fn total_pieces(collection: CollectionId, token_id: TokenId) -> Result { + fn total_pieces(collection: CollectionId, token_id: TokenId) -> Result, DispatchError> { dispatch_unique_runtime!(collection.total_pieces(token_id)) } } --- 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():', () => { --- 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;