git.delta.rocks / unique-network / refs/commits / 1bd93c2627cf

difftreelog

manual merge of develop

Igor Kozyrev2021-10-27parents: #5164830 #65a56d5.patch.diff
in: master

9 files changed

modifiedpallets/nft/src/eth/erc.rsdiffbeforeafterboth
--- a/pallets/nft/src/eth/erc.rs
+++ b/pallets/nft/src/eth/erc.rs
@@ -194,7 +194,8 @@
 		let caller = T::CrossAccountId::from_eth(caller);
 		let token_id = token_id.try_into().map_err(|_| "amount overflow")?;
 
-		<Module<T>>::burn_item_internal(&caller, &self, token_id, 1).map_err(|_| "burn error")?;
+		<Module<T>>::burn_item_internal(&caller, &self, token_id, 1, true)
+			.map_err(|_| "burn error")?;
 		Ok(())
 	}
 }
modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -992,11 +992,39 @@
 			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
 			let target_collection = Self::get_collection(collection_id)?;
 
-			Self::burn_item_internal(&sender, &target_collection, item_id, value)?;
+			Self::burn_item_internal(&sender, &target_collection, item_id, value, true)?;
 
 			target_collection.submit_logs()
 		}
 
+		/// Destroys a concrete instance of NFT on behalf of the owner
+		/// See also: [`approve`]
+		///
+		/// # Permissions
+		///
+		/// * Collection Owner.
+		/// * Collection Admin.
+		/// * Current NFT Owner.
+		///
+		/// # Arguments
+		///
+		/// * collection_id: ID of the collection.
+		///
+		/// * item_id: ID of NFT to burn.
+		///
+		/// * from: owner of item
+		#[weight = <SelfWeightOf<T>>::burn_item()]
+		#[transactional]
+		pub fn burn_from(origin, collection_id: CollectionId, from: T::CrossAccountId, item_id: TokenId, value: u128) -> DispatchResult {
+
+			let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
+			let target_collection = Self::get_collection(collection_id)?;
+
+			Self::burn_from_internal(&sender, &target_collection, &from, item_id, value)?;
+
+			target_collection.submit_logs()
+		}
+
 		/// Change ownership of the token.
 		///
 		/// # Permissions
@@ -1593,13 +1621,60 @@
 	}
 
 	pub fn burn_item_internal(
+		owner: &T::CrossAccountId,
+		collection: &CollectionHandle<T>,
+		item_id: TokenId,
+		value: u128,
+		allow_escalation: bool,
+	) -> DispatchResult {
+		ensure!(
+			Self::is_item_owner(owner, collection, item_id)?
+				|| (allow_escalation
+					&& collection.limits.owner_can_transfer
+					&& Self::is_owner_or_admin_permissions(collection, owner)?),
+			Error::<T>::NoPermission
+		);
+
+		if collection.access == AccessMode::WhiteList {
+			Self::check_white_list(collection, owner)?;
+		}
+
+		match collection.mode {
+			CollectionMode::NFT => match value {
+				1 => Self::burn_nft_item(collection, item_id)?,
+				0 => (),
+				_ => fail!(<Error<T>>::TokenValueTooLow),
+			},
+			CollectionMode::Fungible(_) => Self::burn_fungible_item(collection, owner, value)?,
+			CollectionMode::ReFungible => {
+				Self::burn_refungible_item(collection, item_id, owner, value)?
+			}
+			_ => (),
+		};
+
+		Ok(())
+	}
+
+	pub fn burn_from_internal(
 		sender: &T::CrossAccountId,
 		collection: &CollectionHandle<T>,
+		from: &T::CrossAccountId,
 		item_id: TokenId,
-		value: u128,
+		amount: u128,
 	) -> DispatchResult {
+		if sender == from {
+			// Transfer by `from`, because it is either equal to sender, or derived from him
+			return Self::burn_item_internal(from, collection, item_id, amount, true);
+		}
+
+		// Check approval
+		collection.consume_sload()?;
+		let approval: u128 =
+			<Allowances<T>>::get(collection.id, (item_id, from.as_sub(), sender.as_sub()));
+
+		// Transfer permissions check
 		ensure!(
-			Self::is_item_owner(sender, collection, item_id)?
+			approval >= amount
 				|| (collection.limits.owner_can_transfer
 					&& Self::is_owner_or_admin_permissions(collection, sender)?),
 			Error::<T>::NoPermission
@@ -1609,11 +1684,29 @@
 			Self::check_white_list(collection, sender)?;
 		}
 
-		match collection.mode {
-			CollectionMode::NFT => Self::burn_nft_item(collection, item_id)?,
-			CollectionMode::Fungible(_) => Self::burn_fungible_item(sender, collection, value)?,
-			CollectionMode::ReFungible => Self::burn_refungible_item(collection, item_id, sender)?,
-		};
+		// Reduce approval by transferred amount or remove if remaining approval drops to 0
+		let allowance = approval.saturating_sub(amount);
+		collection.consume_sstore()?;
+		if allowance > 0 {
+			<Allowances<T>>::insert(
+				collection.id,
+				(item_id, from.as_sub(), sender.as_sub()),
+				allowance,
+			);
+		} else {
+			<Allowances<T>>::remove(collection.id, (item_id, from.as_sub(), sender.as_sub()));
+		}
+
+		// Escalation is disallowed here, because we need to be sure that passed owner is real
+		Self::burn_item_internal(from, collection, item_id, amount, false)?;
+
+		if matches!(collection.mode, CollectionMode::Fungible(_)) {
+			collection.log(ERC20Events::Approval {
+				owner: *from.as_eth(),
+				spender: *sender.as_eth(),
+				value: allowance.into(),
+			})?;
+		}
 
 		Ok(())
 	}
