1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {itSub, Pallets, usingPlaygrounds, expect} from './util/playgrounds';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 = privateKey('//Alice');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);47 48 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);59 60 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 68 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 = privateKey('//Alice');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 108109110111 112113114115 116117118119 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 });352});