git.delta.rocks / unique-network / refs/commits / 118b8d5eaba9

difftreelog

feature: add `createERC721MetadataCompatibleRFTCollection` method

Trubnikov Sergey2022-07-25parent: #e516992.patch.diff
in: master

4 files changed

modifiedpallets/unique/src/eth/mod.rsdiffbeforeafterboth
--- a/pallets/unique/src/eth/mod.rs
+++ b/pallets/unique/src/eth/mod.rs
@@ -237,6 +237,33 @@
 		Ok(address)
 	}
 
+	#[weight(<SelfWeightOf<T>>::create_collection())]
+	#[solidity(rename_selector = "createERC721MetadataCompatibleRFTCollection")]
+	fn create_refungible_collection_with_properties(
+		&mut self,
+		caller: caller,
+		name: string,
+		description: string,
+		token_prefix: string,
+		base_uri: string,
+	) -> Result<address> {
+		let (caller, name, description, token_prefix, base_uri_value) =
+			convert_data::<T>(caller, name, description, token_prefix, base_uri)?;
+		let data = make_data::<T>(
+			name,
+			CollectionMode::NFT,
+			description,
+			token_prefix,
+			base_uri_value,
+			true,
+		)?;
+		let collection_id = <pallet_refungible::Pallet<T>>::init_collection(caller.clone(), data)
+			.map_err(pallet_evm_coder_substrate::dispatch_to_evm::<T>)?;
+
+		let address = pallet_common::eth::collection_id_to_address(collection_id);
+		Ok(address)
+	}
+
 	/// Check if a collection exists
 	/// @param collection_address Address of the collection in question
 	/// @return bool Does the collection exist?
addedtests/src/eth/api/UniqueRefungible.soldiffbeforeafterboth
after · tests/src/eth/api/UniqueRefungible.sol
1// SPDX-License-Identifier: OTHER2// This code is automatically generated34pragma solidity >=0.8.0 <0.9.0;56// Common stubs holder7interface Dummy {89}1011interface ERC165 is Dummy {12	function supportsInterface(bytes4 interfaceID) external view returns (bool);13}1415// Selector: 7d9262e616interface Collection is Dummy, ERC165 {17	// Set collection property.18	//19	// @param key Property key.20	// @param value Propery value.21	//22	// Selector: setCollectionProperty(string,bytes) 2f073f6623	function setCollectionProperty(string memory key, bytes memory value)24		external;2526	// Delete collection property.27	//28	// @param key Property key.29	//30	// Selector: deleteCollectionProperty(string) 7b7debce31	function deleteCollectionProperty(string memory key) external;3233	// Get collection property.34	//35	// @dev Throws error if key not found.36	//37	// @param key Property key.38	// @return bytes The property corresponding to the key.39	//40	// Selector: collectionProperty(string) cf24fd6d41	function collectionProperty(string memory key)42		external43		view44		returns (bytes memory);4546	// Set the sponsor of the collection.47	//48	// @dev In order for sponsorship to work, it must be confirmed on behalf of the sponsor.49	//50	// @param sponsor Address of the sponsor from whose account funds will be debited for operations with the contract.51	//52	// Selector: setCollectionSponsor(address) 7623402e53	function setCollectionSponsor(address sponsor) external;5455	// Collection sponsorship confirmation.56	//57	// @dev After setting the sponsor for the collection, it must be confirmed with this function.58	//59	// Selector: confirmCollectionSponsorship() 3c50e97a60	function confirmCollectionSponsorship() external;6162	// Set limits for the collection.63	// @dev Throws error if limit not found.64	// @param limit Name of the limit. Valid names:65	// 	"accountTokenOwnershipLimit",66	// 	"sponsoredDataSize",67	// 	"sponsoredDataRateLimit",68	// 	"tokenLimit",69	// 	"sponsorTransferTimeout",70	// 	"sponsorApproveTimeout"71	// @param value Value of the limit.72	//73	// Selector: setCollectionLimit(string,uint32) 6a3841db74	function setCollectionLimit(string memory limit, uint32 value) external;7576	// Set limits for the collection.77	// @dev Throws error if limit not found.78	// @param limit Name of the limit. Valid names:79	// 	"ownerCanTransfer",80	// 	"ownerCanDestroy",81	// 	"transfersEnabled"82	// @param value Value of the limit.83	//84	// Selector: setCollectionLimit(string,bool) 993b7fba85	function setCollectionLimit(string memory limit, bool value) external;8687	// Get contract address.88	//89	// Selector: contractAddress() f6b4dfb490	function contractAddress() external view returns (address);9192	// Add collection admin by substrate address.93	// @param new_admin Substrate administrator address.94	//95	// Selector: addCollectionAdminSubstrate(uint256) 5730062b96	function addCollectionAdminSubstrate(uint256 newAdmin) external;9798	// Remove collection admin by substrate address.99	// @param admin Substrate administrator address.100	//101	// Selector: removeCollectionAdminSubstrate(uint256) 4048fcf9102	function removeCollectionAdminSubstrate(uint256 admin) external;103104	// Add collection admin.105	// @param new_admin Address of the added administrator.106	//107	// Selector: addCollectionAdmin(address) 92e462c7108	function addCollectionAdmin(address newAdmin) external;109110	// Remove collection admin.111	//112	// @param new_admin Address of the removed administrator.113	//114	// Selector: removeCollectionAdmin(address) fafd7b42115	function removeCollectionAdmin(address admin) external;116117	// Toggle accessibility of collection nesting.118	//119	// @param enable If "true" degenerates to nesting: 'Owner' else to nesting: 'Disabled'120	//121	// Selector: setCollectionNesting(bool) 112d4586122	function setCollectionNesting(bool enable) external;123124	// Toggle accessibility of collection nesting.125	//126	// @param enable If "true" degenerates to nesting: {OwnerRestricted: [1, 2, 3]} else to nesting: 'Disabled'127	// @param collections Addresses of collections that will be available for nesting.128	//129	// Selector: setCollectionNesting(bool,address[]) 64872396130	function setCollectionNesting(bool enable, address[] memory collections)131		external;132133	// Set the collection access method.134	// @param mode Access mode135	// 	0 for Normal136	// 	1 for AllowList137	//138	// Selector: setCollectionAccess(uint8) 41835d4c139	function setCollectionAccess(uint8 mode) external;140141	// Add the user to the allowed list.142	//143	// @param user Address of a trusted user.144	//145	// Selector: addToCollectionAllowList(address) 67844fe6146	function addToCollectionAllowList(address user) external;147148	// Remove the user from the allowed list.149	//150	// @param user Address of a removed user.151	//152	// Selector: removeFromCollectionAllowList(address) 85c51acb153	function removeFromCollectionAllowList(address user) external;154155	// Switch permission for minting.156	//157	// @param mode Enable if "true".158	//159	// Selector: setCollectionMintMode(bool) 00018e84160	function setCollectionMintMode(bool mode) external;161}162163interface UniqueRefungible is Dummy, ERC165, Collection {}
modifiedtests/src/eth/nonFungible.test.tsdiffbeforeafterboth
--- a/tests/src/eth/nonFungible.test.ts
+++ b/tests/src/eth/nonFungible.test.ts
@@ -72,7 +72,7 @@
   });
 });
 
