git.delta.rocks / unique-network / refs/commits / 98307afa9ae6

difftreelog

source

tests/src/eth/createRFTCollection.test.ts12.3 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';20import {expect, itEth, usingEthPlaygrounds} from './util';21import {CollectionLimitField} from './util/playgrounds/types';222324describe('Create RFT collection from EVM', () => {25  let donor: IKeyringPair;2627  before(async function() {28    await usingEthPlaygrounds(async (helper, privateKey) => {29      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);30      donor = await privateKey({url: import.meta.url});31    });32  });3334  itEth('Create collection', async ({helper}) => {35    const owner = await helper.eth.createAccountWithBalance(donor);3637    const name = 'CollectionEVM';38    const description = 'Some description';39    const prefix = 'token prefix';4041    const {collectionId} = await helper.eth.createRFTCollection(owner, name, description, prefix);42    const data = (await helper.rft.getData(collectionId))!;43    const collection = helper.rft.getCollectionObject(collectionId);4445    expect(data.name).to.be.eq(name);46    expect(data.description).to.be.eq(description);47    expect(data.raw.tokenPrefix).to.be.eq(prefix);48    expect(data.raw.mode).to.be.eq('ReFungible');4950    const options = await collection.getOptions();5152    expect(options.tokenPropertyPermissions).to.be.empty;53  });54555657  itEth('Create collection with properties & get description', async ({helper}) => {58    const owner = await helper.eth.createAccountWithBalance(donor);5960    const name = 'CollectionEVM';61    const description = 'Some description';62    const prefix = 'token prefix';63    const baseUri = 'BaseURI';6465    const {collectionId, collectionAddress} = await helper.eth.createERC721MetadataCompatibleRFTCollection(owner, name, description, prefix, baseUri);66    // FIXME: caller is not needed67    const contract = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);6869    const collection = helper.rft.getCollectionObject(collectionId);70    const data = (await collection.getData())!;7172    expect(data.name).to.be.eq(name);73    expect(data.description).to.be.eq(description);74    expect(data.raw.tokenPrefix).to.be.eq(prefix);75    expect(data.raw.mode).to.be.eq('ReFungible');7677    expect(await contract.methods.description().call()).to.deep.equal(description);7879    const options = await collection.getOptions();80    expect(options.tokenPropertyPermissions).to.be.deep.equal([81      {82        key: 'URI',83        permission: {mutable: true, collectionAdmin: true, tokenOwner: false},84      },85      {86        key: 'URISuffix',87        permission: {mutable: true, collectionAdmin: true, tokenOwner: false},88      },89    ]);90  });9192  // Soft-deprecated93  itEth('[eth] Set sponsorship', async ({helper}) => {94    const owner = await helper.eth.createAccountWithBalance(donor);95    const sponsor = await helper.eth.createAccountWithBalance(donor);96    const ss58Format = helper.chain.getChainProperties().ss58Format;97    const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(owner, 'Sponsor', 'absolutely anything', 'ENVY');9899    const collection = await helper.ethNativeContract.collection(collectionAddress, 'rft', owner, true);100    await collection.methods.setCollectionSponsor(sponsor).send();101102    let data = (await helper.rft.getData(collectionId))!;103    expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));104105    await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');106107    const sponsorCollection = await helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor, true);108    await sponsorCollection.methods.confirmCollectionSponsorship().send();109110    data = (await helper.rft.getData(collectionId))!;111    expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));112  });113114  itEth('[cross] Set sponsorship', async ({helper}) => {115    const owner = await helper.eth.createAccountWithBalance(donor);116    const sponsor = await helper.eth.createAccountWithBalance(donor);117    const ss58Format = helper.chain.getChainProperties().ss58Format;118    const {collectionId, collectionAddress} = await helper.eth.createRFTCollection(owner, 'Sponsor', 'absolutely anything', 'ENVY');119120    const collection = await helper.ethNativeContract.collection(collectionAddress, 'rft', owner);121    const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);122    await collection.methods.setCollectionSponsorCross(sponsorCross).send();123124    let data = (await helper.rft.getData(collectionId))!;125    expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));126127    await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');128129    const sponsorCollection = await helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);130    await sponsorCollection.methods.confirmCollectionSponsorship().send();131132    data = (await helper.rft.getData(collectionId))!;133    expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));134  });135136  itEth('Collection address exist', async ({helper}) => {137    const owner = await helper.eth.createAccountWithBalance(donor);138    const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';139    const collectionHelpers = await helper.ethNativeContract.collectionHelpers(owner);140141    expect(await collectionHelpers142      .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())143      .to.be.false;144145    const {collectionAddress} = await helper.eth.createRFTCollection(owner, 'Exister', 'absolutely anything', 'WIWT');146    expect(await collectionHelpers147      .methods.isCollectionExist(collectionAddress).call())148      .to.be.true;149150    // check collectionOwner:151    const collectionEvm = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner, true);152    const collectionOwner = await collectionEvm.methods.collectionOwner().call();153    expect(helper.address.restoreCrossAccountFromBigInt(BigInt(collectionOwner.sub))).to.eq(helper.address.ethToSubstrate(owner, true));154  });155});156157describe('(!negative tests!) Create RFT collection from EVM', () => {158  let donor: IKeyringPair;159  let nominal: bigint;160161  before(async function() {162    await usingEthPlaygrounds(async (helper, privateKey) => {163      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);164      donor = await privateKey({url: import.meta.url});165      nominal = helper.balance.getOneTokenNominal();166    });167  });168169  itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {170    const owner = await helper.eth.createAccountWithBalance(donor);171    const collectionHelper = await helper.ethNativeContract.collectionHelpers(owner);172    {173      const MAX_NAME_LENGTH = 64;174      const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);175      const description = 'A';176      const tokenPrefix = 'A';177178      await expect(collectionHelper.methods179        .createRFTCollection(collectionName, description, tokenPrefix)180        .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);181    }182    {183      const MAX_DESCRIPTION_LENGTH = 256;184      const collectionName = 'A';185      const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);186      const tokenPrefix = 'A';187      await expect(collectionHelper.methods188        .createRFTCollection(collectionName, description, tokenPrefix)189        .call({value: Number(2n * nominal)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);190    }191    {192      const MAX_TOKEN_PREFIX_LENGTH = 16;193      const collectionName = 'A';194      const description = 'A';195      const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);196      await expect(collectionHelper.methods197        .createRFTCollection(collectionName, description, tokenPrefix)198        .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);199    }200  });201202  itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {203    const owner = await helper.eth.createAccountWithBalance(donor);204    const collectionHelper = await helper.ethNativeContract.collectionHelpers(owner);205    await expect(collectionHelper.methods206      .createRFTCollection('Peasantry', 'absolutely anything', 'TWIW')207      .call({value: Number(1n * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');208  });209210  // Soft-deprecated211  itEth('(!negative test!) [eth] Check owner', async ({helper}) => {212    const owner = await helper.eth.createAccountWithBalance(donor);213    const peasant = helper.eth.createAccount();214    const {collectionAddress} = await helper.eth.createRFTCollection(owner, 'Transgressed', 'absolutely anything', 'YVNE');215    const peasantCollection = await helper.ethNativeContract.collection(collectionAddress, 'rft', peasant, true);216    const EXPECTED_ERROR = 'NoPermission';217    {218      const sponsor = await helper.eth.createAccountWithBalance(donor);219      await expect(peasantCollection.methods220        .setCollectionSponsor(sponsor)221        .call()).to.be.rejectedWith(EXPECTED_ERROR);222223      const sponsorCollection = await helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor, true);224      await expect(sponsorCollection.methods225        .confirmCollectionSponsorship()226        .call()).to.be.rejectedWith('ConfirmSponsorshipFail');227    }228    {229      await expect(peasantCollection.methods230        .setCollectionLimit({field: CollectionLimitField.AccountTokenOwnership, value: {status: true, value: 1000}})231        .call()).to.be.rejectedWith(EXPECTED_ERROR);232    }233  });234235  itEth('(!negative test!) [cross] Check owner', async ({helper}) => {236    const owner = await helper.eth.createAccountWithBalance(donor);237    const peasant = helper.eth.createAccount();238    const {collectionAddress} = await helper.eth.createRFTCollection(owner, 'Transgressed', 'absolutely anything', 'YVNE');239    const peasantCollection = await helper.ethNativeContract.collection(collectionAddress, 'rft', peasant);240    const EXPECTED_ERROR = 'NoPermission';241    {242      const sponsor = await helper.eth.createAccountWithBalance(donor);243      const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);244      await expect(peasantCollection.methods245        .setCollectionSponsorCross(sponsorCross)246        .call()).to.be.rejectedWith(EXPECTED_ERROR);247248      const sponsorCollection = await helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);249      await expect(sponsorCollection.methods250        .confirmCollectionSponsorship()251        .call()).to.be.rejectedWith('ConfirmSponsorshipFail');252    }253    {254      await expect(peasantCollection.methods255        .setCollectionLimit({field: CollectionLimitField.AccountTokenOwnership, value: {status: true, value: 1000}})256        .call()).to.be.rejectedWith(EXPECTED_ERROR);257    }258  });259260  itEth('destroyCollection', async ({helper}) => {261    const owner = await helper.eth.createAccountWithBalance(donor);262    const {collectionAddress, collectionId} = await helper.eth.createRFTCollection(owner, 'Limits', 'absolutely anything', 'OLF');263    const collectionHelper = await helper.ethNativeContract.collectionHelpers(owner);264265    await expect(collectionHelper.methods266      .destroyCollection(collectionAddress)267      .send({from: owner})).to.be.fulfilled;268269    expect(await collectionHelper.methods270      .isCollectionExist(collectionAddress)271      .call()).to.be.false;272    expect(await helper.collection.getData(collectionId)).to.be.null;273  });274});