1234567891011121314151617import {evmToAddress} from '@polkadot/util-crypto';18import {IKeyringPair} from '@polkadot/types/types';19import {expect, itEth, usingEthPlaygrounds} from './util/playgrounds';202122describe('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 * helper.balance.getOneTokenNominal())});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;150 let nominal: bigint;151152 before(async function() {153 await usingEthPlaygrounds(async (helper, privateKey) => {154 donor = privateKey('//Alice');155 nominal = helper.balance.getOneTokenNominal();156 });157 });158159 itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {160 const owner = await helper.eth.createAccountWithBalance(donor);161 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);162 {163 const MAX_NAME_LENGTH = 64;164 const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);165 const description = 'A';166 const tokenPrefix = 'A';167168 await expect(collectionHelper.methods169 .createNonfungibleCollection(collectionName, description, tokenPrefix)170 .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);171 172 }173 {174 const MAX_DESCRIPTION_LENGTH = 256;175 const collectionName = 'A';176 const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);177 const tokenPrefix = 'A';178 await expect(collectionHelper.methods179 .createNonfungibleCollection(collectionName, description, tokenPrefix)180 .call({value: Number(2n * nominal)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);181 }182 {183 const MAX_TOKEN_PREFIX_LENGTH = 16;184 const collectionName = 'A';185 const description = 'A';186 const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);187 await expect(collectionHelper.methods188 .createNonfungibleCollection(collectionName, description, tokenPrefix)189 .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);190 }191 });192 193 itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {194 const owner = await helper.eth.createAccountWithBalance(donor);195 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);196 await expect(collectionHelper.methods197 .createNonfungibleCollection('Peasantry', 'absolutely anything', 'CVE')198 .call({value: Number(1n * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');199 });200201 itEth('(!negative test!) Check owner', async ({helper}) => {202 const owner = await helper.eth.createAccountWithBalance(donor);203 const malfeasant = helper.eth.createAccount();204 const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'Transgressed', 'absolutely anything', 'COR');205 const malfeasantCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', malfeasant);206 const EXPECTED_ERROR = 'NoPermission';207 {208 const sponsor = await helper.eth.createAccountWithBalance(donor);209 await expect(malfeasantCollection.methods210 .setCollectionSponsor(sponsor)211 .call()).to.be.rejectedWith(EXPECTED_ERROR);212 213 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);214 await expect(sponsorCollection.methods215 .confirmCollectionSponsorship()216 .call()).to.be.rejectedWith('caller is not set as sponsor');217 }218 {219 await expect(malfeasantCollection.methods220 .setCollectionLimit('account_token_ownership_limit', '1000')221 .call()).to.be.rejectedWith(EXPECTED_ERROR);222 }223 });224225 itEth('(!negative test!) Set limits', async ({helper}) => {226 const owner = await helper.eth.createAccountWithBalance(donor);227 const {collectionAddress} = await helper.eth.createNonfungibleCollection(owner, 'Limits', 'absolutely anything', 'OLF');228 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);229 await expect(collectionEvm.methods230 .setCollectionLimit('badLimit', 'true')231 .call()).to.be.rejectedWith('unknown boolean limit "badLimit"');232 });233});