git.delta.rocks / unique-network / refs/commits / 5946798abe51

difftreelog

source

tests/src/scheduler.test.ts28.9 KiBsourcehistory
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;44    45    await token.scheduleAfter(schedulerId, blocksBeforeExecution)46      .transfer(alice, {Substrate: bob.address});47    const executionBlock = await helper.chain.getLatestBlockNumber() + blocksBeforeExecution + 1;4849    expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});5051    await helper.wait.forParachainBlockNumber(executionBlock);5253    expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});54  });5556  itSub('Can transfer funds periodically', async ({helper}) => {57    const scheduledId = await helper.arrange.makeScheduledId();58    const waitForBlocks = 1;5960    const amount = 1n * helper.balance.getOneTokenNominal();61    const periodic = {62      period: 2,63      repetitions: 2,64    };6566    const bobsBalanceBefore = await helper.balance.getSubstrate(bob.address);6768    await helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {periodic})69      .balance.transferToSubstrate(alice, bob.address, amount);70    const executionBlock = await helper.chain.getLatestBlockNumber() + waitForBlocks + 1;7172    await helper.wait.forParachainBlockNumber(executionBlock);7374    const bobsBalanceAfterFirst = await helper.balance.getSubstrate(bob.address);75    expect(bobsBalanceAfterFirst)76      .to.be.equal(77        bobsBalanceBefore + 1n * amount,78        '#1 Balance of the recipient should be increased by 1 * amount',79      );8081    await helper.wait.forParachainBlockNumber(executionBlock + periodic.period);8283    const bobsBalanceAfterSecond = await helper.balance.getSubstrate(bob.address);84    expect(bobsBalanceAfterSecond)85      .to.be.equal(86        bobsBalanceBefore + 2n * amount,87        '#2 Balance of the recipient should be increased by 2 * amount',88      );89  });9091  itSub('Can cancel a scheduled operation which has not yet taken effect', async ({helper}) => {92    const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});93    const token = await collection.mintToken(alice);9495    const scheduledId = await helper.arrange.makeScheduledId();96    const waitForBlocks = 4;9798    expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});99100    await token.scheduleAfter(scheduledId, waitForBlocks)101      .transfer(alice, {Substrate: bob.address});102    const executionBlock = await helper.chain.getLatestBlockNumber() + waitForBlocks + 1;103104    await helper.scheduler.cancelScheduled(alice, scheduledId);105106    await helper.wait.forParachainBlockNumber(executionBlock);107108    expect(await token.getOwner()).to.be.deep.equal({Substrate: alice.address});109  });110111  itSub('Can cancel a periodic operation (transfer of funds)', 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 * helper.balance.getOneTokenNominal();121122    const bobsBalanceBefore = await helper.balance.getSubstrate(bob.address);123124    await helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {periodic})125      .balance.transferToSubstrate(alice, bob.address, amount);126    const executionBlock = await helper.chain.getLatestBlockNumber() + waitForBlocks + 1;127128    await helper.wait.forParachainBlockNumber(executionBlock);129130    const bobsBalanceAfterFirst = await helper.balance.getSubstrate(bob.address);131132    expect(bobsBalanceAfterFirst)133      .to.be.equal(134        bobsBalanceBefore + 1n * amount,135        '#1 Balance of the recipient should be increased by 1 * amount',136      );137138    await helper.scheduler.cancelScheduled(alice, scheduledId);139    await helper.wait.forParachainBlockNumber(executionBlock + periodic.period);140141    const bobsBalanceAfterSecond = await helper.balance.getSubstrate(bob.address);142    expect(bobsBalanceAfterSecond)143      .to.be.equal(144        bobsBalanceAfterFirst,145        '#2 Balance of the recipient should not be changed',146      );147  });148149  itSub.ifWithPallets('Scheduled tasks are transactional', [Pallets.TestUtils], async ({helper}) => {150    const scheduledId = await helper.arrange.makeScheduledId();151    const waitForBlocks = 4;152153    const initTestVal = 42;154    const changedTestVal = 111;155156    await helper.testUtils.setTestValue(alice, initTestVal);157158    await helper.scheduler.scheduleAfter<DevUniqueHelper>(scheduledId, waitForBlocks)159      .testUtils.setTestValueAndRollback(alice, changedTestVal);160    const executionBlock = await helper.chain.getLatestBlockNumber() + waitForBlocks + 1;161162    await helper.wait.forParachainBlockNumber(executionBlock);163164    const testVal = await helper.testUtils.testValue();165    expect(testVal, 'The test value should NOT be commited')166      .to.be.equal(initTestVal);167  });168169  itSub.ifWithPallets('Scheduled tasks should take correct fees', [Pallets.TestUtils], async function({helper}) {170    const scheduledId = await helper.arrange.makeScheduledId();171    const waitForBlocks = 4;172    const periodic = {173      period: 2,174      repetitions: 2,175    };176177    const dummyTx = helper.constructApiCall('api.tx.testUtils.justTakeFee', []);178    const scheduledLen = dummyTx.callIndex.length;179180    const expectedScheduledFee = (await helper.getPaymentInfo(alice, dummyTx, scheduledLen))181      .partialFee.toBigInt();182183    await helper.scheduler.scheduleAfter<DevUniqueHelper>(scheduledId, waitForBlocks, {periodic})184      .testUtils.justTakeFee(alice);185    const executionBlock = await helper.chain.getLatestBlockNumber() + waitForBlocks + 1;186187    const aliceInitBalance = await helper.balance.getSubstrate(alice.address);188    let diff;189190    await helper.wait.forParachainBlockNumber(executionBlock);191192    const aliceBalanceAfterFirst = await helper.balance.getSubstrate(alice.address);193    expect(194      aliceBalanceAfterFirst < aliceInitBalance,195      '[after execution #1] Scheduled task should take a fee',196    ).to.be.true;197198    diff = aliceInitBalance - aliceBalanceAfterFirst;199    expect(diff).to.be.equal(200      expectedScheduledFee,201      'Scheduled task should take the right amount of fees',202    );203204    await helper.wait.forParachainBlockNumber(executionBlock + periodic.period);205206    const aliceBalanceAfterSecond = await helper.balance.getSubstrate(alice.address);207    expect(208      aliceBalanceAfterSecond < aliceBalanceAfterFirst,209      '[after execution #2] Scheduled task should take a fee',210    ).to.be.true;211212    diff = aliceBalanceAfterFirst - aliceBalanceAfterSecond;213    expect(diff).to.be.equal(214      expectedScheduledFee,215      'Scheduled task should take the right amount of fees',216    );217  });218219  // Check if we can cancel a scheduled periodic operation220  // in the same block in which it is running221  itSub.ifWithPallets('Can cancel the periodic sheduled tx when the tx is running', [Pallets.TestUtils], async ({helper}) => {222    const currentBlockNumber = await helper.chain.getLatestBlockNumber();223    const blocksBeforeExecution = 10;224    const firstExecutionBlockNumber = currentBlockNumber + blocksBeforeExecution;225226    const [227      scheduledId,228      scheduledCancelId,229    ] = await helper.arrange.makeScheduledIds(2);230231    const periodic = {232      period: 5,233      repetitions: 5,234    };235236    const initTestVal = 0;237    const incTestVal = initTestVal + 1;238    const finalTestVal = initTestVal + 2;239240    await helper.testUtils.setTestValue(alice, initTestVal);241242    await helper.scheduler.scheduleAt<DevUniqueHelper>(scheduledId, firstExecutionBlockNumber, {periodic})243      .testUtils.incTestValue(alice);244245    // Cancel the inc tx after 2 executions246    // *in the same block* in which the second execution is scheduled247    await helper.scheduler.scheduleAt(248      scheduledCancelId,249      firstExecutionBlockNumber + periodic.period,250    ).scheduler.cancelScheduled(alice, scheduledId);251252    await helper.wait.forParachainBlockNumber(firstExecutionBlockNumber);253254    // execution #0255    expect(await helper.testUtils.testValue())256      .to.be.equal(incTestVal);257258    await helper.wait.forParachainBlockNumber(firstExecutionBlockNumber + periodic.period);259260    // execution #1261    expect(await helper.testUtils.testValue())262      .to.be.equal(finalTestVal);263264    for (let i = 1; i < periodic.repetitions; i++) {265      await helper.wait.forParachainBlockNumber(firstExecutionBlockNumber + periodic.period * (i + 1));266      expect(await helper.testUtils.testValue())267        .to.be.equal(finalTestVal);268    }269  });270271  itSub.ifWithPallets('A scheduled operation can cancel itself', [Pallets.TestUtils], async ({helper}) => {272    const scheduledId = await helper.arrange.makeScheduledId();273    const waitForBlocks = 4;274    const periodic = {275      period: 2,276      repetitions: 5,277    };278279    const initTestVal = 0;280    const maxTestVal = 2;281282    await helper.testUtils.setTestValue(alice, initTestVal);283284    await helper.scheduler.scheduleAfter<DevUniqueHelper>(scheduledId, waitForBlocks, {periodic})285      .testUtils.selfCancelingInc(alice, scheduledId, maxTestVal);286    const executionBlock = await helper.chain.getLatestBlockNumber() + waitForBlocks + 1;287288    await helper.wait.forParachainBlockNumber(executionBlock);289290    // execution #0291    expect(await helper.testUtils.testValue())292      .to.be.equal(initTestVal + 1);293294    await helper.wait.forParachainBlockNumber(executionBlock + periodic.period);295296    // execution #1297    expect(await helper.testUtils.testValue())298      .to.be.equal(initTestVal + 2);299300    await helper.wait.forParachainBlockNumber(executionBlock + 2 * periodic.period);301302    // <canceled>303    expect(await helper.testUtils.testValue())304      .to.be.equal(initTestVal + 2);305  });306307  itSub('Root can cancel any scheduled operation', async ({helper}) => {308    const collection = await helper.nft.mintCollection(bob, {tokenPrefix: 'schd'});309    const token = await collection.mintToken(bob);310311    const scheduledId = await helper.arrange.makeScheduledId();312    const waitForBlocks = 4;313314    await token.scheduleAfter(scheduledId, waitForBlocks)315      .transfer(bob, {Substrate: alice.address});316    const executionBlock = await helper.chain.getLatestBlockNumber() + waitForBlocks + 1;317318    await helper.getSudo().scheduler.cancelScheduled(superuser, scheduledId);319320    await helper.wait.forParachainBlockNumber(executionBlock);321322    expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});323  });324325  itSub('Root can set prioritized scheduled operation', async ({helper}) => {326    const scheduledId = await helper.arrange.makeScheduledId();327    const waitForBlocks = 4;328329    const amount = 42n * helper.balance.getOneTokenNominal();330331    const balanceBefore = await helper.balance.getSubstrate(charlie.address);332333    await helper.getSudo()334      .scheduler.scheduleAfter(scheduledId, waitForBlocks, {priority: 42})335      .balance.forceTransferToSubstrate(superuser, bob.address, charlie.address, amount);336    const executionBlock = await helper.chain.getLatestBlockNumber() + waitForBlocks + 1;337338    await helper.wait.forParachainBlockNumber(executionBlock);339340    const balanceAfter = await helper.balance.getSubstrate(charlie.address);341342    expect(balanceAfter > balanceBefore).to.be.true;343344    const diff = balanceAfter - balanceBefore;345    expect(diff).to.be.equal(amount);346  });347348  itSub("Root can change scheduled operation's priority", async ({helper}) => {349    const collection = await helper.nft.mintCollection(bob, {tokenPrefix: 'schd'});350    const token = await collection.mintToken(bob);351352    const scheduledId = await helper.arrange.makeScheduledId();353    const waitForBlocks = 6;354355    await token.scheduleAfter(scheduledId, waitForBlocks)356      .transfer(bob, {Substrate: alice.address});357358    const priority = 112;359    await helper.getSudo().scheduler.changePriority(superuser, scheduledId, priority);360361    const priorityChanged = await helper.wait.event(362      waitForBlocks,363      'scheduler',364      'PriorityChanged',365    );366367    expect(priorityChanged !== null).to.be.true;368    expect(priorityChanged!.event.data[2].toString()).to.be.equal(priority.toString());369  });370371  itSub('Prioritized operations execute in valid order', async ({helper}) => {372    const [373      scheduledFirstId,374      scheduledSecondId,375    ] = await helper.arrange.makeScheduledIds(2);376377    const currentBlockNumber = await helper.chain.getLatestBlockNumber();378    const blocksBeforeExecution = 6;379    const firstExecutionBlockNumber = currentBlockNumber + blocksBeforeExecution;380381    const prioHigh = 0;382    const prioLow = 255;383384    const periodic = {385      period: 6,386      repetitions: 2,387    };388389    const amount = 1n * helper.balance.getOneTokenNominal();390391    // Scheduler a task with a lower priority first, then with a higher priority392    await helper.getSudo().scheduler.scheduleAt(scheduledFirstId, firstExecutionBlockNumber, {priority: prioLow, periodic})393      .balance.forceTransferToSubstrate(superuser, alice.address, bob.address, amount);394395    await helper.getSudo().scheduler.scheduleAt(scheduledSecondId, firstExecutionBlockNumber, {priority: prioHigh, periodic})396      .balance.forceTransferToSubstrate(superuser, alice.address, bob.address, amount);397398    const capture = await helper.arrange.captureEvents('scheduler', 'Dispatched');399400    await helper.wait.forParachainBlockNumber(firstExecutionBlockNumber);401402    // Flip priorities403    await helper.getSudo().scheduler.changePriority(superuser, scheduledFirstId, prioHigh);404    await helper.getSudo().scheduler.changePriority(superuser, scheduledSecondId, prioLow);405406    await helper.wait.forParachainBlockNumber(firstExecutionBlockNumber + periodic.period);407408    const dispatchEvents = capture.extractCapturedEvents();409    expect(dispatchEvents.length).to.be.equal(4);410411    const dispatchedIds = dispatchEvents.map(r => r.event.data[1].toString());412413    const firstExecuctionIds = [dispatchedIds[0], dispatchedIds[1]];414    const secondExecuctionIds = [dispatchedIds[2], dispatchedIds[3]];415416    expect(firstExecuctionIds[0]).to.be.equal(scheduledSecondId);417    expect(firstExecuctionIds[1]).to.be.equal(scheduledFirstId);418419    expect(secondExecuctionIds[0]).to.be.equal(scheduledFirstId);420    expect(secondExecuctionIds[1]).to.be.equal(scheduledSecondId);421  });422423  itSub('Periodic operations always can be rescheduled', async ({helper}) => {424    const maxScheduledPerBlock = 50;425    const numFilledBlocks = 3;426    const ids = await helper.arrange.makeScheduledIds(numFilledBlocks * maxScheduledPerBlock + 1);427    const periodicId = ids[0];428    const fillIds = ids.slice(1);429430    const initTestVal = 0;431    const firstExecTestVal = 1;432    const secondExecTestVal = 2;433    await helper.testUtils.setTestValue(alice, initTestVal);434435    const currentBlockNumber = await helper.chain.getLatestBlockNumber();436    const blocksBeforeExecution = 8;437    const firstExecutionBlockNumber = currentBlockNumber + blocksBeforeExecution;438439    const period = 5;440441    const periodic = {442      period,443      repetitions: 2,444    };445446    // Fill `numFilledBlocks` blocks beginning from the block in which the second execution should occur447    const txs = [];448    for (let offset = 0; offset < numFilledBlocks; offset ++) {449      for (let i = 0; i < maxScheduledPerBlock; i++) {450451        const scheduledTx = helper.constructApiCall('api.tx.balances.transfer', [bob.address, 1n]);452453        const when = firstExecutionBlockNumber + period + offset;454        const tx = helper.constructApiCall('api.tx.scheduler.scheduleNamed', [fillIds[i + offset * maxScheduledPerBlock], when, null, null, scheduledTx]);455456        txs.push(tx);457      }458    }459    await helper.executeExtrinsic(alice, 'api.tx.testUtils.batchAll', [txs], true);460461    await helper.scheduler.scheduleAt<DevUniqueHelper>(periodicId, firstExecutionBlockNumber, {periodic})462      .testUtils.incTestValue(alice);463464    await helper.wait.forParachainBlockNumber(firstExecutionBlockNumber);465    expect(await helper.testUtils.testValue()).to.be.equal(firstExecTestVal);466467    await helper.wait.forParachainBlockNumber(firstExecutionBlockNumber + period + numFilledBlocks);468469    // The periodic operation should be postponed by `numFilledBlocks`470    for (let i = 0; i < numFilledBlocks; i++) {471      expect(await helper.testUtils.testValue(firstExecutionBlockNumber + period + i)).to.be.equal(firstExecTestVal);472    }473474    // After the `numFilledBlocks` the periodic operation will eventually be executed475    expect(await helper.testUtils.testValue()).to.be.equal(secondExecTestVal);476  });477478  itSub('scheduled operations does not change nonce', async ({helper}) => {479    const scheduledId = await helper.arrange.makeScheduledId();480    const blocksBeforeExecution = 4;481482    await helper.scheduler483      .scheduleAfter<DevUniqueHelper>(scheduledId, blocksBeforeExecution)484      .balance.transferToSubstrate(alice, bob.address, 1n);485    const executionBlock = await helper.chain.getLatestBlockNumber() + blocksBeforeExecution + 1;486487    const initNonce = await helper.chain.getNonce(alice.address);488489    await helper.wait.forParachainBlockNumber(executionBlock);490491    const finalNonce = await helper.chain.getNonce(alice.address);492493    expect(initNonce).to.be.equal(finalNonce);494  });495});496497describe('Negative Test: Scheduling', () => {498  let alice: IKeyringPair;499  let bob: IKeyringPair;500501  before(async function() {502    await usingPlaygrounds(async (helper, privateKey) => {503      requirePalletsOrSkip(this, helper, [Pallets.Scheduler]);504505      const donor = await privateKey({filename: __filename});506      [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);507508      await helper.testUtils.enable();509    });510  });511512  itSub("Can't overwrite a scheduled ID", async ({helper}) => {513    const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});514    const token = await collection.mintToken(alice);515516    const scheduledId = await helper.arrange.makeScheduledId();517    const waitForBlocks = 4;518519    await token.scheduleAfter(scheduledId, waitForBlocks)520      .transfer(alice, {Substrate: bob.address});521    const executionBlock = await helper.chain.getLatestBlockNumber() + waitForBlocks + 1;522523    const scheduled = helper.scheduler.scheduleAfter(scheduledId, waitForBlocks);524    await expect(scheduled.balance.transferToSubstrate(alice, bob.address, 1n * helper.balance.getOneTokenNominal()))525      .to.be.rejectedWith(/scheduler\.FailedToSchedule/);526527    const bobsBalanceBefore = await helper.balance.getSubstrate(bob.address);528529    await helper.wait.forParachainBlockNumber(executionBlock);530531    const bobsBalanceAfter = await helper.balance.getSubstrate(bob.address);532533    expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});534    expect(bobsBalanceBefore).to.be.equal(bobsBalanceAfter);535  });536537  itSub("Can't cancel an operation which is not scheduled", async ({helper}) => {538    const scheduledId = await helper.arrange.makeScheduledId();539    await expect(helper.scheduler.cancelScheduled(alice, scheduledId))540      .to.be.rejectedWith(/scheduler\.NotFound/);541  });542543  itSub("Can't cancel a non-owned scheduled operation", async ({helper}) => {544    const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'schd'});545    const token = await collection.mintToken(alice);546547    const scheduledId = await helper.arrange.makeScheduledId();548    const waitForBlocks = 4;549550    await token.scheduleAfter(scheduledId, waitForBlocks)551      .transfer(alice, {Substrate: bob.address});552    const executionBlock = await helper.chain.getLatestBlockNumber() + waitForBlocks + 1;553554    await expect(helper.scheduler.cancelScheduled(bob, scheduledId))555      .to.be.rejectedWith(/BadOrigin/);556557    await helper.wait.forParachainBlockNumber(executionBlock);558559    expect(await token.getOwner()).to.be.deep.equal({Substrate: bob.address});560  });561562  itSub("Regular user can't set prioritized scheduled operation", async ({helper}) => {563    const scheduledId = await helper.arrange.makeScheduledId();564    const waitForBlocks = 4;565566    const amount = 42n * helper.balance.getOneTokenNominal();567568    const balanceBefore = await helper.balance.getSubstrate(bob.address);569570    const scheduled = helper.scheduler.scheduleAfter(scheduledId, waitForBlocks, {priority: 42});571    572    await expect(scheduled.balance.transferToSubstrate(alice, bob.address, amount))573      .to.be.rejectedWith(/BadOrigin/);574575    const executionBlock = await helper.chain.getLatestBlockNumber() + waitForBlocks + 1;576577    await helper.wait.forParachainBlockNumber(executionBlock);578579    const balanceAfter = await helper.balance.getSubstrate(bob.address);580581    expect(balanceAfter).to.be.equal(balanceBefore);582  });583584  itSub("Regular user can't change scheduled operation's priority", async ({helper}) => {585    const collection = await helper.nft.mintCollection(bob, {tokenPrefix: 'schd'});586    const token = await collection.mintToken(bob);587588    const scheduledId = await helper.arrange.makeScheduledId();589    const waitForBlocks = 4;590591    await token.scheduleAfter(scheduledId, waitForBlocks)592      .transfer(bob, {Substrate: alice.address});593594    const priority = 112;595    await expect(helper.scheduler.changePriority(alice, scheduledId, priority))596      .to.be.rejectedWith(/BadOrigin/);597598    const priorityChanged = await helper.wait.event(599      waitForBlocks,600      'scheduler',601      'PriorityChanged',602    );603604    expect(priorityChanged === null).to.be.true;605  });606});607608// Implementation of the functionality tested here was postponed/shelved609describe.skip('Sponsoring scheduling', () => {610  // let alice: IKeyringPair;611  // let bob: IKeyringPair;612613  // before(async() => {614  //   await usingApi(async (_, privateKey) => {615  //     alice = privateKey('//Alice');616  //     bob = privateKey('//Bob');617  //   });618  // });619620  it('Can sponsor scheduling a transaction', async () => {621    // const collectionId = await createCollectionExpectSuccess();622    // await setCollectionSponsorExpectSuccess(collectionId, bob.address);623    // await confirmSponsorshipExpectSuccess(collectionId, '//Bob');624625    // await usingApi(async api => {626    //   const scheduledId = await makeScheduledId();627    //   const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address);628629    //   const bobBalanceBefore = await getFreeBalance(bob);630    //   const waitForBlocks = 4;631    //   // no need to wait to check, fees must be deducted on scheduling, immediately632    //   await scheduleTransferExpectSuccess(api, collectionId, tokenId, alice, bob, 0, waitForBlocks, scheduledId);633    //   const bobBalanceAfter = await getFreeBalance(bob);634    //   // expect(aliceBalanceAfter == aliceBalanceBefore).to.be.true;635    //   expect(bobBalanceAfter < bobBalanceBefore).to.be.true;636    //   // wait for sequentiality matters637    //   await waitNewBlocks(waitForBlocks - 1);638    // });639  });640641  it('Schedules and dispatches a transaction even if the caller has no funds at the time of the dispatch', async () => {642    // await usingApi(async (api, privateKey) => {643    //   // Find an empty, unused account644    //   const zeroBalance = await findUnusedAddress(api, privateKey);645646    //   const collectionId = await createCollectionExpectSuccess();647648    //   // Add zeroBalance address to allow list649    //   await enablePublicMintingExpectSuccess(alice, collectionId);650    //   await addToAllowListExpectSuccess(alice, collectionId, zeroBalance.address);651652    //   // Grace zeroBalance with money, enough to cover future transactions653    //   const balanceTx = api.tx.balances.transfer(zeroBalance.address, 1n * UNIQUE);654    //   await submitTransactionAsync(alice, balanceTx);655656    //   // Mint a fresh NFT657    //   const tokenId = await createItemExpectSuccess(zeroBalance, collectionId, 'NFT');658    //   const scheduledId = await makeScheduledId();659660    //   // Schedule transfer of the NFT a few blocks ahead661    //   const waitForBlocks = 5;662    //   await scheduleTransferExpectSuccess(api, collectionId, tokenId, zeroBalance, alice, 1, waitForBlocks, scheduledId);663664    //   // Get rid of the account's funds before the scheduled transaction takes place665    //   const balanceTx2 = api.tx.balances.transfer(alice.address, UNIQUE * 68n / 100n);666    //   const events = await submitTransactionAsync(zeroBalance, balanceTx2);667    //   expect(getGenericResult(events).success).to.be.true;668    //   /*const emptyBalanceTx = api.tx.balances.setBalance(zeroBalance.address, 0, 0); // do not null reserved?669    //   const sudoTx = api.tx.sudo.sudo(emptyBalanceTx 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, discarding 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(alice.address));677    // });678  });679680  it('Sponsor going bankrupt does not impact a scheduled transaction', async () => {681    // const collectionId = await createCollectionExpectSuccess();682683    // await usingApi(async (api, privateKey) => {684    //   const zeroBalance = await findUnusedAddress(api, privateKey);685    //   const balanceTx = api.tx.balances.transfer(zeroBalance.address, 1n * UNIQUE);686    //   await submitTransactionAsync(alice, balanceTx);687688    //   await setCollectionSponsorExpectSuccess(collectionId, zeroBalance.address);689    //   await confirmSponsorshipByKeyExpectSuccess(collectionId, zeroBalance);690691    //   const scheduledId = await makeScheduledId();692    //   const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address);693694    //   const waitForBlocks = 5;695    //   await scheduleTransferExpectSuccess(api, collectionId, tokenId, alice, zeroBalance, 1, waitForBlocks, scheduledId);696697    //   const emptyBalanceSponsorTx = api.tx.balances.setBalance(zeroBalance.address, 0, 0);698    //   const sudoTx = api.tx.sudo.sudo(emptyBalanceSponsorTx as any);699    //   const events = await submitTransactionAsync(alice, sudoTx);700    //   expect(getGenericResult(events).success).to.be.true;701702    //   // Wait for a certain number of blocks, save for the ones that already happened while accepting the late transactions703    //   await waitNewBlocks(waitForBlocks - 3);704705    //   expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(zeroBalance.address));706    // });707  });708709  it('Exceeding sponsor rate limit without having enough funds prevents scheduling a periodic transaction', async () => {710    // const collectionId = await createCollectionExpectSuccess();711    // await setCollectionSponsorExpectSuccess(collectionId, bob.address);712    // await confirmSponsorshipExpectSuccess(collectionId, '//Bob');713714    // await usingApi(async (api, privateKey) => {715    //   const zeroBalance = await findUnusedAddress(api, privateKey);716717    //   await enablePublicMintingExpectSuccess(alice, collectionId);718    //   await addToAllowListExpectSuccess(alice, collectionId, zeroBalance.address);719720    //   const bobBalanceBefore = await getFreeBalance(bob);721722    //   const createData = {nft: {const_data: [], variable_data: []}};723    //   const creationTx = api.tx.unique.createItem(collectionId, normalizeAccountId(zeroBalance), createData as any);724    //   const scheduledId = await makeScheduledId();725726    //   /*const badTransaction = async function () {727    //     await submitTransactionExpectFailAsync(zeroBalance, zeroToAlice);728    //   };729    //   await expect(badTransaction()).to.be.rejectedWith('Inability to pay some fees');*/730731    //   await expect(scheduleAfter(api, creationTx, zeroBalance, 3, scheduledId, 1, 3)).to.be.rejectedWith(/Inability to pay some fees/);732733    //   expect(await getFreeBalance(bob)).to.be.equal(bobBalanceBefore);734    // });735  });736});