git.delta.rocks / unique-network / refs/commits / 792a004ac69c

difftreelog

source

tests/src/confirmSponsorship.test.ts13.8 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 from 'chai';18import chaiAsPromised from 'chai-as-promised';19import {IKeyringPair} from '@polkadot/types/types';20import {usingPlaygrounds} from './util/playgrounds';2122chai.use(chaiAsPromised);23const expect = chai.expect;2425let donor: IKeyringPair;2627before(async () => {28  await usingPlaygrounds(async (_, privateKey) => {29    donor = privateKey('//Alice');30  });31});3233let alice: IKeyringPair;34let bob: IKeyringPair;35let charlie: IKeyringPair;3637describe('integration test: ext. confirmSponsorship():', () => {3839  before(async () => {40    await usingPlaygrounds(async (helper) => {41      [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);42    });43  });4445  it('Confirm collection sponsorship', async () => {46    await usingPlaygrounds(async (helper) => {47      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});48      await collection.setSponsor(alice, bob.address);49      await collection.confirmSponsorship(bob);50    });51  });5253  it('Add sponsor to a collection after the same sponsor was already added and confirmed', async () => {54    await usingPlaygrounds(async (helper) => {55      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});56      await collection.setSponsor(alice, bob.address);57      await collection.confirmSponsorship(bob);58      await collection.setSponsor(alice, bob.address);59    });60  });61  it('Add new sponsor to a collection after another sponsor was already added and confirmed', async () => {62    await usingPlaygrounds(async (helper) => {63      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});64      await collection.setSponsor(alice, bob.address);65      await collection.confirmSponsorship(bob);66      await collection.setSponsor(alice, charlie.address);67    });68  });6970  it('NFT: Transfer fees are paid by the sponsor after confirmation', async () => {71    await usingPlaygrounds(async (helper) => {72      const [zeroBalance] = await helper.arrange.createAccounts([0n], donor);73      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});74      await collection.setSponsor(alice, bob.address);75      await collection.confirmSponsorship(bob);76      const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);77      const token = await collection.mintToken(alice, {Substrate: zeroBalance.address});78      await token.transfer(zeroBalance, {Substrate: alice.address});79      const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);80      expect(bobBalanceAfter < bobBalanceBefore).to.be.true;81    });82  });8384  it('Fungible: Transfer fees are paid by the sponsor after confirmation', async () => {85    await usingPlaygrounds(async (helper) => {86      const [zeroBalance] = await helper.arrange.createAccounts([0n], donor);87      const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);88      await collection.setSponsor(alice, bob.address);89      await collection.confirmSponsorship(bob);90      const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);91      await collection.mint(alice, {Substrate: zeroBalance.address}, 100n);92      await collection.transfer(zeroBalance, {Substrate: alice.address}, 1n);93      const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);94      expect(bobBalanceAfter < bobBalanceBefore).to.be.true;95    });96  });9798  it('ReFungible: Transfer fees are paid by the sponsor after confirmation', async function() {99    await usingPlaygrounds(async (helper) => {100      const [zeroBalance] = await helper.arrange.createAccounts([0n], donor);101      const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});102      await collection.setSponsor(alice, bob.address);103      await collection.confirmSponsorship(bob);104      const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);105      const token = await collection.mintToken(alice, {Substrate: zeroBalance.address}, 100n);106      await token.transfer(zeroBalance, {Substrate: alice.address}, 1n);107      const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);108      expect(bobBalanceAfter < bobBalanceBefore).to.be.true;109    });110  });111112  it('CreateItem fees are paid by the sponsor after confirmation', async () => {113    await usingPlaygrounds(async (helper) => {114      const [zeroBalance] = await helper.arrange.createAccounts([0n], donor);115      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});116      await collection.setSponsor(alice, bob.address);117      await collection.confirmSponsorship(bob);118      await collection.setPermissions(alice, {access: 'AllowList', mintMode: true});119      await collection.addToAllowList(alice, {Substrate: zeroBalance.address});120121      const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);122      await collection.mintToken(zeroBalance, {Substrate: zeroBalance.address});123      const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);124125      expect(bobBalanceAfter < bobBalanceBefore).to.be.true;126    });127  });128129  it('NFT: Sponsoring of transfers is rate limited', async () => {130    await usingPlaygrounds(async (helper) => {131      const [zeroBalance] = await helper.arrange.createAccounts([0n], donor);132      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});133      await collection.setSponsor(alice, bob.address);134      await collection.confirmSponsorship(bob);135136      const token = await collection.mintToken(alice, {Substrate: alice.address});137      await token.transfer(alice, {Substrate: zeroBalance.address});138      const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);139140      const transferTx = async () => token.transfer(zeroBalance, {Substrate: alice.address});141      await expect(transferTx()).to.be.rejectedWith('Inability to pay some fees');142      const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);143144      expect(bobBalanceAfter === bobBalanceBefore).to.be.true;145    });146  });147148  it('Fungible: Sponsoring is rate limited', async () => {149    await usingPlaygrounds(async (helper) => {150      const [zeroBalance] = await helper.arrange.createAccounts([0n], donor);151      const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});152      await collection.setSponsor(alice, bob.address);153      await collection.confirmSponsorship(bob);154155      await collection.mint(alice, {Substrate: zeroBalance.address}, 100n);156      await collection.transfer(zeroBalance, {Substrate: zeroBalance.address}, 1n);157      const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);158159      const transferTx = async () => collection.transfer(zeroBalance, {Substrate: zeroBalance.address});160      await expect(transferTx()).to.be.rejected;161      const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);162163      expect(bobBalanceAfter === bobBalanceBefore).to.be.true;164    });165  });166167  it('ReFungible: Sponsoring is rate limited', async function() {168    await usingPlaygrounds(async (helper) => {169      const [zeroBalance] = await helper.arrange.createAccounts([0n], donor);170      const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});171      await collection.setSponsor(alice, bob.address);172      await collection.confirmSponsorship(bob);173174      const token = await collection.mintToken(alice, {Substrate: zeroBalance.address}, 100n);175      await token.transfer(zeroBalance, {Substrate: alice.address});176177      const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);178      const transferTx = async () => token.transfer(zeroBalance, {Substrate: alice.address});179      await expect(transferTx()).to.be.rejectedWith('Inability to pay some fees');180      const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);181182      expect(bobBalanceAfter === bobBalanceBefore).to.be.true;183    });184  });185186  it('NFT: Sponsoring of createItem is rate limited', async () => {187    await usingPlaygrounds(async (helper) => {188      const [zeroBalance] = await helper.arrange.createAccounts([0n], donor);189      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});190      await collection.setSponsor(alice, bob.address);191      await collection.confirmSponsorship(bob);192      await collection.setPermissions(alice, {mintMode: true, access: 'AllowList'});193      await collection.addToAllowList(alice, {Substrate: zeroBalance.address});194195      await collection.mintToken(zeroBalance, {Substrate: zeroBalance.address});196197      const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);198      const mintTx = async () => collection.mintToken(zeroBalance, {Substrate: zeroBalance.address});199      await expect(mintTx()).to.be.rejectedWith('Inability to pay some fees');200      const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);201202      expect(bobBalanceAfter === bobBalanceBefore).to.be.true;203    });204  });205});206207describe('(!negative test!) integration test: ext. confirmSponsorship():', () => {208  before(async () => {209    await usingPlaygrounds(async (helper) => {210      [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);211    });212  });213214  it('(!negative test!) Confirm sponsorship for a collection that never existed', async () => {215    await usingPlaygrounds(async (helper) => {216      const collectionId = 1 << 32 - 1;217      const confirmSponsorshipTx = async () => helper.collection.confirmSponsorship(bob, collectionId);218      await expect(confirmSponsorshipTx()).to.be.rejected;219    });220  });221222  it('(!negative test!) Confirm sponsorship using a non-sponsor address', async () => {223    await usingPlaygrounds(async (helper) => {224      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});225      await collection.setSponsor(alice, bob.address);226      const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);227      await expect(confirmSponsorshipTx()).to.be.rejected;228    });229  });230231  it('(!negative test!) Confirm sponsorship using owner address', async () => {232    await usingPlaygrounds(async (helper) => {233      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});234      await collection.setSponsor(alice, bob.address);235      const confirmSponsorshipTx = async () => collection.confirmSponsorship(alice);236      await expect(confirmSponsorshipTx()).to.be.rejected;237    });238  });239240  it('(!negative test!) Confirm sponsorship by collection admin', async () => {241    await usingPlaygrounds(async (helper) => {242      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});243      await collection.setSponsor(alice, bob.address);244      await collection.addAdmin(alice, {Substrate: charlie.address});245      const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);246      await expect(confirmSponsorshipTx()).to.be.rejected;247    });248  });249250  it('(!negative test!) Confirm sponsorship without sponsor being set with setCollectionSponsor', async () => {251    await usingPlaygrounds(async (helper) => {252      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});253      const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);254      await expect(confirmSponsorshipTx()).to.be.rejected;255    });256  });257258  it('(!negative test!) Confirm sponsorship in a collection that was destroyed', async () => {259    await usingPlaygrounds(async (helper) => {260      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});261      await collection.burn(alice);262      const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);263      await expect(confirmSponsorshipTx()).to.be.rejected;264    });265  });266267  it('(!negative test!) Transfer fees are not paid by the sponsor if the transfer failed', async () => {268    await usingPlaygrounds(async (helper) => {269      const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});270      await collection.setSponsor(alice, bob.address);271      await collection.confirmSponsorship(bob);272      const [ownerZeroBalance, senderZeroBalance] = await helper.arrange.createAccounts([0n, 0n], donor);273      const token = await collection.mintToken(alice, {Substrate: ownerZeroBalance.address});274      const sponsorBalanceBefore = await helper.balance.getSubstrate(bob.address);275      const transferTx = async () =>  token.transfer(senderZeroBalance, {Substrate: alice.address});276      await expect(transferTx()).to.be.rejected;277      const sponsorBalanceAfter = await helper.balance.getSubstrate(bob.address);278      expect(sponsorBalanceAfter).to.equal(sponsorBalanceBefore);279    });280  });281});