difftreelog
Tests: code-style fixes
in: master
14 files changed
tests/src/addCollectionAdmin.test.tsdiffbeforeafterboth--- a/tests/src/addCollectionAdmin.test.ts
+++ b/tests/src/addCollectionAdmin.test.ts
@@ -110,7 +110,7 @@
const [alice, ...accounts] = await helper.arrange.createAccounts([10n, 0n, 0n, 0n, 0n, 0n, 0n, 0n], donor);
const collection = await helper.nft.mintCollection(alice, {name: 'Collection Name', description: 'Collection Description', tokenPrefix: 'COL'});
- const chainAdminLimit = (helper.api!.consts.common.collectionAdminsLimit as any).toNumber();
+ const chainAdminLimit = (helper.getApi().consts.common.collectionAdminsLimit as any).toNumber();
expect(chainAdminLimit).to.be.equal(5);
for (let i = 0; i < chainAdminLimit; i++) {
tests/src/approve.test.tsdiffbeforeafterboth1// 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 {expect, itSub, Pallets, usingPlaygrounds} from './util/playgrounds';192021describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {22 let alice: IKeyringPair;23 let bob: IKeyringPair;24 let charlie: IKeyringPair;2526 before(async () => {27 await usingPlaygrounds(async (helper, privateKey) => {28 const donor = privateKey('//Alice');29 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);30 });31 });3233 itSub('[nft] Execute the extrinsic and check approvedList', async ({helper}) => {34 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});35 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: alice.address});36 await helper.nft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address});37 expect(await helper.nft.isTokenApproved(collectionId, tokenId, {Substrate: bob.address})).to.be.true;38 });3940 itSub('[fungible] Execute the extrinsic and check approvedList', async ({helper}) => {41 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);42 await helper.ft.mintTokens(alice, collectionId, 10n, {Substrate: alice.address});43 const tokenId = await helper.ft.getLastTokenId(collectionId);44 await helper.ft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address});45 const amount = await helper.ft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});46 expect(amount).to.be.equal(BigInt(1));47 });4849 itSub.ifWithPallets('[refungible] Execute the extrinsic and check approvedList', [Pallets.ReFungible], async ({helper}) => {50 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});51 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: alice.address, pieces: 100n});52 await helper.rft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address});53 const amount = await helper.rft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});54 expect(amount).to.be.equal(BigInt(1));55 });5657 itSub('[nft] Remove approval by using 0 amount', async ({helper}) => {58 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});59 const collectionId = collection.collectionId;60 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: alice.address});61 await helper.nft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address});62 expect(await helper.nft.isTokenApproved(collectionId, tokenId, {Substrate: bob.address})).to.be.true;63 await helper.signTransaction(alice, helper.api?.tx.unique.approve({Substrate: bob.address}, collectionId, tokenId, 0));64 expect(await helper.nft.isTokenApproved(collectionId, tokenId, {Substrate: bob.address})).to.be.false;65 });6667 itSub('[fungible] Remove approval by using 0 amount', async ({helper}) => {68 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);69 await helper.ft.mintTokens(alice, collectionId, 10n, {Substrate: alice.address});70 const tokenId = await helper.ft.getLastTokenId(collectionId);71 await helper.ft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address});72 const amountBefore = await helper.ft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});73 expect(amountBefore).to.be.equal(BigInt(1));7475 await helper.ft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address}, 0n);76 const amountAfter = await helper.ft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});77 expect(amountAfter).to.be.equal(BigInt(0));78 });7980 itSub.ifWithPallets('[refungible] Remove approval by using 0 amount', [Pallets.ReFungible], async ({helper}) => {81 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});82 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: alice.address, pieces: 100n});83 await helper.rft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address});84 const amountBefore = await helper.rft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});85 expect(amountBefore).to.be.equal(BigInt(1));8687 await helper.rft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address}, 0n);88 const amountAfter = await helper.rft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});89 expect(amountAfter).to.be.equal(BigInt(0));90 });9192 itSub('can`t be called by collection owner on non-owned item when OwnerCanTransfer == false', async ({helper}) => {93 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});94 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: bob.address});95 const approveTokenTx = async () => helper.nft.approveToken(alice, collectionId, tokenId, {Substrate: charlie.address});96 await expect(approveTokenTx()).to.be.rejected;97 });98});99100describe('Normal user can approve other users to transfer:', () => {101 let alice: IKeyringPair;102 let bob: IKeyringPair;103 let charlie: IKeyringPair;104105 before(async () => {106 await usingPlaygrounds(async (helper, privateKey) => {107 const donor = privateKey('//Alice');108 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);109 });110 });111112 itSub('NFT', async ({helper}) => {113 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});114 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: bob.address});115 await helper.nft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address});116 expect(await helper.nft.isTokenApproved(collectionId, tokenId, {Substrate: charlie.address})).to.be.true;117 });118119 itSub('Fungible up to an approved amount', async ({helper}) => {120 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);121 await helper.ft.mintTokens(alice, collectionId, 10n, bob.address);122 const tokenId = await helper.ft.getLastTokenId(collectionId);123 await helper.ft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address});124 const amount = await helper.ft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: charlie.address}, {Substrate: bob.address});125 expect(amount).to.be.equal(BigInt(1));126 });127128 itSub.ifWithPallets('ReFungible up to an approved amount', [Pallets.ReFungible], async ({helper}) => {129 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});130 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: bob.address, pieces: 100n});131 await helper.rft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address}, 100n);132 const amount = await helper.rft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: charlie.address}, {Substrate: bob.address});133 expect(amount).to.be.equal(BigInt(100n));134 });135});136137describe('Approved users can transferFrom up to approved amount:', () => {138 let alice: IKeyringPair;139 let bob: IKeyringPair;140 let charlie: IKeyringPair;141142 before(async () => {143 await usingPlaygrounds(async (helper, privateKey) => {144 const donor = privateKey('//Alice');145 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);146 });147 });148149 itSub('NFT', async ({helper}) => {150 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});151 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: bob.address});152 await helper.nft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address});153 await helper.nft.transferTokenFrom(charlie, collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});154 const owner = await helper.nft.getTokenOwner(collectionId, tokenId);155 expect(owner.Substrate).to.be.equal(alice.address);156 });157158 itSub('Fungible up to an approved amount', async ({helper}) => {159 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);160 await helper.ft.mintTokens(alice, collectionId, 10n, bob.address);161 const tokenId = await helper.ft.getLastTokenId(collectionId);162 await helper.ft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address});163 const before = await helper.ft.getBalance(collectionId, {Substrate: alice.address});164 await helper.ft.transferTokenFrom(charlie, collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address}, 1n);165 const after = await helper.ft.getBalance(collectionId, {Substrate: alice.address});166 expect(after - before).to.be.equal(BigInt(1));167 });168169 itSub.ifWithPallets('ReFungible up to an approved amount', [Pallets.ReFungible], async ({helper}) => {170 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});171 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: bob.address, pieces: 100n});172 await helper.rft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address});173 const before = await helper.rft.getTokenBalance(collectionId, tokenId, {Substrate: alice.address});174 await helper.rft.transferTokenFrom(charlie, collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address}, 1n);175 const after = await helper.rft.getTokenBalance(collectionId, tokenId, {Substrate: alice.address});176 expect(after - before).to.be.equal(BigInt(1));177 });178});179180describe('Approved users cannot use transferFrom to repeat transfers if approved amount was already transferred:', () => {181 let alice: IKeyringPair;182 let bob: IKeyringPair;183 let charlie: IKeyringPair;184185 before(async () => {186 await usingPlaygrounds(async (helper, privateKey) => {187 const donor = privateKey('//Alice');188 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);189 });190 });191192 itSub('NFT', async ({helper}) => {193 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});194 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: bob.address});195 await helper.nft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address});196 await helper.nft.transferTokenFrom(charlie, collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});197 const owner = await helper.nft.getTokenOwner(collectionId, tokenId);198 expect(owner.Substrate).to.be.equal(alice.address);199 const transferTokenFromTx = async () => helper.nft.transferTokenFrom(charlie, collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});200 await expect(transferTokenFromTx()).to.be.rejected;201 });202203 itSub('Fungible up to an approved amount', async ({helper}) => {204 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);205 await helper.ft.mintTokens(alice, collectionId, 10n, bob.address);206 const tokenId = await helper.ft.getLastTokenId(collectionId);207 await helper.ft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address});208 const before = await helper.ft.getBalance(collectionId, {Substrate: alice.address});209 await helper.ft.transferTokenFrom(charlie, collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address}, 1n);210 const after = await helper.ft.getBalance(collectionId, {Substrate: alice.address});211 expect(after - before).to.be.equal(BigInt(1));212213 const transferTokenFromTx = async () => helper.ft.transferTokenFrom(charlie, collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address}, 1n);214 await expect(transferTokenFromTx()).to.be.rejected;215 });216217 itSub.ifWithPallets('ReFungible up to an approved amount', [Pallets.ReFungible], async ({helper}) => {218 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});219 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: bob.address, pieces: 100n});220 await helper.rft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address}, 100n);221 const before = await helper.rft.getTokenBalance(collectionId, tokenId, {Substrate: alice.address});222 await helper.rft.transferTokenFrom(charlie, collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address}, 100n);223 const after = await helper.rft.getTokenBalance(collectionId, tokenId, {Substrate: alice.address});224 expect(after - before).to.be.equal(BigInt(100));225 const transferTokenFromTx = async () => helper.rft.transferTokenFrom(charlie, collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address}, 100n);226 await expect(transferTokenFromTx()).to.be.rejected;227 });228});229230describe('Approved amount decreases by the transferred amount.:', () => {231 let alice: IKeyringPair;232 let bob: IKeyringPair;233 let charlie: IKeyringPair;234 let dave: IKeyringPair;235236 before(async () => {237 await usingPlaygrounds(async (helper, privateKey) => {238 const donor = privateKey('//Alice');239 [alice, bob, charlie, dave] = await helper.arrange.createAccounts([100n, 100n, 100n, 100n], donor);240 });241 });242243 itSub('If a user B is approved to transfer 10 Fungible tokens from user A, they can transfer 2 tokens to user C, which will result in decreasing approval from 10 to 8. Then user B can transfer 8 tokens to user D.', async ({helper}) => {244 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);245 await helper.ft.mintTokens(alice, collectionId, 10n, alice.address);246 const tokenId = await helper.ft.getLastTokenId(collectionId);247 await helper.ft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address}, 10n);248249 const charlieBefore = await helper.ft.getBalance(collectionId, {Substrate: charlie.address});250 await helper.ft.transferTokenFrom(bob, collectionId, tokenId, {Substrate: alice.address}, {Substrate: charlie.address}, 2n);251 const charlieAfter = await helper.ft.getBalance(collectionId, {Substrate: charlie.address});252 expect(charlieAfter - charlieBefore).to.be.equal(BigInt(2));253254 const daveBefore = await helper.ft.getBalance(collectionId, {Substrate: dave.address});255 await helper.ft.transferTokenFrom(bob, collectionId, tokenId, {Substrate: alice.address}, {Substrate: dave.address}, 8n);256 const daveAfter = await helper.ft.getBalance(collectionId, {Substrate: dave.address});257 expect(daveAfter - daveBefore).to.be.equal(BigInt(8));258 });259});260261describe('User may clear the approvals to approving for 0 amount:', () => {262 let alice: IKeyringPair;263 let bob: IKeyringPair;264 let charlie: IKeyringPair;265266 before(async () => {267 await usingPlaygrounds(async (helper, privateKey) => {268 const donor = privateKey('//Alice');269 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);270 });271 });272273 itSub('NFT', async ({helper}) => {274 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});275 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: alice.address});276 await helper.nft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address});277 expect(await helper.nft.isTokenApproved(collectionId, tokenId, {Substrate: bob.address})).to.be.true;278 await helper.signTransaction(alice, helper.api?.tx.unique.approve({Substrate: bob.address}, collectionId, tokenId, 0));279 expect(await helper.nft.isTokenApproved(collectionId, tokenId, {Substrate: bob.address})).to.be.false;280 const transferTokenFromTx = async () => helper.nft.transferTokenFrom(bob, collectionId, tokenId, {Substrate: bob.address}, {Substrate: bob.address});281 await expect(transferTokenFromTx()).to.be.rejected;282 });283284 itSub('Fungible', async ({helper}) => {285 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);286 await helper.ft.mintTokens(alice, collectionId, 10n, alice.address);287 const tokenId = await helper.ft.getLastTokenId(collectionId);288 await helper.ft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address});289 const amountBefore = await helper.ft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});290 expect(amountBefore).to.be.equal(BigInt(1));291292 await helper.ft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address}, 0n);293 const amountAfter = await helper.ft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});294 expect(amountAfter).to.be.equal(BigInt(0));295296 const transferTokenFromTx = async () => helper.ft.transferTokenFrom(bob, collectionId, tokenId, {Substrate: bob.address}, {Substrate: charlie.address}, 1n);297 await expect(transferTokenFromTx()).to.be.rejected;298 });299300 itSub.ifWithPallets('ReFungible', [Pallets.ReFungible], async ({helper}) => {301 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});302 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: alice.address, pieces: 100n});303 await helper.rft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address});304 const amountBefore = await helper.rft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});305 expect(amountBefore).to.be.equal(BigInt(1));306307 await helper.rft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address}, 0n);308 const amountAfter = await helper.rft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});309 expect(amountAfter).to.be.equal(BigInt(0));310311 const transferTokenFromTx = async () => helper.rft.transferTokenFrom(bob, collectionId, tokenId, {Substrate: bob.address}, {Substrate: charlie.address}, 100n);312 await expect(transferTokenFromTx()).to.be.rejected;313 });314});315316describe('User cannot approve for the amount greater than they own:', () => {317 let alice: IKeyringPair;318 let bob: IKeyringPair;319 let charlie: IKeyringPair;320321 before(async () => {322 await usingPlaygrounds(async (helper, privateKey) => {323 const donor = privateKey('//Alice');324 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);325 });326 });327328 itSub('1 for NFT', async ({helper}) => {329 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});330 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: bob.address});331 const approveTx = async () => helper.signTransaction(bob, helper.api?.tx.unique.approve({Substrate: charlie.address}, collectionId, tokenId, 2));332 await expect(approveTx()).to.be.rejected;333 expect(await helper.nft.isTokenApproved(collectionId, tokenId, {Substrate: charlie.address})).to.be.false;334 });335336 itSub('Fungible', async ({helper}) => {337 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);338 await helper.ft.mintTokens(alice, collectionId, 10n, alice.address);339 const tokenId = await helper.ft.getLastTokenId(collectionId);340 const approveTx = async () => helper.ft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address}, 11n);341 await expect(approveTx()).to.be.rejected;342 });343344 itSub.ifWithPallets('ReFungible', [Pallets.ReFungible], async ({helper}) => {345 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});346 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: alice.address, pieces: 100n});347 const approveTx = async () => helper.rft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address}, 101n);348 await expect(approveTx()).to.be.rejected;349 });350});351352describe('Administrator and collection owner do not need approval in order to execute TransferFrom (with owner_can_transfer_flag = true):', () => {353 let alice: IKeyringPair;354 let bob: IKeyringPair;355 let charlie: IKeyringPair;356 let dave: IKeyringPair;357358 before(async () => {359 await usingPlaygrounds(async (helper, privateKey) => {360 const donor = privateKey('//Alice');361 [alice, bob, charlie, dave] = await helper.arrange.createAccounts([100n, 100n, 100n, 100n], donor);362 });363 });364365 itSub('NFT', async ({helper}) => {366 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});367 await helper.collection.setLimits(alice, collectionId, {ownerCanTransfer: true});368 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: charlie.address});369370 await helper.nft.transferTokenFrom(alice, collectionId, tokenId, {Substrate: charlie.address}, {Substrate: dave.address});371 const owner1 = await helper.nft.getTokenOwner(collectionId, tokenId);372 expect(owner1.Substrate).to.be.equal(dave.address);373374 await helper.collection.addAdmin(alice, collectionId, {Substrate: bob.address});375 await helper.nft.transferTokenFrom(bob, collectionId, tokenId, {Substrate: dave.address}, {Substrate: alice.address});376 const owner2 = await helper.nft.getTokenOwner(collectionId, tokenId);377 expect(owner2.Substrate).to.be.equal(alice.address);378 });379380 itSub('Fungible up to an approved amount', async ({helper}) => {381 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);382 await helper.collection.setLimits(alice, collectionId, {ownerCanTransfer: true});383 await helper.ft.mintTokens(alice, collectionId, 10n, charlie.address);384 const tokenId = await helper.ft.getLastTokenId(collectionId);385386 const daveBalanceBefore = await helper.ft.getBalance(collectionId, {Substrate: dave.address});387 await helper.ft.transferTokenFrom(alice, collectionId, tokenId, {Substrate: charlie.address}, {Substrate: dave.address}, 1n);388 const daveBalanceAfter = await helper.ft.getBalance(collectionId, {Substrate: dave.address});389 expect(daveBalanceAfter - daveBalanceBefore).to.be.equal(BigInt(1));390391 await helper.collection.addAdmin(alice ,collectionId, {Substrate: bob.address});392393 const aliceBalanceBefore = await helper.ft.getBalance(collectionId, {Substrate: alice.address});394 await helper.ft.transferTokenFrom(bob, collectionId, tokenId, {Substrate: dave.address}, {Substrate: alice.address}, 1n);395 const aliceBalanceAfter = await helper.ft.getBalance(collectionId, {Substrate: alice.address});396 expect(aliceBalanceAfter - aliceBalanceBefore).to.be.equal(BigInt(1));397 });398399 itSub.ifWithPallets('ReFungible up to an approved amount', [Pallets.ReFungible], async ({helper}) => {400 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});401 await helper.collection.setLimits(alice, collectionId, {ownerCanTransfer: true});402 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: charlie.address, pieces: 100n});403404 const daveBefore = await helper.rft.getTokenBalance(collectionId, tokenId, {Substrate: dave.address});405 await helper.rft.transferTokenFrom(alice, collectionId, tokenId, {Substrate: charlie.address}, {Substrate: dave.address}, 1n);406 const daveAfter = await helper.rft.getTokenBalance(collectionId, tokenId, {Substrate: dave.address});407 expect(daveAfter - daveBefore).to.be.equal(BigInt(1));408409 await helper.collection.addAdmin(alice, collectionId, {Substrate: bob.address});410411 const aliceBefore = await helper.rft.getTokenBalance(collectionId, tokenId, {Substrate: alice.address});412 await helper.rft.transferTokenFrom(bob, collectionId, tokenId, {Substrate: dave.address}, {Substrate: alice.address}, 1n);413 const aliceAfter = await helper.rft.getTokenBalance(collectionId, tokenId, {Substrate: alice.address});414 expect(aliceAfter - aliceBefore).to.be.equal(BigInt(1));415 });416});417418describe('Repeated approvals add up', () => {419 let alice: IKeyringPair;420 let bob: IKeyringPair;421 let charlie: IKeyringPair;422 let dave: IKeyringPair;423424 before(async () => {425 await usingPlaygrounds(async (helper, privateKey) => {426 const donor = privateKey('//Alice');427 [alice, bob, charlie, dave] = await helper.arrange.createAccounts([100n, 100n, 100n, 100n], donor);428 });429 });430431 itSub.skip('Owned 10, approval 1: 1, approval 2: 1, resulting approved value: 2. Fungible', async ({helper}) => {432 const collection = await helper.ft.mintCollection(alice, {});433 await collection.mint(alice, 10n);434 await collection.approveTokens(alice, {Substrate: bob.address}, 1n);435 await collection.approveTokens(alice, {Substrate: charlie.address}, 1n);436 // const allowances1 = await getAllowance(collectionId, 0, Alice.address, Bob.address);437 // const allowances2 = await getAllowance(collectionId, 0, Alice.address, Charlie.address);438 // expect(allowances1 + allowances2).to.be.eq(BigInt(2));439 });440441 itSub.skip('Owned 10, approval 1: 1, approval 2: 1, resulting approved value: 2. ReFungible', async ({helper}) => {442 const collection = await helper.rft.mintCollection(alice, {});443 const token = await collection.mintToken(alice, 10n);444 await token.approve(alice, {Substrate: bob.address}, 1n);445 await token.approve(alice, {Substrate: charlie.address}, 1n);446 // const allowances1 = await getAllowance(collectionId, itemId, Alice.address, Bob.address);447 // const allowances2 = await getAllowance(collectionId, itemId, Alice.address, Charlie.address);448 // expect(allowances1 + allowances2).to.be.eq(BigInt(2));449 });450451 // Canceled by changing approve logic452 itSub.skip('Cannot approve for more than total user\'s amount (owned: 10, approval 1: 5 - should succeed, approval 2: 6 - should fail). Fungible', async ({helper}) => {453 const collection = await helper.ft.mintCollection(alice, {});454 await collection.mint(alice, 10n, {Substrate: dave.address});455 await collection.approveTokens(dave, {Substrate: bob.address}, 5n);456 await expect(collection.approveTokens(dave, {Substrate: charlie.address}, 6n))457 .to.be.rejectedWith('this test would fail (since it is skipped), replace this expecting message with what would have been received');458 });459460 // Canceled by changing approve logic461 itSub.skip('Cannot approve for more than total user\'s amount (owned: 100, approval 1: 50 - should succeed, approval 2: 51 - should fail). ReFungible', async ({helper}) => {462 const collection = await helper.rft.mintCollection(alice, {});463 const token = await collection.mintToken(alice, 100n, {Substrate: dave.address});464 await token.approve(dave, {Substrate: bob.address}, 50n);465 await expect(token.approve(dave, {Substrate: charlie.address}, 51n))466 .to.be.rejectedWith('this test would fail (since it is skipped), replace this expecting message with what would have been received');467 });468});469470describe('Integration Test approve(spender, collection_id, item_id, amount) with collection admin permissions:', () => {471 let alice: IKeyringPair;472 let bob: IKeyringPair;473 let charlie: IKeyringPair;474475 before(async () => {476 await usingPlaygrounds(async (helper, privateKey) => {477 const donor = privateKey('//Alice');478 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);479 });480 });481482 itSub('can be called by collection admin on non-owned item', async ({helper}) => {483 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});484 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: alice.address});485 await helper.collection.addAdmin(alice, collectionId, {Substrate: bob.address});486 const approveTx = async () => helper.nft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address});487 await expect(approveTx()).to.be.rejected;488 });489});490491describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {492 let alice: IKeyringPair;493 let bob: IKeyringPair;494 let charlie: IKeyringPair;495496 before(async () => {497 await usingPlaygrounds(async (helper, privateKey) => {498 const donor = privateKey('//Alice');499 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);500 });501 });502503 itSub('[nft] Approve for a collection that does not exist', async ({helper}) => {504 const collectionId = 1 << 32 - 1;505 const approveTx = async () => helper.nft.approveToken(bob, collectionId, 1, {Substrate: charlie.address});506 await expect(approveTx()).to.be.rejected;507 });508509 itSub('[fungible] Approve for a collection that does not exist', async ({helper}) => {510 const collectionId = 1 << 32 - 1;511 const approveTx = async () => helper.ft.approveToken(bob, collectionId, 1, {Substrate: charlie.address});512 await expect(approveTx()).to.be.rejected;513 });514515 itSub.ifWithPallets('[refungible] Approve for a collection that does not exist', [Pallets.ReFungible], async ({helper}) => {516 const collectionId = 1 << 32 - 1;517 const approveTx = async () => helper.rft.approveToken(bob, collectionId, 1, {Substrate: charlie.address});518 await expect(approveTx()).to.be.rejected;519 });520521 itSub('[nft] Approve for a collection that was destroyed', async ({helper}) => {522 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});523 await helper.nft.burn(alice, collectionId);524 const approveTx = async () => helper.nft.approveToken(alice, collectionId, 1, {Substrate: bob.address});525 await expect(approveTx()).to.be.rejected;526 });527528 itSub('[fungible] Approve for a collection that was destroyed', async ({helper}) => {529 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});530 await helper.ft.burn(alice, collectionId);531 const approveTx = async () => helper.ft.approveToken(alice, collectionId, 1, {Substrate: bob.address});532 await expect(approveTx()).to.be.rejected;533 });534535 itSub.ifWithPallets('[refungible] Approve for a collection that was destroyed', [Pallets.ReFungible], async ({helper}) => {536 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});537 await helper.rft.burn(alice, collectionId);538 const approveTx = async () => helper.rft.approveToken(alice, collectionId, 1, {Substrate: bob.address});539 await expect(approveTx()).to.be.rejected;540 });541 542 itSub('[nft] Approve transfer of a token that does not exist', async ({helper}) => {543 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});544 const approveTx = async () => helper.nft.approveToken(alice, collectionId, 2, {Substrate: bob.address});545 await expect(approveTx()).to.be.rejected;546 });547548 itSub.ifWithPallets('[refungible] Approve transfer of a token that does not exist', [Pallets.ReFungible], async ({helper}) => {549 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});550 const approveTx = async () => helper.rft.approveToken(alice, collectionId, 2, {Substrate: bob.address});551 await expect(approveTx()).to.be.rejected;552 });553554 itSub('[nft] Approve using the address that does not own the approved token', async ({helper}) => {555 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});556 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: alice.address});557 const approveTx = async () => helper.nft.approveToken(bob, collectionId, tokenId, {Substrate: alice.address});558 await expect(approveTx()).to.be.rejected;559 });560561 itSub('[fungible] Approve using the address that does not own the approved token', async ({helper}) => {562 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});563 await helper.ft.mintTokens(alice, collectionId, 10n, alice.address);564 const tokenId = await helper.ft.getLastTokenId(collectionId);565 const approveTx = async () => helper.ft.approveToken(bob, collectionId, tokenId, {Substrate: alice.address});566 await expect(approveTx()).to.be.rejected;567 });568569 itSub.ifWithPallets('[refungible] Approve using the address that does not own the approved token', [Pallets.ReFungible], async ({helper}) => {570 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});571 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: alice.address, pieces: 100n});572 const approveTx = async () => helper.rft.approveToken(bob, collectionId, tokenId, {Substrate: alice.address});573 await expect(approveTx()).to.be.rejected;574 });575576 itSub.ifWithPallets('should fail if approved more ReFungibles than owned', [Pallets.ReFungible], async ({helper}) => {577 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});578 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: alice.address, pieces: 100n});579 await helper.rft.transferToken(alice, collectionId, tokenId, {Substrate: bob.address}, 100n);580 await helper.rft.approveToken(bob, collectionId, tokenId, {Substrate: alice.address}, 100n);581582 const approveTx = async () => helper.rft.approveToken(bob, collectionId, tokenId, {Substrate: alice.address}, 101n);583 await expect(approveTx()).to.be.rejected;584 });585586 itSub('should fail if approved more Fungibles than owned', async ({helper}) => {587 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});588 await helper.ft.mintTokens(alice, collectionId, 10n, alice.address);589 const tokenId = await helper.ft.getLastTokenId(collectionId);590591 await helper.ft.transferToken(alice, collectionId, tokenId, {Substrate: bob.address}, 10n);592 await helper.ft.approveToken(bob, collectionId, tokenId, {Substrate: alice.address}, 10n);593 const approveTx = async () => helper.ft.approveToken(bob, collectionId, tokenId, {Substrate: alice.address}, 11n);594 await expect(approveTx()).to.be.rejected;595 });596597 itSub('fails when called by collection owner on non-owned item when OwnerCanTransfer == false', async ({helper}) => {598 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});599 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: bob.address});600 await helper.collection.setLimits(alice, collectionId, {ownerCanTransfer: false});601602 const approveTx = async () => helper.nft.approveToken(alice, collectionId, tokenId, {Substrate: charlie.address});603 await expect(approveTx()).to.be.rejected;604 });605});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 {expect, itSub, Pallets, usingPlaygrounds} from './util/playgrounds';192021describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {22 let alice: IKeyringPair;23 let bob: IKeyringPair;24 let charlie: IKeyringPair;2526 before(async () => {27 await usingPlaygrounds(async (helper, privateKey) => {28 const donor = privateKey('//Alice');29 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);30 });31 });3233 itSub('[nft] Execute the extrinsic and check approvedList', async ({helper}) => {34 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});35 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: alice.address});36 await helper.nft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address});37 expect(await helper.nft.isTokenApproved(collectionId, tokenId, {Substrate: bob.address})).to.be.true;38 });3940 itSub('[fungible] Execute the extrinsic and check approvedList', async ({helper}) => {41 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);42 await helper.ft.mintTokens(alice, collectionId, 10n, {Substrate: alice.address});43 const tokenId = await helper.ft.getLastTokenId(collectionId);44 await helper.ft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address});45 const amount = await helper.ft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});46 expect(amount).to.be.equal(BigInt(1));47 });4849 itSub.ifWithPallets('[refungible] Execute the extrinsic and check approvedList', [Pallets.ReFungible], async ({helper}) => {50 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});51 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: alice.address, pieces: 100n});52 await helper.rft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address});53 const amount = await helper.rft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});54 expect(amount).to.be.equal(BigInt(1));55 });5657 itSub('[nft] Remove approval by using 0 amount', async ({helper}) => {58 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});59 const collectionId = collection.collectionId;60 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: alice.address});61 await helper.nft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address});62 expect(await helper.nft.isTokenApproved(collectionId, tokenId, {Substrate: bob.address})).to.be.true;63 await helper.signTransaction(alice, helper.constructApiCall('api.tx.unique.approve', [{Substrate: bob.address}, collectionId, tokenId, 0]));64 expect(await helper.nft.isTokenApproved(collectionId, tokenId, {Substrate: bob.address})).to.be.false;65 });6667 itSub('[fungible] Remove approval by using 0 amount', async ({helper}) => {68 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);69 await helper.ft.mintTokens(alice, collectionId, 10n, {Substrate: alice.address});70 const tokenId = await helper.ft.getLastTokenId(collectionId);71 await helper.ft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address});72 const amountBefore = await helper.ft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});73 expect(amountBefore).to.be.equal(BigInt(1));7475 await helper.ft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address}, 0n);76 const amountAfter = await helper.ft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});77 expect(amountAfter).to.be.equal(BigInt(0));78 });7980 itSub.ifWithPallets('[refungible] Remove approval by using 0 amount', [Pallets.ReFungible], async ({helper}) => {81 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});82 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: alice.address, pieces: 100n});83 await helper.rft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address});84 const amountBefore = await helper.rft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});85 expect(amountBefore).to.be.equal(BigInt(1));8687 await helper.rft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address}, 0n);88 const amountAfter = await helper.rft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});89 expect(amountAfter).to.be.equal(BigInt(0));90 });9192 itSub('can`t be called by collection owner on non-owned item when OwnerCanTransfer == false', async ({helper}) => {93 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});94 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: bob.address});95 const approveTokenTx = async () => helper.nft.approveToken(alice, collectionId, tokenId, {Substrate: charlie.address});96 await expect(approveTokenTx()).to.be.rejected;97 });98});99100describe('Normal user can approve other users to transfer:', () => {101 let alice: IKeyringPair;102 let bob: IKeyringPair;103 let charlie: IKeyringPair;104105 before(async () => {106 await usingPlaygrounds(async (helper, privateKey) => {107 const donor = privateKey('//Alice');108 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);109 });110 });111112 itSub('NFT', async ({helper}) => {113 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});114 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: bob.address});115 await helper.nft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address});116 expect(await helper.nft.isTokenApproved(collectionId, tokenId, {Substrate: charlie.address})).to.be.true;117 });118119 itSub('Fungible up to an approved amount', async ({helper}) => {120 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);121 await helper.ft.mintTokens(alice, collectionId, 10n, bob.address);122 const tokenId = await helper.ft.getLastTokenId(collectionId);123 await helper.ft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address});124 const amount = await helper.ft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: charlie.address}, {Substrate: bob.address});125 expect(amount).to.be.equal(BigInt(1));126 });127128 itSub.ifWithPallets('ReFungible up to an approved amount', [Pallets.ReFungible], async ({helper}) => {129 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});130 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: bob.address, pieces: 100n});131 await helper.rft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address}, 100n);132 const amount = await helper.rft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: charlie.address}, {Substrate: bob.address});133 expect(amount).to.be.equal(BigInt(100n));134 });135});136137describe('Approved users can transferFrom up to approved amount:', () => {138 let alice: IKeyringPair;139 let bob: IKeyringPair;140 let charlie: IKeyringPair;141142 before(async () => {143 await usingPlaygrounds(async (helper, privateKey) => {144 const donor = privateKey('//Alice');145 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);146 });147 });148149 itSub('NFT', async ({helper}) => {150 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});151 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: bob.address});152 await helper.nft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address});153 await helper.nft.transferTokenFrom(charlie, collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});154 const owner = await helper.nft.getTokenOwner(collectionId, tokenId);155 expect(owner.Substrate).to.be.equal(alice.address);156 });157158 itSub('Fungible up to an approved amount', async ({helper}) => {159 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);160 await helper.ft.mintTokens(alice, collectionId, 10n, bob.address);161 const tokenId = await helper.ft.getLastTokenId(collectionId);162 await helper.ft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address});163 const before = await helper.ft.getBalance(collectionId, {Substrate: alice.address});164 await helper.ft.transferTokenFrom(charlie, collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address}, 1n);165 const after = await helper.ft.getBalance(collectionId, {Substrate: alice.address});166 expect(after - before).to.be.equal(BigInt(1));167 });168169 itSub.ifWithPallets('ReFungible up to an approved amount', [Pallets.ReFungible], async ({helper}) => {170 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});171 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: bob.address, pieces: 100n});172 await helper.rft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address});173 const before = await helper.rft.getTokenBalance(collectionId, tokenId, {Substrate: alice.address});174 await helper.rft.transferTokenFrom(charlie, collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address}, 1n);175 const after = await helper.rft.getTokenBalance(collectionId, tokenId, {Substrate: alice.address});176 expect(after - before).to.be.equal(BigInt(1));177 });178});179180describe('Approved users cannot use transferFrom to repeat transfers if approved amount was already transferred:', () => {181 let alice: IKeyringPair;182 let bob: IKeyringPair;183 let charlie: IKeyringPair;184185 before(async () => {186 await usingPlaygrounds(async (helper, privateKey) => {187 const donor = privateKey('//Alice');188 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);189 });190 });191192 itSub('NFT', async ({helper}) => {193 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});194 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: bob.address});195 await helper.nft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address});196 await helper.nft.transferTokenFrom(charlie, collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});197 const owner = await helper.nft.getTokenOwner(collectionId, tokenId);198 expect(owner.Substrate).to.be.equal(alice.address);199 const transferTokenFromTx = async () => helper.nft.transferTokenFrom(charlie, collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});200 await expect(transferTokenFromTx()).to.be.rejected;201 });202203 itSub('Fungible up to an approved amount', async ({helper}) => {204 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);205 await helper.ft.mintTokens(alice, collectionId, 10n, bob.address);206 const tokenId = await helper.ft.getLastTokenId(collectionId);207 await helper.ft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address});208 const before = await helper.ft.getBalance(collectionId, {Substrate: alice.address});209 await helper.ft.transferTokenFrom(charlie, collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address}, 1n);210 const after = await helper.ft.getBalance(collectionId, {Substrate: alice.address});211 expect(after - before).to.be.equal(BigInt(1));212213 const transferTokenFromTx = async () => helper.ft.transferTokenFrom(charlie, collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address}, 1n);214 await expect(transferTokenFromTx()).to.be.rejected;215 });216217 itSub.ifWithPallets('ReFungible up to an approved amount', [Pallets.ReFungible], async ({helper}) => {218 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});219 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: bob.address, pieces: 100n});220 await helper.rft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address}, 100n);221 const before = await helper.rft.getTokenBalance(collectionId, tokenId, {Substrate: alice.address});222 await helper.rft.transferTokenFrom(charlie, collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address}, 100n);223 const after = await helper.rft.getTokenBalance(collectionId, tokenId, {Substrate: alice.address});224 expect(after - before).to.be.equal(BigInt(100));225 const transferTokenFromTx = async () => helper.rft.transferTokenFrom(charlie, collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address}, 100n);226 await expect(transferTokenFromTx()).to.be.rejected;227 });228});229230describe('Approved amount decreases by the transferred amount.:', () => {231 let alice: IKeyringPair;232 let bob: IKeyringPair;233 let charlie: IKeyringPair;234 let dave: IKeyringPair;235236 before(async () => {237 await usingPlaygrounds(async (helper, privateKey) => {238 const donor = privateKey('//Alice');239 [alice, bob, charlie, dave] = await helper.arrange.createAccounts([100n, 100n, 100n, 100n], donor);240 });241 });242243 itSub('If a user B is approved to transfer 10 Fungible tokens from user A, they can transfer 2 tokens to user C, which will result in decreasing approval from 10 to 8. Then user B can transfer 8 tokens to user D.', async ({helper}) => {244 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);245 await helper.ft.mintTokens(alice, collectionId, 10n, alice.address);246 const tokenId = await helper.ft.getLastTokenId(collectionId);247 await helper.ft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address}, 10n);248249 const charlieBefore = await helper.ft.getBalance(collectionId, {Substrate: charlie.address});250 await helper.ft.transferTokenFrom(bob, collectionId, tokenId, {Substrate: alice.address}, {Substrate: charlie.address}, 2n);251 const charlieAfter = await helper.ft.getBalance(collectionId, {Substrate: charlie.address});252 expect(charlieAfter - charlieBefore).to.be.equal(BigInt(2));253254 const daveBefore = await helper.ft.getBalance(collectionId, {Substrate: dave.address});255 await helper.ft.transferTokenFrom(bob, collectionId, tokenId, {Substrate: alice.address}, {Substrate: dave.address}, 8n);256 const daveAfter = await helper.ft.getBalance(collectionId, {Substrate: dave.address});257 expect(daveAfter - daveBefore).to.be.equal(BigInt(8));258 });259});260261describe('User may clear the approvals to approving for 0 amount:', () => {262 let alice: IKeyringPair;263 let bob: IKeyringPair;264 let charlie: IKeyringPair;265266 before(async () => {267 await usingPlaygrounds(async (helper, privateKey) => {268 const donor = privateKey('//Alice');269 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);270 });271 });272273 itSub('NFT', async ({helper}) => {274 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});275 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: alice.address});276 await helper.nft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address});277 expect(await helper.nft.isTokenApproved(collectionId, tokenId, {Substrate: bob.address})).to.be.true;278 await helper.signTransaction(alice, helper.constructApiCall('api.tx.unique.approve', [{Substrate: bob.address}, collectionId, tokenId, 0]));279 expect(await helper.nft.isTokenApproved(collectionId, tokenId, {Substrate: bob.address})).to.be.false;280 const transferTokenFromTx = async () => helper.nft.transferTokenFrom(bob, collectionId, tokenId, {Substrate: bob.address}, {Substrate: bob.address});281 await expect(transferTokenFromTx()).to.be.rejected;282 });283284 itSub('Fungible', async ({helper}) => {285 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);286 await helper.ft.mintTokens(alice, collectionId, 10n, alice.address);287 const tokenId = await helper.ft.getLastTokenId(collectionId);288 await helper.ft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address});289 const amountBefore = await helper.ft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});290 expect(amountBefore).to.be.equal(BigInt(1));291292 await helper.ft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address}, 0n);293 const amountAfter = await helper.ft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});294 expect(amountAfter).to.be.equal(BigInt(0));295296 const transferTokenFromTx = async () => helper.ft.transferTokenFrom(bob, collectionId, tokenId, {Substrate: bob.address}, {Substrate: charlie.address}, 1n);297 await expect(transferTokenFromTx()).to.be.rejected;298 });299300 itSub.ifWithPallets('ReFungible', [Pallets.ReFungible], async ({helper}) => {301 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});302 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: alice.address, pieces: 100n});303 await helper.rft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address});304 const amountBefore = await helper.rft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});305 expect(amountBefore).to.be.equal(BigInt(1));306307 await helper.rft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address}, 0n);308 const amountAfter = await helper.rft.getTokenApprovedPieces(collectionId, tokenId, {Substrate: bob.address}, {Substrate: alice.address});309 expect(amountAfter).to.be.equal(BigInt(0));310311 const transferTokenFromTx = async () => helper.rft.transferTokenFrom(bob, collectionId, tokenId, {Substrate: bob.address}, {Substrate: charlie.address}, 100n);312 await expect(transferTokenFromTx()).to.be.rejected;313 });314});315316describe('User cannot approve for the amount greater than they own:', () => {317 let alice: IKeyringPair;318 let bob: IKeyringPair;319 let charlie: IKeyringPair;320321 before(async () => {322 await usingPlaygrounds(async (helper, privateKey) => {323 const donor = privateKey('//Alice');324 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);325 });326 });327328 itSub('1 for NFT', async ({helper}) => {329 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});330 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: bob.address});331 const approveTx = async () => helper.signTransaction(bob, helper.constructApiCall('api.tx.unique.approve', [{Substrate: charlie.address}, collectionId, tokenId, 2]));332 await expect(approveTx()).to.be.rejected;333 expect(await helper.nft.isTokenApproved(collectionId, tokenId, {Substrate: charlie.address})).to.be.false;334 });335336 itSub('Fungible', async ({helper}) => {337 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);338 await helper.ft.mintTokens(alice, collectionId, 10n, alice.address);339 const tokenId = await helper.ft.getLastTokenId(collectionId);340 const approveTx = async () => helper.ft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address}, 11n);341 await expect(approveTx()).to.be.rejected;342 });343344 itSub.ifWithPallets('ReFungible', [Pallets.ReFungible], async ({helper}) => {345 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});346 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: alice.address, pieces: 100n});347 const approveTx = async () => helper.rft.approveToken(alice, collectionId, tokenId, {Substrate: bob.address}, 101n);348 await expect(approveTx()).to.be.rejected;349 });350});351352describe('Administrator and collection owner do not need approval in order to execute TransferFrom (with owner_can_transfer_flag = true):', () => {353 let alice: IKeyringPair;354 let bob: IKeyringPair;355 let charlie: IKeyringPair;356 let dave: IKeyringPair;357358 before(async () => {359 await usingPlaygrounds(async (helper, privateKey) => {360 const donor = privateKey('//Alice');361 [alice, bob, charlie, dave] = await helper.arrange.createAccounts([100n, 100n, 100n, 100n], donor);362 });363 });364365 itSub('NFT', async ({helper}) => {366 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});367 await helper.collection.setLimits(alice, collectionId, {ownerCanTransfer: true});368 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: charlie.address});369370 await helper.nft.transferTokenFrom(alice, collectionId, tokenId, {Substrate: charlie.address}, {Substrate: dave.address});371 const owner1 = await helper.nft.getTokenOwner(collectionId, tokenId);372 expect(owner1.Substrate).to.be.equal(dave.address);373374 await helper.collection.addAdmin(alice, collectionId, {Substrate: bob.address});375 await helper.nft.transferTokenFrom(bob, collectionId, tokenId, {Substrate: dave.address}, {Substrate: alice.address});376 const owner2 = await helper.nft.getTokenOwner(collectionId, tokenId);377 expect(owner2.Substrate).to.be.equal(alice.address);378 });379380 itSub('Fungible up to an approved amount', async ({helper}) => {381 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);382 await helper.collection.setLimits(alice, collectionId, {ownerCanTransfer: true});383 await helper.ft.mintTokens(alice, collectionId, 10n, charlie.address);384 const tokenId = await helper.ft.getLastTokenId(collectionId);385386 const daveBalanceBefore = await helper.ft.getBalance(collectionId, {Substrate: dave.address});387 await helper.ft.transferTokenFrom(alice, collectionId, tokenId, {Substrate: charlie.address}, {Substrate: dave.address}, 1n);388 const daveBalanceAfter = await helper.ft.getBalance(collectionId, {Substrate: dave.address});389 expect(daveBalanceAfter - daveBalanceBefore).to.be.equal(BigInt(1));390391 await helper.collection.addAdmin(alice ,collectionId, {Substrate: bob.address});392393 const aliceBalanceBefore = await helper.ft.getBalance(collectionId, {Substrate: alice.address});394 await helper.ft.transferTokenFrom(bob, collectionId, tokenId, {Substrate: dave.address}, {Substrate: alice.address}, 1n);395 const aliceBalanceAfter = await helper.ft.getBalance(collectionId, {Substrate: alice.address});396 expect(aliceBalanceAfter - aliceBalanceBefore).to.be.equal(BigInt(1));397 });398399 itSub.ifWithPallets('ReFungible up to an approved amount', [Pallets.ReFungible], async ({helper}) => {400 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});401 await helper.collection.setLimits(alice, collectionId, {ownerCanTransfer: true});402 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: charlie.address, pieces: 100n});403404 const daveBefore = await helper.rft.getTokenBalance(collectionId, tokenId, {Substrate: dave.address});405 await helper.rft.transferTokenFrom(alice, collectionId, tokenId, {Substrate: charlie.address}, {Substrate: dave.address}, 1n);406 const daveAfter = await helper.rft.getTokenBalance(collectionId, tokenId, {Substrate: dave.address});407 expect(daveAfter - daveBefore).to.be.equal(BigInt(1));408409 await helper.collection.addAdmin(alice, collectionId, {Substrate: bob.address});410411 const aliceBefore = await helper.rft.getTokenBalance(collectionId, tokenId, {Substrate: alice.address});412 await helper.rft.transferTokenFrom(bob, collectionId, tokenId, {Substrate: dave.address}, {Substrate: alice.address}, 1n);413 const aliceAfter = await helper.rft.getTokenBalance(collectionId, tokenId, {Substrate: alice.address});414 expect(aliceAfter - aliceBefore).to.be.equal(BigInt(1));415 });416});417418describe('Repeated approvals add up', () => {419 let alice: IKeyringPair;420 let bob: IKeyringPair;421 let charlie: IKeyringPair;422 let dave: IKeyringPair;423424 before(async () => {425 await usingPlaygrounds(async (helper, privateKey) => {426 const donor = privateKey('//Alice');427 [alice, bob, charlie, dave] = await helper.arrange.createAccounts([100n, 100n, 100n, 100n], donor);428 });429 });430431 itSub.skip('Owned 10, approval 1: 1, approval 2: 1, resulting approved value: 2. Fungible', async ({helper}) => {432 const collection = await helper.ft.mintCollection(alice, {});433 await collection.mint(alice, 10n);434 await collection.approveTokens(alice, {Substrate: bob.address}, 1n);435 await collection.approveTokens(alice, {Substrate: charlie.address}, 1n);436 // const allowances1 = await getAllowance(collectionId, 0, Alice.address, Bob.address);437 // const allowances2 = await getAllowance(collectionId, 0, Alice.address, Charlie.address);438 // expect(allowances1 + allowances2).to.be.eq(BigInt(2));439 });440441 itSub.skip('Owned 10, approval 1: 1, approval 2: 1, resulting approved value: 2. ReFungible', async ({helper}) => {442 const collection = await helper.rft.mintCollection(alice, {});443 const token = await collection.mintToken(alice, 10n);444 await token.approve(alice, {Substrate: bob.address}, 1n);445 await token.approve(alice, {Substrate: charlie.address}, 1n);446 // const allowances1 = await getAllowance(collectionId, itemId, Alice.address, Bob.address);447 // const allowances2 = await getAllowance(collectionId, itemId, Alice.address, Charlie.address);448 // expect(allowances1 + allowances2).to.be.eq(BigInt(2));449 });450451 // Canceled by changing approve logic452 itSub.skip('Cannot approve for more than total user\'s amount (owned: 10, approval 1: 5 - should succeed, approval 2: 6 - should fail). Fungible', async ({helper}) => {453 const collection = await helper.ft.mintCollection(alice, {});454 await collection.mint(alice, 10n, {Substrate: dave.address});455 await collection.approveTokens(dave, {Substrate: bob.address}, 5n);456 await expect(collection.approveTokens(dave, {Substrate: charlie.address}, 6n))457 .to.be.rejectedWith('this test would fail (since it is skipped), replace this expecting message with what would have been received');458 });459460 // Canceled by changing approve logic461 itSub.skip('Cannot approve for more than total user\'s amount (owned: 100, approval 1: 50 - should succeed, approval 2: 51 - should fail). ReFungible', async ({helper}) => {462 const collection = await helper.rft.mintCollection(alice, {});463 const token = await collection.mintToken(alice, 100n, {Substrate: dave.address});464 await token.approve(dave, {Substrate: bob.address}, 50n);465 await expect(token.approve(dave, {Substrate: charlie.address}, 51n))466 .to.be.rejectedWith('this test would fail (since it is skipped), replace this expecting message with what would have been received');467 });468});469470describe('Integration Test approve(spender, collection_id, item_id, amount) with collection admin permissions:', () => {471 let alice: IKeyringPair;472 let bob: IKeyringPair;473 let charlie: IKeyringPair;474475 before(async () => {476 await usingPlaygrounds(async (helper, privateKey) => {477 const donor = privateKey('//Alice');478 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);479 });480 });481482 itSub('can be called by collection admin on non-owned item', async ({helper}) => {483 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});484 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: alice.address});485 await helper.collection.addAdmin(alice, collectionId, {Substrate: bob.address});486 const approveTx = async () => helper.nft.approveToken(bob, collectionId, tokenId, {Substrate: charlie.address});487 await expect(approveTx()).to.be.rejected;488 });489});490491describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {492 let alice: IKeyringPair;493 let bob: IKeyringPair;494 let charlie: IKeyringPair;495496 before(async () => {497 await usingPlaygrounds(async (helper, privateKey) => {498 const donor = privateKey('//Alice');499 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);500 });501 });502503 itSub('[nft] Approve for a collection that does not exist', async ({helper}) => {504 const collectionId = 1 << 32 - 1;505 const approveTx = async () => helper.nft.approveToken(bob, collectionId, 1, {Substrate: charlie.address});506 await expect(approveTx()).to.be.rejected;507 });508509 itSub('[fungible] Approve for a collection that does not exist', async ({helper}) => {510 const collectionId = 1 << 32 - 1;511 const approveTx = async () => helper.ft.approveToken(bob, collectionId, 1, {Substrate: charlie.address});512 await expect(approveTx()).to.be.rejected;513 });514515 itSub.ifWithPallets('[refungible] Approve for a collection that does not exist', [Pallets.ReFungible], async ({helper}) => {516 const collectionId = 1 << 32 - 1;517 const approveTx = async () => helper.rft.approveToken(bob, collectionId, 1, {Substrate: charlie.address});518 await expect(approveTx()).to.be.rejected;519 });520521 itSub('[nft] Approve for a collection that was destroyed', async ({helper}) => {522 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});523 await helper.nft.burn(alice, collectionId);524 const approveTx = async () => helper.nft.approveToken(alice, collectionId, 1, {Substrate: bob.address});525 await expect(approveTx()).to.be.rejected;526 });527528 itSub('[fungible] Approve for a collection that was destroyed', async ({helper}) => {529 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});530 await helper.ft.burn(alice, collectionId);531 const approveTx = async () => helper.ft.approveToken(alice, collectionId, 1, {Substrate: bob.address});532 await expect(approveTx()).to.be.rejected;533 });534535 itSub.ifWithPallets('[refungible] Approve for a collection that was destroyed', [Pallets.ReFungible], async ({helper}) => {536 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});537 await helper.rft.burn(alice, collectionId);538 const approveTx = async () => helper.rft.approveToken(alice, collectionId, 1, {Substrate: bob.address});539 await expect(approveTx()).to.be.rejected;540 });541 542 itSub('[nft] Approve transfer of a token that does not exist', async ({helper}) => {543 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});544 const approveTx = async () => helper.nft.approveToken(alice, collectionId, 2, {Substrate: bob.address});545 await expect(approveTx()).to.be.rejected;546 });547548 itSub.ifWithPallets('[refungible] Approve transfer of a token that does not exist', [Pallets.ReFungible], async ({helper}) => {549 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});550 const approveTx = async () => helper.rft.approveToken(alice, collectionId, 2, {Substrate: bob.address});551 await expect(approveTx()).to.be.rejected;552 });553554 itSub('[nft] Approve using the address that does not own the approved token', async ({helper}) => {555 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});556 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: alice.address});557 const approveTx = async () => helper.nft.approveToken(bob, collectionId, tokenId, {Substrate: alice.address});558 await expect(approveTx()).to.be.rejected;559 });560561 itSub('[fungible] Approve using the address that does not own the approved token', async ({helper}) => {562 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});563 await helper.ft.mintTokens(alice, collectionId, 10n, alice.address);564 const tokenId = await helper.ft.getLastTokenId(collectionId);565 const approveTx = async () => helper.ft.approveToken(bob, collectionId, tokenId, {Substrate: alice.address});566 await expect(approveTx()).to.be.rejected;567 });568569 itSub.ifWithPallets('[refungible] Approve using the address that does not own the approved token', [Pallets.ReFungible], async ({helper}) => {570 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});571 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: alice.address, pieces: 100n});572 const approveTx = async () => helper.rft.approveToken(bob, collectionId, tokenId, {Substrate: alice.address});573 await expect(approveTx()).to.be.rejected;574 });575576 itSub.ifWithPallets('should fail if approved more ReFungibles than owned', [Pallets.ReFungible], async ({helper}) => {577 const {collectionId} = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});578 const {tokenId} = await helper.rft.mintToken(alice, {collectionId: collectionId, owner: alice.address, pieces: 100n});579 await helper.rft.transferToken(alice, collectionId, tokenId, {Substrate: bob.address}, 100n);580 await helper.rft.approveToken(bob, collectionId, tokenId, {Substrate: alice.address}, 100n);581582 const approveTx = async () => helper.rft.approveToken(bob, collectionId, tokenId, {Substrate: alice.address}, 101n);583 await expect(approveTx()).to.be.rejected;584 });585586 itSub('should fail if approved more Fungibles than owned', async ({helper}) => {587 const {collectionId} = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});588 await helper.ft.mintTokens(alice, collectionId, 10n, alice.address);589 const tokenId = await helper.ft.getLastTokenId(collectionId);590591 await helper.ft.transferToken(alice, collectionId, tokenId, {Substrate: bob.address}, 10n);592 await helper.ft.approveToken(bob, collectionId, tokenId, {Substrate: alice.address}, 10n);593 const approveTx = async () => helper.ft.approveToken(bob, collectionId, tokenId, {Substrate: alice.address}, 11n);594 await expect(approveTx()).to.be.rejected;595 });596597 itSub('fails when called by collection owner on non-owned item when OwnerCanTransfer == false', async ({helper}) => {598 const {collectionId} = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});599 const {tokenId} = await helper.nft.mintToken(alice, {collectionId: collectionId, owner: bob.address});600 await helper.collection.setLimits(alice, collectionId, {ownerCanTransfer: false});601602 const approveTx = async () => helper.nft.approveToken(alice, collectionId, tokenId, {Substrate: charlie.address});603 await expect(approveTx()).to.be.rejected;604 });605});tests/src/block-production.test.tsdiffbeforeafterboth--- a/tests/src/block-production.test.ts
+++ b/tests/src/block-production.test.ts
@@ -37,7 +37,7 @@
describe('Block Production smoke test', () => {
itSub('Node produces new blocks', async ({helper}) => {
- const blocks: number[] | undefined = await getBlocks(helper.api!);
+ const blocks: number[] | undefined = await getBlocks(helper.getApi());
expect(blocks[0]).to.be.lessThan(blocks[1]);
});
});
tests/src/createMultipleItemsEx.test.tsdiffbeforeafterboth--- a/tests/src/createMultipleItemsEx.test.ts
+++ b/tests/src/createMultipleItemsEx.test.ts
@@ -178,13 +178,12 @@
tokenPrefix: 'COL',
}, 0);
- const api = helper.api;
- await helper.signTransaction(alice, api?.tx.unique.createMultipleItemsEx(collection.collectionId, {
+ await helper.executeExtrinsic(alice, 'api.tx.unique.createMultipleItemsEx',[collection.collectionId, {
Fungible: new Map([
[JSON.stringify({Substrate: alice.address}), 50],
[JSON.stringify({Substrate: bob.address}), 100],
]),
- }));
+ }], true);
expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(50n);
expect(await collection.getBalance({Substrate: bob.address})).to.be.equal(100n);
@@ -200,8 +199,7 @@
],
});
- const api = helper.api;
- await helper.signTransaction(alice, api?.tx.unique.createMultipleItemsEx(collection.collectionId, {
+ await helper.executeExtrinsic(alice, 'api.tx.unique.createMultipleItemsEx', [collection.collectionId, {
RefungibleMultipleOwners: {
users: new Map([
[JSON.stringify({Substrate: alice.address}), 1],
@@ -211,7 +209,7 @@
{key: 'k', value: 'v'},
],
},
- }));
+ }], true);
const tokenId = await collection.getLastTokenId();
expect(tokenId).to.be.equal(1);
expect(await collection.getTokenBalance(1, {Substrate: alice.address})).to.be.equal(1n);
@@ -228,9 +226,7 @@
],
});
- const api = helper.api;
-
- await helper.signTransaction(alice, api?.tx.unique.createMultipleItemsEx(collection.collectionId, {
+ await helper.executeExtrinsic(alice, 'api.tx.unique.createMultipleItemsEx', [collection.collectionId, {
RefungibleMultipleItems: [
{
user: {Substrate: alice.address}, pieces: 1,
@@ -245,7 +241,7 @@
],
},
],
- }));
+ }], true);
expect(await collection.getLastTokenId()).to.be.equal(2);
expect(await collection.getTokenBalance(1, {Substrate: alice.address})).to.be.equal(1n);
tests/src/creditFeesToTreasury.test.tsdiffbeforeafterboth--- a/tests/src/creditFeesToTreasury.test.ts
+++ b/tests/src/creditFeesToTreasury.test.ts
@@ -70,7 +70,7 @@
});
itSub('Sender balance decreased by fee+sent amount, Treasury balance increased by fee', async ({helper}) => {
- await skipInflationBlock(helper.api!);
+ await skipInflationBlock(helper.getApi());
await helper.wait.newBlocks(1);
const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY);
@@ -89,7 +89,7 @@
});
itSub('Treasury balance increased by failed tx fee', async ({helper}) => {
- const api = helper.api!;
+ const api = helper.getApi();
await helper.wait.newBlocks(1);
const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY);
@@ -107,7 +107,7 @@
});
itSub('NFT Transactions also send fees to Treasury', async ({helper}) => {
- await skipInflationBlock(helper.api!);
+ await skipInflationBlock(helper.getApi());
await helper.wait.newBlocks(1);
const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY);
@@ -125,7 +125,7 @@
itSub('Fees are sane', async ({helper}) => {
const unique = helper.balance.getOneTokenNominal();
- await skipInflationBlock(helper.api!);
+ await skipInflationBlock(helper.getApi());
await helper.wait.newBlocks(1);
const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);
@@ -140,7 +140,7 @@
});
itSub('NFT Transfer fee is close to 0.1 Unique', async ({helper}) => {
- await skipInflationBlock(helper.api!);
+ await skipInflationBlock(helper.getApi());
await helper.wait.newBlocks(1);
const collection = await helper.nft.mintCollection(alice, {
tests/src/eth/createNFTCollection.test.tsdiffbeforeafterboth--- a/tests/src/eth/createNFTCollection.test.ts
+++ b/tests/src/eth/createNFTCollection.test.ts
@@ -152,7 +152,7 @@
before(async function() {
await usingEthPlaygrounds(async (helper, privateKey) => {
donor = privateKey('//Alice');
- nominal = helper.balance.getOneTokenNominal()
+ nominal = helper.balance.getOneTokenNominal();
});
});
tests/src/eth/proxy/nonFungibleProxy.test.tsdiffbeforeafterboth--- a/tests/src/eth/proxy/nonFungibleProxy.test.ts
+++ b/tests/src/eth/proxy/nonFungibleProxy.test.ts
@@ -138,23 +138,16 @@
});
//TODO: CORE-302 add eth methods
- itEth.skip('Can perform mintBulk()', async ({helper, privateKey}) => {
- const api = helper.getApi();
- const web3 = helper.getWeb3();
- const privateKeyWrapper = privateKey;
- /*
- const collection = await createCollectionExpectSuccess({
- mode: {type: 'NFT'},
- });
- const alice = privateKeyWrapper('//Alice');
+ itEth.skip('Can perform mintBulk()', async ({helper}) => {
+ const collection = await helper.nft.mintCollection(donor, {name: 'New', description: 'New collection', tokenPrefix: 'NEW'});
- const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
- const receiver = createEthAccount(web3);
+ const caller = await helper.eth.createAccountWithBalance(donor, 30n);
+ const receiver = helper.eth.createAccount();
- const address = collectionIdToAddress(collection);
- const contract = await proxyWrap(api, web3, new web3.eth.Contract(nonFungibleAbi as any, address, {from: caller, ...GAS_ARGS}), privateKeyWrapper);
- const changeAdminTx = api.tx.unique.addCollectionAdmin(collection, {Ethereum: contract.options.address});
- await submitTransactionAsync(alice, changeAdminTx);
+ const address = helper.ethAddress.fromCollectionId(collection.collectionId);
+ const evmCollection = helper.ethNativeContract.collection(address, 'nft', caller);
+ const contract = await proxyWrap(helper, evmCollection, donor);
+ await collection.addAdmin(donor, {Ethereum: contract.options.address});
{
const nextTokenId = await contract.methods.nextTokenId().call();
@@ -167,7 +160,7 @@
[+nextTokenId + 2, 'Test URI 2'],
],
).send({from: caller});
- const events = normalizeEvents(result.events);
+ const events = helper.eth.normalizeEvents(result.events);
expect(events).to.be.deep.equal([
{
@@ -203,7 +196,6 @@
expect(await contract.methods.tokenURI(+nextTokenId + 1).call()).to.be.equal('Test URI 1');
expect(await contract.methods.tokenURI(+nextTokenId + 2).call()).to.be.equal('Test URI 2');
}
- */
});
itEth('Can perform burn()', async ({helper}) => {
tests/src/eth/util/playgrounds/unique.dev.tsdiffbeforeafterboth--- a/tests/src/eth/util/playgrounds/unique.dev.ts
+++ b/tests/src/eth/util/playgrounds/unique.dev.ts
@@ -285,8 +285,8 @@
await code();
// In dev mode, the transaction might not finish processing in time
await this.helper.wait.newBlocks(1);
- }
- return await this.helper.arrange.calculcateFee(address, code);
+ };
+ return await this.helper.arrange.calculcateFee(address, wrappedCode);
}
}
tests/src/inflation.test.tsdiffbeforeafterboth--- a/tests/src/inflation.test.ts
+++ b/tests/src/inflation.test.ts
@@ -40,9 +40,9 @@
const tx = helper.constructApiCall('api.tx.inflation.startInflation', [1]);
await expect(helper.executeExtrinsic(superuser, 'api.tx.sudo.sudo', [tx])).to.not.be.rejected;
- const blockInterval = (helper.api!.consts.inflation.inflationBlockInterval as any).toBigInt();
- const totalIssuanceStart = ((await helper.api!.query.inflation.startingYearTotalIssuance()) as any).toBigInt();
- const blockInflation = (await helper.api!.query.inflation.blockInflation() as any).toBigInt();
+ const blockInterval = (helper.getApi().consts.inflation.inflationBlockInterval as any).toBigInt();
+ const totalIssuanceStart = ((await helper.callRpc('api.query.inflation.startingYearTotalIssuance', [])) as any).toBigInt();
+ const blockInflation = (await helper.callRpc('api.query.inflation.blockInflation', []) as any).toBigInt();
const YEAR = 5259600n; // 6-second block. Blocks in one year
// const YEAR = 2629800n; // 12-second block. Blocks in one year
tests/src/nesting/properties.test.tsdiffbeforeafterboth--- a/tests/src/nesting/properties.test.ts
+++ b/tests/src/nesting/properties.test.ts
@@ -276,7 +276,7 @@
itSub('Reads access rights to properties of a collection', async ({helper}) => {
const collection = await helper.nft.mintCollection(alice);
- const propertyRights = (await helper.api!.query.common.collectionPropertyPermissions(collection.collectionId)).toJSON();
+ const propertyRights = (await helper.callRpc('api.query.common.collectionPropertyPermissions', [collection.collectionId])).toJSON();
expect(propertyRights).to.be.empty;
});
@@ -817,7 +817,7 @@
).to.be.fulfilled;
}
- const originalSpace = await getConsumedSpace(token.collection.helper.api, token.collectionId, token.tokenId, pieces == 1n ? 'NFT' : 'RFT');
+ const originalSpace = await getConsumedSpace(token.collection.helper.getApi(), token.collectionId, token.tokenId, pieces == 1n ? 'NFT' : 'RFT');
return originalSpace;
}
@@ -840,7 +840,7 @@
).to.be.rejectedWith(/common\.NoPermission/);
}
- const consumedSpace = await getConsumedSpace(token.collection.helper.api, token.collectionId, token.tokenId, pieces == 1n ? 'NFT' : 'RFT');
+ const consumedSpace = await getConsumedSpace(token.collection.helper.getApi(), token.collectionId, token.tokenId, pieces == 1n ? 'NFT' : 'RFT');
expect(consumedSpace).to.be.equal(originalSpace);
}
@@ -875,7 +875,7 @@
).to.be.rejectedWith(/common\.NoPermission/);
}
- const consumedSpace = await getConsumedSpace(token.collection.helper.api, token.collectionId, token.tokenId, pieces == 1n ? 'NFT' : 'RFT');
+ const consumedSpace = await getConsumedSpace(token.collection.helper.getApi(), token.collectionId, token.tokenId, pieces == 1n ? 'NFT' : 'RFT');
expect(consumedSpace).to.be.equal(originalSpace);
}
@@ -911,7 +911,7 @@
expect(await token.getProperties(['non-existent', 'now-existent'])).to.be.empty;
- const consumedSpace = await getConsumedSpace(token.collection.helper.api, token.collectionId, token.tokenId, pieces == 1n ? 'NFT' : 'RFT');
+ const consumedSpace = await getConsumedSpace(token.collection.helper.getApi(), token.collectionId, token.tokenId, pieces == 1n ? 'NFT' : 'RFT');
expect(consumedSpace).to.be.equal(originalSpace);
}
@@ -951,7 +951,7 @@
])).to.be.rejectedWith(/common\.NoSpaceForProperty/);
expect(await token.getProperties(['a_holy_book', 'young_years'])).to.be.empty;
- const consumedSpace = await getConsumedSpace(token.collection.helper.api, token.collectionId, token.tokenId, pieces == 1n ? 'NFT' : 'RFT');
+ const consumedSpace = await getConsumedSpace(token.collection.helper.getApi(), token.collectionId, token.tokenId, pieces == 1n ? 'NFT' : 'RFT');
expect(consumedSpace).to.be.equal(originalSpace);
}
tests/src/pallet-presence.test.tsdiffbeforeafterboth--- a/tests/src/pallet-presence.test.ts
+++ b/tests/src/pallet-presence.test.ts
@@ -59,7 +59,7 @@
describe('Pallet presence', () => {
before(async () => {
await usingPlaygrounds(async helper => {
- const chain = await helper.api!.rpc.system.chain();
+ const chain = await helper.callRpc('api.rpc.system.chain', []);
const refungible = 'refungible';
const scheduler = 'scheduler';
tests/src/tx-version-presence.test.tsdiffbeforeafterboth--- a/tests/src/tx-version-presence.test.ts
+++ b/tests/src/tx-version-presence.test.ts
@@ -22,7 +22,7 @@
describe('TxVersion is present', () => {
before(async () => {
await usingPlaygrounds(async helper => {
- metadata = await helper.api!.rpc.state.getMetadata();
+ metadata = await helper.callRpc('api.rpc.state.getMetadata', []);
});
});
tests/src/util/playgrounds/unique.dev.tsdiffbeforeafterboth--- a/tests/src/util/playgrounds/unique.dev.ts
+++ b/tests/src/util/playgrounds/unique.dev.ts
@@ -215,8 +215,8 @@
};
isDevNode = async () => {
- const block1 = await this.helper.api?.rpc.chain.getBlock(await this.helper.api?.rpc.chain.getBlockHash(1));
- const block2 = await this.helper.api?.rpc.chain.getBlock(await this.helper.api?.rpc.chain.getBlockHash(2));
+ const block1 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [1])]);
+ const block2 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [2])]);
const findCreationDate = async (block: any) => {
const humanBlock = block.toHuman();
let date;
@@ -259,7 +259,7 @@
async newBlocks(blocksCount = 1): Promise<void> {
// eslint-disable-next-line no-async-promise-executor
const promise = new Promise<void>(async (resolve) => {
- const unsubscribe = await this.helper.api!.rpc.chain.subscribeNewHeads(() => {
+ const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(() => {
if (blocksCount > 0) {
blocksCount--;
} else {
@@ -274,7 +274,7 @@
async forParachainBlockNumber(blockNumber: bigint) {
// eslint-disable-next-line no-async-promise-executor
return new Promise<void>(async (resolve) => {
- const unsubscribe = await this.helper.api!.rpc.chain.subscribeNewHeads(async (data: any) => {
+ const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(async (data: any) => {
if (data.number.toNumber() >= blockNumber) {
unsubscribe();
resolve();
@@ -286,7 +286,7 @@
async forRelayBlockNumber(blockNumber: bigint) {
// eslint-disable-next-line no-async-promise-executor
return new Promise<void>(async (resolve) => {
- const unsubscribe = await this.helper.api!.query.parachainSystem.validationData(async (data: any) => {
+ const unsubscribe = await this.helper.getApi().query.parachainSystem.validationData(async (data: any) => {
if (data.value.relayParentNumber.toNumber() >= blockNumber) {
// @ts-ignore
unsubscribe();
tests/src/util/playgrounds/unique.tsdiffbeforeafterboth--- a/tests/src/util/playgrounds/unique.ts
+++ b/tests/src/util/playgrounds/unique.ts
@@ -1991,7 +1991,7 @@
* @returns ss58Format, token decimals, and token symbol
*/
getChainProperties(): IChainProperties {
- const properties = (this.helper.api as any).registry.getChainProperties().toJSON();
+ const properties = (this.helper.getApi() as any).registry.getChainProperties().toJSON();
return {
ss58Format: properties.ss58Format.toJSON(),
tokenDecimals: properties.tokenDecimals.toJSON(),
@@ -2034,7 +2034,7 @@
* @returns number, account's nonce
*/
async getNonce(address: TSubstrateAccount): Promise<number> {
- return (await (this.helper.api as any).query.system.account(address)).nonce.toNumber();
+ return (await this.helper.callRpc('api.query.system.account', [address])).nonce.toNumber();
}
}