git.delta.rocks / unique-network / refs/commits / 06a1c73f4a1c

difftreelog

Combine collection sponsoring tests

Max Andreev2022-12-07parent: #15c7e8f.patch.diff
in: master

1 file changed

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 {usingPlaygrounds} from '../util/index';19import {itEth, expect} from './util';2021describe('evm collection sponsoring', () => {22  let donor: IKeyringPair;23  let alice: IKeyringPair;24  let nominal: bigint;2526  before(async () => {27    await usingPlaygrounds(async (helper, privateKey) => {28      donor = await privateKey({filename: __filename});29      [alice] = await helper.arrange.createAccounts([100n], donor);30      nominal = helper.balance.getOneTokenNominal();31    });32  });33  34  itEth('sponsors mint transactions', async ({helper}) => {35    const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'spnr', permissions: {mintMode: true}});36    await collection.setSponsor(alice, alice.address);37    await collection.confirmSponsorship(alice);3839    const minter = helper.eth.createAccount();40    expect(await helper.balance.getEthereum(minter)).to.equal(0n);4142    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);43    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', minter);4445    await collection.addToAllowList(alice, {Ethereum: minter});4647    const result = await contract.methods.mint(minter).send();4849    const events = helper.eth.normalizeEvents(result.events);50    expect(events).to.be.deep.equal([51      {52        address: collectionAddress,53        event: 'Transfer',54        args: {55          from: '0x0000000000000000000000000000000000000000',56          to: minter,57          tokenId: '1',58        },59      },60    ]);61  });6263  // TODO: Temprorary off. Need refactor64  // itWeb3('Set substrate sponsor', async ({api, web3, privateKeyWrapper}) => {65  //   const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);66  //   const collectionHelpers = evmCollectionHelpers(web3, owner);67  //   let result = await collectionHelpers.methods.createNFTCollection('Sponsor collection', '1', '1').send();68  //   const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);69  //   const sponsor = privateKeyWrapper('//Alice');70  //   const collectionEvm = evmCollection(web3, owner, collectionIdAddress);7172  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;73  //   result = await collectionEvm.methods.setCollectionSponsorSubstrate(sponsor.addressRaw).send({from: owner});74  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;7576  //   const confirmTx = await api.tx.unique.confirmSponsorship(collectionId);77  //   await submitTransactionAsync(sponsor, confirmTx);78  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;7980  //   const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});81  //   expect(bigIntToSub(api, BigInt(sponsorTuple[1]))).to.be.eq(sponsor.address);82  // });8384  // Soft-deprecated85  itEth('[eth] Remove sponsor', async ({helper}) => {86    const owner = await helper.eth.createAccountWithBalance(donor);87    const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);8889    let result = await collectionHelpers.methods.createNFTCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});90    const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);91    const sponsor = await helper.eth.createAccountWithBalance(donor);92    const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner, true);9394    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;95    result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});96    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;9798    await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});99    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;100101    await collectionEvm.methods.removeCollectionSponsor().send({from: owner});102103    const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});104    expect(sponsorTuple.field_0).to.be.eq('0x0000000000000000000000000000000000000000');105  });106107  itEth('[cross] Remove sponsor', async ({helper}) => {108    const owner = await helper.eth.createAccountWithBalance(donor);109    const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);110111    let result = await collectionHelpers.methods.createNFTCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});112    const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);113    const sponsor = await helper.eth.createAccountWithBalance(donor);114    const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);115    const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);116117    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;118    result = await collectionEvm.methods.setCollectionSponsorCross(sponsorCross).send({from: owner});119    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;120121    await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});122    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;123124    await collectionEvm.methods.removeCollectionSponsor().send({from: owner});125126    const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});127    expect(sponsorTuple.field_0).to.be.eq('0x0000000000000000000000000000000000000000');128  });129130  // Soft-deprecated131  itEth('[eth] Sponsoring collection from evm address via access list', async ({helper}) => {132    const owner = await helper.eth.createAccountWithBalance(donor);133134    const {collectionId, collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Sponsor collection', '1', '1', '');135136    const collection = helper.nft.getCollectionObject(collectionId);137    const sponsor = await helper.eth.createAccountWithBalance(donor);138    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);139140    await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});141    let collectionData = (await collection.getData())!;142    expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));143    await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');144145    await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});146    collectionData = (await collection.getData())!;147    expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));148149    const user = helper.eth.createAccount();150    const nextTokenId = await collectionEvm.methods.nextTokenId().call();151    expect(nextTokenId).to.be.equal('1');152153    const oldPermissions = (await collection.getData())!.raw.permissions; // (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();154    expect(oldPermissions.mintMode).to.be.false;155    expect(oldPermissions.access).to.be.equal('Normal');156157    await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});158    await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});159    await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});160161    const newPermissions = (await collection.getData())!.raw.permissions; // (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();162    expect(newPermissions.mintMode).to.be.true;163    expect(newPermissions.access).to.be.equal('AllowList');164165    const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));166    const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));167168    {169      const result = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});170      const events = helper.eth.normalizeEvents(result.events);171172      expect(events).to.be.deep.equal([173        {174          address: collectionAddress,175          event: 'Transfer',176          args: {177            from: '0x0000000000000000000000000000000000000000',178            to: user,179            tokenId: '1',180          },181        },182      ]);183184      const ownerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(owner));185      const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));186187      expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');188      expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);189      expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;190    }191  });192193  itEth('[cross] Sponsoring collection from evm address via access list', async ({helper}) => {194    const owner = await helper.eth.createAccountWithBalance(donor);195196    const {collectionId, collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Sponsor collection', '1', '1', '');197198    const collection = helper.nft.getCollectionObject(collectionId);199    const sponsor = await helper.eth.createAccountWithBalance(donor);200    const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);201    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);202203    await collectionEvm.methods.setCollectionSponsorCross(sponsorCross).send({from: owner});204    let collectionData = (await collection.getData())!;205    expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));206    await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');207208    await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});209    collectionData = (await collection.getData())!;210    expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));211212    const user = helper.eth.createAccount();213    const userCross = helper.ethCrossAccount.fromAddress(user);214    const nextTokenId = await collectionEvm.methods.nextTokenId().call();215    expect(nextTokenId).to.be.equal('1');216217    const oldPermissions = (await collection.getData())!.raw.permissions; // (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();218    expect(oldPermissions.mintMode).to.be.false;219    expect(oldPermissions.access).to.be.equal('Normal');220221    await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});222    await collectionEvm.methods.addToCollectionAllowListCross(userCross).send({from: owner});223    await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});224225    const newPermissions = (await collection.getData())!.raw.permissions; // (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();226    expect(newPermissions.mintMode).to.be.true;227    expect(newPermissions.access).to.be.equal('AllowList');228229    const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));230    const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));231232    {233      const result = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});234      const events = helper.eth.normalizeEvents(result.events);235236      expect(events).to.be.deep.equal([237        {238          address: collectionAddress,239          event: 'Transfer',240          args: {241            from: '0x0000000000000000000000000000000000000000',242            to: user,243            tokenId: '1',244          },245        },246      ]);247248      const ownerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(owner));249      const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));250251      expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');252      expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);253      expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;254    }255  });256257  // TODO: Temprorary off. Need refactor258  // itWeb3('Sponsoring collection from substrate address via access list', async ({api, web3, privateKeyWrapper}) => {259  //   const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);260  //   const collectionHelpers = evmCollectionHelpers(web3, owner);261  //   const result = await collectionHelpers.methods.createERC721MetadataCompatibleNFTCollection('Sponsor collection', '1', '1', '').send();262  //   const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);263  //   const sponsor = privateKeyWrapper('//Alice');264  //   const collectionEvm = evmCollection(web3, owner, collectionIdAddress);265266  //   await collectionEvm.methods.setCollectionSponsorSubstrate(sponsor.addressRaw).send({from: owner});267268  //   const confirmTx = await api.tx.unique.confirmSponsorship(collectionId);269  //   await submitTransactionAsync(sponsor, confirmTx);270271  //   const user = createEthAccount(web3);272  //   const nextTokenId = await collectionEvm.methods.nextTokenId().call();273  //   expect(nextTokenId).to.be.equal('1');274275  //   await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});276  //   await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});277  //   await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});278279  //   const ownerBalanceBefore = await ethBalanceViaSub(api, owner);280  //   const sponsorBalanceBefore = (await getBalance(api, [sponsor.address]))[0];281282  //   {283  //     const nextTokenId = await collectionEvm.methods.nextTokenId().call();284  //     expect(nextTokenId).to.be.equal('1');285  //     const result = await collectionEvm.methods.mintWithTokenURI(286  //       user,287  //       nextTokenId,288  //       'Test URI',289  //     ).send({from: user});290  //     const events = normalizeEvents(result.events);291292  //     expect(events).to.be.deep.equal([293  //       {294  //         address: collectionIdAddress,295  //         event: 'Transfer',296  //         args: {297  //           from: '0x0000000000000000000000000000000000000000',298  //           to: user,299  //           tokenId: nextTokenId,300  //         },301  //       },302  //     ]);303304  //     const ownerBalanceAfter = await ethBalanceViaSub(api, owner);305  //     const sponsorBalanceAfter = (await getBalance(api, [sponsor.address]))[0];306307  //     expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');308  //     expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);309  //     expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;310  //   }311  // });312313  // Soft-deprecated314  itEth('[eth] Check that transaction via EVM spend money from sponsor address', async ({helper}) => {315    const owner = await helper.eth.createAccountWithBalance(donor);316317    const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner,'Sponsor collection', '1', '1', '');318    const collection = helper.nft.getCollectionObject(collectionId);319    const sponsor = await helper.eth.createAccountWithBalance(donor);320    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);321322    await collectionEvm.methods.setCollectionSponsor(sponsor).send();323    let collectionData = (await collection.getData())!;324    expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));325    await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');326327    const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor, true);328    await sponsorCollection.methods.confirmCollectionSponsorship().send();329    collectionData = (await collection.getData())!;330    expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));331332    const user = helper.eth.createAccount();333    await collectionEvm.methods.addCollectionAdmin(user).send();334335    const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));336    const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));337338    const userCollectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', user, true);339340    const result = await userCollectionEvm.methods.mintWithTokenURI(user, 'Test URI').send();341    const tokenId = result.events.Transfer.returnValues.tokenId;342343    const events = helper.eth.normalizeEvents(result.events);344    const address = helper.ethAddress.fromCollectionId(collectionId);345346    expect(events).to.be.deep.equal([347      {348        address,349        event: 'Transfer',350        args: {351          from: '0x0000000000000000000000000000000000000000',352          to: user,353          tokenId: '1',354        },355      },356    ]);357    expect(await userCollectionEvm.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');358359    const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));360    expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);361    const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));362    expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;363  });364365  itEth('[cross] Check that transaction via EVM spend money from sponsor address', async ({helper}) => {366    const owner = await helper.eth.createAccountWithBalance(donor);367368    const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner,'Sponsor collection', '1', '1', '');369    const collection = helper.nft.getCollectionObject(collectionId);370    const sponsor = await helper.eth.createAccountWithBalance(donor);371    const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);372    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);373374    await collectionEvm.methods.setCollectionSponsorCross(sponsorCross).send();375    let collectionData = (await collection.getData())!;376    expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));377    await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');378379    const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);380    await sponsorCollection.methods.confirmCollectionSponsorship().send();381    collectionData = (await collection.getData())!;382    expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));383384    const user = helper.eth.createAccount();385    const userCross = helper.ethCrossAccount.fromAddress(user);386    await collectionEvm.methods.addCollectionAdminCross(userCross).send();387388    const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));389    const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));390391    const userCollectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', user);392393    const result = await userCollectionEvm.methods.mintWithTokenURI(user, 'Test URI').send();394    const tokenId = result.events.Transfer.returnValues.tokenId;395396    const events = helper.eth.normalizeEvents(result.events);397    const address = helper.ethAddress.fromCollectionId(collectionId);398399    expect(events).to.be.deep.equal([400      {401        address,402        event: 'Transfer',403        args: {404          from: '0x0000000000000000000000000000000000000000',405          to: user,406          tokenId: '1',407        },408      },409    ]);410    expect(await userCollectionEvm.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');411412    const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));413    expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);414    const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));415    expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;416  });417});
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 {usingPlaygrounds} from '../util/index';19import {itEth, expect} from './util';2021describe('evm collection sponsoring', () => {22  let donor: IKeyringPair;23  let alice: IKeyringPair;24  let nominal: bigint;2526  before(async () => {27    await usingPlaygrounds(async (helper, privateKey) => {28      donor = await privateKey({filename: __filename});29      [alice] = await helper.arrange.createAccounts([100n], donor);30      nominal = helper.balance.getOneTokenNominal();31    });32  });33  34  itEth('sponsors mint transactions', async ({helper}) => {35    const collection = await helper.nft.mintCollection(alice, {tokenPrefix: 'spnr', permissions: {mintMode: true}});36    await collection.setSponsor(alice, alice.address);37    await collection.confirmSponsorship(alice);3839    const minter = helper.eth.createAccount();40    expect(await helper.balance.getEthereum(minter)).to.equal(0n);4142    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);43    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', minter);4445    await collection.addToAllowList(alice, {Ethereum: minter});4647    const result = await contract.methods.mint(minter).send();4849    const events = helper.eth.normalizeEvents(result.events);50    expect(events).to.be.deep.equal([51      {52        address: collectionAddress,53        event: 'Transfer',54        args: {55          from: '0x0000000000000000000000000000000000000000',56          to: minter,57          tokenId: '1',58        },59      },60    ]);61  });6263  // TODO: Temprorary off. Need refactor64  // itWeb3('Set substrate sponsor', async ({api, web3, privateKeyWrapper}) => {65  //   const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);66  //   const collectionHelpers = evmCollectionHelpers(web3, owner);67  //   let result = await collectionHelpers.methods.createNFTCollection('Sponsor collection', '1', '1').send();68  //   const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);69  //   const sponsor = privateKeyWrapper('//Alice');70  //   const collectionEvm = evmCollection(web3, owner, collectionIdAddress);7172  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;73  //   result = await collectionEvm.methods.setCollectionSponsorSubstrate(sponsor.addressRaw).send({from: owner});74  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;7576  //   const confirmTx = await api.tx.unique.confirmSponsorship(collectionId);77  //   await submitTransactionAsync(sponsor, confirmTx);78  //   expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;7980  //   const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});81  //   expect(bigIntToSub(api, BigInt(sponsorTuple[1]))).to.be.eq(sponsor.address);82  // });8384  [85    'setCollectionSponsorCross',86    'setCollectionSponsor', // Soft-deprecated87  ].map(testCase => 88    itEth(`[${testCase}] can remove collection sponsor`, async ({helper}) => {89      const owner = await helper.eth.createAccountWithBalance(donor);90      const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);91  92      let result = await collectionHelpers.methods.createNFTCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});93      const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);94      const sponsor = await helper.eth.createAccountWithBalance(donor);95      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);96      const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner, testCase === 'setCollectionSponsor');97  98      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;99      result = await collectionEvm.methods[testCase](testCase === 'setCollectionSponsor' ? sponsor : sponsorCross).send({from: owner});100      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;101  102      await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});103      expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;104  105      await collectionEvm.methods.removeCollectionSponsor().send({from: owner});106  107      const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});108      expect(sponsorTuple.field_0).to.be.eq('0x0000000000000000000000000000000000000000');109    }));110111  [112    'setCollectionSponsorCross',113    'setCollectionSponsor', // Soft-deprecated114  ].map(testCase => 115    itEth(`[${testCase}] Can sponsor from evm address via access list`, async ({helper}) => {116      const owner = await helper.eth.createAccountWithBalance(donor);117      const sponsorEth = await helper.eth.createAccountWithBalance(donor);118      const sponsorCrossEth = helper.ethCrossAccount.fromAddress(sponsorEth);119  120      const {collectionId, collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Sponsor collection', '1', '1', '');121  122      const collectionSub = helper.nft.getCollectionObject(collectionId);123      const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, testCase === 'setCollectionSponsor');124  125      // Set collection sponsor:126      await collectionEvm.methods[testCase](testCase === 'setCollectionSponsor' ? sponsorEth : sponsorCrossEth).send({from: owner});127      let collectionData = (await collectionSub.getData())!;128      expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsorEth, true));129      // Account cannot confirm sponsorship if it is not set as a sponsor130      await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');131      132      // Sponsor can confirm sponsorship:133      await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsorEth});134      collectionData = (await collectionSub.getData())!;135      expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsorEth, true));136  137      // Create user with no balance:138      const user = helper.eth.createAccount();139      const userCross = helper.ethCrossAccount.fromAddress(user);140      const nextTokenId = await collectionEvm.methods.nextTokenId().call();141      expect(nextTokenId).to.be.equal('1');142  143      // Set collection permissions:144      const oldPermissions = (await collectionSub.getData())!.raw.permissions; // (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();145      expect(oldPermissions.mintMode).to.be.false;146      expect(oldPermissions.access).to.be.equal('Normal');147148      await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});149      await collectionEvm.methods.addToCollectionAllowListCross(userCross).send({from: owner});150      await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});151  152      const newPermissions = (await collectionSub.getData())!.raw.permissions; // (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();153      expect(newPermissions.mintMode).to.be.true;154      expect(newPermissions.access).to.be.equal('AllowList');155  156      const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));157      const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsorEth));158159      // User can mint token without balance:160      {161        const result = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});162        const events = helper.eth.normalizeEvents(result.events);163  164        expect(events).to.be.deep.equal([165          {166            address: collectionAddress,167            event: 'Transfer',168            args: {169              from: '0x0000000000000000000000000000000000000000',170              to: user,171              tokenId: '1',172            },173          },174        ]);175  176        const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));177        const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsorEth));178        const userBalanceAfter =  await helper.balance.getSubstrate(helper.address.ethToSubstrate(user));179  180        expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');181        expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);182        expect(userBalanceAfter).to.be.eq(0n);183        expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;184      }185    }));186187  // TODO: Temprorary off. Need refactor188  // itWeb3('Sponsoring collection from substrate address via access list', async ({api, web3, privateKeyWrapper}) => {189  //   const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);190  //   const collectionHelpers = evmCollectionHelpers(web3, owner);191  //   const result = await collectionHelpers.methods.createERC721MetadataCompatibleNFTCollection('Sponsor collection', '1', '1', '').send();192  //   const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);193  //   const sponsor = privateKeyWrapper('//Alice');194  //   const collectionEvm = evmCollection(web3, owner, collectionIdAddress);195196  //   await collectionEvm.methods.setCollectionSponsorSubstrate(sponsor.addressRaw).send({from: owner});197198  //   const confirmTx = await api.tx.unique.confirmSponsorship(collectionId);199  //   await submitTransactionAsync(sponsor, confirmTx);200201  //   const user = createEthAccount(web3);202  //   const nextTokenId = await collectionEvm.methods.nextTokenId().call();203  //   expect(nextTokenId).to.be.equal('1');204205  //   await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});206  //   await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});207  //   await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});208209  //   const ownerBalanceBefore = await ethBalanceViaSub(api, owner);210  //   const sponsorBalanceBefore = (await getBalance(api, [sponsor.address]))[0];211212  //   {213  //     const nextTokenId = await collectionEvm.methods.nextTokenId().call();214  //     expect(nextTokenId).to.be.equal('1');215  //     const result = await collectionEvm.methods.mintWithTokenURI(216  //       user,217  //       nextTokenId,218  //       'Test URI',219  //     ).send({from: user});220  //     const events = normalizeEvents(result.events);221222  //     expect(events).to.be.deep.equal([223  //       {224  //         address: collectionIdAddress,225  //         event: 'Transfer',226  //         args: {227  //           from: '0x0000000000000000000000000000000000000000',228  //           to: user,229  //           tokenId: nextTokenId,230  //         },231  //       },232  //     ]);233234  //     const ownerBalanceAfter = await ethBalanceViaSub(api, owner);235  //     const sponsorBalanceAfter = (await getBalance(api, [sponsor.address]))[0];236237  //     expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');238  //     expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);239  //     expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;240  //   }241  // });242243  [244    'setCollectionSponsorCross',245    'setCollectionSponsor', // Soft-deprecated246  ].map(testCase => 247    itEth(`[${testCase}] Check that transaction via EVM spend money from sponsor address`, async ({helper}) => {248      const owner = await helper.eth.createAccountWithBalance(donor);249      const sponsor = await helper.eth.createAccountWithBalance(donor);250      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);251  252      const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner,'Sponsor collection', '1', '1', '');253254      const collectionSub = helper.nft.getCollectionObject(collectionId);255      const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, testCase === 'setCollectionSponsor');256      // Set collection sponsor:257      await collectionEvm.methods[testCase](testCase === 'setCollectionSponsor' ? sponsor : sponsorCross).send();258      let collectionData = (await collectionSub.getData())!;259      expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));260      await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');261  262      const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);263      await sponsorCollection.methods.confirmCollectionSponsorship().send();264      collectionData = (await collectionSub.getData())!;265      expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));266  267      const user = helper.eth.createAccount();268      const userCross = helper.ethCrossAccount.fromAddress(user);269      await collectionEvm.methods.addCollectionAdminCross(userCross).send();270  271      const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));272      const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));273  274      const userCollectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', user);275  276      const result = await userCollectionEvm.methods.mintWithTokenURI(user, 'Test URI').send();277      const tokenId = result.events.Transfer.returnValues.tokenId;278  279      const events = helper.eth.normalizeEvents(result.events);280      const address = helper.ethAddress.fromCollectionId(collectionId);281  282      expect(events).to.be.deep.equal([283        {284          address,285          event: 'Transfer',286          args: {287            from: '0x0000000000000000000000000000000000000000',288            to: user,289            tokenId: '1',290          },291        },292      ]);293      expect(await userCollectionEvm.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');294  295      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    }));300});