@@ -1884,31 +1977,42 @@
 		collection: &CollectionHandle<T>,
 		item_id: TokenId,
 		owner: &T::CrossAccountId,
+		value: u128,
 	) -> DispatchResult {
 		let collection_id = collection.id;
 
 		let mut token = <ReFungibleItemList<T>>::get(collection_id, item_id)
 			.ok_or(Error::<T>::TokenNotFound)?;
-		let rft_balance = token
+		let mut rft_balance = token
 			.owner
 			.iter()
 			.find(|&i| i.owner == *owner)
-			.ok_or(Error::<T>::TokenNotFound)?;
+			.ok_or(Error::<T>::TokenNotFound)?
+			.clone();
 		Self::remove_token_index(collection, item_id, owner)?;
 
 		// update balance
 		let new_balance = <Balance<T>>::get(collection_id, rft_balance.owner.as_sub())
-			.checked_sub(rft_balance.fraction)
+			.checked_sub(value)
 			.ok_or(Error::<T>::NumOverflow)?;
 		<Balance<T>>::insert(collection_id, rft_balance.owner.as_sub(), new_balance);
 
-		// Re-create owners list with sender removed
+		rft_balance.fraction = (rft_balance.fraction)
+			.checked_sub(value)
+			.ok_or(Error::<T>::NumOverflow)?;
+
 		let index = token
 			.owner
 			.iter()
 			.position(|i| i.owner == *owner)
 			.expect("owned item is exists");
-		token.owner.remove(index);
+		if rft_balance.fraction == 0 {
+			// Re-create owners list with sender removed
+			token.owner.remove(index);
+		} else {
+			token.owner[index] = rft_balance;
+		}
+
 		let owner_count = token.owner.len();
 
 		// Burn the token completely if this was the last (only) owner
@@ -1947,8 +2051,8 @@
 	}
 
 	fn burn_fungible_item(
+		collection: &CollectionHandle<T>,
 		owner: &T::CrossAccountId,
-		collection: &CollectionHandle<T>,
 		value: u128,
 	) -> DispatchResult {
 		let collection_id = collection.id;
modifiedpallets/nft/src/tests.rsdiffbeforeafterboth
--- a/pallets/nft/src/tests.rs
+++ b/pallets/nft/src/tests.rs
@@ -707,9 +707,15 @@
 		assert_eq!(TemplateModule::balance_count(1, 1), 1);
 
 		// burn item
-		assert_ok!(TemplateModule::burn_item(origin1.clone(), 1, 1, 5));
+		assert_ok!(TemplateModule::burn_item(
+			origin1.clone(),
+			1,
+			1,
+			account(1),
+			5
+		));
 		assert_noop!(
-			TemplateModule::burn_item(origin1, 1, 1, 5),
+			TemplateModule::burn_item(origin1, 1, 1, account(1), 5),
 			Error::<Test>::TokenNotFound
 		);
 
@@ -736,9 +742,15 @@
 		assert_eq!(TemplateModule::balance_count(1, 1), 5);
 
 		// burn item
-		assert_ok!(TemplateModule::burn_item(origin1.clone(), 1, 1, 5));
+		assert_ok!(TemplateModule::burn_item(
+			origin1.clone(),
+			1,
+			1,
+			account(1),
+			5
+		));
 		assert_noop!(
-			TemplateModule::burn_item(origin1, 1, 1, 5),
+			TemplateModule::burn_item(origin1, 1, 1, account(1), 5),
 			Error::<Test>::TokenValueNotEnough
 		);
 
@@ -781,9 +793,15 @@
 		assert_eq!(TemplateModule::balance_count(1, 1), 1023);
 
 		// burn item
-		assert_ok!(TemplateModule::burn_item(origin1.clone(), 1, 1, 1023));
+		assert_ok!(TemplateModule::burn_item(
+			origin1.clone(),
+			1,
+			1,
+			account(1),
+			1023
+		));
 		assert_noop!(
-			TemplateModule::burn_item(origin1, 1, 1, 1023),
+			TemplateModule::burn_item(origin1, 1, 1, account(1), 1023),
 			Error::<Test>::TokenNotFound
 		);
 
@@ -1378,7 +1396,7 @@
 			AccessMode::WhiteList
 		));
 		assert_noop!(
-			TemplateModule::burn_item(origin1, 1, 1, 5),
+			TemplateModule::burn_item(origin1.clone(), 1, 1, account(1), 5),
 			Error::<Test>::AddresNotInWhiteList
 		);
 	});
