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
992 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);992 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
993 let target_collection = Self::get_collection(collection_id)?;993 let target_collection = Self::get_collection(collection_id)?;
994994
995 Self::burn_item_internal(&sender, &target_collection, item_id, value)?;995 Self::burn_item_internal(&sender, &target_collection, item_id, value, true)?;
996996
997 target_collection.submit_logs()997 target_collection.submit_logs()
998 }998 }
999
1000 /// Destroys a concrete instance of NFT on behalf of the owner
1001 /// See also: [`approve`]
1002 ///
1003 /// # Permissions
1004 ///
1005 /// * Collection Owner.
1006 /// * Collection Admin.
1007 /// * Current NFT Owner.
1008 ///
1009 /// # Arguments
1010 ///
1011 /// * collection_id: ID of the collection.
1012 ///
1013 /// * item_id: ID of NFT to burn.
1014 ///
1015 /// * from: owner of item
1016 #[weight = <SelfWeightOf<T>>::burn_item()]
1017 #[transactional]
1018 pub fn burn_from(origin, collection_id: CollectionId, from: T::CrossAccountId, item_id: TokenId, value: u128) -> DispatchResult {
1019
1020 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);
1021 let target_collection = Self::get_collection(collection_id)?;
1022
1023 Self::burn_from_internal(&sender, &target_collection, &from, item_id, value)?;
1024
1025 target_collection.submit_logs()
1026 }
9991027
1000 /// Change ownership of the token.1028 /// Change ownership of the token.
1001 ///1029 ///
1593 }1621 }
15941622
1595 pub fn burn_item_internal(1623 pub fn burn_item_internal(
1596 sender: &T::CrossAccountId,1624 owner: &T::CrossAccountId,
1597 collection: &CollectionHandle<T>,1625 collection: &CollectionHandle<T>,
1598 item_id: TokenId,1626 item_id: TokenId,
1599 value: u128,1627 value: u128,
1628 allow_escalation: bool,
1600 ) -> DispatchResult {1629 ) -> DispatchResult {
1601 ensure!(1630 ensure!(
1602 Self::is_item_owner(sender, collection, item_id)?1631 Self::is_item_owner(owner, collection, item_id)?
1603 || (collection.limits.owner_can_transfer1632 || (allow_escalation
1633 && collection.limits.owner_can_transfer
1604 && Self::is_owner_or_admin_permissions(collection, sender)?),1634 && Self::is_owner_or_admin_permissions(collection, owner)?),
1605 Error::<T>::NoPermission1635 Error::<T>::NoPermission
1606 );1636 );
16071637
1608 if collection.access == AccessMode::WhiteList {1638 if collection.access == AccessMode::WhiteList {
1609 Self::check_white_list(collection, sender)?;1639 Self::check_white_list(collection, owner)?;
1610 }1640 }
16111641
1612 match collection.mode {1642 match collection.mode {
1613 CollectionMode::NFT => Self::burn_nft_item(collection, item_id)?,1643 CollectionMode::NFT => match value {
1644 1 => Self::burn_nft_item(collection, item_id)?,
1645 0 => (),
1646 _ => fail!(<Error<T>>::TokenValueTooLow),
1647 },
1614 CollectionMode::Fungible(_) => Self::burn_fungible_item(sender, collection, value)?,1648 CollectionMode::Fungible(_) => Self::burn_fungible_item(collection, owner, value)?,
1615 CollectionMode::ReFungible => Self::burn_refungible_item(collection, item_id, sender)?,1649 CollectionMode::ReFungible => {
1650 Self::burn_refungible_item(collection, item_id, owner, value)?
1651 }
1652 _ => (),
1616 };1653 };
16171654
1618 Ok(())1655 Ok(())
1619 }1656 }
1657
1658 pub fn burn_from_internal(
1659 sender: &T::CrossAccountId,
1660 collection: &CollectionHandle<T>,
1661 from: &T::CrossAccountId,
1662 item_id: TokenId,
1663 amount: u128,
1664 ) -> DispatchResult {
1665 if sender == from {
1666 // Transfer by `from`, because it is either equal to sender, or derived from him
1667 return Self::burn_item_internal(from, collection, item_id, amount, true);
1668 }
1669
1670 // Check approval
1671 collection.consume_sload()?;
1672 let approval: u128 =
1673 <Allowances<T>>::get(collection.id, (item_id, from.as_sub(), sender.as_sub()));
1674
1675 // Transfer permissions check
1676 ensure!(
1677 approval >= amount
1678 || (collection.limits.owner_can_transfer
1679 && Self::is_owner_or_admin_permissions(collection, sender)?),
1680 Error::<T>::NoPermission
1681 );
1682
1683 if collection.access == AccessMode::WhiteList {
1684 Self::check_white_list(collection, sender)?;
1685 }
1686
1687 // Reduce approval by transferred amount or remove if remaining approval drops to 0
1688 let allowance = approval.saturating_sub(amount);
1689 collection.consume_sstore()?;
1690 if allowance > 0 {
1691 <Allowances<T>>::insert(
1692 collection.id,
1693 (item_id, from.as_sub(), sender.as_sub()),
1694 allowance,
1695 );
1696 } else {
1697 <Allowances<T>>::remove(collection.id, (item_id, from.as_sub(), sender.as_sub()));
1698 }
1699
1700 // Escalation is disallowed here, because we need to be sure that passed owner is real
1701 Self::burn_item_internal(from, collection, item_id, amount, false)?;
1702
1703 if matches!(collection.mode, CollectionMode::Fungible(_)) {
1704 collection.log(ERC20Events::Approval {
1705 owner: *from.as_eth(),
1706 spender: *sender.as_eth(),
1707 value: allowance.into(),
1708 })?;
1709 }
1710
1711 Ok(())
1712 }
16201713
1621 pub fn toggle_white_list_internal(1714 pub fn toggle_white_list_internal(
1622 sender: &T::CrossAccountId,1715 sender: &T::CrossAccountId,
1884 collection: &CollectionHandle<T>,1977 collection: &CollectionHandle<T>,
1885 item_id: TokenId,1978 item_id: TokenId,
1886 owner: &T::CrossAccountId,1979 owner: &T::CrossAccountId,
1980 value: u128,
1887 ) -> DispatchResult {1981 ) -> DispatchResult {
1888 let collection_id = collection.id;1982 let collection_id = collection.id;
18891983
1890 let mut token = <ReFungibleItemList<T>>::get(collection_id, item_id)1984 let mut token = <ReFungibleItemList<T>>::get(collection_id, item_id)
1891 .ok_or(Error::<T>::TokenNotFound)?;1985 .ok_or(Error::<T>::TokenNotFound)?;
1892 let rft_balance = token1986 let mut rft_balance = token
1893 .owner1987 .owner
1894 .iter()1988 .iter()
1895 .find(|&i| i.owner == *owner)1989 .find(|&i| i.owner == *owner)
1896 .ok_or(Error::<T>::TokenNotFound)?;1990 .ok_or(Error::<T>::TokenNotFound)?
1991 .clone();
1897 Self::remove_token_index(collection, item_id, owner)?;1992 Self::remove_token_index(collection, item_id, owner)?;
18981993
1899 // update balance1994 // update balance
1900 let new_balance = <Balance<T>>::get(collection_id, rft_balance.owner.as_sub())1995 let new_balance = <Balance<T>>::get(collection_id, rft_balance.owner.as_sub())
1901 .checked_sub(rft_balance.fraction)1996 .checked_sub(value)
1902 .ok_or(Error::<T>::NumOverflow)?;1997 .ok_or(Error::<T>::NumOverflow)?;
1903 <Balance<T>>::insert(collection_id, rft_balance.owner.as_sub(), new_balance);1998 <Balance<T>>::insert(collection_id, rft_balance.owner.as_sub(), new_balance);
19041999
1905 // Re-create owners list with sender removed2000 rft_balance.fraction = (rft_balance.fraction)
2001 .checked_sub(value)
2002 .ok_or(Error::<T>::NumOverflow)?;
2003
1906 let index = token2004 let index = token
1907 .owner2005 .owner
1908 .iter()2006 .iter()
1909 .position(|i| i.owner == *owner)2007 .position(|i| i.owner == *owner)
1910 .expect("owned item is exists");2008 .expect("owned item is exists");
2009 if rft_balance.fraction == 0 {
2010 // Re-create owners list with sender removed
1911 token.owner.remove(index);2011 token.owner.remove(index);
2012 } else {
2013 token.owner[index] = rft_balance;
2014 }
2015
1912 let owner_count = token.owner.len();2016 let owner_count = token.owner.len();
19132017
1947 }2051 }
19482052
1949 fn burn_fungible_item(2053 fn burn_fungible_item(
1950 owner: &T::CrossAccountId,2054 collection: &CollectionHandle<T>,
1951 collection: &CollectionHandle<T>,2055 owner: &T::CrossAccountId,
1952 value: u128,2056 value: u128,
1953 ) -> DispatchResult {2057 ) -> DispatchResult {
1954 let collection_id = collection.id;2058 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
--- a/tests/package.json
+++ b/tests/package.json
@@ -20,6 +20,10 @@
     "tslint": "^6.1.3",
     "typescript": "^4.4.4"
   },
+  "mocha": {
+    "timeout": 9999999,
+    "require": "ts-node/register"
+  },
   "scripts": {
     "lint": "eslint --ext .ts,.js src/",
     "fix": "eslint --ext .ts,.js src/ --fix",
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();