git.delta.rocks / unique-network / refs/commits / 8ad09da9d2af

difftreelog

source

tests/src/eth/createRFTCollection.test.ts13.0 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 {Pallets, requirePalletsOrSkip} from '../util/playgrounds';20import {expect, itEth, usingEthPlaygrounds} from './util/playgrounds';212223describe('Create RFT collection from EVM', () => {24  let donor: IKeyringPair;2526  before(async function() {27    await usingEthPlaygrounds(async (helper, privateKey) => {28      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);29      donor = privateKey('//Alice');30    });31  });3233  itEth('Create collection', async ({helper}) => {34    const owner = await helper.eth.createAccountWithBalance(donor);35    36    const name = 'CollectionEVM';37    const description = 'Some description';38    const prefix = 'token prefix';39  40    // todo:playgrounds this might fail when in async environment.41    const collectionCountBefore = +(await helper.callRpc('api.rpc.unique.collectionStats')).created;42    const {collectionId} = await helper.eth.createRFTCollection(owner, name, description, prefix);43    const collectionCountAfter = +(await helper.callRpc('api.rpc.unique.collectionStats')).created;44  45    const collection = helper.rft.getCollectionObject(collectionId);46    const data = (await collection.getData())!;4748    expect(collectionCountAfter - collectionCountBefore).to.be.eq(1);49    expect(collectionId).to.be.eq(collectionCountAfter);50    expect(data.name).to.be.eq(name);51    expect(data.description).to.be.eq(description);52    expect(data.raw.tokenPrefix).to.be.eq(prefix);53    expect(data.raw.mode).to.be.eq('ReFungible');5455    const options = await collection.getOptions();5657    expect(options.tokenPropertyPermissions).to.be.empty;58  });5960  6162  itEth('Create collection with properties', async ({helper}) => {63    const owner = await helper.eth.createAccountWithBalance(donor);6465    const name = 'CollectionEVM';66    const description = 'Some description';67    const prefix = 'token prefix';68    const baseUri = 'BaseURI';6970    // todo:playgrounds this might fail when in async environment.71    const collectionCountBefore = +(await helper.callRpc('api.rpc.unique.collectionStats')).created;72    const {collectionId} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner, name, description, prefix, baseUri);73    const collectionCountAfter = +(await helper.callRpc('api.rpc.unique.collectionStats')).created;7475    const collection = helper.rft.getCollectionObject(collectionId);76    const data = (await collection.getData())!;77    78    expect(collectionCountAfter - collectionCountBefore).to.be.eq(1);79    expect(collectionId).to.be.eq(collectionCountAfter);80    expect(data.name).to.be.eq(name);81    expect(data.description).to.be.eq(description);82    expect(data.raw.tokenPrefix).to.be.eq(prefix);83    expect(data.raw.mode).to.be.eq('ReFungible');8485    const options = await collection.getOptions();86    expect(options.tokenPropertyPermissions).to.be.deep.equal([87      {88        key: 'URI',89        permission: {mutable: true, collectionAdmin: true, tokenOwner: false},90      },91      {92        key: 'URISuffix',93        permission: {mutable: true, collectionAdmin: true, tokenOwner: false},94      },95    ]);96  });9798  // todo:playgrounds this test will fail when in async environment.99  itEth('Check collection address exist', async ({helper}) => {100    const owner = await helper.eth.createAccountWithBalance(donor);101102    const expectedCollectionId = +(await helper.callRpc('api.rpc.unique.collectionStats')).created + 1;103    const expectedCollectionAddress = helper.ethAddress.fromCollectionId(expectedCollectionId);104    const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);105106    expect(await collectionHelpers.methods107      .isCollectionExist(expectedCollectionAddress)108      .call()).to.be.false;109110    await collectionHelpers.methods111      .createRFTCollection('A', 'A', 'A')112      .send({value: Number(2n * helper.balance.getOneTokenNominal())});113    114    expect(await collectionHelpers.methods115      .isCollectionExist(expectedCollectionAddress)116      .call()).to.be.true;117  });118  119  itEth('Set sponsorship', async ({helper}) => {120    const owner = await helper.eth.createAccountWithBalance(donor);121    const sponsor = await helper.eth.createAccountWithBalance(donor);122    const ss58Format = helper.chain.getChainProperties().ss58Format;123    const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(owner, 'Sponsor', 'absolutely anything', 'ENVY');124125    const collection = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);126    await collection.methods.setCollectionSponsor(sponsor).send();127128    let data = (await helper.rft.getData(collectionId))!;129    expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));130131    await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');132133    const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);134    await sponsorCollection.methods.confirmCollectionSponsorship().send();135136    data = (await helper.rft.getData(collectionId))!;137    expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));138  });139140  itEth('Set limits', async ({helper}) => {141    const owner = await helper.eth.createAccountWithBalance(donor);142    const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(owner, 'Limits', 'absolutely anything', 'INSI');143    const limits = {144      accountTokenOwnershipLimit: 1000,145      sponsoredDataSize: 1024,146      sponsoredDataRateLimit: 30,147      tokenLimit: 1000000,148      sponsorTransferTimeout: 6,149      sponsorApproveTimeout: 6,150      ownerCanTransfer: false,151      ownerCanDestroy: false,152      transfersEnabled: false,153    };154155    const collection = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);156    await collection.methods['setCollectionLimit(string,uint32)']('accountTokenOwnershipLimit', limits.accountTokenOwnershipLimit).send();157    await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataSize', limits.sponsoredDataSize).send();158    await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataRateLimit', limits.sponsoredDataRateLimit).send();159    await collection.methods['setCollectionLimit(string,uint32)']('tokenLimit', limits.tokenLimit).send();160    await collection.methods['setCollectionLimit(string,uint32)']('sponsorTransferTimeout', limits.sponsorTransferTimeout).send();161    await collection.methods['setCollectionLimit(string,uint32)']('sponsorApproveTimeout', limits.sponsorApproveTimeout).send();162    await collection.methods['setCollectionLimit(string,bool)']('ownerCanTransfer', limits.ownerCanTransfer).send();163    await collection.methods['setCollectionLimit(string,bool)']('ownerCanDestroy', limits.ownerCanDestroy).send();164    await collection.methods['setCollectionLimit(string,bool)']('transfersEnabled', limits.transfersEnabled).send();165    166    const data = (await helper.rft.getData(collectionId))!;167    expect(data.raw.limits.accountTokenOwnershipLimit).to.be.eq(limits.accountTokenOwnershipLimit);168    expect(data.raw.limits.sponsoredDataSize).to.be.eq(limits.sponsoredDataSize);169    expect(data.raw.limits.sponsoredDataRateLimit.blocks).to.be.eq(limits.sponsoredDataRateLimit);170    expect(data.raw.limits.tokenLimit).to.be.eq(limits.tokenLimit);171    expect(data.raw.limits.sponsorTransferTimeout).to.be.eq(limits.sponsorTransferTimeout);172    expect(data.raw.limits.sponsorApproveTimeout).to.be.eq(limits.sponsorApproveTimeout);173    expect(data.raw.limits.ownerCanTransfer).to.be.eq(limits.ownerCanTransfer);174    expect(data.raw.limits.ownerCanDestroy).to.be.eq(limits.ownerCanDestroy);175    expect(data.raw.limits.transfersEnabled).to.be.eq(limits.transfersEnabled);176  });177178  itEth('Collection address exist', async ({helper}) => {179    const owner = await helper.eth.createAccountWithBalance(donor);180    const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';181    expect(await helper.ethNativeContract.collectionHelpers(collectionAddressForNonexistentCollection)182      .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())183      .to.be.false;184    185    const {collectionAddress} = await helper.eth.createRFTCollection(owner, 'Exister', 'absolutely anything', 'WIWT');186    expect(await helper.ethNativeContract.collectionHelpers(collectionAddress)187      .methods.isCollectionExist(collectionAddress).call())188      .to.be.true;189  });190});191192describe('(!negative tests!) Create RFT collection from EVM', () => {193  let donor: IKeyringPair;194  let nominal: bigint;195196  before(async function() {197    await usingEthPlaygrounds(async (helper, privateKey) => {198      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);199      donor = privateKey('//Alice');200      nominal = helper.balance.getOneTokenNominal();201    });202  });203204  itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {205    const owner = await helper.eth.createAccountWithBalance(donor);206    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);207    {208      const MAX_NAME_LENGTH = 64;209      const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);210      const description = 'A';211      const tokenPrefix = 'A';212213      await expect(collectionHelper.methods214        .createRFTCollection(collectionName, description, tokenPrefix)215        .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);216    }217    {218      const MAX_DESCRIPTION_LENGTH = 256;219      const collectionName = 'A';220      const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);221      const tokenPrefix = 'A';222      await expect(collectionHelper.methods223        .createRFTCollection(collectionName, description, tokenPrefix)224        .call({value: Number(2n * nominal)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);225    }226    {227      const MAX_TOKEN_PREFIX_LENGTH = 16;228      const collectionName = 'A';229      const description = 'A';230      const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);231      await expect(collectionHelper.methods232        .createRFTCollection(collectionName, description, tokenPrefix)233        .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);234    }235  });236  237  itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {238    const owner = await helper.eth.createAccountWithBalance(donor);239    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);240    await expect(collectionHelper.methods241      .createRFTCollection('Peasantry', 'absolutely anything', 'TWIW')242      .call({value: Number(1n * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');243  });244245  itEth('(!negative test!) Check owner', async ({helper}) => {246    const owner = await helper.eth.createAccountWithBalance(donor);247    const peasant = helper.eth.createAccount();248    const {collectionAddress} = await helper.eth.createRFTCollection(owner, 'Transgressed', 'absolutely anything', 'YVNE');249    const peasantCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', peasant);250    const EXPECTED_ERROR = 'NoPermission';251    {252      const sponsor = await helper.eth.createAccountWithBalance(donor);253      await expect(peasantCollection.methods254        .setCollectionSponsor(sponsor)255        .call()).to.be.rejectedWith(EXPECTED_ERROR);256      257      const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);258      await expect(sponsorCollection.methods259        .confirmCollectionSponsorship()260        .call()).to.be.rejectedWith('caller is not set as sponsor');261    }262    {263      await expect(peasantCollection.methods264        .setCollectionLimit('account_token_ownership_limit', '1000')265        .call()).to.be.rejectedWith(EXPECTED_ERROR);266    }267  });268269  itEth('(!negative test!) Set limits', async ({helper}) => {270    const owner = await helper.eth.createAccountWithBalance(donor);271    const {collectionAddress} = await helper.eth.createRFTCollection(owner, 'Limits', 'absolutely anything', 'ISNI');272    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);273    await expect(collectionEvm.methods274      .setCollectionLimit('badLimit', 'true')275      .call()).to.be.rejectedWith('unknown boolean limit "badLimit"');276  });277});