1234567891011121314151617import {itEth, usingEthPlaygrounds, expect} from './util';18import {IKeyringPair} from '@polkadot/types/types';19import {ITokenPropertyPermission} from '../util/playgrounds/types';20import {Pallets} from '../util';2122describe('EVM token properties', () => {23 let donor: IKeyringPair;24 let alice: IKeyringPair;2526 before(async function() {27 await usingEthPlaygrounds(async (helper, privateKey) => {28 donor = await privateKey({filename: __filename});29 [alice] = await helper.arrange.createAccounts([100n], donor);30 });31 });3233 itEth('Can be reconfigured', async({helper}) => {34 const caller = await helper.eth.createAccountWithBalance(donor);35 for(const [mutable,collectionAdmin, tokenOwner] of cartesian([], [false, true], [false, true], [false, true])) {36 const collection = await helper.nft.mintCollection(alice);37 await collection.addAdmin(alice, {Ethereum: caller});38 39 const address = helper.ethAddress.fromCollectionId(collection.collectionId);40 const contract = helper.ethNativeContract.collection(address, 'nft', caller);41 42 await contract.methods.setTokenPropertyPermission('testKey', mutable, collectionAdmin, tokenOwner).send({from: caller});43 44 expect(await collection.getPropertyPermissions()).to.be.deep.equal([{45 key: 'testKey',46 permission: {mutable, collectionAdmin, tokenOwner},47 }]);48 }49 });5051 itEth('Can be set', async({helper}) => {52 const caller = await helper.eth.createAccountWithBalance(donor);53 const collection = await helper.nft.mintCollection(alice, {54 tokenPropertyPermissions: [{55 key: 'testKey',56 permission: {57 collectionAdmin: true,58 },59 }],60 });61 const token = await collection.mintToken(alice);6263 await collection.addAdmin(alice, {Ethereum: caller});6465 const address = helper.ethAddress.fromCollectionId(collection.collectionId);66 const contract = helper.ethNativeContract.collection(address, 'nft', caller);6768 await contract.methods.setProperty(token.tokenId, 'testKey', Buffer.from('testValue')).send({from: caller});6970 const [{value}] = await token.getProperties(['testKey']);71 expect(value).to.equal('testValue');72 });73 74 itEth('Can be multiple set for NFT ', async({helper}) => {75 const caller = await helper.eth.createAccountWithBalance(donor);76 77 const properties = Array(5).fill(0).map((_, i) => { return {field_0: `key_${i}`, field_1: Buffer.from(`value_${i}`)}; });78 const permissions: ITokenPropertyPermission[] = properties.map(p => { return {key: p.field_0, permission: {tokenOwner: true,79 collectionAdmin: true,80 mutable: true}}; });81 82 const collection = await helper.nft.mintCollection(alice, {83 tokenPrefix: 'ethp',84 tokenPropertyPermissions: permissions,85 });86 87 const token = await collection.mintToken(alice);88 89 const valuesBefore = await token.getProperties(properties.map(p => p.field_0));90 expect(valuesBefore).to.be.deep.equal([]);91 92 await collection.addAdmin(alice, {Ethereum: caller});9394 const address = helper.ethAddress.fromCollectionId(collection.collectionId);95 const contract = helper.ethNativeContract.collection(address, 'nft', caller);9697 await contract.methods.setProperties(token.tokenId, properties).send({from: caller});9899 const values = await token.getProperties(properties.map(p => p.field_0));100 expect(values).to.be.deep.equal(properties.map(p => { return {key: p.field_0, value: p.field_1.toString()}; }));101 });102 103 itEth.ifWithPallets('Can be multiple set for RFT ', [Pallets.ReFungible], async({helper}) => {104 const caller = await helper.eth.createAccountWithBalance(donor);105 106 const properties = Array(5).fill(0).map((_, i) => { return {field_0: `key_${i}`, field_1: Buffer.from(`value_${i}`)}; });107 const permissions: ITokenPropertyPermission[] = properties.map(p => { return {key: p.field_0, permission: {tokenOwner: true,108 collectionAdmin: true,109 mutable: true}}; });110 111 const collection = await helper.rft.mintCollection(alice, {112 tokenPrefix: 'ethp',113 tokenPropertyPermissions: permissions,114 });115 116 const token = await collection.mintToken(alice);117 118 const valuesBefore = await token.getProperties(properties.map(p => p.field_0));119 expect(valuesBefore).to.be.deep.equal([]);120 121 await collection.addAdmin(alice, {Ethereum: caller});122123 const address = helper.ethAddress.fromCollectionId(collection.collectionId);124 const contract = helper.ethNativeContract.collection(address, 'rft', caller);125126 await contract.methods.setProperties(token.tokenId, properties).send({from: caller});127128 const values = await token.getProperties(properties.map(p => p.field_0));129 expect(values).to.be.deep.equal(properties.map(p => { return {key: p.field_0, value: p.field_1.toString()}; }));130 });131132 itEth('Can be deleted', async({helper}) => {133 const caller = await helper.eth.createAccountWithBalance(donor);134 const collection = await helper.nft.mintCollection(alice, {135 tokenPropertyPermissions: [{136 key: 'testKey',137 permission: {138 mutable: true,139 collectionAdmin: true,140 },141 },142 {143 key: 'testKey_1',144 permission: {145 mutable: true,146 collectionAdmin: true,147 },148 }],149 });150 151 const token = await collection.mintToken(alice);152 await token.setProperties(alice, [{key: 'testKey', value: 'testValue'}, {key: 'testKey_1', value: 'testValue_1'}]);153154 await collection.addAdmin(alice, {Ethereum: caller});155156 const address = helper.ethAddress.fromCollectionId(collection.collectionId);157 const contract = helper.ethNativeContract.collection(address, 'nft', caller);158159 await contract.methods.deleteProperties(token.tokenId, ['testKey', 'testKey_1']).send({from: caller});160161 const result = await token.getProperties(['testKey']);162 expect(result.length).to.equal(0);163 });164165 itEth('Can be read', async({helper}) => {166 const caller = helper.eth.createAccount();167 const collection = await helper.nft.mintCollection(alice, {168 tokenPropertyPermissions: [{169 key: 'testKey',170 permission: {171 collectionAdmin: true,172 },173 }],174 });175 176 const token = await collection.mintToken(alice);177 await token.setProperties(alice, [{key: 'testKey', value: 'testValue'}]);178179 const address = helper.ethAddress.fromCollectionId(collection.collectionId);180 const contract = helper.ethNativeContract.collection(address, 'nft', caller);181182 const value = await contract.methods.property(token.tokenId, 'testKey').call();183 expect(value).to.equal(helper.getWeb3().utils.toHex('testValue'));184 });185});186187188type ElementOf<A> = A extends readonly (infer T)[] ? T : never;189function* cartesian<T extends Array<Array<any>>, R extends Array<any>>(internalRest: [...R], ...args: [...T]): Generator<[...R, ...{[K in keyof T]: ElementOf<T[K]>}]> {190 if(args.length === 0) {191 yield internalRest as any;192 return;193 }194 for(const value of args[0]) {195 yield* cartesian([...internalRest, value], ...args.slice(1)) as any;196 }197}