git.delta.rocks / unique-network / refs/commits / 9fbaca1b2bad

difftreelog

source

tests/src/eth/collectionSponsoring.test.ts14.3 KiBsourcehistory
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 nextTokenId = await contract.methods.nextTokenId().call();48    expect(nextTokenId).to.equal('1');49    const result = await contract.methods.mint(minter, nextTokenId).send();50    const events = helper.eth.normalizeEvents(result.events);51    expect(events).to.be.deep.equal([52      {53        address: collectionAddress,54        event: 'Transfer',55        args: {56          from: '0x0000000000000000000000000000000000000000',57          to: minter,58          tokenId: nextTokenId,59        },60      },61    ]);62  });6364  // 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.createNonfungibleCollection('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;76    77  //   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;80    81  //   const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});82  //   expect(bigIntToSub(api, BigInt(sponsorTuple[1]))).to.be.eq(sponsor.address);83  // });8485  itEth('Remove sponsor', async ({helper}) => {86    const owner = await helper.eth.createAccountWithBalance(donor);87    const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);8889    let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});90    const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);91    const sponsor = await helper.eth.createAccountWithBalance(donor);92    const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);9394    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;95    result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});96    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;97    98    await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});99    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;100    101    await collectionEvm.methods.removeCollectionSponsor().send({from: owner});102    103    const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});104    expect(sponsorTuple.field_0).to.be.eq('0x0000000000000000000000000000000000000000');105  });106107  itEth('Sponsoring collection from evm address via access list', async ({helper}) => {108    const owner = await helper.eth.createAccountWithBalance(donor);109    const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);110111    let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});112    const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);113    const collectionId = helper.ethAddress.extractCollectionId(collectionIdAddress);114    const collection = helper.nft.getCollectionObject(collectionId);115    const sponsor = await helper.eth.createAccountWithBalance(donor);116    const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);117118    result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});119    let collectionData = (await collection.getData())!;120    expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));121    await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');122123    await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});124    collectionData = (await collection.getData())!;125    expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));126127    const user = helper.eth.createAccount();128    const nextTokenId = await collectionEvm.methods.nextTokenId().call();129    expect(nextTokenId).to.be.equal('1');130131    const oldPermissions = (await collection.getData())!.raw.permissions; // (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();132    expect(oldPermissions.mintMode).to.be.false;133    expect(oldPermissions.access).to.be.equal('Normal');134135    await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});136    await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});137    await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});138139    const newPermissions = (await collection.getData())!.raw.permissions; // (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();140    expect(newPermissions.mintMode).to.be.true;141    expect(newPermissions.access).to.be.equal('AllowList');142143    const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));144    const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));145146    {147      const nextTokenId = await collectionEvm.methods.nextTokenId().call();148      expect(nextTokenId).to.be.equal('1');149      const result = await collectionEvm.methods.mintWithTokenURI(150        user,151        nextTokenId,152        'Test URI',153      ).send({from: user});154      const events = helper.eth.normalizeEvents(result.events);155156      expect(events).to.be.deep.equal([157        {158          address: collectionIdAddress,159          event: 'Transfer',160          args: {161            from: '0x0000000000000000000000000000000000000000',162            to: user,163            tokenId: nextTokenId,164          },165        },166      ]);167168      const ownerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(owner));169      const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));170171      expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');172      expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);173      expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;174    }175  });176177  // TODO: Temprorary off. Need refactor178  // itWeb3('Sponsoring collection from substrate address via access list', async ({api, web3, privateKeyWrapper}) => {179  //   const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);180  //   const collectionHelpers = evmCollectionHelpers(web3, owner);181  //   const result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();182  //   const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);183  //   const sponsor = privateKeyWrapper('//Alice');184  //   const collectionEvm = evmCollection(web3, owner, collectionIdAddress);185186  //   await collectionEvm.methods.setCollectionSponsorSubstrate(sponsor.addressRaw).send({from: owner});187    188  //   const confirmTx = await api.tx.unique.confirmSponsorship(collectionId);189  //   await submitTransactionAsync(sponsor, confirmTx);190    191  //   const user = createEthAccount(web3);192  //   const nextTokenId = await collectionEvm.methods.nextTokenId().call();193  //   expect(nextTokenId).to.be.equal('1');194195  //   await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});196  //   await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});197  //   await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});198199  //   const ownerBalanceBefore = await ethBalanceViaSub(api, owner);200  //   const sponsorBalanceBefore = (await getBalance(api, [sponsor.address]))[0];201202  //   {203  //     const nextTokenId = await collectionEvm.methods.nextTokenId().call();204  //     expect(nextTokenId).to.be.equal('1');205  //     const result = await collectionEvm.methods.mintWithTokenURI(206  //       user,207  //       nextTokenId,208  //       'Test URI',209  //     ).send({from: user});210  //     const events = normalizeEvents(result.events);211212  //     expect(events).to.be.deep.equal([213  //       {214  //         address: collectionIdAddress,215  //         event: 'Transfer',216  //         args: {217  //           from: '0x0000000000000000000000000000000000000000',218  //           to: user,219  //           tokenId: nextTokenId,220  //         },221  //       },222  //     ]);223224  //     const ownerBalanceAfter = await ethBalanceViaSub(api, owner);225  //     const sponsorBalanceAfter = (await getBalance(api, [sponsor.address]))[0];226227  //     expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');228  //     expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);229  //     expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;230  //   }231  // });232233  itEth('Check that transaction via EVM spend money from sponsor address', async ({helper}) => {234    const owner = await helper.eth.createAccountWithBalance(donor);235    const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);236237    let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});238    const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);239    const collectionId = helper.ethAddress.extractCollectionId(collectionIdAddress);240    const collection = helper.nft.getCollectionObject(collectionId);241    const sponsor = await helper.eth.createAccountWithBalance(donor);242    const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);243244    result = await collectionEvm.methods.setCollectionSponsor(sponsor).send();245    let collectionData = (await collection.getData())!;246    expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));247    await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');248249    const sponsorCollection = helper.ethNativeContract.collection(collectionIdAddress, 'nft', sponsor);250    await sponsorCollection.methods.confirmCollectionSponsorship().send();251    collectionData = (await collection.getData())!;252    expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));253254    const user = helper.eth.createAccount();255    await collectionEvm.methods.addCollectionAdmin(user).send();256    257    const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));258    const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));259260    const userCollectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', user);261    const nextTokenId = await userCollectionEvm.methods.nextTokenId().call();262    expect(nextTokenId).to.be.equal('1');263    result = await userCollectionEvm.methods.mintWithTokenURI(264      user,265      nextTokenId,266      'Test URI',267    ).send();268269    const events = helper.eth.normalizeEvents(result.events);270    const address = helper.ethAddress.fromCollectionId(collectionId);271272    expect(events).to.be.deep.equal([273      {274        address,275        event: 'Transfer',276        args: {277          from: '0x0000000000000000000000000000000000000000',278          to: user,279          tokenId: nextTokenId,280        },281      },282    ]);283    expect(await userCollectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');284  285    const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));286    expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);287    const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));288    expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;289  });290});