difftreelog
feature: Added `delete_properties` in eth functions. The `delete_property` function is now deprecated.
in: master
15 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6376,7 +6376,7 @@
[[package]]
name = "pallet-nonfungible"
-version = "0.1.7"
+version = "0.1.8"
dependencies = [
"ethereum",
"evm-coder",
@@ -6498,7 +6498,7 @@
[[package]]
name = "pallet-refungible"
-version = "0.2.6"
+version = "0.2.7"
dependencies = [
"derivative",
"ethereum",
pallets/nonfungible/CHANGELOG.mddiffbeforeafterboth--- a/pallets/nonfungible/CHANGELOG.md
+++ b/pallets/nonfungible/CHANGELOG.md
@@ -3,10 +3,19 @@
All notable changes to this project will be documented in this file.
<!-- bureaucrate goes here -->
+
+## [v0.1.8] - 2022-11-11
+
+### Changed
+
+- Added `delete_properties` in eth functions. The `delete_property` function is now deprecated.
+
## [v0.1.7] - 2022-11-02
+
### Changed
- - Use named structure `EthCrossAccount` in eth functions.
+- Use named structure `EthCrossAccount` in eth functions.
+
## [v0.1.6] - 2022-20-10
### Change
pallets/nonfungible/Cargo.tomldiffbeforeafterboth--- a/pallets/nonfungible/Cargo.toml
+++ b/pallets/nonfungible/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "pallet-nonfungible"
-version = "0.1.7"
+version = "0.1.8"
license = "GPLv3"
edition = "2021"
pallets/nonfungible/src/erc.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -162,6 +162,7 @@
/// @dev Throws error if `msg.sender` has no permission to edit the property.
/// @param tokenId ID of the token.
/// @param key Property key.
+ #[solidity(hide)]
fn delete_property(&mut self, token_id: uint256, caller: caller, key: string) -> Result<()> {
let caller = T::CrossAccountId::from_eth(caller);
let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;
@@ -177,6 +178,37 @@
.map_err(dispatch_to_evm::<T>)
}
+ /// @notice Delete token properties value.
+ /// @dev Throws error if `msg.sender` has no permission to edit the property.
+ /// @param tokenId ID of the token.
+ /// @param keys Properties key.
+ fn delete_properties(
+ &mut self,
+ token_id: uint256,
+ caller: caller,
+ keys: Vec<string>,
+ ) -> Result<()> {
+ let caller = T::CrossAccountId::from_eth(caller);
+ let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;
+ let keys = keys
+ .into_iter()
+ .map(|k| Ok(<Vec<u8>>::from(k).try_into().map_err(|_| "key too long")?))
+ .collect::<Result<Vec<_>>>()?;
+
+ let nesting_budget = self
+ .recorder
+ .weight_calls_budget(<StructureWeight<T>>::find_parent());
+
+ <Pallet<T>>::delete_token_properties(
+ self,
+ &caller,
+ TokenId(token_id),
+ keys.into_iter(),
+ &nesting_budget,
+ )
+ .map_err(dispatch_to_evm::<T>)
+ }
+
/// @notice Get token property value.
/// @dev Throws error if key not found
/// @param tokenId ID of the token.
pallets/nonfungible/src/stubs/UniqueNFT.soldiffbeforeafterboth--- a/pallets/nonfungible/src/stubs/UniqueNFT.sol
+++ b/pallets/nonfungible/src/stubs/UniqueNFT.sol
@@ -18,7 +18,7 @@
}
/// @title A contract that allows to set and delete token properties and change token property permissions.
-/// @dev the ERC-165 identifier for this interface is 0x55dba919
+/// @dev the ERC-165 identifier for this interface is 0x91a97a68
contract TokenProperties is Dummy, ERC165 {
/// @notice Set permissions for token property.
/// @dev Throws error if `msg.sender` is not admin or owner of the collection.
@@ -74,16 +74,29 @@
dummy = 0;
}
- /// @notice Delete token property value.
+ // /// @notice Delete token property value.
+ // /// @dev Throws error if `msg.sender` has no permission to edit the property.
+ // /// @param tokenId ID of the token.
+ // /// @param key Property key.
+ // /// @dev EVM selector for this function is: 0x066111d1,
+ // /// or in textual repr: deleteProperty(uint256,string)
+ // function deleteProperty(uint256 tokenId, string memory key) public {
+ // require(false, stub_error);
+ // tokenId;
+ // key;
+ // dummy = 0;
+ // }
+
+ /// @notice Delete token properties value.
/// @dev Throws error if `msg.sender` has no permission to edit the property.
/// @param tokenId ID of the token.
- /// @param key Property key.
- /// @dev EVM selector for this function is: 0x066111d1,
- /// or in textual repr: deleteProperty(uint256,string)
- function deleteProperty(uint256 tokenId, string memory key) public {
+ /// @param keys Properties key.
+ /// @dev EVM selector for this function is: 0xc472d371,
+ /// or in textual repr: deleteProperties(uint256,string[])
+ function deleteProperties(uint256 tokenId, string[] memory keys) public {
require(false, stub_error);
tokenId;
- key;
+ keys;
dummy = 0;
}
pallets/refungible/CHANGELOG.mddiffbeforeafterboth--- a/pallets/refungible/CHANGELOG.md
+++ b/pallets/refungible/CHANGELOG.md
@@ -2,10 +2,20 @@
All notable changes to this project will be documented in this file.
+<!-- bureaucrate goes here -->
+
+## [v0.2.7] - 2022-11-11
+
+### Changed
+
+- Added `delete_properties` in eth functions. The `delete_property` function is now deprecated.
+
## [v0.2.6] - 2022-11-02
+
### Changed
- - Use named structure `EthCrossAccount` in eth functions.
+- Use named structure `EthCrossAccount` in eth functions.
+
## [v0.2.5] - 2022-20-10
### Change
@@ -17,8 +27,6 @@
### Change
- Add bound `AsRef<[u8; 32]>` to `T::CrossAccountId`.
-
-<!-- bureaucrate goes here -->
## [v0.2.3] 2022-08-16
pallets/refungible/Cargo.tomldiffbeforeafterboth--- a/pallets/refungible/Cargo.toml
+++ b/pallets/refungible/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "pallet-refungible"
-version = "0.2.6"
+version = "0.2.7"
license = "GPLv3"
edition = "2021"
pallets/refungible/src/erc.rsdiffbeforeafterboth--- a/pallets/refungible/src/erc.rs
+++ b/pallets/refungible/src/erc.rs
@@ -164,6 +164,7 @@
/// @dev Throws error if `msg.sender` has no permission to edit the property.
/// @param tokenId ID of the token.
/// @param key Property key.
+ #[solidity(hide)]
fn delete_property(&mut self, token_id: uint256, caller: caller, key: string) -> Result<()> {
let caller = T::CrossAccountId::from_eth(caller);
let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;
@@ -179,6 +180,37 @@
.map_err(dispatch_to_evm::<T>)
}
+ /// @notice Delete token properties value.
+ /// @dev Throws error if `msg.sender` has no permission to edit the property.
+ /// @param tokenId ID of the token.
+ /// @param keys Properties key.
+ fn delete_properties(
+ &mut self,
+ token_id: uint256,
+ caller: caller,
+ keys: Vec<string>,
+ ) -> Result<()> {
+ let caller = T::CrossAccountId::from_eth(caller);
+ let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;
+ let keys = keys
+ .into_iter()
+ .map(|k| Ok(<Vec<u8>>::from(k).try_into().map_err(|_| "key too long")?))
+ .collect::<Result<Vec<_>>>()?;
+
+ let nesting_budget = self
+ .recorder
+ .weight_calls_budget(<StructureWeight<T>>::find_parent());
+
+ <Pallet<T>>::delete_token_properties(
+ self,
+ &caller,
+ TokenId(token_id),
+ keys.into_iter(),
+ &nesting_budget,
+ )
+ .map_err(dispatch_to_evm::<T>)
+ }
+
/// @notice Get token property value.
/// @dev Throws error if key not found
/// @param tokenId ID of the token.
pallets/refungible/src/stubs/UniqueRefungible.soldiffbeforeafterboth--- a/pallets/refungible/src/stubs/UniqueRefungible.sol
+++ b/pallets/refungible/src/stubs/UniqueRefungible.sol
@@ -18,7 +18,7 @@
}
/// @title A contract that allows to set and delete token properties and change token property permissions.
-/// @dev the ERC-165 identifier for this interface is 0x55dba919
+/// @dev the ERC-165 identifier for this interface is 0x91a97a68
contract TokenProperties is Dummy, ERC165 {
/// @notice Set permissions for token property.
/// @dev Throws error if `msg.sender` is not admin or owner of the collection.
@@ -74,16 +74,29 @@
dummy = 0;
}
- /// @notice Delete token property value.
+ // /// @notice Delete token property value.
+ // /// @dev Throws error if `msg.sender` has no permission to edit the property.
+ // /// @param tokenId ID of the token.
+ // /// @param key Property key.
+ // /// @dev EVM selector for this function is: 0x066111d1,
+ // /// or in textual repr: deleteProperty(uint256,string)
+ // function deleteProperty(uint256 tokenId, string memory key) public {
+ // require(false, stub_error);
+ // tokenId;
+ // key;
+ // dummy = 0;
+ // }
+
+ /// @notice Delete token properties value.
/// @dev Throws error if `msg.sender` has no permission to edit the property.
/// @param tokenId ID of the token.
- /// @param key Property key.
- /// @dev EVM selector for this function is: 0x066111d1,
- /// or in textual repr: deleteProperty(uint256,string)
- function deleteProperty(uint256 tokenId, string memory key) public {
+ /// @param keys Properties key.
+ /// @dev EVM selector for this function is: 0xc472d371,
+ /// or in textual repr: deleteProperties(uint256,string[])
+ function deleteProperties(uint256 tokenId, string[] memory keys) public {
require(false, stub_error);
tokenId;
- key;
+ keys;
dummy = 0;
}
tests/src/eth/api/UniqueNFT.soldiffbeforeafterboth--- a/tests/src/eth/api/UniqueNFT.sol
+++ b/tests/src/eth/api/UniqueNFT.sol
@@ -13,7 +13,7 @@
}
/// @title A contract that allows to set and delete token properties and change token property permissions.
-/// @dev the ERC-165 identifier for this interface is 0x55dba919
+/// @dev the ERC-165 identifier for this interface is 0x91a97a68
interface TokenProperties is Dummy, ERC165 {
/// @notice Set permissions for token property.
/// @dev Throws error if `msg.sender` is not admin or owner of the collection.
@@ -51,13 +51,21 @@
/// or in textual repr: setProperties(uint256,(string,bytes)[])
function setProperties(uint256 tokenId, Tuple21[] memory properties) external;
- /// @notice Delete token property value.
+ // /// @notice Delete token property value.
+ // /// @dev Throws error if `msg.sender` has no permission to edit the property.
+ // /// @param tokenId ID of the token.
+ // /// @param key Property key.
+ // /// @dev EVM selector for this function is: 0x066111d1,
+ // /// or in textual repr: deleteProperty(uint256,string)
+ // function deleteProperty(uint256 tokenId, string memory key) external;
+
+ /// @notice Delete token properties value.
/// @dev Throws error if `msg.sender` has no permission to edit the property.
/// @param tokenId ID of the token.
- /// @param key Property key.
- /// @dev EVM selector for this function is: 0x066111d1,
- /// or in textual repr: deleteProperty(uint256,string)
- function deleteProperty(uint256 tokenId, string memory key) external;
+ /// @param keys Properties key.
+ /// @dev EVM selector for this function is: 0xc472d371,
+ /// or in textual repr: deleteProperties(uint256,string[])
+ function deleteProperties(uint256 tokenId, string[] memory keys) external;
/// @notice Get token property value.
/// @dev Throws error if key not found
tests/src/eth/api/UniqueRefungible.soldiffbeforeafterboth--- a/tests/src/eth/api/UniqueRefungible.sol
+++ b/tests/src/eth/api/UniqueRefungible.sol
@@ -13,7 +13,7 @@
}
/// @title A contract that allows to set and delete token properties and change token property permissions.
-/// @dev the ERC-165 identifier for this interface is 0x55dba919
+/// @dev the ERC-165 identifier for this interface is 0x91a97a68
interface TokenProperties is Dummy, ERC165 {
/// @notice Set permissions for token property.
/// @dev Throws error if `msg.sender` is not admin or owner of the collection.
@@ -51,13 +51,21 @@
/// or in textual repr: setProperties(uint256,(string,bytes)[])
function setProperties(uint256 tokenId, Tuple20[] memory properties) external;
- /// @notice Delete token property value.
+ // /// @notice Delete token property value.
+ // /// @dev Throws error if `msg.sender` has no permission to edit the property.
+ // /// @param tokenId ID of the token.
+ // /// @param key Property key.
+ // /// @dev EVM selector for this function is: 0x066111d1,
+ // /// or in textual repr: deleteProperty(uint256,string)
+ // function deleteProperty(uint256 tokenId, string memory key) external;
+
+ /// @notice Delete token properties value.
/// @dev Throws error if `msg.sender` has no permission to edit the property.
/// @param tokenId ID of the token.
- /// @param key Property key.
- /// @dev EVM selector for this function is: 0x066111d1,
- /// or in textual repr: deleteProperty(uint256,string)
- function deleteProperty(uint256 tokenId, string memory key) external;
+ /// @param keys Properties key.
+ /// @dev EVM selector for this function is: 0xc472d371,
+ /// or in textual repr: deleteProperties(uint256,string[])
+ function deleteProperties(uint256 tokenId, string[] memory keys) external;
/// @notice Get token property value.
/// @dev Throws error if key not found
tests/src/eth/collectionProperties.test.tsdiffbeforeafterboth--- a/tests/src/eth/collectionProperties.test.ts
+++ b/tests/src/eth/collectionProperties.test.ts
@@ -139,7 +139,7 @@
await contract.methods.setProperty(tokenId1, 'URI', Buffer.from(URI)).send();
expect(await contract.methods.tokenURI(tokenId1).call()).to.equal(URI);
- await contract.methods.deleteProperty(tokenId1, 'URI').send();
+ await contract.methods.deleteProperties(tokenId1, ['URI']).send();
expect(await contract.methods.tokenURI(tokenId1).call()).to.equal(BASE_URI + SUFFIX);
const token2Result = await contract.methods.mintWithTokenURI(bruh, URI).send();
@@ -147,7 +147,7 @@
expect(await contract.methods.tokenURI(tokenId2).call()).to.equal(URI);
- await contract.methods.deleteProperty(tokenId2, 'URI').send();
+ await contract.methods.deleteProperties(tokenId2, ['URI']).send();
expect(await contract.methods.tokenURI(tokenId2).call()).to.equal(BASE_URI);
await contract.methods.setProperty(tokenId2, 'URISuffix', Buffer.from(SUFFIX)).send();
tests/src/eth/nonFungibleAbi.jsondiffbeforeafterboth--- a/tests/src/eth/nonFungibleAbi.json
+++ b/tests/src/eth/nonFungibleAbi.json
@@ -334,9 +334,9 @@
{
"inputs": [
{ "internalType": "uint256", "name": "tokenId", "type": "uint256" },
- { "internalType": "string", "name": "key", "type": "string" }
+ { "internalType": "string[]", "name": "keys", "type": "string[]" }
],
- "name": "deleteProperty",
+ "name": "deleteProperties",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
tests/src/eth/reFungibleAbi.jsondiffbeforeafterboth--- a/tests/src/eth/reFungibleAbi.json
+++ b/tests/src/eth/reFungibleAbi.json
@@ -316,9 +316,9 @@
{
"inputs": [
{ "internalType": "uint256", "name": "tokenId", "type": "uint256" },
- { "internalType": "string", "name": "key", "type": "string" }
+ { "internalType": "string[]", "name": "keys", "type": "string[]" }
],
- "name": "deleteProperty",
+ "name": "deleteProperties",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
tests/src/eth/tokenProperties.test.tsdiffbeforeafterboth1// 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';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}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';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}