1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {usingPlaygrounds} from '../util/index';19import {itEth, expect} from './util';2021describe('evm collection sponsoring', () => {22 let donor: IKeyringPair;23 let alice: IKeyringPair;24 let nominal: bigint;2526 before(async () => {27 await usingPlaygrounds(async (helper, privateKey) => {28 donor = await privateKey({filename: __filename});29 [alice] = await helper.arrange.createAccounts([100n], donor);30 nominal = helper.balance.getOneTokenNominal();31 });32 });33 34 itEth('sponsors mint transactions', async ({helper}) => {35 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'spnr', permissions: {mintMode: true}});36 await collection.setSponsor(alice, alice.address);37 await collection.confirmSponsorship(alice);3839 const minter = helper.eth.createAccount();40 expect(await helper.balance.getEthereum(minter)).to.equal(0n);4142 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);43 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', minter);4445 await collection.addToAllowList(alice, {Ethereum: minter});4647 const nextTokenId = await contract.methods.nextTokenId().call();48 expect(nextTokenId).to.equal('1');49 const result = await contract.methods.mint(minter, nextTokenId).send();50 const events = helper.eth.normalizeEvents(result.events);51 expect(events).to.be.deep.equal([52 {53 address: collectionAddress,54 event: 'Transfer',55 args: {56 from: '0x0000000000000000000000000000000000000000',57 to: minter,58 tokenId: nextTokenId,59 },60 },61 ]);62 });6364 65 66 67 68 69 70 71 7273 74 75 76 77 78 79 80 81 82 83 8485 itEth('Remove sponsor', async ({helper}) => {86 const owner = await helper.eth.createAccountWithBalance(donor);87 const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);8889 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});90 const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);91 const sponsor = await helper.eth.createAccountWithBalance(donor);92 const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);9394 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;95 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});96 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;97 98 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});99 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;100 101 await collectionEvm.methods.removeCollectionSponsor().send({from: owner});102 103 const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});104 expect(sponsorTuple.field_0).to.be.eq('0x0000000000000000000000000000000000000000');105 });106107 itEth('Sponsoring collection from evm address via access list', async ({helper}) => {108 const owner = await helper.eth.createAccountWithBalance(donor);109 const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);110111 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});112 const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);113 const collectionId = helper.ethAddress.extractCollectionId(collectionIdAddress);114 const collection = helper.nft.getCollectionObject(collectionId);115 const sponsor = await helper.eth.createAccountWithBalance(donor);116 const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);117118 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});119 let collectionData = (await collection.getData())!;120 expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));121 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');122123 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});124 collectionData = (await collection.getData())!;125 expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));126127 const user = helper.eth.createAccount();128 const nextTokenId = await collectionEvm.methods.nextTokenId().call();129 expect(nextTokenId).to.be.equal('1');130131 const oldPermissions = (await collection.getData())!.raw.permissions; 132 expect(oldPermissions.mintMode).to.be.false;133 expect(oldPermissions.access).to.be.equal('Normal');134135 await collectionEvm.methods.setCollectionAccess(1 ).send({from: owner});136 await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});137 await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});138139 const newPermissions = (await collection.getData())!.raw.permissions; 140 expect(newPermissions.mintMode).to.be.true;141 expect(newPermissions.access).to.be.equal('AllowList');142143 const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));144 const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));145146 {147 const nextTokenId = await collectionEvm.methods.nextTokenId().call();148 expect(nextTokenId).to.be.equal('1');149 const result = await collectionEvm.methods.mintWithTokenURI(150 user,151 nextTokenId,152 'Test URI',153 ).send({from: user});154 const events = helper.eth.normalizeEvents(result.events);155156 expect(events).to.be.deep.equal([157 {158 address: collectionIdAddress,159 event: 'Transfer',160 args: {161 from: '0x0000000000000000000000000000000000000000',162 to: user,163 tokenId: nextTokenId,164 },165 },166 ]);167168 const ownerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(owner));169 const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));170171 expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');172 expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);173 expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;174 }175 });176177 178 179 180 181 182 183 184 185186 187 188 189 190 191 192 193 194195 196 197 198199 200 201202 203 204 205 206 207 208 209 210 211212 213 214 215 216 217 218 219 220 221 222 223224 225 226227 228 229 230 231 232233 itEth('Check that transaction via EVM spend money from sponsor address', async ({helper}) => {234 const owner = await helper.eth.createAccountWithBalance(donor);235 const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);236237 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});238 const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);239 const collectionId = helper.ethAddress.extractCollectionId(collectionIdAddress);240 const collection = helper.nft.getCollectionObject(collectionId);241 const sponsor = await helper.eth.createAccountWithBalance(donor);242 const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);243244 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send();245 let collectionData = (await collection.getData())!;246 expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));247 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');248249 const sponsorCollection = helper.ethNativeContract.collection(collectionIdAddress, 'nft', sponsor);250 await sponsorCollection.methods.confirmCollectionSponsorship().send();251 collectionData = (await collection.getData())!;252 expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));253254 const user = helper.eth.createAccount();255 await collectionEvm.methods.addCollectionAdmin(user).send();256 257 const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));258 const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));259260 const userCollectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', user);261 const nextTokenId = await userCollectionEvm.methods.nextTokenId().call();262 expect(nextTokenId).to.be.equal('1');263 result = await userCollectionEvm.methods.mintWithTokenURI(264 user,265 nextTokenId,266 'Test URI',267 ).send();268269 const events = helper.eth.normalizeEvents(result.events);270 const address = helper.ethAddress.fromCollectionId(collectionId);271272 expect(events).to.be.deep.equal([273 {274 address,275 event: 'Transfer',276 args: {277 from: '0x0000000000000000000000000000000000000000',278 to: user,279 tokenId: nextTokenId,280 },281 },282 ]);283 expect(await userCollectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');284 285 const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));286 expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);287 const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));288 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;289 });290});