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

difftreelog

source

tests/src/confirmSponsorship.test.ts13.0 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 {IKeyringPair} from '@polkadot/types/types';18import {usingPlaygrounds, expect, itSub, Pallets} from './util/playgrounds';1920async function setSponsorHelper(collection: any, signer: IKeyringPair, sponsorAddress: string) {21  await collection.setSponsor(signer, sponsorAddress);22  const raw = (await collection.getData())?.raw;23  expect(raw.sponsorship.Unconfirmed).to.be.equal(sponsorAddress);24}2526async function confirmSponsorHelper(collection: any, signer: IKeyringPair) {27  await collection.confirmSponsorship(signer);28  const raw = (await collection.getData())?.raw;29  expect(raw.sponsorship.Confirmed).to.be.equal(signer.address);30}3132describe('integration test: ext. confirmSponsorship():', () => {33  let alice: IKeyringPair;34  let bob: IKeyringPair;35  let charlie: IKeyringPair;36  let zeroBalance: IKeyringPair;3738  before(async () => {39    await usingPlaygrounds(async (helper, privateKey) => {40      const donor = privateKey('//Alice');41      [alice, bob, charlie, zeroBalance] = await helper.arrange.createAccounts([100n, 100n, 100n, 0n], donor);42    });43  });4445  itSub('Confirm collection sponsorship', async ({helper}) => {46    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});47    await setSponsorHelper(collection, alice, bob.address);48    await confirmSponsorHelper(collection, bob);49  });5051  itSub('Add sponsor to a collection after the same sponsor was already added and confirmed', async ({helper}) => {52    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});53    await setSponsorHelper(collection, alice, bob.address);54    await confirmSponsorHelper(collection, bob);55    await setSponsorHelper(collection, alice, bob.address);56  });57  itSub('Add new sponsor to a collection after another sponsor was already added and confirmed', async ({helper}) => {58    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});59    await setSponsorHelper(collection, alice, bob.address);60    await confirmSponsorHelper(collection, bob);61    await setSponsorHelper(collection, alice, charlie.address);62  });6364  itSub('NFT: Transfer fees are paid by the sponsor after confirmation', async ({helper}) => {65    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});66    await collection.setSponsor(alice, bob.address);67    await collection.confirmSponsorship(bob);68    const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);69    const token = await collection.mintToken(alice, {Substrate: zeroBalance.address});70    await token.transfer(zeroBalance, {Substrate: alice.address});71    const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);72    expect(bobBalanceAfter < bobBalanceBefore).to.be.true;73  });7475  itSub('Fungible: Transfer fees are paid by the sponsor after confirmation', async ({helper}) => {76    const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);77    await collection.setSponsor(alice, bob.address);78    await collection.confirmSponsorship(bob);79    const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);80    await collection.mint(alice, 100n, {Substrate: zeroBalance.address});81    await collection.transfer(zeroBalance, {Substrate: alice.address}, 1n);82    const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);83    expect(bobBalanceAfter < bobBalanceBefore).to.be.true;84  });8586  itSub.ifWithPallets('ReFungible: Transfer fees are paid by the sponsor after confirmation', [Pallets.ReFungible], async ({helper}) => {87    const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});88    await collection.setSponsor(alice, bob.address);89    await collection.confirmSponsorship(bob);90    const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);91    const token = await collection.mintToken(alice, 100n, {Substrate: zeroBalance.address});92    await token.transfer(zeroBalance, {Substrate: alice.address}, 1n);93    const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);94    expect(bobBalanceAfter < bobBalanceBefore).to.be.true;95  });9697  itSub('CreateItem fees are paid by the sponsor after confirmation', async ({helper}) => {98    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});99    await collection.setSponsor(alice, bob.address);100    await collection.confirmSponsorship(bob);101    await collection.setPermissions(alice, {access: 'AllowList', mintMode: true});102    await collection.addToAllowList(alice, {Substrate: zeroBalance.address});103104    const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);105    await collection.mintToken(zeroBalance, {Substrate: zeroBalance.address});106    const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);107108    expect(bobBalanceAfter < bobBalanceBefore).to.be.true;109  });110111  itSub('NFT: Sponsoring of transfers is rate limited', async ({helper}) => {112    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});113    await collection.setSponsor(alice, bob.address);114    await collection.confirmSponsorship(bob);115116    const token = await collection.mintToken(alice, {Substrate: alice.address});117    await token.transfer(alice, {Substrate: zeroBalance.address});118    const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);119120    const transferTx = async () => token.transfer(zeroBalance, {Substrate: alice.address});121    await expect(transferTx()).to.be.rejectedWith('Inability to pay some fees');122    const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);123124    expect(bobBalanceAfter === bobBalanceBefore).to.be.true;125  });126127  itSub('Fungible: Sponsoring is rate limited', async ({helper}) => {128    const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});129    await collection.setSponsor(alice, bob.address);130    await collection.confirmSponsorship(bob);131132    await collection.mint(alice, 100n, {Substrate: zeroBalance.address});133    await collection.transfer(zeroBalance, {Substrate: zeroBalance.address}, 1n);134    const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);135136    const transferTx = async () => collection.transfer(zeroBalance, {Substrate: zeroBalance.address});137    await expect(transferTx()).to.be.rejected;138    const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);139140    expect(bobBalanceAfter === bobBalanceBefore).to.be.true;141  });142143  itSub.ifWithPallets('ReFungible: Sponsoring is rate limited', [Pallets.ReFungible], async ({helper}) => {144    const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});145    await collection.setSponsor(alice, bob.address);146    await collection.confirmSponsorship(bob);147148    const token = await collection.mintToken(alice, 100n, {Substrate: zeroBalance.address});149    await token.transfer(zeroBalance, {Substrate: alice.address});150151    const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);152    const transferTx = async () => token.transfer(zeroBalance, {Substrate: alice.address});153    await expect(transferTx()).to.be.rejectedWith('Inability to pay some fees');154    const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);155156    expect(bobBalanceAfter === bobBalanceBefore).to.be.true;157  });158159  itSub('NFT: Sponsoring of createItem is rate limited', async ({helper}) => {160    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});161    await collection.setSponsor(alice, bob.address);162    await collection.confirmSponsorship(bob);163    await collection.setPermissions(alice, {mintMode: true, access: 'AllowList'});164    await collection.addToAllowList(alice, {Substrate: zeroBalance.address});165166    await collection.mintToken(zeroBalance, {Substrate: zeroBalance.address});167168    const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);169    const mintTx = async () => collection.mintToken(zeroBalance, {Substrate: zeroBalance.address});170    await expect(mintTx()).to.be.rejectedWith('Inability to pay some fees');171    const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);172173    expect(bobBalanceAfter === bobBalanceBefore).to.be.true;174  });175});176177describe('(!negative test!) integration test: ext. confirmSponsorship():', () => {178  let alice: IKeyringPair;179  let bob: IKeyringPair;180  let charlie: IKeyringPair;181  let ownerZeroBalance: IKeyringPair;182  let senderZeroBalance: IKeyringPair;183184  before(async () => {185    await usingPlaygrounds(async (helper, privateKey) => {186      const donor = privateKey('//Alice');187      [alice, bob, charlie, ownerZeroBalance, senderZeroBalance] = await helper.arrange.createAccounts([100n, 100n, 100n, 0n, 0n], donor);188    });189  });190191  itSub('(!negative test!) Confirm sponsorship for a collection that never existed', async ({helper}) => {192    const collectionId = 1_000_000;193    const confirmSponsorshipTx = async () => helper.collection.confirmSponsorship(bob, collectionId);194    await expect(confirmSponsorshipTx()).to.be.rejectedWith(/common\.CollectionNotFound/);195  });196197  itSub('(!negative test!) Confirm sponsorship using a non-sponsor address', async ({helper}) => {198    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});199    await collection.setSponsor(alice, bob.address);200    const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);201    await expect(confirmSponsorshipTx()).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);202  });203204  itSub('(!negative test!) Confirm sponsorship using owner address', async ({helper}) => {205    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});206    await collection.setSponsor(alice, bob.address);207    const confirmSponsorshipTx = async () => collection.confirmSponsorship(alice);208    await expect(confirmSponsorshipTx()).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);209  });210211  itSub('(!negative test!) Confirm sponsorship by collection admin', async ({helper}) => {212    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});213    await collection.setSponsor(alice, bob.address);214    await collection.addAdmin(alice, {Substrate: charlie.address});215    const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);216    await expect(confirmSponsorshipTx()).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);217  });218219  itSub('(!negative test!) Confirm sponsorship without sponsor being set with setCollectionSponsor', async ({helper}) => {220    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});221    const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);222    await expect(confirmSponsorshipTx()).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);223  });224225  itSub('(!negative test!) Confirm sponsorship in a collection that was destroyed', async ({helper}) => {226    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});227    await collection.burn(alice);228    const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);229    await expect(confirmSponsorshipTx()).to.be.rejectedWith(/common\.CollectionNotFound/);230  });231232  itSub('(!negative test!) Transfer fees are not paid by the sponsor if the transfer failed', async ({helper}) => {233    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});234    await collection.setSponsor(alice, bob.address);235    await collection.confirmSponsorship(bob);236    const token = await collection.mintToken(alice, {Substrate: ownerZeroBalance.address});237    const sponsorBalanceBefore = await helper.balance.getSubstrate(bob.address);238    const transferTx = async () =>  token.transfer(senderZeroBalance, {Substrate: alice.address});239    await expect(transferTx()).to.be.rejectedWith('Inability to pay some fees');240    const sponsorBalanceAfter = await helper.balance.getSubstrate(bob.address);241    expect(sponsorBalanceAfter).to.equal(sponsorBalanceBefore);242  });243});