1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {Contract} from 'web3-eth-contract';19import {itEth, usingEthPlaygrounds, expect} from './util';20import {ITokenPropertyPermission} from '../util/playgrounds/types';21import {Pallets} from '../util';22import {UniqueNFTCollection, UniqueNFToken, UniqueRFTCollection} from '../util/playgrounds/unique';23import {TokenPermissionField} from './util/playgrounds/types';2425describe('EVM token properties', () => {26 let donor: IKeyringPair;27 let alice: IKeyringPair;2829 before(async function() {30 await usingEthPlaygrounds(async (helper, privateKey) => {31 donor = await privateKey({url: import.meta.url});32 [alice] = await helper.arrange.createAccounts([1000n], donor);33 });34 });3536 [37 {mode: 'nft' as const, requiredPallets: []},38 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},39 ].map(testCase =>40 itEth.ifWithPallets(`[${testCase.mode}] Can set all possible token property permissions`, testCase.requiredPallets, async({helper}) => {41 const owner = await helper.eth.createAccountWithBalance(donor);42 const caller = await helper.ethCrossAccount.createAccountWithBalance(donor);43 for(const [mutable,collectionAdmin, tokenOwner] of cartesian([], [false, true], [false, true], [false, true])) {44 const {collectionId, collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');45 const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);46 await collection.methods.addCollectionAdminCross(caller).send({from: owner});4748 await collection.methods.setTokenPropertyPermissions([49 ['testKey', [50 [TokenPermissionField.Mutable, mutable],51 [TokenPermissionField.TokenOwner, tokenOwner],52 [TokenPermissionField.CollectionAdmin, collectionAdmin]],53 ],54 ]).send({from: caller.eth});5556 expect(await helper[testCase.mode].getPropertyPermissions(collectionId)).to.be.deep.equal([{57 key: 'testKey',58 permission: {mutable, collectionAdmin, tokenOwner},59 }]);6061 expect(await collection.methods.tokenPropertyPermissions().call({from: caller.eth})).to.be.like([62 ['testKey', [63 [TokenPermissionField.Mutable.toString(), mutable],64 [TokenPermissionField.TokenOwner.toString(), tokenOwner],65 [TokenPermissionField.CollectionAdmin.toString(), collectionAdmin]],66 ],67 ]);68 }69 }));7071 [72 {mode: 'nft' as const, requiredPallets: []},73 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},74 ].map(testCase =>75 itEth.ifWithPallets(`[${testCase.mode}] Can set multiple token property permissions as owner`, testCase.requiredPallets, async({helper}) => {76 const owner = await helper.eth.createAccountWithBalance(donor);7778 const {collectionId, collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');79 const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);8081 await collection.methods.setTokenPropertyPermissions([82 ['testKey_0', [83 [TokenPermissionField.Mutable, true],84 [TokenPermissionField.TokenOwner, true],85 [TokenPermissionField.CollectionAdmin, true]],86 ],87 ['testKey_1', [88 [TokenPermissionField.Mutable, true],89 [TokenPermissionField.TokenOwner, false],90 [TokenPermissionField.CollectionAdmin, true]],91 ],92 ['testKey_2', [93 [TokenPermissionField.Mutable, false],94 [TokenPermissionField.TokenOwner, true],95 [TokenPermissionField.CollectionAdmin, false]],96 ],97 ]).send({from: owner});9899 expect(await helper[testCase.mode].getPropertyPermissions(collectionId)).to.be.deep.equal([100 {101 key: 'testKey_0',102 permission: {mutable: true, tokenOwner: true, collectionAdmin: true},103 },104 {105 key: 'testKey_1',106 permission: {mutable: true, tokenOwner: false, collectionAdmin: true},107 },108 {109 key: 'testKey_2',110 permission: {mutable: false, tokenOwner: true, collectionAdmin: false},111 },112 ]);113114 expect(await collection.methods.tokenPropertyPermissions().call({from: owner})).to.be.like([115 ['testKey_0', [116 [TokenPermissionField.Mutable.toString(), true],117 [TokenPermissionField.TokenOwner.toString(), true],118 [TokenPermissionField.CollectionAdmin.toString(), true]],119 ],120 ['testKey_1', [121 [TokenPermissionField.Mutable.toString(), true],122 [TokenPermissionField.TokenOwner.toString(), false],123 [TokenPermissionField.CollectionAdmin.toString(), true]],124 ],125 ['testKey_2', [126 [TokenPermissionField.Mutable.toString(), false],127 [TokenPermissionField.TokenOwner.toString(), true],128 [TokenPermissionField.CollectionAdmin.toString(), false]],129 ],130 ]);131 }));132133 [134 {mode: 'nft' as const, requiredPallets: []},135 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},136 ].map(testCase =>137 itEth.ifWithPallets(`[${testCase.mode}] Can set multiple token property permissions as admin`, testCase.requiredPallets, async({helper}) => {138 const owner = await helper.eth.createAccountWithBalance(donor);139 const caller = await helper.ethCrossAccount.createAccountWithBalance(donor);140141 const {collectionId, collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');142 const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);143 await collection.methods.addCollectionAdminCross(caller).send({from: owner});144145 await collection.methods.setTokenPropertyPermissions([146 ['testKey_0', [147 [TokenPermissionField.Mutable, true],148 [TokenPermissionField.TokenOwner, true],149 [TokenPermissionField.CollectionAdmin, true]],150 ],151 ['testKey_1', [152 [TokenPermissionField.Mutable, true],153 [TokenPermissionField.TokenOwner, false],154 [TokenPermissionField.CollectionAdmin, true]],155 ],156 ['testKey_2', [157 [TokenPermissionField.Mutable, false],158 [TokenPermissionField.TokenOwner, true],159 [TokenPermissionField.CollectionAdmin, false]],160 ],161 ]).send({from: caller.eth});162163 expect(await helper[testCase.mode].getPropertyPermissions(collectionId)).to.be.deep.equal([164 {165 key: 'testKey_0',166 permission: {mutable: true, tokenOwner: true, collectionAdmin: true},167 },168 {169 key: 'testKey_1',170 permission: {mutable: true, tokenOwner: false, collectionAdmin: true},171 },172 {173 key: 'testKey_2',174 permission: {mutable: false, tokenOwner: true, collectionAdmin: false},175 },176 ]);177178 expect(await collection.methods.tokenPropertyPermissions().call({from: caller.eth})).to.be.like([179 ['testKey_0', [180 [TokenPermissionField.Mutable.toString(), true],181 [TokenPermissionField.TokenOwner.toString(), true],182 [TokenPermissionField.CollectionAdmin.toString(), true]],183 ],184 ['testKey_1', [185 [TokenPermissionField.Mutable.toString(), true],186 [TokenPermissionField.TokenOwner.toString(), false],187 [TokenPermissionField.CollectionAdmin.toString(), true]],188 ],189 ['testKey_2', [190 [TokenPermissionField.Mutable.toString(), false],191 [TokenPermissionField.TokenOwner.toString(), true],192 [TokenPermissionField.CollectionAdmin.toString(), false]],193 ],194 ]);195196 }));197198 [199 {200 method: 'setProperties',201 methodParams: [[{key: 'testKey1', value: Buffer.from('testValue1')}, {key: 'testKey2', value: Buffer.from('testValue2')}]],202 expectedProps: [{key: 'testKey1', value: 'testValue1'}, {key: 'testKey2', value: 'testValue2'}],203 },204 {205 method: 'setProperty' ,206 methodParams: ['testKey1', Buffer.from('testValue1')],207 expectedProps: [{key: 'testKey1', value: 'testValue1'}],208 },209 ].map(testCase =>210 itEth(`[${testCase.method}] Can be set`, async({helper}) => {211 const caller = await helper.eth.createAccountWithBalance(donor);212 const collection = await helper.nft.mintCollection(alice, {213 tokenPropertyPermissions: [{214 key: 'testKey1',215 permission: {216 collectionAdmin: true,217 },218 }, {219 key: 'testKey2',220 permission: {221 collectionAdmin: true,222 },223 }],224 });225226 await collection.addAdmin(alice, {Ethereum: caller});227 const token = await collection.mintToken(alice);228229 const collectionEvm = await helper.ethNativeContract.collectionById(collection.collectionId, 'nft', caller, testCase.method === 'setProperty');230231 await collectionEvm.methods[testCase.method](token.tokenId, ...testCase.methodParams).send({from: caller});232233 const properties = await token.getProperties();234 expect(properties).to.deep.equal(testCase.expectedProps);235 }));236237 [238 {mode: 'nft' as const, requiredPallets: []},239 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},240 ].map(testCase =>241 itEth.ifWithPallets(`Can be multiple set/read for ${testCase.mode}`, testCase.requiredPallets, async({helper}) => {242 const caller = await helper.eth.createAccountWithBalance(donor);243244 const properties = Array(5).fill(0).map((_, i) => ({key: `key_${i}`, value: Buffer.from(`value_${i}`)}));245 const permissions: ITokenPropertyPermission[] = properties.map(p => ({key: p.key, permission: {tokenOwner: true,246 collectionAdmin: true,247 mutable: true}}));248249 const collection = await helper[testCase.mode].mintCollection(alice, {250 tokenPrefix: 'ethp',251 tokenPropertyPermissions: permissions,252 }) as UniqueNFTCollection | UniqueRFTCollection;253254 const token = await collection.mintToken(alice);255256 const valuesBefore = await token.getProperties(properties.map(p => p.key));257 expect(valuesBefore).to.be.deep.equal([]);258259260 await collection.addAdmin(alice, {Ethereum: caller});261262 const address = helper.ethAddress.fromCollectionId(collection.collectionId);263 const contract = await helper.ethNativeContract.collection(address, testCase.mode, caller);264265 expect(await contract.methods.properties(token.tokenId, []).call()).to.be.deep.equal([]);266267 await contract.methods.setProperties(token.tokenId, properties).send({from: caller});268269 const values = await token.getProperties(properties.map(p => p.key));270 expect(values).to.be.deep.equal(properties.map(p => ({key: p.key, value: p.value.toString()})));271272 expect(await contract.methods.properties(token.tokenId, []).call()).to.be.like(properties273 .map(p => helper.ethProperty.property(p.key, p.value.toString())));274275 expect(await contract.methods.properties(token.tokenId, [properties[0].key]).call())276 .to.be.like([helper.ethProperty.property(properties[0].key, properties[0].value.toString())]);277 }));278279 [280 {mode: 'nft' as const, requiredPallets: []},281 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},282 ].map(testCase =>283 itEth.ifWithPallets(`Can be deleted for ${testCase.mode}`, testCase.requiredPallets, async({helper}) => {284 const caller = await helper.eth.createAccountWithBalance(donor);285 const collection = await helper[testCase.mode].mintCollection(alice, {286 tokenPropertyPermissions: [{287 key: 'testKey',288 permission: {289 mutable: true,290 collectionAdmin: true,291 },292 },293 {294 key: 'testKey_1',295 permission: {296 mutable: true,297 collectionAdmin: true,298 },299 }],300 });301302 const token = await collection.mintToken(alice);303 await token.setProperties(alice, [{key: 'testKey', value: 'testValue'}, {key: 'testKey_1', value: 'testValue_1'}]);304 expect(await token.getProperties()).to.has.length(2);305306 await collection.addAdmin(alice, {Ethereum: caller});307308 const address = helper.ethAddress.fromCollectionId(collection.collectionId);309 const contract = await helper.ethNativeContract.collection(address, testCase.mode, caller);310311 await contract.methods.deleteProperties(token.tokenId, ['testKey', 'testKey_1']).send({from: caller});312313 const result = await token.getProperties(['testKey', 'testKey_1']);314 expect(result.length).to.equal(0);315 }));316317 itEth('Can be read', async({helper}) => {318 319 const caller = await helper.eth.createAccountWithBalance(alice);320 const collection = await helper.nft.mintCollection(alice, {321 tokenPropertyPermissions: [{322 key: 'testKey',323 permission: {324 collectionAdmin: true,325 },326 }],327 });328329 const token = await collection.mintToken(alice);330 await token.setProperties(alice, [{key: 'testKey', value: 'testValue'}]);331332 const address = helper.ethAddress.fromCollectionId(collection.collectionId);333 const contract = await helper.ethNativeContract.collection(address, 'nft', caller);334335 const value = await contract.methods.property(token.tokenId, 'testKey').call();336 expect(value).to.equal(helper.getWeb3().utils.toHex('testValue'));337 });338});339340describe('EVM token properties negative', () => {341 let donor: IKeyringPair;342 let alice: IKeyringPair;343 let caller: string;344 let aliceCollection: UniqueNFTCollection;345 let token: UniqueNFToken;346 const tokenProps = [{key: 'testKey_1', value: 'testValue_1'}, {key: 'testKey_2', value: 'testValue_2'}];347 let collectionEvm: Contract;348349 before(async function() {350 await usingEthPlaygrounds(async (helper, privateKey) => {351 donor = await privateKey({url: import.meta.url});352 [alice] = await helper.arrange.createAccounts([100n], donor);353 });354 });355356 beforeEach(async () => {357 358 359 await usingEthPlaygrounds(async (helper) => {360 aliceCollection = await helper.nft.mintCollection(alice, {361 tokenPropertyPermissions: [{362 key: 'testKey_1',363 permission: {364 mutable: true,365 collectionAdmin: true,366 },367 },368 {369 key: 'testKey_2',370 permission: {371 mutable: true,372 collectionAdmin: true,373 },374 }],375 });376 token = await aliceCollection.mintToken(alice);377 await token.setProperties(alice, tokenProps);378 collectionEvm = await helper.ethNativeContract.collection(helper.ethAddress.fromCollectionId(aliceCollection.collectionId), 'nft', caller, true);379 });380 });381382 [383 {method: 'setProperty', methodParams: [tokenProps[1].key, Buffer.from('newValue')]},384 {method: 'setProperties', methodParams: [[{key: tokenProps[1].key, value: Buffer.from('newValue')}]]},385 ].map(testCase =>386 itEth(`[${testCase.method}] Cannot set properties of non-owned collection`, async ({helper}) => {387 caller = await helper.eth.createAccountWithBalance(donor);388 collectionEvm = await helper.ethNativeContract.collection(helper.ethAddress.fromCollectionId(aliceCollection.collectionId), 'nft', caller, true);389 390 await expect(collectionEvm.methods[testCase.method](token.tokenId, ...testCase.methodParams).call({from: caller})).to.be.rejectedWith('NoPermission');391 await expect(collectionEvm.methods[testCase.method](token.tokenId, ...testCase.methodParams).send({from: caller})).to.be.rejected;392393 394 const expectedProps = tokenProps.map(p => helper.ethProperty.property(p.key, p.value.toString()));395 const actualProps = await collectionEvm.methods.properties(token.tokenId, []).call();396 expect(actualProps).to.deep.eq(expectedProps);397 }));398399 [400 {method: 'setProperty', methodParams: ['testKey_3', Buffer.from('testValue3')]},401 {method: 'setProperties', methodParams: [[{key: 'testKey_3', value: Buffer.from('testValue3')}]]},402 ].map(testCase =>403 itEth(`[${testCase.method}] Cannot set non-existing properties`, async ({helper}) => {404 caller = await helper.eth.createAccountWithBalance(donor);405 collectionEvm = await helper.ethNativeContract.collection(helper.ethAddress.fromCollectionId(aliceCollection.collectionId), 'nft', caller, true);406 await helper.collection.addAdmin(alice, aliceCollection.collectionId, {Ethereum: caller});407408 await expect(collectionEvm.methods[testCase.method](token.tokenId, ...testCase.methodParams).call({from: caller})).to.be.rejectedWith('NoPermission');409 await expect(collectionEvm.methods[testCase.method](token.tokenId, ...testCase.methodParams).send({from: caller})).to.be.rejected;410411 412 const expectedProps = tokenProps.map(p => helper.ethProperty.property(p.key, p.value.toString()));413 const actualProps = await collectionEvm.methods.properties(token.tokenId, []).call();414 expect(actualProps).to.deep.eq(expectedProps);415 }));416417 [418 {method: 'deleteProperty', methodParams: ['testKey_2']},419 {method: 'deleteProperties', methodParams: [['testKey_2']]},420 ].map(testCase =>421 itEth(`[${testCase.method}] Cannot delete properties of non-owned collection`, async ({helper}) => {422 caller = await helper.eth.createAccountWithBalance(donor);423 collectionEvm = await helper.ethNativeContract.collection(helper.ethAddress.fromCollectionId(aliceCollection.collectionId), 'nft', caller, testCase.method == 'deleteProperty');424 425 await expect(collectionEvm.methods[testCase.method](token.tokenId, ...testCase.methodParams).call({from: caller})).to.be.rejectedWith('NoPermission');426 await expect(collectionEvm.methods[testCase.method](token.tokenId, ...testCase.methodParams).send({from: caller})).to.be.rejected;427428 429 const expectedProps = tokenProps.map(p => helper.ethProperty.property(p.key, p.value.toString()));430 const actualProps = await collectionEvm.methods.properties(token.tokenId, []).call();431 expect(actualProps).to.deep.eq(expectedProps);432 }));433434 [435 {method: 'deleteProperty', methodParams: ['testKey_3']},436 {method: 'deleteProperties', methodParams: [['testKey_3']]},437 ].map(testCase =>438 itEth(`[${testCase.method}] Cannot delete non-existing properties`, async ({helper}) => {439 caller = await helper.eth.createAccountWithBalance(donor);440 collectionEvm = await helper.ethNativeContract.collection(helper.ethAddress.fromCollectionId(aliceCollection.collectionId), 'nft', caller, testCase.method == 'deleteProperty');441 await helper.collection.addAdmin(alice, aliceCollection.collectionId, {Ethereum: caller});442 443 await expect(collectionEvm.methods[testCase.method](token.tokenId, ...testCase.methodParams).call({from: caller})).to.be.rejectedWith('NoPermission');444 await expect(collectionEvm.methods[testCase.method](token.tokenId, ...testCase.methodParams).send({from: caller})).to.be.rejected;445 446 const expectedProps = tokenProps.map(p => helper.ethProperty.property(p.key, p.value.toString()));447 const actualProps = await collectionEvm.methods.properties(token.tokenId, []).call();448 expect(actualProps).to.deep.eq(expectedProps);449 }));450451 [452 {mode: 'nft' as const, requiredPallets: []},453 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},454 ].map(testCase =>455 itEth.ifWithPallets(`[${testCase.mode}] Cannot set token property permissions as non owner or admin`, testCase.requiredPallets, async({helper}) => {456 const owner = await helper.eth.createAccountWithBalance(donor);457 const caller = await helper.eth.createAccountWithBalance(donor);458459 const {collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');460 const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);461462 await expect(collection.methods.setTokenPropertyPermissions([463 ['testKey_0', [464 [TokenPermissionField.Mutable, true],465 [TokenPermissionField.TokenOwner, true],466 [TokenPermissionField.CollectionAdmin, true]],467 ],468 ]).call({from: caller})).to.be.rejectedWith('NoPermission');469 }));470471 [472 {mode: 'nft' as const, requiredPallets: []},473 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},474 ].map(testCase =>475 itEth.ifWithPallets(`[${testCase.mode}] Cannot set token property permissions with invalid character`, testCase.requiredPallets, async({helper}) => {476 const owner = await helper.eth.createAccountWithBalance(donor);477478 const {collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');479 const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);480481 await expect(collection.methods.setTokenPropertyPermissions([482 483 ['testKey 0', [484 [TokenPermissionField.Mutable, true],485 [TokenPermissionField.TokenOwner, true],486 [TokenPermissionField.CollectionAdmin, true]],487 ],488 ]).call({from: owner})).to.be.rejectedWith('InvalidCharacterInPropertyKey');489 }));490491 [492 {mode: 'nft' as const, requiredPallets: []},493 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},494 ].map(testCase =>495 itEth.ifWithPallets(`[${testCase.mode}] Can reconfigure token property permissions to stricter ones`, testCase.requiredPallets, async({helper}) => {496 const owner = await helper.eth.createAccountWithBalance(donor);497498 const {collectionAddress, collectionId} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');499 const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);500501 502 await collection.methods.setTokenPropertyPermissions([503 ['testKey', [504 [TokenPermissionField.Mutable, true],505 [TokenPermissionField.TokenOwner, true],506 [TokenPermissionField.CollectionAdmin, true]],507 ],508 ]).send({from: owner});509510 511 for(const values of [[true, true, false], [true, false, false], [false, false, false]]) {512 await collection.methods.setTokenPropertyPermissions([513 ['testKey', [514 [TokenPermissionField.Mutable, values[0]],515 [TokenPermissionField.TokenOwner, values[1]],516 [TokenPermissionField.CollectionAdmin, values[2]]],517 ],518 ]).send({from: owner});519 }520521 expect(await helper[testCase.mode].getPropertyPermissions(collectionId)).to.be.deep.equal([{522 key: 'testKey',523 permission: {mutable: false, collectionAdmin: false, tokenOwner: false},524 }]);525 }));526527 [528 {mode: 'nft' as const, requiredPallets: []},529 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},530 ].map(testCase =>531 itEth.ifWithPallets(`[${testCase.mode}] Cannot reconfigure token property permissions to less strict ones`, testCase.requiredPallets, async({helper}) => {532 const owner = await helper.eth.createAccountWithBalance(donor);533534 const {collectionAddress} = await helper.eth.createCollection(testCase.mode, owner, 'A', 'B', 'C');535 const collection = await helper.ethNativeContract.collection(collectionAddress, testCase.mode, owner);536537 538 await collection.methods.setTokenPropertyPermissions([539 ['testKey', [540 [TokenPermissionField.Mutable, false],541 [TokenPermissionField.TokenOwner, false],542 [TokenPermissionField.CollectionAdmin, false]],543 ],544 ]).send({from: owner});545546 547 for(const values of [[true, false, false], [false, true, false], [false, false, true]]) {548 await expect(collection.methods.setTokenPropertyPermissions([549 ['testKey', [550 [TokenPermissionField.Mutable, values[0]],551 [TokenPermissionField.TokenOwner, values[1]],552 [TokenPermissionField.CollectionAdmin, values[2]]],553 ],554 ]).call({from: owner})).to.be.rejectedWith('NoPermission');555 }556 }));557});558559560type ElementOf<A> = A extends readonly (infer T)[] ? T : never;561function* cartesian<T extends Array<Array<any>>, R extends Array<any>>(internalRest: [...R], ...args: [...T]): Generator<[...R, ...{[K in keyof T]: ElementOf<T[K]>}]> {562 if(args.length === 0) {563 yield internalRest as any;564 return;565 }566 for(const value of args[0]) {567 yield* cartesian([...internalRest, value], ...args.slice(1)) as any;568 }569}