modifiedtests/.vscode/settings.jsondiffbeforeafterboth
--- a/tests/.vscode/settings.json
+++ b/tests/.vscode/settings.json
@@ -1,3 +1,5 @@
 {
-    "mocha.enabled": true
-}
\ No newline at end of file
+    "mocha.enabled": true,
+    "mochaExplorer.files": "**/*.test.ts",
+    "mochaExplorer.require": "ts-node/register"
+}
modifiedtests/package.jsondiffbeforeafterboth
before · tests/package.json
1{2  "name": "nfttests",3  "version": "1.0.0",4  "description": "Substrate Nft tests",5  "main": "",6  "devDependencies": {7    "@polkadot/dev": "0.63.9",8    "@polkadot/ts": "0.4.12",9    "@polkadot/typegen": "6.5.2-3",10    "@types/chai": "^4.2.22",11    "@types/chai-as-promised": "^7.1.4",12    "@types/mocha": "^9.0.0",13    "@types/node": "^16.11.4",14    "@typescript-eslint/eslint-plugin": "^5.1.0",15    "@typescript-eslint/parser": "^5.1.0",16    "chai": "^4.3.4",17    "eslint": "^8.1.0",18    "mocha": "^9.1.3",19    "ts-node": "^10.4.0",20    "tslint": "^6.1.3",21    "typescript": "^4.4.4"22  },23  "scripts": {24    "lint": "eslint --ext .ts,.js src/",25    "fix": "eslint --ext .ts,.js src/ --fix",26    "test": "mocha --timeout 9999999 -r ts-node/register './**/*.test.ts'",27    "testEth": "mocha --timeout 9999999 -r ts-node/register './**/eth/**/*.test.ts'",28    "load": "mocha --timeout 9999999 -r ts-node/register './**/*.load.ts'",29    "loadTransfer": "ts-node src/transfer.nload.ts",30    "testCollision": "mocha --timeout 9999999 -r ts-node/register ./src/collision-tests/*.test.ts",31    "testEvent": "mocha --timeout 9999999 -r ts-node/register ./src/check-event/*.test.ts",32    "testAddCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/addCollectionAdmin.test.ts",33    "testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",34    "testSetVariableMetaData": "mocha --timeout 9999999 -r ts-node/register ./**/setVariableMetaData.test.ts",35    "testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",36    "testSetCollectionSponsor": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionSponsor.test.ts",37    "testConfirmSponsorship": "mocha --timeout 9999999 -r ts-node/register ./**/confirmSponsorship.test.ts",38    "testRemoveCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/removeCollectionAdmin.test.ts",39    "testRemoveCollectionSponsor": "mocha --timeout 9999999 -r ts-node/register ./**/removeCollectionSponsor.test.ts",40    "testRemoveFromWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/removeFromWhiteList.test.ts",41    "testConnection": "mocha --timeout 9999999 -r ts-node/register ./**/connection.test.ts",42    "testCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",43    "testContracts": "mocha --timeout 9999999 -r ts-node/register ./**/contracts.test.ts",44    "testCreateItem": "mocha --timeout 9999999 -r ts-node/register ./**/createItem.test.ts",45    "testCreateMultipleItems": "mocha --timeout 9999999 -r ts-node/register ./**/createMultipleItems.test.ts",46    "testApprove": "mocha --timeout 9999999 -r ts-node/register ./**/approve.test.ts",47    "testTransferFrom": "mocha --timeout 9999999 -r ts-node/register ./**/transferFrom.test.ts",48    "testCreateCollection": "mocha --timeout 9999999 -r ts-node/register ./**/createCollection.test.ts",49    "testToggleContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/toggleContractWhiteList.test.ts",50    "testAddToContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/addToContractWhiteList.test.ts",51    "testTransfer": "mocha --timeout 9999999 -r ts-node/register ./**/transfer.test.ts",52    "testBurnItem": "mocha --timeout 9999999 -r ts-node/register ./**/burnItem.test.ts",53    "testSetMintPermission": "mocha --timeout 9999999 -r ts-node/register ./**/setMintPermission.test.ts",54    "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",55    "testEnableContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/enableContractSponsoring.test.ts",56    "testRemoveFromContractWhiteList": "mocha --timeout 9999999 -r ts-node/register ./**/removeFromContractWhiteList.test.ts",57    "testSetContractSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setContractSponsoringRateLimit.test.ts",58    "testSetOffchainSchema": "mocha --timeout 9999999 -r ts-node/register ./**/setOffchainSchema.test.ts",59    "testOverflow": "mocha --timeout 9999999 -r ts-node/register ./**/overflow.test.ts",60    "testSetVariableMetadataSponsoringRateLimit": "mocha --timeout 9999999 -r ts-node/register ./**/setVariableMetadataSponsoringRateLimit.test.ts",61    "testInflation": "mocha --timeout 9999999 -r ts-node/register ./**/inflation.test.ts",62    "testPalletPresence": "mocha --timeout 9999999 -r ts-node/register ./**/pallet-presence.test.ts",63    "testBlockProduction": "mocha --timeout 9999999 -r ts-node/register ./**/block-production.test.ts",64    "testEnableDisableTransfers": "mocha --timeout 9999999 -r ts-node/register ./**/enableDisableTransfer.test.ts"65  },66  "author": "",67  "license": "SEE LICENSE IN ../LICENSE",68  "homepage": "",69  "dependencies": {70    "@polkadot/api": "6.5.1",71    "@polkadot/api-contract": "6.5.1",72    "@polkadot/util-crypto": "^7.6.1",73    "bignumber.js": "^9.0.1",74    "chai-as-promised": "^7.1.1",75    "solc": "^0.8.9",76    "web3": "^1.6.0"77  },78  "standard": {79    "globals": [80      "it",81      "assert",82      "beforeEach",83      "afterEach",84      "describe",85      "contract",86      "artifacts"87    ]88  }89}
modifiedtests/src/burnItem.test.tsdiffbeforeafterboth
--- a/tests/src/burnItem.test.ts
+++ b/tests/src/burnItem.test.ts
@@ -38,7 +38,7 @@
     const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);
 
     await usingApi(async (api) => {
-      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);
+      const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);
       const events = await submitTransactionAsync(alice, tx);
       const result = getGenericResult(events);
       // Get the item
@@ -79,7 +79,7 @@
     const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);
 
     await usingApi(async (api) => {
-      const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);
+      const tx = api.tx.nft.burnItem(collectionId, tokenId, 100);
       const events = await submitTransactionAsync(alice, tx);
       const result = getGenericResult(events);
 
@@ -107,7 +107,7 @@
       const balanceBefore: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();
 
       // Bob burns his portion
-      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);
+      const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);
       const events2 = await submitTransactionAsync(bob, tx);
       const result2 = getGenericResult(events2);
 
