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

difftreelog

source

tests/src/eth/createNFTCollection.test.ts11.1 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/playgrounds';2021describe('Create NFT collection from EVM', () => {22  let donor: IKeyringPair;2324  before(async function() {25    await usingEthPlaygrounds(async (_helper, privateKey) => {26      donor = privateKey('//Alice');27    });28  });2930  itEth('Create collection', async ({helper}) => {31    const owner = await helper.eth.createAccountWithBalance(donor);3233    const name = 'CollectionEVM';34    const description = 'Some description';35    const prefix = 'token prefix';3637    // todo:playgrounds this might fail when in async environment.38    const collectionCountBefore = +(await helper.callRpc('api.rpc.unique.collectionStats')).created;39    const {collectionId} = await helper.eth.createNonfungibleCollection(owner, name, description, prefix);40    const collectionCountAfter = +(await helper.callRpc('api.rpc.unique.collectionStats')).created;4142    const collection = helper.nft.getCollectionObject(collectionId);43    const data = (await collection.getData())!;44    45    expect(collectionCountAfter - collectionCountBefore).to.be.eq(1);46    expect(collectionId).to.be.eq(collectionCountAfter);47    expect(data.name).to.be.eq(name);48    expect(data.description).to.be.eq(description);49    expect(data.raw.tokenPrefix).to.be.eq(prefix);50    expect(data.raw.mode).to.be.eq('NFT');51  });5253  // todo:playgrounds this test will fail when in async environment.54  itEth('Check collection address exist', async ({helper}) => {55    const owner = await helper.eth.createAccountWithBalance(donor);5657    const expectedCollectionId = +(await helper.callRpc('api.rpc.unique.collectionStats')).created + 1;58    const expectedCollectionAddress = helper.ethAddress.fromCollectionId(expectedCollectionId);59    const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);6061    expect(await collectionHelpers.methods62      .isCollectionExist(expectedCollectionAddress)63      .call()).to.be.false;6465    await collectionHelpers.methods66      .createNonfungibleCollection('A', 'A', 'A')67      .send();68    69    expect(await collectionHelpers.methods70      .isCollectionExist(expectedCollectionAddress)71      .call()).to.be.true;72  });73  74  itEth('Set sponsorship', async ({helper}) => {75    const owner = await helper.eth.createAccountWithBalance(donor);76    const sponsor = await helper.eth.createAccountWithBalance(donor);77    const ss58Format = helper.chain.getChainProperties().ss58Format;78    const {collectionId, collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'Sponsor', 'absolutely anything', 'ROC');7980    const collection = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);81    await collection.methods.setCollectionSponsor(sponsor).send();8283    let data = (await helper.nft.getData(collectionId))!;84    expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));8586    await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');8788    const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);89    await sponsorCollection.methods.confirmCollectionSponsorship().send();9091    data = (await helper.nft.getData(collectionId))!;92    expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));93  });9495  itEth('Set limits', async ({helper}) => {96    const owner = await helper.eth.createAccountWithBalance(donor);97    const {collectionId, collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'Limits', 'absolutely anything', 'FLO');98    const limits = {99      accountTokenOwnershipLimit: 1000,100      sponsoredDataSize: 1024,101      sponsoredDataRateLimit: 30,102      tokenLimit: 1000000,103      sponsorTransferTimeout: 6,104      sponsorApproveTimeout: 6,105      ownerCanTransfer: false,106      ownerCanDestroy: false,107      transfersEnabled: false,108    };109110    const collection = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);111    await collection.methods['setCollectionLimit(string,uint32)']('accountTokenOwnershipLimit', limits.accountTokenOwnershipLimit).send();112    await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataSize', limits.sponsoredDataSize).send();113    await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataRateLimit', limits.sponsoredDataRateLimit).send();114    await collection.methods['setCollectionLimit(string,uint32)']('tokenLimit', limits.tokenLimit).send();115    await collection.methods['setCollectionLimit(string,uint32)']('sponsorTransferTimeout', limits.sponsorTransferTimeout).send();116    await collection.methods['setCollectionLimit(string,uint32)']('sponsorApproveTimeout', limits.sponsorApproveTimeout).send();117    await collection.methods['setCollectionLimit(string,bool)']('ownerCanTransfer', limits.ownerCanTransfer).send();118    await collection.methods['setCollectionLimit(string,bool)']('ownerCanDestroy', limits.ownerCanDestroy).send();119    await collection.methods['setCollectionLimit(string,bool)']('transfersEnabled', limits.transfersEnabled).send();120    121    const data = (await helper.nft.getData(collectionId))!;122    expect(data.raw.limits.accountTokenOwnershipLimit).to.be.eq(limits.accountTokenOwnershipLimit);123    expect(data.raw.limits.sponsoredDataSize).to.be.eq(limits.sponsoredDataSize);124    expect(data.raw.limits.sponsoredDataRateLimit.blocks).to.be.eq(limits.sponsoredDataRateLimit);125    expect(data.raw.limits.tokenLimit).to.be.eq(limits.tokenLimit);126    expect(data.raw.limits.sponsorTransferTimeout).to.be.eq(limits.sponsorTransferTimeout);127    expect(data.raw.limits.sponsorApproveTimeout).to.be.eq(limits.sponsorApproveTimeout);128    expect(data.raw.limits.ownerCanTransfer).to.be.eq(limits.ownerCanTransfer);129    expect(data.raw.limits.ownerCanDestroy).to.be.eq(limits.ownerCanDestroy);130    expect(data.raw.limits.transfersEnabled).to.be.eq(limits.transfersEnabled);131  });132133  itEth('Collection address exist', async ({helper}) => {134    const owner = await helper.eth.createAccountWithBalance(donor);135    const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';136    expect(await helper.ethNativeContract.collectionHelpers(collectionAddressForNonexistentCollection)137      .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())138      .to.be.false;139    140    const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'Exister', 'absolutely anything', 'EVC');141    expect(await helper.ethNativeContract.collectionHelpers(collectionAddress)142      .methods.isCollectionExist(collectionAddress).call())143      .to.be.true;144  });145});146147describe('(!negative tests!) Create NFT collection from EVM', () => {148  let donor: IKeyringPair;149150  before(async function() {151    await usingEthPlaygrounds(async (_helper, privateKey) => {152      donor = privateKey('//Alice');153    });154  });155156  itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {157    const owner = await helper.eth.createAccountWithBalance(donor);158    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);159    {160      const MAX_NAME_LENGTH = 64;161      const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);162      const description = 'A';163      const tokenPrefix = 'A';164165      await expect(collectionHelper.methods166        .createNonfungibleCollection(collectionName, description, tokenPrefix)167        .call()).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);168      169    }170    {171      const MAX_DESCRIPTION_LENGTH = 256;172      const collectionName = 'A';173      const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);174      const tokenPrefix = 'A';175      await expect(collectionHelper.methods176        .createNonfungibleCollection(collectionName, description, tokenPrefix)177        .call()).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);178    }179    {180      const MAX_TOKEN_PREFIX_LENGTH = 16;181      const collectionName = 'A';182      const description = 'A';183      const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);184      await expect(collectionHelper.methods185        .createNonfungibleCollection(collectionName, description, tokenPrefix)186        .call()).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);187    }188  });189  190  itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {191    const owner = helper.eth.createAccount();192    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);193    await expect(collectionHelper.methods194      .createNonfungibleCollection('Peasantry', 'absolutely anything', 'CVE')195      .call()).to.be.rejectedWith('NotSufficientFounds');196  });197198  itEth('(!negative test!) Check owner', async ({helper}) => {199    const owner = await helper.eth.createAccountWithBalance(donor);200    const malfeasant = helper.eth.createAccount();201    const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'Transgressed', 'absolutely anything', 'COR');202    const malfeasantCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', malfeasant);203    const EXPECTED_ERROR = 'NoPermission';204    {205      const sponsor = await helper.eth.createAccountWithBalance(donor);206      await expect(malfeasantCollection.methods207        .setCollectionSponsor(sponsor)208        .call()).to.be.rejectedWith(EXPECTED_ERROR);209      210      const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);211      await expect(sponsorCollection.methods212        .confirmCollectionSponsorship()213        .call()).to.be.rejectedWith('caller is not set as sponsor');214    }215    {216      await expect(malfeasantCollection.methods217        .setCollectionLimit('account_token_ownership_limit', '1000')218        .call()).to.be.rejectedWith(EXPECTED_ERROR);219    }220  });221222  itEth('(!negative test!) Set limits', async ({helper}) => {223    const owner = await helper.eth.createAccountWithBalance(donor);224    const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'Limits', 'absolutely anything', 'OLF');225    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);226    await expect(collectionEvm.methods227      .setCollectionLimit('badLimit', 'true')228      .call()).to.be.rejectedWith('unknown boolean limit "badLimit"');229  });230});