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 RFT collection from EVM', () => {31 itWeb3('Create collection', async ({api, web3, privateKeyWrapper}) => {32 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);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 .createRefungibleCollection(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 expect(collection.mode.isReFungible).to.be.true;51 });5253 itWeb3('Check collection address exist', async ({api, web3, privateKeyWrapper}) => {54 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);55 const collectionHelpers = evmCollectionHelpers(web3, owner);56 57 const expectedCollectionId = await getCreatedCollectionCount(api) + 1;58 const expectedCollectionAddress = collectionIdToAddress(expectedCollectionId);59 expect(await collectionHelpers.methods60 .isCollectionExist(expectedCollectionAddress)61 .call()).to.be.false;6263 await collectionHelpers.methods64 .createRefungibleCollection('A', 'A', 'A')65 .send();66 67 expect(await collectionHelpers.methods68 .isCollectionExist(expectedCollectionAddress)69 .call()).to.be.true;70 });71 72 itWeb3.only('Set sponsorship', async ({api, web3, privateKeyWrapper}) => {73 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);74 const collectionHelpers = evmCollectionHelpers(web3, owner);75 let result = await collectionHelpers.methods.createRefungibleCollection('Sponsor collection', '1', '1').send();76 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);77 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);78 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);79 result = await collectionEvm.methods.setCollectionSponsor(sponsor).call();80 81 82 83 84 85 86 87 88 89 90 });9192 93 itWeb3('Set limits', async ({api, web3, privateKeyWrapper}) => {94 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);95 const collectionHelpers = evmCollectionHelpers(web3, owner);96 const result = await collectionHelpers.methods.createRefungibleCollection('Const collection', '5', '5').send();97 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);98 const limits = {99 accountTokenOwnershipLimit: 1000,100 sponsoredDataSize: 1024,101 sponsoredDataRateLimit: 30,102 tokenLimit: 1000000,103 sponsorTransferTimeout: 6,104 sponsorApproveTimeout: 6,105 ownerCanTransfer: false,106 ownerCanDestroy: false,107 transfersEnabled: false,108 };109110 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);111 await collectionEvm.methods['setCollectionLimit(string,uint32)']('accountTokenOwnershipLimit', limits.accountTokenOwnershipLimit).send();112 await collectionEvm.methods['setCollectionLimit(string,uint32)']('sponsoredDataSize', limits.sponsoredDataSize).send();113 await collectionEvm.methods['setCollectionLimit(string,uint32)']('sponsoredDataRateLimit', limits.sponsoredDataRateLimit).send();114 await collectionEvm.methods['setCollectionLimit(string,uint32)']('tokenLimit', limits.tokenLimit).send();115 await collectionEvm.methods['setCollectionLimit(string,uint32)']('sponsorTransferTimeout', limits.sponsorTransferTimeout).send();116 await collectionEvm.methods['setCollectionLimit(string,uint32)']('sponsorApproveTimeout', limits.sponsorApproveTimeout).send();117 await collectionEvm.methods['setCollectionLimit(string,bool)']('ownerCanTransfer', limits.ownerCanTransfer).send();118 await collectionEvm.methods['setCollectionLimit(string,bool)']('ownerCanDestroy', limits.ownerCanDestroy).send();119 await collectionEvm.methods['setCollectionLimit(string,bool)']('transfersEnabled', limits.transfersEnabled).send();120 121 const collectionSub = (await getDetailedCollectionInfo(api, collectionId))!;122 expect(collectionSub.limits.accountTokenOwnershipLimit.unwrap().toNumber()).to.be.eq(limits.accountTokenOwnershipLimit);123 expect(collectionSub.limits.sponsoredDataSize.unwrap().toNumber()).to.be.eq(limits.sponsoredDataSize);124 expect(collectionSub.limits.sponsoredDataRateLimit.unwrap().asBlocks.toNumber()).to.be.eq(limits.sponsoredDataRateLimit);125 expect(collectionSub.limits.tokenLimit.unwrap().toNumber()).to.be.eq(limits.tokenLimit);126 expect(collectionSub.limits.sponsorTransferTimeout.unwrap().toNumber()).to.be.eq(limits.sponsorTransferTimeout);127 expect(collectionSub.limits.sponsorApproveTimeout.unwrap().toNumber()).to.be.eq(limits.sponsorApproveTimeout);128 expect(collectionSub.limits.ownerCanTransfer.toHuman()).to.be.eq(limits.ownerCanTransfer);129 expect(collectionSub.limits.ownerCanDestroy.toHuman()).to.be.eq(limits.ownerCanDestroy);130 expect(collectionSub.limits.transfersEnabled.toHuman()).to.be.eq(limits.transfersEnabled);131 });132133 itWeb3('Collection address exist', async ({api, web3, privateKeyWrapper}) => {134 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);135 const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';136 const collectionHelpers = evmCollectionHelpers(web3, owner);137 expect(await collectionHelpers.methods138 .isCollectionExist(collectionAddressForNonexistentCollection).call())139 .to.be.false;140 141 const result = await collectionHelpers.methods.createRefungibleCollection('Collection address exist', '7', '7').send();142 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);143 expect(await collectionHelpers.methods144 .isCollectionExist(collectionIdAddress).call())145 .to.be.true;146 });147});148149describe('(!negative tests!) Create RFT collection from EVM', () => {150 itWeb3('(!negative test!) Create collection (bad lengths)', async ({api, web3, privateKeyWrapper}) => {151 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);152 const helper = evmCollectionHelpers(web3, owner);153 {154 const MAX_NAME_LENGHT = 64;155 const collectionName = 'A'.repeat(MAX_NAME_LENGHT + 1);156 const description = 'A';157 const tokenPrefix = 'A';158 159 await expect(helper.methods160 .createRefungibleCollection(collectionName, description, tokenPrefix)161 .call()).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGHT);162 163 }164 { 165 const MAX_DESCRIPTION_LENGHT = 256;166 const collectionName = 'A';167 const description = 'A'.repeat(MAX_DESCRIPTION_LENGHT + 1);168 const tokenPrefix = 'A';169 await expect(helper.methods170 .createRefungibleCollection(collectionName, description, tokenPrefix)171 .call()).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGHT);172 }173 { 174 const MAX_TOKEN_PREFIX_LENGHT = 16;175 const collectionName = 'A';176 const description = 'A';177 const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGHT + 1);178 await expect(helper.methods179 .createRefungibleCollection(collectionName, description, tokenPrefix)180 .call()).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGHT);181 }182 });183 184 itWeb3('(!negative test!) Create collection (no funds)', async ({web3}) => {185 const owner = await createEthAccount(web3);186 const helper = evmCollectionHelpers(web3, owner);187 const collectionName = 'A';188 const description = 'A';189 const tokenPrefix = 'A';190 191 await expect(helper.methods192 .createRefungibleCollection(collectionName, description, tokenPrefix)193 .call()).to.be.rejectedWith('NotSufficientFounds');194 });195196 197 itWeb3('(!negative test!) Check owner', async ({api, web3, privateKeyWrapper}) => {198 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);199 const notOwner = await createEthAccount(web3);200 const collectionHelpers = evmCollectionHelpers(web3, owner);201 const result = await collectionHelpers.methods.createRefungibleCollection('A', 'A', 'A').send();202 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);203 const contractEvmFromNotOwner = evmCollection(web3, notOwner, collectionIdAddress);204 const EXPECTED_ERROR = 'NoPermission';205 {206 const sponsor = await createEthAccountWithBalance(api, web3, privateKeyWrapper);207 await expect(contractEvmFromNotOwner.methods208 .setCollectionSponsor(sponsor)209 .call()).to.be.rejectedWith(EXPECTED_ERROR);210 211 const sponsorCollection = evmCollection(web3, sponsor, collectionIdAddress);212 await expect(sponsorCollection.methods213 .confirmCollectionSponsorship()214 .call()).to.be.rejectedWith('caller is not set as sponsor');215 }216 {217 await expect(contractEvmFromNotOwner.methods218 .setCollectionLimit('account_token_ownership_limit', '1000')219 .call()).to.be.rejectedWith(EXPECTED_ERROR);220 }221 });222223 224 itWeb3('(!negative test!) Set limits', async ({api, web3, privateKeyWrapper}) => {225 const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);226 const collectionHelpers = evmCollectionHelpers(web3, owner);227 const result = await collectionHelpers.methods.createRefungibleCollection('Schema collection', 'A', 'A').send();228 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);229 const collectionEvm = evmCollection(web3, owner, collectionIdAddress);230 await expect(collectionEvm.methods231 .setCollectionLimit('badLimit', 'true')232 .call()).to.be.rejectedWith('unknown boolean limit "badLimit"');233 });234});