git.delta.rocks / unique-network / refs/commits / 61b47c80db35

difftreelog

source

tests/src/eth/collectionSponsoring.test.ts13.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 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  itEth('Remove sponsor', async ({helper}) => {85    const owner = await helper.eth.createAccountWithBalance(donor);86    const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);8788    let result = await collectionHelpers.methods.createNFTCollection('Sponsor collection', '1', '1').send({value: Number(2n * nominal)});89    const collectionIdAddress = helper.ethAddress.normalizeAddress(result.events.CollectionCreated.returnValues.collectionId);90    const sponsor = await helper.eth.createAccountWithBalance(donor);91    const collectionEvm = helper.ethNativeContract.collection(collectionIdAddress, 'nft', owner);9293    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;94    result = await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});95    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.true;9697    await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});98    expect(await collectionEvm.methods.hasCollectionPendingSponsor().call({from: owner})).to.be.false;99100    await collectionEvm.methods.removeCollectionSponsor().send({from: owner});101102    const sponsorTuple = await collectionEvm.methods.collectionSponsor().call({from: owner});103    expect(sponsorTuple.field_0).to.be.eq('0x0000000000000000000000000000000000000000');104  });105106  itEth('Sponsoring collection from evm address via access list', async ({helper}) => {107    const owner = await helper.eth.createAccountWithBalance(donor);108109    const {collectionId, collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'Sponsor collection', '1', '1', '');110111    const collection = helper.nft.getCollectionObject(collectionId);112    const sponsor = await helper.eth.createAccountWithBalance(donor);113    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);114115    await collectionEvm.methods.setCollectionSponsor(sponsor).send({from: owner});116    let collectionData = (await collection.getData())!;117    expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));118    await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');119120    await collectionEvm.methods.confirmCollectionSponsorship().send({from: sponsor});121    collectionData = (await collection.getData())!;122    expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));123124    const user = helper.eth.createAccount();125    const nextTokenId = await collectionEvm.methods.nextTokenId().call();126    expect(nextTokenId).to.be.equal('1');127128    const oldPermissions = (await collection.getData())!.raw.permissions; // (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();129    expect(oldPermissions.mintMode).to.be.false;130    expect(oldPermissions.access).to.be.equal('Normal');131132    await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});133    await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});134    await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});135136    const newPermissions = (await collection.getData())!.raw.permissions; // (await getDetailedCollectionInfo(api, collectionId))!.permissions.toHuman();137    expect(newPermissions.mintMode).to.be.true;138    expect(newPermissions.access).to.be.equal('AllowList');139140    const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));141    const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));142143    {144      const result = await collectionEvm.methods.mintWithTokenURI(user, 'Test URI').send({from: user});145      const events = helper.eth.normalizeEvents(result.events);146147      expect(events).to.be.deep.equal([148        {149          address: collectionAddress,150          event: 'Transfer',151          args: {152            from: '0x0000000000000000000000000000000000000000',153            to: user,154            tokenId: '1',155          },156        },157      ]);158159      const ownerBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(owner));160      const sponsorBalanceAfter = await helper.balance.getSubstrate(await helper.address.ethToSubstrate(sponsor));161162      expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');163      expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);164      expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;165    }166  });167168  // TODO: Temprorary off. Need refactor169  // itWeb3('Sponsoring collection from substrate address via access list', async ({api, web3, privateKeyWrapper}) => {170  //   const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);171  //   const collectionHelpers = evmCollectionHelpers(web3, owner);172  //   const result = await collectionHelpers.methods.createERC721MetadataCompatibleNFTCollection('Sponsor collection', '1', '1', '').send();173  //   const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);174  //   const sponsor = privateKeyWrapper('//Alice');175  //   const collectionEvm = evmCollection(web3, owner, collectionIdAddress);176177  //   await collectionEvm.methods.setCollectionSponsorSubstrate(sponsor.addressRaw).send({from: owner});178179  //   const confirmTx = await api.tx.unique.confirmSponsorship(collectionId);180  //   await submitTransactionAsync(sponsor, confirmTx);181182  //   const user = createEthAccount(web3);183  //   const nextTokenId = await collectionEvm.methods.nextTokenId().call();184  //   expect(nextTokenId).to.be.equal('1');185186  //   await collectionEvm.methods.setCollectionAccess(1 /*'AllowList'*/).send({from: owner});187  //   await collectionEvm.methods.addToCollectionAllowList(user).send({from: owner});188  //   await collectionEvm.methods.setCollectionMintMode(true).send({from: owner});189190  //   const ownerBalanceBefore = await ethBalanceViaSub(api, owner);191  //   const sponsorBalanceBefore = (await getBalance(api, [sponsor.address]))[0];192193  //   {194  //     const nextTokenId = await collectionEvm.methods.nextTokenId().call();195  //     expect(nextTokenId).to.be.equal('1');196  //     const result = await collectionEvm.methods.mintWithTokenURI(197  //       user,198  //       nextTokenId,199  //       'Test URI',200  //     ).send({from: user});201  //     const events = normalizeEvents(result.events);202203  //     expect(events).to.be.deep.equal([204  //       {205  //         address: collectionIdAddress,206  //         event: 'Transfer',207  //         args: {208  //           from: '0x0000000000000000000000000000000000000000',209  //           to: user,210  //           tokenId: nextTokenId,211  //         },212  //       },213  //     ]);214215  //     const ownerBalanceAfter = await ethBalanceViaSub(api, owner);216  //     const sponsorBalanceAfter = (await getBalance(api, [sponsor.address]))[0];217218  //     expect(await collectionEvm.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');219  //     expect(ownerBalanceBefore).to.be.eq(ownerBalanceAfter);220  //     expect(sponsorBalanceBefore > sponsorBalanceAfter).to.be.true;221  //   }222  // });223224  itEth('Check that transaction via EVM spend money from sponsor address', async ({helper}) => {225    const owner = await helper.eth.createAccountWithBalance(donor);226227    const {collectionAddress, collectionId} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner,'Sponsor collection', '1', '1', '');228    const collection = helper.nft.getCollectionObject(collectionId);229    const sponsor = await helper.eth.createAccountWithBalance(donor);230    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);231232    await collectionEvm.methods.setCollectionSponsor(sponsor).send();233    let collectionData = (await collection.getData())!;234    expect(collectionData.raw.sponsorship.Unconfirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));235    await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');236237    const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);238    await sponsorCollection.methods.confirmCollectionSponsorship().send();239    collectionData = (await collection.getData())!;240    expect(collectionData.raw.sponsorship.Confirmed).to.be.eq(helper.address.ethToSubstrate(sponsor, true));241242    const user = helper.eth.createAccount();243    await collectionEvm.methods.addCollectionAdmin(user).send();244245    const ownerBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));246    const sponsorBalanceBefore = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));247248    const userCollectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', user);249250    const result = await userCollectionEvm.methods.mintWithTokenURI(user, 'Test URI').send();251    const tokenId = result.events.Transfer.returnValues.tokenId;252253    const events = helper.eth.normalizeEvents(result.events);254    const address = helper.ethAddress.fromCollectionId(collectionId);255256    expect(events).to.be.deep.equal([257      {258        address,259        event: 'Transfer',260        args: {261          from: '0x0000000000000000000000000000000000000000',262          to: user,263          tokenId: '1',264        },265      },266    ]);267    expect(await userCollectionEvm.methods.tokenURI(tokenId).call()).to.be.equal('Test URI');268269    const ownerBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(owner));270    expect(ownerBalanceAfter).to.be.eq(ownerBalanceBefore);271    const sponsorBalanceAfter = await helper.balance.getSubstrate(helper.address.ethToSubstrate(sponsor));272    expect(sponsorBalanceAfter < sponsorBalanceBefore).to.be.true;273  });274});