1import {addToAllowListExpectSuccess, bigIntToSub, confirmSponsorshipExpectSuccess, createCollectionExpectSuccess, enablePublicMintingExpectSuccess, getDetailedCollectionInfo, setCollectionSponsorExpectSuccess} from '../util/helpers';2import {itWeb3, createEthAccount, collectionIdToAddress, GAS_ARGS, normalizeEvents, createEthAccountWithBalance, evmCollectionHelpers, getCollectionAddressFromResult, evmCollection, ethBalanceViaSub} from './util/helpers';3import nonFungibleAbi from './nonFungibleAbi.json';4import {expect} from 'chai';5import {evmToAddress} from '@polkadot/util-crypto';6import {submitTransactionAsync} from '../substrate/substrate-api';7import getBalance from '../substrate/get-balance';89describe('evm collection sponsoring', () => {10 itWeb3('sponsors mint transactions', async ({web3, privateKeyWrapper}) => {11 const alice = privateKeyWrapper('//Alice');1213 const collection = await createCollectionExpectSuccess();14 await setCollectionSponsorExpectSuccess(collection, alice.address);15 await confirmSponsorshipExpectSuccess(collection);1617 const minter = createEthAccount(web3);18 expect(await web3.eth.getBalance(minter)).to.equal('0');1920 const address = collectionIdToAddress(collection);21 const contract = new web3.eth.Contract(nonFungibleAbi as any, collectionIdToAddress(collection), {from: minter, ...GAS_ARGS});2223 await enablePublicMintingExpectSuccess(alice, collection);24 await addToAllowListExpectSuccess(alice, collection, {Ethereum: minter});2526 const nextTokenId = await contract.methods.nextTokenId().call();27 expect(nextTokenId).to.equal('1');28 const result = await contract.methods.mint(minter, nextTokenId).send();29 const events = normalizeEvents(result.events);30 expect(events).to.be.deep.equal([31 {32 address,33 event: 'Transfer',34 args: {35 from: '0x0000000000000000000000000000000000000000',36 to: minter,37 tokenId: nextTokenId,38 },39 },40 ]);41 });4243 44 45 46 47 48 49 50 5152 53 54 55 56 57 58 59 60 61 62 6364 itWeb3('Remove sponsor', async ({api, web3, privateKeyWrapper}) => {65 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);66 const collectionHelpers = evmCollectionHelpers(web3, owner);67 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();68 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);69 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);70 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);7172 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;73 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});74 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;75 76 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});77 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;78 79 await collectionEvm.methods.removeCollectionSponsor().send({from: owner});80 81 const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});82 expect(sponsorTuple.field_0).to.be.eq('0x0000000000000000000000000000000000000000');83 });8485 itWeb3('Sponsoring collection from evm address via access list', async ({api, web3, privateKeyWrapper}) => {86 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);87 const collectionHelpers = evmCollectionHelpers(web3, owner);88 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();89 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);90 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);91 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);92 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});93 let collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;94 const ss58Format = (api.registry.getChainProperties())!.toJSON().ss58Format;95 expect(collectionSub.sponsorship.isUnconfirmed).to.be.true;96 expect(collectionSub.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));97 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');9899 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});100 collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;101 expect(collectionSub.sponsorship.isConfirmed).to.be.true;102 expect(collectionSub.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));103104 const user = createEthAccount(web3);105 const nextTokenId = await collectionEvm.methods.nextTokenId().call();106 expect(nextTokenId).to.be.equal('1');107108 const oldPermissions = (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();109 expect(oldPermissions.mintMode).to.be.false;110 expect(oldPermissions.access).to.be.equal('Normal');111112 await collectionEvm.methods.setCollectionAccess(1 ).send({from: owner});113 await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});114 await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});115116 const newPermissions = (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();117 expect(newPermissions.mintMode).to.be.true;118 expect(newPermissions.access).to.be.equal('AllowList');119120 const ownerBalanceBefore = await ethBalanceViaSub(api, owner);121 const sponsorBalanceBefore = await ethBalanceViaSub(api, sponsor);122123 {124 const nextTokenId = await collectionEvm.methods.nextTokenId().call();125 expect(nextTokenId).to.be.equal('1');126 const result = await collectionEvm.methods.mintWithTokenURI(127 user,128 nextTokenId,129 'Test URI',130 ).send({from: user});131 const events = normalizeEvents(result.events);132133 expect(events).to.be.deep.equal([134 {135 address: collectionIdAddress,136 event: 'Transfer',137 args: {138 from: '0x0000000000000000000000000000000000000000',139 to: user,140 tokenId: nextTokenId,141 },142 },143 ]);144145 const ownerBalanceAfter = await ethBalanceViaSub(api, owner);146 const sponsorBalanceAfter = await ethBalanceViaSub(api, sponsor);147148 expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');149 expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);150 expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;151 }152 });153154 155 156 157 158 159 160 161 162163 164 165 166 167 168 169 170 171172 173 174 175176 177 178179 180 181 182 183 184 185 186 187 188189 190 191 192 193 194 195 196 197 198 199 200201 202 203204 205 206 207 208 209210 itWeb3('Check that transaction via EVM spend money from sponsor address', async ({api, web3, privateKeyWrapper}) => {211 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);212 const collectionHelpers = evmCollectionHelpers(web3, owner);213 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();214 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);215 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);216 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);217 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send();218 let collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;219 const ss58Format = (api.registry.getChainProperties())!.toJSON().ss58Format;220 expect(collectionSub.sponsorship.isUnconfirmed).to.be.true;221 expect(collectionSub.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));222 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');223 const sponsorCollection = evmCollection(web3, sponsor, collectionIdAddress);224 await sponsorCollection.methods.confirmCollectionSponsorship().send();225 collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;226 expect(collectionSub.sponsorship.isConfirmed).to.be.true;227 expect(collectionSub.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor, Number(ss58Format)));228229 const user = createEthAccount(web3);230 await collectionEvm.methods.addCollectionAdmin(user).send();231 232 const ownerBalanceBefore = await ethBalanceViaSub(api, owner);233 const sponsorBalanceBefore = await ethBalanceViaSub(api, sponsor);234 235 236 const userCollectionEvm = evmCollection(web3, user, collectionIdAddress);237 const nextTokenId = await userCollectionEvm.methods.nextTokenId().call();238 expect(nextTokenId).to.be.equal('1');239 result = await userCollectionEvm.methods.mintWithTokenURI(240 user,241 nextTokenId,242 'Test URI',243 ).send();244245 const events = normalizeEvents(result.events);246 const address = collectionIdToAddress(collectionId);247248 expect(events).to.be.deep.equal([249 {250 address,251 event: 'Transfer',252 args: {253 from: '0x0000000000000000000000000000000000000000',254 to: user,255 tokenId: nextTokenId,256 },257 },258 ]);259 expect(await userCollectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');260 261 const ownerBalanceAfter = await ethBalanceViaSub(api, owner);262 expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);263 const sponsorBalanceAfter = await ethBalanceViaSub(api, sponsor);264 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;265 });266});