git.delta.rocks / unique-network / refs/commits / 88ef7226e031

difftreelog

Test all collection for total_pieces

Trubnikov Sergey2022-06-30parent: #2d796da.patch.diff
in: master

10 files changed

modifiedclient/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)]
modifiedpallets/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,
modifiedpallets/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()
 	}
 }
modifiedpallets/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)
 	}
 }
modifiedpallets/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)
 	}
 }
modifiedpallets/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()
 	}
 }
modifiedprimitives/rpc/src/lib.rsdiffbeforeafterboth
--- a/primitives/rpc/src/lib.rs
+++ b/primitives/rpc/src/lib.rs
@@ -80,6 +80,6 @@
 		fn collection_stats() -> Result<CollectionStats>;
 		fn next_sponsored(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<Option<u64>>;
 		fn effective_collection_limits(collection_id: CollectionId) -> Result<Option<CollectionLimits>>;
-		fn total_pieces(collection_id: CollectionId, token_id: TokenId) -> Result<u128>;
+		fn total_pieces(collection_id: CollectionId, token_id: TokenId) -> Result<Option<u128>>;
 	}
 }
modifiedruntime/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))
                 }
             }
modifiedtests/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():', () => {
modifiedtests/src/refungible.test.tsdiffbeforeafterboth
before · tests/src/refungible.test.ts
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/>.1617import {default as usingApi} from './substrate/substrate-api';18import {IKeyringPair} from '@polkadot/types/types';19import {20  createCollectionExpectSuccess,21  getBalance,22  createMultipleItemsExpectSuccess,23  isTokenExists,24  getLastTokenId,25  getAllowance,26  approve,27  transferFrom,28  createCollection,29  createRefungibleToken,30  transfer,31  burnItem,32  repartitionRFT,33} from './util/helpers';3435import chai from 'chai';36import chaiAsPromised from 'chai-as-promised';37chai.use(chaiAsPromised);38const expect = chai.expect;3940let alice: IKeyringPair;41let bob: IKeyringPair;4243describe('integration test: Refungible functionality:', () => {44  before(async () => {45    await usingApi(async (api, privateKeyWrapper) => {46      alice = privateKeyWrapper('//Alice');47      bob = privateKeyWrapper('//Bob');48    });49  });5051  it('Create refungible collection and token', async () => {52    await usingApi(async api => {53      const createCollectionResult = await createCollection(api, alice, {mode: {type: 'ReFungible'}});54      expect(createCollectionResult.success).to.be.true;    55      const collectionId  = createCollectionResult.collectionId;5657      const itemCountBefore = await getLastTokenId(api, collectionId);58      const result = await createRefungibleToken(api, alice, collectionId, 100n);5960      const itemCountAfter = await getLastTokenId(api, collectionId);6162      // What to expect63      // tslint:disable-next-line:no-unused-expression64      expect(result.success).to.be.true;65      expect(itemCountAfter).to.be.equal(itemCountBefore + 1);66      expect(collectionId).to.be.equal(result.collectionId);67      expect(itemCountAfter.toString()).to.be.equal(result.itemId.toString());68    });69  });7071  it.only('Check total pieces of token', async () => {72    await usingApi(async api => {73      const createCollectionResult = await createCollection(api, alice, {mode: {type: 'ReFungible'}});74      expect(createCollectionResult.success).to.be.true;    75      const collectionId  = createCollectionResult.collectionId;76      const amountPieces = 100n;77      const result = await createRefungibleToken(api, alice, collectionId, amountPieces);78      expect(result.success).to.be.true;79      {80        const totalPieces = await api.rpc.unique.totalPieces(collectionId, result.itemId);81        expect(totalPieces.toBigInt()).to.be.eq(amountPieces);82      }8384      await transfer(api, collectionId, result.itemId, alice, bob, 60n);85      {86        const totalPieces = await api.rpc.unique.totalPieces(collectionId, result.itemId);87        expect(totalPieces.toBigInt()).to.be.eq(amountPieces);88      }89    });90  });9192  it('Transfer token pieces', async () => {93    await usingApi(async api => {94      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;95      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;9697      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);98      expect(await transfer(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;99100      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(40n);101      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(60n);102      await expect(transfer(api, collectionId, tokenId, alice, bob, 41n)).to.eventually.be.rejected;103    });104  });105106  it('Create multiple tokens', async () => {107    const collectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});108    const args = [109      {ReFungible: {pieces: 1}},110      {ReFungible: {pieces: 2}},111      {ReFungible: {pieces: 100}},112    ];113    await createMultipleItemsExpectSuccess(alice, collectionId, args);114115    await usingApi(async api => {      116      const tokenId = await getLastTokenId(api, collectionId);117      expect(tokenId).to.be.equal(3);118      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);119    });120  });121122  it('Burn some pieces', async () => {123    await usingApi(async api => {   124      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;125      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;126      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;127      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);128      expect(await burnItem(api, alice, collectionId, tokenId, 99n)).to.be.true;129      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;130      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(1n);131    });132  });133134  it('Burn all pieces', async () => {135    await usingApi(async api => {   136      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;137      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;138      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;139      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);140      expect(await burnItem(api, alice, collectionId, tokenId, 100n)).to.be.true;141      expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;142    });143  });144145  it('Burn some pieces for multiple users', async () => {146    await usingApi(async api => {   147      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;148      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;149      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;150151      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);152      expect(await transfer(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;153154      155      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(40n);156      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(60n);157      expect(await burnItem(api, alice, collectionId, tokenId, 40n)).to.be.true;158159      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(0n);160      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;161      expect(await burnItem(api, bob, collectionId, tokenId, 59n)).to.be.true;162163      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(1n);164      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;165      expect(await burnItem(api, bob, collectionId, tokenId, 1n)).to.be.true;166      167      expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;168    });169  });170171  it('Set allowance for token', async () => {172    await usingApi(async api => {173      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;174      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;175176      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);177178      expect(await approve(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;179      expect(await getAllowance(api, collectionId, alice, bob, tokenId)).to.be.equal(60n);180181      expect(await transferFrom(api, collectionId, tokenId, bob, alice, bob,  20n)).to.be.true;182      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(80n);183      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(20n);184      expect(await getAllowance(api, collectionId, alice, bob, tokenId)).to.be.equal(40n);185    });186  });187188  it('Repartition', async () => {189    await usingApi(async api => {190      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;191      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;192193      expect(await repartitionRFT(api, collectionId, alice, tokenId, 200n)).to.be.true;194      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(200n);195196      expect(await transfer(api, collectionId, tokenId, alice, bob, 110n)).to.be.true;197      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(90n);198      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(110n);199200      await expect(repartitionRFT(api, collectionId, alice, tokenId, 80n)).to.eventually.be.rejected;201202      expect(await transfer(api, collectionId, tokenId, alice, bob, 90n)).to.be.true;203      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(0n);204      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(200n);205206      expect(await repartitionRFT(api, collectionId, bob, tokenId, 150n)).to.be.true;207      await expect(transfer(api, collectionId, tokenId, bob, alice, 160n)).to.eventually.be.rejected;208    });209  });210});