git.delta.rocks / unique-network / refs/commits / 9fbaca1b2bad

difftreelog

source

tests/src/eth/tokenProperties.test.ts4.8 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} from './util';18import {IKeyringPair} from '@polkadot/types/types';1920describe('EVM token properties', () => {21  let donor: IKeyringPair;22  let alice: IKeyringPair;2324  before(async function() {25    await usingEthPlaygrounds(async (helper, privateKey) => {26      donor = await privateKey({filename: __filename});27      [alice] = await helper.arrange.createAccounts([100n], donor);28    });29  });3031  itEth('Can be reconfigured', async({helper}) => {32    const caller = await helper.eth.createAccountWithBalance(donor);33    for(const [mutable,collectionAdmin, tokenOwner] of cartesian([], [false, true], [false, true], [false, true])) {34      const collection = await helper.nft.mintCollection(alice);35      await collection.addAdmin(alice, {Ethereum: caller});36      37      const address = helper.ethAddress.fromCollectionId(collection.collectionId);38      const contract = helper.ethNativeContract.collection(address, 'nft', caller);39  40      await contract.methods.setTokenPropertyPermission('testKey', mutable, collectionAdmin, tokenOwner).send({from: caller});41  42      expect(await collection.getPropertyPermissions()).to.be.deep.equal([{43        key: 'testKey',44        permission: {mutable, collectionAdmin, tokenOwner},45      }]);46    }47  });4849  itEth('Can be set', async({helper}) => {50    const caller = await helper.eth.createAccountWithBalance(donor);51    const collection = await helper.nft.mintCollection(alice, {52      tokenPropertyPermissions: [{53        key: 'testKey',54        permission: {55          collectionAdmin: true,56        },57      }],58    });59    const token = await collection.mintToken(alice);6061    await collection.addAdmin(alice, {Ethereum: caller});6263    const address = helper.ethAddress.fromCollectionId(collection.collectionId);64    const contract = helper.ethNativeContract.collection(address, 'nft', caller);6566    await contract.methods.setProperty(token.tokenId, 'testKey', Buffer.from('testValue')).send({from: caller});6768    const [{value}] = await token.getProperties(['testKey']);69    expect(value).to.equal('testValue');70  });7172  itEth('Can be deleted', async({helper}) => {73    const caller = await helper.eth.createAccountWithBalance(donor);74    const collection = await helper.nft.mintCollection(alice, {75      tokenPropertyPermissions: [{76        key: 'testKey',77        permission: {78          mutable: true,79          collectionAdmin: true,80        },81      }],82    });83    84    const token = await collection.mintToken(alice);85    await token.setProperties(alice, [{key: 'testKey', value: 'testValue'}]);8687    await collection.addAdmin(alice, {Ethereum: caller});8889    const address = helper.ethAddress.fromCollectionId(collection.collectionId);90    const contract = helper.ethNativeContract.collection(address, 'nft', caller);9192    await contract.methods.deleteProperty(token.tokenId, 'testKey').send({from: caller});9394    const result = await token.getProperties(['testKey']);95    expect(result.length).to.equal(0);96  });9798  itEth('Can be read', async({helper}) => {99    const caller = helper.eth.createAccount();100    const collection = await helper.nft.mintCollection(alice, {101      tokenPropertyPermissions: [{102        key: 'testKey',103        permission: {104          collectionAdmin: true,105        },106      }],107    });108  109    const token = await collection.mintToken(alice);110    await token.setProperties(alice, [{key: 'testKey', value: 'testValue'}]);111112    const address = helper.ethAddress.fromCollectionId(collection.collectionId);113    const contract = helper.ethNativeContract.collection(address, 'nft', caller);114115    const value = await contract.methods.property(token.tokenId, 'testKey').call();116    expect(value).to.equal(helper.getWeb3().utils.toHex('testValue'));117  });118});119120121type ElementOf<A> = A extends readonly (infer T)[] ? T : never;122function* cartesian<T extends Array<Array<any>>, R extends Array<any>>(internalRest: [...R], ...args: [...T]): Generator<[...R, ...{[K in keyof T]: ElementOf<T[K]>}]> {123  if(args.length === 0) {124    yield internalRest as any;125    return;126  }127  for(const value of args[0]) {128    yield* cartesian([...internalRest, value], ...args.slice(1)) as any;129  }130}