git.delta.rocks / unique-network / refs/commits / 591c0ace12a9

difftreelog

source

tests/src/eth/collectionProperties.test.ts10.5 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {itEth, usingEthPlaygrounds, expect, EthUniqueHelper} from './util';18import {Pallets} from '../util';19import {IProperty, ITokenPropertyPermission} from '../util/playgrounds/types';20import {IKeyringPair} from '@polkadot/types/types';21import {TCollectionMode} from '../util/playgrounds/types';2223describe('EVM collection properties', () => {24  let donor: IKeyringPair;25  let alice: IKeyringPair;2627  before(async function() {28    await usingEthPlaygrounds(async (_helper, privateKey) => {29      donor = await privateKey({filename: __filename});30      [alice] = await _helper.arrange.createAccounts([10n], donor);31    });32  });3334  itEth('Can be set', async({helper}) => {35    const caller = await helper.eth.createAccountWithBalance(donor);36    const collection = await helper.nft.mintCollection(alice, {name: 'name', description: 'test', tokenPrefix: 'test', properties: []});37    await collection.addAdmin(alice, {Ethereum: caller});3839    const address = helper.ethAddress.fromCollectionId(collection.collectionId);40    const contract = helper.ethNativeContract.collection(address, 'nft', caller);4142    await contract.methods.setCollectionProperty('testKey', Buffer.from('testValue')).send({from: caller});4344    const raw = (await collection.getData())?.raw;4546    expect(raw.properties[0].value).to.equal('testValue');47  });4849  itEth('Can be deleted', async({helper}) => {50    const caller = await helper.eth.createAccountWithBalance(donor);51    const collection = await helper.nft.mintCollection(alice, {name: 'name', description: 'test', tokenPrefix: 'test', properties: [{key: 'testKey', value: 'testValue'}]});5253    await collection.addAdmin(alice, {Ethereum: caller});5455    const address = helper.ethAddress.fromCollectionId(collection.collectionId);56    const contract = helper.ethNativeContract.collection(address, 'nft', caller);5758    await contract.methods.deleteCollectionProperty('testKey').send({from: caller});5960    const raw = (await collection.getData())?.raw;6162    expect(raw.properties.length).to.equal(0);63  });6465  itEth('Can be read', async({helper}) => {66    const caller = helper.eth.createAccount();67    const collection = await helper.nft.mintCollection(alice, {name: 'name', description: 'test', tokenPrefix: 'test', properties: [{key: 'testKey', value: 'testValue'}]});6869    const address = helper.ethAddress.fromCollectionId(collection.collectionId);70    const contract = helper.ethNativeContract.collection(address, 'nft', caller);7172    const value = await contract.methods.collectionProperty('testKey').call();73    expect(value).to.equal(helper.getWeb3().utils.toHex('testValue'));74  });75});7677describe('Supports ERC721Metadata', () => {78  let donor: IKeyringPair;7980  before(async function() {81    await usingEthPlaygrounds(async (_helper, privateKey) => {82      donor = await privateKey({filename: __filename});83    });84  });8586  const checkERC721Metadata = async (helper: EthUniqueHelper, mode: 'nft' | 'rft') => {87    const caller = await helper.eth.createAccountWithBalance(donor);88    const bruh = await helper.eth.createAccountWithBalance(donor);8990    const BASE_URI = 'base/';91    const SUFFIX = 'suffix1';92    const URI = 'uri1';9394    const collectionHelpers = helper.ethNativeContract.collectionHelpers(caller);95    const creatorMethod = mode === 'rft' ? 'createRFTCollection' : 'createNFTCollection';9697    const {collectionId, collectionAddress} = await helper.eth[creatorMethod](caller, 'n', 'd', 'p');9899    const contract = helper.ethNativeContract.collectionById(collectionId, mode, caller);100    await contract.methods.addCollectionAdmin(bruh).send(); // to check that admin will work too101102    const collection1 = helper.nft.getCollectionObject(collectionId);103    const data1 = await collection1.getData();104    expect(data1?.raw.flags.erc721metadata).to.be.false;105    expect(await contract.methods.supportsInterface('0x5b5e139f').call()).to.be.false;106107    await collectionHelpers.methods.makeCollectionERC721MetadataCompatible(collectionAddress, BASE_URI)108      .send({from: bruh});109110    expect(await contract.methods.supportsInterface('0x5b5e139f').call()).to.be.true;111112    const collection2 = helper.nft.getCollectionObject(collectionId);113    const data2 = await collection2.getData();114    expect(data2?.raw.flags.erc721metadata).to.be.true;115116    const propertyPermissions = data2?.raw.tokenPropertyPermissions;117    expect(propertyPermissions?.length).to.equal(2);118119    expect(propertyPermissions.find((tpp: ITokenPropertyPermission) => {120      return tpp.key === 'URI' && tpp.permission.mutable && tpp.permission.collectionAdmin && !tpp.permission.tokenOwner;121    })).to.be.not.null;122123    expect(propertyPermissions.find((tpp: ITokenPropertyPermission) => {124      return tpp.key === 'URISuffix' && tpp.permission.mutable && tpp.permission.collectionAdmin && !tpp.permission.tokenOwner;125    })).to.be.not.null;126127    expect(data2?.raw.properties?.find((property: IProperty) => {128      return property.key === 'baseURI' && property.value === BASE_URI;129    })).to.be.not.null;130131    const token1Result = await contract.methods.mint(bruh).send();132    const tokenId1 = token1Result.events.Transfer.returnValues.tokenId;133134    expect(await contract.methods.tokenURI(tokenId1).call()).to.equal(BASE_URI);135136    await contract.methods.setProperty(tokenId1, 'URISuffix', Buffer.from(SUFFIX)).send();137    expect(await contract.methods.tokenURI(tokenId1).call()).to.equal(BASE_URI + SUFFIX);138139    await contract.methods.setProperty(tokenId1, 'URI', Buffer.from(URI)).send();140    expect(await contract.methods.tokenURI(tokenId1).call()).to.equal(URI);141142    await contract.methods.deleteProperty(tokenId1, 'URI').send();143    expect(await contract.methods.tokenURI(tokenId1).call()).to.equal(BASE_URI + SUFFIX);144145    const token2Result = await contract.methods.mintWithTokenURI(bruh, URI).send();146    const tokenId2 = token2Result.events.Transfer.returnValues.tokenId;147148    expect(await contract.methods.tokenURI(tokenId2).call()).to.equal(URI);149150    await contract.methods.deleteProperty(tokenId2, 'URI').send();151    expect(await contract.methods.tokenURI(tokenId2).call()).to.equal(BASE_URI);152153    await contract.methods.setProperty(tokenId2, 'URISuffix', Buffer.from(SUFFIX)).send();154    expect(await contract.methods.tokenURI(tokenId2).call()).to.equal(BASE_URI + SUFFIX);155  };156157  itEth('ERC721Metadata property can be set for NFT collection', async({helper}) => {158    await checkERC721Metadata(helper, 'nft');159  });160161  itEth.ifWithPallets('ERC721Metadata property can be set for RFT collection', [Pallets.ReFungible], async({helper}) => {162    await checkERC721Metadata(helper, 'rft');163  });164});165166describe('EVM collection property', () => {167  let donor: IKeyringPair;168169  before(async function() {170    await usingEthPlaygrounds(async (_helper, privateKey) => {171      donor = await privateKey({filename: __filename});172    });173  });174175  async function testSetReadProperties(helper: EthUniqueHelper, mode: TCollectionMode) {176    const collection = await helper[mode].mintCollection(donor, {name: 'A', description: 'B', tokenPrefix: 'C'});177178    const sender = await helper.eth.createAccountWithBalance(donor, 100n);179    await collection.addAdmin(donor, {Ethereum: sender});180181    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);182    const contract = helper.ethNativeContract.collection(collectionAddress, mode, sender);183184    const keys = ['key0', 'key1'];185186    const writeProperties = [187      helper.ethProperty.property(keys[0], 'value0'),188      helper.ethProperty.property(keys[1], 'value1'),189    ];190191    await contract.methods.setCollectionProperties(writeProperties).send();192    const readProperties = await contract.methods.collectionProperties([keys[0], keys[1]]).call();193    expect(readProperties).to.be.like(writeProperties);194  }195196  itEth('Set/read properties ft', async ({helper}) => {197    await testSetReadProperties(helper, 'ft');198  });199  itEth.ifWithPallets('Set/read properties rft', [Pallets.ReFungible], async ({helper}) => {200    await testSetReadProperties(helper, 'rft');201  });202  itEth('Set/read properties nft', async ({helper}) => {203    await testSetReadProperties(helper, 'nft');204  });205206  async function testDeleteProperties(helper: EthUniqueHelper, mode: TCollectionMode) {207    const collection = await helper[mode].mintCollection(donor, {name: 'A', description: 'B', tokenPrefix: 'C'});208209    const sender = await helper.eth.createAccountWithBalance(donor, 100n);210    await collection.addAdmin(donor, {Ethereum: sender});211212    const collectionAddress = helper.ethAddress.fromCollectionId(collection.collectionId);213    const contract = helper.ethNativeContract.collection(collectionAddress, mode, sender);214215    const keys = ['key0', 'key1', 'key2', 'key3'];216217    {218      const writeProperties = [219        helper.ethProperty.property(keys[0], 'value0'),220        helper.ethProperty.property(keys[1], 'value1'),221        helper.ethProperty.property(keys[2], 'value2'),222        helper.ethProperty.property(keys[3], 'value3'),223      ];224225      await contract.methods.setCollectionProperties(writeProperties).send();226      const readProperties = await contract.methods.collectionProperties([keys[0], keys[1], keys[2], keys[3]]).call();227      expect(readProperties).to.be.like(writeProperties);228    }229230    {231      const expectProperties = [232        helper.ethProperty.property(keys[0], 'value0'),233        helper.ethProperty.property(keys[1], 'value1'),234      ];235236      await contract.methods.deleteCollectionProperties([keys[2], keys[3]]).send();237      const readProperties = await contract.methods.collectionProperties([]).call();238      expect(readProperties).to.be.like(expectProperties);239    }240  }241  242  itEth('Delete properties ft', async ({helper}) => {243    await testDeleteProperties(helper, 'ft');244  });245  itEth.ifWithPallets('Delete properties rft', [Pallets.ReFungible], async ({helper}) => {246    await testDeleteProperties(helper, 'rft');247  });248  itEth('Delete properties nft', async ({helper}) => {249    await testDeleteProperties(helper, 'nft');250  });251    252});