difftreelog
confirmSponsorship migrated
in: master
1 file changed
tests/src/confirmSponsorship.test.tsdiffbeforeafterboth161617import chai from 'chai';17import chai from 'chai';18import chaiAsPromised from 'chai-as-promised';18import chaiAsPromised from 'chai-as-promised';19import {default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync} from './substrate/substrate-api';20import {21 createCollectionExpectSuccess,22 setCollectionSponsorExpectSuccess,23 destroyCollectionExpectSuccess,24 confirmSponsorshipExpectSuccess,25 confirmSponsorshipExpectFailure,26 createItemExpectSuccess,27 findUnusedAddress,28 getGenericResult,29 enableAllowListExpectSuccess,30 enablePublicMintingExpectSuccess,31 addToAllowListExpectSuccess,32 normalizeAccountId,33 addCollectionAdminExpectSuccess,34 getCreatedCollectionCount,35 UNIQUE,36 requirePallets,37 Pallets,38} from './util/helpers';39import {IKeyringPair} from '@polkadot/types/types';19import {IKeyringPair} from '@polkadot/types/types';40import {usingPlaygrounds} from './util/playgrounds';20import {usingPlaygrounds} from './util/playgrounds';4121583859 before(async () => {39 before(async () => {60 await usingPlaygrounds(async (helper) => {40 await usingPlaygrounds(async (helper) => {61 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 0n], donor);41 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);62 });42 });63 });43 });644482 await usingPlaygrounds(async (helper) => {62 await usingPlaygrounds(async (helper) => {83 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});63 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});84 await collection.setSponsor(alice, bob.address);64 await collection.setSponsor(alice, bob.address);85 await collection.confirmSponsorship(charlie);65 await collection.confirmSponsorship(bob);66 await collection.setSponsor(alice, charlie.address);86 });67 });87 });68 });886989 it('NFT: Transfer fees are paid by the sponsor after confirmation', async () => {70 it('NFT: Transfer fees are paid by the sponsor after confirmation', async () => {90 await usingPlaygrounds(async (helper) => {71 await usingPlaygrounds(async (helper) => {72 const [zeroBalance] = await helper.arrange.createAccounts([0n], donor);91 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});73 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});92 await collection.setSponsor(alice, bob.address);74 await collection.setSponsor(alice, bob.address);93 await collection.confirmSponsorship(bob);75 await collection.confirmSponsorship(bob);94 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);76 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);95 const token = await collection.mintToken(alice, {Substrate: charlie.address});77 const token = await collection.mintToken(alice, {Substrate: zeroBalance.address});96 await token.transfer(charlie, {Substrate: alice.address});78 await token.transfer(zeroBalance, {Substrate: alice.address});97 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);79 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);98 expect(bobBalanceAfter < bobBalanceBefore).to.be.true;80 expect(bobBalanceAfter < bobBalanceBefore).to.be.true;99 });81 });100 });82 });10183102 it('Fungible: Transfer fees are paid by the sponsor after confirmation', async () => {84 it('Fungible: Transfer fees are paid by the sponsor after confirmation', async () => {103 await usingPlaygrounds(async (helper) => {85 await usingPlaygrounds(async (helper) => {86 const [zeroBalance] = await helper.arrange.createAccounts([0n], donor);104 const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);87 const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);105 await collection.setSponsor(alice, bob.address);88 await collection.setSponsor(alice, bob.address);106 await collection.confirmSponsorship(bob);89 await collection.confirmSponsorship(bob);107 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);90 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);108 await collection.mint(alice, {Substrate: charlie.address}, 100n);91 await collection.mint(alice, {Substrate: zeroBalance.address}, 100n);109 await collection.transfer(charlie, {Substrate: alice.address}, 1n);92 await collection.transfer(zeroBalance, {Substrate: alice.address}, 1n);110 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);93 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);111 expect(bobBalanceAfter < bobBalanceBefore).to.be.true;94 expect(bobBalanceAfter < bobBalanceBefore).to.be.true;112 });95 });113 });96 });11497115 it('ReFungible: Transfer fees are paid by the sponsor after confirmation', async function() {98 it('ReFungible: Transfer fees are paid by the sponsor after confirmation', async function() {116 await usingPlaygrounds(async (helper) => {99 await usingPlaygrounds(async (helper) => {100 const [zeroBalance] = await helper.arrange.createAccounts([0n], donor);117 const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});101 const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});118 await collection.setSponsor(alice, bob.address);102 await collection.setSponsor(alice, bob.address);119 await collection.confirmSponsorship(bob);103 await collection.confirmSponsorship(bob);120 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);104 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);121 const token = await collection.mintToken(alice, {Substrate: charlie.address}, 100n);105 const token = await collection.mintToken(alice, {Substrate: zeroBalance.address}, 100n);122 await token.transfer(charlie, {Substrate: alice.address}, 1n);106 await token.transfer(zeroBalance, {Substrate: alice.address}, 1n);123 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);107 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);124 expect(bobBalanceAfter < bobBalanceBefore).to.be.true;108 expect(bobBalanceAfter < bobBalanceBefore).to.be.true;125 });109 });126 });110 });127111128 it('CreateItem fees are paid by the sponsor after confirmation', async () => {112 it('CreateItem fees are paid by the sponsor after confirmation', async () => {129 await usingPlaygrounds(async (helper) => {113 await usingPlaygrounds(async (helper) => {114 const [zeroBalance] = await helper.arrange.createAccounts([0n], donor);130 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});115 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});131 await collection.setSponsor(alice, bob.address);116 await collection.setSponsor(alice, bob.address);132 await collection.confirmSponsorship(bob);117 await collection.confirmSponsorship(bob);133 await collection.setPermissions(alice, {access: 'AllowList', mintMode: true});118 await collection.setPermissions(alice, {access: 'AllowList', mintMode: true});134 await collection.addToAllowList(alice, {Substrate: charlie.address});119 await collection.addToAllowList(alice, {Substrate: zeroBalance.address});135120136 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);121 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);137 await collection.mintToken(charlie, {Substrate: charlie.address});122 await collection.mintToken(zeroBalance, {Substrate: zeroBalance.address});138 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);123 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);139124140 expect(bobBalanceAfter < bobBalanceBefore).to.be.true;125 expect(bobBalanceAfter < bobBalanceBefore).to.be.true;143128144 it('NFT: Sponsoring of transfers is rate limited', async () => {129 it('NFT: Sponsoring of transfers is rate limited', async () => {145 await usingPlaygrounds(async (helper) => {130 await usingPlaygrounds(async (helper) => {131 const [zeroBalance] = await helper.arrange.createAccounts([0n], donor);146 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});132 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});147 await collection.setSponsor(alice, bob.address);133 await collection.setSponsor(alice, bob.address);148 await collection.confirmSponsorship(bob);134 await collection.confirmSponsorship(bob);149135150 const token = await collection.mintToken(alice, {Substrate: alice.address});136 const token = await collection.mintToken(alice, {Substrate: alice.address});151 await token.transfer(alice, {Substrate: charlie.address});137 await token.transfer(alice, {Substrate: zeroBalance.address});152 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);138 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);153139154 const transferTx = async () => token.transfer(charlie, {Substrate: alice.address});140 const transferTx = async () => token.transfer(zeroBalance, {Substrate: alice.address});155 await expect(transferTx()).to.be.rejectedWith('Inability to pay some fees');141 await expect(transferTx()).to.be.rejectedWith('Inability to pay some fees');156 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);142 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);157143161147162 it('Fungible: Sponsoring is rate limited', async () => {148 it('Fungible: Sponsoring is rate limited', async () => {163 await usingPlaygrounds(async (helper) => {149 await usingPlaygrounds(async (helper) => {150 const [zeroBalance] = await helper.arrange.createAccounts([0n], donor);164 const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});151 const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});165 await collection.setSponsor(alice, bob.address);152 await collection.setSponsor(alice, bob.address);166 await collection.confirmSponsorship(bob);153 await collection.confirmSponsorship(bob);167154168 await collection.mint(alice, {Substrate: charlie.address}, 100n);155 await collection.mint(alice, {Substrate: zeroBalance.address}, 100n);169 await collection.transfer(charlie, {Substrate: charlie.address}, 1n);156 await collection.transfer(zeroBalance, {Substrate: zeroBalance.address}, 1n);170 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);157 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);171158172 const transferTx = async () => collection.transfer(charlie, {Substrate: charlie.address});159 const transferTx = async () => collection.transfer(zeroBalance, {Substrate: zeroBalance.address});173 await expect(transferTx()).to.be.rejected;160 await expect(transferTx()).to.be.rejected;174 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);161 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);175162179166180 it('ReFungible: Sponsoring is rate limited', async function() {167 it('ReFungible: Sponsoring is rate limited', async function() {181 await usingPlaygrounds(async (helper) => {168 await usingPlaygrounds(async (helper) => {169 const [zeroBalance] = await helper.arrange.createAccounts([0n], donor);182 const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});170 const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});183 await collection.setSponsor(alice, bob.address);171 await collection.setSponsor(alice, bob.address);184 await collection.confirmSponsorship(bob);172 await collection.confirmSponsorship(bob);185173186 const token = await collection.mintToken(alice, {Substrate: charlie.address}, 100n);174 const token = await collection.mintToken(alice, {Substrate: zeroBalance.address}, 100n);187 await token.transfer(charlie, {Substrate: alice.address});175 await token.transfer(zeroBalance, {Substrate: alice.address});188176189 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);177 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);190 const transferTx = async () => token.transfer(charlie, {Substrate: alice.address});178 const transferTx = async () => token.transfer(zeroBalance, {Substrate: alice.address});191 await expect(transferTx()).to.be.rejectedWith('Inability to pay some fees');179 await expect(transferTx()).to.be.rejectedWith('Inability to pay some fees');192 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);180 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);193181197185198 it('NFT: Sponsoring of createItem is rate limited', async () => {186 it('NFT: Sponsoring of createItem is rate limited', async () => {199 await usingPlaygrounds(async (helper) => {187 await usingPlaygrounds(async (helper) => {188 const [zeroBalance] = await helper.arrange.createAccounts([0n], donor);200 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});189 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});201 await collection.setSponsor(alice, bob.address);190 await collection.setSponsor(alice, bob.address);202 await collection.confirmSponsorship(bob);191 await collection.confirmSponsorship(bob);203 await collection.setPermissions(alice, {mintMode: true, access: 'AllowList'});192 await collection.setPermissions(alice, {mintMode: true, access: 'AllowList'});204 await collection.addToAllowList(alice, {Substrate: charlie.address});193 await collection.addToAllowList(alice, {Substrate: zeroBalance.address});205194206 await collection.mintToken(charlie, {Substrate: charlie.address});195 await collection.mintToken(zeroBalance, {Substrate: zeroBalance.address});207196208 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);197 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);209 const mintTx = async () => collection.mintToken(charlie, {Substrate: charlie.address});198 const mintTx = async () => collection.mintToken(zeroBalance, {Substrate: zeroBalance.address});210 await expect(mintTx()).to.be.rejectedWith('Inability to pay some fees');199 await expect(mintTx()).to.be.rejectedWith('Inability to pay some fees');211 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);200 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);212201217206218describe('(!negative test!) integration test: ext. confirmSponsorship():', () => {207describe('(!negative test!) integration test: ext. confirmSponsorship():', () => {219 before(async () => {208 before(async () => {220 await usingApi(async (api, privateKeyWrapper) => {209 await usingPlaygrounds(async (helper) => {221 alice = privateKeyWrapper('//Alice');210 [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);222 bob = privateKeyWrapper('//Bob');223 charlie = privateKeyWrapper('//Charlie');224 });211 });225 });212 });226213227 it('(!negative test!) Confirm sponsorship for a collection that never existed', async () => {214 it('(!negative test!) Confirm sponsorship for a collection that never existed', async () => {228 // Find the collection that never existed229 let collectionId = 0;230 await usingApi(async (api) => {215 await usingPlaygrounds(async (helper) => {231 collectionId = await getCreatedCollectionCount(api) + 1;216 const collectionId = 1 << 32 - 1;217 const confirmSponsorshipTx = async () => helper.collection.confirmSponsorship(bob, collectionId);218 await expect(confirmSponsorshipTx()).to.be.rejected;232 });219 });233234 await confirmSponsorshipExpectFailure(collectionId, '//Bob');235 });220 });236221237 it('(!negative test!) Confirm sponsorship using a non-sponsor address', async () => {222 it('(!negative test!) Confirm sponsorship using a non-sponsor address', async () => {238 const collectionId = await createCollectionExpectSuccess();239 await setCollectionSponsorExpectSuccess(collectionId, bob.address);240241 await usingApi(async (api) => {223 await usingPlaygrounds(async (helper) => {242 const transfer = api.tx.balances.transfer(charlie.address, 1e15);224 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});243 await submitTransactionAsync(alice, transfer);225 await collection.setSponsor(alice, bob.address);226 const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);227 await expect(confirmSponsorshipTx()).to.be.rejected;244 });228 });245246 await confirmSponsorshipExpectFailure(collectionId, '//Charlie');247 });229 });248230249 it('(!negative test!) Confirm sponsorship using owner address', async () => {231 it('(!negative test!) Confirm sponsorship using owner address', async () => {250 const collectionId = await createCollectionExpectSuccess();251 await setCollectionSponsorExpectSuccess(collectionId, bob.address);232 await usingPlaygrounds(async (helper) => {252 await confirmSponsorshipExpectFailure(collectionId, '//Alice');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 });253 });238 });254239255 it('(!negative test!) Confirm sponsorship by collection admin', async () => {240 it('(!negative test!) Confirm sponsorship by collection admin', async () => {256 const collectionId = await createCollectionExpectSuccess();241 await usingPlaygrounds(async (helper) => {257 await setCollectionSponsorExpectSuccess(collectionId, bob.address);242 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});243 await collection.setSponsor(alice, bob.address);258 await addCollectionAdminExpectSuccess(alice, collectionId, charlie.address);244 await collection.addAdmin(alice, {Substrate: charlie.address});245 const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);259 await confirmSponsorshipExpectFailure(collectionId, '//Charlie');246 await expect(confirmSponsorshipTx()).to.be.rejected;247 });260 });248 });261249262 it('(!negative test!) Confirm sponsorship without sponsor being set with setCollectionSponsor', async () => {250 it('(!negative test!) Confirm sponsorship without sponsor being set with setCollectionSponsor', async () => {251 await usingPlaygrounds(async (helper) => {263 const collectionId = await createCollectionExpectSuccess();252 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});264 await confirmSponsorshipExpectFailure(collectionId, '//Bob');253 const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);254 await expect(confirmSponsorshipTx()).to.be.rejected;255 });265 });256 });266257267 it('(!negative test!) Confirm sponsorship in a collection that was destroyed', async () => {258 it('(!negative test!) Confirm sponsorship in a collection that was destroyed', async () => {259 await usingPlaygrounds(async (helper) => {268 const collectionId = await createCollectionExpectSuccess();260 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});269 await destroyCollectionExpectSuccess(collectionId);261 await collection.burn(alice);270 await confirmSponsorshipExpectFailure(collectionId, '//Bob');262 const confirmSponsorshipTx = async () => collection.confirmSponsorship(charlie);263 await expect(confirmSponsorshipTx()).to.be.rejected;264 });271 });265 });272266273 it('(!negative test!) Transfer fees are not paid by the sponsor if the transfer failed', async () => {267 it('(!negative test!) Transfer fees are not paid by the sponsor if the transfer failed', async () => {274 const collectionId = await createCollectionExpectSuccess();275 await setCollectionSponsorExpectSuccess(collectionId, bob.address);276 await confirmSponsorshipExpectSuccess(collectionId, '//Bob');277278 await usingApi(async (api, privateKeyWrapper) => {268 await usingPlaygrounds(async (helper) => {279 // Find unused address280 const ownerZeroBalance = await findUnusedAddress(api, privateKeyWrapper);269 const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});281270 await collection.setSponsor(alice, bob.address);282 // Find another unused address271 await collection.confirmSponsorship(bob);283 const senderZeroBalance = await findUnusedAddress(api, privateKeyWrapper);272 const [ownerZeroBalance, senderZeroBalance] = await helper.arrange.createAccounts([0n, 0n], donor);284285 // Mint token for an unused address286 const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', ownerZeroBalance.address);273 const token = await collection.mintToken(alice, {Substrate: ownerZeroBalance.address});287288 const sponsorBalanceBeforeTx = (await api.query.system.account(bob.address)).data.free.toBigInt();274 const sponsorBalanceBefore = await helper.balance.getSubstrate(bob.address);289290 // Try to transfer this token from an unsponsored unused adress to Alice291 const zeroToAlice = api.tx.unique.transfer(normalizeAccountId(alice.address), collectionId, itemId, 0);275 const transferTx = async () => token.transfer(senderZeroBalance, {Substrate: alice.address});292 await expect(submitTransactionExpectFailAsync(senderZeroBalance, zeroToAlice)).to.be.rejected;276 await expect(transferTx()).to.be.rejected;293294 const sponsorBalanceAfterTx = (await api.query.system.account(bob.address)).data.free.toBigInt();277 const sponsorBalanceAfter = await helper.balance.getSubstrate(bob.address);295296 expect(sponsorBalanceAfterTx).to.equal(sponsorBalanceBeforeTx);278 expect(sponsorBalanceAfter).to.equal(sponsorBalanceBefore);297 });279 });298 });280 });299});281});