git.delta.rocks / unique-network / refs/commits / 5871c2c14464

difftreelog

chore fix code review requests

Grigoriy Simonov2022-12-22parent: #dd7822d.patch.diff
in: master

2 files changed

modifiedruntime/common/ethereum/sponsoring/refungible.rsdiffbeforeafterboth
--- a/runtime/common/ethereum/sponsoring/refungible.rs
+++ b/runtime/common/ethereum/sponsoring/refungible.rs
@@ -233,9 +233,9 @@
 			| TokenContractAddress { .. } => None,
 
 			// Not sponsored
-			BurnFrom { .. } | BurnFromCross { .. } => None,
+			BurnFrom { .. } | BurnFromCross { .. } | MintBulk { .. } | MintBulkWithTokenUri { .. } => None,
 
-			MintCross { .. } | MintBulk { .. } | MintBulkWithTokenUri { .. } => {
+			MintCross { .. } => {
 				withdraw_create_item::<T>(
 					&collection,
 					&who,
modifiedtests/src/eth/collectionSponsoring.test.tsdiffbeforeafterboth
before · tests/src/eth/collectionSponsoring.test.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {IKeyringPair} from '@polkadot/types/types';18import {Pallets, requirePalletsOrSkip, usingPlaygrounds} from '../util/index';19import { CrossAccountId } from '../util/playgrounds/unique';20import {itEth, expect} from './util';2122describe('evm nft collection sponsoring', () => {23  let donor: IKeyringPair;24  let alice: IKeyringPair;25  let nominal: bigint;2627  before(async () => {28    await usingPlaygrounds(async (helper, privateKey) => {29      donor = await privateKey({filename: __filename});30      [alice] = await helper.arrange.createAccounts([100n], donor);31      nominal = helper.balance.getOneTokenNominal();32    });33  });3435  // TODO: move to substrate tests36  itEth('sponsors mint transactions', async ({helper}) => {37    const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'spnr', permissions: {mintMode: true}});38    await collection.setSponsor(alice, alice.address);39    await collection.confirmSponsorship(alice);4041    const minter = helper.eth.createAccount();42    expect(await helper.balance.getEthereum(minter)).to.equal(0n);4344    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);45    const contract = await helper.ethNativeContract.collection(collectionAddress, 'nft', minter);4647    await collection.addToAllowList(alice, {Ethereum: minter});4849    const result = await contract.methods.mint(minter).send();5051    const events = helper.eth.normalizeEvents(result.events);52    expect(events).to.be.deep.equal([53      {54        address: collectionAddress,55        event: 'Transfer',56        args: {57          from: '0x0000000000000000000000000000000000000000',58          to: minter,59          tokenId: '1',60        },61      },62    ]);63  });6465  // TODO: Temprorary off. Need refactor66  // itWeb3('Set substrate sponsor', async ({api, web3, privateKeyWrapper}) => {67  //   const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);68  //   const collectionHelpers = evmCollectionHelpers(web3, owner);69  //   let result = await collectionHelpers.methods.createNFTCollection('Sponsor collection', '1', '1').send();70  //   const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);71  //   const sponsor = privateKeyWrapper('//Alice');72  //   const collectionEvm = evmCollection(web3, owner, collectionIdAddress);7374  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;75  //   result = await collectionEvm.methods.setCollectionSponsorSubstrate(sponsor.addressRaw).send({from: owner});76  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;7778  //   const confirmTx = await api.tx.unique.confirmSponsorship(collectionId);79  //   await submitTransactionAsync(sponsor, confirmTx);80  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;8182  //   const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});83  //   expect(bigIntToSub(api, BigInt(sponsorTuple[1]))).to.be.eq(sponsor.address);84  // });8586  [87    'setCollectionSponsorCross',88    'setCollectionSponsor', // Soft-deprecated89  ].map(testCase =>90    itEth(`[${testCase}] can remove collection sponsor`, async ({helper}) => {91      const owner = await helper.eth.createAccountWithBalance(donor);92      const collectionHelpers = await helper.ethNativeContract.collectionHelpers(owner);9394      let result = await collectionHelpers.methods.createNFTCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});95      const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);96      const sponsor = await helper.eth.createAccountWithBalance(donor);97      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);98      const collectionEvm = await helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner, testCase === 'setCollectionSponsor');99100      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;101      result = await collectionEvm.methods[testCase](testCase === 'setCollectionSponsor' ? sponsor : sponsorCross).send({from: owner});102      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;103104      await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});105      let sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});106      expect(helper.address.restoreCrossAccountFromBigInt(BigInt(sponsorTuple.sub))).to.be.eq(helper.address.ethToSubstrate(sponsor, true));107      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;108109      await collectionEvm.methods.removeCollectionSponsor().send({from: owner});110111      sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});112      expect(sponsorTuple.eth).to.be.eq('0x0000000000000000000000000000000000000000');113    }));114115  [116    'setCollectionSponsorCross',117    'setCollectionSponsor', // Soft-deprecated118  ].map(testCase =>119    itEth(`[${testCase}] Can sponsor from evm address via access list`, async ({helper}) => {120      const owner = await helper.eth.createAccountWithBalance(donor);121      const sponsorEth = await helper.eth.createAccountWithBalance(donor);122      const sponsorCrossEth = helper.ethCrossAccount.fromAddress(sponsorEth);123124      const {collectionId, collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Sponsor collection', '1', '1', '');125126      const collectionSub = helper.nft.getCollectionObject(collectionId);127      const collectionEvm = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner, testCase === 'setCollectionSponsor');128129      // Set collection sponsor:130      await collectionEvm.methods[testCase](testCase === 'setCollectionSponsor' ? sponsorEth : sponsorCrossEth).send({from: owner});131      let sponsorship = (await collectionSub.getData())!.raw.sponsorship;132      expect(sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsorEth, true));133      // Account cannot confirm sponsorship if it is not set as a sponsor134      await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');135136      // Sponsor can confirm sponsorship:137      await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsorEth});138      sponsorship = (await collectionSub.getData())!.raw.sponsorship;139      expect(sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsorEth, true));140141      // Create user with no balance:142      const user = helper.eth.createAccount();143      const userCross = helper.ethCrossAccount.fromAddress(user);144      const nextTokenId = await collectionEvm.methods.nextTokenId().call();145      expect(nextTokenId).to.be.equal('1');146147      // Set collection permissions:148      const oldPermissions = (await collectionSub.getData())!.raw.permissions;149      expect(oldPermissions.mintMode).to.be.false;150      expect(oldPermissions.access).to.be.equal('Normal');151152      await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});153      await collectionEvm.methods.addToCollectionAllowListCross(userCross).send({from: owner});154      await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});155  156      const newPermissions = (await collectionSub.getData())!.raw.permissions;157      expect(newPermissions.mintMode).to.be.true;158      expect(newPermissions.access).to.be.equal('AllowList');159160      const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));161      const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsorEth));162      const userBalanceBefore =  await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));163164      // User can mint token without balance:165      {166        const result = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});167        const event = helper.eth.normalizeEvents(result.events)168          .find(event => event.event === 'Transfer');169170        expect(event).to.be.deep.equal({171          address: collectionAddress,172          event: 'Transfer',173          args: {174            from: '0x0000000000000000000000000000000000000000',175            to: user,176            tokenId: '1',177          },178        });179180        const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));181        const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsorEth));182        const userBalanceAfter =  await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));183184        expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');185        expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);186        expect(userBalanceAfter).to.be.eq(userBalanceBefore);187        expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;188      }189    }));190191  // TODO: Temprorary off. Need refactor192  // itWeb3('Sponsoring collection from substrate address via access list', async ({api, web3, privateKeyWrapper}) => {193  //   const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);194  //   const collectionHelpers = evmCollectionHelpers(web3, owner);195  //   const result = await collectionHelpers.methods.createERC721MetadataCompatibleNFTCollection('Sponsor collection', '1', '1', '').send();196  //   const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);197  //   const sponsor = privateKeyWrapper('//Alice');198  //   const collectionEvm = evmCollection(web3, owner, collectionIdAddress);199200  //   await collectionEvm.methods.setCollectionSponsorSubstrate(sponsor.addressRaw).send({from: owner});201202  //   const confirmTx = await api.tx.unique.confirmSponsorship(collectionId);203  //   await submitTransactionAsync(sponsor, confirmTx);204205  //   const user = createEthAccount(web3);206  //   const nextTokenId = await collectionEvm.methods.nextTokenId().call();207  //   expect(nextTokenId).to.be.equal('1');208209  //   await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});210  //   await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});211  //   await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});212213  //   const ownerBalanceBefore = await ethBalanceViaSub(api, owner);214  //   const sponsorBalanceBefore = (await getBalance(api, [sponsor.address]))[0];215216  //   {217  //     const nextTokenId = await collectionEvm.methods.nextTokenId().call();218  //     expect(nextTokenId).to.be.equal('1');219  //     const result = await collectionEvm.methods.mintWithTokenURI(220  //       user,221  //       nextTokenId,222  //       'Test URI',223  //     ).send({from: user});224  //     const events = normalizeEvents(result.events);225226  //     expect(events).to.be.deep.equal([227  //       {228  //         address: collectionIdAddress,229  //         event: 'Transfer',230  //         args: {231  //           from: '0x0000000000000000000000000000000000000000',232  //           to: user,233  //           tokenId: nextTokenId,234  //         },235  //       },236  //     ]);237238  //     const ownerBalanceAfter = await ethBalanceViaSub(api, owner);239  //     const sponsorBalanceAfter = (await getBalance(api, [sponsor.address]))[0];240241  //     expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');242  //     expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);243  //     expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;244  //   }245  // });246247  [248    'setCollectionSponsorCross',249    'setCollectionSponsor', // Soft-deprecated250  ].map(testCase =>251    itEth(`[${testCase}] Check that transaction via EVM spend money from sponsor address`, async ({helper}) => {252      const owner = await helper.eth.createAccountWithBalance(donor);253      const sponsor = await helper.eth.createAccountWithBalance(donor);254      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);255256      const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner,'Sponsor collection', '1', '1', '');257258      const collectionSub = helper.nft.getCollectionObject(collectionId);259      const collectionEvm = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner, testCase === 'setCollectionSponsor');260      // Set collection sponsor:261      await collectionEvm.methods[testCase](testCase === 'setCollectionSponsor' ? sponsor : sponsorCross).send();262      let collectionData = (await collectionSub.getData())!;263      expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));264      await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');265266      await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});267      collectionData = (await collectionSub.getData())!;268      expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));269270      const user = helper.eth.createAccount();271      const userCross = helper.ethCrossAccount.fromAddress(user);272      await collectionEvm.methods.addCollectionAdminCross(userCross).send();273274      const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));275      const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));276277      const mintingResult = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});278      const tokenId = mintingResult.events.Transfer.returnValues.tokenId;279280      const event = helper.eth.normalizeEvents(mintingResult.events)281        .find(event => event.event === 'Transfer');282      const address = helper.ethAddress.fromCollectionId(collectionId);283284      expect(event).to.be.deep.equal({285        address,286        event: 'Transfer',287        args: {288          from: '0x0000000000000000000000000000000000000000',289          to: user,290          tokenId: '1',291        },292      });293      expect(await collectionEvm.methods.tokenURI(tokenId).call({from: user})).to.be.equal('Test URI');294295      const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));296      expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);297      const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));298      expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;299    }));300301  itEth('Can reassign collection sponsor', async ({helper}) => {302    const owner = await helper.eth.createAccountWithBalance(donor);303    const sponsorEth = await helper.eth.createAccountWithBalance(donor);304    const sponsorCrossEth = helper.ethCrossAccount.fromAddress(sponsorEth);305    const [sponsorSub] = await helper.arrange.createAccounts([100n], donor);306    const sponsorCrossSub = helper.ethCrossAccount.fromKeyringPair(sponsorSub);307308    const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner,'Sponsor collection', '1', '1', '');309    const collectionSub = helper.nft.getCollectionObject(collectionId);310    const collectionEvm = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);311312    // Set and confirm sponsor:313    await collectionEvm.methods.setCollectionSponsorCross(sponsorCrossEth).send({from: owner});314    await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsorEth});315316    // Can reassign sponsor:317    await collectionEvm.methods.setCollectionSponsorCross(sponsorCrossSub).send({from: owner});318    const collectionSponsor = (await collectionSub.getData())?.raw.sponsorship;319    expect(collectionSponsor).to.deep.eq({Unconfirmed: sponsorSub.address});320  });321});322323describe('evm RFT collection sponsoring', () => {324  let donor: IKeyringPair;325  let alice: IKeyringPair;326  let nominal: bigint;327328  before(async function() {329    await usingPlaygrounds(async (helper, privateKey) => {330      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);331      donor = await privateKey({filename: __filename});332      [alice] = await helper.arrange.createAccounts([100n], donor);333      nominal = helper.balance.getOneTokenNominal();334    });335  });336  337  [338    'mintCross',339    'mintWithTokenURI',340    'mintBulk',341    'mintBulkWithTokenUri',342  ].map(testCase => 343    itEth(`[${testCase}] sponsors mint transactions`, async ({helper}) => {344      const collection = await helper.rft.mintCollection(alice, {tokenPrefix: 'spnr', permissions: {mintMode: true}, tokenPropertyPermissions: [345        {key: 'URI', permission: {tokenOwner: true, mutable: true, collectionAdmin: true}},346      ]});347      348      const owner = await helper.eth.createAccountWithBalance(donor);349      await collection.setSponsor(alice, alice.address);350      await collection.confirmSponsorship(alice);351352      const minter = helper.eth.createAccount();353      const minterCross = helper.ethCrossAccount.fromAddress(minter);354      expect(await helper.balance.getEthereum(minter)).to.equal(0n);355356      const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);357      const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', minter, true);358359      await collection.addToAllowList(alice, {Ethereum: minter});360      await collection.addAdmin(alice, {Ethereum: owner});361      const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);362      await collectionHelpers.methods.makeCollectionERC721MetadataCompatible(collectionAddress, 'base/')363        .send();364365      let mintingResult;366      let tokenId;367      const nextTokenId = await contract.methods.nextTokenId().call();368      switch (testCase) {369        case 'mintCross':370          mintingResult = await contract.methods.mintCross(minterCross, []).send();371          break;372        case 'mintWithTokenURI':373          mintingResult = await contract.methods.mintWithTokenURI(minter, 'Test URI').send();374          tokenId = mintingResult.events.Transfer.returnValues.tokenId;375          expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');376          break;377        case 'mintBulk':378          mintingResult = await contract.methods.mintBulk(minter, [nextTokenId]).send();379          break;380        case 'mintBulkWithTokenUri':381          mintingResult = await contract.methods.mintBulkWithTokenURI(minter, [[nextTokenId, 'Test URI']]).send();382          tokenId = mintingResult.events.Transfer.returnValues.tokenId;383          expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');384          break;385      }386387      const events = helper.eth.normalizeEvents(mintingResult.events);388      expect(events).to.deep.include({389        address: collectionAddress,390        event: 'Transfer',391        args: {392          from: '0x0000000000000000000000000000000000000000',393          to: minter,394          tokenId: '1',395        },396      });397    }));398399  [400    'setCollectionSponsorCross',401    'setCollectionSponsor', // Soft-deprecated402  ].map(testCase => 403    itEth(`[${testCase}] can remove collection sponsor`, async ({helper}) => {404      const owner = await helper.eth.createAccountWithBalance(donor);405      const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);406  407      let result = await collectionHelpers.methods.createRFTCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});408      const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);409      const sponsor = await helper.eth.createAccountWithBalance(donor);410      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);411      const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'rft', owner, testCase === 'setCollectionSponsor');412  413      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;414      result = await collectionEvm.methods[testCase](testCase === 'setCollectionSponsor' ? sponsor : sponsorCross).send({from: owner});415      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;416  417      await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});418      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;419  420      await collectionEvm.methods.removeCollectionSponsor().send({from: owner});421  422      const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});423      expect(sponsorTuple.eth).to.be.eq('0x0000000000000000000000000000000000000000');424    }));425426  [427    'setCollectionSponsorCross',428    'setCollectionSponsor', // Soft-deprecated429  ].map(testCase => 430    itEth(`[${testCase}] Can sponsor from evm address via access list`, async ({helper}) => {431      const owner = await helper.eth.createAccountWithBalance(donor);432      const sponsorEth = await helper.eth.createAccountWithBalance(donor);433      const sponsorCrossEth = helper.ethCrossAccount.fromAddress(sponsorEth);434  435      const {collectionId, collectionAddress} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner, 'Sponsor collection', '1', '1', '');436  437      const collectionSub = helper.rft.getCollectionObject(collectionId);438      const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner, testCase === 'setCollectionSponsor');439  440      // Set collection sponsor:441      await collectionEvm.methods[testCase](testCase === 'setCollectionSponsor' ? sponsorEth : sponsorCrossEth).send({from: owner});442      let sponsorship = (await collectionSub.getData())!.raw.sponsorship;443      expect(sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsorEth, true));444      // Account cannot confirm sponsorship if it is not set as a sponsor445      await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');446      447      // Sponsor can confirm sponsorship:448      await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsorEth});449      sponsorship = (await collectionSub.getData())!.raw.sponsorship;450      expect(sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsorEth, true));451  452      // Create user with no balance:453      const user = helper.eth.createAccount();454      const userCross = helper.ethCrossAccount.fromAddress(user);455      const nextTokenId = await collectionEvm.methods.nextTokenId().call();456      expect(nextTokenId).to.be.equal('1');457  458      // Set collection permissions:459      const oldPermissions = (await collectionSub.getData())!.raw.permissions;460      expect(oldPermissions.mintMode).to.be.false;461      expect(oldPermissions.access).to.be.equal('Normal');462463      await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});464      await collectionEvm.methods.addToCollectionAllowListCross(userCross).send({from: owner});465      await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});466  467      const newPermissions = (await collectionSub.getData())!.raw.permissions;468      expect(newPermissions.mintMode).to.be.true;469      expect(newPermissions.access).to.be.equal('AllowList');470  471      const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));472      const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsorEth));473      const userBalanceBefore =  await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));474475      // User can mint token without balance:476      {477        const result = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});478        const events = helper.eth.normalizeEvents(result.events);479480        expect(events).to.deep.include({481          address: collectionAddress,482          event: 'Transfer',483          args: {484            from: '0x0000000000000000000000000000000000000000',485            to: user,486            tokenId: '1',487          },488        });489  490        const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));491        const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsorEth));492        const userBalanceAfter =  await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));493  494        expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');495        expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);496        expect(userBalanceAfter).to.be.eq(userBalanceBefore);497        expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;498      }499    }));500501  [502    'setCollectionSponsorCross',503    'setCollectionSponsor', // Soft-deprecated504  ].map(testCase => 505    itEth(`[${testCase}] Check that collection admin EVM transaction spend money from sponsor eth address`, async ({helper}) => {506      const owner = await helper.eth.createAccountWithBalance(donor);507      const sponsor = await helper.eth.createAccountWithBalance(donor);508      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);509  510      const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner,'Sponsor collection', '1', '1', '');511512      const collectionSub = helper.rft.getCollectionObject(collectionId);513      const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner, testCase === 'setCollectionSponsor');514      // Set collection sponsor:515      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call()).to.be.false;516      await collectionEvm.methods[testCase](testCase === 'setCollectionSponsor' ? sponsor : sponsorCross).send();517      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call()).to.be.true;518      let collectionData = (await collectionSub.getData())!;519      expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));520      await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');521      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call()).to.be.true;522  523      await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});524      collectionData = (await collectionSub.getData())!;525      expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));526      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call()).to.be.false;527      const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});528      const sponsorSubAddress = helper.address.normalizeSubstrateToChainFormat(helper.address.ethToSubstrate(sponsor));529      const actualSubAddress = helper.address.normalizeSubstrateToChainFormat(helper.address.restoreCrossAccountFromBigInt(BigInt(sponsorTuple.sub)));530      expect(actualSubAddress).to.be.equal(sponsorSubAddress);531532      const user = helper.eth.createAccount();533      const userCross = helper.ethCrossAccount.fromAddress(user);534      await collectionEvm.methods.addCollectionAdminCross(userCross).send();535  536      const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));537      const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));538    539      const mintingResult = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});540      const tokenId = mintingResult.events.Transfer.returnValues.tokenId;541  542      const events = helper.eth.normalizeEvents(mintingResult.events);543      const address = helper.ethAddress.fromCollectionId(collectionId);544  545      expect(events).to.deep.include({546        address,547        event: 'Transfer',548        args: {549          from: '0x0000000000000000000000000000000000000000',550          to: user,551          tokenId: '1',552        },553      });554      expect(await collectionEvm.methods.tokenURI(tokenId).call({from: user})).to.be.equal('Test URI');555  556      const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));557      expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);558      const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));559      expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;560    }));561562  itEth('Check that collection admin EVM transaction spend money from sponsor sub address', async ({helper}) => {563    const owner = await helper.eth.createAccountWithBalance(donor);564    const sponsor = alice;565    const sponsorCross = helper.ethCrossAccount.fromKeyringPair(sponsor);566567    const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner,'Sponsor collection', '1', '1', '');568569    const collectionSub = helper.rft.getCollectionObject(collectionId);570    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner, false);571    // Set collection sponsor:572    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call()).to.be.false;573    await collectionEvm.methods.setCollectionSponsorCross(sponsorCross).send();574    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call()).to.be.true;575    let collectionData = (await collectionSub.getData())!;576    expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(sponsor.address);577    await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');578579    await collectionSub.confirmSponsorship(sponsor);580    collectionData = (await collectionSub.getData())!;581    expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(sponsor.address);582    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call()).to.be.false;583    const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});584    expect(BigInt(sponsorTuple.sub)).to.be.equal(BigInt('0x' + Buffer.from(sponsor.addressRaw).toString('hex')));585586    const user = helper.eth.createAccount();587    const userCross = helper.ethCrossAccount.fromAddress(user);588    await collectionEvm.methods.addCollectionAdminCross(userCross).send();589590    const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));591    const sponsorBalanceBefore = await helper.balance.getSubstrate(sponsor.address);592  593    const mintingResult = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});594    const tokenId = mintingResult.events.Transfer.returnValues.tokenId;595596    const events = helper.eth.normalizeEvents(mintingResult.events);597598    expect(events).to.deep.include({599      address: collectionAddress,600      event: 'Transfer',601      args: {602        from: '0x0000000000000000000000000000000000000000',603        to: user,604        tokenId: '1',605      },606    });607    expect(await collectionEvm.methods.tokenURI(tokenId).call({from: user})).to.be.equal('Test URI');608609    const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));610    expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);611    const sponsorBalanceAfter = await helper.balance.getSubstrate(sponsor.address);612    expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;613  });614615  itEth('Sponsoring collection from substrate address via access list', async ({helper}) => {616    const owner = await helper.eth.createAccountWithBalance(donor);617    const user = helper.eth.createAccount();618    const userCross =  helper.ethCrossAccount.fromAddress(user);619    const sponsor = alice;620    const sponsorCross = helper.ethCrossAccount.fromKeyringPair(sponsor);621622    const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner,'Sponsor collection', '1', '1', '');623    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner, false);624625    await collectionEvm.methods.setCollectionSponsorCross(sponsorCross).send({from: owner});626627    const collectionSub = helper.rft.getCollectionObject(collectionId);628    await collectionSub.confirmSponsorship(sponsor);629630    const nextTokenId = await collectionEvm.methods.nextTokenId().call();631    expect(nextTokenId).to.be.equal('1');632633    await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});634    635    await collectionEvm.methods.addToCollectionAllowListCross(userCross).send({from: owner});636    await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});637638    const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));639    const sponsorBalanceBefore = await helper.balance.getSubstrate(sponsor.address);640    const userBalanceBefore =  await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));641642    {643      const nextTokenId = await collectionEvm.methods.nextTokenId().call();644      expect(nextTokenId).to.be.equal('1');645      const mintingResult = await collectionEvm.methods.mintWithTokenURI(646        user,647        'Test URI',648      ).send({from: user});649650      const events = helper.eth.normalizeEvents(mintingResult.events);651      652653      expect(events).to.deep.include({654        address: collectionAddress,655        event: 'Transfer',656        args: {657          from: '0x0000000000000000000000000000000000000000',658          to: user,659          tokenId: nextTokenId,660        },661      });662663      const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));664      const sponsorBalanceAfter = await helper.balance.getSubstrate(sponsor.address);665      const userBalanceAfter =  await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));666667      expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');668      expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);669      expect(userBalanceAfter).to.be.eq(userBalanceBefore);670      expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;671    }672  });673674  itEth('Can reassign collection sponsor', async ({helper}) => {675    const owner = await helper.eth.createAccountWithBalance(donor);676    const sponsorEth = await helper.eth.createAccountWithBalance(donor);677    const sponsorCrossEth = helper.ethCrossAccount.fromAddress(sponsorEth);678    const [sponsorSub] = await helper.arrange.createAccounts([100n], donor);679    const sponsorCrossSub = helper.ethCrossAccount.fromKeyringPair(sponsorSub);680681    const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner,'Sponsor collection', '1', '1', '');682    const collectionSub = helper.rft.getCollectionObject(collectionId);683    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);684685    // Set and confirm sponsor:686    await collectionEvm.methods.setCollectionSponsorCross(sponsorCrossEth).send({from: owner});687    await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsorEth});688689    // Can reassign sponsor:690    await collectionEvm.methods.setCollectionSponsorCross(sponsorCrossSub).send({from: owner});691    const collectionSponsor = (await collectionSub.getData())?.raw.sponsorship;692    expect(collectionSponsor).to.deep.eq({Unconfirmed: sponsorSub.address});693  });694695  [696    'transfer',697    'transferCross',698    'transferFrom',699    'transferFromCross',700  ].map(testCase => 701    itEth(`[${testCase}] Check that transfer via EVM spend money from sponsor address`, async ({helper}) => {702      const owner = await helper.eth.createAccountWithBalance(donor);703704      const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner,'Sponsor collection', '1', '1', '');705      const sponsor = await helper.eth.createAccountWithBalance(donor);706      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);707      const receiver = await helper.eth.createAccountWithBalance(donor);708      const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);709710      await collectionEvm.methods.setCollectionSponsorCross(sponsorCross).send();711      await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});712713      const user = await helper.eth.createAccountWithBalance(donor);714      const userCross = helper.ethCrossAccount.fromAddress(user);715      await collectionEvm.methods.addCollectionAdminCross(userCross).send();716717      const result = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});718      const tokenId = result.events.Transfer.returnValues.tokenId;719      720      const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));721      const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));722      const userBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));723724      switch (testCase) {725        case 'transfer':726          await collectionEvm.methods.transfer(receiver, tokenId).send({from: user});727          break;728        case 'transferCross':729          await collectionEvm.methods.transferCross(helper.ethCrossAccount.fromAddress(receiver), tokenId).send({from: user});730          break;731        case 'transferFrom':732          await collectionEvm.methods.transferFrom(user, receiver, tokenId).send({from: user});733          break;734        case 'transferFromCross':735          await collectionEvm.methods.transferFromCross(helper.ethCrossAccount.fromAddress(user), helper.ethCrossAccount.fromAddress(receiver), tokenId).send({from: user});736          break;737      }738739      const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));740      expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);741      const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));742      expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;743      const userBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));744      expect(userBalanceAfter).to.be.eq(userBalanceBefore);745    }));746});747748describe('evm RFT token sponsoring', () => {749  let donor: IKeyringPair;750751  before(async function() {752    await usingPlaygrounds(async (helper, privateKey) => {753      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);754      donor = await privateKey({filename: __filename});755    });756  });757758  [759    'transfer',760    'transferCross',761    'transferFrom',762    'transferFromCross',763  ].map(testCase => 764    itEth(`[${testCase}] Check that token piece transfer via EVM spend money from sponsor address`, async ({helper}) => {765      const owner = await helper.eth.createAccountWithBalance(donor);766767      const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner,'Sponsor collection', '1', '1', '');768      const sponsor = await helper.eth.createAccountWithBalance(donor);769      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);770      const receiver = await helper.eth.createAccountWithBalance(donor);771      const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);772773      await collectionEvm.methods.setCollectionSponsorCross(sponsorCross).send();774      await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});775776      const user = await helper.eth.createAccountWithBalance(donor);777      const userCross = helper.ethCrossAccount.fromAddress(user);778      await collectionEvm.methods.addCollectionAdminCross(userCross).send();779780      const result = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});781      const tokenId = result.events.Transfer.returnValues.tokenId;782783      const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, user);   784      await tokenContract.methods.repartition(2).send();785      786      const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));787      const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));788      const userBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));789790      switch (testCase) {791        case 'transfer':792          await tokenContract.methods.transfer(receiver, 1).send();793          break;794        case 'transferCross':795          await tokenContract.methods.transferCross(helper.ethCrossAccount.fromAddress(receiver), 1).send();796          break;797        case 'transferFrom':798          await tokenContract.methods.transferFrom(user, receiver, 1).send();799          break;800        case 'transferFromCross':801          await tokenContract.methods.transferFromCross(helper.ethCrossAccount.fromAddress(user), helper.ethCrossAccount.fromAddress(receiver), 1).send();802          break;803      }804805      const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));806      expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);807      const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));808      expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;809      const userBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));810      expect(userBalanceAfter).to.be.eq(userBalanceBefore);811    }));812813  [814    'approve',815    'approveCross',816  ].map(testCase => 817    itEth(`[${testCase}] Check that approve via EVM spend money from sponsor address`, async ({helper}) => {818      const owner = await helper.eth.createAccountWithBalance(donor);819820      const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner,'Sponsor collection', '1', '1', '');821      const sponsor = await helper.eth.createAccountWithBalance(donor);822      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);823      const receiver = await helper.eth.createAccountWithBalance(donor);824      const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);825826      await collectionEvm.methods.setCollectionSponsorCross(sponsorCross).send();827      await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});828829      const user = await helper.eth.createAccountWithBalance(donor);830      const userCross = helper.ethCrossAccount.fromAddress(user);831      await collectionEvm.methods.addCollectionAdminCross(userCross).send();832833      const result = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});834      const tokenId = result.events.Transfer.returnValues.tokenId;835836      const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, user);  837      await tokenContract.methods.repartition(2).send();838      839      const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));840      const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));841      const userBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));842843      switch (testCase) {844        case 'approve':845          await tokenContract.methods.approve(receiver, 1).send();846          break;847        case 'approveCross':848          await tokenContract.methods.approveCross(helper.ethCrossAccount.fromAddress(receiver), 1).send();849          break;850      }851852      const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));853      expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);854      const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));855      expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;856      const userBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));857      expect(userBalanceAfter).to.be.eq(userBalanceBefore);858    }));859});860
after · tests/src/eth/collectionSponsoring.test.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {IKeyringPair} from '@polkadot/types/types';18import {Pallets, requirePalletsOrSkip, usingPlaygrounds} from '../util/index';19import {itEth, expect} from './util';2021describe('evm nft 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  // TODO: move to substrate tests35  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  // TODO: Temprorary off. Need refactor65  // itWeb3('Set substrate sponsor', async ({api, web3, privateKeyWrapper}) => {66  //   const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);67  //   const collectionHelpers = evmCollectionHelpers(web3, owner);68  //   let result = await collectionHelpers.methods.createNFTCollection('Sponsor collection', '1', '1').send();69  //   const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);70  //   const sponsor = privateKeyWrapper('//Alice');71  //   const collectionEvm = evmCollection(web3, owner, collectionIdAddress);7273  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;74  //   result = await collectionEvm.methods.setCollectionSponsorSubstrate(sponsor.addressRaw).send({from: owner});75  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;7677  //   const confirmTx = await api.tx.unique.confirmSponsorship(collectionId);78  //   await submitTransactionAsync(sponsor, confirmTx);79  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;8081  //   const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});82  //   expect(bigIntToSub(api, BigInt(sponsorTuple[1]))).to.be.eq(sponsor.address);83  // });8485  [86    'setCollectionSponsorCross',87    'setCollectionSponsor', // Soft-deprecated88  ].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 sponsorStruct = await collectionEvm.methods.collectionSponsor().call({from: owner});105      expect(helper.address.restoreCrossAccountFromBigInt(BigInt(sponsorStruct.sub))).to.be.eq(helper.address.ethToSubstrate(sponsor, true));106      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;107108      await collectionEvm.methods.removeCollectionSponsor().send({from: owner});109110      sponsorStruct = await collectionEvm.methods.collectionSponsor().call({from: owner});111      expect(sponsorStruct.eth).to.be.eq('0x0000000000000000000000000000000000000000');112    }));113114  [115    'setCollectionSponsorCross',116    'setCollectionSponsor', // Soft-deprecated117  ].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      // Set collection sponsor: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      // Account cannot confirm sponsorship if it is not set as a sponsor133      await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');134135      // Sponsor can confirm sponsorship: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      // Create user with no balance: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      // Set collection permissions: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 /*'AllowList'*/).send({from: owner});152      await collectionEvm.methods.addToCollectionAllowListCross(userCross).send({from: owner});153      await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});154  155      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));161      const userBalanceBefore =  await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));162163      // User can mint token without balance:164      {165        const result = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});166        const event = helper.eth.normalizeEvents(result.events)167          .find(event => event.event === 'Transfer');168169        expect(event).to.be.deep.equal({170          address: collectionAddress,171          event: 'Transfer',172          args: {173            from: '0x0000000000000000000000000000000000000000',174            to: user,175            tokenId: '1',176          },177        });178179        const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));180        const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsorEth));181        const userBalanceAfter =  await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));182183        expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');184        expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);185        expect(userBalanceAfter).to.be.eq(userBalanceBefore);186        expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;187      }188    }));189190  // TODO: Temprorary off. Need refactor191  // itWeb3('Sponsoring collection from substrate address via access list', async ({api, web3, privateKeyWrapper}) => {192  //   const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);193  //   const collectionHelpers = evmCollectionHelpers(web3, owner);194  //   const result = await collectionHelpers.methods.createERC721MetadataCompatibleNFTCollection('Sponsor collection', '1', '1', '').send();195  //   const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);196  //   const sponsor = privateKeyWrapper('//Alice');197  //   const collectionEvm = evmCollection(web3, owner, collectionIdAddress);198199  //   await collectionEvm.methods.setCollectionSponsorSubstrate(sponsor.addressRaw).send({from: owner});200201  //   const confirmTx = await api.tx.unique.confirmSponsorship(collectionId);202  //   await submitTransactionAsync(sponsor, confirmTx);203204  //   const user = createEthAccount(web3);205  //   const nextTokenId = await collectionEvm.methods.nextTokenId().call();206  //   expect(nextTokenId).to.be.equal('1');207208  //   await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});209  //   await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});210  //   await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});211212  //   const ownerBalanceBefore = await ethBalanceViaSub(api, owner);213  //   const sponsorBalanceBefore = (await getBalance(api, [sponsor.address]))[0];214215  //   {216  //     const nextTokenId = await collectionEvm.methods.nextTokenId().call();217  //     expect(nextTokenId).to.be.equal('1');218  //     const result = await collectionEvm.methods.mintWithTokenURI(219  //       user,220  //       nextTokenId,221  //       'Test URI',222  //     ).send({from: user});223  //     const events = normalizeEvents(result.events);224225  //     expect(events).to.be.deep.equal([226  //       {227  //         address: collectionIdAddress,228  //         event: 'Transfer',229  //         args: {230  //           from: '0x0000000000000000000000000000000000000000',231  //           to: user,232  //           tokenId: nextTokenId,233  //         },234  //       },235  //     ]);236237  //     const ownerBalanceAfter = await ethBalanceViaSub(api, owner);238  //     const sponsorBalanceAfter = (await getBalance(api, [sponsor.address]))[0];239240  //     expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');241  //     expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);242  //     expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;243  //   }244  // });245246  [247    'setCollectionSponsorCross',248    'setCollectionSponsor', // Soft-deprecated249  ].map(testCase =>250    itEth(`[${testCase}] Check that transaction via EVM spend money from sponsor address`, async ({helper}) => {251      const owner = await helper.eth.createAccountWithBalance(donor);252      const sponsor = await helper.eth.createAccountWithBalance(donor);253      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);254255      const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner,'Sponsor collection', '1', '1', '');256257      const collectionSub = helper.nft.getCollectionObject(collectionId);258      const collectionEvm = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner, testCase === 'setCollectionSponsor');259      // Set collection sponsor:260      await collectionEvm.methods[testCase](testCase === 'setCollectionSponsor' ? sponsor : sponsorCross).send();261      let collectionData = (await collectionSub.getData())!;262      expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));263      await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');264265      await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});266      collectionData = (await collectionSub.getData())!;267      expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));268269      const user = helper.eth.createAccount();270      const userCross = helper.ethCrossAccount.fromAddress(user);271      await collectionEvm.methods.addCollectionAdminCross(userCross).send();272273      const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));274      const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));275276      const mintingResult = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});277      const tokenId = mintingResult.events.Transfer.returnValues.tokenId;278279      const event = helper.eth.normalizeEvents(mintingResult.events)280        .find(event => event.event === 'Transfer');281      const address = helper.ethAddress.fromCollectionId(collectionId);282283      expect(event).to.be.deep.equal({284        address,285        event: 'Transfer',286        args: {287          from: '0x0000000000000000000000000000000000000000',288          to: user,289          tokenId: '1',290        },291      });292      expect(await collectionEvm.methods.tokenURI(tokenId).call({from: user})).to.be.equal('Test URI');293294      const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));295      expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);296      const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));297      expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;298    }));299300  itEth('Can reassign collection sponsor', async ({helper}) => {301    const owner = await helper.eth.createAccountWithBalance(donor);302    const sponsorEth = await helper.eth.createAccountWithBalance(donor);303    const sponsorCrossEth = helper.ethCrossAccount.fromAddress(sponsorEth);304    const [sponsorSub] = await helper.arrange.createAccounts([100n], donor);305    const sponsorCrossSub = helper.ethCrossAccount.fromKeyringPair(sponsorSub);306307    const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner,'Sponsor collection', '1', '1', '');308    const collectionSub = helper.nft.getCollectionObject(collectionId);309    const collectionEvm = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);310311    // Set and confirm sponsor:312    await collectionEvm.methods.setCollectionSponsorCross(sponsorCrossEth).send({from: owner});313    await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsorEth});314315    // Can reassign sponsor:316    await collectionEvm.methods.setCollectionSponsorCross(sponsorCrossSub).send({from: owner});317    const collectionSponsor = (await collectionSub.getData())?.raw.sponsorship;318    expect(collectionSponsor).to.deep.eq({Unconfirmed: sponsorSub.address});319  });320});321322describe('evm RFT collection sponsoring', () => {323  let donor: IKeyringPair;324  let alice: IKeyringPair;325  let nominal: bigint;326327  before(async function() {328    await usingPlaygrounds(async (helper, privateKey) => {329      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);330      donor = await privateKey({filename: __filename});331      [alice] = await helper.arrange.createAccounts([100n], donor);332      nominal = helper.balance.getOneTokenNominal();333    });334  });335  336  [337    'mintCross',338    'mintWithTokenURI',339  ].map(testCase => 340    itEth(`[${testCase}] sponsors mint transactions`, async ({helper}) => {341      const collection = await helper.rft.mintCollection(alice, {tokenPrefix: 'spnr', permissions: {mintMode: true}, tokenPropertyPermissions: [342        {key: 'URI', permission: {tokenOwner: true, mutable: true, collectionAdmin: true}},343      ]});344      345      const owner = await helper.eth.createAccountWithBalance(donor);346      await collection.setSponsor(alice, alice.address);347      await collection.confirmSponsorship(alice);348349      const minter = helper.eth.createAccount();350      const minterCross = helper.ethCrossAccount.fromAddress(minter);351      expect(await helper.balance.getEthereum(minter)).to.equal(0n);352353      const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);354      const contract = helper.ethNativeContract.collection(collectionAddress, 'rft', minter, true);355356      await collection.addToAllowList(alice, {Ethereum: minter});357      await collection.addAdmin(alice, {Ethereum: owner});358      const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);359      await collectionHelpers.methods.makeCollectionERC721MetadataCompatible(collectionAddress, 'base/')360        .send();361362      let mintingResult;363      let tokenId;364      switch (testCase) {365        case 'mintCross':366          mintingResult = await contract.methods.mintCross(minterCross, []).send();367          break;368        case 'mintWithTokenURI':369          mintingResult = await contract.methods.mintWithTokenURI(minter, 'Test URI').send();370          tokenId = mintingResult.events.Transfer.returnValues.tokenId;371          expect(await contract.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');372          break;373      }374375      const events = helper.eth.normalizeEvents(mintingResult.events);376      expect(events).to.deep.include({377        address: collectionAddress,378        event: 'Transfer',379        args: {380          from: '0x0000000000000000000000000000000000000000',381          to: minter,382          tokenId: '1',383        },384      });385    }));386387  [388    'setCollectionSponsorCross',389    'setCollectionSponsor', // Soft-deprecated390  ].map(testCase => 391    itEth(`[${testCase}] can remove collection sponsor`, async ({helper}) => {392      const owner = await helper.eth.createAccountWithBalance(donor);393      const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);394  395      let result = await collectionHelpers.methods.createRFTCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});396      const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);397      const sponsor = await helper.eth.createAccountWithBalance(donor);398      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);399      const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'rft', owner, testCase === 'setCollectionSponsor');400  401      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;402      result = await collectionEvm.methods[testCase](testCase === 'setCollectionSponsor' ? sponsor : sponsorCross).send({from: owner});403      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;404  405      await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});406      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;407  408      await collectionEvm.methods.removeCollectionSponsor().send({from: owner});409  410      const sponsorStruct = await collectionEvm.methods.collectionSponsor().call({from: owner});411      expect(sponsorStruct.eth).to.be.eq('0x0000000000000000000000000000000000000000');412    }));413414  [415    'setCollectionSponsorCross',416    'setCollectionSponsor', // Soft-deprecated417  ].map(testCase => 418    itEth(`[${testCase}] Can sponsor from evm address via access list`, async ({helper}) => {419      const owner = await helper.eth.createAccountWithBalance(donor);420      const sponsorEth = await helper.eth.createAccountWithBalance(donor);421      const sponsorCrossEth = helper.ethCrossAccount.fromAddress(sponsorEth);422  423      const {collectionId, collectionAddress} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner, 'Sponsor collection', '1', '1', '');424  425      const collectionSub = helper.rft.getCollectionObject(collectionId);426      const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner, testCase === 'setCollectionSponsor');427  428      // Set collection sponsor:429      await collectionEvm.methods[testCase](testCase === 'setCollectionSponsor' ? sponsorEth : sponsorCrossEth).send({from: owner});430      let sponsorship = (await collectionSub.getData())!.raw.sponsorship;431      expect(sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsorEth, true));432      // Account cannot confirm sponsorship if it is not set as a sponsor433      await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');434      435      // Sponsor can confirm sponsorship:436      await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsorEth});437      sponsorship = (await collectionSub.getData())!.raw.sponsorship;438      expect(sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsorEth, true));439  440      // Create user with no balance:441      const user = helper.eth.createAccount();442      const userCross = helper.ethCrossAccount.fromAddress(user);443      const nextTokenId = await collectionEvm.methods.nextTokenId().call();444      expect(nextTokenId).to.be.equal('1');445  446      // Set collection permissions:447      const oldPermissions = (await collectionSub.getData())!.raw.permissions;448      expect(oldPermissions.mintMode).to.be.false;449      expect(oldPermissions.access).to.be.equal('Normal');450451      await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});452      await collectionEvm.methods.addToCollectionAllowListCross(userCross).send({from: owner});453      await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});454  455      const newPermissions = (await collectionSub.getData())!.raw.permissions;456      expect(newPermissions.mintMode).to.be.true;457      expect(newPermissions.access).to.be.equal('AllowList');458  459      const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));460      const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsorEth));461      const userBalanceBefore =  await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));462463      // User can mint token without balance:464      {465        const result = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});466        const events = helper.eth.normalizeEvents(result.events);467468        expect(events).to.deep.include({469          address: collectionAddress,470          event: 'Transfer',471          args: {472            from: '0x0000000000000000000000000000000000000000',473            to: user,474            tokenId: '1',475          },476        });477  478        const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));479        const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsorEth));480        const userBalanceAfter =  await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));481  482        expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');483        expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);484        expect(userBalanceAfter).to.be.eq(userBalanceBefore);485        expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;486      }487    }));488489  [490    'setCollectionSponsorCross',491    'setCollectionSponsor', // Soft-deprecated492  ].map(testCase => 493    itEth(`[${testCase}] Check that collection admin EVM transaction spend money from sponsor eth address`, async ({helper}) => {494      const owner = await helper.eth.createAccountWithBalance(donor);495      const sponsor = await helper.eth.createAccountWithBalance(donor);496      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);497  498      const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner,'Sponsor collection', '1', '1', '');499500      const collectionSub = helper.rft.getCollectionObject(collectionId);501      const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner, testCase === 'setCollectionSponsor');502      // Set collection sponsor:503      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call()).to.be.false;504      await collectionEvm.methods[testCase](testCase === 'setCollectionSponsor' ? sponsor : sponsorCross).send();505      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call()).to.be.true;506      let collectionData = (await collectionSub.getData())!;507      expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));508      await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');509      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call()).to.be.true;510  511      await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});512      collectionData = (await collectionSub.getData())!;513      expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));514      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call()).to.be.false;515      const sponsorStruct = await collectionEvm.methods.collectionSponsor().call({from: owner});516      const sponsorSubAddress = helper.address.normalizeSubstrateToChainFormat(helper.address.ethToSubstrate(sponsor));517      const actualSubAddress = helper.address.normalizeSubstrateToChainFormat(helper.address.restoreCrossAccountFromBigInt(BigInt(sponsorStruct.sub)));518      expect(actualSubAddress).to.be.equal(sponsorSubAddress);519520      const user = helper.eth.createAccount();521      const userCross = helper.ethCrossAccount.fromAddress(user);522      await collectionEvm.methods.addCollectionAdminCross(userCross).send();523  524      const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));525      const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));526    527      const mintingResult = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});528      const tokenId = mintingResult.events.Transfer.returnValues.tokenId;529  530      const events = helper.eth.normalizeEvents(mintingResult.events);531      const address = helper.ethAddress.fromCollectionId(collectionId);532  533      expect(events).to.deep.include({534        address,535        event: 'Transfer',536        args: {537          from: '0x0000000000000000000000000000000000000000',538          to: user,539          tokenId: '1',540        },541      });542      expect(await collectionEvm.methods.tokenURI(tokenId).call({from: user})).to.be.equal('Test URI');543  544      const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));545      expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);546      const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));547      expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;548    }));549550  itEth('Check that collection admin EVM transaction spend money from sponsor sub address', async ({helper}) => {551    const owner = await helper.eth.createAccountWithBalance(donor);552    const sponsor = alice;553    const sponsorCross = helper.ethCrossAccount.fromKeyringPair(sponsor);554555    const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner,'Sponsor collection', '1', '1', '');556557    const collectionSub = helper.rft.getCollectionObject(collectionId);558    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner, false);559    // Set collection sponsor:560    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call()).to.be.false;561    await collectionEvm.methods.setCollectionSponsorCross(sponsorCross).send();562    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call()).to.be.true;563    let collectionData = (await collectionSub.getData())!;564    expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(sponsor.address);565    await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');566567    await collectionSub.confirmSponsorship(sponsor);568    collectionData = (await collectionSub.getData())!;569    expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(sponsor.address);570    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call()).to.be.false;571    const sponsorStruct = await collectionEvm.methods.collectionSponsor().call({from: owner});572    expect(BigInt(sponsorStruct.sub)).to.be.equal(BigInt('0x' + Buffer.from(sponsor.addressRaw).toString('hex')));573574    const user = helper.eth.createAccount();575    const userCross = helper.ethCrossAccount.fromAddress(user);576    await collectionEvm.methods.addCollectionAdminCross(userCross).send();577578    const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));579    const sponsorBalanceBefore = await helper.balance.getSubstrate(sponsor.address);580  581    const mintingResult = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});582    const tokenId = mintingResult.events.Transfer.returnValues.tokenId;583584    const events = helper.eth.normalizeEvents(mintingResult.events);585586    expect(events).to.deep.include({587      address: collectionAddress,588      event: 'Transfer',589      args: {590        from: '0x0000000000000000000000000000000000000000',591        to: user,592        tokenId: '1',593      },594    });595    expect(await collectionEvm.methods.tokenURI(tokenId).call({from: user})).to.be.equal('Test URI');596597    const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));598    expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);599    const sponsorBalanceAfter = await helper.balance.getSubstrate(sponsor.address);600    expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;601  });602603  itEth('Sponsoring collection from substrate address via access list', async ({helper}) => {604    const owner = await helper.eth.createAccountWithBalance(donor);605    const user = helper.eth.createAccount();606    const userCross =  helper.ethCrossAccount.fromAddress(user);607    const sponsor = alice;608    const sponsorCross = helper.ethCrossAccount.fromKeyringPair(sponsor);609610    const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner,'Sponsor collection', '1', '1', '');611    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner, false);612613    await collectionEvm.methods.setCollectionSponsorCross(sponsorCross).send({from: owner});614615    const collectionSub = helper.rft.getCollectionObject(collectionId);616    await collectionSub.confirmSponsorship(sponsor);617618    const nextTokenId = await collectionEvm.methods.nextTokenId().call();619    expect(nextTokenId).to.be.equal('1');620621    await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});622    623    await collectionEvm.methods.addToCollectionAllowListCross(userCross).send({from: owner});624    await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});625626    const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));627    const sponsorBalanceBefore = await helper.balance.getSubstrate(sponsor.address);628    const userBalanceBefore =  await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));629630    {631      const nextTokenId = await collectionEvm.methods.nextTokenId().call();632      expect(nextTokenId).to.be.equal('1');633      const mintingResult = await collectionEvm.methods.mintWithTokenURI(634        user,635        'Test URI',636      ).send({from: user});637638      const events = helper.eth.normalizeEvents(mintingResult.events);639      640641      expect(events).to.deep.include({642        address: collectionAddress,643        event: 'Transfer',644        args: {645          from: '0x0000000000000000000000000000000000000000',646          to: user,647          tokenId: nextTokenId,648        },649      });650651      const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));652      const sponsorBalanceAfter = await helper.balance.getSubstrate(sponsor.address);653      const userBalanceAfter =  await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));654655      expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');656      expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);657      expect(userBalanceAfter).to.be.eq(userBalanceBefore);658      expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;659    }660  });661662  itEth('Can reassign collection sponsor', async ({helper}) => {663    const owner = await helper.eth.createAccountWithBalance(donor);664    const sponsorEth = await helper.eth.createAccountWithBalance(donor);665    const sponsorCrossEth = helper.ethCrossAccount.fromAddress(sponsorEth);666    const [sponsorSub] = await helper.arrange.createAccounts([100n], donor);667    const sponsorCrossSub = helper.ethCrossAccount.fromKeyringPair(sponsorSub);668669    const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner,'Sponsor collection', '1', '1', '');670    const collectionSub = helper.rft.getCollectionObject(collectionId);671    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);672673    // Set and confirm sponsor:674    await collectionEvm.methods.setCollectionSponsorCross(sponsorCrossEth).send({from: owner});675    await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsorEth});676677    // Can reassign sponsor:678    await collectionEvm.methods.setCollectionSponsorCross(sponsorCrossSub).send({from: owner});679    const collectionSponsor = (await collectionSub.getData())?.raw.sponsorship;680    expect(collectionSponsor).to.deep.eq({Unconfirmed: sponsorSub.address});681  });682683  [684    'transfer',685    'transferCross',686    'transferFrom',687    'transferFromCross',688  ].map(testCase => 689    itEth(`[${testCase}] Check that transfer via EVM spend money from sponsor address`, async ({helper}) => {690      const owner = await helper.eth.createAccountWithBalance(donor);691692      const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner,'Sponsor collection', '1', '1', '');693      const sponsor = await helper.eth.createAccountWithBalance(donor);694      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);695      const receiver = await helper.eth.createAccountWithBalance(donor);696      const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);697698      await collectionEvm.methods.setCollectionSponsorCross(sponsorCross).send();699      await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});700701      const user = await helper.eth.createAccountWithBalance(donor);702      const userCross = helper.ethCrossAccount.fromAddress(user);703      await collectionEvm.methods.addCollectionAdminCross(userCross).send();704705      const result = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});706      const tokenId = result.events.Transfer.returnValues.tokenId;707      708      const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));709      const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));710      const userBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));711712      switch (testCase) {713        case 'transfer':714          await collectionEvm.methods.transfer(receiver, tokenId).send({from: user});715          break;716        case 'transferCross':717          await collectionEvm.methods.transferCross(helper.ethCrossAccount.fromAddress(receiver), tokenId).send({from: user});718          break;719        case 'transferFrom':720          await collectionEvm.methods.transferFrom(user, receiver, tokenId).send({from: user});721          break;722        case 'transferFromCross':723          await collectionEvm.methods.transferFromCross(helper.ethCrossAccount.fromAddress(user), helper.ethCrossAccount.fromAddress(receiver), tokenId).send({from: user});724          break;725      }726727      const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));728      expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);729      const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));730      expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;731      const userBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));732      expect(userBalanceAfter).to.be.eq(userBalanceBefore);733    }));734});735736describe('evm RFT token sponsoring', () => {737  let donor: IKeyringPair;738739  before(async function() {740    await usingPlaygrounds(async (helper, privateKey) => {741      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);742      donor = await privateKey({filename: __filename});743    });744  });745746  [747    'transfer',748    'transferCross',749    'transferFrom',750    'transferFromCross',751  ].map(testCase => 752    itEth(`[${testCase}] Check that token piece transfer via EVM spend money from sponsor address`, async ({helper}) => {753      const owner = await helper.eth.createAccountWithBalance(donor);754755      const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner,'Sponsor collection', '1', '1', '');756      const sponsor = await helper.eth.createAccountWithBalance(donor);757      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);758      const receiver = await helper.eth.createAccountWithBalance(donor);759      const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);760761      await collectionEvm.methods.setCollectionSponsorCross(sponsorCross).send();762      await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});763764      const user = await helper.eth.createAccountWithBalance(donor);765      const userCross = helper.ethCrossAccount.fromAddress(user);766      await collectionEvm.methods.addCollectionAdminCross(userCross).send();767768      const result = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});769      const tokenId = result.events.Transfer.returnValues.tokenId;770771      const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, user);   772      await tokenContract.methods.repartition(2).send();773      774      const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));775      const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));776      const userBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));777778      switch (testCase) {779        case 'transfer':780          await tokenContract.methods.transfer(receiver, 1).send();781          break;782        case 'transferCross':783          await tokenContract.methods.transferCross(helper.ethCrossAccount.fromAddress(receiver), 1).send();784          break;785        case 'transferFrom':786          await tokenContract.methods.transferFrom(user, receiver, 1).send();787          break;788        case 'transferFromCross':789          await tokenContract.methods.transferFromCross(helper.ethCrossAccount.fromAddress(user), helper.ethCrossAccount.fromAddress(receiver), 1).send();790          break;791      }792793      const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));794      expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);795      const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));796      expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;797      const userBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));798      expect(userBalanceAfter).to.be.eq(userBalanceBefore);799    }));800801  [802    'approve',803    'approveCross',804  ].map(testCase => 805    itEth(`[${testCase}] Check that approve via EVM spend money from sponsor address`, async ({helper}) => {806      const owner = await helper.eth.createAccountWithBalance(donor);807808      const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner,'Sponsor collection', '1', '1', '');809      const sponsor = await helper.eth.createAccountWithBalance(donor);810      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);811      const receiver = await helper.eth.createAccountWithBalance(donor);812      const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);813814      await collectionEvm.methods.setCollectionSponsorCross(sponsorCross).send();815      await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});816817      const user = await helper.eth.createAccountWithBalance(donor);818      const userCross = helper.ethCrossAccount.fromAddress(user);819      await collectionEvm.methods.addCollectionAdminCross(userCross).send();820821      const result = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});822      const tokenId = result.events.Transfer.returnValues.tokenId;823824      const tokenContract = helper.ethNativeContract.rftTokenById(collectionId, tokenId, user);  825      await tokenContract.methods.repartition(2).send();826      827      const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));828      const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));829      const userBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));830831      switch (testCase) {832        case 'approve':833          await tokenContract.methods.approve(receiver, 1).send();834          break;835        case 'approveCross':836          await tokenContract.methods.approveCross(helper.ethCrossAccount.fromAddress(receiver), 1).send();837          break;838      }839840      const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));841      expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);842      const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));843      expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;844      const userBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));845      expect(userBalanceAfter).to.be.eq(userBalanceBefore);846    }));847});848