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 144 const token = await collection.mintToken(alice);145 await token.setProperties(alice, [{key: 'testKey', value: 'testValue'}]);146147 await collection.addAdmin(alice, {Ethereum: caller});148149 const address = helper.ethAddress.fromCollectionId(collection.collectionId);150 const contract = helper.ethNativeContract.collection(address, 'nft', caller);151152 await contract.methods.deleteProperty(token.tokenId, 'testKey').send({from: caller});153154 const result = await token.getProperties(['testKey']);155 expect(result.length).to.equal(0);156 });157158 itEth('Can be read', async({helper}) => {159 const caller = helper.eth.createAccount();160 const collection = await helper.nft.mintCollection(alice, {161 tokenPropertyPermissions: [{162 key: 'testKey',163 permission: {164 collectionAdmin: true,165 },166 }],167 });168 169 const token = await collection.mintToken(alice);170 await token.setProperties(alice, [{key: 'testKey', value: 'testValue'}]);171172 const address = helper.ethAddress.fromCollectionId(collection.collectionId);173 const contract = helper.ethNativeContract.collection(address, 'nft', caller);174175 const value = await contract.methods.property(token.tokenId, 'testKey').call();176 expect(value).to.equal(helper.getWeb3().utils.toHex('testValue'));177 });178});179180181type ElementOf<A> = A extends readonly (infer T)[] ? T : never;182function* cartesian<T extends Array<Array<any>>, R extends Array<any>>(internalRest: [...R], ...args: [...T]): Generator<[...R, ...{[K in keyof T]: ElementOf<T[K]>}]> {183 if(args.length === 0) {184 yield internalRest as any;185 return;186 }187 for(const value of args[0]) {188 yield* cartesian([...internalRest, value], ...args.slice(1)) as any;189 }190}