1234567891011121314151617import {evmToAddress} from '@polkadot/util-crypto';18import {IKeyringPair} from '@polkadot/types/types';19import {expect, itEth, usingEthPlaygrounds} from './util';20import {CollectionLimitField} from './util/playgrounds/types';21import {COLLECTION_HELPER} from '../util';222324describe('Create NFT collection from EVM', () => {25 let donor: IKeyringPair;2627 before(async function () {28 await usingEthPlaygrounds(async (_helper, privateKey) => {29 donor = await privateKey({url: import.meta.url});30 });31 });3233 itEth('Create collection with properties & get desctription', async ({helper}) => {34 const owner = await helper.eth.createAccountWithBalance(donor);3536 const name = 'CollectionEVM';37 const description = 'Some description';38 const prefix = 'token prefix';39 const baseUri = 'BaseURI';4041 const {collectionId, collectionAddress, events} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, name, description, prefix, baseUri);42 const contract = helper.ethNativeContract.collection(collectionAddress, 'nft');4344 expect(events).to.be.deep.equal([45 {46 address: '0x6C4E9fE1AE37a41E93CEE429e8E1881aBdcbb54F',47 event: 'CollectionCreated',48 args: {49 owner: owner,50 collectionId: collectionAddress,51 },52 },53 ]);5455 const collection = helper.nft.getCollectionObject(collectionId);56 const data = (await collection.getData())!;5758 expect(data.name).to.be.eq(name);59 expect(data.description).to.be.eq(description);60 expect(data.raw.tokenPrefix).to.be.eq(prefix);61 expect(data.raw.mode).to.be.eq('NFT');6263 expect(await contract.methods.description().call()).to.deep.equal(description);6465 const options = await collection.getOptions();66 expect(options.tokenPropertyPermissions).to.be.deep.equal([67 {68 key: 'URI',69 permission: {mutable: true, collectionAdmin: true, tokenOwner: false},70 },71 {72 key: 'URISuffix',73 permission: {mutable: true, collectionAdmin: true, tokenOwner: false},74 },75 ]);76 });7778 79 itEth('[eth] Set sponsorship', async ({helper}) => {80 const owner = await helper.eth.createAccountWithBalance(donor);81 const sponsor = await helper.eth.createAccountWithBalance(donor);82 const ss58Format = helper.chain.getChainProperties().ss58Format;83 const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(owner, 'Sponsor', 'absolutely anything', 'ROC');8485 const collection = helper.ethNativeContract.collection(collectionAddress, 'nft', owner, true);86 await collection.methods.setCollectionSponsor(sponsor).send();8788 let data = (await helper.nft.getData(collectionId))!;89 expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));9091 await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');9293 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor, true);94 await sponsorCollection.methods.confirmCollectionSponsorship().send();9596 data = (await helper.nft.getData(collectionId))!;97 expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));98 });99100 itEth('[cross] Set sponsorship & get description', async ({helper}) => {101 const owner = await helper.eth.createAccountWithBalance(donor);102 const sponsor = await helper.eth.createAccountWithBalance(donor);103 const ss58Format = helper.chain.getChainProperties().ss58Format;104 const description = 'absolutely anything';105 const {collectionId, collectionAddress} = await helper.eth.createNFTCollection(owner, 'Sponsor', description, 'ROC');106107 const collection = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);108 const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);109 await collection.methods.setCollectionSponsorCross(sponsorCross).send();110111 let data = (await helper.nft.getData(collectionId))!;112 expect(data.raw.sponsorship.Unconfirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));113114 await expect(collection.methods.confirmCollectionSponsorship().call()).to.be.rejectedWith('ConfirmSponsorshipFail');115116 const sponsorCollection = await helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);117 await sponsorCollection.methods.confirmCollectionSponsorship().send();118119 data = (await helper.nft.getData(collectionId))!;120 expect(data.raw.sponsorship.Confirmed).to.be.equal(evmToAddress(sponsor, Number(ss58Format)));121122 expect(await sponsorCollection.methods.description().call()).to.deep.equal(description);123 });124125 itEth('Collection address exist', async ({helper}) => {126 const owner = await helper.eth.createAccountWithBalance(donor);127 const collectionAddressForNonexistentCollection = '0x17C4E6453CC49AAAAEACA894E6D9683E00112233';128 const collectionHelpers = helper.ethNativeContract.collectionHelpers(owner);129130 expect(await collectionHelpers131 .methods.isCollectionExist(collectionAddressForNonexistentCollection).call())132 .to.be.false;133134 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Exister', 'absolutely anything', 'EVC');135 expect(await collectionHelpers136 .methods.isCollectionExist(collectionAddress).call())137 .to.be.true;138139 140 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'ft', owner, true);141 const collectionOwner = await collectionEvm.methods.collectionOwner().call();142 expect(helper.address.restoreCrossAccountFromBigInt(BigInt(collectionOwner.sub))).to.eq(helper.address.ethToSubstrate(owner, true));143 });144});145146describe('(!negative tests!) Create NFT collection from EVM', () => {147 let donor: IKeyringPair;148 let nominal: bigint;149150 before(async function () {151 await usingEthPlaygrounds(async (helper, privateKey) => {152 donor = await privateKey({url: import.meta.url});153 nominal = helper.balance.getOneTokenNominal();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 .createNFTCollection(collectionName, description, tokenPrefix)168 .call({value: Number(2n * nominal)})).to.be.rejectedWith('name is too long. Max length is ' + MAX_NAME_LENGTH);169170 }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 .createNFTCollection(collectionName, description, tokenPrefix)178 .call({value: Number(2n * nominal)})).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 .createNFTCollection(collectionName, description, tokenPrefix)187 .call({value: Number(2n * nominal)})).to.be.rejectedWith('token_prefix is too long. Max length is ' + MAX_TOKEN_PREFIX_LENGTH);188 }189 });190191 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 .createNFTCollection('Peasantry', 'absolutely anything', 'CVE')196 .call({value: Number(1n * nominal)})).to.be.rejectedWith('Sent amount not equals to collection creation price (2000000000000000000)');197 });198199 200 itEth('(!negative test!) [eth] Check owner', async ({helper}) => {201 const owner = await helper.eth.createAccountWithBalance(donor);202 const malfeasant = helper.eth.createAccount();203 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Transgressed', 'absolutely anything', 'COR');204 const malfeasantCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', malfeasant, true);205 const EXPECTED_ERROR = 'NoPermission';206 {207 const sponsor = await helper.eth.createAccountWithBalance(donor);208 await expect(malfeasantCollection.methods209 .setCollectionSponsor(sponsor)210 .call()).to.be.rejectedWith(EXPECTED_ERROR);211212 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor, true);213 await expect(sponsorCollection.methods214 .confirmCollectionSponsorship()215 .call()).to.be.rejectedWith('ConfirmSponsorshipFail');216 }217 {218 await expect(malfeasantCollection.methods219 .setCollectionLimit({field: CollectionLimitField.AccountTokenOwnership, value: {status: true, value: 1000}})220 .call()).to.be.rejectedWith(EXPECTED_ERROR);221 }222 });223224 itEth('(!negative test!) [cross] Check owner', async ({helper}) => {225 const owner = await helper.eth.createAccountWithBalance(donor);226 const malfeasant = helper.eth.createAccount();227 const {collectionAddress} = await helper.eth.createNFTCollection(owner, 'Transgressed', 'absolutely anything', 'COR');228 const malfeasantCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', malfeasant);229 const EXPECTED_ERROR = 'NoPermission';230 {231 const sponsor = await helper.eth.createAccountWithBalance(donor);232 const sponsorCross = helper.ethCrossAccount.fromAddress(sponsor);233 await expect(malfeasantCollection.methods234 .setCollectionSponsorCross(sponsorCross)235 .call()).to.be.rejectedWith(EXPECTED_ERROR);236237 const sponsorCollection = helper.ethNativeContract.collection(collectionAddress, 'nft', sponsor);238 await expect(sponsorCollection.methods239 .confirmCollectionSponsorship()240 .call()).to.be.rejectedWith('ConfirmSponsorshipFail');241 }242 {243 await expect(malfeasantCollection.methods244 .setCollectionLimit({field: CollectionLimitField.AccountTokenOwnership, value: {status: true, value: 1000}})245 .call()).to.be.rejectedWith(EXPECTED_ERROR);246 }247 });248249 itEth('destroyCollection', async ({helper}) => {250 const owner = await helper.eth.createAccountWithBalance(donor);251 const {collectionAddress, collectionId} = await helper.eth.createNFTCollection(owner, 'Limits', 'absolutely anything', 'OLF');252 const collectionHelper = helper.ethNativeContract.collectionHelpers(owner);253254255 const result = await collectionHelper.methods256 .destroyCollection(collectionAddress)257 .send({from: owner});258259 const events = helper.eth.normalizeEvents(result.events);260261 expect(events).to.be.deep.equal([262 {263 address: collectionHelper.options.address,264 event: 'CollectionDestroyed',265 args: {266 collectionId: collectionAddress,267 },268 },269 ]);270271 expect(await collectionHelper.methods272 .isCollectionExist(collectionAddress)273 .call()).to.be.false;274 expect(await helper.collection.getData(collectionId)).to.be.null;275 });276});