1234567891011121314151617import {evmToAddress} from '@polkadot/util-crypto';18import {IKeyringPair} from '@polkadot/types/types';19import {expect, itEth, usingEthPlaygrounds} from './util/playgrounds';20import {UNIQUE} from '../util/helpers';2122describe('Create NFT collection from EVM', () => {23 let donor: IKeyringPair;2425 before(async function() {26 await usingEthPlaygrounds(async (_helper, privateKey) => {27 donor = privateKey('//Alice');28 });29 });3031 itEth('Create collection', async ({helper}) => {32 const owner = await helper.eth.createAccountWithBalance(donor);3334 const name = 'CollectionEVM';35 const description = 'Some description';36 const prefix = 'token prefix';3738 39 const collectionCountBefore = +(await helper.callRpc('api.rpc.unique.collectionStats')).created;40 const {collectionId} = await helper.eth.createNonfungibleCollection(owner, name, description, prefix);41 const collectionCountAfter = +(await helper.callRpc('api.rpc.unique.collectionStats')).created;4243 const collection = helper.nft.getCollectionObject(collectionId);44 const data = (await collection.getData())!;45 46 expect(collectionCountAfter - collectionCountBefore).to.be.eq(1);47 expect(collectionId).to.be.eq(collectionCountAfter);48 expect(data.name).to.be.eq(name);49 expect(data.description).to.be.eq(description);50 expect(data.raw.tokenPrefix).to.be.eq(prefix);51 expect(data.raw.mode).to.be.eq('NFT');52 });5354 55 itEth('Check collection address exist', async ({helper}) => {56 const owner = await helper.eth.createAccountWithBalance(donor);5758 const expectedCollectionId = +(await helper.callRpc('api.rpc.unique.collectionStats')).created + 1;59 const expectedCollectionAddress = helper.ethAddress.fromCollectionId(expectedCollectionId);60 const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);6162 expect(await collectionHelpers.methods63 .isCollectionExist(expectedCollectionAddress)64 .call()).to.be.false;6566 await collectionHelpers.methods67 .createNonfungibleCollection('A', 'A', 'A')68 .send({value: Number(2n * UNIQUE)});69 70 expect(await collectionHelpers.methods71 .isCollectionExist(expectedCollectionAddress)72 .call()).to.be.true;73 });74 75 itEth('Set sponsorship', async ({helper}) => {76 const owner = await helper.eth.createAccountWithBalance(donor);77 const sponsor = await helper.eth.createAccountWithBalance(donor);78 const ss58Format = helper.chain.getChainProperties().ss58Format;79 const {collectionId, collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'Sponsor', 'absolutely anything', 'ROC');8081 const collection = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);82 await collection.methods.setCollectionSponsor(sponsor).send();8384 let data = (await helper.nft.getData(collectionId))!;85 expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));8687 await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');8889 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);90 await sponsorCollection.methods.confirmCollectionSponsorship().send();9192 data = (await helper.nft.getData(collectionId))!;93 expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));94 });9596 itEth('Set limits', async ({helper}) => {97 const owner = await helper.eth.createAccountWithBalance(donor);98 const {collectionId, collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'Limits', 'absolutely anything', 'FLO');99 const limits = {100 accountTokenOwnershipLimit: 1000,101 sponsoredDataSize: 1024,102 sponsoredDataRateLimit: 30,103 tokenLimit: 1000000,104 sponsorTransferTimeout: 6,105 sponsorApproveTimeout: 6,106 ownerCanTransfer: false,107 ownerCanDestroy: false,108 transfersEnabled: false,109 };110111 const collection = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);112 await collection.methods['setCollectionLimit(string,uint32)']('accountTokenOwnershipLimit', limits.accountTokenOwnershipLimit).send();113 await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataSize', limits.sponsoredDataSize).send();114 await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataRateLimit', limits.sponsoredDataRateLimit).send();115 await collection.methods['setCollectionLimit(string,uint32)']('tokenLimit', limits.tokenLimit).send();116 await collection.methods['setCollectionLimit(string,uint32)']('sponsorTransferTimeout', limits.sponsorTransferTimeout).send();117 await collection.methods['setCollectionLimit(string,uint32)']('sponsorApproveTimeout', limits.sponsorApproveTimeout).send();118 await collection.methods['setCollectionLimit(string,bool)']('ownerCanTransfer', limits.ownerCanTransfer).send();119 await collection.methods['setCollectionLimit(string,bool)']('ownerCanDestroy', limits.ownerCanDestroy).send();120 await collection.methods['setCollectionLimit(string,bool)']('transfersEnabled', limits.transfersEnabled).send();121 122 const data = (await helper.nft.getData(collectionId))!;123 expect(data.raw.limits.accountTokenOwnershipLimit).to.be.eq(limits.accountTokenOwnershipLimit);124 expect(data.raw.limits.sponsoredDataSize).to.be.eq(limits.sponsoredDataSize);125 expect(data.raw.limits.sponsoredDataRateLimit.blocks).to.be.eq(limits.sponsoredDataRateLimit);126 expect(data.raw.limits.tokenLimit).to.be.eq(limits.tokenLimit);127 expect(data.raw.limits.sponsorTransferTimeout).to.be.eq(limits.sponsorTransferTimeout);128 expect(data.raw.limits.sponsorApproveTimeout).to.be.eq(limits.sponsorApproveTimeout);129 expect(data.raw.limits.ownerCanTransfer).to.be.eq(limits.ownerCanTransfer);130 expect(data.raw.limits.ownerCanDestroy).to.be.eq(limits.ownerCanDestroy);131 expect(data.raw.limits.transfersEnabled).to.be.eq(limits.transfersEnabled);132 });133134 itEth('Collection address exist', async ({helper}) => {135 const owner = await helper.eth.createAccountWithBalance(donor);136 const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';137 expect(await helper.ethNativeContract.collectionHelpers(collectionAddressForNonexistentCollection)138 .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())139 .to.be.false;140 141 const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'Exister', 'absolutely anything', 'EVC');142 expect(await helper.ethNativeContract.collectionHelpers(collectionAddress)143 .methods.isCollectionExist(collectionAddress).call())144 .to.be.true;145 });146});147148describe('(!negative tests!) Create NFT collection from EVM', () => {149 let donor: IKeyringPair;150151 before(async function() {152 await usingEthPlaygrounds(async (_helper, privateKey) => {153 donor = privateKey('//Alice');154 });155 });156157 itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {158 const owner = await helper.eth.createAccountWithBalance(donor);159 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);160 {161 const MAX_NAME_LENGTH = 64;162 const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);163 const description = 'A';164 const tokenPrefix = 'A';165166 await expect(collectionHelper.methods167 .createNonfungibleCollection(collectionName, description, tokenPrefix)168 .call({value: Number(2n * UNIQUE)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);169 170 }171 {172 const MAX_DESCRIPTION_LENGTH = 256;173 const collectionName = 'A';174 const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);175 const tokenPrefix = 'A';176 await expect(collectionHelper.methods177 .createNonfungibleCollection(collectionName, description, tokenPrefix)178 .call({value: Number(2n * UNIQUE)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);179 }180 {181 const MAX_TOKEN_PREFIX_LENGTH = 16;182 const collectionName = 'A';183 const description = 'A';184 const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);185 await expect(collectionHelper.methods186 .createNonfungibleCollection(collectionName, description, tokenPrefix)187 .call({value: Number(2n * UNIQUE)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);188 }189 });190 191 itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {192 const owner = await helper.eth.createAccountWithBalance(donor);193 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);194 await expect(collectionHelper.methods195 .createNonfungibleCollection('Peasantry', 'absolutely anything', 'CVE')196 .call({value: Number(1n * UNIQUE)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');197 });198199 itEth('(!negative test!) Check owner', async ({helper}) => {200 const owner = await helper.eth.createAccountWithBalance(donor);201 const malfeasant = helper.eth.createAccount();202 const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'Transgressed', 'absolutely anything', 'COR');203 const malfeasantCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', malfeasant);204 const EXPECTED_ERROR = 'NoPermission';205 {206 const sponsor = await helper.eth.createAccountWithBalance(donor);207 await expect(malfeasantCollection.methods208 .setCollectionSponsor(sponsor)209 .call()).to.be.rejectedWith(EXPECTED_ERROR);210 211 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);212 await expect(sponsorCollection.methods213 .confirmCollectionSponsorship()214 .call()).to.be.rejectedWith('caller is not set as sponsor');215 }216 {217 await expect(malfeasantCollection.methods218 .setCollectionLimit('account_token_ownership_limit', '1000')219 .call()).to.be.rejectedWith(EXPECTED_ERROR);220 }221 });222223 itEth('(!negative test!) Set limits', async ({helper}) => {224 const owner = await helper.eth.createAccountWithBalance(donor);225 const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'Limits', 'absolutely anything', 'OLF');226 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);227 await expect(collectionEvm.methods228 .setCollectionLimit('badLimit', 'true')229 .call()).to.be.rejectedWith('unknown boolean limit "badLimit"');230 });231});