1234567891011121314151617import {itEth, usingEthPlaygrounds, expect} from './util/index.js';18import {Pallets} from '../util/index.js';19import type {IProperty, ITokenPropertyPermission} from '@unique/playgrounds/types.js';20import type {IKeyringPair} from '@polkadot/types/types';2122describe('EVM collection properties', () => {23 let donor: IKeyringPair;24 let alice: IKeyringPair;2526 before(async function() {27 await usingEthPlaygrounds(async (_helper, privateKey) => {28 donor = await privateKey({url: import.meta.url});29 [alice] = await _helper.arrange.createAccounts([50n], donor);30 });31 });3233 34 [35 {method: 'setCollectionProperties', mode: 'nft' as const, methodParams: [[{key: 'testKey1', value: Buffer.from('testValue1')}, {key: 'testKey2', value: Buffer.from('testValue2')}]], expectedProps: [{key: 'testKey1', value: 'testValue1'}, {key: 'testKey2', value: 'testValue2'}]},36 {method: 'setCollectionProperties', mode: 'rft' as const, methodParams: [[{key: 'testKey1', value: Buffer.from('testValue1')}, {key: 'testKey2', value: Buffer.from('testValue2')}]], expectedProps: [{key: 'testKey1', value: 'testValue1'}, {key: 'testKey2', value: 'testValue2'}]},37 {method: 'setCollectionProperties', mode: 'ft' as const, methodParams: [[{key: 'testKey1', value: Buffer.from('testValue1')}, {key: 'testKey2', value: Buffer.from('testValue2')}]], expectedProps: [{key: 'testKey1', value: 'testValue1'}, {key: 'testKey2', value: 'testValue2'}]},38 {method: 'setCollectionProperty', mode: 'nft' as const, methodParams: ['testKey', Buffer.from('testValue')], expectedProps: [{key: 'testKey', value: 'testValue'}]},39 ].map(testCase =>40 itEth.ifWithPallets(`Collection properties can be set: ${testCase.method}() for ${testCase.mode}`, testCase.mode === 'rft' ? [Pallets.ReFungible] : [], async({helper}) => {41 const caller = await helper.eth.createAccountWithBalance(donor);42 const collection = await helper[testCase.mode].mintCollection(alice, {name: 'name', description: 'test', tokenPrefix: 'test', properties: []});43 await collection.addAdmin(alice, {Ethereum: caller});4445 const address = helper.ethAddress.fromCollectionId(collection.collectionId);46 const collectionEvm = await helper.ethNativeContract.collection(address, 'nft', caller, testCase.method === 'setCollectionProperty');4748 49 expect(await collectionEvm.methods.collectionProperties([]).call()).to.be.like([]);50 expect(await collectionEvm.methods.collectionProperties(['NonExistingKey']).call()).to.be.like([]);5152 await collectionEvm.methods[testCase.method](...testCase.methodParams).send({from: caller});5354 const raw = (await collection.getData())?.raw;55 expect(raw.properties).to.deep.equal(testCase.expectedProps);5657 58 expect(await collectionEvm.methods.collectionProperties([]).call()).to.be.like(testCase.expectedProps.map(prop => helper.ethProperty.property(prop.key, prop.value)));59 }));6061 itEth('Cannot set invalid properties', async({helper}) => {62 const caller = await helper.eth.createAccountWithBalance(donor);63 const collection = await helper.nft.mintCollection(alice, {name: 'name', description: 'test', tokenPrefix: 'test', properties: []});64 await collection.addAdmin(alice, {Ethereum: caller});6566 const address = helper.ethAddress.fromCollectionId(collection.collectionId);67 const contract = await helper.ethNativeContract.collection(address, 'nft', caller);6869 await expect(contract.methods.setCollectionProperties([{key: '', value: Buffer.from('val1')}]).send({from: caller})).to.be.rejected;70 await expect(contract.methods.setCollectionProperties([{key: 'déjà vu', value: Buffer.from('hmm...')}]).send({from: caller})).to.be.rejected;71 await expect(contract.methods.setCollectionProperties([{key: 'a'.repeat(257), value: Buffer.from('val3')}]).send({from: caller})).to.be.rejected;72 73 const raw = (await collection.getData())?.raw;74 expect(raw.properties).to.deep.equal([]);75 });767778 79 [80 {method: 'deleteCollectionProperties', mode: 'nft' as const, methodParams: [['testKey1', 'testKey2']], expectedProps: [{key: 'testKey3', value: 'testValue3'}]},81 {method: 'deleteCollectionProperties', mode: 'rft' as const, methodParams: [['testKey1', 'testKey2']], expectedProps: [{key: 'testKey3', value: 'testValue3'}]},82 {method: 'deleteCollectionProperties', mode: 'ft' as const, methodParams: [['testKey1', 'testKey2']], expectedProps: [{key: 'testKey3', value: 'testValue3'}]},83 {method: 'deleteCollectionProperty', mode: 'nft' as const, methodParams: ['testKey1'], expectedProps: [{key: 'testKey2', value: 'testValue2'}, {key: 'testKey3', value: 'testValue3'}]},84 ].map(testCase =>85 itEth.ifWithPallets(`Collection properties can be deleted: ${testCase.method}() for ${testCase.mode}`, testCase.mode === 'rft' ? [Pallets.ReFungible] : [], async({helper}) => {86 const properties = [87 {key: 'testKey1', value: 'testValue1'},88 {key: 'testKey2', value: 'testValue2'},89 {key: 'testKey3', value: 'testValue3'}];90 const caller = await helper.eth.createAccountWithBalance(donor);91 const collection = await helper[testCase.mode].mintCollection(alice, {name: 'name', description: 'test', tokenPrefix: 'test', properties});9293 await collection.addAdmin(alice, {Ethereum: caller});9495 const address = helper.ethAddress.fromCollectionId(collection.collectionId);96 const contract = await helper.ethNativeContract.collection(address, 'nft', caller, testCase.method === 'deleteCollectionProperty');9798 await contract.methods[testCase.method](...testCase.methodParams).send({from: caller});99100 const raw = (await collection.getData())?.raw;101102 expect(raw.properties.length).to.equal(testCase.expectedProps.length);103 expect(raw.properties).to.deep.equal(testCase.expectedProps);104 }));105106 [107 {method: 'deleteCollectionProperties', methodParams: [['testKey2']]},108 {method: 'deleteCollectionProperty', methodParams: ['testKey2']},109 ].map(testCase =>110 itEth(`cannot ${testCase.method}() of non-owned collections`, async ({helper}) => {111 const properties = [112 {key: 'testKey1', value: 'testValue1'},113 {key: 'testKey2', value: 'testValue2'},114 ];115 const caller = await helper.eth.createAccountWithBalance(donor);116 const collection = await helper.nft.mintCollection(alice, {name: 'name', description: 'test', tokenPrefix: 'test', properties});117118 const address = helper.ethAddress.fromCollectionId(collection.collectionId);119 const collectionEvm = await helper.ethNativeContract.collection(address, 'nft', caller, testCase.method === 'deleteCollectionProperty');120121 await expect(collectionEvm.methods[testCase.method](...testCase.methodParams).send({from: caller})).to.be.rejected;122 expect(await collection.getProperties()).to.deep.eq(properties);123 }));124125 itEth('Can be read', async({helper}) => {126 const caller = helper.eth.createAccount();127 const collection = await helper.nft.mintCollection(alice, {name: 'name', description: 'test', tokenPrefix: 'test', properties: [{key: 'testKey', value: 'testValue'}]});128129 const address = helper.ethAddress.fromCollectionId(collection.collectionId);130 const contract = await helper.ethNativeContract.collection(address, 'nft', caller);131132 const value = await contract.methods.collectionProperty('testKey').call();133 expect(value).to.equal(helper.getWeb3().utils.toHex('testValue'));134 });135});136137describe('Supports ERC721Metadata', () => {138 let donor: IKeyringPair;139140 before(async function() {141 await usingEthPlaygrounds(async (_helper, privateKey) => {142 donor = await privateKey({url: import.meta.url});143 });144 });145146 [147 {case: 'nft' as const},148 {case: 'rft' as const, requiredPallets: [Pallets.ReFungible]},149 ].map(testCase =>150 itEth.ifWithPallets(`ERC721Metadata property can be set for ${testCase.case} collection`, testCase.requiredPallets || [], async ({helper}) => {151 const caller = await helper.eth.createAccountWithBalance(donor);152 const bruh = await helper.eth.createAccountWithBalance(donor);153154 const BASE_URI = 'base/';155 const SUFFIX = 'suffix1';156 const URI = 'uri1';157158 const collectionHelpers = await helper.ethNativeContract.collectionHelpers(caller);159 const creatorMethod = testCase.case === 'rft' ? 'createRFTCollection' : 'createNFTCollection';160161 const {collectionId, collectionAddress} = await helper.eth[creatorMethod](caller, 'n', 'd', 'p');162 const bruhCross = helper.ethCrossAccount.fromAddress(bruh);163164 const contract = await helper.ethNativeContract.collectionById(collectionId, testCase.case, caller);165 await contract.methods.addCollectionAdminCross(bruhCross).send(); 166167 const collection1 = helper.nft.getCollectionObject(collectionId);168 const data1 = await collection1.getData();169 expect(data1?.raw.flags.erc721metadata).to.be.false;170 expect(await contract.methods.supportsInterface('0x5b5e139f').call()).to.be.false;171172 await collectionHelpers.methods.makeCollectionERC721MetadataCompatible(collectionAddress, BASE_URI)173 .send({from: bruh});174175 expect(await contract.methods.supportsInterface('0x5b5e139f').call()).to.be.true;176177 const collection2 = helper.nft.getCollectionObject(collectionId);178 const data2 = await collection2.getData();179 expect(data2?.raw.flags.erc721metadata).to.be.true;180181 const propertyPermissions = data2?.raw.tokenPropertyPermissions;182 expect(propertyPermissions?.length).to.equal(2);183184 expect(propertyPermissions.find((tpp: ITokenPropertyPermission) => tpp.key === 'URI' && tpp.permission.mutable && tpp.permission.collectionAdmin && !tpp.permission.tokenOwner)).to.be.not.null;185186 expect(propertyPermissions.find((tpp: ITokenPropertyPermission) => tpp.key === 'URISuffix' && tpp.permission.mutable && tpp.permission.collectionAdmin && !tpp.permission.tokenOwner)).to.be.not.null;187188 expect(data2?.raw.properties?.find((property: IProperty) => property.key === 'baseURI' && property.value === BASE_URI)).to.be.not.null;189190 const token1Result = await contract.methods.mint(bruh).send();191 const tokenId1 = token1Result.events.Transfer.returnValues.tokenId;192193 expect(await contract.methods.tokenURI(tokenId1).call()).to.equal(BASE_URI);194195 await contract.methods.setProperties(tokenId1, [{key: 'URISuffix', value: Buffer.from(SUFFIX)}]).send();196 expect(await contract.methods.tokenURI(tokenId1).call()).to.equal(BASE_URI + SUFFIX);197198 await contract.methods.setProperties(tokenId1, [{key: 'URI', value: Buffer.from(URI)}]).send();199 expect(await contract.methods.tokenURI(tokenId1).call()).to.equal(URI);200201 await contract.methods.deleteProperties(tokenId1, ['URI']).send();202 expect(await contract.methods.tokenURI(tokenId1).call()).to.equal(BASE_URI + SUFFIX);203204 const token2Result = await contract.methods.mintWithTokenURI(bruh, URI).send();205 const tokenId2 = token2Result.events.Transfer.returnValues.tokenId;206207 expect(await contract.methods.tokenURI(tokenId2).call()).to.equal(URI);208209 await contract.methods.deleteProperties(tokenId2, ['URI']).send();210 expect(await contract.methods.tokenURI(tokenId2).call()).to.equal(BASE_URI);211212 await contract.methods.setProperties(tokenId2, [{key: 'URISuffix', value: Buffer.from(SUFFIX)}]).send();213 expect(await contract.methods.tokenURI(tokenId2).call()).to.equal(BASE_URI + SUFFIX);214 }));215});