git.delta.rocks / unique-network / refs/commits / d82d09e3c952

difftreelog

source

tests/src/scheduler.test.ts8.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 chai, {expect} from 'chai';18import chaiAsPromised from 'chai-as-promised';19import {20  default as usingApi, 21  submitTransactionAsync,22} from './substrate/substrate-api';23import {24  createItemExpectSuccess,25  createCollectionExpectSuccess,26  scheduleTransferExpectSuccess,27  scheduleTransferAndWaitExpectSuccess,28  setCollectionSponsorExpectSuccess,29  confirmSponsorshipExpectSuccess,30  findUnusedAddress,31  UNIQUE,32  enablePublicMintingExpectSuccess,33  addToAllowListExpectSuccess,34  waitNewBlocks,35  normalizeAccountId,36  getTokenOwner,37  getGenericResult,38  scheduleTransferFundsPeriodicExpectSuccess,39  getFreeBalance,40  confirmSponsorshipByKeyExpectSuccess,41  scheduleExpectFailure,42} from './util/helpers';43import {IKeyringPair} from '@polkadot/types/types';4445chai.use(chaiAsPromised);4647describe.skip('Scheduling token and balance transfers', () => {48  let alice: IKeyringPair;49  let bob: IKeyringPair;50  let scheduledIdBase: string;51  let scheduledIdSlider: number;5253  before(async() => {54    await usingApi(async (api, privateKeyWrapper) => {55      alice = privateKeyWrapper('//Alice');56      bob = privateKeyWrapper('//Bob');57    });5859    scheduledIdBase = '0x' + '0'.repeat(31);60    scheduledIdSlider = 0;61  });6263  // Loop scheduledId around 10. Unless there are concurrent tasks with long periods/repetitions, tests' tasks' ids shouldn't ovelap.64  function makeScheduledId(): string {65    return scheduledIdBase + ((scheduledIdSlider++) % 10);66  }6768  it('Can schedule a transfer of an owned token with delay', async () => {69    await usingApi(async () => {70      const nftCollectionId = await createCollectionExpectSuccess();71      const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');72      await setCollectionSponsorExpectSuccess(nftCollectionId, alice.address);73      await confirmSponsorshipExpectSuccess(nftCollectionId);7475      await scheduleTransferAndWaitExpectSuccess(nftCollectionId, newNftTokenId, alice, bob, 1, 4, makeScheduledId());76    });77  });7879  it('Can transfer funds periodically', async () => {80    await usingApi(async () => {81      const waitForBlocks = 4;82      const period = 2;83      await scheduleTransferFundsPeriodicExpectSuccess(1n * UNIQUE, alice, bob, waitForBlocks, makeScheduledId(), period, 2);84      const bobsBalanceBefore = await getFreeBalance(bob);8586      // discounting already waited-for operations87      await waitNewBlocks(waitForBlocks - 2);88      const bobsBalanceAfterFirst = await getFreeBalance(bob);89      expect(bobsBalanceAfterFirst > bobsBalanceBefore).to.be.true;9091      await waitNewBlocks(period);92      const bobsBalanceAfterSecond = await getFreeBalance(bob);93      expect(bobsBalanceAfterSecond > bobsBalanceAfterFirst).to.be.true;94    });95  });9697  it('Can sponsor scheduling a transaction', async () => {98    const collectionId = await createCollectionExpectSuccess();99    await setCollectionSponsorExpectSuccess(collectionId, bob.address);100    await confirmSponsorshipExpectSuccess(collectionId, '//Bob');101102    await usingApi(async () => {103      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address);104105      const bobBalanceBefore = await getFreeBalance(bob);106      const waitForBlocks = 4;107      // no need to wait to check, fees must be deducted on scheduling, immediately108      await scheduleTransferExpectSuccess(collectionId, tokenId, alice, bob, 0, waitForBlocks, makeScheduledId());109      const bobBalanceAfter = await getFreeBalance(bob);110      // expect(aliceBalanceAfter == aliceBalanceBefore).to.be.true;111      expect(bobBalanceAfter < bobBalanceBefore).to.be.true;112      // wait for sequentiality matters113      await waitNewBlocks(waitForBlocks - 1);114    });115  });116117  it('Schedules and dispatches a transaction even if the caller has no funds at the time of the dispatch', async () => {118    await usingApi(async (api, privateKeyWrapper) => {119      // Find an empty, unused account120      const zeroBalance = await findUnusedAddress(api, privateKeyWrapper);121122      const collectionId = await createCollectionExpectSuccess();123124      // Add zeroBalance address to allow list125      await enablePublicMintingExpectSuccess(alice, collectionId);126      await addToAllowListExpectSuccess(alice, collectionId, zeroBalance.address);127128      // Grace zeroBalance with money, enough to cover future transactions129      const balanceTx = api.tx.balances.transfer(zeroBalance.address, 1n * UNIQUE);130      await submitTransactionAsync(alice, balanceTx);131132      // Mint a fresh NFT133      const tokenId = await createItemExpectSuccess(zeroBalance, collectionId, 'NFT');134135      // Schedule transfer of the NFT a few blocks ahead136      const waitForBlocks = 5;137      await scheduleTransferExpectSuccess(collectionId, tokenId, zeroBalance, alice, 1, waitForBlocks, makeScheduledId());138139      // Get rid of the account's funds before the scheduled transaction takes place140      const balanceTx2 = api.tx.balances.transfer(alice.address, UNIQUE * 68n / 100n);141      const events = await submitTransactionAsync(zeroBalance, balanceTx2);142      expect(getGenericResult(events).success).to.be.true;143      /*const emptyBalanceTx = api.tx.balances.setBalance(zeroBalance.address, 0, 0); // do not null reserved?144      const sudoTx = api.tx.sudo.sudo(emptyBalanceTx as any);145      const events = await submitTransactionAsync(alice, sudoTx);146      expect(getGenericResult(events).success).to.be.true;*/147148      // Wait for a certain number of blocks, discarding the ones that already happened while accepting the late transactions149      await waitNewBlocks(waitForBlocks - 3);150151      expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(alice.address));152    });153  });154155  it('Sponsor going bankrupt does not impact a scheduled transaction', async () => {156    const collectionId = await createCollectionExpectSuccess();157158    await usingApi(async (api, privateKeyWrapper) => {159      const zeroBalance = await findUnusedAddress(api, privateKeyWrapper);160      const balanceTx = api.tx.balances.transfer(zeroBalance.address, 1n * UNIQUE);161      await submitTransactionAsync(alice, balanceTx);162163      await setCollectionSponsorExpectSuccess(collectionId, zeroBalance.address);164      await confirmSponsorshipByKeyExpectSuccess(collectionId, zeroBalance);165166      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address);167168      const waitForBlocks = 5;169      await scheduleTransferExpectSuccess(collectionId, tokenId, alice, zeroBalance, 1, waitForBlocks, makeScheduledId());170171      const emptyBalanceSponsorTx = api.tx.balances.setBalance(zeroBalance.address, 0, 0);172      const sudoTx = api.tx.sudo.sudo(emptyBalanceSponsorTx as any);173      const events = await submitTransactionAsync(alice, sudoTx);174      expect(getGenericResult(events).success).to.be.true;175176      // Wait for a certain number of blocks, save for the ones that already happened while accepting the late transactions177      await waitNewBlocks(waitForBlocks - 3);178179      expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(zeroBalance.address));180    });181  });182183  it.skip('Exceeding sponsor rate limit without having enough funds prevents scheduling a periodic transaction', async () => {184    const collectionId = await createCollectionExpectSuccess();185    await setCollectionSponsorExpectSuccess(collectionId, bob.address);186    await confirmSponsorshipExpectSuccess(collectionId, '//Bob');187188    await usingApi(async (api, privateKeyWrapper) => {189      const zeroBalance = await findUnusedAddress(api, privateKeyWrapper);190191      await enablePublicMintingExpectSuccess(alice, collectionId);192      await addToAllowListExpectSuccess(alice, collectionId, zeroBalance.address);193194      const bobBalanceBefore = await getFreeBalance(bob);195196      const createData = {nft: {const_data: [], variable_data: []}};197      const creationTx = api.tx.unique.createItem(collectionId, normalizeAccountId(zeroBalance), createData as any);198199      /*const badTransaction = async function () {200        await submitTransactionExpectFailAsync(zeroBalance, zeroToAlice);201      };202      await expect(badTransaction()).to.be.rejectedWith('Inability to pay some fees');*/203204      await scheduleExpectFailure(creationTx, zeroBalance, 3, makeScheduledId(), 1, 3);205206      expect(await getFreeBalance(bob)).to.be.equal(bobBalanceBefore);207    });208  });209});