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 });3334 35 itEth('sponsors mint transactions', async ({helper}) => {36 const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'spnr', permissions: {mintMode: true}});37 await collection.setSponsor(alice, alice.address);38 await collection.confirmSponsorship(alice);3940 const minter = helper.eth.createAccount();41 expect(await helper.balance.getEthereum(minter)).to.equal(0n);4243 const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);44 const contract = await helper.ethNativeContract.collection(collectionAddress, 'nft', minter);4546 await collection.addToAllowList(alice, {Ethereum: minter});4748 const result = await contract.methods.mint(minter).send();4950 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: '1',59 },60 },61 ]);62 });6364 65 66 67 68 69 70 71 7273 74 75 7677 78 79 8081 82 83 8485 [86 'setCollectionSponsorCross',87 'setCollectionSponsor', 88 ].map(testCase =>89 itEth(`[${testCase}] can remove collection sponsor`, async ({helper}) => {90 const owner = await helper.eth.createAccountWithBalance(donor);91 const collectionHelpers = await helper.ethNativeContract.collectionHelpers(owner);9293 let result = await collectionHelpers.methods.createNFTCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});94 const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);95 const sponsor = await helper.eth.createAccountWithBalance(donor);96 const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);97 const collectionEvm = await helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner, testCase === 'setCollectionSponsor');9899 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;100 result = await collectionEvm.methods[testCase](testCase === 'setCollectionSponsor' ? sponsor : sponsorCross).send({from: owner});101 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;102103 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});104 let sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});105 expect(helper.address.restoreCrossAccountFromBigInt(BigInt(sponsorTuple.sub))).to.be.eq(helper.address.ethToSubstrate(sponsor));106 expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;107108 await collectionEvm.methods.removeCollectionSponsor().send({from: owner});109110 sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});111 expect(sponsorTuple.eth).to.be.eq('0x0000000000000000000000000000000000000000');112 }));113114 [115 'setCollectionSponsorCross',116 'setCollectionSponsor', 117 ].map(testCase =>118 itEth(`[${testCase}] Can sponsor from evm address via access list`, async ({helper}) => {119 const owner = await helper.eth.createAccountWithBalance(donor);120 const sponsorEth = await helper.eth.createAccountWithBalance(donor);121 const sponsorCrossEth = helper.ethCrossAccount.fromAddress(sponsorEth);122123 const {collectionId, collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Sponsor collection', '1', '1', '');124125 const collectionSub = helper.nft.getCollectionObject(collectionId);126 const collectionEvm = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner, testCase === 'setCollectionSponsor');127128 129 await collectionEvm.methods[testCase](testCase === 'setCollectionSponsor' ? sponsorEth : sponsorCrossEth).send({from: owner});130 let sponsorship = (await collectionSub.getData())!.raw.sponsorship;131 expect(sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsorEth, true));132 133 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');134135 136 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsorEth});137 sponsorship = (await collectionSub.getData())!.raw.sponsorship;138 expect(sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsorEth, true));139140 141 const user = helper.eth.createAccount();142 const userCross = helper.ethCrossAccount.fromAddress(user);143 const nextTokenId = await collectionEvm.methods.nextTokenId().call();144 expect(nextTokenId).to.be.equal('1');145146 147 const oldPermissions = (await collectionSub.getData())!.raw.permissions; 148 expect(oldPermissions.mintMode).to.be.false;149 expect(oldPermissions.access).to.be.equal('Normal');150151 await collectionEvm.methods.setCollectionAccess(1 ).send({from: owner});152 await collectionEvm.methods.addToCollectionAllowListCross(userCross).send({from: owner});153 await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});154155 const newPermissions = (await collectionSub.getData())!.raw.permissions; 156 expect(newPermissions.mintMode).to.be.true;157 expect(newPermissions.access).to.be.equal('AllowList');158159 const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));160 const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsorEth));161162 163 {164 const result = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});165 const event = helper.eth.normalizeEvents(result.events)166 .find(event => event.event === 'Transfer');167168 expect(event).to.be.deep.equal({169 address: collectionAddress,170 event: 'Transfer',171 args: {172 from: '0x0000000000000000000000000000000000000000',173 to: user,174 tokenId: '1',175 },176 });177178 const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));179 const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsorEth));180 const userBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));181182 expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');183 expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);184 expect(userBalanceAfter).to.be.eq(0n);185 expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;186 }187 }));188189 190 191 192 193 194 195 196 197198 199200 201 202203 204 205 206207 208 209 210211 212 213214 215 216 217 218 219 220 221 222 223224 225 226 227 228 229 230 231 232 233 234 235236 237 238239 240 241 242 243 244245 [246 'setCollectionSponsorCross',247 'setCollectionSponsor', 248 ].map(testCase =>249 itEth(`[${testCase}] Check that transaction via EVM spend money from sponsor address`, async ({helper}) => {250 const owner = await helper.eth.createAccountWithBalance(donor);251 const sponsor = await helper.eth.createAccountWithBalance(donor);252 const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);253254 const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner,'Sponsor collection', '1', '1', '');255256 const collectionSub = helper.nft.getCollectionObject(collectionId);257 const collectionEvm = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner, testCase === 'setCollectionSponsor');258 259 await collectionEvm.methods[testCase](testCase === 'setCollectionSponsor' ? sponsor : sponsorCross).send();260 let collectionData = (await collectionSub.getData())!;261 expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));262 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');263264 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});265 collectionData = (await collectionSub.getData())!;266 expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));267268 const user = helper.eth.createAccount();269 const userCross = helper.ethCrossAccount.fromAddress(user);270 await collectionEvm.methods.addCollectionAdminCross(userCross).send();271272 const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));273 const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));274275 const mintingResult = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});276 const tokenId = mintingResult.events.Transfer.returnValues.tokenId;277278 const event = helper.eth.normalizeEvents(mintingResult.events)279 .find(event => event.event === 'Transfer');280 const address = helper.ethAddress.fromCollectionId(collectionId);281282 expect(event).to.be.deep.equal({283 address,284 event: 'Transfer',285 args: {286 from: '0x0000000000000000000000000000000000000000',287 to: user,288 tokenId: '1',289 },290 });291 expect(await collectionEvm.methods.tokenURI(tokenId).call({from: user})).to.be.equal('Test URI');292293 const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));294 expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);295 const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));296 expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;297 }));298299 itEth('Can reassign collection sponsor', async ({helper}) => {300 const owner = await helper.eth.createAccountWithBalance(donor);301 const sponsorEth = await helper.eth.createAccountWithBalance(donor);302 const sponsorCrossEth = helper.ethCrossAccount.fromAddress(sponsorEth);303 const [sponsorSub] = await helper.arrange.createAccounts([100n], donor);304 const sponsorCrossSub = helper.ethCrossAccount.fromKeyringPair(sponsorSub);305306 const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner,'Sponsor collection', '1', '1', '');307 const collectionSub = helper.nft.getCollectionObject(collectionId);308 const collectionEvm = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);309310 311 await collectionEvm.methods.setCollectionSponsorCross(sponsorCrossEth).send({from: owner});312 await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsorEth});313314 315 await collectionEvm.methods.setCollectionSponsorCross(sponsorCrossSub).send({from: owner});316 const collectionSponsor = (await collectionSub.getData())?.raw.sponsorship;317 expect(collectionSponsor).to.deep.eq({Unconfirmed: sponsorSub.address});318 });319});