difftreelog
fix remove unused import
in: master
1 file changed
tests/src/.outdated/scheduler.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 {18 default as usingApi,19 submitTransactionAsync,20 submitTransactionExpectFailAsync,21} from '../substrate/substrate-api';22import {23 UNIQUE,24 waitNewBlocks,25} from './util/helpers';26import {expect, itSub, Pallets, usingPlaygrounds} from './util/playgrounds';27import {IKeyringPair} from '@polkadot/types/types';2829describe('Scheduling token and balance transfers', () => {30 let alice: IKeyringPair;31 let bob: IKeyringPair;32 let charlie: IKeyringPair;3334 before(async () => {35 await usingPlaygrounds(async (_, privateKeyWrapper) => {36 alice = privateKeyWrapper('//Alice');37 bob = privateKeyWrapper('//Bob');38 charlie = privateKeyWrapper('//Charlie');39 });40 });4142 itSub.ifWithPallets('Can delay a transfer of an owned token', [Pallets.Scheduler], async ({helper}) => {43 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});44 const token = await collection.mintToken(alice);45 const schedulerId = await helper.arrange.makeScheduledId();46 const blocksBeforeExecution = 4;4748 await token.scheduleAfter(schedulerId, blocksBeforeExecution)49 .transfer(alice, {Substrate: bob.address});5051 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});5253 await helper.wait.newBlocks(blocksBeforeExecution + 1);5455 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});56 });5758 itSub.ifWithPallets('Can transfer funds periodically', [Pallets.Scheduler], async ({helper}) => {59 const scheduledId = await helper.arrange.makeScheduledId();60 const waitForBlocks = 1;6162 const amount = 1n * UNIQUE;63 const periodic = {64 period: 2,65 repetitions: 2,66 };6768 const bobsBalanceBefore = await helper.balance.getSubstrate(bob.address);6970 await helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {periodic})71 .balance.transferToSubstrate(alice, bob.address, amount);7273 await helper.wait.newBlocks(waitForBlocks + 1);7475 const bobsBalanceAfterFirst = await helper.balance.getSubstrate(bob.address);76 expect(bobsBalanceAfterFirst)77 .to.be.equal(78 bobsBalanceBefore + 1n * amount,79 '#1 Balance of the recipient should be increased by 1 * amount',80 );8182 await helper.wait.newBlocks(periodic.period);8384 const bobsBalanceAfterSecond = await helper.balance.getSubstrate(bob.address);85 expect(bobsBalanceAfterSecond)86 .to.be.equal(87 bobsBalanceBefore + 2n * amount,88 '#2 Balance of the recipient should be increased by 2 * amount',89 );90 });9192 itSub.ifWithPallets('Can cancel a scheduled operation which has not yet taken effect', [Pallets.Scheduler], async ({helper}) => {93 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});94 const token = await collection.mintToken(alice);9596 const scheduledId = await helper.arrange.makeScheduledId();97 const waitForBlocks = 4;9899 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});100101 await token.scheduleAfter(scheduledId, waitForBlocks)102 .transfer(alice, {Substrate: bob.address});103104 await helper.scheduler.cancelScheduled(alice, scheduledId);105106 await waitNewBlocks(waitForBlocks + 1);107108 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});109 });110111 itSub.ifWithPallets('Can cancel a periodic operation (transfer of funds)', [Pallets.Scheduler], async ({helper}) => {112 const waitForBlocks = 1;113 const periodic = {114 period: 3,115 repetitions: 2,116 };117118 const scheduledId = await helper.arrange.makeScheduledId();119120 const amount = 1n * UNIQUE;121122 const bobsBalanceBefore = await helper.balance.getSubstrate(bob.address);123124 await helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {periodic})125 .balance.transferToSubstrate(alice, bob.address, amount);126127 await helper.wait.newBlocks(waitForBlocks + 1);128129 const bobsBalanceAfterFirst = await helper.balance.getSubstrate(bob.address);130131 expect(bobsBalanceAfterFirst)132 .to.be.equal(133 bobsBalanceBefore + 1n * amount,134 '#1 Balance of the recipient should be increased by 1 * amount',135 );136137 await helper.scheduler.cancelScheduled(alice, scheduledId);138 await helper.wait.newBlocks(periodic.period);139140 const bobsBalanceAfterSecond = await helper.balance.getSubstrate(bob.address);141 expect(bobsBalanceAfterSecond)142 .to.be.equal(143 bobsBalanceAfterFirst,144 '#2 Balance of the recipient should not be changed',145 );146 });147148 itSub.ifWithPallets('Scheduled tasks are transactional', [Pallets.Scheduler, Pallets.TestUtils], async ({helper}) => {149 const scheduledId = await helper.arrange.makeScheduledId();150 const waitForBlocks = 4;151152 const initTestVal = 42;153 const changedTestVal = 111;154155 await helper.executeExtrinsic(156 alice,157 'api.tx.testUtils.setTestValue',158 [initTestVal],159 true,160 );161162 await helper.scheduler.scheduleAfter(scheduledId, waitForBlocks)163 .executeExtrinsic(164 alice,165 'api.tx.testUtils.setTestValueAndRollback',166 [changedTestVal],167 true,168 );169170 await helper.wait.newBlocks(waitForBlocks + 1);171172 const testVal = (await helper.api!.query.testUtils.testValue()).toNumber();173 expect(testVal, 'The test value should NOT be commited')174 .to.be.equal(initTestVal);175 });176177 itSub.ifWithPallets('Scheduled tasks should take correct fees', [Pallets.Scheduler, Pallets.TestUtils], async function({helper}) {178 const scheduledId = await helper.arrange.makeScheduledId();179 const waitForBlocks = 4;180 const periodic = {181 period: 2,182 repetitions: 2,183 };184185 const dummyTx = helper.constructApiCall('api.tx.testUtils.justTakeFee', []);186 const scheduledLen = dummyTx.callIndex.length;187188 const expectedScheduledFee = (await helper.getPaymentInfo(alice, dummyTx, scheduledLen))189 .partialFee.toBigInt();190191 await helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {periodic})192 .executeExtrinsic(alice, 'api.tx.testUtils.justTakeFee', [], true);193194 await helper.wait.newBlocks(1);195196 const aliceInitBalance = await helper.balance.getSubstrate(alice.address);197 let diff;198199 await helper.wait.newBlocks(waitForBlocks);200201 const aliceBalanceAfterFirst = await helper.balance.getSubstrate(alice.address);202 expect(203 aliceBalanceAfterFirst < aliceInitBalance,204 '[after execution #1] Scheduled task should take a fee',205 ).to.be.true;206207 diff = aliceInitBalance - aliceBalanceAfterFirst;208 expect(diff).to.be.equal(209 expectedScheduledFee,210 'Scheduled task should take the right amount of fees',211 );212213 await helper.wait.newBlocks(periodic.period);214215 const aliceBalanceAfterSecond = await helper.balance.getSubstrate(alice.address);216 expect(217 aliceBalanceAfterSecond < aliceBalanceAfterFirst,218 '[after execution #2] Scheduled task should take a fee',219 ).to.be.true;220221 diff = aliceBalanceAfterFirst - aliceBalanceAfterSecond;222 expect(diff).to.be.equal(223 expectedScheduledFee,224 'Scheduled task should take the right amount of fees',225 );226 });227228 // Check if we can cancel a scheduled periodic operation229 // in the same block in which it is running230 itSub.ifWithPallets('Can cancel the periodic sheduled tx when the tx is running', [Pallets.Scheduler, Pallets.TestUtils], async ({helper}) => {231 const currentBlockNumber = await helper.chain.getLatestBlockNumber();232 const blocksBeforeExecution = 10;233 const firstExecutionBlockNumber = currentBlockNumber + blocksBeforeExecution;234235 const [236 scheduledId,237 scheduledCancelId,238 ] = await helper.arrange.makeScheduledIds(2);239240 const periodic = {241 period: 5,242 repetitions: 5,243 };244245 const initTestVal = 0;246 const incTestVal = initTestVal + 1;247 const finalTestVal = initTestVal + 2;248249 await helper.executeExtrinsic(250 alice,251 'api.tx.testUtils.setTestValue',252 [initTestVal],253 true,254 );255256 await helper.scheduler.scheduleAt(scheduledId, firstExecutionBlockNumber, {periodic})257 .executeExtrinsic(258 alice,259 'api.tx.testUtils.incTestValue',260 [],261 true,262 );263264 // Cancel the inc tx after 2 executions265 // *in the same block* in which the second execution is scheduled266 await helper.scheduler.scheduleAt(267 scheduledCancelId,268 firstExecutionBlockNumber + periodic.period,269 ).scheduler.cancelScheduled(alice, scheduledId);270271 await helper.wait.newBlocks(blocksBeforeExecution);272273 // execution #0274 expect((await helper.api!.query.testUtils.testValue()).toNumber())275 .to.be.equal(incTestVal);276277 await helper.wait.newBlocks(periodic.period);278279 // execution #1280 expect((await helper.api!.query.testUtils.testValue()).toNumber())281 .to.be.equal(finalTestVal);282283 for (let i = 1; i < periodic.repetitions; i++) {284 await waitNewBlocks(periodic.period);285 expect((await helper.api!.query.testUtils.testValue()).toNumber())286 .to.be.equal(finalTestVal);287 }288 });289290 itSub.ifWithPallets('A scheduled operation can cancel itself', [Pallets.Scheduler, Pallets.TestUtils], async ({helper}) => {291 const scheduledId = await helper.arrange.makeScheduledId();292 const waitForBlocks = 4;293 const periodic = {294 period: 2,295 repetitions: 5,296 };297298 const initTestVal = 0;299 const maxTestVal = 2;300301 await helper.executeExtrinsic(302 alice,303 'api.tx.testUtils.setTestValue',304 [initTestVal],305 true,306 );307308 await helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {periodic})309 .executeExtrinsic(310 alice,311 'api.tx.testUtils.selfCancelingInc',312 [scheduledId, maxTestVal],313 true,314 );315316 await helper.wait.newBlocks(waitForBlocks + 1);317318 // execution #0319 expect((await helper.api!.query.testUtils.testValue()).toNumber())320 .to.be.equal(initTestVal + 1);321322 await helper.wait.newBlocks(periodic.period);323324 // execution #1325 expect((await helper.api!.query.testUtils.testValue()).toNumber())326 .to.be.equal(initTestVal + 2);327328 await helper.wait.newBlocks(periodic.period);329330 // <canceled>331 expect((await helper.api!.query.testUtils.testValue()).toNumber())332 .to.be.equal(initTestVal + 2);333 });334335 itSub.ifWithPallets('Root can cancel any scheduled operation', [Pallets.Scheduler], async ({helper}) => {336 const collection = await helper.nft.mintCollection(bob, {tokenPrefix: 'schd'});337 const token = await collection.mintToken(bob);338339 const scheduledId = await helper.arrange.makeScheduledId();340 const waitForBlocks = 4;341342 await token.scheduleAfter(scheduledId, waitForBlocks)343 .transfer(bob, {Substrate: alice.address});344345 await helper.getSudo().scheduler.cancelScheduled(alice, scheduledId);346347 await helper.wait.newBlocks(waitForBlocks + 1);348349 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});350 });351352 itSub.ifWithPallets('Root can set prioritized scheduled operation', [Pallets.Scheduler], async ({helper}) => {353 const scheduledId = await helper.arrange.makeScheduledId();354 const waitForBlocks = 4;355356 const amount = 42n * UNIQUE;357358 const balanceBefore = await helper.balance.getSubstrate(charlie.address);359360 await helper.getSudo()361 .scheduler.scheduleAfter(scheduledId, waitForBlocks, {priority: 42})362 .balance.forceTransferToSubstrate(alice, bob.address, charlie.address, amount);363364 await helper.wait.newBlocks(waitForBlocks + 1);365366 const balanceAfter = await helper.balance.getSubstrate(charlie.address);367368 expect(balanceAfter > balanceBefore).to.be.true;369370 const diff = balanceAfter - balanceBefore;371 expect(diff).to.be.equal(amount);372 });373374 itSub.ifWithPallets("Root can change scheduled operation's priority", [Pallets.Scheduler], async ({helper}) => {375 const collection = await helper.nft.mintCollection(bob, {tokenPrefix: 'schd'});376 const token = await collection.mintToken(bob);377378 const scheduledId = await helper.arrange.makeScheduledId();379 const waitForBlocks = 6;380381 await token.scheduleAfter(scheduledId, waitForBlocks)382 .transfer(bob, {Substrate: alice.address});383384 const priority = 112;385 await helper.getSudo().scheduler.changePriority(alice, scheduledId, priority);386387 const priorityChanged = await helper.wait.event(388 waitForBlocks,389 'scheduler',390 'PriorityChanged',391 );392393 expect(priorityChanged !== null).to.be.true;394 expect(priorityChanged!.event.data[2].toString()).to.be.equal(priority.toString());395 });396397 itSub.ifWithPallets('Prioritized operations executes in valid order', [Pallets.Scheduler], async ({helper}) => {398 const [399 scheduledFirstId,400 scheduledSecondId,401 ] = await helper.arrange.makeScheduledIds(2);402403 const currentBlockNumber = await helper.chain.getLatestBlockNumber();404 const blocksBeforeExecution = 4;405 const firstExecutionBlockNumber = currentBlockNumber + blocksBeforeExecution;406407 const prioHigh = 0;408 const prioLow = 255;409410 const periodic = {411 period: 6,412 repetitions: 2,413 };414415 const amount = 1n * UNIQUE;416417 // Scheduler a task with a lower priority first, then with a higher priority418 await helper.getSudo().scheduler.scheduleAt(scheduledFirstId, firstExecutionBlockNumber, {priority: prioLow, periodic})419 .balance.forceTransferToSubstrate(alice, alice.address, bob.address, amount);420421 await helper.getSudo().scheduler.scheduleAt(scheduledSecondId, firstExecutionBlockNumber, {priority: prioHigh, periodic})422 .balance.forceTransferToSubstrate(alice, alice.address, bob.address, amount);423424 const capture = await helper.arrange.captureEvents('scheduler', 'Dispatched');425426 await helper.wait.newBlocks(blocksBeforeExecution);427428 // Flip priorities429 await helper.getSudo().scheduler.changePriority(alice, scheduledFirstId, prioHigh);430 await helper.getSudo().scheduler.changePriority(alice, scheduledSecondId, prioLow);431432 await helper.wait.newBlocks(periodic.period);433434 const dispatchEvents = capture.extractCapturedEvents();435 expect(dispatchEvents.length).to.be.equal(4);436437 const dispatchedIds = dispatchEvents.map(r => r.event.data[1].toString());438439 const firstExecuctionIds = [dispatchedIds[0], dispatchedIds[1]];440 const secondExecuctionIds = [dispatchedIds[2], dispatchedIds[3]];441442 expect(firstExecuctionIds[0]).to.be.equal(scheduledSecondId);443 expect(firstExecuctionIds[1]).to.be.equal(scheduledFirstId);444445 expect(secondExecuctionIds[0]).to.be.equal(scheduledFirstId);446 expect(secondExecuctionIds[1]).to.be.equal(scheduledSecondId);447 });448});449450describe('Negative Test: Scheduling', () => {451 let alice: IKeyringPair;452 let bob: IKeyringPair;453454 before(async () => {455 await usingPlaygrounds(async (_, privateKeyWrapper) => {456 alice = privateKeyWrapper('//Alice');457 bob = privateKeyWrapper('//Bob');458 });459 });460461 itSub.ifWithPallets("Can't overwrite a scheduled ID", [Pallets.Scheduler], async ({helper}) => {462 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});463 const token = await collection.mintToken(alice);464465 const scheduledId = await helper.arrange.makeScheduledId();466 const waitForBlocks = 4;467468 await token.scheduleAfter(scheduledId, waitForBlocks)469 .transfer(alice, {Substrate: bob.address});470471 const scheduled = helper.scheduler.scheduleAfter(scheduledId, waitForBlocks);472 await expect(scheduled.balance.transferToSubstrate(alice, bob.address, 1n * UNIQUE))473 .to.be.rejectedWith(/scheduler\.FailedToSchedule/);474475 const bobsBalanceBefore = await helper.balance.getSubstrate(bob.address);476477 await helper.wait.newBlocks(waitForBlocks + 1);478479 const bobsBalanceAfter = await helper.balance.getSubstrate(bob.address);480481 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});482 expect(bobsBalanceBefore).to.be.equal(bobsBalanceAfter);483 });484485 itSub.ifWithPallets("Can't cancel an operation which is not scheduled", [Pallets.Scheduler], async ({helper}) => {486 const scheduledId = await helper.arrange.makeScheduledId();487 await expect(helper.scheduler.cancelScheduled(alice, scheduledId))488 .to.be.rejectedWith(/scheduler\.NotFound/);489 });490491 itSub.ifWithPallets("Can't cancel a non-owned scheduled operation", [Pallets.Scheduler], async ({helper}) => {492 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});493 const token = await collection.mintToken(alice);494495 const scheduledId = await helper.arrange.makeScheduledId();496 const waitForBlocks = 4;497498 await token.scheduleAfter(scheduledId, waitForBlocks)499 .transfer(alice, {Substrate: bob.address});500501 await expect(helper.scheduler.cancelScheduled(bob, scheduledId))502 .to.be.rejectedWith(/BadOrigin/);503504 await helper.wait.newBlocks(waitForBlocks + 1);505506 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});507 });508509 itSub.ifWithPallets("Regular user can't set prioritized scheduled operation", [Pallets.Scheduler], async ({helper}) => {510 const scheduledId = await helper.arrange.makeScheduledId();511 const waitForBlocks = 4;512513 const amount = 42n * UNIQUE;514515 const balanceBefore = await helper.balance.getSubstrate(bob.address);516517 const scheduled = helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {priority: 42});518 519 await expect(scheduled.balance.transferToSubstrate(alice, bob.address, amount))520 .to.be.rejectedWith(/BadOrigin/);521522 await helper.wait.newBlocks(waitForBlocks + 1);523524 const balanceAfter = await helper.balance.getSubstrate(bob.address);525526 expect(balanceAfter).to.be.equal(balanceBefore);527 });528529 itSub.ifWithPallets("Regular user can't change scheduled operation's priority", [Pallets.Scheduler], async ({helper}) => {530 const collection = await helper.nft.mintCollection(bob, {tokenPrefix: 'schd'});531 const token = await collection.mintToken(bob);532533 const scheduledId = await helper.arrange.makeScheduledId();534 const waitForBlocks = 4;535536 await token.scheduleAfter(scheduledId, waitForBlocks)537 .transfer(bob, {Substrate: alice.address});538539 const priority = 112;540 await expect(helper.scheduler.changePriority(alice, scheduledId, priority))541 .to.be.rejectedWith(/BadOrigin/);542543 const priorityChanged = await helper.wait.event(544 waitForBlocks,545 'scheduler',546 'PriorityChanged',547 );548549 expect(priorityChanged === null).to.be.true;550 });551});552553// Implementation of the functionality tested here was postponed/shelved554describe.skip('Sponsoring scheduling', () => {555 // let alice: IKeyringPair;556 // let bob: IKeyringPair;557558 // before(async() => {559 // await usingApi(async (_, privateKeyWrapper) => {560 // alice = privateKeyWrapper('//Alice');561 // bob = privateKeyWrapper('//Bob');562 // });563 // });564565 it('Can sponsor scheduling a transaction', async () => {566 // const collectionId = await createCollectionExpectSuccess();567 // await setCollectionSponsorExpectSuccess(collectionId, bob.address);568 // await confirmSponsorshipExpectSuccess(collectionId, '//Bob');569570 // await usingApi(async api => {571 // const scheduledId = await makeScheduledId();572 // const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address);573574 // const bobBalanceBefore = await getFreeBalance(bob);575 // const waitForBlocks = 4;576 // // no need to wait to check, fees must be deducted on scheduling, immediately577 // await scheduleTransferExpectSuccess(api, collectionId, tokenId, alice, bob, 0, waitForBlocks, scheduledId);578 // const bobBalanceAfter = await getFreeBalance(bob);579 // // expect(aliceBalanceAfter == aliceBalanceBefore).to.be.true;580 // expect(bobBalanceAfter < bobBalanceBefore).to.be.true;581 // // wait for sequentiality matters582 // await waitNewBlocks(waitForBlocks - 1);583 // });584 });585586 it('Schedules and dispatches a transaction even if the caller has no funds at the time of the dispatch', async () => {587 // await usingApi(async (api, privateKeyWrapper) => {588 // // Find an empty, unused account589 // const zeroBalance = await findUnusedAddress(api, privateKeyWrapper);590591 // const collectionId = await createCollectionExpectSuccess();592593 // // Add zeroBalance address to allow list594 // await enablePublicMintingExpectSuccess(alice, collectionId);595 // await addToAllowListExpectSuccess(alice, collectionId, zeroBalance.address);596597 // // Grace zeroBalance with money, enough to cover future transactions598 // const balanceTx = api.tx.balances.transfer(zeroBalance.address, 1n * UNIQUE);599 // await submitTransactionAsync(alice, balanceTx);600601 // // Mint a fresh NFT602 // const tokenId = await createItemExpectSuccess(zeroBalance, collectionId, 'NFT');603 // const scheduledId = await makeScheduledId();604605 // // Schedule transfer of the NFT a few blocks ahead606 // const waitForBlocks = 5;607 // await scheduleTransferExpectSuccess(api, collectionId, tokenId, zeroBalance, alice, 1, waitForBlocks, scheduledId);608609 // // Get rid of the account's funds before the scheduled transaction takes place610 // const balanceTx2 = api.tx.balances.transfer(alice.address, UNIQUE * 68n / 100n);611 // const events = await submitTransactionAsync(zeroBalance, balanceTx2);612 // expect(getGenericResult(events).success).to.be.true;613 // /*const emptyBalanceTx = api.tx.balances.setBalance(zeroBalance.address, 0, 0); // do not null reserved?614 // const sudoTx = api.tx.sudo.sudo(emptyBalanceTx as any);615 // const events = await submitTransactionAsync(alice, sudoTx);616 // expect(getGenericResult(events).success).to.be.true;*/617618 // // Wait for a certain number of blocks, discarding the ones that already happened while accepting the late transactions619 // await waitNewBlocks(waitForBlocks - 3);620621 // expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(alice.address));622 // });623 });624625 it('Sponsor going bankrupt does not impact a scheduled transaction', async () => {626 // const collectionId = await createCollectionExpectSuccess();627628 // await usingApi(async (api, privateKeyWrapper) => {629 // const zeroBalance = await findUnusedAddress(api, privateKeyWrapper);630 // const balanceTx = api.tx.balances.transfer(zeroBalance.address, 1n * UNIQUE);631 // await submitTransactionAsync(alice, balanceTx);632633 // await setCollectionSponsorExpectSuccess(collectionId, zeroBalance.address);634 // await confirmSponsorshipByKeyExpectSuccess(collectionId, zeroBalance);635636 // const scheduledId = await makeScheduledId();637 // const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address);638639 // const waitForBlocks = 5;640 // await scheduleTransferExpectSuccess(api, collectionId, tokenId, alice, zeroBalance, 1, waitForBlocks, scheduledId);641642 // const emptyBalanceSponsorTx = api.tx.balances.setBalance(zeroBalance.address, 0, 0);643 // const sudoTx = api.tx.sudo.sudo(emptyBalanceSponsorTx as any);644 // const events = await submitTransactionAsync(alice, sudoTx);645 // expect(getGenericResult(events).success).to.be.true;646647 // // Wait for a certain number of blocks, save for the ones that already happened while accepting the late transactions648 // await waitNewBlocks(waitForBlocks - 3);649650 // expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(zeroBalance.address));651 // });652 });653654 it('Exceeding sponsor rate limit without having enough funds prevents scheduling a periodic transaction', async () => {655 // const collectionId = await createCollectionExpectSuccess();656 // await setCollectionSponsorExpectSuccess(collectionId, bob.address);657 // await confirmSponsorshipExpectSuccess(collectionId, '//Bob');658659 // await usingApi(async (api, privateKeyWrapper) => {660 // const zeroBalance = await findUnusedAddress(api, privateKeyWrapper);661662 // await enablePublicMintingExpectSuccess(alice, collectionId);663 // await addToAllowListExpectSuccess(alice, collectionId, zeroBalance.address);664665 // const bobBalanceBefore = await getFreeBalance(bob);666667 // const createData = {nft: {const_data: [], variable_data: []}};668 // const creationTx = api.tx.unique.createItem(collectionId, normalizeAccountId(zeroBalance), createData as any);669 // const scheduledId = await makeScheduledId();670671 // /*const badTransaction = async function () {672 // await submitTransactionExpectFailAsync(zeroBalance, zeroToAlice);673 // };674 // await expect(badTransaction()).to.be.rejectedWith('Inability to pay some fees');*/675676 // await expect(scheduleAfter(api, creationTx, zeroBalance, 3, scheduledId, 1, 3)).to.be.rejectedWith(/Inability to pay some fees/);677678 // expect(await getFreeBalance(bob)).to.be.equal(bobBalanceBefore);679 // });680 });681});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 {18 UNIQUE,19 waitNewBlocks,20} from './util/helpers';21import {expect, itSub, Pallets, usingPlaygrounds} from './util/playgrounds';22import {IKeyringPair} from '@polkadot/types/types';2324describe('Scheduling token and balance transfers', () => {25 let alice: IKeyringPair;26 let bob: IKeyringPair;27 let charlie: IKeyringPair;2829 before(async () => {30 await usingPlaygrounds(async (_, privateKeyWrapper) => {31 alice = privateKeyWrapper('//Alice');32 bob = privateKeyWrapper('//Bob');33 charlie = privateKeyWrapper('//Charlie');34 });35 });3637 itSub.ifWithPallets('Can delay a transfer of an owned token', [Pallets.Scheduler], async ({helper}) => {38 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});39 const token = await collection.mintToken(alice);40 const schedulerId = await helper.arrange.makeScheduledId();41 const blocksBeforeExecution = 4;4243 await token.scheduleAfter(schedulerId, blocksBeforeExecution)44 .transfer(alice, {Substrate: bob.address});4546 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});4748 await helper.wait.newBlocks(blocksBeforeExecution + 1);4950 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});51 });5253 itSub.ifWithPallets('Can transfer funds periodically', [Pallets.Scheduler], async ({helper}) => {54 const scheduledId = await helper.arrange.makeScheduledId();55 const waitForBlocks = 1;5657 const amount = 1n * UNIQUE;58 const periodic = {59 period: 2,60 repetitions: 2,61 };6263 const bobsBalanceBefore = await helper.balance.getSubstrate(bob.address);6465 await helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {periodic})66 .balance.transferToSubstrate(alice, bob.address, amount);6768 await helper.wait.newBlocks(waitForBlocks + 1);6970 const bobsBalanceAfterFirst = await helper.balance.getSubstrate(bob.address);71 expect(bobsBalanceAfterFirst)72 .to.be.equal(73 bobsBalanceBefore + 1n * amount,74 '#1 Balance of the recipient should be increased by 1 * amount',75 );7677 await helper.wait.newBlocks(periodic.period);7879 const bobsBalanceAfterSecond = await helper.balance.getSubstrate(bob.address);80 expect(bobsBalanceAfterSecond)81 .to.be.equal(82 bobsBalanceBefore + 2n * amount,83 '#2 Balance of the recipient should be increased by 2 * amount',84 );85 });8687 itSub.ifWithPallets('Can cancel a scheduled operation which has not yet taken effect', [Pallets.Scheduler], async ({helper}) => {88 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});89 const token = await collection.mintToken(alice);9091 const scheduledId = await helper.arrange.makeScheduledId();92 const waitForBlocks = 4;9394 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});9596 await token.scheduleAfter(scheduledId, waitForBlocks)97 .transfer(alice, {Substrate: bob.address});9899 await helper.scheduler.cancelScheduled(alice, scheduledId);100101 await waitNewBlocks(waitForBlocks + 1);102103 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});104 });105106 itSub.ifWithPallets('Can cancel a periodic operation (transfer of funds)', [Pallets.Scheduler], async ({helper}) => {107 const waitForBlocks = 1;108 const periodic = {109 period: 3,110 repetitions: 2,111 };112113 const scheduledId = await helper.arrange.makeScheduledId();114115 const amount = 1n * UNIQUE;116117 const bobsBalanceBefore = await helper.balance.getSubstrate(bob.address);118119 await helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {periodic})120 .balance.transferToSubstrate(alice, bob.address, amount);121122 await helper.wait.newBlocks(waitForBlocks + 1);123124 const bobsBalanceAfterFirst = await helper.balance.getSubstrate(bob.address);125126 expect(bobsBalanceAfterFirst)127 .to.be.equal(128 bobsBalanceBefore + 1n * amount,129 '#1 Balance of the recipient should be increased by 1 * amount',130 );131132 await helper.scheduler.cancelScheduled(alice, scheduledId);133 await helper.wait.newBlocks(periodic.period);134135 const bobsBalanceAfterSecond = await helper.balance.getSubstrate(bob.address);136 expect(bobsBalanceAfterSecond)137 .to.be.equal(138 bobsBalanceAfterFirst,139 '#2 Balance of the recipient should not be changed',140 );141 });142143 itSub.ifWithPallets('Scheduled tasks are transactional', [Pallets.Scheduler, Pallets.TestUtils], async ({helper}) => {144 const scheduledId = await helper.arrange.makeScheduledId();145 const waitForBlocks = 4;146147 const initTestVal = 42;148 const changedTestVal = 111;149150 await helper.executeExtrinsic(151 alice,152 'api.tx.testUtils.setTestValue',153 [initTestVal],154 true,155 );156157 await helper.scheduler.scheduleAfter(scheduledId, waitForBlocks)158 .executeExtrinsic(159 alice,160 'api.tx.testUtils.setTestValueAndRollback',161 [changedTestVal],162 true,163 );164165 await helper.wait.newBlocks(waitForBlocks + 1);166167 const testVal = (await helper.api!.query.testUtils.testValue()).toNumber();168 expect(testVal, 'The test value should NOT be commited')169 .to.be.equal(initTestVal);170 });171172 itSub.ifWithPallets('Scheduled tasks should take correct fees', [Pallets.Scheduler, Pallets.TestUtils], async function({helper}) {173 const scheduledId = await helper.arrange.makeScheduledId();174 const waitForBlocks = 4;175 const periodic = {176 period: 2,177 repetitions: 2,178 };179180 const dummyTx = helper.constructApiCall('api.tx.testUtils.justTakeFee', []);181 const scheduledLen = dummyTx.callIndex.length;182183 const expectedScheduledFee = (await helper.getPaymentInfo(alice, dummyTx, scheduledLen))184 .partialFee.toBigInt();185186 await helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {periodic})187 .executeExtrinsic(alice, 'api.tx.testUtils.justTakeFee', [], true);188189 await helper.wait.newBlocks(1);190191 const aliceInitBalance = await helper.balance.getSubstrate(alice.address);192 let diff;193194 await helper.wait.newBlocks(waitForBlocks);195196 const aliceBalanceAfterFirst = await helper.balance.getSubstrate(alice.address);197 expect(198 aliceBalanceAfterFirst < aliceInitBalance,199 '[after execution #1] Scheduled task should take a fee',200 ).to.be.true;201202 diff = aliceInitBalance - aliceBalanceAfterFirst;203 expect(diff).to.be.equal(204 expectedScheduledFee,205 'Scheduled task should take the right amount of fees',206 );207208 await helper.wait.newBlocks(periodic.period);209210 const aliceBalanceAfterSecond = await helper.balance.getSubstrate(alice.address);211 expect(212 aliceBalanceAfterSecond < aliceBalanceAfterFirst,213 '[after execution #2] Scheduled task should take a fee',214 ).to.be.true;215216 diff = aliceBalanceAfterFirst - aliceBalanceAfterSecond;217 expect(diff).to.be.equal(218 expectedScheduledFee,219 'Scheduled task should take the right amount of fees',220 );221 });222223 // Check if we can cancel a scheduled periodic operation224 // in the same block in which it is running225 itSub.ifWithPallets('Can cancel the periodic sheduled tx when the tx is running', [Pallets.Scheduler, Pallets.TestUtils], async ({helper}) => {226 const currentBlockNumber = await helper.chain.getLatestBlockNumber();227 const blocksBeforeExecution = 10;228 const firstExecutionBlockNumber = currentBlockNumber + blocksBeforeExecution;229230 const [231 scheduledId,232 scheduledCancelId,233 ] = await helper.arrange.makeScheduledIds(2);234235 const periodic = {236 period: 5,237 repetitions: 5,238 };239240 const initTestVal = 0;241 const incTestVal = initTestVal + 1;242 const finalTestVal = initTestVal + 2;243244 await helper.executeExtrinsic(245 alice,246 'api.tx.testUtils.setTestValue',247 [initTestVal],248 true,249 );250251 await helper.scheduler.scheduleAt(scheduledId, firstExecutionBlockNumber, {periodic})252 .executeExtrinsic(253 alice,254 'api.tx.testUtils.incTestValue',255 [],256 true,257 );258259 // Cancel the inc tx after 2 executions260 // *in the same block* in which the second execution is scheduled261 await helper.scheduler.scheduleAt(262 scheduledCancelId,263 firstExecutionBlockNumber + periodic.period,264 ).scheduler.cancelScheduled(alice, scheduledId);265266 await helper.wait.newBlocks(blocksBeforeExecution);267268 // execution #0269 expect((await helper.api!.query.testUtils.testValue()).toNumber())270 .to.be.equal(incTestVal);271272 await helper.wait.newBlocks(periodic.period);273274 // execution #1275 expect((await helper.api!.query.testUtils.testValue()).toNumber())276 .to.be.equal(finalTestVal);277278 for (let i = 1; i < periodic.repetitions; i++) {279 await waitNewBlocks(periodic.period);280 expect((await helper.api!.query.testUtils.testValue()).toNumber())281 .to.be.equal(finalTestVal);282 }283 });284285 itSub.ifWithPallets('A scheduled operation can cancel itself', [Pallets.Scheduler, Pallets.TestUtils], async ({helper}) => {286 const scheduledId = await helper.arrange.makeScheduledId();287 const waitForBlocks = 4;288 const periodic = {289 period: 2,290 repetitions: 5,291 };292293 const initTestVal = 0;294 const maxTestVal = 2;295296 await helper.executeExtrinsic(297 alice,298 'api.tx.testUtils.setTestValue',299 [initTestVal],300 true,301 );302303 await helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {periodic})304 .executeExtrinsic(305 alice,306 'api.tx.testUtils.selfCancelingInc',307 [scheduledId, maxTestVal],308 true,309 );310311 await helper.wait.newBlocks(waitForBlocks + 1);312313 // execution #0314 expect((await helper.api!.query.testUtils.testValue()).toNumber())315 .to.be.equal(initTestVal + 1);316317 await helper.wait.newBlocks(periodic.period);318319 // execution #1320 expect((await helper.api!.query.testUtils.testValue()).toNumber())321 .to.be.equal(initTestVal + 2);322323 await helper.wait.newBlocks(periodic.period);324325 // <canceled>326 expect((await helper.api!.query.testUtils.testValue()).toNumber())327 .to.be.equal(initTestVal + 2);328 });329330 itSub.ifWithPallets('Root can cancel any scheduled operation', [Pallets.Scheduler], async ({helper}) => {331 const collection = await helper.nft.mintCollection(bob, {tokenPrefix: 'schd'});332 const token = await collection.mintToken(bob);333334 const scheduledId = await helper.arrange.makeScheduledId();335 const waitForBlocks = 4;336337 await token.scheduleAfter(scheduledId, waitForBlocks)338 .transfer(bob, {Substrate: alice.address});339340 await helper.getSudo().scheduler.cancelScheduled(alice, scheduledId);341342 await helper.wait.newBlocks(waitForBlocks + 1);343344 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});345 });346347 itSub.ifWithPallets('Root can set prioritized scheduled operation', [Pallets.Scheduler], async ({helper}) => {348 const scheduledId = await helper.arrange.makeScheduledId();349 const waitForBlocks = 4;350351 const amount = 42n * UNIQUE;352353 const balanceBefore = await helper.balance.getSubstrate(charlie.address);354355 await helper.getSudo()356 .scheduler.scheduleAfter(scheduledId, waitForBlocks, {priority: 42})357 .balance.forceTransferToSubstrate(alice, bob.address, charlie.address, amount);358359 await helper.wait.newBlocks(waitForBlocks + 1);360361 const balanceAfter = await helper.balance.getSubstrate(charlie.address);362363 expect(balanceAfter > balanceBefore).to.be.true;364365 const diff = balanceAfter - balanceBefore;366 expect(diff).to.be.equal(amount);367 });368369 itSub.ifWithPallets("Root can change scheduled operation's priority", [Pallets.Scheduler], async ({helper}) => {370 const collection = await helper.nft.mintCollection(bob, {tokenPrefix: 'schd'});371 const token = await collection.mintToken(bob);372373 const scheduledId = await helper.arrange.makeScheduledId();374 const waitForBlocks = 6;375376 await token.scheduleAfter(scheduledId, waitForBlocks)377 .transfer(bob, {Substrate: alice.address});378379 const priority = 112;380 await helper.getSudo().scheduler.changePriority(alice, scheduledId, priority);381382 const priorityChanged = await helper.wait.event(383 waitForBlocks,384 'scheduler',385 'PriorityChanged',386 );387388 expect(priorityChanged !== null).to.be.true;389 expect(priorityChanged!.event.data[2].toString()).to.be.equal(priority.toString());390 });391392 itSub.ifWithPallets('Prioritized operations executes in valid order', [Pallets.Scheduler], async ({helper}) => {393 const [394 scheduledFirstId,395 scheduledSecondId,396 ] = await helper.arrange.makeScheduledIds(2);397398 const currentBlockNumber = await helper.chain.getLatestBlockNumber();399 const blocksBeforeExecution = 4;400 const firstExecutionBlockNumber = currentBlockNumber + blocksBeforeExecution;401402 const prioHigh = 0;403 const prioLow = 255;404405 const periodic = {406 period: 6,407 repetitions: 2,408 };409410 const amount = 1n * UNIQUE;411412 // Scheduler a task with a lower priority first, then with a higher priority413 await helper.getSudo().scheduler.scheduleAt(scheduledFirstId, firstExecutionBlockNumber, {priority: prioLow, periodic})414 .balance.forceTransferToSubstrate(alice, alice.address, bob.address, amount);415416 await helper.getSudo().scheduler.scheduleAt(scheduledSecondId, firstExecutionBlockNumber, {priority: prioHigh, periodic})417 .balance.forceTransferToSubstrate(alice, alice.address, bob.address, amount);418419 const capture = await helper.arrange.captureEvents('scheduler', 'Dispatched');420421 await helper.wait.newBlocks(blocksBeforeExecution);422423 // Flip priorities424 await helper.getSudo().scheduler.changePriority(alice, scheduledFirstId, prioHigh);425 await helper.getSudo().scheduler.changePriority(alice, scheduledSecondId, prioLow);426427 await helper.wait.newBlocks(periodic.period);428429 const dispatchEvents = capture.extractCapturedEvents();430 expect(dispatchEvents.length).to.be.equal(4);431432 const dispatchedIds = dispatchEvents.map(r => r.event.data[1].toString());433434 const firstExecuctionIds = [dispatchedIds[0], dispatchedIds[1]];435 const secondExecuctionIds = [dispatchedIds[2], dispatchedIds[3]];436437 expect(firstExecuctionIds[0]).to.be.equal(scheduledSecondId);438 expect(firstExecuctionIds[1]).to.be.equal(scheduledFirstId);439440 expect(secondExecuctionIds[0]).to.be.equal(scheduledFirstId);441 expect(secondExecuctionIds[1]).to.be.equal(scheduledSecondId);442 });443});444445describe('Negative Test: Scheduling', () => {446 let alice: IKeyringPair;447 let bob: IKeyringPair;448449 before(async () => {450 await usingPlaygrounds(async (_, privateKeyWrapper) => {451 alice = privateKeyWrapper('//Alice');452 bob = privateKeyWrapper('//Bob');453 });454 });455456 itSub.ifWithPallets("Can't overwrite a scheduled ID", [Pallets.Scheduler], async ({helper}) => {457 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});458 const token = await collection.mintToken(alice);459460 const scheduledId = await helper.arrange.makeScheduledId();461 const waitForBlocks = 4;462463 await token.scheduleAfter(scheduledId, waitForBlocks)464 .transfer(alice, {Substrate: bob.address});465466 const scheduled = helper.scheduler.scheduleAfter(scheduledId, waitForBlocks);467 await expect(scheduled.balance.transferToSubstrate(alice, bob.address, 1n * UNIQUE))468 .to.be.rejectedWith(/scheduler\.FailedToSchedule/);469470 const bobsBalanceBefore = await helper.balance.getSubstrate(bob.address);471472 await helper.wait.newBlocks(waitForBlocks + 1);473474 const bobsBalanceAfter = await helper.balance.getSubstrate(bob.address);475476 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});477 expect(bobsBalanceBefore).to.be.equal(bobsBalanceAfter);478 });479480 itSub.ifWithPallets("Can't cancel an operation which is not scheduled", [Pallets.Scheduler], async ({helper}) => {481 const scheduledId = await helper.arrange.makeScheduledId();482 await expect(helper.scheduler.cancelScheduled(alice, scheduledId))483 .to.be.rejectedWith(/scheduler\.NotFound/);484 });485486 itSub.ifWithPallets("Can't cancel a non-owned scheduled operation", [Pallets.Scheduler], async ({helper}) => {487 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});488 const token = await collection.mintToken(alice);489490 const scheduledId = await helper.arrange.makeScheduledId();491 const waitForBlocks = 4;492493 await token.scheduleAfter(scheduledId, waitForBlocks)494 .transfer(alice, {Substrate: bob.address});495496 await expect(helper.scheduler.cancelScheduled(bob, scheduledId))497 .to.be.rejectedWith(/BadOrigin/);498499 await helper.wait.newBlocks(waitForBlocks + 1);500501 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});502 });503504 itSub.ifWithPallets("Regular user can't set prioritized scheduled operation", [Pallets.Scheduler], async ({helper}) => {505 const scheduledId = await helper.arrange.makeScheduledId();506 const waitForBlocks = 4;507508 const amount = 42n * UNIQUE;509510 const balanceBefore = await helper.balance.getSubstrate(bob.address);511512 const scheduled = helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {priority: 42});513 514 await expect(scheduled.balance.transferToSubstrate(alice, bob.address, amount))515 .to.be.rejectedWith(/BadOrigin/);516517 await helper.wait.newBlocks(waitForBlocks + 1);518519 const balanceAfter = await helper.balance.getSubstrate(bob.address);520521 expect(balanceAfter).to.be.equal(balanceBefore);522 });523524 itSub.ifWithPallets("Regular user can't change scheduled operation's priority", [Pallets.Scheduler], async ({helper}) => {525 const collection = await helper.nft.mintCollection(bob, {tokenPrefix: 'schd'});526 const token = await collection.mintToken(bob);527528 const scheduledId = await helper.arrange.makeScheduledId();529 const waitForBlocks = 4;530531 await token.scheduleAfter(scheduledId, waitForBlocks)532 .transfer(bob, {Substrate: alice.address});533534 const priority = 112;535 await expect(helper.scheduler.changePriority(alice, scheduledId, priority))536 .to.be.rejectedWith(/BadOrigin/);537538 const priorityChanged = await helper.wait.event(539 waitForBlocks,540 'scheduler',541 'PriorityChanged',542 );543544 expect(priorityChanged === null).to.be.true;545 });546});547548// Implementation of the functionality tested here was postponed/shelved549describe.skip('Sponsoring scheduling', () => {550 // let alice: IKeyringPair;551 // let bob: IKeyringPair;552553 // before(async() => {554 // await usingApi(async (_, privateKeyWrapper) => {555 // alice = privateKeyWrapper('//Alice');556 // bob = privateKeyWrapper('//Bob');557 // });558 // });559560 it('Can sponsor scheduling a transaction', async () => {561 // const collectionId = await createCollectionExpectSuccess();562 // await setCollectionSponsorExpectSuccess(collectionId, bob.address);563 // await confirmSponsorshipExpectSuccess(collectionId, '//Bob');564565 // await usingApi(async api => {566 // const scheduledId = await makeScheduledId();567 // const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address);568569 // const bobBalanceBefore = await getFreeBalance(bob);570 // const waitForBlocks = 4;571 // // no need to wait to check, fees must be deducted on scheduling, immediately572 // await scheduleTransferExpectSuccess(api, collectionId, tokenId, alice, bob, 0, waitForBlocks, scheduledId);573 // const bobBalanceAfter = await getFreeBalance(bob);574 // // expect(aliceBalanceAfter == aliceBalanceBefore).to.be.true;575 // expect(bobBalanceAfter < bobBalanceBefore).to.be.true;576 // // wait for sequentiality matters577 // await waitNewBlocks(waitForBlocks - 1);578 // });579 });580581 it('Schedules and dispatches a transaction even if the caller has no funds at the time of the dispatch', async () => {582 // await usingApi(async (api, privateKeyWrapper) => {583 // // Find an empty, unused account584 // const zeroBalance = await findUnusedAddress(api, privateKeyWrapper);585586 // const collectionId = await createCollectionExpectSuccess();587588 // // Add zeroBalance address to allow list589 // await enablePublicMintingExpectSuccess(alice, collectionId);590 // await addToAllowListExpectSuccess(alice, collectionId, zeroBalance.address);591592 // // Grace zeroBalance with money, enough to cover future transactions593 // const balanceTx = api.tx.balances.transfer(zeroBalance.address, 1n * UNIQUE);594 // await submitTransactionAsync(alice, balanceTx);595596 // // Mint a fresh NFT597 // const tokenId = await createItemExpectSuccess(zeroBalance, collectionId, 'NFT');598 // const scheduledId = await makeScheduledId();599600 // // Schedule transfer of the NFT a few blocks ahead601 // const waitForBlocks = 5;602 // await scheduleTransferExpectSuccess(api, collectionId, tokenId, zeroBalance, alice, 1, waitForBlocks, scheduledId);603604 // // Get rid of the account's funds before the scheduled transaction takes place605 // const balanceTx2 = api.tx.balances.transfer(alice.address, UNIQUE * 68n / 100n);606 // const events = await submitTransactionAsync(zeroBalance, balanceTx2);607 // expect(getGenericResult(events).success).to.be.true;608 // /*const emptyBalanceTx = api.tx.balances.setBalance(zeroBalance.address, 0, 0); // do not null reserved?609 // const sudoTx = api.tx.sudo.sudo(emptyBalanceTx as any);610 // const events = await submitTransactionAsync(alice, sudoTx);611 // expect(getGenericResult(events).success).to.be.true;*/612613 // // Wait for a certain number of blocks, discarding the ones that already happened while accepting the late transactions614 // await waitNewBlocks(waitForBlocks - 3);615616 // expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(alice.address));617 // });618 });619620 it('Sponsor going bankrupt does not impact a scheduled transaction', async () => {621 // const collectionId = await createCollectionExpectSuccess();622623 // await usingApi(async (api, privateKeyWrapper) => {624 // const zeroBalance = await findUnusedAddress(api, privateKeyWrapper);625 // const balanceTx = api.tx.balances.transfer(zeroBalance.address, 1n * UNIQUE);626 // await submitTransactionAsync(alice, balanceTx);627628 // await setCollectionSponsorExpectSuccess(collectionId, zeroBalance.address);629 // await confirmSponsorshipByKeyExpectSuccess(collectionId, zeroBalance);630631 // const scheduledId = await makeScheduledId();632 // const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address);633634 // const waitForBlocks = 5;635 // await scheduleTransferExpectSuccess(api, collectionId, tokenId, alice, zeroBalance, 1, waitForBlocks, scheduledId);636637 // const emptyBalanceSponsorTx = api.tx.balances.setBalance(zeroBalance.address, 0, 0);638 // const sudoTx = api.tx.sudo.sudo(emptyBalanceSponsorTx as any);639 // const events = await submitTransactionAsync(alice, sudoTx);640 // expect(getGenericResult(events).success).to.be.true;641642 // // Wait for a certain number of blocks, save for the ones that already happened while accepting the late transactions643 // await waitNewBlocks(waitForBlocks - 3);644645 // expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(zeroBalance.address));646 // });647 });648649 it('Exceeding sponsor rate limit without having enough funds prevents scheduling a periodic transaction', async () => {650 // const collectionId = await createCollectionExpectSuccess();651 // await setCollectionSponsorExpectSuccess(collectionId, bob.address);652 // await confirmSponsorshipExpectSuccess(collectionId, '//Bob');653654 // await usingApi(async (api, privateKeyWrapper) => {655 // const zeroBalance = await findUnusedAddress(api, privateKeyWrapper);656657 // await enablePublicMintingExpectSuccess(alice, collectionId);658 // await addToAllowListExpectSuccess(alice, collectionId, zeroBalance.address);659660 // const bobBalanceBefore = await getFreeBalance(bob);661662 // const createData = {nft: {const_data: [], variable_data: []}};663 // const creationTx = api.tx.unique.createItem(collectionId, normalizeAccountId(zeroBalance), createData as any);664 // const scheduledId = await makeScheduledId();665666 // /*const badTransaction = async function () {667 // await submitTransactionExpectFailAsync(zeroBalance, zeroToAlice);668 // };669 // await expect(badTransaction()).to.be.rejectedWith('Inability to pay some fees');*/670671 // await expect(scheduleAfter(api, creationTx, zeroBalance, 3, scheduledId, 1, 3)).to.be.rejectedWith(/Inability to pay some fees/);672673 // expect(await getFreeBalance(bob)).to.be.equal(bobBalanceBefore);674 // });675 });676});