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 result = await contract.methods.mint(minter).send();4849 const events = helper.eth.normalizeEvents(result.events);50 expect(events).to.be.deep.equal([51 {52 address: collectionAddress,53 event: 'Transfer',54 args: {55 from: '0x0000000000000000000000000000000000000000',56 to: minter,57 tokenId: '1',58 },59 },60 ]);61 });6263 64 65 66 67 68 69 70 7172 73 74 7576 77 78 7980 81 82 8384 itEth('Remove sponsor', async ({helper}) => {85 const owner = await helper.eth.createAccountWithBalance(donor);86 const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);8788 let result = await collectionHelpers.methods.createNFTCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});89 const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);90 const sponsor = await helper.eth.createAccountWithBalance(donor);91 const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);9293 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;94 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});95 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;9697 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});98 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;99100 await collectionEvm.methods.removeCollectionSponsor().send({from: owner});101102 const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});103 expect(sponsorTuple.field_0).to.be.eq('0x0000000000000000000000000000000000000000');104 });105106 itEth('Sponsoring collection from evm address via access list', async ({helper}) => {107 const owner = await helper.eth.createAccountWithBalance(donor);108109 const {collectionId, collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Sponsor collection', '1', '1', '');110111 const collection = helper.nft.getCollectionObject(collectionId);112 const sponsor = await helper.eth.createAccountWithBalance(donor);113 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);114115 await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});116 let collectionData = (await collection.getData())!;117 expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));118 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');119120 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});121 collectionData = (await collection.getData())!;122 expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));123124 const user = helper.eth.createAccount();125 const nextTokenId = await collectionEvm.methods.nextTokenId().call();126 expect(nextTokenId).to.be.equal('1');127128 const oldPermissions = (await collection.getData())!.raw.permissions; 129 expect(oldPermissions.mintMode).to.be.false;130 expect(oldPermissions.access).to.be.equal('Normal');131132 await collectionEvm.methods.setCollectionAccess(1 ).send({from: owner});133 await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});134 await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});135136 const newPermissions = (await collection.getData())!.raw.permissions; 137 expect(newPermissions.mintMode).to.be.true;138 expect(newPermissions.access).to.be.equal('AllowList');139140 const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));141 const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));142143 {144 const result = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});145 const events = helper.eth.normalizeEvents(result.events);146147 expect(events).to.be.deep.equal([148 {149 address: collectionAddress,150 event: 'Transfer',151 args: {152 from: '0x0000000000000000000000000000000000000000',153 to: user,154 tokenId: '1',155 },156 },157 ]);158159 const ownerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(owner));160 const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));161162 expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');163 expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);164 expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;165 }166 });167168 169 170 171 172 173 174 175 176177 178179 180 181182 183 184 185186 187 188 189190 191 192193 194 195 196 197 198 199 200 201 202203 204 205 206 207 208 209 210 211 212 213 214215 216 217218 219 220 221 222 223224 itEth('Check that transaction via EVM spend money from sponsor address', async ({helper}) => {225 const owner = await helper.eth.createAccountWithBalance(donor);226227 const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner,'Sponsor collection', '1', '1', '');228 const collection = helper.nft.getCollectionObject(collectionId);229 const sponsor = await helper.eth.createAccountWithBalance(donor);230 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);231232 await collectionEvm.methods.setCollectionSponsor(sponsor).send();233 let collectionData = (await collection.getData())!;234 expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));235 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');236237 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);238 await sponsorCollection.methods.confirmCollectionSponsorship().send();239 collectionData = (await collection.getData())!;240 expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));241242 const user = helper.eth.createAccount();243 await collectionEvm.methods.addCollectionAdmin(user).send();244245 const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));246 const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));247248 const userCollectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', user);249250 const result = await userCollectionEvm.methods.mintWithTokenURI(user, 'Test URI').send();251 const tokenId = result.events.Transfer.returnValues.tokenId;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: '1',264 },265 },266 ]);267 expect(await userCollectionEvm.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');268269 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});