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 = await privateKey({filename: __filename});13 [alice] = await helper.arrange.createAccounts([100n], donor);14 nominal = helper.balance.getOneTokenNominal();15 });16 });17 18 itEth('sponsors mint transactions', async ({helper}) => {19 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'spnr', permissions: {mintMode: true}});20 await collection.setSponsor(alice, alice.address);21 await collection.confirmSponsorship(alice);2223 const minter = helper.eth.createAccount();24 expect(await helper.balance.getEthereum(minter)).to.equal(0n);2526 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);27 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', minter);2829 await collection.addToAllowList(alice, {Ethereum: minter});3031 const nextTokenId = await contract.methods.nextTokenId().call();32 expect(nextTokenId).to.equal('1');33 const result = await contract.methods.mint(minter, nextTokenId).send();34 const events = helper.eth.normalizeEvents(result.events);35 expect(events).to.be.deep.equal([36 {37 address: collectionAddress,38 event: 'Transfer',39 args: {40 from: '0x0000000000000000000000000000000000000000',41 to: minter,42 tokenId: nextTokenId,43 },44 },45 ]);46 });4748 49 50 51 52 53 54 55 5657 58 59 60 61 62 63 64 65 66 67 6869 itEth('Remove sponsor', async ({helper}) => {70 const owner = await helper.eth.createAccountWithBalance(donor);71 const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);7273 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});74 const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);75 const sponsor = await helper.eth.createAccountWithBalance(donor);76 const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);7778 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;79 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});80 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;81 82 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});83 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;84 85 await collectionEvm.methods.removeCollectionSponsor().send({from: owner});86 87 const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});88 expect(sponsorTuple.field_0).to.be.eq('0x0000000000000000000000000000000000000000');89 });9091 itEth('Sponsoring collection from evm address via access list', async ({helper}) => {92 const owner = await helper.eth.createAccountWithBalance(donor);93 const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);9495 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});96 const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);97 const collectionId = helper.ethAddress.extractCollectionId(collectionIdAddress);98 const collection = helper.nft.getCollectionObject(collectionId);99 const sponsor = await helper.eth.createAccountWithBalance(donor);100 const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);101102 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});103 let collectionData = (await collection.getData())!;104 expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));105 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');106107 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});108 collectionData = (await collection.getData())!;109 expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));110111 const user = helper.eth.createAccount();112 const nextTokenId = await collectionEvm.methods.nextTokenId().call();113 expect(nextTokenId).to.be.equal('1');114115 const oldPermissions = (await collection.getData())!.raw.permissions; 116 expect(oldPermissions.mintMode).to.be.false;117 expect(oldPermissions.access).to.be.equal('Normal');118119 await collectionEvm.methods.setCollectionAccess(1 ).send({from: owner});120 await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});121 await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});122123 const newPermissions = (await collection.getData())!.raw.permissions; 124 expect(newPermissions.mintMode).to.be.true;125 expect(newPermissions.access).to.be.equal('AllowList');126127 const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));128 const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));129130 {131 const nextTokenId = await collectionEvm.methods.nextTokenId().call();132 expect(nextTokenId).to.be.equal('1');133 const result = await collectionEvm.methods.mintWithTokenURI(134 user,135 nextTokenId,136 'Test URI',137 ).send({from: user});138 const events = helper.eth.normalizeEvents(result.events);139140 expect(events).to.be.deep.equal([141 {142 address: collectionIdAddress,143 event: 'Transfer',144 args: {145 from: '0x0000000000000000000000000000000000000000',146 to: user,147 tokenId: nextTokenId,148 },149 },150 ]);151152 const ownerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(owner));153 const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));154155 expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');156 expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);157 expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;158 }159 });160161 162 163 164 165 166 167 168 169170 171 172 173 174 175 176 177 178179 180 181 182183 184 185186 187 188 189 190 191 192 193 194 195196 197 198 199 200 201 202 203 204 205 206 207208 209 210211 212 213 214 215 216217 itEth('Check that transaction via EVM spend money from sponsor address', async ({helper}) => {218 const owner = await helper.eth.createAccountWithBalance(donor);219 const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);220221 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});222 const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);223 const collectionId = helper.ethAddress.extractCollectionId(collectionIdAddress);224 const collection = helper.nft.getCollectionObject(collectionId);225 const sponsor = await helper.eth.createAccountWithBalance(donor);226 const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);227228 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send();229 let collectionData = (await collection.getData())!;230 expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));231 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');232233 const sponsorCollection = helper.ethNativeContract.collection(collectionIdAddress, 'nft', sponsor);234 await sponsorCollection.methods.confirmCollectionSponsorship().send();235 collectionData = (await collection.getData())!;236 expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));237238 const user = helper.eth.createAccount();239 await collectionEvm.methods.addCollectionAdmin(user).send();240 241 const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));242 const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));243244 const userCollectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', user);245 const nextTokenId = await userCollectionEvm.methods.nextTokenId().call();246 expect(nextTokenId).to.be.equal('1');247 result = await userCollectionEvm.methods.mintWithTokenURI(248 user,249 nextTokenId,250 'Test URI',251 ).send();252253 const events = helper.eth.normalizeEvents(result.events);254 const address = helper.ethAddress.fromCollectionId(collectionId);255256 expect(events).to.be.deep.equal([257 {258 address,259 event: 'Transfer',260 args: {261 from: '0x0000000000000000000000000000000000000000',262 to: user,263 tokenId: nextTokenId,264 },265 },266 ]);267 expect(await userCollectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');268 269 const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));270 expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);271 const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));272 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;273 });274});