1234567891011121314151617import {evmToAddress} from '@polkadot/util-crypto';18import {IKeyringPair} from '@polkadot/types/types';19import {expect, itEth, usingEthPlaygrounds} from './util';20import {CollectionLimits} from './util/playgrounds/types';212223describe('Create NFT collection from EVM', () => {24 let donor: IKeyringPair;2526 before(async function () {27 await usingEthPlaygrounds(async (_helper, privateKey) => {28 donor = await privateKey({filename: __filename});29 });30 });3132 itEth('Create collection with properties & get desctription', async ({helper}) => {33 const owner = await helper.eth.createAccountWithBalance(donor);3435 const name = 'CollectionEVM';36 const description = 'Some description';37 const prefix = 'token prefix';38 const baseUri = 'BaseURI';3940 const {collectionId, collectionAddress, events} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, name, description, prefix, baseUri);41 const contract = await helper.ethNativeContract.collection(collectionAddress, 'nft');4243 expect(events).to.be.deep.equal([44 {45 address: '0x6C4E9fE1AE37a41E93CEE429e8E1881aBdcbb54F',46 event: 'CollectionCreated',47 args: {48 owner: owner,49 collectionId: collectionAddress,50 },51 },52 ]);5354 const collection = helper.nft.getCollectionObject(collectionId);55 const data = (await collection.getData())!;5657 expect(data.name).to.be.eq(name);58 expect(data.description).to.be.eq(description);59 expect(data.raw.tokenPrefix).to.be.eq(prefix);60 expect(data.raw.mode).to.be.eq('NFT');6162 expect(await contract.methods.description().call()).to.deep.equal(description);6364 const options = await collection.getOptions();65 expect(options.tokenPropertyPermissions).to.be.deep.equal([66 {67 key: 'URI',68 permission: {mutable: true, collectionAdmin: true, tokenOwner: false},69 },70 {71 key: 'URISuffix',72 permission: {mutable: true, collectionAdmin: true, tokenOwner: false},73 },74 ]);75 });7677 78 itEth('[eth] Set sponsorship', async ({helper}) => {79 const owner = await helper.eth.createAccountWithBalance(donor);80 const sponsor = await helper.eth.createAccountWithBalance(donor);81 const ss58Format = helper.chain.getChainProperties().ss58Format;82 const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(owner, 'Sponsor', 'absolutely anything', 'ROC');8384 const collection = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);85 await collection.methods.setCollectionSponsor(sponsor).send();8687 let data = (await helper.nft.getData(collectionId))!;88 expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));8990 await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');9192 const sponsorCollection = await helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor, true);93 await sponsorCollection.methods.confirmCollectionSponsorship().send();9495 data = (await helper.nft.getData(collectionId))!;96 expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));97 });9899 itEth('[cross] Set sponsorship & get description', async ({helper}) => {100 const owner = await helper.eth.createAccountWithBalance(donor);101 const sponsor = await helper.eth.createAccountWithBalance(donor);102 const ss58Format = helper.chain.getChainProperties().ss58Format;103 const description = 'absolutely anything';104 const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(owner, 'Sponsor', description, 'ROC');105106 const collection = await helper.ethNativeContract.collection(collectionAddress, 'nft', owner);107 const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);108 await collection.methods.setCollectionSponsorCross(sponsorCross).send();109110 let data = (await helper.nft.getData(collectionId))!;111 expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));112113 await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');114115 const sponsorCollection = await helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);116 await sponsorCollection.methods.confirmCollectionSponsorship().send();117118 data = (await helper.nft.getData(collectionId))!;119 expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));120121 expect(await sponsorCollection.methods.description().call()).to.deep.equal(description);122 });123124 itEth('Collection address exist', async ({helper}) => {125 const owner = await helper.eth.createAccountWithBalance(donor);126 const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';127 const collectionHelpers = await helper.ethNativeContract.collectionHelpers(owner);128129 expect(await collectionHelpers130 .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())131 .to.be.false;132133 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Exister', 'absolutely anything', 'EVC');134 expect(await collectionHelpers135 .methods.isCollectionExist(collectionAddress).call())136 .to.be.true;137 });138});139140describe('(!negative tests!) Create NFT collection from EVM', () => {141 let donor: IKeyringPair;142 let nominal: bigint;143144 before(async function () {145 await usingEthPlaygrounds(async (helper, privateKey) => {146 donor = await privateKey({filename: __filename});147 nominal = helper.balance.getOneTokenNominal();148 });149 });150151 itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {152 const owner = await helper.eth.createAccountWithBalance(donor);153 const collectionHelper = await helper.ethNativeContract.collectionHelpers(owner);154 {155 const MAX_NAME_LENGTH = 64;156 const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);157 const description = 'A';158 const tokenPrefix = 'A';159160 await expect(collectionHelper.methods161 .createNFTCollection(collectionName, description, tokenPrefix)162 .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);163164 }165 {166 const MAX_DESCRIPTION_LENGTH = 256;167 const collectionName = 'A';168 const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);169 const tokenPrefix = 'A';170 await expect(collectionHelper.methods171 .createNFTCollection(collectionName, description, tokenPrefix)172 .call({value: Number(2n * nominal)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);173 }174 {175 const MAX_TOKEN_PREFIX_LENGTH = 16;176 const collectionName = 'A';177 const description = 'A';178 const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);179 await expect(collectionHelper.methods180 .createNFTCollection(collectionName, description, tokenPrefix)181 .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);182 }183 });184185 itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {186 const owner = await helper.eth.createAccountWithBalance(donor);187 const collectionHelper = await helper.ethNativeContract.collectionHelpers(owner);188 await expect(collectionHelper.methods189 .createNFTCollection('Peasantry', 'absolutely anything', 'CVE')190 .call({value: Number(1n * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');191 });192193 194 itEth('(!negative test!) [eth] Check owner', async ({helper}) => {195 const owner = await helper.eth.createAccountWithBalance(donor);196 const malfeasant = helper.eth.createAccount();197 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Transgressed', 'absolutely anything', 'COR');198 const malfeasantCollection = await helper.ethNativeContract.collection(collectionAddress, 'nft', malfeasant, true);199 const EXPECTED_ERROR = 'NoPermission';200 {201 const sponsor = await helper.eth.createAccountWithBalance(donor);202 await expect(malfeasantCollection.methods203 .setCollectionSponsor(sponsor)204 .call()).to.be.rejectedWith(EXPECTED_ERROR);205206 const sponsorCollection = await helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor, true);207 await expect(sponsorCollection.methods208 .confirmCollectionSponsorship()209 .call()).to.be.rejectedWith('ConfirmSponsorshipFail');210 }211 {212 await expect(malfeasantCollection.methods213 .setCollectionLimit(CollectionLimits.AccountTokenOwnership, true, 1000)214 .call()).to.be.rejectedWith(EXPECTED_ERROR);215 }216 });217218 itEth('(!negative test!) [cross] Check owner', async ({helper}) => {219 const owner = await helper.eth.createAccountWithBalance(donor);220 const malfeasant = helper.eth.createAccount();221 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Transgressed', 'absolutely anything', 'COR');222 const malfeasantCollection = await helper.ethNativeContract.collection(collectionAddress, 'nft', malfeasant);223 const EXPECTED_ERROR = 'NoPermission';224 {225 const sponsor = await helper.eth.createAccountWithBalance(donor);226 const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);227 await expect(malfeasantCollection.methods228 .setCollectionSponsorCross(sponsorCross)229 .call()).to.be.rejectedWith(EXPECTED_ERROR);230231 const sponsorCollection = await helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);232 await expect(sponsorCollection.methods233 .confirmCollectionSponsorship()234 .call()).to.be.rejectedWith('ConfirmSponsorshipFail');235 }236 {237 await expect(malfeasantCollection.methods238 .setCollectionLimit(CollectionLimits.AccountTokenOwnership, true, 1000)239 .call()).to.be.rejectedWith(EXPECTED_ERROR);240 }241 });242243 itEth('destroyCollection', async ({helper}) => {244 const owner = await helper.eth.createAccountWithBalance(donor);245 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'Limits', 'absolutely anything', 'OLF');246 const collectionHelper = await helper.ethNativeContract.collectionHelpers(owner);247248249 const result = await collectionHelper.methods250 .destroyCollection(collectionAddress)251 .send({from: owner});252253 const events = helper.eth.normalizeEvents(result.events);254255 expect(events).to.be.deep.equal([256 {257 address: collectionHelper.options.address,258 event: 'CollectionDestroyed',259 args: {260 collectionId: collectionAddress,261 },262 },263 ]);264265 expect(await collectionHelper.methods266 .isCollectionExist(collectionAddress)267 .call()).to.be.false;268 expect(await helper.collection.getData(collectionId)).to.be.null;269 });270});