1234567891011121314151617import {evmToAddress} from '@polkadot/util-crypto';18import {expect} from 'chai';19import {getCreatedCollectionCount, getDetailedCollectionInfo} from '../util/helpers';20import {21 evmCollectionHelpers,22 collectionIdToAddress,23 createEthAccount,24 createEthAccountWithBalance,25 evmCollection,26 itWeb3,27 getCollectionAddressFromResult,28} from './util/helpers';2930describe('Create collection from EVM', () => {31 itWeb3('Create collection', async ({api, web3}) => {32 const owner = await createEthAccountWithBalance(api, web3);33 const collectionHelper = evmCollectionHelpers(web3, owner);34 const collectionName = 'CollectionEVM';35 const description = 'Some description';36 const tokenPrefix = 'token prefix';37 38 const collectionCountBefore = await getCreatedCollectionCount(api);39 const result = await collectionHelper.methods40 .createNonfungibleCollection(collectionName, description, tokenPrefix)41 .send();42 const collectionCountAfter = await getCreatedCollectionCount(api);43 44 const {collectionId, collection} = await getCollectionAddressFromResult(api, result);45 expect(collectionCountAfter - collectionCountBefore).to.be.eq(1);46 expect(collectionId).to.be.eq(collectionCountAfter);47 expect(collection.name.map(v => String.fromCharCode(v.toNumber())).join('')).to.be.eq(collectionName);48 expect(collection.description.map(v => String.fromCharCode(v.toNumber())).join('')).to.be.eq(description);49 expect(collection.tokenPrefix.toHuman()).to.be.eq(tokenPrefix);50 });5152 itWeb3('Check collection address exist', async ({api, web3}) => {53 const owner = await createEthAccountWithBalance(api, web3);54 const collectionHelpers = evmCollectionHelpers(web3, owner);55 56 const expectedCollectionId = await getCreatedCollectionCount(api) + 1;57 const expectedCollectionAddress = collectionIdToAddress(expectedCollectionId);58 expect(await collectionHelpers.methods59 .isCollectionExist(expectedCollectionAddress)60 .call()).to.be.false;6162 await collectionHelpers.methods63 .createNonfungibleCollection('A', 'A', 'A')64 .send();65 66 expect(await collectionHelpers.methods67 .isCollectionExist(expectedCollectionAddress)68 .call()).to.be.true;69 });70 71 itWeb3('Set sponsorship', async ({api, web3}) => {72 const owner = await createEthAccountWithBalance(api, web3);73 const collectionHelpers = evmCollectionHelpers(web3, owner);74 let result = await collectionHelpers.methods.createNonfungibleCollection('Sponsor collection', '1', '1').send();75 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);76 const sponsor = await createEthAccountWithBalance(api, web3);77 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);78 result = await collectionEvm.methods.setCollectionSponsor(sponsor).send();79 let collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;80 expect(collectionSub.sponsorship.isUnconfirmed).to.be.true;81 expect(collectionSub.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));82 await expect(collectionEvm.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('Caller is not set as sponsor');83 const sponsorCollection = evmCollection(web3, sponsor, collectionIdAddress);84 await sponsorCollection.methods.confirmCollectionSponsorship().send();85 collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;86 expect(collectionSub.sponsorship.isConfirmed).to.be.true;87 expect(collectionSub.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));88 });8990 itWeb3('Set limits', async ({api, web3}) => {91 const owner = await createEthAccountWithBalance(api, web3);92 const collectionHelpers = evmCollectionHelpers(web3, owner);93 const result = await collectionHelpers.methods.createNonfungibleCollection('Const collection', '5', '5').send();94 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);95 const limits = {96 accountTokenOwnershipLimit: 1000,97 sponsoredDataSize: 1024,98 sponsoredDataRateLimit: 30,99 tokenLimit: 1000000,100 sponsorTransferTimeout: 6,101 sponsorApproveTimeout: 6,102 ownerCanTransfer: false,103 ownerCanDestroy: false,104 transfersEnabled: false,105 };106107 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);108 await collectionEvm.methods['setCollectionLimit(string,uint32)']('accountTokenOwnershipLimit', limits.accountTokenOwnershipLimit).send();109 await collectionEvm.methods['setCollectionLimit(string,uint32)']('sponsoredDataSize', limits.sponsoredDataSize).send();110 await collectionEvm.methods['setCollectionLimit(string,uint32)']('sponsoredDataRateLimit', limits.sponsoredDataRateLimit).send();111 await collectionEvm.methods['setCollectionLimit(string,uint32)']('tokenLimit', limits.tokenLimit).send();112 await collectionEvm.methods['setCollectionLimit(string,uint32)']('sponsorTransferTimeout', limits.sponsorTransferTimeout).send();113 await collectionEvm.methods['setCollectionLimit(string,uint32)']('sponsorApproveTimeout', limits.sponsorApproveTimeout).send();114 await collectionEvm.methods['setCollectionLimit(string,bool)']('ownerCanTransfer', limits.ownerCanTransfer).send();115 await collectionEvm.methods['setCollectionLimit(string,bool)']('ownerCanDestroy', limits.ownerCanDestroy).send();116 await collectionEvm.methods['setCollectionLimit(string,bool)']('transfersEnabled', limits.transfersEnabled).send();117 118 const collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;119 expect(collectionSub.limits.accountTokenOwnershipLimit.unwrap().toNumber()).to.be.eq(limits.accountTokenOwnershipLimit);120 expect(collectionSub.limits.sponsoredDataSize.unwrap().toNumber()).to.be.eq(limits.sponsoredDataSize);121 expect(collectionSub.limits.sponsoredDataRateLimit.unwrap().asBlocks.toNumber()).to.be.eq(limits.sponsoredDataRateLimit);122 expect(collectionSub.limits.tokenLimit.unwrap().toNumber()).to.be.eq(limits.tokenLimit);123 expect(collectionSub.limits.sponsorTransferTimeout.unwrap().toNumber()).to.be.eq(limits.sponsorTransferTimeout);124 expect(collectionSub.limits.sponsorApproveTimeout.unwrap().toNumber()).to.be.eq(limits.sponsorApproveTimeout);125 expect(collectionSub.limits.ownerCanTransfer.toHuman()).to.be.eq(limits.ownerCanTransfer);126 expect(collectionSub.limits.ownerCanDestroy.toHuman()).to.be.eq(limits.ownerCanDestroy);127 expect(collectionSub.limits.transfersEnabled.toHuman()).to.be.eq(limits.transfersEnabled);128 });129130 itWeb3('Collection address exist', async ({api, web3}) => {131 const owner = await createEthAccountWithBalance(api, web3);132 const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';133 const collectionHelpers = evmCollectionHelpers(web3, owner);134 expect(await collectionHelpers.methods135 .isCollectionExist(collectionAddressForNonexistentCollection).call())136 .to.be.false;137 138 const result = await collectionHelpers.methods.createNonfungibleCollection('Collection address exist', '7', '7').send();139 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);140 expect(await collectionHelpers.methods141 .isCollectionExist(collectionIdAddress).call())142 .to.be.true;143 });144});145146describe('(!negative tests!) Create collection from EVM', () => {147 itWeb3('(!negative test!) Create collection (bad lengths)', async ({api, web3}) => {148 const owner = await createEthAccountWithBalance(api, web3);149 const helper = evmCollectionHelpers(web3, owner);150 {151 const MAX_NAME_LENGHT = 64;152 const collectionName = 'A'.repeat(MAX_NAME_LENGHT + 1);153 const description = 'A';154 const tokenPrefix = 'A';155 156 await expect(helper.methods157 .createNonfungibleCollection(collectionName, description, tokenPrefix)158 .call()).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGHT);159 160 }161 { 162 const MAX_DESCRIPTION_LENGHT = 256;163 const collectionName = 'A';164 const description = 'A'.repeat(MAX_DESCRIPTION_LENGHT + 1);165 const tokenPrefix = 'A';166 await expect(helper.methods167 .createNonfungibleCollection(collectionName, description, tokenPrefix)168 .call()).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGHT);169 }170 { 171 const MAX_TOKEN_PREFIX_LENGHT = 16;172 const collectionName = 'A';173 const description = 'A';174 const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGHT + 1);175 await expect(helper.methods176 .createNonfungibleCollection(collectionName, description, tokenPrefix)177 .call()).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGHT);178 }179 });180 181 itWeb3('(!negative test!) Create collection (no funds)', async ({web3}) => {182 const owner = await createEthAccount(web3);183 const helper = evmCollectionHelpers(web3, owner);184 const collectionName = 'A';185 const description = 'A';186 const tokenPrefix = 'A';187 188 await expect(helper.methods189 .createNonfungibleCollection(collectionName, description, tokenPrefix)190 .call()).to.be.rejectedWith('NotSufficientFounds');191 });192193 itWeb3('(!negative test!) Check owner', async ({api, web3}) => {194 const owner = await createEthAccountWithBalance(api, web3);195 const notOwner = await createEthAccount(web3);196 const collectionHelpers = evmCollectionHelpers(web3, owner);197 const result = await collectionHelpers.methods.createNonfungibleCollection('A', 'A', 'A').send();198 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);199 const contractEvmFromNotOwner = evmCollection(web3, notOwner, collectionIdAddress);200 const EXPECTED_ERROR = 'NoPermission';201 {202 const sponsor = await createEthAccountWithBalance(api, web3);203 await expect(contractEvmFromNotOwner.methods204 .setCollectionSponsor(sponsor)205 .call()).to.be.rejectedWith(EXPECTED_ERROR);206 207 const sponsorCollection = evmCollection(web3, sponsor, collectionIdAddress);208 await expect(sponsorCollection.methods209 .confirmCollectionSponsorship()210 .call()).to.be.rejectedWith('Caller is not set as sponsor');211 }212 {213 await expect(contractEvmFromNotOwner.methods214 .setCollectionLimit('account_token_ownership_limit', '1000')215 .call()).to.be.rejectedWith(EXPECTED_ERROR);216 }217 });218219 itWeb3('(!negative test!) Set limits', async ({api, web3}) => {220 const owner = await createEthAccountWithBalance(api, web3);221 const collectionHelpers = evmCollectionHelpers(web3, owner);222 const result = await collectionHelpers.methods.createNonfungibleCollection('Schema collection', 'A', 'A').send();223 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);224 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);225 await expect(collectionEvm.methods226 .setCollectionLimit('badLimit', 'true')227 .call()).to.be.rejectedWith('Unknown boolean limit "badLimit"');228 });229});