git.delta.rocks / unique-network / refs/commits / ddd521cfaa4d

difftreelog

source

tests/src/eth/createNFTCollection.test.ts11.9 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;135136    // check collectionOwner:137    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', owner, true);138    const collectionOwner = await collectionEvm.methods.collectionOwner().call();139    expect(helper.address.restoreCrossAccountFromBigInt(BigInt(collectionOwner.sub))).to.eq(helper.address.ethToSubstrate(owner));140  });141});142143describe('(!negative tests!) Create NFT collection from EVM', () => {144  let donor: IKeyringPair;145  let nominal: bigint;146147  before(async function () {148    await usingEthPlaygrounds(async (helper, privateKey) => {149      donor = await privateKey({filename: __filename});150      nominal = helper.balance.getOneTokenNominal();151    });152  });153154  itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {155    const owner = await helper.eth.createAccountWithBalance(donor);156    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);157    {158      const MAX_NAME_LENGTH = 64;159      const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);160      const description = 'A';161      const tokenPrefix = 'A';162163      await expect(collectionHelper.methods164        .createNFTCollection(collectionName, description, tokenPrefix)165        .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);166167    }168    {169      const MAX_DESCRIPTION_LENGTH = 256;170      const collectionName = 'A';171      const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);172      const tokenPrefix = 'A';173      await expect(collectionHelper.methods174        .createNFTCollection(collectionName, description, tokenPrefix)175        .call({value: Number(2n * nominal)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);176    }177    {178      const MAX_TOKEN_PREFIX_LENGTH = 16;179      const collectionName = 'A';180      const description = 'A';181      const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);182      await expect(collectionHelper.methods183        .createNFTCollection(collectionName, description, tokenPrefix)184        .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);185    }186  });187188  itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {189    const owner = await helper.eth.createAccountWithBalance(donor);190    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);191    await expect(collectionHelper.methods192      .createNFTCollection('Peasantry', 'absolutely anything', 'CVE')193      .call({value: Number(1n * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');194  });195196  // Soft-deprecated197  itEth('(!negative test!) [eth] Check owner', async ({helper}) => {198    const owner = await helper.eth.createAccountWithBalance(donor);199    const malfeasant = helper.eth.createAccount();200    const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Transgressed', 'absolutely anything', 'COR');201    const malfeasantCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', malfeasant, true);202    const EXPECTED_ERROR = 'NoPermission';203    {204      const sponsor = await helper.eth.createAccountWithBalance(donor);205      await expect(malfeasantCollection.methods206        .setCollectionSponsor(sponsor)207        .call()).to.be.rejectedWith(EXPECTED_ERROR);208209      const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor, true);210      await expect(sponsorCollection.methods211        .confirmCollectionSponsorship()212        .call()).to.be.rejectedWith('ConfirmSponsorshipFail');213    }214    {215      await expect(malfeasantCollection.methods216        .setCollectionLimit(CollectionLimits.AccountTokenOwnership, true, 1000)217        .call()).to.be.rejectedWith(EXPECTED_ERROR);218    }219  });220221  itEth('(!negative test!) [cross] Check owner', async ({helper}) => {222    const owner = await helper.eth.createAccountWithBalance(donor);223    const malfeasant = helper.eth.createAccount();224    const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Transgressed', 'absolutely anything', 'COR');225    const malfeasantCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', malfeasant);226    const EXPECTED_ERROR = 'NoPermission';227    {228      const sponsor = await helper.eth.createAccountWithBalance(donor);229      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);230      await expect(malfeasantCollection.methods231        .setCollectionSponsorCross(sponsorCross)232        .call()).to.be.rejectedWith(EXPECTED_ERROR);233234      const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);235      await expect(sponsorCollection.methods236        .confirmCollectionSponsorship()237        .call()).to.be.rejectedWith('ConfirmSponsorshipFail');238    }239    {240      await expect(malfeasantCollection.methods241        .setCollectionLimit(CollectionLimits.AccountTokenOwnership, true, 1000)242        .call()).to.be.rejectedWith(EXPECTED_ERROR);243    }244  });245246  itEth('destroyCollection', async ({helper}) => {247    const owner = await helper.eth.createAccountWithBalance(donor);248    const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'Limits', 'absolutely anything', 'OLF');249    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);250251252    const result = await collectionHelper.methods253      .destroyCollection(collectionAddress)254      .send({from: owner});255256    const events = helper.eth.normalizeEvents(result.events);257    258    expect(events).to.be.deep.equal([259      {260        address: collectionHelper.options.address,261        event: 'CollectionDestroyed',262        args: {263          collectionId: collectionAddress,264        },265      },266    ]);267268    expect(await collectionHelper.methods269      .isCollectionExist(collectionAddress)270      .call()).to.be.false;271    expect(await helper.collection.getData(collectionId)).to.be.null;272  });273});