1234567891011121314151617import {ApiPromise} from '@polkadot/api';18import {evmToAddress} from '@polkadot/util-crypto';19import {expect} from 'chai';20import {getCreatedCollectionCount, getDetailedCollectionInfo} from '../util/helpers';21import {collectionHelper, collectionIdFromAddress, createEthAccount, createEthAccountWithBalance, itWeb3, normalizeAddress} from './util/helpers';2223async function getCollectionAddressFromResult(api: ApiPromise, result: any) {24 const collectionIdAddress = normalizeAddress(result.events[0].raw.topics[2]);25 const collectionId = collectionIdFromAddress(collectionIdAddress); 26 const collection = (await getDetailedCollectionInfo(api, collectionId))!;27 return {collectionIdAddress, collectionId, collection};28}2930describe('Create collection from EVM', () => {31 itWeb3('Create collection', async ({api, web3}) => {32 const owner = await createEthAccountWithBalance(api, web3);33 const helper = collectionHelper(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 helper.methods40 .create721Collection(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.schemaVersion.type).to.be.eq('ImageURL');51 });52 53 itWeb3('Set sponsorship', async ({api, web3}) => {54 const owner = await createEthAccountWithBalance(api, web3);55 const helper = collectionHelper(web3, owner);56 let result = await helper.methods.create721Collection('Sponsor collection', '1', '1').send();57 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);58 const sponsor = await createEthAccountWithBalance(api, web3);59 result = await helper.methods.setSponsor(collectionIdAddress, sponsor).send();60 let collection = (await getDetailedCollectionInfo(api, collectionId))!;61 expect(collection.sponsorship.isUnconfirmed).to.be.true;62 expect(collection.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));63 await expect(helper.methods.confirmSponsorship(collectionIdAddress).call()).to.be.rejectedWith('Caller is not set as sponsor');64 const sponsorHelper = collectionHelper(web3, sponsor);65 await sponsorHelper.methods.confirmSponsorship(collectionIdAddress).send();66 collection = (await getDetailedCollectionInfo(api, collectionId))!;67 expect(collection.sponsorship.isConfirmed).to.be.true;68 expect(collection.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));69 });70 71 itWeb3('Set offchain shema', async ({api, web3}) => {72 const owner = await createEthAccountWithBalance(api, web3);73 const helper = collectionHelper(web3, owner);74 let result = await helper.methods.create721Collection('Shema collection', '2', '2').send();75 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);76 const shema = 'Some shema';77 result = await helper.methods.setOffchainShema(collectionIdAddress, shema).send();78 const collection = (await getDetailedCollectionInfo(api, collectionId))!;79 expect(collection.offchainSchema.toHuman()).to.be.eq(shema);80 });81 82 itWeb3('Set variable on chain schema', async ({api, web3}) => {83 const owner = await createEthAccountWithBalance(api, web3);84 const helper = collectionHelper(web3, owner);85 let result = await helper.methods.create721Collection('Variable collection', '3', '3').send();86 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);87 const variable = 'Some variable';88 result = await helper.methods.setVariableOnChainSchema(collectionIdAddress, variable).send();89 const collection = (await getDetailedCollectionInfo(api, collectionId))!;90 expect(collection.variableOnChainSchema.toHuman()).to.be.eq(variable);91 });92 93 itWeb3('Set const on chain schema', async ({api, web3}) => {94 const owner = await createEthAccountWithBalance(api, web3);95 const helper = collectionHelper(web3, owner);96 let result = await helper.methods.create721Collection('Const collection', '4', '4').send();97 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);98 const constShema = 'Some const';99 result = await helper.methods.setConstOnChainSchema(collectionIdAddress, constShema).send();100 const collection = (await getDetailedCollectionInfo(api, collectionId))!;101 expect(collection.constOnChainSchema.toHuman()).to.be.eq(constShema);102 });103104 itWeb3('Set limits', async ({api, web3}) => {105 const owner = await createEthAccountWithBalance(api, web3);106 const helper = collectionHelper(web3, owner);107 const result = await helper.methods.create721Collection('Const collection', '4', '4').send();108 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);109 const limits = {110 accountTokenOwnershipLimit: 1000,111 sponsoredDataSize: 1024,112 113 tokenLimit: 1000000,114 sponsorTransferTimeout: 6,115 sponsorApproveTimeout: 6,116 ownerCanTransfer: false,117 ownerCanDestroy: false,118 transfersEnabled: false,119 };120 const limitsJson = '{' +121 '"account_token_ownership_limit": '+ limits.accountTokenOwnershipLimit +',' +122 '"sponsored_data_size": ' + limits.sponsoredDataSize + ',' +123 124 '"token_limit": ' + limits.tokenLimit + ',' +125 '"sponsor_transfer_timeout": ' + limits.sponsorTransferTimeout + ',' +126 '"sponsor_approve_timeout": ' + limits.sponsorApproveTimeout + ',' +127 '"owner_can_transfer": ' + limits.ownerCanTransfer + ',' +128 '"owner_can_destroy": ' + limits.ownerCanDestroy + ',' +129 '"transfers_enabled": ' + limits.transfersEnabled +130 '}';131132 await helper.methods.setLimits(collectionIdAddress, limitsJson).send();133 134 const collection = (await getDetailedCollectionInfo(api, collectionId))!;135 expect(collection.limits.accountTokenOwnershipLimit.unwrap().toNumber()).to.be.eq(limits.accountTokenOwnershipLimit);136 expect(collection.limits.sponsoredDataSize.unwrap().toNumber()).to.be.eq(limits.sponsoredDataSize);137 expect(collection.limits.tokenLimit.unwrap().toNumber()).to.be.eq(limits.tokenLimit);138 expect(collection.limits.sponsorTransferTimeout.unwrap().toNumber()).to.be.eq(limits.sponsorTransferTimeout);139 expect(collection.limits.sponsorApproveTimeout.unwrap().toNumber()).to.be.eq(limits.sponsorApproveTimeout);140 expect(collection.limits.ownerCanTransfer.toHuman()).to.be.eq(limits.ownerCanTransfer);141 expect(collection.limits.ownerCanDestroy.toHuman()).to.be.eq(limits.ownerCanDestroy);142 expect(collection.limits.transfersEnabled.toHuman()).to.be.eq(limits.transfersEnabled);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 = collectionHelper(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 .create721Collection(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 .create721Collection(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 .create721Collection(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 = collectionHelper(web3, owner);184 const collectionName = 'A';185 const description = 'A';186 const tokenPrefix = 'A';187 188 await expect(helper.methods189 .create721Collection(collectionName, description, tokenPrefix)190 .call()).to.be.rejectedWith('NotSufficientFounds');191 });192193 itWeb3('(!negative test!) Collection address (Bad ETH prefix)', async ({api, web3}) => {194 const owner = await createEthAccountWithBalance(api, web3);195 const helper = collectionHelper(web3, owner);196 const collectionAddressWithBadPrefix = '0x00112233445566778899AABBCCDDEEFF00112233';197 const EXPECTED_ERROR = 'Bad ETH prefix';198 {199 const sponsor = await createEthAccountWithBalance(api, web3);200 await expect(helper.methods201 .setSponsor(collectionAddressWithBadPrefix, sponsor)202 .call()).to.be.rejectedWith(EXPECTED_ERROR);203 204 const sponsorHelper = collectionHelper(web3, sponsor);205 await expect(sponsorHelper.methods206 .confirmSponsorship(collectionAddressWithBadPrefix)207 .call()).to.be.rejectedWith(EXPECTED_ERROR);208 }209 {210 const shema = 'Some shema';211 await expect(helper.methods212 .setOffchainShema(collectionAddressWithBadPrefix, shema)213 .call()).to.be.rejectedWith(EXPECTED_ERROR);214 }215 {216 const variable = 'Some variable';217 await expect(helper.methods218 .setVariableOnChainSchema(collectionAddressWithBadPrefix, variable)219 .call()).to.be.rejectedWith(EXPECTED_ERROR);220 }221 {222 const constData = 'Some const';223 await expect(helper.methods224 .setConstOnChainSchema(collectionAddressWithBadPrefix, constData)225 .call()).to.be.rejectedWith(EXPECTED_ERROR);226 }227 {228 const limits = '{"account_token_ownership_limit":1000}';229 await expect(helper.methods230 .setLimits(collectionAddressWithBadPrefix, limits)231 .call()).to.be.rejectedWith(EXPECTED_ERROR);232 }233 });234235 itWeb3('(!negative test!) Check owner', async ({api, web3}) => {236 const owner = await createEthAccountWithBalance(api, web3);237 const notOwner = await createEthAccount(web3);238 const helperFromOwner = collectionHelper(web3, owner);239 const helperFromNotOwner = collectionHelper(web3, notOwner);240 const result = await helperFromOwner.methods.create721Collection('A', 'A', 'A').send();241 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);242 const EXPECTED_ERROR = 'NoPermission';243 {244 const sponsor = await createEthAccountWithBalance(api, web3);245 await expect(helperFromNotOwner.methods246 .setSponsor(collectionIdAddress, sponsor)247 .call()).to.be.rejectedWith(EXPECTED_ERROR);248 249 const sponsorHelper = collectionHelper(web3, sponsor);250 await expect(sponsorHelper.methods251 .confirmSponsorship(collectionIdAddress)252 .call()).to.be.rejectedWith('Caller is not set as sponsor');253 }254 {255 const shema = 'Some shema';256 await expect(helperFromNotOwner.methods257 .setOffchainShema(collectionIdAddress, shema)258 .call()).to.be.rejectedWith(EXPECTED_ERROR);259 }260 {261 const variable = 'Some variable';262 await expect(helperFromNotOwner.methods263 .setVariableOnChainSchema(collectionIdAddress, variable)264 .call()).to.be.rejectedWith(EXPECTED_ERROR);265 }266 {267 const constData = 'Some const';268 await expect(helperFromNotOwner.methods269 .setConstOnChainSchema(collectionIdAddress, constData)270 .call()).to.be.rejectedWith(EXPECTED_ERROR);271 }272 {273 const limits = '{"account_token_ownership_limit":1000}';274 await expect(helperFromNotOwner.methods275 .setLimits(collectionIdAddress, limits)276 .call()).to.be.rejectedWith(EXPECTED_ERROR);277 }278 });279280 itWeb3('(!negative test!) Set offchain shema (length limit)', async ({api, web3}) => {281 const owner = await createEthAccountWithBalance(api, web3);282 const helper = collectionHelper(web3, owner);283 const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send();284 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);285 const OFFCHAIN_SCHEMA_LIMIT = 8192;286 const shema = 'A'.repeat(OFFCHAIN_SCHEMA_LIMIT + 1);287 await expect(helper.methods288 .setOffchainShema(collectionIdAddress, shema)289 .call()).to.be.rejectedWith('shema is too long. Max length is ' + OFFCHAIN_SCHEMA_LIMIT);290 });291292 itWeb3('(!negative test!) Set variable on chain schema (length limit)', async ({api, web3}) => {293 const owner = await createEthAccountWithBalance(api, web3);294 const helper = collectionHelper(web3, owner);295 const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send();296 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);297 const VARIABLE_ON_CHAIN_SCHEMA_LIMIT = 8192;298 const variable = 'A'.repeat(VARIABLE_ON_CHAIN_SCHEMA_LIMIT + 1);299 await expect(helper.methods300 .setVariableOnChainSchema(collectionIdAddress, variable)301 .call()).to.be.rejectedWith('variable is too long. Max length is ' + VARIABLE_ON_CHAIN_SCHEMA_LIMIT);302 });303304 itWeb3('(!negative test!) Set const on chain schema (length limit)', async ({api, web3}) => {305 const owner = await createEthAccountWithBalance(api, web3);306 const helper = collectionHelper(web3, owner);307 const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send();308 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);309 const CONST_ON_CHAIN_SCHEMA_LIMIT = 32768;310 const constData = 'A'.repeat(CONST_ON_CHAIN_SCHEMA_LIMIT + 1);311 await expect(helper.methods312 .setConstOnChainSchema(collectionIdAddress, constData)313 .call()).to.be.rejectedWith('const_on_chain is too long. Max length is ' + CONST_ON_CHAIN_SCHEMA_LIMIT);314 });315316 itWeb3('(!negative test!) Set limits', async ({api, web3}) => {317 const owner = await createEthAccountWithBalance(api, web3);318 const helper = collectionHelper(web3, owner);319 const result = await helper.methods.create721Collection('Shema collection', 'A', 'A').send();320 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);321 const badJson = '{accountTokenOwnershipLimit: 1000}';322 await expect(helper.methods323 .setLimits(collectionIdAddress, badJson)324 .call()).to.be.rejectedWith('Parse JSON error:');325 });326});