1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {usingPlaygrounds, expect, itSub, Pallets} from './util/playgrounds';1920describe('integration test: ext. confirmSponsorship():', () => {21 let alice: IKeyringPair;22 let bob: IKeyringPair;23 let charlie: IKeyringPair;24 let zeroBalance: IKeyringPair;2526 before(async () => {27 await usingPlaygrounds(async (helper, privateKey) => {28 const donor = privateKey('//Alice');29 [alice, bob, charlie, zeroBalance] = await helper.arrange.createAccounts([100n, 100n, 100n, 0n], donor);30 });31 });3233 itSub('Confirm collection sponsorship', async ({helper}) => {34 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});35 await collection.setSponsor(alice, bob.address);36 await collection.confirmSponsorship(bob);37 });3839 itSub('Add sponsor to a collection after the same sponsor was already added and confirmed', async ({helper}) => {40 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});41 await collection.setSponsor(alice, bob.address);42 await collection.confirmSponsorship(bob);43 await collection.setSponsor(alice, bob.address);44 });45 itSub('Add new sponsor to a collection after another sponsor was already added and confirmed', async ({helper}) => {46 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});47 await collection.setSponsor(alice, bob.address);48 await collection.confirmSponsorship(bob);49 await collection.setSponsor(alice, charlie.address);50 });5152 itSub('NFT: Transfer fees are paid by the sponsor after confirmation', async ({helper}) => {53 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});54 await collection.setSponsor(alice, bob.address);55 await collection.confirmSponsorship(bob);56 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);57 const token = await collection.mintToken(alice, {Substrate: zeroBalance.address});58 await token.transfer(zeroBalance, {Substrate: alice.address});59 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);60 expect(bobBalanceAfter < bobBalanceBefore).to.be.true;61 });6263 itSub('Fungible: Transfer fees are paid by the sponsor after confirmation', async ({helper}) => {64 const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);65 await collection.setSponsor(alice, bob.address);66 await collection.confirmSponsorship(bob);67 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);68 await collection.mint(alice, 100n, {Substrate: zeroBalance.address});69 await collection.transfer(zeroBalance, {Substrate: alice.address}, 1n);70 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);71 expect(bobBalanceAfter < bobBalanceBefore).to.be.true;72 });7374 itSub.ifWithPallets('ReFungible: Transfer fees are paid by the sponsor after confirmation', [Pallets.ReFungible], async ({helper}) => {75 const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});76 await collection.setSponsor(alice, bob.address);77 await collection.confirmSponsorship(bob);78 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);79 const token = await collection.mintToken(alice, 100n, {Substrate: zeroBalance.address});80 await token.transfer(zeroBalance, {Substrate: alice.address}, 1n);81 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);82 expect(bobBalanceAfter < bobBalanceBefore).to.be.true;83 });8485 itSub('CreateItem fees are paid by the sponsor after confirmation', async ({helper}) => {86 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});87 await collection.setSponsor(alice, bob.address);88 await collection.confirmSponsorship(bob);89 await collection.setPermissions(alice, {access: 'AllowList', mintMode: true});90 await collection.addToAllowList(alice, {Substrate: zeroBalance.address});9192 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);93 await collection.mintToken(zeroBalance, {Substrate: zeroBalance.address});94 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);9596 expect(bobBalanceAfter < bobBalanceBefore).to.be.true;97 });9899 itSub('NFT: Sponsoring of transfers is rate limited', async ({helper}) => {100 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});101 await collection.setSponsor(alice, bob.address);102 await collection.confirmSponsorship(bob);103104 const token = await collection.mintToken(alice, {Substrate: alice.address});105 await token.transfer(alice, {Substrate: zeroBalance.address});106 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);107108 const transferTx = async () => token.transfer(zeroBalance, {Substrate: alice.address});109 await expect(transferTx()).to.be.rejectedWith('Inability to pay some fees');110 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);111112 expect(bobBalanceAfter === bobBalanceBefore).to.be.true;113 });114115 itSub('Fungible: Sponsoring is rate limited', async ({helper}) => {116 const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});117 await collection.setSponsor(alice, bob.address);118 await collection.confirmSponsorship(bob);119120 await collection.mint(alice, 100n, {Substrate: zeroBalance.address});121 await collection.transfer(zeroBalance, {Substrate: zeroBalance.address}, 1n);122 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);123124 const transferTx = async () => collection.transfer(zeroBalance, {Substrate: zeroBalance.address});125 await expect(transferTx()).to.be.rejected;126 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);127128 expect(bobBalanceAfter === bobBalanceBefore).to.be.true;129 });130131 itSub.ifWithPallets('ReFungible: Sponsoring is rate limited', [Pallets.ReFungible], async ({helper}) => {132 const collection = await helper.rft.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, 100n, {Substrate: zeroBalance.address});137 await token.transfer(zeroBalance, {Substrate: alice.address});138139 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);140 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 });146147 itSub('NFT: Sponsoring of createItem is rate limited', async ({helper}) => {148 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});149 await collection.setSponsor(alice, bob.address);150 await collection.confirmSponsorship(bob);151 await collection.setPermissions(alice, {mintMode: true, access: 'AllowList'});152 await collection.addToAllowList(alice, {Substrate: zeroBalance.address});153154 await collection.mintToken(zeroBalance, {Substrate: zeroBalance.address});155156 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);157 const mintTx = async () => collection.mintToken(zeroBalance, {Substrate: zeroBalance.address});158 await expect(mintTx()).to.be.rejectedWith('Inability to pay some fees');159 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);160161 expect(bobBalanceAfter === bobBalanceBefore).to.be.true;162 });163});164165describe('(!negative test!) integration test: ext. confirmSponsorship():', () => {166 let alice: IKeyringPair;167 let bob: IKeyringPair;168 let charlie: IKeyringPair;169 let ownerZeroBalance: IKeyringPair;170 let senderZeroBalance: IKeyringPair;171172 before(async () => {173 await usingPlaygrounds(async (helper, privateKey) => {174 const donor = privateKey('//Alice');175 [alice, bob, charlie, ownerZeroBalance, senderZeroBalance] = await helper.arrange.createAccounts([100n, 100n, 100n, 0n, 0n], donor);176 });177 });178179 itSub('(!negative test!) Confirm sponsorship for a collection that never existed', async ({helper}) => {180 const collectionId = 1_000_000;181 const confirmSponsorshipTx = async () => helper.collection.confirmSponsorship(bob, collectionId);182 await expect(confirmSponsorshipTx()).to.be.rejectedWith(/common\.CollectionNotFound/);183 });184185 itSub('(!negative test!) Confirm sponsorship using a non-sponsor address', async ({helper}) => {186 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});187 await collection.setSponsor(alice, bob.address);188 const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);189 await expect(confirmSponsorshipTx()).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);190 });191192 itSub('(!negative test!) Confirm sponsorship using owner address', async ({helper}) => {193 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});194 await collection.setSponsor(alice, bob.address);195 const confirmSponsorshipTx = async () => collection.confirmSponsorship(alice);196 await expect(confirmSponsorshipTx()).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);197 });198199 itSub('(!negative test!) Confirm sponsorship by collection admin', async ({helper}) => {200 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});201 await collection.setSponsor(alice, bob.address);202 await collection.addAdmin(alice, {Substrate: charlie.address});203 const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);204 await expect(confirmSponsorshipTx()).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);205 });206207 itSub('(!negative test!) Confirm sponsorship without sponsor being set with setCollectionSponsor', async ({helper}) => {208 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});209 const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);210 await expect(confirmSponsorshipTx()).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);211 });212213 itSub('(!negative test!) Confirm sponsorship in a collection that was destroyed', async ({helper}) => {214 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});215 await collection.burn(alice);216 const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);217 await expect(confirmSponsorshipTx()).to.be.rejectedWith(/common\.CollectionNotFound/);218 });219220 itSub('(!negative test!) Transfer fees are not paid by the sponsor if the transfer failed', async ({helper}) => {221 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});222 await collection.setSponsor(alice, bob.address);223 await collection.confirmSponsorship(bob);224 const token = await collection.mintToken(alice, {Substrate: ownerZeroBalance.address});225 const sponsorBalanceBefore = await helper.balance.getSubstrate(bob.address);226 const transferTx = async () => token.transfer(senderZeroBalance, {Substrate: alice.address});227 await expect(transferTx()).to.be.rejectedWith('Inability to pay some fees');228 const sponsorBalanceAfter = await helper.balance.getSubstrate(bob.address);229 expect(sponsorBalanceAfter).to.equal(sponsorBalanceBefore);230 });231});