difftreelog
fix scheduler doesn't change nonce
in: master
2 files changed
runtime/common/scheduler.rsdiffbeforeafterboth--- a/runtime/common/scheduler.rs
+++ b/runtime/common/scheduler.rs
@@ -32,10 +32,6 @@
/// The SignedExtension to the basic transaction logic.
pub type SignedExtraScheduler = (
- frame_system::CheckSpecVersion<Runtime>,
- frame_system::CheckGenesis<Runtime>,
- frame_system::CheckEra<Runtime>,
- frame_system::CheckNonce<Runtime>,
frame_system::CheckWeight<Runtime>,
maintenance::CheckMaintenance,
ChargeTransactionPayment<Runtime>,
@@ -43,12 +39,6 @@
fn get_signed_extras(from: <Runtime as frame_system::Config>::AccountId) -> SignedExtraScheduler {
(
- frame_system::CheckSpecVersion::<Runtime>::new(),
- frame_system::CheckGenesis::<Runtime>::new(),
- frame_system::CheckEra::<Runtime>::from(Era::Immortal),
- frame_system::CheckNonce::<Runtime>::from(frame_system::Pallet::<Runtime>::account_nonce(
- from,
- )),
frame_system::CheckWeight::<Runtime>::new(),
maintenance::CheckMaintenance,
ChargeTransactionPayment::<Runtime>::from(0),
tests/src/scheduler.seqtest.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 {expect, itSub, Pallets, requirePalletsOrSkip, usingPlaygrounds} from './util';18import {IKeyringPair} from '@polkadot/types/types';19import {DevUniqueHelper} from './util/playgrounds/unique.dev';2021describe('Scheduling token and balance transfers', () => {22 let superuser: IKeyringPair;23 let alice: IKeyringPair;24 let bob: IKeyringPair;25 let charlie: IKeyringPair;2627 before(async function() {28 await usingPlaygrounds(async (helper, privateKey) => {29 requirePalletsOrSkip(this, helper, [Pallets.Scheduler]);3031 superuser = await privateKey('//Alice');32 const donor = await privateKey({filename: __filename});33 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);3435 await helper.testUtils.enable();36 });37 });3839 itSub('Can delay a transfer of an owned token', async ({helper}) => {40 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});41 const token = await collection.mintToken(alice);42 const schedulerId = await helper.arrange.makeScheduledId();43 const blocksBeforeExecution = 4;4445 await token.scheduleAfter(schedulerId, blocksBeforeExecution)46 .transfer(alice, {Substrate: bob.address});4748 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});4950 await helper.wait.newBlocks(blocksBeforeExecution + 1);5152 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});53 });5455 itSub('Can transfer funds periodically', async ({helper}) => {56 const scheduledId = await helper.arrange.makeScheduledId();57 const waitForBlocks = 1;5859 const amount = 1n * helper.balance.getOneTokenNominal();60 const periodic = {61 period: 2,62 repetitions: 2,63 };6465 const bobsBalanceBefore = await helper.balance.getSubstrate(bob.address);6667 await helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {periodic})68 .balance.transferToSubstrate(alice, bob.address, amount);6970 await helper.wait.newBlocks(waitForBlocks + 1);7172 const bobsBalanceAfterFirst = await helper.balance.getSubstrate(bob.address);73 expect(bobsBalanceAfterFirst)74 .to.be.equal(75 bobsBalanceBefore + 1n * amount,76 '#1 Balance of the recipient should be increased by 1 * amount',77 );7879 await helper.wait.newBlocks(periodic.period);8081 const bobsBalanceAfterSecond = await helper.balance.getSubstrate(bob.address);82 expect(bobsBalanceAfterSecond)83 .to.be.equal(84 bobsBalanceBefore + 2n * amount,85 '#2 Balance of the recipient should be increased by 2 * amount',86 );87 });8889 itSub('Can cancel a scheduled operation which has not yet taken effect', async ({helper}) => {90 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});91 const token = await collection.mintToken(alice);9293 const scheduledId = await helper.arrange.makeScheduledId();94 const waitForBlocks = 4;9596 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});9798 await token.scheduleAfter(scheduledId, waitForBlocks)99 .transfer(alice, {Substrate: bob.address});100101 await helper.scheduler.cancelScheduled(alice, scheduledId);102103 await helper.wait.newBlocks(waitForBlocks + 1);104105 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});106 });107108 itSub('Can cancel a periodic operation (transfer of funds)', async ({helper}) => {109 const waitForBlocks = 1;110 const periodic = {111 period: 3,112 repetitions: 2,113 };114115 const scheduledId = await helper.arrange.makeScheduledId();116117 const amount = 1n * helper.balance.getOneTokenNominal();118119 const bobsBalanceBefore = await helper.balance.getSubstrate(bob.address);120121 await helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {periodic})122 .balance.transferToSubstrate(alice, bob.address, amount);123124 await helper.wait.newBlocks(waitForBlocks + 1);125126 const bobsBalanceAfterFirst = await helper.balance.getSubstrate(bob.address);127128 expect(bobsBalanceAfterFirst)129 .to.be.equal(130 bobsBalanceBefore + 1n * amount,131 '#1 Balance of the recipient should be increased by 1 * amount',132 );133134 await helper.scheduler.cancelScheduled(alice, scheduledId);135 await helper.wait.newBlocks(periodic.period);136137 const bobsBalanceAfterSecond = await helper.balance.getSubstrate(bob.address);138 expect(bobsBalanceAfterSecond)139 .to.be.equal(140 bobsBalanceAfterFirst,141 '#2 Balance of the recipient should not be changed',142 );143 });144145 itSub.ifWithPallets('Scheduled tasks are transactional', [Pallets.TestUtils], async ({helper}) => {146 const scheduledId = await helper.arrange.makeScheduledId();147 const waitForBlocks = 4;148149 const initTestVal = 42;150 const changedTestVal = 111;151152 await helper.testUtils.setTestValue(alice, initTestVal);153154 await helper.scheduler.scheduleAfter<DevUniqueHelper>(scheduledId, waitForBlocks)155 .testUtils.setTestValueAndRollback(alice, changedTestVal);156157 await helper.wait.newBlocks(waitForBlocks + 1);158159 const testVal = await helper.testUtils.testValue();160 expect(testVal, 'The test value should NOT be commited')161 .to.be.equal(initTestVal);162 });163164 itSub.ifWithPallets('Scheduled tasks should take correct fees', [Pallets.TestUtils], async function({helper}) {165 const scheduledId = await helper.arrange.makeScheduledId();166 const waitForBlocks = 4;167 const periodic = {168 period: 2,169 repetitions: 2,170 };171172 const dummyTx = helper.constructApiCall('api.tx.testUtils.justTakeFee', []);173 const scheduledLen = dummyTx.callIndex.length;174175 const expectedScheduledFee = (await helper.getPaymentInfo(alice, dummyTx, scheduledLen))176 .partialFee.toBigInt();177178 await helper.scheduler.scheduleAfter<DevUniqueHelper>(scheduledId, waitForBlocks, {periodic})179 .testUtils.justTakeFee(alice);180181 await helper.wait.newBlocks(1);182183 const aliceInitBalance = await helper.balance.getSubstrate(alice.address);184 let diff;185186 await helper.wait.newBlocks(waitForBlocks);187188 const aliceBalanceAfterFirst = await helper.balance.getSubstrate(alice.address);189 expect(190 aliceBalanceAfterFirst < aliceInitBalance,191 '[after execution #1] Scheduled task should take a fee',192 ).to.be.true;193194 diff = aliceInitBalance - aliceBalanceAfterFirst;195 expect(diff).to.be.equal(196 expectedScheduledFee,197 'Scheduled task should take the right amount of fees',198 );199200 await helper.wait.newBlocks(periodic.period);201202 const aliceBalanceAfterSecond = await helper.balance.getSubstrate(alice.address);203 expect(204 aliceBalanceAfterSecond < aliceBalanceAfterFirst,205 '[after execution #2] Scheduled task should take a fee',206 ).to.be.true;207208 diff = aliceBalanceAfterFirst - aliceBalanceAfterSecond;209 expect(diff).to.be.equal(210 expectedScheduledFee,211 'Scheduled task should take the right amount of fees',212 );213 });214215 // Check if we can cancel a scheduled periodic operation216 // in the same block in which it is running217 itSub.ifWithPallets('Can cancel the periodic sheduled tx when the tx is running', [Pallets.TestUtils], async ({helper}) => {218 const currentBlockNumber = await helper.chain.getLatestBlockNumber();219 const blocksBeforeExecution = 10;220 const firstExecutionBlockNumber = currentBlockNumber + blocksBeforeExecution;221222 const [223 scheduledId,224 scheduledCancelId,225 ] = await helper.arrange.makeScheduledIds(2);226227 const periodic = {228 period: 5,229 repetitions: 5,230 };231232 const initTestVal = 0;233 const incTestVal = initTestVal + 1;234 const finalTestVal = initTestVal + 2;235236 await helper.testUtils.setTestValue(alice, initTestVal);237238 await helper.scheduler.scheduleAt<DevUniqueHelper>(scheduledId, firstExecutionBlockNumber, {periodic})239 .testUtils.incTestValue(alice);240241 // Cancel the inc tx after 2 executions242 // *in the same block* in which the second execution is scheduled243 await helper.scheduler.scheduleAt(244 scheduledCancelId,245 firstExecutionBlockNumber + periodic.period,246 ).scheduler.cancelScheduled(alice, scheduledId);247248 await helper.wait.forParachainBlockNumber(firstExecutionBlockNumber);249250 // execution #0251 expect(await helper.testUtils.testValue())252 .to.be.equal(incTestVal);253254 await helper.wait.forParachainBlockNumber(firstExecutionBlockNumber + periodic.period);255256 // execution #1257 expect(await helper.testUtils.testValue())258 .to.be.equal(finalTestVal);259260 for (let i = 1; i < periodic.repetitions; i++) {261 await helper.wait.forParachainBlockNumber(firstExecutionBlockNumber + periodic.period * (i + 1));262 expect(await helper.testUtils.testValue())263 .to.be.equal(finalTestVal);264 }265 });266267 itSub.ifWithPallets('A scheduled operation can cancel itself', [Pallets.TestUtils], async ({helper}) => {268 const scheduledId = await helper.arrange.makeScheduledId();269 const waitForBlocks = 4;270 const periodic = {271 period: 2,272 repetitions: 5,273 };274275 const initTestVal = 0;276 const maxTestVal = 2;277278 await helper.testUtils.setTestValue(alice, initTestVal);279280 await helper.scheduler.scheduleAfter<DevUniqueHelper>(scheduledId, waitForBlocks, {periodic})281 .testUtils.selfCancelingInc(alice, scheduledId, maxTestVal);282283 await helper.wait.newBlocks(waitForBlocks + 1);284285 // execution #0286 expect(await helper.testUtils.testValue())287 .to.be.equal(initTestVal + 1);288289 await helper.wait.newBlocks(periodic.period);290291 // execution #1292 expect(await helper.testUtils.testValue())293 .to.be.equal(initTestVal + 2);294295 await helper.wait.newBlocks(periodic.period);296297 // <canceled>298 expect(await helper.testUtils.testValue())299 .to.be.equal(initTestVal + 2);300 });301302 itSub('Root can cancel any scheduled operation', async ({helper}) => {303 const collection = await helper.nft.mintCollection(bob, {tokenPrefix: 'schd'});304 const token = await collection.mintToken(bob);305306 const scheduledId = await helper.arrange.makeScheduledId();307 const waitForBlocks = 4;308309 await token.scheduleAfter(scheduledId, waitForBlocks)310 .transfer(bob, {Substrate: alice.address});311312 await helper.getSudo().scheduler.cancelScheduled(superuser, scheduledId);313314 await helper.wait.newBlocks(waitForBlocks + 1);315316 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});317 });318319 itSub('Root can set prioritized scheduled operation', async ({helper}) => {320 const scheduledId = await helper.arrange.makeScheduledId();321 const waitForBlocks = 4;322323 const amount = 42n * helper.balance.getOneTokenNominal();324325 const balanceBefore = await helper.balance.getSubstrate(charlie.address);326327 await helper.getSudo()328 .scheduler.scheduleAfter(scheduledId, waitForBlocks, {priority: 42})329 .balance.forceTransferToSubstrate(superuser, bob.address, charlie.address, amount);330331 await helper.wait.newBlocks(waitForBlocks + 1);332333 const balanceAfter = await helper.balance.getSubstrate(charlie.address);334335 expect(balanceAfter > balanceBefore).to.be.true;336337 const diff = balanceAfter - balanceBefore;338 expect(diff).to.be.equal(amount);339 });340341 itSub("Root can change scheduled operation's priority", async ({helper}) => {342 const collection = await helper.nft.mintCollection(bob, {tokenPrefix: 'schd'});343 const token = await collection.mintToken(bob);344345 const scheduledId = await helper.arrange.makeScheduledId();346 const waitForBlocks = 6;347348 await token.scheduleAfter(scheduledId, waitForBlocks)349 .transfer(bob, {Substrate: alice.address});350351 const priority = 112;352 await helper.getSudo().scheduler.changePriority(superuser, scheduledId, priority);353354 const priorityChanged = await helper.wait.event(355 waitForBlocks,356 'scheduler',357 'PriorityChanged',358 );359360 expect(priorityChanged !== null).to.be.true;361 expect(priorityChanged!.event.data[2].toString()).to.be.equal(priority.toString());362 });363364 itSub('Prioritized operations execute in valid order', async ({helper}) => {365 const [366 scheduledFirstId,367 scheduledSecondId,368 ] = await helper.arrange.makeScheduledIds(2);369370 const currentBlockNumber = await helper.chain.getLatestBlockNumber();371 const blocksBeforeExecution = 6;372 const firstExecutionBlockNumber = currentBlockNumber + blocksBeforeExecution;373374 const prioHigh = 0;375 const prioLow = 255;376377 const periodic = {378 period: 6,379 repetitions: 2,380 };381382 const amount = 1n * helper.balance.getOneTokenNominal();383384 // Scheduler a task with a lower priority first, then with a higher priority385 await helper.getSudo().scheduler.scheduleAt(scheduledFirstId, firstExecutionBlockNumber, {priority: prioLow, periodic})386 .balance.forceTransferToSubstrate(superuser, alice.address, bob.address, amount);387388 await helper.getSudo().scheduler.scheduleAt(scheduledSecondId, firstExecutionBlockNumber, {priority: prioHigh, periodic})389 .balance.forceTransferToSubstrate(superuser, alice.address, bob.address, amount);390391 const capture = await helper.arrange.captureEvents('scheduler', 'Dispatched');392393 await helper.wait.forParachainBlockNumber(firstExecutionBlockNumber);394395 // Flip priorities396 await helper.getSudo().scheduler.changePriority(superuser, scheduledFirstId, prioHigh);397 await helper.getSudo().scheduler.changePriority(superuser, scheduledSecondId, prioLow);398399 await helper.wait.forParachainBlockNumber(firstExecutionBlockNumber + periodic.period);400401 const dispatchEvents = capture.extractCapturedEvents();402 expect(dispatchEvents.length).to.be.equal(4);403404 const dispatchedIds = dispatchEvents.map(r => r.event.data[1].toString());405406 const firstExecuctionIds = [dispatchedIds[0], dispatchedIds[1]];407 const secondExecuctionIds = [dispatchedIds[2], dispatchedIds[3]];408409 expect(firstExecuctionIds[0]).to.be.equal(scheduledSecondId);410 expect(firstExecuctionIds[1]).to.be.equal(scheduledFirstId);411412 expect(secondExecuctionIds[0]).to.be.equal(scheduledFirstId);413 expect(secondExecuctionIds[1]).to.be.equal(scheduledSecondId);414 });415416 itSub('Periodic operations always can be rescheduled', async ({helper}) => {417 const maxScheduledPerBlock = 50;418 const numFilledBlocks = 3;419 const ids = await helper.arrange.makeScheduledIds(numFilledBlocks * maxScheduledPerBlock + 1);420 const periodicId = ids[0];421 const fillIds = ids.slice(1);422423 const initTestVal = 0;424 const firstExecTestVal = 1;425 const secondExecTestVal = 2;426 await helper.testUtils.setTestValue(alice, initTestVal);427428 const currentBlockNumber = await helper.chain.getLatestBlockNumber();429 const blocksBeforeExecution = 8;430 const firstExecutionBlockNumber = currentBlockNumber + blocksBeforeExecution;431432 const period = 5;433434 const periodic = {435 period,436 repetitions: 2,437 };438439 // Fill `numFilledBlocks` blocks beginning from the block in which the second execution should occur440 const txs = [];441 for (let offset = 0; offset < numFilledBlocks; offset ++) {442 for (let i = 0; i < maxScheduledPerBlock; i++) {443444 const scheduledTx = helper.constructApiCall('api.tx.balances.transfer', [bob.address, 1n]);445446 const when = firstExecutionBlockNumber + period + offset;447 const tx = helper.constructApiCall('api.tx.scheduler.scheduleNamed', [fillIds[i + offset * maxScheduledPerBlock], when, null, null, scheduledTx]);448449 txs.push(tx);450 }451 }452 await helper.executeExtrinsic(alice, 'api.tx.testUtils.batchAll', [txs], true);453454 await helper.scheduler.scheduleAt<DevUniqueHelper>(periodicId, firstExecutionBlockNumber, {periodic})455 .testUtils.incTestValue(alice);456457 await helper.wait.newBlocks(blocksBeforeExecution);458 expect(await helper.testUtils.testValue()).to.be.equal(firstExecTestVal);459460 await helper.wait.newBlocks(period + numFilledBlocks);461462 // The periodic operation should be postponed by `numFilledBlocks`463 for (let i = 0; i < numFilledBlocks; i++) {464 expect(await helper.testUtils.testValue(firstExecutionBlockNumber + period + i)).to.be.equal(firstExecTestVal);465 }466467 // After the `numFilledBlocks` the periodic operation will eventually be executed468 expect(await helper.testUtils.testValue()).to.be.equal(secondExecTestVal);469 });470});471472describe('Negative Test: Scheduling', () => {473 let alice: IKeyringPair;474 let bob: IKeyringPair;475476 before(async function() {477 await usingPlaygrounds(async (helper, privateKey) => {478 requirePalletsOrSkip(this, helper, [Pallets.Scheduler]);479480 const donor = await privateKey({filename: __filename});481 [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);482483 await helper.testUtils.enable();484 });485 });486487 itSub("Can't overwrite a scheduled ID", async ({helper}) => {488 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});489 const token = await collection.mintToken(alice);490491 const scheduledId = await helper.arrange.makeScheduledId();492 const waitForBlocks = 4;493494 await token.scheduleAfter(scheduledId, waitForBlocks)495 .transfer(alice, {Substrate: bob.address});496497 const scheduled = helper.scheduler.scheduleAfter(scheduledId, waitForBlocks);498 await expect(scheduled.balance.transferToSubstrate(alice, bob.address, 1n * helper.balance.getOneTokenNominal()))499 .to.be.rejectedWith(/scheduler\.FailedToSchedule/);500501 const bobsBalanceBefore = await helper.balance.getSubstrate(bob.address);502503 await helper.wait.newBlocks(waitForBlocks + 1);504505 const bobsBalanceAfter = await helper.balance.getSubstrate(bob.address);506507 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});508 expect(bobsBalanceBefore).to.be.equal(bobsBalanceAfter);509 });510511 itSub("Can't cancel an operation which is not scheduled", async ({helper}) => {512 const scheduledId = await helper.arrange.makeScheduledId();513 await expect(helper.scheduler.cancelScheduled(alice, scheduledId))514 .to.be.rejectedWith(/scheduler\.NotFound/);515 });516517 itSub("Can't cancel a non-owned scheduled operation", async ({helper}) => {518 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});519 const token = await collection.mintToken(alice);520521 const scheduledId = await helper.arrange.makeScheduledId();522 const waitForBlocks = 4;523524 await token.scheduleAfter(scheduledId, waitForBlocks)525 .transfer(alice, {Substrate: bob.address});526527 await expect(helper.scheduler.cancelScheduled(bob, scheduledId))528 .to.be.rejectedWith(/BadOrigin/);529530 await helper.wait.newBlocks(waitForBlocks + 1);531532 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});533 });534535 itSub("Regular user can't set prioritized scheduled operation", async ({helper}) => {536 const scheduledId = await helper.arrange.makeScheduledId();537 const waitForBlocks = 4;538539 const amount = 42n * helper.balance.getOneTokenNominal();540541 const balanceBefore = await helper.balance.getSubstrate(bob.address);542543 const scheduled = helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {priority: 42});544 545 await expect(scheduled.balance.transferToSubstrate(alice, bob.address, amount))546 .to.be.rejectedWith(/BadOrigin/);547548 await helper.wait.newBlocks(waitForBlocks + 1);549550 const balanceAfter = await helper.balance.getSubstrate(bob.address);551552 expect(balanceAfter).to.be.equal(balanceBefore);553 });554555 itSub("Regular user can't change scheduled operation's priority", async ({helper}) => {556 const collection = await helper.nft.mintCollection(bob, {tokenPrefix: 'schd'});557 const token = await collection.mintToken(bob);558559 const scheduledId = await helper.arrange.makeScheduledId();560 const waitForBlocks = 4;561562 await token.scheduleAfter(scheduledId, waitForBlocks)563 .transfer(bob, {Substrate: alice.address});564565 const priority = 112;566 await expect(helper.scheduler.changePriority(alice, scheduledId, priority))567 .to.be.rejectedWith(/BadOrigin/);568569 const priorityChanged = await helper.wait.event(570 waitForBlocks,571 'scheduler',572 'PriorityChanged',573 );574575 expect(priorityChanged === null).to.be.true;576 });577});578579// Implementation of the functionality tested here was postponed/shelved580describe.skip('Sponsoring scheduling', () => {581 // let alice: IKeyringPair;582 // let bob: IKeyringPair;583584 // before(async() => {585 // await usingApi(async (_, privateKey) => {586 // alice = privateKey('//Alice');587 // bob = privateKey('//Bob');588 // });589 // });590591 it('Can sponsor scheduling a transaction', async () => {592 // const collectionId = await createCollectionExpectSuccess();593 // await setCollectionSponsorExpectSuccess(collectionId, bob.address);594 // await confirmSponsorshipExpectSuccess(collectionId, '//Bob');595596 // await usingApi(async api => {597 // const scheduledId = await makeScheduledId();598 // const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address);599600 // const bobBalanceBefore = await getFreeBalance(bob);601 // const waitForBlocks = 4;602 // // no need to wait to check, fees must be deducted on scheduling, immediately603 // await scheduleTransferExpectSuccess(api, collectionId, tokenId, alice, bob, 0, waitForBlocks, scheduledId);604 // const bobBalanceAfter = await getFreeBalance(bob);605 // // expect(aliceBalanceAfter == aliceBalanceBefore).to.be.true;606 // expect(bobBalanceAfter < bobBalanceBefore).to.be.true;607 // // wait for sequentiality matters608 // await waitNewBlocks(waitForBlocks - 1);609 // });610 });611612 it('Schedules and dispatches a transaction even if the caller has no funds at the time of the dispatch', async () => {613 // await usingApi(async (api, privateKey) => {614 // // Find an empty, unused account615 // const zeroBalance = await findUnusedAddress(api, privateKey);616617 // const collectionId = await createCollectionExpectSuccess();618619 // // Add zeroBalance address to allow list620 // await enablePublicMintingExpectSuccess(alice, collectionId);621 // await addToAllowListExpectSuccess(alice, collectionId, zeroBalance.address);622623 // // Grace zeroBalance with money, enough to cover future transactions624 // const balanceTx = api.tx.balances.transfer(zeroBalance.address, 1n * UNIQUE);625 // await submitTransactionAsync(alice, balanceTx);626627 // // Mint a fresh NFT628 // const tokenId = await createItemExpectSuccess(zeroBalance, collectionId, 'NFT');629 // const scheduledId = await makeScheduledId();630631 // // Schedule transfer of the NFT a few blocks ahead632 // const waitForBlocks = 5;633 // await scheduleTransferExpectSuccess(api, collectionId, tokenId, zeroBalance, alice, 1, waitForBlocks, scheduledId);634635 // // Get rid of the account's funds before the scheduled transaction takes place636 // const balanceTx2 = api.tx.balances.transfer(alice.address, UNIQUE * 68n / 100n);637 // const events = await submitTransactionAsync(zeroBalance, balanceTx2);638 // expect(getGenericResult(events).success).to.be.true;639 // /*const emptyBalanceTx = api.tx.balances.setBalance(zeroBalance.address, 0, 0); // do not null reserved?640 // const sudoTx = api.tx.sudo.sudo(emptyBalanceTx as any);641 // const events = await submitTransactionAsync(alice, sudoTx);642 // expect(getGenericResult(events).success).to.be.true;*/643644 // // Wait for a certain number of blocks, discarding the ones that already happened while accepting the late transactions645 // await waitNewBlocks(waitForBlocks - 3);646647 // expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(alice.address));648 // });649 });650651 it('Sponsor going bankrupt does not impact a scheduled transaction', async () => {652 // const collectionId = await createCollectionExpectSuccess();653654 // await usingApi(async (api, privateKey) => {655 // const zeroBalance = await findUnusedAddress(api, privateKey);656 // const balanceTx = api.tx.balances.transfer(zeroBalance.address, 1n * UNIQUE);657 // await submitTransactionAsync(alice, balanceTx);658659 // await setCollectionSponsorExpectSuccess(collectionId, zeroBalance.address);660 // await confirmSponsorshipByKeyExpectSuccess(collectionId, zeroBalance);661662 // const scheduledId = await makeScheduledId();663 // const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address);664665 // const waitForBlocks = 5;666 // await scheduleTransferExpectSuccess(api, collectionId, tokenId, alice, zeroBalance, 1, waitForBlocks, scheduledId);667668 // const emptyBalanceSponsorTx = api.tx.balances.setBalance(zeroBalance.address, 0, 0);669 // const sudoTx = api.tx.sudo.sudo(emptyBalanceSponsorTx as any);670 // const events = await submitTransactionAsync(alice, sudoTx);671 // expect(getGenericResult(events).success).to.be.true;672673 // // Wait for a certain number of blocks, save for the ones that already happened while accepting the late transactions674 // await waitNewBlocks(waitForBlocks - 3);675676 // expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(zeroBalance.address));677 // });678 });679680 it('Exceeding sponsor rate limit without having enough funds prevents scheduling a periodic transaction', async () => {681 // const collectionId = await createCollectionExpectSuccess();682 // await setCollectionSponsorExpectSuccess(collectionId, bob.address);683 // await confirmSponsorshipExpectSuccess(collectionId, '//Bob');684685 // await usingApi(async (api, privateKey) => {686 // const zeroBalance = await findUnusedAddress(api, privateKey);687688 // await enablePublicMintingExpectSuccess(alice, collectionId);689 // await addToAllowListExpectSuccess(alice, collectionId, zeroBalance.address);690691 // const bobBalanceBefore = await getFreeBalance(bob);692693 // const createData = {nft: {const_data: [], variable_data: []}};694 // const creationTx = api.tx.unique.createItem(collectionId, normalizeAccountId(zeroBalance), createData as any);695 // const scheduledId = await makeScheduledId();696697 // /*const badTransaction = async function () {698 // await submitTransactionExpectFailAsync(zeroBalance, zeroToAlice);699 // };700 // await expect(badTransaction()).to.be.rejectedWith('Inability to pay some fees');*/701702 // await expect(scheduleAfter(api, creationTx, zeroBalance, 3, scheduledId, 1, 3)).to.be.rejectedWith(/Inability to pay some fees/);703704 // expect(await getFreeBalance(bob)).to.be.equal(bobBalanceBefore);705 // });706 });707});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 {expect, itSub, Pallets, requirePalletsOrSkip, usingPlaygrounds} from './util';18import {IKeyringPair} from '@polkadot/types/types';19import {DevUniqueHelper} from './util/playgrounds/unique.dev';2021describe('Scheduling token and balance transfers', () => {22 let superuser: IKeyringPair;23 let alice: IKeyringPair;24 let bob: IKeyringPair;25 let charlie: IKeyringPair;2627 before(async function() {28 await usingPlaygrounds(async (helper, privateKey) => {29 requirePalletsOrSkip(this, helper, [Pallets.Scheduler]);3031 superuser = await privateKey('//Alice');32 const donor = await privateKey({filename: __filename});33 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);3435 await helper.testUtils.enable();36 });37 });3839 itSub('Can delay a transfer of an owned token', async ({helper}) => {40 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});41 const token = await collection.mintToken(alice);42 const schedulerId = await helper.arrange.makeScheduledId();43 const blocksBeforeExecution = 4;4445 await token.scheduleAfter(schedulerId, blocksBeforeExecution)46 .transfer(alice, {Substrate: bob.address});4748 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});4950 await helper.wait.newBlocks(blocksBeforeExecution + 1);5152 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});53 });5455 itSub('Can transfer funds periodically', async ({helper}) => {56 const scheduledId = await helper.arrange.makeScheduledId();57 const waitForBlocks = 1;5859 const amount = 1n * helper.balance.getOneTokenNominal();60 const periodic = {61 period: 2,62 repetitions: 2,63 };6465 const bobsBalanceBefore = await helper.balance.getSubstrate(bob.address);6667 await helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {periodic})68 .balance.transferToSubstrate(alice, bob.address, amount);6970 await helper.wait.newBlocks(waitForBlocks + 1);7172 const bobsBalanceAfterFirst = await helper.balance.getSubstrate(bob.address);73 expect(bobsBalanceAfterFirst)74 .to.be.equal(75 bobsBalanceBefore + 1n * amount,76 '#1 Balance of the recipient should be increased by 1 * amount',77 );7879 await helper.wait.newBlocks(periodic.period);8081 const bobsBalanceAfterSecond = await helper.balance.getSubstrate(bob.address);82 expect(bobsBalanceAfterSecond)83 .to.be.equal(84 bobsBalanceBefore + 2n * amount,85 '#2 Balance of the recipient should be increased by 2 * amount',86 );87 });8889 itSub('Can cancel a scheduled operation which has not yet taken effect', async ({helper}) => {90 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});91 const token = await collection.mintToken(alice);9293 const scheduledId = await helper.arrange.makeScheduledId();94 const waitForBlocks = 4;9596 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});9798 await token.scheduleAfter(scheduledId, waitForBlocks)99 .transfer(alice, {Substrate: bob.address});100101 await helper.scheduler.cancelScheduled(alice, scheduledId);102103 await helper.wait.newBlocks(waitForBlocks + 1);104105 expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});106 });107108 itSub('Can cancel a periodic operation (transfer of funds)', async ({helper}) => {109 const waitForBlocks = 1;110 const periodic = {111 period: 3,112 repetitions: 2,113 };114115 const scheduledId = await helper.arrange.makeScheduledId();116117 const amount = 1n * helper.balance.getOneTokenNominal();118119 const bobsBalanceBefore = await helper.balance.getSubstrate(bob.address);120121 await helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {periodic})122 .balance.transferToSubstrate(alice, bob.address, amount);123124 await helper.wait.newBlocks(waitForBlocks + 1);125126 const bobsBalanceAfterFirst = await helper.balance.getSubstrate(bob.address);127128 expect(bobsBalanceAfterFirst)129 .to.be.equal(130 bobsBalanceBefore + 1n * amount,131 '#1 Balance of the recipient should be increased by 1 * amount',132 );133134 await helper.scheduler.cancelScheduled(alice, scheduledId);135 await helper.wait.newBlocks(periodic.period);136137 const bobsBalanceAfterSecond = await helper.balance.getSubstrate(bob.address);138 expect(bobsBalanceAfterSecond)139 .to.be.equal(140 bobsBalanceAfterFirst,141 '#2 Balance of the recipient should not be changed',142 );143 });144145 itSub.ifWithPallets('Scheduled tasks are transactional', [Pallets.TestUtils], async ({helper}) => {146 const scheduledId = await helper.arrange.makeScheduledId();147 const waitForBlocks = 4;148149 const initTestVal = 42;150 const changedTestVal = 111;151152 await helper.testUtils.setTestValue(alice, initTestVal);153154 await helper.scheduler.scheduleAfter<DevUniqueHelper>(scheduledId, waitForBlocks)155 .testUtils.setTestValueAndRollback(alice, changedTestVal);156157 await helper.wait.newBlocks(waitForBlocks + 1);158159 const testVal = await helper.testUtils.testValue();160 expect(testVal, 'The test value should NOT be commited')161 .to.be.equal(initTestVal);162 });163164 itSub.ifWithPallets('Scheduled tasks should take correct fees', [Pallets.TestUtils], async function({helper}) {165 const scheduledId = await helper.arrange.makeScheduledId();166 const waitForBlocks = 4;167 const periodic = {168 period: 2,169 repetitions: 2,170 };171172 const dummyTx = helper.constructApiCall('api.tx.testUtils.justTakeFee', []);173 const scheduledLen = dummyTx.callIndex.length;174175 const expectedScheduledFee = (await helper.getPaymentInfo(alice, dummyTx, scheduledLen))176 .partialFee.toBigInt();177178 await helper.scheduler.scheduleAfter<DevUniqueHelper>(scheduledId, waitForBlocks, {periodic})179 .testUtils.justTakeFee(alice);180181 await helper.wait.newBlocks(1);182183 const aliceInitBalance = await helper.balance.getSubstrate(alice.address);184 let diff;185186 await helper.wait.newBlocks(waitForBlocks);187188 const aliceBalanceAfterFirst = await helper.balance.getSubstrate(alice.address);189 expect(190 aliceBalanceAfterFirst < aliceInitBalance,191 '[after execution #1] Scheduled task should take a fee',192 ).to.be.true;193194 diff = aliceInitBalance - aliceBalanceAfterFirst;195 expect(diff).to.be.equal(196 expectedScheduledFee,197 'Scheduled task should take the right amount of fees',198 );199200 await helper.wait.newBlocks(periodic.period);201202 const aliceBalanceAfterSecond = await helper.balance.getSubstrate(alice.address);203 expect(204 aliceBalanceAfterSecond < aliceBalanceAfterFirst,205 '[after execution #2] Scheduled task should take a fee',206 ).to.be.true;207208 diff = aliceBalanceAfterFirst - aliceBalanceAfterSecond;209 expect(diff).to.be.equal(210 expectedScheduledFee,211 'Scheduled task should take the right amount of fees',212 );213 });214215 // Check if we can cancel a scheduled periodic operation216 // in the same block in which it is running217 itSub.ifWithPallets('Can cancel the periodic sheduled tx when the tx is running', [Pallets.TestUtils], async ({helper}) => {218 const currentBlockNumber = await helper.chain.getLatestBlockNumber();219 const blocksBeforeExecution = 10;220 const firstExecutionBlockNumber = currentBlockNumber + blocksBeforeExecution;221222 const [223 scheduledId,224 scheduledCancelId,225 ] = await helper.arrange.makeScheduledIds(2);226227 const periodic = {228 period: 5,229 repetitions: 5,230 };231232 const initTestVal = 0;233 const incTestVal = initTestVal + 1;234 const finalTestVal = initTestVal + 2;235236 await helper.testUtils.setTestValue(alice, initTestVal);237238 await helper.scheduler.scheduleAt<DevUniqueHelper>(scheduledId, firstExecutionBlockNumber, {periodic})239 .testUtils.incTestValue(alice);240241 // Cancel the inc tx after 2 executions242 // *in the same block* in which the second execution is scheduled243 await helper.scheduler.scheduleAt(244 scheduledCancelId,245 firstExecutionBlockNumber + periodic.period,246 ).scheduler.cancelScheduled(alice, scheduledId);247248 await helper.wait.forParachainBlockNumber(firstExecutionBlockNumber);249250 // execution #0251 expect(await helper.testUtils.testValue())252 .to.be.equal(incTestVal);253254 await helper.wait.forParachainBlockNumber(firstExecutionBlockNumber + periodic.period);255256 // execution #1257 expect(await helper.testUtils.testValue())258 .to.be.equal(finalTestVal);259260 for (let i = 1; i < periodic.repetitions; i++) {261 await helper.wait.forParachainBlockNumber(firstExecutionBlockNumber + periodic.period * (i + 1));262 expect(await helper.testUtils.testValue())263 .to.be.equal(finalTestVal);264 }265 });266267 itSub.ifWithPallets('A scheduled operation can cancel itself', [Pallets.TestUtils], async ({helper}) => {268 const scheduledId = await helper.arrange.makeScheduledId();269 const waitForBlocks = 4;270 const periodic = {271 period: 2,272 repetitions: 5,273 };274275 const initTestVal = 0;276 const maxTestVal = 2;277278 await helper.testUtils.setTestValue(alice, initTestVal);279280 await helper.scheduler.scheduleAfter<DevUniqueHelper>(scheduledId, waitForBlocks, {periodic})281 .testUtils.selfCancelingInc(alice, scheduledId, maxTestVal);282283 await helper.wait.newBlocks(waitForBlocks + 1);284285 // execution #0286 expect(await helper.testUtils.testValue())287 .to.be.equal(initTestVal + 1);288289 await helper.wait.newBlocks(periodic.period);290291 // execution #1292 expect(await helper.testUtils.testValue())293 .to.be.equal(initTestVal + 2);294295 await helper.wait.newBlocks(periodic.period);296297 // <canceled>298 expect(await helper.testUtils.testValue())299 .to.be.equal(initTestVal + 2);300 });301302 itSub('Root can cancel any scheduled operation', async ({helper}) => {303 const collection = await helper.nft.mintCollection(bob, {tokenPrefix: 'schd'});304 const token = await collection.mintToken(bob);305306 const scheduledId = await helper.arrange.makeScheduledId();307 const waitForBlocks = 4;308309 await token.scheduleAfter(scheduledId, waitForBlocks)310 .transfer(bob, {Substrate: alice.address});311312 await helper.getSudo().scheduler.cancelScheduled(superuser, scheduledId);313314 await helper.wait.newBlocks(waitForBlocks + 1);315316 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});317 });318319 itSub('Root can set prioritized scheduled operation', async ({helper}) => {320 const scheduledId = await helper.arrange.makeScheduledId();321 const waitForBlocks = 4;322323 const amount = 42n * helper.balance.getOneTokenNominal();324325 const balanceBefore = await helper.balance.getSubstrate(charlie.address);326327 await helper.getSudo()328 .scheduler.scheduleAfter(scheduledId, waitForBlocks, {priority: 42})329 .balance.forceTransferToSubstrate(superuser, bob.address, charlie.address, amount);330331 await helper.wait.newBlocks(waitForBlocks + 1);332333 const balanceAfter = await helper.balance.getSubstrate(charlie.address);334335 expect(balanceAfter > balanceBefore).to.be.true;336337 const diff = balanceAfter - balanceBefore;338 expect(diff).to.be.equal(amount);339 });340341 itSub("Root can change scheduled operation's priority", async ({helper}) => {342 const collection = await helper.nft.mintCollection(bob, {tokenPrefix: 'schd'});343 const token = await collection.mintToken(bob);344345 const scheduledId = await helper.arrange.makeScheduledId();346 const waitForBlocks = 6;347348 await token.scheduleAfter(scheduledId, waitForBlocks)349 .transfer(bob, {Substrate: alice.address});350351 const priority = 112;352 await helper.getSudo().scheduler.changePriority(superuser, scheduledId, priority);353354 const priorityChanged = await helper.wait.event(355 waitForBlocks,356 'scheduler',357 'PriorityChanged',358 );359360 expect(priorityChanged !== null).to.be.true;361 expect(priorityChanged!.event.data[2].toString()).to.be.equal(priority.toString());362 });363364 itSub('Prioritized operations execute in valid order', async ({helper}) => {365 const [366 scheduledFirstId,367 scheduledSecondId,368 ] = await helper.arrange.makeScheduledIds(2);369370 const currentBlockNumber = await helper.chain.getLatestBlockNumber();371 const blocksBeforeExecution = 6;372 const firstExecutionBlockNumber = currentBlockNumber + blocksBeforeExecution;373374 const prioHigh = 0;375 const prioLow = 255;376377 const periodic = {378 period: 6,379 repetitions: 2,380 };381382 const amount = 1n * helper.balance.getOneTokenNominal();383384 // Scheduler a task with a lower priority first, then with a higher priority385 await helper.getSudo().scheduler.scheduleAt(scheduledFirstId, firstExecutionBlockNumber, {priority: prioLow, periodic})386 .balance.forceTransferToSubstrate(superuser, alice.address, bob.address, amount);387388 await helper.getSudo().scheduler.scheduleAt(scheduledSecondId, firstExecutionBlockNumber, {priority: prioHigh, periodic})389 .balance.forceTransferToSubstrate(superuser, alice.address, bob.address, amount);390391 const capture = await helper.arrange.captureEvents('scheduler', 'Dispatched');392393 await helper.wait.forParachainBlockNumber(firstExecutionBlockNumber);394395 // Flip priorities396 await helper.getSudo().scheduler.changePriority(superuser, scheduledFirstId, prioHigh);397 await helper.getSudo().scheduler.changePriority(superuser, scheduledSecondId, prioLow);398399 await helper.wait.forParachainBlockNumber(firstExecutionBlockNumber + periodic.period);400401 const dispatchEvents = capture.extractCapturedEvents();402 expect(dispatchEvents.length).to.be.equal(4);403404 const dispatchedIds = dispatchEvents.map(r => r.event.data[1].toString());405406 const firstExecuctionIds = [dispatchedIds[0], dispatchedIds[1]];407 const secondExecuctionIds = [dispatchedIds[2], dispatchedIds[3]];408409 expect(firstExecuctionIds[0]).to.be.equal(scheduledSecondId);410 expect(firstExecuctionIds[1]).to.be.equal(scheduledFirstId);411412 expect(secondExecuctionIds[0]).to.be.equal(scheduledFirstId);413 expect(secondExecuctionIds[1]).to.be.equal(scheduledSecondId);414 });415416 itSub('Periodic operations always can be rescheduled', async ({helper}) => {417 const maxScheduledPerBlock = 50;418 const numFilledBlocks = 3;419 const ids = await helper.arrange.makeScheduledIds(numFilledBlocks * maxScheduledPerBlock + 1);420 const periodicId = ids[0];421 const fillIds = ids.slice(1);422423 const initTestVal = 0;424 const firstExecTestVal = 1;425 const secondExecTestVal = 2;426 await helper.testUtils.setTestValue(alice, initTestVal);427428 const currentBlockNumber = await helper.chain.getLatestBlockNumber();429 const blocksBeforeExecution = 8;430 const firstExecutionBlockNumber = currentBlockNumber + blocksBeforeExecution;431432 const period = 5;433434 const periodic = {435 period,436 repetitions: 2,437 };438439 // Fill `numFilledBlocks` blocks beginning from the block in which the second execution should occur440 const txs = [];441 for (let offset = 0; offset < numFilledBlocks; offset ++) {442 for (let i = 0; i < maxScheduledPerBlock; i++) {443444 const scheduledTx = helper.constructApiCall('api.tx.balances.transfer', [bob.address, 1n]);445446 const when = firstExecutionBlockNumber + period + offset;447 const tx = helper.constructApiCall('api.tx.scheduler.scheduleNamed', [fillIds[i + offset * maxScheduledPerBlock], when, null, null, scheduledTx]);448449 txs.push(tx);450 }451 }452 await helper.executeExtrinsic(alice, 'api.tx.testUtils.batchAll', [txs], true);453454 await helper.scheduler.scheduleAt<DevUniqueHelper>(periodicId, firstExecutionBlockNumber, {periodic})455 .testUtils.incTestValue(alice);456457 await helper.wait.newBlocks(blocksBeforeExecution);458 expect(await helper.testUtils.testValue()).to.be.equal(firstExecTestVal);459460 await helper.wait.newBlocks(period + numFilledBlocks);461462 // The periodic operation should be postponed by `numFilledBlocks`463 for (let i = 0; i < numFilledBlocks; i++) {464 expect(await helper.testUtils.testValue(firstExecutionBlockNumber + period + i)).to.be.equal(firstExecTestVal);465 }466467 // After the `numFilledBlocks` the periodic operation will eventually be executed468 expect(await helper.testUtils.testValue()).to.be.equal(secondExecTestVal);469 });470471 itSub('scheduled operations does not change nonce', async ({helper}) => {472 const scheduledId = await helper.arrange.makeScheduledId();473 const blocksBeforeExecution = 4;474475 await helper.scheduler476 .scheduleAfter<DevUniqueHelper>(scheduledId, blocksBeforeExecution)477 .balance.transferToSubstrate(alice, bob.address, 1n);478479 const initNonce = await helper.chain.getNonce(alice.address);480481 await helper.wait.newBlocks(blocksBeforeExecution + 1);482483 const finalNonce = await helper.chain.getNonce(alice.address);484485 expect(initNonce).to.be.equal(finalNonce);486 });487});488489describe('Negative Test: Scheduling', () => {490 let alice: IKeyringPair;491 let bob: IKeyringPair;492493 before(async function() {494 await usingPlaygrounds(async (helper, privateKey) => {495 requirePalletsOrSkip(this, helper, [Pallets.Scheduler]);496497 const donor = await privateKey({filename: __filename});498 [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);499500 await helper.testUtils.enable();501 });502 });503504 itSub("Can't overwrite a scheduled ID", async ({helper}) => {505 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});506 const token = await collection.mintToken(alice);507508 const scheduledId = await helper.arrange.makeScheduledId();509 const waitForBlocks = 4;510511 await token.scheduleAfter(scheduledId, waitForBlocks)512 .transfer(alice, {Substrate: bob.address});513514 const scheduled = helper.scheduler.scheduleAfter(scheduledId, waitForBlocks);515 await expect(scheduled.balance.transferToSubstrate(alice, bob.address, 1n * helper.balance.getOneTokenNominal()))516 .to.be.rejectedWith(/scheduler\.FailedToSchedule/);517518 const bobsBalanceBefore = await helper.balance.getSubstrate(bob.address);519520 await helper.wait.newBlocks(waitForBlocks + 1);521522 const bobsBalanceAfter = await helper.balance.getSubstrate(bob.address);523524 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});525 expect(bobsBalanceBefore).to.be.equal(bobsBalanceAfter);526 });527528 itSub("Can't cancel an operation which is not scheduled", async ({helper}) => {529 const scheduledId = await helper.arrange.makeScheduledId();530 await expect(helper.scheduler.cancelScheduled(alice, scheduledId))531 .to.be.rejectedWith(/scheduler\.NotFound/);532 });533534 itSub("Can't cancel a non-owned scheduled operation", async ({helper}) => {535 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});536 const token = await collection.mintToken(alice);537538 const scheduledId = await helper.arrange.makeScheduledId();539 const waitForBlocks = 4;540541 await token.scheduleAfter(scheduledId, waitForBlocks)542 .transfer(alice, {Substrate: bob.address});543544 await expect(helper.scheduler.cancelScheduled(bob, scheduledId))545 .to.be.rejectedWith(/BadOrigin/);546547 await helper.wait.newBlocks(waitForBlocks + 1);548549 expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});550 });551552 itSub("Regular user can't set prioritized scheduled operation", async ({helper}) => {553 const scheduledId = await helper.arrange.makeScheduledId();554 const waitForBlocks = 4;555556 const amount = 42n * helper.balance.getOneTokenNominal();557558 const balanceBefore = await helper.balance.getSubstrate(bob.address);559560 const scheduled = helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {priority: 42});561 562 await expect(scheduled.balance.transferToSubstrate(alice, bob.address, amount))563 .to.be.rejectedWith(/BadOrigin/);564565 await helper.wait.newBlocks(waitForBlocks + 1);566567 const balanceAfter = await helper.balance.getSubstrate(bob.address);568569 expect(balanceAfter).to.be.equal(balanceBefore);570 });571572 itSub("Regular user can't change scheduled operation's priority", async ({helper}) => {573 const collection = await helper.nft.mintCollection(bob, {tokenPrefix: 'schd'});574 const token = await collection.mintToken(bob);575576 const scheduledId = await helper.arrange.makeScheduledId();577 const waitForBlocks = 4;578579 await token.scheduleAfter(scheduledId, waitForBlocks)580 .transfer(bob, {Substrate: alice.address});581582 const priority = 112;583 await expect(helper.scheduler.changePriority(alice, scheduledId, priority))584 .to.be.rejectedWith(/BadOrigin/);585586 const priorityChanged = await helper.wait.event(587 waitForBlocks,588 'scheduler',589 'PriorityChanged',590 );591592 expect(priorityChanged === null).to.be.true;593 });594});595596// Implementation of the functionality tested here was postponed/shelved597describe.skip('Sponsoring scheduling', () => {598 // let alice: IKeyringPair;599 // let bob: IKeyringPair;600601 // before(async() => {602 // await usingApi(async (_, privateKey) => {603 // alice = privateKey('//Alice');604 // bob = privateKey('//Bob');605 // });606 // });607608 it('Can sponsor scheduling a transaction', async () => {609 // const collectionId = await createCollectionExpectSuccess();610 // await setCollectionSponsorExpectSuccess(collectionId, bob.address);611 // await confirmSponsorshipExpectSuccess(collectionId, '//Bob');612613 // await usingApi(async api => {614 // const scheduledId = await makeScheduledId();615 // const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address);616617 // const bobBalanceBefore = await getFreeBalance(bob);618 // const waitForBlocks = 4;619 // // no need to wait to check, fees must be deducted on scheduling, immediately620 // await scheduleTransferExpectSuccess(api, collectionId, tokenId, alice, bob, 0, waitForBlocks, scheduledId);621 // const bobBalanceAfter = await getFreeBalance(bob);622 // // expect(aliceBalanceAfter == aliceBalanceBefore).to.be.true;623 // expect(bobBalanceAfter < bobBalanceBefore).to.be.true;624 // // wait for sequentiality matters625 // await waitNewBlocks(waitForBlocks - 1);626 // });627 });628629 it('Schedules and dispatches a transaction even if the caller has no funds at the time of the dispatch', async () => {630 // await usingApi(async (api, privateKey) => {631 // // Find an empty, unused account632 // const zeroBalance = await findUnusedAddress(api, privateKey);633634 // const collectionId = await createCollectionExpectSuccess();635636 // // Add zeroBalance address to allow list637 // await enablePublicMintingExpectSuccess(alice, collectionId);638 // await addToAllowListExpectSuccess(alice, collectionId, zeroBalance.address);639640 // // Grace zeroBalance with money, enough to cover future transactions641 // const balanceTx = api.tx.balances.transfer(zeroBalance.address, 1n * UNIQUE);642 // await submitTransactionAsync(alice, balanceTx);643644 // // Mint a fresh NFT645 // const tokenId = await createItemExpectSuccess(zeroBalance, collectionId, 'NFT');646 // const scheduledId = await makeScheduledId();647648 // // Schedule transfer of the NFT a few blocks ahead649 // const waitForBlocks = 5;650 // await scheduleTransferExpectSuccess(api, collectionId, tokenId, zeroBalance, alice, 1, waitForBlocks, scheduledId);651652 // // Get rid of the account's funds before the scheduled transaction takes place653 // const balanceTx2 = api.tx.balances.transfer(alice.address, UNIQUE * 68n / 100n);654 // const events = await submitTransactionAsync(zeroBalance, balanceTx2);655 // expect(getGenericResult(events).success).to.be.true;656 // /*const emptyBalanceTx = api.tx.balances.setBalance(zeroBalance.address, 0, 0); // do not null reserved?657 // const sudoTx = api.tx.sudo.sudo(emptyBalanceTx as any);658 // const events = await submitTransactionAsync(alice, sudoTx);659 // expect(getGenericResult(events).success).to.be.true;*/660661 // // Wait for a certain number of blocks, discarding the ones that already happened while accepting the late transactions662 // await waitNewBlocks(waitForBlocks - 3);663664 // expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(alice.address));665 // });666 });667668 it('Sponsor going bankrupt does not impact a scheduled transaction', async () => {669 // const collectionId = await createCollectionExpectSuccess();670671 // await usingApi(async (api, privateKey) => {672 // const zeroBalance = await findUnusedAddress(api, privateKey);673 // const balanceTx = api.tx.balances.transfer(zeroBalance.address, 1n * UNIQUE);674 // await submitTransactionAsync(alice, balanceTx);675676 // await setCollectionSponsorExpectSuccess(collectionId, zeroBalance.address);677 // await confirmSponsorshipByKeyExpectSuccess(collectionId, zeroBalance);678679 // const scheduledId = await makeScheduledId();680 // const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address);681682 // const waitForBlocks = 5;683 // await scheduleTransferExpectSuccess(api, collectionId, tokenId, alice, zeroBalance, 1, waitForBlocks, scheduledId);684685 // const emptyBalanceSponsorTx = api.tx.balances.setBalance(zeroBalance.address, 0, 0);686 // const sudoTx = api.tx.sudo.sudo(emptyBalanceSponsorTx as any);687 // const events = await submitTransactionAsync(alice, sudoTx);688 // expect(getGenericResult(events).success).to.be.true;689690 // // Wait for a certain number of blocks, save for the ones that already happened while accepting the late transactions691 // await waitNewBlocks(waitForBlocks - 3);692693 // expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(zeroBalance.address));694 // });695 });696697 it('Exceeding sponsor rate limit without having enough funds prevents scheduling a periodic transaction', async () => {698 // const collectionId = await createCollectionExpectSuccess();699 // await setCollectionSponsorExpectSuccess(collectionId, bob.address);700 // await confirmSponsorshipExpectSuccess(collectionId, '//Bob');701702 // await usingApi(async (api, privateKey) => {703 // const zeroBalance = await findUnusedAddress(api, privateKey);704705 // await enablePublicMintingExpectSuccess(alice, collectionId);706 // await addToAllowListExpectSuccess(alice, collectionId, zeroBalance.address);707708 // const bobBalanceBefore = await getFreeBalance(bob);709710 // const createData = {nft: {const_data: [], variable_data: []}};711 // const creationTx = api.tx.unique.createItem(collectionId, normalizeAccountId(zeroBalance), createData as any);712 // const scheduledId = await makeScheduledId();713714 // /*const badTransaction = async function () {715 // await submitTransactionExpectFailAsync(zeroBalance, zeroToAlice);716 // };717 // await expect(badTransaction()).to.be.rejectedWith('Inability to pay some fees');*/718719 // await expect(scheduleAfter(api, creationTx, zeroBalance, 3, scheduledId, 1, 3)).to.be.rejectedWith(/Inability to pay some fees/);720721 // expect(await getFreeBalance(bob)).to.be.equal(bobBalanceBefore);722 // });723 });724});