@@ -152,7 +152,7 @@
     await addCollectionAdminExpectSuccess(alice, collectionId, bob);
 
     await usingApi(async (api) => {
-      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);
+      const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);
       const events = await submitTransactionAsync(bob, tx);
       const result = getGenericResult(events);
       // Get the item
@@ -164,6 +164,48 @@
       expect(item).to.be.null;
     });
   });
+
+
+  it('Burn item in Fungible collection', async () => {
+    const createMode = 'Fungible';
+    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0 }});
+    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode); // Helper creates 10 fungible tokens
+    await addCollectionAdminExpectSuccess(alice, collectionId, bob);
+
+    await usingApi(async (api) => {
+      // Destroy 1 of 10
+      const tx = api.tx.nft.burnFrom(collectionId, normalizeAccountId(alice.address), tokenId, 1);
+      const events = await submitTransactionAsync(bob, tx);
+      const result = getGenericResult(events);
+
+      // Get alice balance
+      const balance: any = (await api.query.nft.fungibleItemList(collectionId, alice.address)).toJSON();
+
+      // What to expect
+      expect(result.success).to.be.true;
+      expect(balance).to.be.not.null;
+      expect(balance.Value).to.be.equal(9);
+    });
+  });
+
+  it('Burn item in ReFungible collection', async () => {
+    const createMode = 'ReFungible';
+    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});
+    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);
+    await addCollectionAdminExpectSuccess(alice, collectionId, bob);
+
+    await usingApi(async (api) => {
+      const tx = api.tx.nft.burnFrom(collectionId, normalizeAccountId(alice.address), tokenId, 100);
+      const events = await submitTransactionAsync(bob, tx);
+      const result = getGenericResult(events);
+      // Get alice balance
+      const balance: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();
+
+      // What to expect
+      expect(result.success).to.be.true;
+      expect(balance).to.be.null;
+    });
+  });
 });
 
 describe('Negative integration test: ext. burnItem():', () => {
@@ -197,8 +239,8 @@
     const tokenId = 10;
 
     await usingApi(async (api) => {
-      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);
-      const badTransaction = async function () {
+      const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);
+      const badTransaction = async function () { 
         await submitTransactionExpectFailAsync(alice, tx);
       };
       await expect(badTransaction()).to.be.rejected;
@@ -228,7 +270,7 @@
 
     await usingApi(async (api) => {
 
-      const burntx = api.tx.nft.burnItem(collectionId, tokenId, 0);
+      const burntx = api.tx.nft.burnItem(collectionId, tokenId, 1);
       const events1 = await submitTransactionAsync(alice, burntx);
       const result1 = getGenericResult(events1);
       expect(result1.success).to.be.true;
modifiedtests/src/transfer.test.tsdiffbeforeafterboth
--- a/tests/src/transfer.test.ts
+++ b/tests/src/transfer.test.ts
@@ -199,7 +199,7 @@
     const reFungibleCollectionId = await
     createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
     const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
-    await burnItemExpectSuccess(Alice, reFungibleCollectionId, newReFungibleTokenId, 1);
+    await burnItemExpectSuccess(Alice, reFungibleCollectionId, newReFungibleTokenId, 100);
     await transferExpectFailure(
       reFungibleCollectionId,
       newReFungibleTokenId,
modifiedtests/src/transferFrom.test.tsdiffbeforeafterboth
--- a/tests/src/transferFrom.test.ts
+++ b/tests/src/transferFrom.test.ts
@@ -265,7 +265,7 @@
     await usingApi(async () => {
       const reFungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
       const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
-      await burnItemExpectSuccess(Alice, reFungibleCollectionId, newReFungibleTokenId, 1);
+      await burnItemExpectSuccess(Alice, reFungibleCollectionId, newReFungibleTokenId, 100);
       await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);
       await transferFromExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice, Charlie, 1);
 
@@ -297,7 +297,7 @@
       const reFungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});
       const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');
       await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);
-      await burnItemExpectSuccess(Alice, reFungibleCollectionId, newReFungibleTokenId, 1);
+      await burnItemExpectSuccess(Alice, reFungibleCollectionId, newReFungibleTokenId, 100);
       await transferFromExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice, Charlie, 1);
 
     });
modifiedtests/src/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -700,10 +700,10 @@
   ReFungible: CreateReFungibleData;
 };
 
-export async function burnItemExpectSuccess(owner: IKeyringPair, collectionId: number, tokenId: number, value = 0) {
+export async function burnItemExpectSuccess(sender: IKeyringPair, collectionId: number, tokenId: number, value = 1) {
   await usingApi(async (api) => {
     const tx = api.tx.nft.burnItem(collectionId, tokenId, value);
-    const events = await submitTransactionAsync(owner, tx);
+    const events = await submitTransactionAsync(sender, tx);
     const result = getGenericResult(events);
     // Get the item
     const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();