-describe('Check ERC721 token URI', () => {
+describe('Check ERC721 token URI for NFT', () => {
   itWeb3('Empty tokenURI', async ({web3, api, privateKeyWrapper}) => {
     const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
     const helper = evmCollectionHelpers(web3, owner);
modifiedtests/src/eth/reFungibleToken.test.tsdiffbeforeafterboth
--- a/tests/src/eth/reFungibleToken.test.ts
+++ b/tests/src/eth/reFungibleToken.test.ts
@@ -15,7 +15,7 @@
 // along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
 
 import {approve, createCollection, createRefungibleToken, transfer, transferFrom, UNIQUE} from '../util/helpers';
-import {createEthAccount, createEthAccountWithBalance, GAS_ARGS, itWeb3, normalizeEvents, recordEthFee, recordEvents, subToEth, tokenIdToAddress, transferBalanceToEth} from './util/helpers';
+import {collectionIdToAddress, createEthAccount, createEthAccountWithBalance, evmCollection, evmCollectionHelpers, GAS_ARGS, getCollectionAddressFromResult, itWeb3, normalizeEvents, recordEthFee, recordEvents, subToEth, tokenIdToAddress, transferBalanceToEth} from './util/helpers';
 import reFungibleTokenAbi from './reFungibleTokenAbi.json';
 
 import chai from 'chai';
@@ -73,6 +73,148 @@
   });
 });
 
+// FIXME: Need erc721 for ReFubgible.
+describe.skip('Check ERC721 token URI for ReFungible', () => {
+  itWeb3('Empty tokenURI', async ({web3, api, privateKeyWrapper}) => {
+    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+    const helper = evmCollectionHelpers(web3, owner);
+    let result = await helper.methods.createERC721MetadataCompatibleCollection('Mint collection', '1', '1', '').send();
+    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);
+    const receiver = createEthAccount(web3);
+    const contract = evmCollection(web3, owner, collectionIdAddress, {type: 'ReFungible'});
+    
+    const nextTokenId = await contract.methods.nextTokenId().call();
+    expect(nextTokenId).to.be.equal('1');
+    result = await contract.methods.mint(
+      receiver,
+      nextTokenId,
+    ).send();
+
+    const events = normalizeEvents(result.events);
+    const address = collectionIdToAddress(collectionId);
+
+    expect(events).to.be.deep.equal([
+      {
+        address,
+        event: 'Transfer',
+        args: {
+          from: '0x0000000000000000000000000000000000000000',
+          to: receiver,
+          tokenId: nextTokenId,
+        },
+      },
+    ]);
+
+    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('');
+  });
+
+  itWeb3('TokenURI from url', async ({web3, api, privateKeyWrapper}) => {
+    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+    const helper = evmCollectionHelpers(web3, owner);
+    let result = await helper.methods.createERC721MetadataCompatibleCollection('Mint collection', '1', '1', 'BaseURI_').send();
+    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);
+    const receiver = createEthAccount(web3);
+    const contract = evmCollection(web3, owner, collectionIdAddress, {type: 'ReFungible'});
+    
+    const nextTokenId = await contract.methods.nextTokenId().call();
+    expect(nextTokenId).to.be.equal('1');
+    result = await contract.methods.mint(
+      receiver,
+      nextTokenId,
+    ).send();
+    
+    // Set URL
+    await contract.methods.setProperty(nextTokenId, 'url', Buffer.from('Token URI')).send();
+      
+    const events = normalizeEvents(result.events);
+    const address = collectionIdToAddress(collectionId);
+
+    expect(events).to.be.deep.equal([
+      {
+        address,
+        event: 'Transfer',
+        args: {
+          from: '0x0000000000000000000000000000000000000000',
+          to: receiver,
+          tokenId: nextTokenId,
+        },
+      },
+    ]);
+
+    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('Token URI');
+  });
+
+  itWeb3('TokenURI from baseURI + tokenId', async ({web3, api, privateKeyWrapper}) => {
+    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+    const helper = evmCollectionHelpers(web3, owner);
+    let result = await helper.methods.createERC721MetadataCompatibleCollection('Mint collection', '1', '1', 'BaseURI_').send();
+    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);
+    const receiver = createEthAccount(web3);
+    const contract = evmCollection(web3, owner, collectionIdAddress, {type: 'ReFungible'});
+    
+    const nextTokenId = await contract.methods.nextTokenId().call();
+    expect(nextTokenId).to.be.equal('1');
+    result = await contract.methods.mint(
+      receiver,
+      nextTokenId,
+    ).send();
+          
+    const events = normalizeEvents(result.events);
+    const address = collectionIdToAddress(collectionId);
+
+    expect(events).to.be.deep.equal([
+      {
+        address,
+        event: 'Transfer',
+        args: {
+          from: '0x0000000000000000000000000000000000000000',
+          to: receiver,
+          tokenId: nextTokenId,
+        },
+      },
+    ]);
+
+    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_' + nextTokenId);
+  });
+
+  itWeb3('TokenURI from baseURI + suffix', async ({web3, api, privateKeyWrapper}) => {
+    const owner = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+    const helper = evmCollectionHelpers(web3, owner);
+    let result = await helper.methods.createERC721MetadataCompatibleCollection('Mint collection', '1', '1', 'BaseURI_').send();
+    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);
+    const receiver = createEthAccount(web3);
+    const contract = evmCollection(web3, owner, collectionIdAddress, {type: 'ReFungible'});
+    
+    const nextTokenId = await contract.methods.nextTokenId().call();
+    expect(nextTokenId).to.be.equal('1');
+    result = await contract.methods.mint(
+      receiver,
+      nextTokenId,
+    ).send();
+          
+    // Set suffix
+    const suffix = '/some/suffix';
+    await contract.methods.setProperty(nextTokenId, 'suffix', Buffer.from(suffix)).send();
+
+    const events = normalizeEvents(result.events);
+    const address = collectionIdToAddress(collectionId);
+
+    expect(events).to.be.deep.equal([
+      {
+        address,
+        event: 'Transfer',
+        args: {
+          from: '0x0000000000000000000000000000000000000000',
+          to: receiver,
+          tokenId: nextTokenId,
+        },
+      },
+    ]);
+
+    expect(await contract.methods.tokenURI(nextTokenId).call()).to.be.equal('BaseURI_' + suffix);
+  });
+});
+
 describe('Refungible: Plain calls', () => {
   itWeb3('Can perform approve()', async ({web3, api, privateKeyWrapper}) => {
     const alice = privateKeyWrapper('//Alice');