git.delta.rocks / unique-network / refs/commits / 96d265cead38

difftreelog

source

tests/src/eth/createNFTCollection.test.ts11.6 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';202122describe('Create NFT collection from EVM', () => {23  let donor: IKeyringPair;2425  before(async function () {26    await usingEthPlaygrounds(async (_helper, privateKey) => {27      donor = await privateKey({filename: __filename});28    });29  });3031  itEth('Create collection with properties', async ({helper}) => {32    const owner = await helper.eth.createAccountWithBalance(donor);3334    const name = 'CollectionEVM';35    const description = 'Some description';36    const prefix = 'token prefix';37    const baseUri = 'BaseURI';3839    const {collectionId, collectionAddress, events} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, name, description, prefix, baseUri);4041    expect(events).to.be.deep.equal([42      {43        address: '0x6C4E9fE1AE37a41E93CEE429e8E1881aBdcbb54F',44        event: 'CollectionCreated',45        args: {46          owner: owner,47          collectionId: collectionAddress,48        },49      },50    ]);5152    const collection = helper.nft.getCollectionObject(collectionId);53    const data = (await collection.getData())!;54    55    expect(data.name).to.be.eq(name);56    expect(data.description).to.be.eq(description);57    expect(data.raw.tokenPrefix).to.be.eq(prefix);58    expect(data.raw.mode).to.be.eq('NFT');5960    const options = await collection.getOptions();61    expect(options.tokenPropertyPermissions).to.be.deep.equal([62      {63        key: 'URI',64        permission: {mutable: true, collectionAdmin: true, tokenOwner: false},65      },66      {67        key: 'URISuffix',68        permission: {mutable: true, collectionAdmin: true, tokenOwner: false},69      },70    ]);71  });7273  itEth('Set sponsorship', async ({helper}) => {74    const owner = await helper.eth.createAccountWithBalance(donor);75    const sponsor = await helper.eth.createAccountWithBalance(donor);76    const ss58Format = helper.chain.getChainProperties().ss58Format;77    const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(owner, 'Sponsor', 'absolutely anything', 'ROC');7879    const collection = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);80    await collection.methods.setCollectionSponsor(sponsor).send();8182    let data = (await helper.nft.getData(collectionId))!;83    expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));8485    await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');8687    const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);88    await sponsorCollection.methods.confirmCollectionSponsorship().send();8990    data = (await helper.nft.getData(collectionId))!;91    expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));92  });9394  itEth('Set limits', async ({helper}) => {95    const owner = await helper.eth.createAccountWithBalance(donor);96    const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(owner, 'Limits', 'absolutely anything', 'FLO');97    const limits = {98      accountTokenOwnershipLimit: 1000,99      sponsoredDataSize: 1024,100      sponsoredDataRateLimit: 30,101      tokenLimit: 1000000,102      sponsorTransferTimeout: 6,103      sponsorApproveTimeout: 6,104      ownerCanTransfer: false,105      ownerCanDestroy: false,106      transfersEnabled: false,107    };108109    const collection = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);110    await collection.methods['setCollectionLimit(string,uint32)']('accountTokenOwnershipLimit', limits.accountTokenOwnershipLimit).send();111    await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataSize', limits.sponsoredDataSize).send();112    await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataRateLimit', limits.sponsoredDataRateLimit).send();113    await collection.methods['setCollectionLimit(string,uint32)']('tokenLimit', limits.tokenLimit).send();114    await collection.methods['setCollectionLimit(string,uint32)']('sponsorTransferTimeout', limits.sponsorTransferTimeout).send();115    await collection.methods['setCollectionLimit(string,uint32)']('sponsorApproveTimeout', limits.sponsorApproveTimeout).send();116    await collection.methods['setCollectionLimit(string,bool)']('ownerCanTransfer', limits.ownerCanTransfer).send();117    await collection.methods['setCollectionLimit(string,bool)']('ownerCanDestroy', limits.ownerCanDestroy).send();118    await collection.methods['setCollectionLimit(string,bool)']('transfersEnabled', limits.transfersEnabled).send();119120    const data = (await helper.nft.getData(collectionId))!;121    expect(data.raw.limits.accountTokenOwnershipLimit).to.be.eq(limits.accountTokenOwnershipLimit);122    expect(data.raw.limits.sponsoredDataSize).to.be.eq(limits.sponsoredDataSize);123    expect(data.raw.limits.sponsoredDataRateLimit.blocks).to.be.eq(limits.sponsoredDataRateLimit);124    expect(data.raw.limits.tokenLimit).to.be.eq(limits.tokenLimit);125    expect(data.raw.limits.sponsorTransferTimeout).to.be.eq(limits.sponsorTransferTimeout);126    expect(data.raw.limits.sponsorApproveTimeout).to.be.eq(limits.sponsorApproveTimeout);127    expect(data.raw.limits.ownerCanTransfer).to.be.eq(limits.ownerCanTransfer);128    expect(data.raw.limits.ownerCanDestroy).to.be.eq(limits.ownerCanDestroy);129    expect(data.raw.limits.transfersEnabled).to.be.eq(limits.transfersEnabled);130  });131132  itEth('Collection address exist', async ({helper}) => {133    const owner = await helper.eth.createAccountWithBalance(donor);134    const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';135    expect(await helper.ethNativeContract.collectionHelpers(collectionAddressForNonexistentCollection)136      .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())137      .to.be.false;138139    const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Exister', 'absolutely anything', 'EVC');140    expect(await helper.ethNativeContract.collectionHelpers(collectionAddress)141      .methods.isCollectionExist(collectionAddress).call())142      .to.be.true;143  });144});145146describe('(!negative tests!) Create NFT collection from EVM', () => {147  let donor: IKeyringPair;148  let nominal: bigint;149150  before(async function () {151    await usingEthPlaygrounds(async (helper, privateKey) => {152      donor = await privateKey({filename: __filename});153      nominal = helper.balance.getOneTokenNominal();154    });155  });156157  itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {158    const owner = await helper.eth.createAccountWithBalance(donor);159    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);160    {161      const MAX_NAME_LENGTH = 64;162      const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);163      const description = 'A';164      const tokenPrefix = 'A';165166      await expect(collectionHelper.methods167        .createNFTCollection(collectionName, description, tokenPrefix)168        .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);169170    }171    {172      const MAX_DESCRIPTION_LENGTH = 256;173      const collectionName = 'A';174      const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);175      const tokenPrefix = 'A';176      await expect(collectionHelper.methods177        .createNFTCollection(collectionName, description, tokenPrefix)178        .call({value: Number(2n * nominal)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);179    }180    {181      const MAX_TOKEN_PREFIX_LENGTH = 16;182      const collectionName = 'A';183      const description = 'A';184      const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);185      await expect(collectionHelper.methods186        .createNFTCollection(collectionName, description, tokenPrefix)187        .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);188    }189  });190191  itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {192    const owner = await helper.eth.createAccountWithBalance(donor);193    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);194    await expect(collectionHelper.methods195      .createNFTCollection('Peasantry', 'absolutely anything', 'CVE')196      .call({value: Number(1n * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');197  });198199  itEth('(!negative test!) Check owner', async ({helper}) => {200    const owner = await helper.eth.createAccountWithBalance(donor);201    const malfeasant = helper.eth.createAccount();202    const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Transgressed', 'absolutely anything', 'COR');203    const malfeasantCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', malfeasant);204    const EXPECTED_ERROR = 'NoPermission';205    {206      const sponsor = await helper.eth.createAccountWithBalance(donor);207      await expect(malfeasantCollection.methods208        .setCollectionSponsor(sponsor)209        .call()).to.be.rejectedWith(EXPECTED_ERROR);210211      const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);212      await expect(sponsorCollection.methods213        .confirmCollectionSponsorship()214        .call()).to.be.rejectedWith('caller is not set as sponsor');215    }216    {217      await expect(malfeasantCollection.methods218        .setCollectionLimit('account_token_ownership_limit', '1000')219        .call()).to.be.rejectedWith(EXPECTED_ERROR);220    }221  });222223  itEth('(!negative test!) Set limits', async ({helper}) => {224    const owner = await helper.eth.createAccountWithBalance(donor);225    const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Limits', 'absolutely anything', 'OLF');226    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);227    await expect(collectionEvm.methods228      .setCollectionLimit('badLimit', 'true')229      .call()).to.be.rejectedWith('unknown boolean limit "badLimit"');230  });231232  itEth('destroyCollection', async ({helper}) => {233    const owner = await helper.eth.createAccountWithBalance(donor);234    const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Limits', 'absolutely anything', 'OLF');235    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);236237238    const result = await collectionHelper.methods239      .destroyCollection(collectionAddress)240      .send({from: owner});241242    const events = helper.eth.normalizeEvents(result.events);243    244    expect(events).to.be.deep.equal([245      {246        address: collectionHelper.options.address,247        event: 'CollectionDestroyed',248        args: {249          collectionId: collectionAddress,250        },251      },252    ]);253254    expect(await collectionHelper.methods255      .isCollectionExist(collectionAddress)256      .call()).to.be.false;257  });258});