1import {IKeyringPair} from '@polkadot/types/types';2import {usingPlaygrounds} from './../util/playgrounds/index';3import {itEth, expect} from '../eth/util/playgrounds';45describe('evm collection sponsoring', () => {6 let donor: IKeyringPair;7 let alice: IKeyringPair;8 let nominal: bigint;910 before(async () => {11 await usingPlaygrounds(async (helper, privateKey) => {12 donor = privateKey('//Alice');13 nominal = helper.balance.getOneTokenNominal();14 });15 });1617 beforeEach(async () => {18 await usingPlaygrounds(async (helper) => {19 [alice] = await helper.arrange.createAccounts([1000n], donor);20 });21 });2223 itEth('sponsors mint transactions', async ({helper}) => {24 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'spnr', permissions: {mintMode: true}});25 await collection.setSponsor(alice, alice.address);26 await collection.confirmSponsorship(alice);2728 const minter = helper.eth.createAccount();29 expect(await helper.balance.getEthereum(minter)).to.equal(0n);3031 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);32 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', minter);3334 await collection.addToAllowList(alice, {Ethereum: minter});3536 const result = await contract.methods.mint(minter).send();3738 const events = helper.eth.normalizeEvents(result.events);39 expect(events).to.be.deep.equal([40 {41 address: collectionAddress,42 event: 'Transfer',43 args: {44 from: '0x0000000000000000000000000000000000000000',45 to: minter,46 tokenId: '1',47 },48 },49 ]);50 });5152 53 54 55 56 57 58 59 6061 62 63 6465 66 67 6869 70 71 7273 itEth('Remove sponsor', async ({helper}) => {74 const owner = await helper.eth.createAccountWithBalance(donor);75 const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);7677 let result = await collectionHelpers.methods.createNFTCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});78 const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);79 const sponsor = await helper.eth.createAccountWithBalance(donor);80 const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);8182 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;83 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});84 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;8586 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});87 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;8889 await collectionEvm.methods.removeCollectionSponsor().send({from: owner});9091 const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});92 expect(sponsorTuple.field_0).to.be.eq('0x0000000000000000000000000000000000000000');93 });9495 itEth('Sponsoring collection from evm address via access list', async ({helper}) => {96 const owner = await helper.eth.createAccountWithBalance(donor);9798 const {collectionId, collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Sponsor collection', '1', '1', '');99100 const collection = helper.nft.getCollectionObject(collectionId);101 const sponsor = await helper.eth.createAccountWithBalance(donor);102 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);103104 await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});105 let collectionData = (await collection.getData())!;106 expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));107 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');108109 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});110 collectionData = (await collection.getData())!;111 expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));112113 const user = helper.eth.createAccount();114 const nextTokenId = await collectionEvm.methods.nextTokenId().call();115 expect(nextTokenId).to.be.equal('1');116117 const oldPermissions = (await collection.getData())!.raw.permissions; 118 expect(oldPermissions.mintMode).to.be.false;119 expect(oldPermissions.access).to.be.equal('Normal');120121 await collectionEvm.methods.setCollectionAccess(1 ).send({from: owner});122 await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});123 await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});124125 const newPermissions = (await collection.getData())!.raw.permissions; 126 expect(newPermissions.mintMode).to.be.true;127 expect(newPermissions.access).to.be.equal('AllowList');128129 const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));130 const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));131132 {133 const result = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});134 const events = helper.eth.normalizeEvents(result.events);135136 expect(events).to.be.deep.equal([137 {138 address: collectionAddress,139 event: 'Transfer',140 args: {141 from: '0x0000000000000000000000000000000000000000',142 to: user,143 tokenId: '1',144 },145 },146 ]);147148 const ownerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(owner));149 const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));150151 expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');152 expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);153 expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;154 }155 });156157 158 159 160 161 162 163 164 165166 167168 169 170171 172 173 174175 176 177 178179 180 181182 183 184 185 186 187 188 189 190 191192 193 194 195 196 197 198 199 200 201 202 203204 205 206207 208 209 210 211 212213 itEth('Check that transaction via EVM spend money from sponsor address', async ({helper}) => {214 const owner = await helper.eth.createAccountWithBalance(donor);215216 const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner,'Sponsor collection', '1', '1', '');217 const collection = helper.nft.getCollectionObject(collectionId);218 const sponsor = await helper.eth.createAccountWithBalance(donor);219 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);220221 await collectionEvm.methods.setCollectionSponsor(sponsor).send();222 let collectionData = (await collection.getData())!;223 expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));224 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');225226 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);227 await sponsorCollection.methods.confirmCollectionSponsorship().send();228 collectionData = (await collection.getData())!;229 expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));230231 const user = helper.eth.createAccount();232 await collectionEvm.methods.addCollectionAdmin(user).send();233234 const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));235 const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));236237 const userCollectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', user);238239 let result = await userCollectionEvm.methods.mintWithTokenURI(user, 'Test URI',).send();240 const tokenId = result.events.Transfer.returnValues.tokenId;241242 const events = helper.eth.normalizeEvents(result.events);243 const address = helper.ethAddress.fromCollectionId(collectionId);244245 expect(events).to.be.deep.equal([246 {247 address,248 event: 'Transfer',249 args: {250 from: '0x0000000000000000000000000000000000000000',251 to: user,252 tokenId: '1',253 },254 },255 ]);256 expect(await userCollectionEvm.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');257258 const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));259 expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);260 const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));261 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;262 });263});