git.delta.rocks / unique-network / refs/commits / 6b1c924f3c5d

difftreelog

source

tests/src/eth/createCollection.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 nonFungibleAbi from './nonFungibleAbi.json';18import {ApiPromise} from '@polkadot/api';19import {evmToAddress} from '@polkadot/util-crypto';20import {expect} from 'chai';21import {getCreatedCollectionCount, getDetailedCollectionInfo} from '../util/helpers';22import {23  evmCollectionHelper,24  collectionIdFromAddress,25  collectionIdToAddress,26  createEthAccount,27  createEthAccountWithBalance,28  evmCollection,29  GAS_ARGS,30  itWeb3,31  normalizeAddress,32  normalizeEvents,33} from './util/helpers';34import util from 'util';3536async function getCollectionAddressFromResult(api: ApiPromise, result: any) {37  const collectionIdAddress = normalizeAddress(result.events[0].raw.topics[2]);38  const collectionId = collectionIdFromAddress(collectionIdAddress);  39  const collection = (await getDetailedCollectionInfo(api, collectionId))!;40  return {collectionIdAddress, collectionId, collection};41}4243describe('Create collection from EVM', () => {44  itWeb3('Create collection', async ({api, web3}) => {45    const owner = await createEthAccountWithBalance(api, web3);46    const helper = evmCollectionHelper(web3, owner);47    const collectionName = 'CollectionEVM';48    const description = 'Some description';49    const tokenPrefix = 'token prefix';50  51    const collectionCountBefore = await getCreatedCollectionCount(api);52    const result = await helper.methods53      .create721Collection(collectionName, description, tokenPrefix)54      .send();55    const collectionCountAfter = await getCreatedCollectionCount(api);56  57    const {collectionId, collection} = await getCollectionAddressFromResult(api, result);58    expect(collectionCountAfter - collectionCountBefore).to.be.eq(1);59    expect(collectionId).to.be.eq(collectionCountAfter);60    expect(collection.name.map(v => String.fromCharCode(v.toNumber())).join('')).to.be.eq(collectionName);61    expect(collection.description.map(v => String.fromCharCode(v.toNumber())).join('')).to.be.eq(description);62    expect(collection.tokenPrefix.toHuman()).to.be.eq(tokenPrefix);63    expect(collection.schemaVersion.type).to.be.eq('ImageURL');64  });65  66  itWeb3('Set sponsorship', async ({api, web3}) => {67    const owner = await createEthAccountWithBalance(api, web3);68    const collectionHelper = evmCollectionHelper(web3, owner);69    let result = await collectionHelper.methods.create721Collection('Sponsor collection', '1', '1').send();70    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);71    const sponsor = await createEthAccountWithBalance(api, web3);72    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);73    result = await collectionEvm.methods.setSponsor(sponsor).send();74    let collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;75    expect(collectionSub.sponsorship.isUnconfirmed).to.be.true;76    expect(collectionSub.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));77    await expect(collectionEvm.methods.confirmSponsorship().call()).to.be.rejectedWith('Caller is not set as sponsor');78    const sponsorCollection = evmCollection(web3, sponsor, collectionIdAddress);79    await sponsorCollection.methods.confirmSponsorship().send();80    collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;81    expect(collectionSub.sponsorship.isConfirmed).to.be.true;82    expect(collectionSub.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));83  });8485  itWeb3('Set limits', async ({api, web3}) => {86    const owner = await createEthAccountWithBalance(api, web3);87    const collectionHelper = evmCollectionHelper(web3, owner);88    const result = await collectionHelper.methods.create721Collection('Const collection', '5', '5').send();89    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);90    const limits = {91      accountTokenOwnershipLimit: 1000,92      sponsoredDataSize: 1024,93      sponsoredDataRateLimit: {Blocks: 30},94      tokenLimit: 1000000,95      sponsorTransferTimeout: 6,96      sponsorApproveTimeout: 6,97      ownerCanTransfer: false,98      ownerCanDestroy: false,99      transfersEnabled: false,100    };101102    const limitsJson = JSON.stringify(limits, null, 1);103    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);104    await collectionEvm.methods.setLimits(limitsJson).send();105    106    const collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;107    expect(collectionSub.limits.accountTokenOwnershipLimit.unwrap().toNumber()).to.be.eq(limits.accountTokenOwnershipLimit);108    expect(collectionSub.limits.sponsoredDataSize.unwrap().toNumber()).to.be.eq(limits.sponsoredDataSize);109    expect(collectionSub.limits.sponsoredDataRateLimit.unwrap().asBlocks.toNumber()).to.be.eq(limits.sponsoredDataRateLimit.Blocks);110    expect(collectionSub.limits.tokenLimit.unwrap().toNumber()).to.be.eq(limits.tokenLimit);111    expect(collectionSub.limits.sponsorTransferTimeout.unwrap().toNumber()).to.be.eq(limits.sponsorTransferTimeout);112    expect(collectionSub.limits.sponsorApproveTimeout.unwrap().toNumber()).to.be.eq(limits.sponsorApproveTimeout);113    expect(collectionSub.limits.ownerCanTransfer.toHuman()).to.be.eq(limits.ownerCanTransfer);114    expect(collectionSub.limits.ownerCanDestroy.toHuman()).to.be.eq(limits.ownerCanDestroy);115    expect(collectionSub.limits.transfersEnabled.toHuman()).to.be.eq(limits.transfersEnabled);116  });117118  itWeb3('Check tokenURI', async ({web3, api}) => {119    const owner = await createEthAccountWithBalance(api, web3);120    const helper = evmCollectionHelper(web3, owner);121    let result = await helper.methods.create721Collection('Mint collection', '6', '6').send();122    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);123    const receiver = createEthAccount(web3);124    const contract = new web3.eth.Contract(nonFungibleAbi as any, collectionIdAddress, {from: owner, ...GAS_ARGS});125    const nextTokenId = await contract.methods.nextTokenId().call();126127    expect(nextTokenId).to.be.equal('1');128    result = await contract.methods.mintWithTokenURI(129      receiver,130      nextTokenId,131      'Test URI',132    ).send();133134    const events = normalizeEvents(result.events);135    const address = collectionIdToAddress(collectionId);136137    expect(events).to.be.deep.equal([138      {139        address,140        event: 'Transfer',141        args: {142          from: '0x0000000000000000000000000000000000000000',143          to: receiver,144          tokenId: nextTokenId,145        },146      },147    ]);148149    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');150151    // TODO: this wont work right now, need release 919000 first152    // await helper.methods.setOffchainSchema(collectionIdAddress, 'https://offchain-service.local/token-info/{id}').send();153    // const tokenUri = await contract.methods.tokenURI(nextTokenId).call();154    // expect(tokenUri).to.be.equal(`https://offchain-service.local/token-info/${nextTokenId}`);155  });156});157158describe('(!negative tests!) Create collection from EVM', () => {159  itWeb3('(!negative test!) Create collection (bad lengths)', async ({api, web3}) => {160    const owner = await createEthAccountWithBalance(api, web3);161    const helper = evmCollectionHelper(web3, owner);162    {163      const MAX_NAME_LENGHT = 64;164      const collectionName = 'A'.repeat(MAX_NAME_LENGHT + 1);165      const description = 'A';166      const tokenPrefix = 'A';167    168      await expect(helper.methods169        .create721Collection(collectionName, description, tokenPrefix)170        .call()).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGHT);171      172    }173    {  174      const MAX_DESCRIPTION_LENGHT = 256;175      const collectionName = 'A';176      const description = 'A'.repeat(MAX_DESCRIPTION_LENGHT + 1);177      const tokenPrefix = 'A';178      await expect(helper.methods179        .create721Collection(collectionName, description, tokenPrefix)180        .call()).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGHT);181    }182    {  183      const MAX_TOKEN_PREFIX_LENGHT = 16;184      const collectionName = 'A';185      const description = 'A';186      const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGHT + 1);187      await expect(helper.methods188        .create721Collection(collectionName, description, tokenPrefix)189        .call()).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGHT);190    }191  });192  193  itWeb3('(!negative test!) Create collection (no funds)', async ({web3}) => {194    const owner = await createEthAccount(web3);195    const helper = evmCollectionHelper(web3, owner);196    const collectionName = 'A';197    const description = 'A';198    const tokenPrefix = 'A';199    200    await expect(helper.methods201      .create721Collection(collectionName, description, tokenPrefix)202      .call()).to.be.rejectedWith('NotSufficientFounds');203  });204205  itWeb3('(!negative test!) Collection address (Create collection handle error)', async ({api, web3}) => {206    const owner = await createEthAccountWithBalance(api, web3);207    const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';208    const collectionEvm = evmCollection(web3, owner, collectionAddressForNonexistentCollection);209    const EXPECTED_ERROR = 'Create collection handle error';210    {211      const sponsor = await createEthAccountWithBalance(api, web3);212      await expect(collectionEvm.methods213        .setSponsor(sponsor)214        .call()).to.be.rejectedWith(EXPECTED_ERROR);215      216      const sponsorCollection = evmCollection(web3, sponsor, collectionAddressForNonexistentCollection);217      await expect(sponsorCollection.methods218        .confirmSponsorship()219        .call()).to.be.rejectedWith(EXPECTED_ERROR);220    }221    {222      const limits = '{"account_token_ownership_limit":1000}';223      await expect(collectionEvm.methods224        .setLimits(limits)225        .call()).to.be.rejectedWith(EXPECTED_ERROR);226    }227  });228229  itWeb3('(!negative test!) Check owner', async ({api, web3}) => {230    const owner = await createEthAccountWithBalance(api, web3);231    const notOwner = await createEthAccount(web3);232    const collectionHelper = evmCollectionHelper(web3, owner);233    const result = await collectionHelper.methods.create721Collection('A', 'A', 'A').send();234    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);235    const contractEvmFromNotOwner = evmCollection(web3, notOwner, collectionIdAddress);236    const EXPECTED_ERROR = 'NoPermission';237    {238      const sponsor = await createEthAccountWithBalance(api, web3);239      await expect(contractEvmFromNotOwner.methods240        .setSponsor(sponsor)241        .call()).to.be.rejectedWith(EXPECTED_ERROR);242      243      const sponsorCollection = evmCollection(web3, sponsor, collectionIdAddress);244      await expect(sponsorCollection.methods245        .confirmSponsorship()246        .call()).to.be.rejectedWith('Caller is not set as sponsor');247    }248    {249      const limits = '{"account_token_ownership_limit":1000}';250      await expect(contractEvmFromNotOwner.methods251        .setLimits(limits)252        .call()).to.be.rejectedWith(EXPECTED_ERROR);253    }254  });255256  itWeb3('(!negative test!) Set limits', async ({api, web3}) => {257    const owner = await createEthAccountWithBalance(api, web3);258    const collectionHelper = evmCollectionHelper(web3, owner);259    const result = await collectionHelper.methods.create721Collection('Schema collection', 'A', 'A').send();260    const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);261    const collectionEvm = evmCollection(web3, owner, collectionIdAddress);262    const badJson = '{accountTokenOwnershipLimit: 1000}';263    await expect(collectionEvm.methods264      .setLimits(badJson)265      .call()).to.be.rejectedWith('Parse JSON error:');266  });267});