1234567891011121314151617import 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 collectionHelper,24 collectionIdFromAddress,25 collectionIdToAddress,26 createEthAccount,27 createEthAccountWithBalance,28 GAS_ARGS,29 itWeb3,30 normalizeAddress,31 normalizeEvents,32} from './util/helpers';3334async function getCollectionAddressFromResult(api: ApiPromise, result: any) {35 const collectionIdAddress = normalizeAddress(result.events[0].raw.topics[2]);36 const collectionId = collectionIdFromAddress(collectionIdAddress); 37 const collection = (await getDetailedCollectionInfo(api, collectionId))!;38 return {collectionIdAddress, collectionId, collection};39}4041describe('Create collection from EVM', () => {42 itWeb3('Create collection', async ({api, web3}) => {43 const owner = await createEthAccountWithBalance(api, web3);44 const helper = collectionHelper(web3, owner);45 const collectionName = 'CollectionEVM';46 const description = 'Some description';47 const tokenPrefix = 'token prefix';48 49 const collectionCountBefore = await getCreatedCollectionCount(api);50 const result = await helper.methods51 .create721Collection(collectionName, description, tokenPrefix)52 .send();53 const collectionCountAfter = await getCreatedCollectionCount(api);54 55 const {collectionId, collection} = await getCollectionAddressFromResult(api, result);56 expect(collectionCountAfter - collectionCountBefore).to.be.eq(1);57 expect(collectionId).to.be.eq(collectionCountAfter);58 expect(collection.name.map(v => String.fromCharCode(v.toNumber())).join('')).to.be.eq(collectionName);59 expect(collection.description.map(v => String.fromCharCode(v.toNumber())).join('')).to.be.eq(description);60 expect(collection.tokenPrefix.toHuman()).to.be.eq(tokenPrefix);61 expect(collection.schemaVersion.type).to.be.eq('ImageURL');62 });63 64 itWeb3('Set sponsorship', async ({api, web3}) => {65 const owner = await createEthAccountWithBalance(api, web3);66 const helper = collectionHelper(web3, owner);67 let result = await helper.methods.create721Collection('Sponsor collection', '1', '1').send();68 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);69 const sponsor = await createEthAccountWithBalance(api, web3);70 result = await helper.methods.setSponsor(collectionIdAddress, sponsor).send();71 let collection = (await getDetailedCollectionInfo(api, collectionId))!;72 expect(collection.sponsorship.isUnconfirmed).to.be.true;73 expect(collection.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));74 await expect(helper.methods.confirmSponsorship(collectionIdAddress).call()).to.be.rejectedWith('Caller is not set as sponsor');75 const sponsorHelper = collectionHelper(web3, sponsor);76 await sponsorHelper.methods.confirmSponsorship(collectionIdAddress).send();77 collection = (await getDetailedCollectionInfo(api, collectionId))!;78 expect(collection.sponsorship.isConfirmed).to.be.true;79 expect(collection.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));80 });8182 itWeb3('Set limits', async ({api, web3}) => {83 const owner = await createEthAccountWithBalance(api, web3);84 const helper = collectionHelper(web3, owner);85 const result = await helper.methods.create721Collection('Const collection', '5', '5').send();86 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);87 const limits = {88 accountTokenOwnershipLimit: 1000,89 sponsoredDataSize: 1024,90 sponsoredDataRateLimit: {Blocks: 30},91 tokenLimit: 1000000,92 sponsorTransferTimeout: 6,93 sponsorApproveTimeout: 6,94 ownerCanTransfer: false,95 ownerCanDestroy: false,96 transfersEnabled: false,97 };9899 const limitsJson = JSON.stringify(limits, null, 1);100 await helper.methods.setLimits(collectionIdAddress, limitsJson).send();101 102 const collection = (await getDetailedCollectionInfo(api, collectionId))!;103 expect(collection.limits.accountTokenOwnershipLimit.unwrap().toNumber()).to.be.eq(limits.accountTokenOwnershipLimit);104 expect(collection.limits.sponsoredDataSize.unwrap().toNumber()).to.be.eq(limits.sponsoredDataSize);105 expect(collection.limits.sponsoredDataRateLimit.unwrap().asBlocks.toNumber()).to.be.eq(limits.sponsoredDataRateLimit.Blocks);106 expect(collection.limits.tokenLimit.unwrap().toNumber()).to.be.eq(limits.tokenLimit);107 expect(collection.limits.sponsorTransferTimeout.unwrap().toNumber()).to.be.eq(limits.sponsorTransferTimeout);108 expect(collection.limits.sponsorApproveTimeout.unwrap().toNumber()).to.be.eq(limits.sponsorApproveTimeout);109 expect(collection.limits.ownerCanTransfer.toHuman()).to.be.eq(limits.ownerCanTransfer);110 expect(collection.limits.ownerCanDestroy.toHuman()).to.be.eq(limits.ownerCanDestroy);111 expect(collection.limits.transfersEnabled.toHuman()).to.be.eq(limits.transfersEnabled);112 });113114 itWeb3('Check tokenURI', async ({web3, api}) => {115 const owner = await createEthAccountWithBalance(api, web3);116 const helper = collectionHelper(web3, owner);117 let result = await helper.methods.create721Collection('Mint collection', '6', '6').send();118 const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);119 const receiver = createEthAccount(web3);120 const contract = new web3.eth.Contract(nonFungibleAbi as any, collectionIdAddress, {from: owner, ...GAS_ARGS});121 const nextTokenId = await contract.methods.nextTokenId().call();122123 expect(nextTokenId).to.be.equal('1');124 result = await contract.methods.mintWithTokenURI(125 receiver,126 nextTokenId,127 'Test URI',128 ).send();129130 const events = normalizeEvents(result.events);131 const address = collectionIdToAddress(collectionId);132133 expect(events).to.be.deep.equal([134 {135 address,136 event: 'Transfer',137 args: {138 from: '0x0000000000000000000000000000000000000000',139 to: receiver,140 tokenId: nextTokenId,141 },142 },143 ]);144145 expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Test URI');146147 148 149 150 151 });152});153154describe('(!negative tests!) Create collection from EVM', () => {155 itWeb3('(!negative test!) Create collection (bad lengths)', async ({api, web3}) => {156 const owner = await createEthAccountWithBalance(api, web3);157 const helper = collectionHelper(web3, owner);158 {159 const MAX_NAME_LENGHT = 64;160 const collectionName = 'A'.repeat(MAX_NAME_LENGHT + 1);161 const description = 'A';162 const tokenPrefix = 'A';163 164 await expect(helper.methods165 .create721Collection(collectionName, description, tokenPrefix)166 .call()).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGHT);167 168 }169 { 170 const MAX_DESCRIPTION_LENGHT = 256;171 const collectionName = 'A';172 const description = 'A'.repeat(MAX_DESCRIPTION_LENGHT + 1);173 const tokenPrefix = 'A';174 await expect(helper.methods175 .create721Collection(collectionName, description, tokenPrefix)176 .call()).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGHT);177 }178 { 179 const MAX_TOKEN_PREFIX_LENGHT = 16;180 const collectionName = 'A';181 const description = 'A';182 const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGHT + 1);183 await expect(helper.methods184 .create721Collection(collectionName, description, tokenPrefix)185 .call()).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGHT);186 }187 });188 189 itWeb3('(!negative test!) Create collection (no funds)', async ({web3}) => {190 const owner = await createEthAccount(web3);191 const helper = collectionHelper(web3, owner);192 const collectionName = 'A';193 const description = 'A';194 const tokenPrefix = 'A';195 196 await expect(helper.methods197 .create721Collection(collectionName, description, tokenPrefix)198 .call()).to.be.rejectedWith('NotSufficientFounds');199 });200201 itWeb3('(!negative test!) Collection address (Contract is not an unique collection)', async ({api, web3}) => {202 const owner = await createEthAccountWithBalance(api, web3);203 const helper = collectionHelper(web3, owner);204 const collectionAddressWithBadPrefix = '0x00112233445566778899AABBCCDDEEFF00112233';205 const EXPECTED_ERROR = 'Contract is not an unique collection';206 {207 const sponsor = await createEthAccountWithBalance(api, web3);208 await expect(helper.methods209 .setSponsor(collectionAddressWithBadPrefix, sponsor)210 .call()).to.be.rejectedWith(EXPECTED_ERROR);211 212 const sponsorHelper = collectionHelper(web3, sponsor);213 await expect(sponsorHelper.methods214 .confirmSponsorship(collectionAddressWithBadPrefix)215 .call()).to.be.rejectedWith(EXPECTED_ERROR);216 }217 {218 const limits = '{"account_token_ownership_limit":1000}';219 await expect(helper.methods220 .setLimits(collectionAddressWithBadPrefix, limits)221 .call()).to.be.rejectedWith(EXPECTED_ERROR);222 }223 });224225 itWeb3('(!negative test!) Check owner', async ({api, web3}) => {226 const owner = await createEthAccountWithBalance(api, web3);227 const notOwner = await createEthAccount(web3);228 const helperFromOwner = collectionHelper(web3, owner);229 const helperFromNotOwner = collectionHelper(web3, notOwner);230 const result = await helperFromOwner.methods.create721Collection('A', 'A', 'A').send();231 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);232 const EXPECTED_ERROR = 'NoPermission';233 {234 const sponsor = await createEthAccountWithBalance(api, web3);235 await expect(helperFromNotOwner.methods236 .setSponsor(collectionIdAddress, sponsor)237 .call()).to.be.rejectedWith(EXPECTED_ERROR);238 239 const sponsorHelper = collectionHelper(web3, sponsor);240 await expect(sponsorHelper.methods241 .confirmSponsorship(collectionIdAddress)242 .call()).to.be.rejectedWith('Caller is not set as sponsor');243 }244 {245 const limits = '{"account_token_ownership_limit":1000}';246 await expect(helperFromNotOwner.methods247 .setLimits(collectionIdAddress, limits)248 .call()).to.be.rejectedWith(EXPECTED_ERROR);249 }250 });251252 itWeb3('(!negative test!) Set limits', async ({api, web3}) => {253 const owner = await createEthAccountWithBalance(api, web3);254 const helper = collectionHelper(web3, owner);255 const result = await helper.methods.create721Collection('Schema collection', 'A', 'A').send();256 const {collectionIdAddress} = await getCollectionAddressFromResult(api, result);257 const badJson = '{accountTokenOwnershipLimit: 1000}';258 await expect(helper.methods259 .setLimits(collectionIdAddress, badJson)260 .call()).to.be.rejectedWith('Parse JSON error:');261 });262});