git.delta.rocks / unique-network / refs/commits / f0ad1b02de6f

difftreelog

refac: incapsulate CollectionHandler into CollectionDispatch

Trubnikov Sergey2023-04-24parent: #361b516.patch.diff
in: master

13 files changed

modifiedpallets/balances-adapter/src/lib.rsdiffbeforeafterboth
--- a/pallets/balances-adapter/src/lib.rs
+++ b/pallets/balances-adapter/src/lib.rs
@@ -3,6 +3,7 @@
 #![warn(missing_docs)]
 
 extern crate alloc;
+use frame_support::sp_runtime::DispatchResult;
 pub use pallet::*;
 use pallet_common::CollectionHandle;
 use pallet_evm_coder_substrate::{WithRecorder, SubstrateRecorder};
@@ -10,24 +11,23 @@
 pub mod common;
 pub mod erc;
 
-pub struct NativeFungibleHandle<T: Config>(CollectionHandle<T>);
+pub struct NativeFungibleHandle<T: Config>(SubstrateRecorder<T>);
 impl<T: Config> NativeFungibleHandle<T> {
-	pub fn cast(inner: CollectionHandle<T>) -> Self {
-		Self(inner)
+	pub fn new() -> NativeFungibleHandle<T> {
+		Self(SubstrateRecorder::new(u64::MAX))
 	}
 
-	/// Casts [`NativeFungibleHandle`] into [`CollectionHandle`][`pallet_common::CollectionHandle`].
-	pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {
-		self.0
+	pub fn check_is_internal(&self) -> DispatchResult {
+		Ok(())
 	}
 }
 
 impl<T: Config> WithRecorder<T> for NativeFungibleHandle<T> {
 	fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {
-		&self.0.recorder
+		&self.0
 	}
 	fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {
-		self.0.recorder
+		self.0
 	}
 }
 #[frame_support::pallet]
modifiedpallets/common/src/dispatch.rsdiffbeforeafterboth
--- a/pallets/common/src/dispatch.rs
+++ b/pallets/common/src/dispatch.rs
@@ -34,16 +34,11 @@
 	collection: CollectionId,
 	call: C,
 ) -> DispatchResultWithPostInfo {
-	let handle =
-		CollectionHandle::try_get(collection).map_err(|error| DispatchErrorWithPostInfo {
-			post_info: PostDispatchInfo {
-				actual_weight: Some(dispatch_weight::<T>()),
-				pays_fee: Pays::Yes,
-			},
-			error,
-		})?;
-	handle
-		.check_is_internal()
+	let dispatched = T::CollectionDispatch::dispatch(collection)
+		.and_then(|dispatched| {
+			dispatched.check_is_internal()?;
+			Ok(dispatched)
+		})
 		.map_err(|error| DispatchErrorWithPostInfo {
 			post_info: PostDispatchInfo {
 				actual_weight: Some(dispatch_weight::<T>()),
@@ -51,7 +46,6 @@
 			},
 			error,
 		})?;
-	let dispatched = T::CollectionDispatch::dispatch(handle);
 	let mut result = call(dispatched.as_dyn());
 	match &mut result {
 		Ok(PostDispatchInfo {
@@ -72,6 +66,8 @@
 
 /// Interface for working with different collections through the dispatcher.
 pub trait CollectionDispatch<T: Config> {
+	fn check_is_internal(&self) -> DispatchResult;
+
 	/// Create a collection. The collection will be created according to the value of [`data.mode`](CreateCollectionData::mode).
 	///
 	/// * `sender` - The user who will become the owner of the collection.
@@ -92,7 +88,9 @@
 	/// Get a specialized collection from the handle.
 	///
 	/// * `handle` - Collection handle.
-	fn dispatch(handle: CollectionHandle<T>) -> Self;
+	fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError>
+	where
+		Self: Sized;
 
 	/// Get the implementation of [`CommonCollectionOperations`].
 	fn as_dyn(&self) -> &dyn CommonCollectionOperations<T>;
modifiedpallets/common/src/erc.rsdiffbeforeafterboth
--- a/pallets/common/src/erc.rs
+++ b/pallets/common/src/erc.rs
@@ -77,6 +77,14 @@
 	fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult>;
 }
 
+impl CommonEvmHandler for () {
+	const CODE: &'static [u8] = &[];
+
+	fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {
+		None
+	}
+}
+
 /// @title A contract that allows you to work with collections.
 #[solidity_interface(name = Collection, enum(derive(PreDispatch)), enum_attr(weight))]
 impl<T: Config> CollectionHandle<T>
modifiedpallets/structure/src/benchmarking.rsdiffbeforeafterboth
--- a/pallets/structure/src/benchmarking.rs
+++ b/pallets/structure/src/benchmarking.rs
@@ -42,7 +42,7 @@
 			},
 			CollectionFlags::default(),
 		)?;
-		let dispatch = T::CollectionDispatch::dispatch(CollectionHandle::try_get(CollectionId(1))?);
+		let dispatch = T::CollectionDispatch::dispatch(CollectionId(1))?;
 		let dispatch = dispatch.as_dyn();
 
 		dispatch.create_item(caller_cross.clone(), caller_cross.clone(), CreateItemData::NFT(CreateNftData::default()), &Unlimited)?;
modifiedpallets/structure/src/lib.rsdiffbeforeafterboth
--- a/pallets/structure/src/lib.rs
+++ b/pallets/structure/src/lib.rs
@@ -155,11 +155,10 @@
 		token: TokenId,
 	) -> Result<Parent<T::CrossAccountId>, DispatchError> {
 		// TODO: Reduce cost by not reading collection config
-		let handle = match CollectionHandle::try_get(collection) {
+		let handle = match T::CollectionDispatch::dispatch(collection) {
 			Ok(v) => v,
 			Err(_) => return Ok(Parent::TokenNotFound),
 		};
-		let handle = T::CollectionDispatch::dispatch(handle);
 		let handle = handle.as_dyn();
 
 		Ok(match handle.token_owner(token) {
@@ -279,8 +278,7 @@
 		self_budget: &dyn Budget,
 		breadth_budget: &dyn Budget,
 	) -> DispatchResultWithPostInfo {
-		let handle = <CollectionHandle<T>>::try_get(collection)?;
-		let dispatch = T::CollectionDispatch::dispatch(handle);
+		let dispatch = T::CollectionDispatch::dispatch(collection)?;
 		let dispatch = dispatch.as_dyn();
 		dispatch.burn_item_recursively(from.clone(), token, self_budget, breadth_budget)
 	}
@@ -404,10 +402,8 @@
 		let Some((collection, token)) = T::CrossTokenAddressMapping::address_to_token(account) else {
 			return Ok(())
 		};
-
-		let handle = <CollectionHandle<T>>::try_get(collection)?;
 
-		let dispatch = T::CollectionDispatch::dispatch(handle);
+		let dispatch = T::CollectionDispatch::dispatch(collection)?;
 		let dispatch = dispatch.as_dyn();
 
 		action(dispatch, token)
modifiedruntime/common/dispatch.rsdiffbeforeafterboth
--- a/runtime/common/dispatch.rs
+++ b/runtime/common/dispatch.rs
@@ -50,6 +50,7 @@
 	Refungible(RefungibleHandle<T>),
 	NativeFungible(NativeFungibleHandle<T>),
 }
+
 impl<T> CollectionDispatch<T> for CollectionDispatchT<T>
 where
 	T: pallet_common::Config
@@ -59,6 +60,15 @@
 		+ pallet_refungible::Config
 		+ pallet_balances_adapter::Config,
 {
+	fn check_is_internal(&self) -> DispatchResult {
+		match self {
+			Self::Fungible(h) => h.check_is_internal(),
+			Self::Nonfungible(h) => h.check_is_internal(),
+			Self::Refungible(h) => h.check_is_internal(),
+			Self::NativeFungible(h) => h.check_is_internal(),
+		}
+	}
+
 	fn create(
 		sender: T::CrossAccountId,
 		payer: T::CrossAccountId,
@@ -104,18 +114,17 @@
 		Ok(())
 	}
 
-	fn dispatch(handle: CollectionHandle<T>) -> Self {
-		match handle.mode {
-			CollectionMode::Fungible(_) => {
-				if handle.id != up_data_structs::CollectionId(0) {
-					Self::Fungible(FungibleHandle::cast(handle))
-				} else {
-					Self::NativeFungible(NativeFungibleHandle::cast(handle))
-				}
-			}
+	fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError> {
+		if collection_id == CollectionId(0) {
+			return Ok(Self::NativeFungible(NativeFungibleHandle::new()));
+		}
+
+		let handle = <CollectionHandle<T>>::try_get(collection_id)?;
+		Ok(match handle.mode {
+			CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),
 			CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),
 			CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),
-		}
+		})
 	}
 
 	fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {
@@ -172,15 +181,19 @@
 	}
 	fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {
 		if let Some(collection_id) = map_eth_to_id(&handle.code_address()) {
-			let collection =
-				<CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;
-			let dispatched = Self::dispatch(collection);
+			if collection_id == CollectionId(0) {
+				<NativeFungibleHandle<T>>::new().call(handle)
+			} else {
+				let collection = <CollectionHandle<T>>::new_with_gas_limit(
+					collection_id,
+					handle.remaining_gas(),
+				)?;
 
-			match dispatched {
-				Self::Fungible(h) => h.call(handle),
-				Self::Nonfungible(h) => h.call(handle),
-				Self::Refungible(h) => h.call(handle),
-				Self::NativeFungible(h) => h.call(handle),
+				match collection.mode {
+					CollectionMode::Fungible(_) => FungibleHandle::cast(collection).call(handle),
+					CollectionMode::NFT => NonfungibleHandle::cast(collection).call(handle),
+					CollectionMode::ReFungible => RefungibleHandle::cast(collection).call(handle),
+				}
 			}
 		} else if let Some((collection_id, token_id)) =
 			<T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(
modifiedruntime/common/runtime_apis.rsdiffbeforeafterboth
--- a/runtime/common/runtime_apis.rs
+++ b/runtime/common/runtime_apis.rs
@@ -17,7 +17,7 @@
 #[macro_export]
 macro_rules! dispatch_unique_runtime {
 	($collection:ident.$method:ident($($name:ident),*) $($rest:tt)*) => {{
-		let collection = <Runtime as pallet_common::Config>::CollectionDispatch::dispatch(<pallet_common::CollectionHandle<Runtime>>::try_get($collection)?);
+		let collection = <Runtime as pallet_common::Config>::CollectionDispatch::dispatch($collection)?;
 		let dispatch = collection.as_dyn();
 
 		Ok::<_, DispatchError>(dispatch.$method($($name),*) $($rest)*)
modifiedtests/src/eth/fungible.test.tsdiffbeforeafterboth
--- a/tests/src/eth/fungible.test.ts
+++ b/tests/src/eth/fungible.test.ts
@@ -33,7 +33,7 @@
     'substrate' as const,
     'ethereum' as const,
   ].map(testCase => {
-    itEth.only(`Can perform mintCross() for ${testCase} address`, async ({helper}) => {
+    itEth(`Can perform mintCross() for ${testCase} address`, async ({helper}) => {
       // 1. Create receiver depending on the test case:
       const receiverEth = helper.eth.createAccount();
       const receiverCrossEth = helper.ethCrossAccount.fromAddress(receiverEth);
modifiedtests/src/eth/nativeFungible.test.tsdiffbeforeafterboth
--- a/tests/src/eth/nativeFungible.test.ts
+++ b/tests/src/eth/nativeFungible.test.ts
@@ -29,7 +29,7 @@
     });
   });
 
-  itEth.only('Can perform approve()', async ({helper}) => {
+  itEth.skip('Can perform approve()', async ({helper}) => {
     const owner = await helper.eth.createAccountWithBalance(donor);
     const spender = helper.eth.createAccount();
     const collection = await helper.ft.mintCollection(alice);
modifiedtests/src/eth/util/playgrounds/types.tsdiffbeforeafterboth
--- a/tests/src/eth/util/playgrounds/types.ts
+++ b/tests/src/eth/util/playgrounds/types.ts
@@ -48,3 +48,5 @@
   field: CollectionLimitField,
   value: OptionUint,
 }
+
+export const NON_EXISTENT_COLLECTION_ID = 4_294_967_295;
\ No newline at end of file
modifiedtests/src/pallet-presence.test.tsdiffbeforeafterboth
--- a/tests/src/pallet-presence.test.ts
+++ b/tests/src/pallet-presence.test.ts
@@ -19,6 +19,7 @@
 // Pallets that must always be present
 const requiredPallets = [
   'balances',
+  'balancesadapter',
   'common',
   'timestamp',
   'transactionpayment',
modifiedtests/src/transfer.test.tsdiffbeforeafterboth
--- a/tests/src/transfer.test.ts
+++ b/tests/src/transfer.test.ts
@@ -17,6 +17,7 @@
 import {IKeyringPair} from '@polkadot/types/types';
 import {itEth, usingEthPlaygrounds} from './eth/util';
 import {itSub, Pallets, usingPlaygrounds, expect} from './util';
+import {NON_EXISTENT_COLLECTION_ID} from './eth/util/playgrounds/types';
 
 describe('Integration Test Transfer(recipient, collection_id, item_id, value)', () => {
   let donor: IKeyringPair;
@@ -124,20 +125,17 @@
 
 
   itSub('[nft] Transfer with not existed collection_id', async ({helper}) => {
-    const collectionId = (1 << 32) - 1;
-    await expect(helper.nft.transferToken(alice, collectionId, 1, {Substrate: bob.address}))
+    await expect(helper.nft.transferToken(alice, NON_EXISTENT_COLLECTION_ID, 1, {Substrate: bob.address}))
       .to.be.rejectedWith(/common\.CollectionNotFound/);
   });
 
   itSub('[fungible] Transfer with not existed collection_id', async ({helper}) => {
-    const collectionId = (1 << 32) - 1;
-    await expect(helper.ft.transfer(alice, collectionId, {Substrate: bob.address}))
+    await expect(helper.ft.transfer(alice, NON_EXISTENT_COLLECTION_ID, {Substrate: bob.address}))
       .to.be.rejectedWith(/common\.CollectionNotFound/);
   });
 
   itSub.ifWithPallets('[refungible] Transfer with not existed collection_id', [Pallets.ReFungible], async ({helper}) => {
-    const collectionId = (1 << 32) - 1;
-    await expect(helper.rft.transferToken(alice, collectionId, 1, {Substrate: bob.address}))
+    await expect(helper.rft.transferToken(alice, NON_EXISTENT_COLLECTION_ID, 1, {Substrate: bob.address}))
       .to.be.rejectedWith(/common\.CollectionNotFound/);
   });
 
modifiedtests/src/transferFrom.test.tsdiffbeforeafterboth
before · tests/src/transferFrom.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 {IKeyringPair} from '@polkadot/types/types';18import {itSub, Pallets, usingPlaygrounds, expect} from './util';1920describe('Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {21  let alice: IKeyringPair;22  let bob: IKeyringPair;23  let charlie: IKeyringPair;2425  before(async () => {26    await usingPlaygrounds(async (helper, privateKey) => {27      const donor = await privateKey({url: import.meta.url});28      [alice, bob, charlie] = await helper.arrange.createAccounts([20n, 10n, 10n], donor);29    });30  });3132  itSub('[nft] Execute the extrinsic and check nftItemList - owner of token', async ({helper}) => {33    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-1', description: '', tokenPrefix: 'TF'});34    const nft = await collection.mintToken(alice);35    await nft.approve(alice, {Substrate: bob.address});36    expect(await nft.isApproved({Substrate: bob.address})).to.be.true;3738    await nft.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address});39    expect(await nft.getOwner()).to.be.deep.equal({Substrate: charlie.address});40  });4142  itSub('[fungible] Execute the extrinsic and check nftItemList - owner of token', async ({helper}) => {43    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-2', description: '', tokenPrefix: 'TF'});44    await collection.mint(alice, 10n);45    await collection.approveTokens(alice, {Substrate: bob.address}, 7n);46    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(7n);4748    await collection.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}, 6n);49    expect(await collection.getBalance({Substrate: charlie.address})).to.be.equal(6n);50    expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(4n);51    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(1n);52  });5354  itSub.ifWithPallets('[refungible] Execute the extrinsic and check nftItemList - owner of token', [Pallets.ReFungible], async ({helper}) => {55    const collection = await helper.rft.mintCollection(alice, {name: 'TransferFrom-3', description: '', tokenPrefix: 'TF'});56    const rft = await collection.mintToken(alice, 10n);57    await rft.approve(alice, {Substrate: bob.address}, 7n);58    expect(await rft.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(7n);5960    await rft.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}, 6n);61    expect(await rft.getBalance({Substrate: charlie.address})).to.be.equal(6n);62    expect(await rft.getBalance({Substrate: alice.address})).to.be.equal(4n);63    expect(await rft.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(1n);64  });6566  itSub('Should reduce allowance if value is big', async ({helper}) => {67    // fungible68    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-4', description: '', tokenPrefix: 'TF'});69    await collection.mint(alice, 500000n);7071    await collection.approveTokens(alice, {Substrate: bob.address}, 500000n);72    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(500000n);73    await collection.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}, 500000n);74    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(0n);75  });7677  itSub('can be called by collection owner on non-owned item when OwnerCanTransfer == true', async ({helper}) => {78    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-5', description: '', tokenPrefix: 'TF'});79    await collection.setLimits(alice, {ownerCanTransfer: true});8081    const nft = await collection.mintToken(alice, {Substrate: bob.address});82    await nft.transferFrom(alice, {Substrate: bob.address}, {Substrate: charlie.address});83    expect(await nft.getOwner()).to.be.deep.equal({Substrate: charlie.address});84  });85});8687describe('Negative Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {88  let alice: IKeyringPair;89  let bob: IKeyringPair;90  let charlie: IKeyringPair;9192  before(async () => {93    await usingPlaygrounds(async (helper, privateKey) => {94      const donor = await privateKey({url: import.meta.url});95      [alice, bob, charlie] = await helper.arrange.createAccounts([50n, 10n, 10n], donor);96    });97  });9899  itSub('transferFrom for a collection that does not exist', async ({helper}) => {100    const collectionId = (1 << 32) - 1;101    await expect(helper.collection.approveToken(alice, collectionId, 0, {Substrate: bob.address}, 1n))102      .to.be.rejectedWith(/common\.CollectionNotFound/);103    await expect(helper.collection.transferTokenFrom(bob, collectionId, 0, {Substrate: alice.address}, {Substrate: bob.address}, 1n))104      .to.be.rejectedWith(/common\.CollectionNotFound/);105  });106107  /* itSub('transferFrom for a collection that was destroyed', async ({helper}) => {108      this test copies approve negative test109  }); */110111  /* itSub('transferFrom a token that does not exist', async ({helper}) => {112    this test copies approve negative test113  }); */114115  /* itSub('transferFrom a token that was deleted', async ({helper}) => {116    this test copies approve negative test117  }); */118119  itSub('[nft] transferFrom for not approved address', async ({helper}) => {120    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-Neg-1', description: '', tokenPrefix: 'TF'});121    const nft = await collection.mintToken(alice);122123    await expect(nft.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}))124      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);125    expect(await nft.getOwner()).to.be.deep.equal({Substrate: alice.address});126  });127128  itSub('[fungible] transferFrom for not approved address', async ({helper}) => {129    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-Neg-1', description: '', tokenPrefix: 'TF'});130    await collection.mint(alice, 10n);131132    await expect(collection.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}, 5n))133      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);134    expect(await collection.getBalance({Substrate: alice.address})).to.be.deep.equal(10n);135    expect(await collection.getBalance({Substrate: bob.address})).to.be.deep.equal(0n);136    expect(await collection.getBalance({Substrate: charlie.address})).to.be.deep.equal(0n);137  });138139  itSub.ifWithPallets('[refungible] transferFrom for not approved address', [Pallets.ReFungible], async({helper}) => {140    const collection = await helper.rft.mintCollection(alice, {name: 'TransferFrom-Neg-3', description: '', tokenPrefix: 'TF'});141    const rft = await collection.mintToken(alice, 10n);142143    await expect(rft.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}))144      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);145    expect(await rft.getBalance({Substrate: alice.address})).to.be.deep.equal(10n);146    expect(await rft.getBalance({Substrate: bob.address})).to.be.deep.equal(0n);147    expect(await rft.getBalance({Substrate: charlie.address})).to.be.deep.equal(0n);148  });149150  itSub('[nft] transferFrom incorrect token count', async ({helper}) => {151    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-Neg-4', description: '', tokenPrefix: 'TF'});152    const nft = await collection.mintToken(alice);153154    await nft.approve(alice, {Substrate: bob.address});155    expect(await nft.isApproved({Substrate: bob.address})).to.be.true;156157    await expect(helper.collection.transferTokenFrom(158      bob,159      collection.collectionId,160      nft.tokenId,161      {Substrate: alice.address},162      {Substrate: charlie.address},163      2n,164    )).to.be.rejectedWith(/nonfungible\.NonfungibleItemsHaveNoAmount/);165    expect(await nft.getOwner()).to.be.deep.equal({Substrate: alice.address});166  });167168  itSub('[fungible] transferFrom incorrect token count', async ({helper}) => {169    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-Neg-5', description: '', tokenPrefix: 'TF'});170    await collection.mint(alice, 10n);171172    await collection.approveTokens(alice, {Substrate: bob.address}, 2n);173    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.eq(2n);174175    await expect(collection.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}, 5n))176      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);177    expect(await collection.getBalance({Substrate: alice.address})).to.be.deep.equal(10n);178    expect(await collection.getBalance({Substrate: bob.address})).to.be.deep.equal(0n);179    expect(await collection.getBalance({Substrate: charlie.address})).to.be.deep.equal(0n);180  });181182  itSub.ifWithPallets('[refungible] transferFrom incorrect token count', [Pallets.ReFungible], async ({helper}) => {183    const collection = await helper.rft.mintCollection(alice, {name: 'TransferFrom-Neg-6', description: '', tokenPrefix: 'TF'});184    const rft = await collection.mintToken(alice, 10n);185186    await rft.approve(alice, {Substrate: bob.address}, 5n);187    expect(await rft.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.eq(5n);188189    await expect(rft.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}, 7n))190      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);191    expect(await rft.getBalance({Substrate: alice.address})).to.be.deep.equal(10n);192    expect(await rft.getBalance({Substrate: bob.address})).to.be.deep.equal(0n);193    expect(await rft.getBalance({Substrate: charlie.address})).to.be.deep.equal(0n);194  });195196  itSub('[nft] execute transferFrom from account that is not owner of collection', async ({helper}) => {197    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-Neg-7', description: '', tokenPrefix: 'TF'});198    const nft = await collection.mintToken(alice);199200    await expect(nft.approve(charlie, {Substrate: bob.address})).to.be.rejectedWith(/common\.CantApproveMoreThanOwned/);201    expect(await nft.isApproved({Substrate: bob.address})).to.be.false;202203    await expect(nft.transferFrom(204      charlie,205      {Substrate: alice.address},206      {Substrate: charlie.address},207    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);208    expect(await nft.getOwner()).to.be.deep.equal({Substrate: alice.address});209  });210211  itSub('[fungible] execute transferFrom from account that is not owner of collection', async ({helper}) => {212    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-Neg-8', description: '', tokenPrefix: 'TF'});213    await collection.mint(alice, 10000n);214215    await expect(collection.approveTokens(charlie, {Substrate: bob.address}, 1n)).to.be.rejectedWith(/common\.CantApproveMoreThanOwned/);216    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.eq(0n);217    expect(await collection.getApprovedTokens({Substrate: charlie.address}, {Substrate: bob.address})).to.be.eq(0n);218219    await expect(collection.transferFrom(220      charlie,221      {Substrate: alice.address},222      {Substrate: charlie.address},223    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);224    expect(await collection.getBalance({Substrate: alice.address})).to.be.deep.equal(10000n);225    expect(await collection.getBalance({Substrate: bob.address})).to.be.deep.equal(0n);226    expect(await collection.getBalance({Substrate: charlie.address})).to.be.deep.equal(0n);227  });228229  itSub.ifWithPallets('[refungible] execute transferFrom from account that is not owner of collection', [Pallets.ReFungible], async ({helper}) => {230    const collection = await helper.rft.mintCollection(alice, {name: 'TransferFrom-Neg-9', description: '', tokenPrefix: 'TF'});231    const rft = await collection.mintToken(alice, 10000n);232233    await expect(rft.approve(charlie, {Substrate: bob.address}, 1n)).to.be.rejectedWith(/common\.CantApproveMoreThanOwned/);234    expect(await rft.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.eq(0n);235    expect(await rft.getApprovedPieces({Substrate: charlie.address}, {Substrate: bob.address})).to.be.eq(0n);236237    await expect(rft.transferFrom(238      charlie,239      {Substrate: alice.address},240      {Substrate: charlie.address},241    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);242    expect(await rft.getBalance({Substrate: alice.address})).to.be.deep.equal(10000n);243    expect(await rft.getBalance({Substrate: bob.address})).to.be.deep.equal(0n);244    expect(await rft.getBalance({Substrate: charlie.address})).to.be.deep.equal(0n);245  });246247  itSub('transferFrom burnt token before approve NFT', async ({helper}) => {248    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-Neg-10', description: '', tokenPrefix: 'TF'});249    await collection.setLimits(alice, {ownerCanTransfer: true});250    const nft = await collection.mintToken(alice);251252    await nft.burn(alice);253    await expect(nft.approve(alice, {Substrate: bob.address})).to.be.rejectedWith(/common\.TokenNotFound/);254255    await expect(nft.transferFrom(256      bob,257      {Substrate: alice.address},258      {Substrate: charlie.address},259    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);260  });261262  itSub('transferFrom burnt token before approve Fungible', async ({helper}) => {263    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-Neg-11', description: '', tokenPrefix: 'TF'});264    await collection.setLimits(alice, {ownerCanTransfer: true});265    await collection.mint(alice, 10n);266267    await collection.burnTokens(alice, 10n);268    await expect(collection.approveTokens(alice, {Substrate: bob.address})).to.be.not.rejected;269270    await expect(collection.transferFrom(271      alice,272      {Substrate: alice.address},273      {Substrate: charlie.address},274    )).to.be.rejectedWith(/common\.TokenValueTooLow/);275  });276277  itSub.ifWithPallets('transferFrom burnt token before approve ReFungible', [Pallets.ReFungible], async ({helper}) => {278    const collection = await helper.rft.mintCollection(alice, {name: 'TransferFrom-Neg-12', description: '', tokenPrefix: 'TF'});279    await collection.setLimits(alice, {ownerCanTransfer: true});280    const rft = await collection.mintToken(alice, 10n);281282    await rft.burn(alice, 10n);283    await expect(rft.approve(alice, {Substrate: bob.address})).to.be.rejectedWith(/common\.CantApproveMoreThanOwned/);284285    await expect(rft.transferFrom(286      alice,287      {Substrate: alice.address},288      {Substrate: charlie.address},289    )).to.be.rejectedWith(/common\.TokenValueTooLow/);290  });291292  itSub('transferFrom burnt token after approve NFT', async ({helper}) => {293    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-Neg-13', description: '', tokenPrefix: 'TF'});294    const nft = await collection.mintToken(alice);295296    await nft.approve(alice, {Substrate: bob.address});297    expect(await nft.isApproved({Substrate: bob.address})).to.be.true;298299    await nft.burn(alice);300301    await expect(nft.transferFrom(302      bob,303      {Substrate: alice.address},304      {Substrate: charlie.address},305    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);306  });307308  itSub('transferFrom burnt token after approve Fungible', async ({helper}) => {309    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-Neg-14', description: '', tokenPrefix: 'TF'});310    await collection.mint(alice, 10n);311312    await collection.approveTokens(alice, {Substrate: bob.address});313    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.eq(1n);314315    await collection.burnTokens(alice, 10n);316317    await expect(collection.transferFrom(318      bob,319      {Substrate: alice.address},320      {Substrate: charlie.address},321    )).to.be.rejectedWith(/common\.TokenValueTooLow/);322  });323324  itSub.ifWithPallets('transferFrom burnt token after approve ReFungible', [Pallets.ReFungible], async ({helper}) => {325    const collection = await helper.rft.mintCollection(alice, {name: 'TransferFrom-Neg-15', description: '', tokenPrefix: 'TF'});326    const rft = await collection.mintToken(alice, 10n);327328    await rft.approve(alice, {Substrate: bob.address}, 10n);329    expect(await rft.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.eq(10n);330331    await rft.burn(alice, 10n);332333    await expect(rft.transferFrom(334      bob,335      {Substrate: alice.address},336      {Substrate: charlie.address},337    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);338  });339340  itSub('fails when called by collection owner on non-owned item when OwnerCanTransfer == false', async ({helper}) => {341    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-Neg-16', description: '', tokenPrefix: 'TF'});342    const nft = await collection.mintToken(alice, {Substrate: bob.address});343344    await collection.setLimits(alice, {ownerCanTransfer: false});345346    await expect(nft.transferFrom(347      alice,348      {Substrate: bob.address},349      {Substrate: charlie.address},350    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);351  });352353  itSub('zero transfer NFT', async ({helper}) => {354    const collection = await helper.nft.mintCollection(alice, {name: 'Zero', description: 'Zero transfer', tokenPrefix: 'TF'});355    const notApprovedNft = await collection.mintToken(alice, {Substrate: bob.address});356    const approvedNft = await collection.mintToken(alice, {Substrate: bob.address});357    await approvedNft.approve(bob, {Substrate: alice.address});358359    // 1. Cannot zero transferFrom (non-existing token)360    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.transferFrom', [{Substrate: bob.address}, {Substrate: alice.address}, collection.collectionId, 9999, 0])).to.be.rejectedWith('common.ApprovedValueTooLow');361    // 2. Cannot zero transferFrom (not approved token)362    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.transferFrom', [{Substrate: bob.address}, {Substrate: alice.address}, collection.collectionId, notApprovedNft.tokenId, 0])).to.be.rejectedWith('common.ApprovedValueTooLow');363    // 3. Can zero transferFrom (approved token):364    await helper.executeExtrinsic(alice, 'api.tx.unique.transferFrom', [{Substrate: bob.address}, {Substrate: alice.address}, collection.collectionId, approvedNft.tokenId, 0]);365366    // 4.1 approvedNft still approved:367    expect(await approvedNft.isApproved({Substrate: alice.address})).to.be.true;368    // 4.2 bob is still the owner:369    expect(await approvedNft.getOwner()).to.deep.eq({Substrate: bob.address});370    expect(await notApprovedNft.getOwner()).to.deep.eq({Substrate: bob.address});371    // 4.3 Alice can transfer approved nft:372    await approvedNft.transferFrom(alice, {Substrate: bob.address}, {Substrate: alice.address});373    expect(await approvedNft.getOwner()).to.deep.eq({Substrate: alice.address});374  });375});
after · tests/src/transferFrom.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 {IKeyringPair} from '@polkadot/types/types';18import {itSub, Pallets, usingPlaygrounds, expect} from './util';19import {NON_EXISTENT_COLLECTION_ID} from './eth/util/playgrounds/types';2021describe('Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {22  let alice: IKeyringPair;23  let bob: IKeyringPair;24  let charlie: IKeyringPair;2526  before(async () => {27    await usingPlaygrounds(async (helper, privateKey) => {28      const donor = await privateKey({url: import.meta.url});29      [alice, bob, charlie] = await helper.arrange.createAccounts([20n, 10n, 10n], donor);30    });31  });3233  itSub('[nft] Execute the extrinsic and check nftItemList - owner of token', async ({helper}) => {34    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-1', description: '', tokenPrefix: 'TF'});35    const nft = await collection.mintToken(alice);36    await nft.approve(alice, {Substrate: bob.address});37    expect(await nft.isApproved({Substrate: bob.address})).to.be.true;3839    await nft.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address});40    expect(await nft.getOwner()).to.be.deep.equal({Substrate: charlie.address});41  });4243  itSub('[fungible] Execute the extrinsic and check nftItemList - owner of token', async ({helper}) => {44    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-2', description: '', tokenPrefix: 'TF'});45    await collection.mint(alice, 10n);46    await collection.approveTokens(alice, {Substrate: bob.address}, 7n);47    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(7n);4849    await collection.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}, 6n);50    expect(await collection.getBalance({Substrate: charlie.address})).to.be.equal(6n);51    expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(4n);52    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(1n);53  });5455  itSub.ifWithPallets('[refungible] Execute the extrinsic and check nftItemList - owner of token', [Pallets.ReFungible], async ({helper}) => {56    const collection = await helper.rft.mintCollection(alice, {name: 'TransferFrom-3', description: '', tokenPrefix: 'TF'});57    const rft = await collection.mintToken(alice, 10n);58    await rft.approve(alice, {Substrate: bob.address}, 7n);59    expect(await rft.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(7n);6061    await rft.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}, 6n);62    expect(await rft.getBalance({Substrate: charlie.address})).to.be.equal(6n);63    expect(await rft.getBalance({Substrate: alice.address})).to.be.equal(4n);64    expect(await rft.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(1n);65  });6667  itSub('Should reduce allowance if value is big', async ({helper}) => {68    // fungible69    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-4', description: '', tokenPrefix: 'TF'});70    await collection.mint(alice, 500000n);7172    await collection.approveTokens(alice, {Substrate: bob.address}, 500000n);73    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(500000n);74    await collection.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}, 500000n);75    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(0n);76  });7778  itSub('can be called by collection owner on non-owned item when OwnerCanTransfer == true', async ({helper}) => {79    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-5', description: '', tokenPrefix: 'TF'});80    await collection.setLimits(alice, {ownerCanTransfer: true});8182    const nft = await collection.mintToken(alice, {Substrate: bob.address});83    await nft.transferFrom(alice, {Substrate: bob.address}, {Substrate: charlie.address});84    expect(await nft.getOwner()).to.be.deep.equal({Substrate: charlie.address});85  });86});8788describe('Negative Integration Test transferFrom(from, recipient, collection_id, item_id, value):', () => {89  let alice: IKeyringPair;90  let bob: IKeyringPair;91  let charlie: IKeyringPair;9293  before(async () => {94    await usingPlaygrounds(async (helper, privateKey) => {95      const donor = await privateKey({url: import.meta.url});96      [alice, bob, charlie] = await helper.arrange.createAccounts([50n, 10n, 10n], donor);97    });98  });99100  itSub('transferFrom for a collection that does not exist', async ({helper}) => {101    await expect(helper.collection.approveToken(alice, NON_EXISTENT_COLLECTION_ID, 0, {Substrate: bob.address}, 1n))102      .to.be.rejectedWith(/common\.CollectionNotFound/);103    await expect(helper.collection.transferTokenFrom(bob, NON_EXISTENT_COLLECTION_ID, 0, {Substrate: alice.address}, {Substrate: bob.address}, 1n))104      .to.be.rejectedWith(/common\.CollectionNotFound/);105  });106107  /* itSub('transferFrom for a collection that was destroyed', async ({helper}) => {108      this test copies approve negative test109  }); */110111  /* itSub('transferFrom a token that does not exist', async ({helper}) => {112    this test copies approve negative test113  }); */114115  /* itSub('transferFrom a token that was deleted', async ({helper}) => {116    this test copies approve negative test117  }); */118119  itSub('[nft] transferFrom for not approved address', async ({helper}) => {120    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-Neg-1', description: '', tokenPrefix: 'TF'});121    const nft = await collection.mintToken(alice);122123    await expect(nft.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}))124      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);125    expect(await nft.getOwner()).to.be.deep.equal({Substrate: alice.address});126  });127128  itSub('[fungible] transferFrom for not approved address', async ({helper}) => {129    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-Neg-1', description: '', tokenPrefix: 'TF'});130    await collection.mint(alice, 10n);131132    await expect(collection.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}, 5n))133      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);134    expect(await collection.getBalance({Substrate: alice.address})).to.be.deep.equal(10n);135    expect(await collection.getBalance({Substrate: bob.address})).to.be.deep.equal(0n);136    expect(await collection.getBalance({Substrate: charlie.address})).to.be.deep.equal(0n);137  });138139  itSub.ifWithPallets('[refungible] transferFrom for not approved address', [Pallets.ReFungible], async({helper}) => {140    const collection = await helper.rft.mintCollection(alice, {name: 'TransferFrom-Neg-3', description: '', tokenPrefix: 'TF'});141    const rft = await collection.mintToken(alice, 10n);142143    await expect(rft.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}))144      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);145    expect(await rft.getBalance({Substrate: alice.address})).to.be.deep.equal(10n);146    expect(await rft.getBalance({Substrate: bob.address})).to.be.deep.equal(0n);147    expect(await rft.getBalance({Substrate: charlie.address})).to.be.deep.equal(0n);148  });149150  itSub('[nft] transferFrom incorrect token count', async ({helper}) => {151    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-Neg-4', description: '', tokenPrefix: 'TF'});152    const nft = await collection.mintToken(alice);153154    await nft.approve(alice, {Substrate: bob.address});155    expect(await nft.isApproved({Substrate: bob.address})).to.be.true;156157    await expect(helper.collection.transferTokenFrom(158      bob,159      collection.collectionId,160      nft.tokenId,161      {Substrate: alice.address},162      {Substrate: charlie.address},163      2n,164    )).to.be.rejectedWith(/nonfungible\.NonfungibleItemsHaveNoAmount/);165    expect(await nft.getOwner()).to.be.deep.equal({Substrate: alice.address});166  });167168  itSub('[fungible] transferFrom incorrect token count', async ({helper}) => {169    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-Neg-5', description: '', tokenPrefix: 'TF'});170    await collection.mint(alice, 10n);171172    await collection.approveTokens(alice, {Substrate: bob.address}, 2n);173    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.eq(2n);174175    await expect(collection.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}, 5n))176      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);177    expect(await collection.getBalance({Substrate: alice.address})).to.be.deep.equal(10n);178    expect(await collection.getBalance({Substrate: bob.address})).to.be.deep.equal(0n);179    expect(await collection.getBalance({Substrate: charlie.address})).to.be.deep.equal(0n);180  });181182  itSub.ifWithPallets('[refungible] transferFrom incorrect token count', [Pallets.ReFungible], async ({helper}) => {183    const collection = await helper.rft.mintCollection(alice, {name: 'TransferFrom-Neg-6', description: '', tokenPrefix: 'TF'});184    const rft = await collection.mintToken(alice, 10n);185186    await rft.approve(alice, {Substrate: bob.address}, 5n);187    expect(await rft.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.eq(5n);188189    await expect(rft.transferFrom(bob, {Substrate: alice.address}, {Substrate: charlie.address}, 7n))190      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);191    expect(await rft.getBalance({Substrate: alice.address})).to.be.deep.equal(10n);192    expect(await rft.getBalance({Substrate: bob.address})).to.be.deep.equal(0n);193    expect(await rft.getBalance({Substrate: charlie.address})).to.be.deep.equal(0n);194  });195196  itSub('[nft] execute transferFrom from account that is not owner of collection', async ({helper}) => {197    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-Neg-7', description: '', tokenPrefix: 'TF'});198    const nft = await collection.mintToken(alice);199200    await expect(nft.approve(charlie, {Substrate: bob.address})).to.be.rejectedWith(/common\.CantApproveMoreThanOwned/);201    expect(await nft.isApproved({Substrate: bob.address})).to.be.false;202203    await expect(nft.transferFrom(204      charlie,205      {Substrate: alice.address},206      {Substrate: charlie.address},207    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);208    expect(await nft.getOwner()).to.be.deep.equal({Substrate: alice.address});209  });210211  itSub('[fungible] execute transferFrom from account that is not owner of collection', async ({helper}) => {212    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-Neg-8', description: '', tokenPrefix: 'TF'});213    await collection.mint(alice, 10000n);214215    await expect(collection.approveTokens(charlie, {Substrate: bob.address}, 1n)).to.be.rejectedWith(/common\.CantApproveMoreThanOwned/);216    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.eq(0n);217    expect(await collection.getApprovedTokens({Substrate: charlie.address}, {Substrate: bob.address})).to.be.eq(0n);218219    await expect(collection.transferFrom(220      charlie,221      {Substrate: alice.address},222      {Substrate: charlie.address},223    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);224    expect(await collection.getBalance({Substrate: alice.address})).to.be.deep.equal(10000n);225    expect(await collection.getBalance({Substrate: bob.address})).to.be.deep.equal(0n);226    expect(await collection.getBalance({Substrate: charlie.address})).to.be.deep.equal(0n);227  });228229  itSub.ifWithPallets('[refungible] execute transferFrom from account that is not owner of collection', [Pallets.ReFungible], async ({helper}) => {230    const collection = await helper.rft.mintCollection(alice, {name: 'TransferFrom-Neg-9', description: '', tokenPrefix: 'TF'});231    const rft = await collection.mintToken(alice, 10000n);232233    await expect(rft.approve(charlie, {Substrate: bob.address}, 1n)).to.be.rejectedWith(/common\.CantApproveMoreThanOwned/);234    expect(await rft.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.eq(0n);235    expect(await rft.getApprovedPieces({Substrate: charlie.address}, {Substrate: bob.address})).to.be.eq(0n);236237    await expect(rft.transferFrom(238      charlie,239      {Substrate: alice.address},240      {Substrate: charlie.address},241    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);242    expect(await rft.getBalance({Substrate: alice.address})).to.be.deep.equal(10000n);243    expect(await rft.getBalance({Substrate: bob.address})).to.be.deep.equal(0n);244    expect(await rft.getBalance({Substrate: charlie.address})).to.be.deep.equal(0n);245  });246247  itSub('transferFrom burnt token before approve NFT', async ({helper}) => {248    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-Neg-10', description: '', tokenPrefix: 'TF'});249    await collection.setLimits(alice, {ownerCanTransfer: true});250    const nft = await collection.mintToken(alice);251252    await nft.burn(alice);253    await expect(nft.approve(alice, {Substrate: bob.address})).to.be.rejectedWith(/common\.TokenNotFound/);254255    await expect(nft.transferFrom(256      bob,257      {Substrate: alice.address},258      {Substrate: charlie.address},259    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);260  });261262  itSub('transferFrom burnt token before approve Fungible', async ({helper}) => {263    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-Neg-11', description: '', tokenPrefix: 'TF'});264    await collection.setLimits(alice, {ownerCanTransfer: true});265    await collection.mint(alice, 10n);266267    await collection.burnTokens(alice, 10n);268    await expect(collection.approveTokens(alice, {Substrate: bob.address})).to.be.not.rejected;269270    await expect(collection.transferFrom(271      alice,272      {Substrate: alice.address},273      {Substrate: charlie.address},274    )).to.be.rejectedWith(/common\.TokenValueTooLow/);275  });276277  itSub.ifWithPallets('transferFrom burnt token before approve ReFungible', [Pallets.ReFungible], async ({helper}) => {278    const collection = await helper.rft.mintCollection(alice, {name: 'TransferFrom-Neg-12', description: '', tokenPrefix: 'TF'});279    await collection.setLimits(alice, {ownerCanTransfer: true});280    const rft = await collection.mintToken(alice, 10n);281282    await rft.burn(alice, 10n);283    await expect(rft.approve(alice, {Substrate: bob.address})).to.be.rejectedWith(/common\.CantApproveMoreThanOwned/);284285    await expect(rft.transferFrom(286      alice,287      {Substrate: alice.address},288      {Substrate: charlie.address},289    )).to.be.rejectedWith(/common\.TokenValueTooLow/);290  });291292  itSub('transferFrom burnt token after approve NFT', async ({helper}) => {293    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-Neg-13', description: '', tokenPrefix: 'TF'});294    const nft = await collection.mintToken(alice);295296    await nft.approve(alice, {Substrate: bob.address});297    expect(await nft.isApproved({Substrate: bob.address})).to.be.true;298299    await nft.burn(alice);300301    await expect(nft.transferFrom(302      bob,303      {Substrate: alice.address},304      {Substrate: charlie.address},305    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);306  });307308  itSub('transferFrom burnt token after approve Fungible', async ({helper}) => {309    const collection = await helper.ft.mintCollection(alice, {name: 'TransferFrom-Neg-14', description: '', tokenPrefix: 'TF'});310    await collection.mint(alice, 10n);311312    await collection.approveTokens(alice, {Substrate: bob.address});313    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.eq(1n);314315    await collection.burnTokens(alice, 10n);316317    await expect(collection.transferFrom(318      bob,319      {Substrate: alice.address},320      {Substrate: charlie.address},321    )).to.be.rejectedWith(/common\.TokenValueTooLow/);322  });323324  itSub.ifWithPallets('transferFrom burnt token after approve ReFungible', [Pallets.ReFungible], async ({helper}) => {325    const collection = await helper.rft.mintCollection(alice, {name: 'TransferFrom-Neg-15', description: '', tokenPrefix: 'TF'});326    const rft = await collection.mintToken(alice, 10n);327328    await rft.approve(alice, {Substrate: bob.address}, 10n);329    expect(await rft.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.eq(10n);330331    await rft.burn(alice, 10n);332333    await expect(rft.transferFrom(334      bob,335      {Substrate: alice.address},336      {Substrate: charlie.address},337    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);338  });339340  itSub('fails when called by collection owner on non-owned item when OwnerCanTransfer == false', async ({helper}) => {341    const collection = await helper.nft.mintCollection(alice, {name: 'TransferFrom-Neg-16', description: '', tokenPrefix: 'TF'});342    const nft = await collection.mintToken(alice, {Substrate: bob.address});343344    await collection.setLimits(alice, {ownerCanTransfer: false});345346    await expect(nft.transferFrom(347      alice,348      {Substrate: bob.address},349      {Substrate: charlie.address},350    )).to.be.rejectedWith(/common\.ApprovedValueTooLow/);351  });352353  itSub('zero transfer NFT', async ({helper}) => {354    const collection = await helper.nft.mintCollection(alice, {name: 'Zero', description: 'Zero transfer', tokenPrefix: 'TF'});355    const notApprovedNft = await collection.mintToken(alice, {Substrate: bob.address});356    const approvedNft = await collection.mintToken(alice, {Substrate: bob.address});357    await approvedNft.approve(bob, {Substrate: alice.address});358359    // 1. Cannot zero transferFrom (non-existing token)360    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.transferFrom', [{Substrate: bob.address}, {Substrate: alice.address}, collection.collectionId, 9999, 0])).to.be.rejectedWith('common.ApprovedValueTooLow');361    // 2. Cannot zero transferFrom (not approved token)362    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.transferFrom', [{Substrate: bob.address}, {Substrate: alice.address}, collection.collectionId, notApprovedNft.tokenId, 0])).to.be.rejectedWith('common.ApprovedValueTooLow');363    // 3. Can zero transferFrom (approved token):364    await helper.executeExtrinsic(alice, 'api.tx.unique.transferFrom', [{Substrate: bob.address}, {Substrate: alice.address}, collection.collectionId, approvedNft.tokenId, 0]);365366    // 4.1 approvedNft still approved:367    expect(await approvedNft.isApproved({Substrate: alice.address})).to.be.true;368    // 4.2 bob is still the owner:369    expect(await approvedNft.getOwner()).to.deep.eq({Substrate: bob.address});370    expect(await notApprovedNft.getOwner()).to.deep.eq({Substrate: bob.address});371    // 4.3 Alice can transfer approved nft:372    await approvedNft.transferFrom(alice, {Substrate: bob.address}, {Substrate: alice.address});373    expect(await approvedNft.getOwner()).to.deep.eq({Substrate: alice.address});374  });375});