1234567891011121314151617import {evmToAddress} from '@polkadot/util-crypto';18import type {IKeyringPair} from '@polkadot/types/types';19import {expect, itEth, usingEthPlaygrounds} from '@unique/test-utils/eth/util.js';20import {CollectionLimitField} from '@unique/test-utils/eth/types.js';212223describe('Create NFT collection from EVM', () => {24 let donor: IKeyringPair;2526 before(async function () {27 await usingEthPlaygrounds(async (_helper, privateKey) => {28 donor = await privateKey({url: import.meta.url});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 = 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 = 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 = 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 = 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 = 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;137138 139 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', owner, true);140 const collectionOwner = await collectionEvm.methods.collectionOwner().call();141 expect(helper.address.restoreCrossAccountFromBigInt(BigInt(collectionOwner.sub))).to.eq(helper.address.ethToSubstrate(owner, true));142 });143});144145describe('(!negative tests!) Create NFT collection from EVM', () => {146 let donor: IKeyringPair;147 let nominal: bigint;148149 before(async function () {150 await usingEthPlaygrounds(async (helper, privateKey) => {151 donor = await privateKey({url: import.meta.url});152 nominal = helper.balance.getOneTokenNominal();153 });154 });155156 itEth('(!negative test!) Create collection (bad lengths)', async ({helper}) => {157 const owner = await helper.eth.createAccountWithBalance(donor);158 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);159 {160 const MAX_NAME_LENGTH = 64;161 const collectionName = 'A'.repeat(MAX_NAME_LENGTH + 1);162 const description = 'A';163 const tokenPrefix = 'A';164165 await expect(collectionHelper.methods166 .createNFTCollection(collectionName, description, tokenPrefix)167 .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);168169 }170 {171 const MAX_DESCRIPTION_LENGTH = 256;172 const collectionName = 'A';173 const description = 'A'.repeat(MAX_DESCRIPTION_LENGTH + 1);174 const tokenPrefix = 'A';175 await expect(collectionHelper.methods176 .createNFTCollection(collectionName, description, tokenPrefix)177 .call({value: Number(2n * nominal)})).to.be.rejectedWith('description is too long. Max length is ' + MAX_DESCRIPTION_LENGTH);178 }179 {180 const MAX_TOKEN_PREFIX_LENGTH = 16;181 const collectionName = 'A';182 const description = 'A';183 const tokenPrefix = 'A'.repeat(MAX_TOKEN_PREFIX_LENGTH + 1);184 await expect(collectionHelper.methods185 .createNFTCollection(collectionName, description, tokenPrefix)186 .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);187 }188 });189190 itEth('(!negative test!) Create collection (no funds)', async ({helper}) => {191 const owner = await helper.eth.createAccountWithBalance(donor);192 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);193 await expect(collectionHelper.methods194 .createNFTCollection('Peasantry', 'absolutely anything', 'CVE')195 .call({value: Number(1n * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');196 });197198 199 itEth('(!negative test!) [eth] Check owner', async ({helper}) => {200 const owner = await helper.eth.createAccountWithBalance(donor);201 const malfeasant = helper.eth.createAccount();202 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Transgressed', 'absolutely anything', 'COR');203 const malfeasantCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', malfeasant, true);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);210211 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor, true);212 await expect(sponsorCollection.methods213 .confirmCollectionSponsorship()214 .call()).to.be.rejectedWith('ConfirmSponsorshipFail');215 }216 {217 await expect(malfeasantCollection.methods218 .setCollectionLimit({field: CollectionLimitField.AccountTokenOwnership, value: {status: true, value: 1000}})219 .call()).to.be.rejectedWith(EXPECTED_ERROR);220 }221 });222223 itEth('(!negative test!) [cross] Check owner', async ({helper}) => {224 const owner = await helper.eth.createAccountWithBalance(donor);225 const malfeasant = helper.eth.createAccount();226 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Transgressed', 'absolutely anything', 'COR');227 const malfeasantCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', malfeasant);228 const EXPECTED_ERROR = 'NoPermission';229 {230 const sponsor = await helper.eth.createAccountWithBalance(donor);231 const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);232 await expect(malfeasantCollection.methods233 .setCollectionSponsorCross(sponsorCross)234 .call()).to.be.rejectedWith(EXPECTED_ERROR);235236 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);237 await expect(sponsorCollection.methods238 .confirmCollectionSponsorship()239 .call()).to.be.rejectedWith('ConfirmSponsorshipFail');240 }241 {242 await expect(malfeasantCollection.methods243 .setCollectionLimit({field: CollectionLimitField.AccountTokenOwnership, value: {status: true, value: 1000}})244 .call()).to.be.rejectedWith(EXPECTED_ERROR);245 }246 });247248 itEth('destroyCollection', async ({helper}) => {249 const owner = await helper.eth.createAccountWithBalance(donor);250 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'Limits', 'absolutely anything', 'OLF');251 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);252253254 const result = await collectionHelper.methods255 .destroyCollection(collectionAddress)256 .send({from: owner});257258 const events = helper.eth.normalizeEvents(result.events);259260 expect(events).to.be.deep.equal([261 {262 address: collectionHelper.options.address,263 event: 'CollectionDestroyed',264 args: {265 collectionId: collectionAddress,266 },267 },268 ]);269270 expect(await collectionHelper.methods271 .isCollectionExist(collectionAddress)272 .call()).to.be.false;273 expect(await helper.collection.getData(collectionId)).to.be.null;274 });275});