git.delta.rocks / unique-network / refs/commits / 1b028edfd0fc

difftreelog

source

tests/src/eth/createNFTCollection.test.ts11.5 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.8//9// 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 {evmToAddress} from '@polkadot/util-crypto';18import {IKeyringPair} from '@polkadot/types/types';19import {expect, itEth, usingEthPlaygrounds} from './util';20import { CollectionLimits } from './util/playgrounds/types';212223describe('Create NFT collection from EVM', () => {24  let donor: IKeyringPair;2526  before(async function () {27    await usingEthPlaygrounds(async (_helper, privateKey) => {28      donor = await privateKey({filename: __filename});29    });30  });3132  itEth('Create collection with properties & get desctription', async ({helper}) => {33    const owner = await helper.eth.createAccountWithBalance(donor);3435    const name = 'CollectionEVM';36    const description = 'Some description';37    const prefix = 'token prefix';38    const baseUri = 'BaseURI';3940    const {collectionId, collectionAddress, events} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, name, description, prefix, baseUri);41    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');42    43    expect(events).to.be.deep.equal([44      {45        address: '0x6C4E9fE1AE37a41E93CEE429e8E1881aBdcbb54F',46        event: 'CollectionCreated',47        args: {48          owner: owner,49          collectionId: collectionAddress,50        },51      },52    ]);5354    const collection = helper.nft.getCollectionObject(collectionId);55    const data = (await collection.getData())!;56    57    expect(data.name).to.be.eq(name);58    expect(data.description).to.be.eq(description);59    expect(data.raw.tokenPrefix).to.be.eq(prefix);60    expect(data.raw.mode).to.be.eq('NFT');61    62    expect(await contract.methods.description().call()).to.deep.equal(description);63    64    const options = await collection.getOptions();65    expect(options.tokenPropertyPermissions).to.be.deep.equal([66      {67        key: 'URI',68        permission: {mutable: true, collectionAdmin: true, tokenOwner: false},69      },70      {71        key: 'URISuffix',72        permission: {mutable: true, collectionAdmin: true, tokenOwner: false},73      },74    ]);75  });7677  // Soft-deprecated78  itEth('[eth] Set sponsorship', async ({helper}) => {79    const owner = await helper.eth.createAccountWithBalance(donor);80    const sponsor = await helper.eth.createAccountWithBalance(donor);81    const ss58Format = helper.chain.getChainProperties().ss58Format;82    const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(owner, 'Sponsor', 'absolutely anything', 'ROC');8384    const collection = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);85    await collection.methods.setCollectionSponsor(sponsor).send();8687    let data = (await helper.nft.getData(collectionId))!;88    expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));8990    await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');9192    const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor, true);93    await sponsorCollection.methods.confirmCollectionSponsorship().send();9495    data = (await helper.nft.getData(collectionId))!;96    expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));97  });9899  itEth('[cross] Set sponsorship & get description', async ({helper}) => {100    const owner = await helper.eth.createAccountWithBalance(donor);101    const sponsor = await helper.eth.createAccountWithBalance(donor);102    const ss58Format = helper.chain.getChainProperties().ss58Format;103    const description = 'absolutely anything';104    const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(owner, 'Sponsor', description, 'ROC');105106    const collection = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);107    const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);108    await collection.methods.setCollectionSponsorCross(sponsorCross).send();109110    let data = (await helper.nft.getData(collectionId))!;111    expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));112113    await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');114115    const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);116    await sponsorCollection.methods.confirmCollectionSponsorship().send();117118    data = (await helper.nft.getData(collectionId))!;119    expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));120    121    expect(await sponsorCollection.methods.description().call()).to.deep.equal(description);122  });123124  itEth('Collection address exist', async ({helper}) => {125    const owner = await helper.eth.createAccountWithBalance(donor);126    const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';127    expect(await helper.ethNativeContract.collectionHelpers(collectionAddressForNonexistentCollection)128      .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())129      .to.be.false;130131    const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Exister', 'absolutely anything', 'EVC');132    expect(await helper.ethNativeContract.collectionHelpers(collectionAddress)133      .methods.isCollectionExist(collectionAddress).call())134      .to.be.true;135  });136});137138describe('(!negative tests!) Create NFT collection from EVM', () => {139  let donor: IKeyringPair;140  let nominal: bigint;141142  before(async function () {143    await usingEthPlaygrounds(async (helper, privateKey) => {144      donor = await privateKey({filename: __filename});145      nominal = helper.balance.getOneTokenNominal();146    });147  });148149  itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {150    const owner = await helper.eth.createAccountWithBalance(donor);151    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);152    {153      const MAX_NAME_LENGTH = 64;154      const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);155      const description = 'A';156      const tokenPrefix = 'A';157158      await expect(collectionHelper.methods159        .createNFTCollection(collectionName, description, tokenPrefix)160        .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);161162    }163    {164      const MAX_DESCRIPTION_LENGTH = 256;165      const collectionName = 'A';166      const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);167      const tokenPrefix = 'A';168      await expect(collectionHelper.methods169        .createNFTCollection(collectionName, description, tokenPrefix)170        .call({value: Number(2n * nominal)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);171    }172    {173      const MAX_TOKEN_PREFIX_LENGTH = 16;174      const collectionName = 'A';175      const description = 'A';176      const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);177      await expect(collectionHelper.methods178        .createNFTCollection(collectionName, description, tokenPrefix)179        .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);180    }181  });182183  itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {184    const owner = await helper.eth.createAccountWithBalance(donor);185    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);186    await expect(collectionHelper.methods187      .createNFTCollection('Peasantry', 'absolutely anything', 'CVE')188      .call({value: Number(1n * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');189  });190191  // Soft-deprecated192  itEth('(!negative test!) [eth] Check owner', async ({helper}) => {193    const owner = await helper.eth.createAccountWithBalance(donor);194    const malfeasant = helper.eth.createAccount();195    const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Transgressed', 'absolutely anything', 'COR');196    const malfeasantCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', malfeasant, true);197    const EXPECTED_ERROR = 'NoPermission';198    {199      const sponsor = await helper.eth.createAccountWithBalance(donor);200      await expect(malfeasantCollection.methods201        .setCollectionSponsor(sponsor)202        .call()).to.be.rejectedWith(EXPECTED_ERROR);203204      const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor, true);205      await expect(sponsorCollection.methods206        .confirmCollectionSponsorship()207        .call()).to.be.rejectedWith('ConfirmSponsorshipFail');208    }209    {210      await expect(malfeasantCollection.methods211        .setCollectionLimit(CollectionLimits.AccountTokenOwnership, true, 1000)212        .call()).to.be.rejectedWith(EXPECTED_ERROR);213    }214  });215216  itEth('(!negative test!) [cross] Check owner', async ({helper}) => {217    const owner = await helper.eth.createAccountWithBalance(donor);218    const malfeasant = helper.eth.createAccount();219    const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Transgressed', 'absolutely anything', 'COR');220    const malfeasantCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', malfeasant);221    const EXPECTED_ERROR = 'NoPermission';222    {223      const sponsor = await helper.eth.createAccountWithBalance(donor);224      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);225      await expect(malfeasantCollection.methods226        .setCollectionSponsorCross(sponsorCross)227        .call()).to.be.rejectedWith(EXPECTED_ERROR);228229      const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);230      await expect(sponsorCollection.methods231        .confirmCollectionSponsorship()232        .call()).to.be.rejectedWith('ConfirmSponsorshipFail');233    }234    {235      await expect(malfeasantCollection.methods236        .setCollectionLimit(CollectionLimits.AccountTokenOwnership, true, 1000)237        .call()).to.be.rejectedWith(EXPECTED_ERROR);238    }239  });240241  itEth('destroyCollection', async ({helper}) => {242    const owner = await helper.eth.createAccountWithBalance(donor);243    const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'Limits', 'absolutely anything', 'OLF');244    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);245246247    const result = await collectionHelper.methods248      .destroyCollection(collectionAddress)249      .send({from: owner});250251    const events = helper.eth.normalizeEvents(result.events);252    253    expect(events).to.be.deep.equal([254      {255        address: collectionHelper.options.address,256        event: 'CollectionDestroyed',257        args: {258          collectionId: collectionAddress,259        },260      },261    ]);262263    expect(await collectionHelper.methods264      .isCollectionExist(collectionAddress)265      .call()).to.be.false;266    expect(await helper.collection.getData(collectionId)).to.be.null;267  });268});