git.delta.rocks / unique-network / refs/commits / 78a9ca040ab8

difftreelog

source

tests/src/eth/createNFTCollection.test.ts14.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';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  // Soft-deprecated74  itEth('[eth] 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.createNFTCollection(owner, 'Sponsor', 'absolutely anything', 'ROC');7980    const collection = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);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, true);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('[cross] Set sponsorship', async ({helper}) => {96    const owner = await helper.eth.createAccountWithBalance(donor);97    const sponsor = await helper.eth.createAccountWithBalance(donor);98    const ss58Format = helper.chain.getChainProperties().ss58Format;99    const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(owner, 'Sponsor', 'absolutely anything', 'ROC');100101    const collection = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);102    const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);103    await collection.methods.setCollectionSponsorCross(sponsorCross).send();104105    let data = (await helper.nft.getData(collectionId))!;106    expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));107108    await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');109110    const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);111    await sponsorCollection.methods.confirmCollectionSponsorship().send();112113    data = (await helper.nft.getData(collectionId))!;114    expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));115  });116117  itEth('Set limits', async ({helper}) => {118    const owner = await helper.eth.createAccountWithBalance(donor);119    const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(owner, 'Limits', 'absolutely anything', 'FLO');120    const limits = {121      accountTokenOwnershipLimit: 1000,122      sponsoredDataSize: 1024,123      sponsoredDataRateLimit: 30,124      tokenLimit: 1000000,125      sponsorTransferTimeout: 6,126      sponsorApproveTimeout: 6,127      ownerCanTransfer: false,128      ownerCanDestroy: false,129      transfersEnabled: false,130    };131132    const collection = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);133    await collection.methods['setCollectionLimit(string,uint32)']('accountTokenOwnershipLimit', limits.accountTokenOwnershipLimit).send();134    await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataSize', limits.sponsoredDataSize).send();135    await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataRateLimit', limits.sponsoredDataRateLimit).send();136    await collection.methods['setCollectionLimit(string,uint32)']('tokenLimit', limits.tokenLimit).send();137    await collection.methods['setCollectionLimit(string,uint32)']('sponsorTransferTimeout', limits.sponsorTransferTimeout).send();138    await collection.methods['setCollectionLimit(string,uint32)']('sponsorApproveTimeout', limits.sponsorApproveTimeout).send();139    await collection.methods['setCollectionLimit(string,bool)']('ownerCanTransfer', limits.ownerCanTransfer).send();140    await collection.methods['setCollectionLimit(string,bool)']('ownerCanDestroy', limits.ownerCanDestroy).send();141    await collection.methods['setCollectionLimit(string,bool)']('transfersEnabled', limits.transfersEnabled).send();142143    const data = (await helper.nft.getData(collectionId))!;144    expect(data.raw.limits.accountTokenOwnershipLimit).to.be.eq(limits.accountTokenOwnershipLimit);145    expect(data.raw.limits.sponsoredDataSize).to.be.eq(limits.sponsoredDataSize);146    expect(data.raw.limits.sponsoredDataRateLimit.blocks).to.be.eq(limits.sponsoredDataRateLimit);147    expect(data.raw.limits.tokenLimit).to.be.eq(limits.tokenLimit);148    expect(data.raw.limits.sponsorTransferTimeout).to.be.eq(limits.sponsorTransferTimeout);149    expect(data.raw.limits.sponsorApproveTimeout).to.be.eq(limits.sponsorApproveTimeout);150    expect(data.raw.limits.ownerCanTransfer).to.be.eq(limits.ownerCanTransfer);151    expect(data.raw.limits.ownerCanDestroy).to.be.eq(limits.ownerCanDestroy);152    expect(data.raw.limits.transfersEnabled).to.be.eq(limits.transfersEnabled);153  });154155  itEth('Collection address exist', async ({helper}) => {156    const owner = await helper.eth.createAccountWithBalance(donor);157    const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';158    expect(await helper.ethNativeContract.collectionHelpers(collectionAddressForNonexistentCollection)159      .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())160      .to.be.false;161162    const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Exister', 'absolutely anything', 'EVC');163    expect(await helper.ethNativeContract.collectionHelpers(collectionAddress)164      .methods.isCollectionExist(collectionAddress).call())165      .to.be.true;166  });167});168169describe('(!negative tests!) Create NFT collection from EVM', () => {170  let donor: IKeyringPair;171  let nominal: bigint;172173  before(async function () {174    await usingEthPlaygrounds(async (helper, privateKey) => {175      donor = await privateKey({filename: __filename});176      nominal = helper.balance.getOneTokenNominal();177    });178  });179180  itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {181    const owner = await helper.eth.createAccountWithBalance(donor);182    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);183    {184      const MAX_NAME_LENGTH = 64;185      const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);186      const description = 'A';187      const tokenPrefix = 'A';188189      await expect(collectionHelper.methods190        .createNFTCollection(collectionName, description, tokenPrefix)191        .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);192193    }194    {195      const MAX_DESCRIPTION_LENGTH = 256;196      const collectionName = 'A';197      const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);198      const tokenPrefix = 'A';199      await expect(collectionHelper.methods200        .createNFTCollection(collectionName, description, tokenPrefix)201        .call({value: Number(2n * nominal)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);202    }203    {204      const MAX_TOKEN_PREFIX_LENGTH = 16;205      const collectionName = 'A';206      const description = 'A';207      const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);208      await expect(collectionHelper.methods209        .createNFTCollection(collectionName, description, tokenPrefix)210        .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);211    }212  });213214  itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {215    const owner = await helper.eth.createAccountWithBalance(donor);216    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);217    await expect(collectionHelper.methods218      .createNFTCollection('Peasantry', 'absolutely anything', 'CVE')219      .call({value: Number(1n * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');220  });221222  // Soft-deprecated223  itEth('(!negative test!) [eth] Check owner', async ({helper}) => {224    const owner = await helper.eth.createAccountWithBalance(donor);225    const malfeasant = helper.eth.createAccount();226    const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Transgressed', 'absolutely anything', 'COR');227    const malfeasantCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', malfeasant, true);228    const EXPECTED_ERROR = 'NoPermission';229    {230      const sponsor = await helper.eth.createAccountWithBalance(donor);231      await expect(malfeasantCollection.methods232        .setCollectionSponsor(sponsor)233        .call()).to.be.rejectedWith(EXPECTED_ERROR);234235      const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor, true);236      await expect(sponsorCollection.methods237        .confirmCollectionSponsorship()238        .call()).to.be.rejectedWith('caller is not set as sponsor');239    }240    {241      await expect(malfeasantCollection.methods242        .setCollectionLimit('account_token_ownership_limit', '1000')243        .call()).to.be.rejectedWith(EXPECTED_ERROR);244    }245  });246247  itEth('(!negative test!) [cross] Check owner', async ({helper}) => {248    const owner = await helper.eth.createAccountWithBalance(donor);249    const malfeasant = helper.eth.createAccount();250    const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Transgressed', 'absolutely anything', 'COR');251    const malfeasantCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', malfeasant);252    const EXPECTED_ERROR = 'NoPermission';253    {254      const sponsor = await helper.eth.createAccountWithBalance(donor);255      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);256      await expect(malfeasantCollection.methods257        .setCollectionSponsorCross(sponsorCross)258        .call()).to.be.rejectedWith(EXPECTED_ERROR);259260      const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);261      await expect(sponsorCollection.methods262        .confirmCollectionSponsorship()263        .call()).to.be.rejectedWith('caller is not set as sponsor');264    }265    {266      await expect(malfeasantCollection.methods267        .setCollectionLimit('account_token_ownership_limit', '1000')268        .call()).to.be.rejectedWith(EXPECTED_ERROR);269    }270  });271272  itEth('(!negative test!) Set limits', async ({helper}) => {273    const owner = await helper.eth.createAccountWithBalance(donor);274    const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Limits', 'absolutely anything', 'OLF');275    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);276    await expect(collectionEvm.methods277      .setCollectionLimit('badLimit', 'true')278      .call()).to.be.rejectedWith('unknown boolean limit "badLimit"');279  });280281  itEth('destroyCollection', async ({helper}) => {282    const owner = await helper.eth.createAccountWithBalance(donor);283    const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Limits', 'absolutely anything', 'OLF');284    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);285286287    const result = await collectionHelper.methods288      .destroyCollection(collectionAddress)289      .send({from: owner});290291    const events = helper.eth.normalizeEvents(result.events);292    293    expect(events).to.be.deep.equal([294      {295        address: collectionHelper.options.address,296        event: 'CollectionDestroyed',297        args: {298          collectionId: collectionAddress,299        },300      },301    ]);302303    expect(await collectionHelper.methods304      .isCollectionExist(collectionAddress)305      .call()).to.be.false;306  });307});