git.delta.rocks / unique-network / refs/commits / 491951b54830

difftreelog

source

tests/src/eth/createRFTCollection.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 {Pallets, requirePalletsOrSkip} from '../util';20import {expect, itEth, usingEthPlaygrounds} from './util';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 = await privateKey({filename: __filename});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    const {collectionId} = await helper.eth.createRefungibleCollection(owner, name, description, prefix);41    const data = (await helper.rft.getData(collectionId))!;4243    expect(data.name).to.be.eq(name);44    expect(data.description).to.be.eq(description);45    expect(data.raw.tokenPrefix).to.be.eq(prefix);46    expect(data.raw.mode).to.be.eq('ReFungible');47  });48  49  // this test will occasionally fail when in async environment.50  itEth.skip('Check collection address exist', async ({helper}) => {51    const owner = await helper.eth.createAccountWithBalance(donor);5253    const expectedCollectionId = +(await helper.callRpc('api.rpc.unique.collectionStats')).created + 1;54    const expectedCollectionAddress = helper.ethAddress.fromCollectionId(expectedCollectionId);55    const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);5657    expect(await collectionHelpers.methods58      .isCollectionExist(expectedCollectionAddress)59      .call()).to.be.false;6061    await collectionHelpers.methods62      .createRFTCollection('A', 'A', 'A')63      .send({value: Number(2n * helper.balance.getOneTokenNominal())});64    65    expect(await collectionHelpers.methods66      .isCollectionExist(expectedCollectionAddress)67      .call()).to.be.true;68  });69  70  itEth('Set sponsorship', async ({helper}) => {71    const owner = await helper.eth.createAccountWithBalance(donor);72    const sponsor = await helper.eth.createAccountWithBalance(donor);73    const ss58Format = helper.chain.getChainProperties().ss58Format;74    const {collectionId, collectionAddress} = await helper.eth.createRefungibleCollection(owner, 'Sponsor', 'absolutely anything', 'ENVY');7576    const collection = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);77    await collection.methods.setCollectionSponsor(sponsor).send();7879    let data = (await helper.rft.getData(collectionId))!;80    expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));8182    await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');8384    const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);85    await sponsorCollection.methods.confirmCollectionSponsorship().send();8687    data = (await helper.rft.getData(collectionId))!;88    expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));89  });9091  itEth('Set limits', async ({helper}) => {92    const owner = await helper.eth.createAccountWithBalance(donor);93    const {collectionId, collectionAddress} = await helper.eth.createRefungibleCollection(owner, 'Limits', 'absolutely anything', 'INSI');94    const limits = {95      accountTokenOwnershipLimit: 1000,96      sponsoredDataSize: 1024,97      sponsoredDataRateLimit: 30,98      tokenLimit: 1000000,99      sponsorTransferTimeout: 6,100      sponsorApproveTimeout: 6,101      ownerCanTransfer: false,102      ownerCanDestroy: false,103      transfersEnabled: false,104    };105106    const collection = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);107    await collection.methods['setCollectionLimit(string,uint32)']('accountTokenOwnershipLimit', limits.accountTokenOwnershipLimit).send();108    await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataSize', limits.sponsoredDataSize).send();109    await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataRateLimit', limits.sponsoredDataRateLimit).send();110    await collection.methods['setCollectionLimit(string,uint32)']('tokenLimit', limits.tokenLimit).send();111    await collection.methods['setCollectionLimit(string,uint32)']('sponsorTransferTimeout', limits.sponsorTransferTimeout).send();112    await collection.methods['setCollectionLimit(string,uint32)']('sponsorApproveTimeout', limits.sponsorApproveTimeout).send();113    await collection.methods['setCollectionLimit(string,bool)']('ownerCanTransfer', limits.ownerCanTransfer).send();114    await collection.methods['setCollectionLimit(string,bool)']('ownerCanDestroy', limits.ownerCanDestroy).send();115    await collection.methods['setCollectionLimit(string,bool)']('transfersEnabled', limits.transfersEnabled).send();116    117    const data = (await helper.rft.getData(collectionId))!;118    expect(data.raw.limits.accountTokenOwnershipLimit).to.be.eq(limits.accountTokenOwnershipLimit);119    expect(data.raw.limits.sponsoredDataSize).to.be.eq(limits.sponsoredDataSize);120    expect(data.raw.limits.sponsoredDataRateLimit.blocks).to.be.eq(limits.sponsoredDataRateLimit);121    expect(data.raw.limits.tokenLimit).to.be.eq(limits.tokenLimit);122    expect(data.raw.limits.sponsorTransferTimeout).to.be.eq(limits.sponsorTransferTimeout);123    expect(data.raw.limits.sponsorApproveTimeout).to.be.eq(limits.sponsorApproveTimeout);124    expect(data.raw.limits.ownerCanTransfer).to.be.eq(limits.ownerCanTransfer);125    expect(data.raw.limits.ownerCanDestroy).to.be.eq(limits.ownerCanDestroy);126    expect(data.raw.limits.transfersEnabled).to.be.eq(limits.transfersEnabled);127  });128129  itEth('Collection address exist', async ({helper}) => {130    const owner = await helper.eth.createAccountWithBalance(donor);131    const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';132    expect(await helper.ethNativeContract.collectionHelpers(collectionAddressForNonexistentCollection)133      .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())134      .to.be.false;135    136    const {collectionAddress} = await helper.eth.createRefungibleCollection(owner, 'Exister', 'absolutely anything', 'WIWT');137    expect(await helper.ethNativeContract.collectionHelpers(collectionAddress)138      .methods.isCollectionExist(collectionAddress).call())139      .to.be.true;140  });141});142143describe('(!negative tests!) Create RFT collection from EVM', () => {144  let donor: IKeyringPair;145  let nominal: bigint;146147  before(async function() {148    await usingEthPlaygrounds(async (helper, privateKey) => {149      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);150      donor = await privateKey({filename: __filename});151      nominal = helper.balance.getOneTokenNominal();152    });153  });154155  itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {156    const owner = await helper.eth.createAccountWithBalance(donor);157    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);158    {159      const MAX_NAME_LENGTH = 64;160      const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);161      const description = 'A';162      const tokenPrefix = 'A';163164      await expect(collectionHelper.methods165        .createRFTCollection(collectionName, description, tokenPrefix)166        .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);167    }168    {169      const MAX_DESCRIPTION_LENGTH = 256;170      const collectionName = 'A';171      const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);172      const tokenPrefix = 'A';173      await expect(collectionHelper.methods174        .createRFTCollection(collectionName, description, tokenPrefix)175        .call({value: Number(2n * nominal)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);176    }177    {178      const MAX_TOKEN_PREFIX_LENGTH = 16;179      const collectionName = 'A';180      const description = 'A';181      const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);182      await expect(collectionHelper.methods183        .createRFTCollection(collectionName, description, tokenPrefix)184        .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);185    }186  });187  188  itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {189    const owner = await helper.eth.createAccountWithBalance(donor);190    const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);191    await expect(collectionHelper.methods192      .createRFTCollection('Peasantry', 'absolutely anything', 'TWIW')193      .call({value: Number(1n * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');194  });195196  itEth('(!negative test!) Check owner', async ({helper}) => {197    const owner = await helper.eth.createAccountWithBalance(donor);198    const peasant = helper.eth.createAccount();199    const {collectionAddress} = await helper.eth.createRefungibleCollection(owner, 'Transgressed', 'absolutely anything', 'YVNE');200    const peasantCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', peasant);201    const EXPECTED_ERROR = 'NoPermission';202    {203      const sponsor = await helper.eth.createAccountWithBalance(donor);204      await expect(peasantCollection.methods205        .setCollectionSponsor(sponsor)206        .call()).to.be.rejectedWith(EXPECTED_ERROR);207      208      const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'rft', sponsor);209      await expect(sponsorCollection.methods210        .confirmCollectionSponsorship()211        .call()).to.be.rejectedWith('caller is not set as sponsor');212    }213    {214      await expect(peasantCollection.methods215        .setCollectionLimit('account_token_ownership_limit', '1000')216        .call()).to.be.rejectedWith(EXPECTED_ERROR);217    }218  });219220  itEth('(!negative test!) Set limits', async ({helper}) => {221    const owner = await helper.eth.createAccountWithBalance(donor);222    const {collectionAddress} = await helper.eth.createRefungibleCollection(owner, 'Limits', 'absolutely anything', 'ISNI');223    const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'rft', owner);224    await expect(collectionEvm.methods225      .setCollectionLimit('badLimit', 'true')226      .call()).to.be.rejectedWith('unknown boolean limit "badLimit"');227  });228});