1234567891011121314151617import {evmToAddress} from '@polkadot/util-crypto';18import {IKeyringPair} from '@polkadot/types/types';19import {expect, itEth, usingEthPlaygrounds} from './util';202122describe('Create NFT collection from EVM', () => {23 let donor: IKeyringPair;2425 before(async function() {26 await usingEthPlaygrounds(async (_helper, privateKey) => {27 donor = await privateKey({filename: __filename});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 const {collectionId} = await helper.eth.createNFTCollection(owner, name, description, prefix);39 const data = (await helper.rft.getData(collectionId))!;40 const collection = helper.nft.getCollectionObject(collectionId);41 42 expect(data.name).to.be.eq(name);43 expect(data.description).to.be.eq(description);44 expect(data.raw.tokenPrefix).to.be.eq(prefix);45 expect(data.raw.mode).to.be.eq('NFT');4647 const options = await collection.getOptions();4849 expect(options.tokenPropertyPermissions).to.be.empty;50 });5152 itEth('Create collection with properties', async ({helper}) => {53 const owner = await helper.eth.createAccountWithBalance(donor);5455 const name = 'CollectionEVM';56 const description = 'Some description';57 const prefix = 'token prefix';58 const baseUri = 'BaseURI';5960 const {collectionId} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, name, description, prefix, baseUri);6162 const collection = helper.nft.getCollectionObject(collectionId);63 const data = (await collection.getData())!;64 65 expect(data.name).to.be.eq(name);66 expect(data.description).to.be.eq(description);67 expect(data.raw.tokenPrefix).to.be.eq(prefix);68 expect(data.raw.mode).to.be.eq('NFT');6970 const options = await collection.getOptions();71 expect(options.tokenPropertyPermissions).to.be.deep.equal([72 {73 key: 'URI',74 permission: {mutable: true, collectionAdmin: true, tokenOwner: false},75 },76 {77 key: 'URISuffix',78 permission: {mutable: true, collectionAdmin: true, tokenOwner: false},79 },80 ]);81 });8283 84 itEth.skip('Check collection address exist', async ({helper}) => {85 const owner = await helper.eth.createAccountWithBalance(donor);8687 const expectedCollectionId = +(await helper.callRpc('api.rpc.unique.collectionStats')).created + 1;88 const expectedCollectionAddress = helper.ethAddress.fromCollectionId(expectedCollectionId);89 const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);9091 expect(await collectionHelpers.methods92 .isCollectionExist(expectedCollectionAddress)93 .call()).to.be.false;9495 await collectionHelpers.methods96 .createNFTCollection('A', 'A', 'A')97 .send({value: Number(2n * helper.balance.getOneTokenNominal())});98 99 expect(await collectionHelpers.methods100 .isCollectionExist(expectedCollectionAddress)101 .call()).to.be.true;102 });103 104 itEth('Set sponsorship', async ({helper}) => {105 const owner = await helper.eth.createAccountWithBalance(donor);106 const sponsor = await helper.eth.createAccountWithBalance(donor);107 const ss58Format = helper.chain.getChainProperties().ss58Format;108 const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(owner, 'Sponsor', 'absolutely anything', 'ROC');109110 const collection = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);111 await collection.methods.setCollectionSponsor(sponsor).send();112113 let data = (await helper.nft.getData(collectionId))!;114 expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));115116 await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('caller is not set as sponsor');117118 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);119 await sponsorCollection.methods.confirmCollectionSponsorship().send();120121 data = (await helper.nft.getData(collectionId))!;122 expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));123 });124125 itEth('Set limits', async ({helper}) => {126 const owner = await helper.eth.createAccountWithBalance(donor);127 const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(owner, 'Limits', 'absolutely anything', 'FLO');128 const limits = {129 accountTokenOwnershipLimit: 1000,130 sponsoredDataSize: 1024,131 sponsoredDataRateLimit: 30,132 tokenLimit: 1000000,133 sponsorTransferTimeout: 6,134 sponsorApproveTimeout: 6,135 ownerCanTransfer: false,136 ownerCanDestroy: false,137 transfersEnabled: false,138 };139140 const collection = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);141 await collection.methods['setCollectionLimit(string,uint32)']('accountTokenOwnershipLimit', limits.accountTokenOwnershipLimit).send();142 await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataSize', limits.sponsoredDataSize).send();143 await collection.methods['setCollectionLimit(string,uint32)']('sponsoredDataRateLimit', limits.sponsoredDataRateLimit).send();144 await collection.methods['setCollectionLimit(string,uint32)']('tokenLimit', limits.tokenLimit).send();145 await collection.methods['setCollectionLimit(string,uint32)']('sponsorTransferTimeout', limits.sponsorTransferTimeout).send();146 await collection.methods['setCollectionLimit(string,uint32)']('sponsorApproveTimeout', limits.sponsorApproveTimeout).send();147 await collection.methods['setCollectionLimit(string,bool)']('ownerCanTransfer', limits.ownerCanTransfer).send();148 await collection.methods['setCollectionLimit(string,bool)']('ownerCanDestroy', limits.ownerCanDestroy).send();149 await collection.methods['setCollectionLimit(string,bool)']('transfersEnabled', limits.transfersEnabled).send();150 151 const data = (await helper.nft.getData(collectionId))!;152 expect(data.raw.limits.accountTokenOwnershipLimit).to.be.eq(limits.accountTokenOwnershipLimit);153 expect(data.raw.limits.sponsoredDataSize).to.be.eq(limits.sponsoredDataSize);154 expect(data.raw.limits.sponsoredDataRateLimit.blocks).to.be.eq(limits.sponsoredDataRateLimit);155 expect(data.raw.limits.tokenLimit).to.be.eq(limits.tokenLimit);156 expect(data.raw.limits.sponsorTransferTimeout).to.be.eq(limits.sponsorTransferTimeout);157 expect(data.raw.limits.sponsorApproveTimeout).to.be.eq(limits.sponsorApproveTimeout);158 expect(data.raw.limits.ownerCanTransfer).to.be.eq(limits.ownerCanTransfer);159 expect(data.raw.limits.ownerCanDestroy).to.be.eq(limits.ownerCanDestroy);160 expect(data.raw.limits.transfersEnabled).to.be.eq(limits.transfersEnabled);161 });162163 itEth('Collection address exist', async ({helper}) => {164 const owner = await helper.eth.createAccountWithBalance(donor);165 const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';166 expect(await helper.ethNativeContract.collectionHelpers(collectionAddressForNonexistentCollection)167 .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())168 .to.be.false;169 170 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Exister', 'absolutely anything', 'EVC');171 expect(await helper.ethNativeContract.collectionHelpers(collectionAddress)172 .methods.isCollectionExist(collectionAddress).call())173 .to.be.true;174 });175});176177describe('(!negative tests!) Create NFT collection from EVM', () => {178 let donor: IKeyringPair;179 let nominal: bigint;180181 before(async function() {182 await usingEthPlaygrounds(async (helper, privateKey) => {183 donor = await privateKey({filename: __filename});184 nominal = helper.balance.getOneTokenNominal();185 });186 });187188 itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {189 const owner = await helper.eth.createAccountWithBalance(donor);190 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);191 {192 const MAX_NAME_LENGTH = 64;193 const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);194 const description = 'A';195 const tokenPrefix = 'A';196197 await expect(collectionHelper.methods198 .createNFTCollection(collectionName, description, tokenPrefix)199 .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);200 201 }202 {203 const MAX_DESCRIPTION_LENGTH = 256;204 const collectionName = 'A';205 const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);206 const tokenPrefix = 'A';207 await expect(collectionHelper.methods208 .createNFTCollection(collectionName, description, tokenPrefix)209 .call({value: Number(2n * nominal)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);210 }211 {212 const MAX_TOKEN_PREFIX_LENGTH = 16;213 const collectionName = 'A';214 const description = 'A';215 const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);216 await expect(collectionHelper.methods217 .createNFTCollection(collectionName, description, tokenPrefix)218 .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);219 }220 });221 222 itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {223 const owner = await helper.eth.createAccountWithBalance(donor);224 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);225 await expect(collectionHelper.methods226 .createNFTCollection('Peasantry', 'absolutely anything', 'CVE')227 .call({value: Number(1n * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');228 });229230 itEth('(!negative test!) Check owner', async ({helper}) => {231 const owner = await helper.eth.createAccountWithBalance(donor);232 const malfeasant = helper.eth.createAccount();233 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Transgressed', 'absolutely anything', 'COR');234 const malfeasantCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', malfeasant);235 const EXPECTED_ERROR = 'NoPermission';236 {237 const sponsor = await helper.eth.createAccountWithBalance(donor);238 await expect(malfeasantCollection.methods239 .setCollectionSponsor(sponsor)240 .call()).to.be.rejectedWith(EXPECTED_ERROR);241 242 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);243 await expect(sponsorCollection.methods244 .confirmCollectionSponsorship()245 .call()).to.be.rejectedWith('caller is not set as sponsor');246 }247 {248 await expect(malfeasantCollection.methods249 .setCollectionLimit('account_token_ownership_limit', '1000')250 .call()).to.be.rejectedWith(EXPECTED_ERROR);251 }252 });253254 itEth('(!negative test!) Set limits', async ({helper}) => {255 const owner = await helper.eth.createAccountWithBalance(donor);256 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Limits', 'absolutely anything', 'OLF');257 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);258 await expect(collectionEvm.methods259 .setCollectionLimit('badLimit', 'true')260 .call()).to.be.rejectedWith('unknown boolean limit "badLimit"');261 });262});