1234567891011121314151617import {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 = await privateKey({filename: __filename});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', limits: {113 sponsorTransferTimeout: 1000,114 }});115 await collection.setSponsor(alice, bob.address);116 await collection.confirmSponsorship(bob);117118 const token = await collection.mintToken(alice, {Substrate: alice.address});119 await token.transfer(alice, {Substrate: zeroBalance.address});120 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);121122 const transferTx = async () => token.transfer(zeroBalance, {Substrate: alice.address});123 await expect(transferTx()).to.be.rejectedWith('Inability to pay some fees');124 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);125126 expect(bobBalanceAfter === bobBalanceBefore).to.be.true;127 });128129 itSub('Fungible: Sponsoring is rate limited', async ({helper}) => {130 const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL', limits: {131 sponsorTransferTimeout: 1000,132 }});133 await collection.setSponsor(alice, bob.address);134 await collection.confirmSponsorship(bob);135136 await collection.mint(alice, 100n, {Substrate: zeroBalance.address});137 await collection.transfer(zeroBalance, {Substrate: zeroBalance.address}, 1n);138 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);139140 const transferTx = async () => collection.transfer(zeroBalance, {Substrate: zeroBalance.address});141 await expect(transferTx()).to.be.rejected;142 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);143144 expect(bobBalanceAfter === bobBalanceBefore).to.be.true;145 });146147 itSub.ifWithPallets('ReFungible: Sponsoring is rate limited', [Pallets.ReFungible], async ({helper}) => {148 const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL', limits: {149 sponsorTransferTimeout: 1000,150 }});151 await collection.setSponsor(alice, bob.address);152 await collection.confirmSponsorship(bob);153154 const token = await collection.mintToken(alice, 100n, {Substrate: zeroBalance.address});155 await token.transfer(zeroBalance, {Substrate: alice.address});156157 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);158 const transferTx = async () => token.transfer(zeroBalance, {Substrate: alice.address});159 await expect(transferTx()).to.be.rejectedWith('Inability to pay some fees');160 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);161162 expect(bobBalanceAfter === bobBalanceBefore).to.be.true;163 });164165 itSub('NFT: Sponsoring of createItem is rate limited', async ({helper}) => {166 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});167 await collection.setSponsor(alice, bob.address);168 await collection.confirmSponsorship(bob);169 await collection.setPermissions(alice, {mintMode: true, access: 'AllowList'});170 await collection.addToAllowList(alice, {Substrate: zeroBalance.address});171172 await collection.mintToken(zeroBalance, {Substrate: zeroBalance.address});173174 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);175 const mintTx = async () => collection.mintToken(zeroBalance, {Substrate: zeroBalance.address});176 await expect(mintTx()).to.be.rejectedWith('Inability to pay some fees');177 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);178179 expect(bobBalanceAfter === bobBalanceBefore).to.be.true;180 });181});182183describe('(!negative test!) integration test: ext. confirmSponsorship():', () => {184 let alice: IKeyringPair;185 let bob: IKeyringPair;186 let charlie: IKeyringPair;187 let ownerZeroBalance: IKeyringPair;188 let senderZeroBalance: IKeyringPair;189190 before(async () => {191 await usingPlaygrounds(async (helper, privateKey) => {192 const donor = await privateKey({filename: __filename});193 [alice, bob, charlie, ownerZeroBalance, senderZeroBalance] = await helper.arrange.createAccounts([100n, 100n, 100n, 0n, 0n], donor);194 });195 });196197 itSub('(!negative test!) Confirm sponsorship for a collection that never existed', async ({helper}) => {198 const collectionId = 1_000_000;199 const confirmSponsorshipTx = async () => helper.collection.confirmSponsorship(bob, collectionId);200 await expect(confirmSponsorshipTx()).to.be.rejectedWith(/common\.CollectionNotFound/);201 });202203 itSub('(!negative test!) Confirm sponsorship using a non-sponsor address', async ({helper}) => {204 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});205 await collection.setSponsor(alice, bob.address);206 const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);207 await expect(confirmSponsorshipTx()).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);208 });209210 itSub('(!negative test!) Confirm sponsorship using owner address', async ({helper}) => {211 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});212 await collection.setSponsor(alice, bob.address);213 const confirmSponsorshipTx = async () => collection.confirmSponsorship(alice);214 await expect(confirmSponsorshipTx()).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);215 });216217 itSub('(!negative test!) Confirm sponsorship by collection admin', async ({helper}) => {218 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});219 await collection.setSponsor(alice, bob.address);220 await collection.addAdmin(alice, {Substrate: charlie.address});221 const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);222 await expect(confirmSponsorshipTx()).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);223 });224225 itSub('(!negative test!) Confirm sponsorship without sponsor being set with setCollectionSponsor', async ({helper}) => {226 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});227 const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);228 await expect(confirmSponsorshipTx()).to.be.rejectedWith(/unique\.ConfirmUnsetSponsorFail/);229 });230231 itSub('(!negative test!) Confirm sponsorship in a collection that was destroyed', async ({helper}) => {232 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});233 await collection.burn(alice);234 const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);235 await expect(confirmSponsorshipTx()).to.be.rejectedWith(/common\.CollectionNotFound/);236 });237238 itSub('(!negative test!) Transfer fees are not paid by the sponsor if the transfer failed', async ({helper}) => {239 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});240 await collection.setSponsor(alice, bob.address);241 await collection.confirmSponsorship(bob);242 const token = await collection.mintToken(alice, {Substrate: ownerZeroBalance.address});243 const sponsorBalanceBefore = await helper.balance.getSubstrate(bob.address);244 const transferTx = async () => token.transfer(senderZeroBalance, {Substrate: alice.address});245 await expect(transferTx()).to.be.rejectedWith('Inability to pay some fees');246 const sponsorBalanceAfter = await helper.balance.getSubstrate(bob.address);247 expect(sponsorBalanceAfter).to.equal(sponsorBalanceBefore);248 });249});