git.delta.rocks / unique-network / refs/commits / 0c9dfc515d68

difftreelog

CORE-302 Implement methods for setup collection.

Trubnikov Sergey2022-04-21parent: #d7a043d.patch.diff
in: master

8 files changed

modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -115,7 +115,7 @@
 		})
 	}
 
-	pub fn new_with_recorder(id: CollectionId, recorder: Rc<SubstrateRecorder<T>>) -> Option<Self> {
+	pub fn new_with_recorder(id: CollectionId, recorder: SubstrateRecorder<T>) -> Option<Self> {
 		<CollectionById<T>>::get(id).map(|collection| Self {
 			id,
 			collection,
@@ -153,6 +153,15 @@
 	pub fn set_sponsor(&mut self, sponsor: T::AccountId) {
 		self.collection.sponsorship = SponsorshipState::Unconfirmed(sponsor);
 	}
+	
+	pub fn confirm_sponsorship(&mut self, sender: &T::AccountId) -> bool {
+		if self.collection.sponsorship.pending_sponsor() != Some(sender) {
+			return false;
+		};
+
+		self.collection.sponsorship = SponsorshipState::Confirmed(sender.clone());
+		true
+	}
 }
 impl<T: Config> Deref for CollectionHandle<T> {
 	type Target = Collection<T::AccountId>;
modifiedpallets/evm-collection/src/eth.rsdiffbeforeafterboth
17use core::marker::PhantomData;17use core::marker::PhantomData;
18use evm_coder::{abi::AbiWriter, execution::*, generate_stubgen, solidity_interface, types::*, ToLog};18use evm_coder::{abi::AbiWriter, execution::*, generate_stubgen, solidity_interface, types::*, ToLog};
19use ethereum as _;19use ethereum as _;
20use pallet_common::{CollectionById, CollectionHandle};20use pallet_common::CollectionById;
21use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};21use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};
22use pallet_evm::{22use pallet_evm::{
23 ExitRevert, OnCreate, OnMethodCall, PrecompileResult, PrecompileFailure,23 ExitRevert, OnCreate, OnMethodCall, PrecompileResult, PrecompileFailure,
26use sp_core::H160;26use sp_core::H160;
27use up_data_structs::{27use up_data_structs::{
28 CreateCollectionData, MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH,28 CreateCollectionData, MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH,
29 MAX_COLLECTION_NAME_LENGTH, SponsorshipState,29 MAX_COLLECTION_NAME_LENGTH,
30};30};
31use crate::{Config, Pallet};31use crate::{Config, Pallet};
32use frame_support::traits::Get;32use frame_support::traits::Get;
103 Ok(address)102 Ok(address)
104 }103 }
105104
106 fn set_sponsor(105 // fn set_sponsor(collection_id: address, sponsor: address) -> Result<void> {
107 &self,106 // let collection_id =
108 caller: caller,107 // pallet_common::eth::map_eth_to_id(&collection_id).ok_or(Error::Revert("".into()))?;
109 contract_address: address,108 // let mut collection = <CollectionById<T>>::get(collection_id).ok_or(Error::Revert("".into()))?;
110 sponsor: address,109 // let sponsor = T::CrossAccountId::from_eth(sponsor);
111 ) -> Result<void> {110 // collection.sponsorship = SponsorshipState::Unconfirmed(sponsor.as_sub().clone());
112 let collection_id =111 // <CollectionById<T>>::insert(collection_id, collection);
113 pallet_common::eth::map_eth_to_id(&contract_address).ok_or(Error::Revert("".into()))?;112 // Ok(())
114 let mut collection =113 // }
115 pallet_common::CollectionHandle::new_with_recorder(collection_id, self.0.clone())
116 .ok_or(Error::Revert("".into()))?;
117
118 let caller = T::CrossAccountId::from_eth(caller);
119 collection.check_is_owner(&caller).map_err(|e| Error::Revert(format!("{:?}", e)))?;
120
121 let sponsor = T::CrossAccountId::from_eth(sponsor);
122 collection.set_sponsor(sponsor.as_sub().clone());
123 collection
124 .save()
125 .map_err(|e| Error::Revert(format!("{:?}", e)))
126 }
127114
128 // fn set_offchain_shema(shema: string) -> Result<void> {115 // fn set_offchain_shema(shema: string) -> Result<void> {
129 // Ok(())116 // Ok(())
168 return None;155 return None;
169 }156 }
170157
171 let helpers = EvmCollection::<T>(SubstrateRecorder::<T>::new(gas_left));158 let helpers = EvmCollection::<T>(SubstrateRecorder::new(gas_left));
172 pallet_evm_coder_substrate::call(*source, helpers, value, input)159 pallet_evm_coder_substrate::call(*source, helpers, value, input)
173 }160 }
174161
modifiedpallets/evm-collection/src/stubs/Collection.rawdiffbeforeafterboth

binary blob — no preview

modifiedpallets/evm-collection/src/stubs/Collection.soldiffbeforeafterboth
--- a/pallets/evm-collection/src/stubs/Collection.sol
+++ b/pallets/evm-collection/src/stubs/Collection.sol
@@ -21,7 +21,7 @@
 	}
 }
 
-// Selector: 6503bbc2
+// Selector: d32d5104
 contract Collection is Dummy, ERC165 {
 	// Selector: create721Collection(string,string,string) 951c0151
 	function create721Collection(
@@ -38,10 +38,53 @@
 	}
 
 	// Selector: setSponsor(address,address) f01fba93
-	function setSponsor(address contractAddress, address sponsor) public view {
+	function setSponsor(address collectionAddress, address sponsor)
+		public
+		view
+	{
 		require(false, stub_error);
-		contractAddress;
+		collectionAddress;
 		sponsor;
 		dummy;
 	}
+
+	// Selector: confirmSponsorship(address) abc00001
+	function confirmSponsorship(address collectionAddress) public view {
+		require(false, stub_error);
+		collectionAddress;
+		dummy;
+	}
+
+	// Selector: setOffchainShema(address,string) d7dc2de3
+	function setOffchainShema(address collectionAddress, string memory shema)
+		public
+		view
+	{
+		require(false, stub_error);
+		collectionAddress;
+		shema;
+		dummy;
+	}
+
+	// Selector: setVariableOnChainSchema(address,string) 582691c3
+	function setVariableOnChainSchema(
+		address collectionAddress,
+		string memory variable
+	) public view {
+		require(false, stub_error);
+		collectionAddress;
+		variable;
+		dummy;
+	}
+
+	// Selector: setConstOnChainSchema(address,string) 921456e7
+	function setConstOnChainSchema(
+		address collectionAddress,
+		string memory constOnChain
+	) public view {
+		require(false, stub_error);
+		collectionAddress;
+		constOnChain;
+		dummy;
+	}
 }
modifiedpallets/unique/src/lib.rsdiffbeforeafterboth
--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -544,11 +544,9 @@
 
 			let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
 			ensure!(
-				target_collection.sponsorship.pending_sponsor() == Some(&sender),
+				target_collection.confirm_sponsorship(&sender),
 				Error::<T>::ConfirmUnsetSponsorFail
 			);
-
-			target_collection.sponsorship = SponsorshipState::Confirmed(sender.clone());
 
 			<Pallet<T>>::deposit_event(Event::<T>::SponsorshipConfirmed(
 				collection_id,
modifiedtests/src/eth/api/Collection.soldiffbeforeafterboth
--- a/tests/src/eth/api/Collection.sol
+++ b/tests/src/eth/api/Collection.sol
@@ -12,7 +12,7 @@
 	function supportsInterface(bytes4 interfaceID) external view returns (bool);
 }
 
-// Selector: 6503bbc2
+// Selector: d32d5104
 interface Collection is Dummy, ERC165 {
 	// Selector: create721Collection(string,string,string) 951c0151
 	function create721Collection(
@@ -22,5 +22,27 @@
 	) external view returns (address);
 
 	// Selector: setSponsor(address,address) f01fba93
-	function setSponsor(address contractAddress, address sponsor) external view;
+	function setSponsor(address collectionAddress, address sponsor)
+		external
+		view;
+
+	// Selector: confirmSponsorship(address) abc00001
+	function confirmSponsorship(address collectionAddress) external view;
+
+	// Selector: setOffchainShema(address,string) d7dc2de3
+	function setOffchainShema(address collectionAddress, string memory shema)
+		external
+		view;
+
+	// Selector: setVariableOnChainSchema(address,string) 582691c3
+	function setVariableOnChainSchema(
+		address collectionAddress,
+		string memory variable
+	) external view;
+
+	// Selector: setConstOnChainSchema(address,string) 921456e7
+	function setConstOnChainSchema(
+		address collectionAddress,
+		string memory constOnChain
+	) external view;
 }
modifiedtests/src/eth/collectionAbi.jsondiffbeforeafterboth
--- a/tests/src/eth/collectionAbi.json
+++ b/tests/src/eth/collectionAbi.json
@@ -1,6 +1,19 @@
 [
   {
     "inputs": [
+      {
+        "internalType": "address",
+        "name": "collectionAddress",
+        "type": "address"
+      }
+    ],
+    "name": "confirmSponsorship",
+    "outputs": [],
+    "stateMutability": "view",
+    "type": "function"
+  },
+  {
+    "inputs": [
       { "internalType": "string", "name": "name", "type": "string" },
       { "internalType": "string", "name": "description", "type": "string" },
       { "internalType": "string", "name": "tokenPrefix", "type": "string" }
@@ -14,7 +27,35 @@
     "inputs": [
       {
         "internalType": "address",
-        "name": "contractAddress",
+        "name": "collectionAddress",
+        "type": "address"
+      },
+      { "internalType": "string", "name": "constOnChain", "type": "string" }
+    ],
+    "name": "setConstOnChainSchema",
+    "outputs": [],
+    "stateMutability": "view",
+    "type": "function"
+  },
+  {
+    "inputs": [
+      {
+        "internalType": "address",
+        "name": "collectionAddress",
+        "type": "address"
+      },
+      { "internalType": "string", "name": "shema", "type": "string" }
+    ],
+    "name": "setOffchainShema",
+    "outputs": [],
+    "stateMutability": "view",
+    "type": "function"
+  },
+  {
+    "inputs": [
+      {
+        "internalType": "address",
+        "name": "collectionAddress",
         "type": "address"
       },
       { "internalType": "address", "name": "sponsor", "type": "address" }
@@ -26,6 +67,20 @@
   },
   {
     "inputs": [
+      {
+        "internalType": "address",
+        "name": "collectionAddress",
+        "type": "address"
+      },
+      { "internalType": "string", "name": "variable", "type": "string" }
+    ],
+    "name": "setVariableOnChainSchema",
+    "outputs": [],
+    "stateMutability": "view",
+    "type": "function"
+  },
+  {
+    "inputs": [
       { "internalType": "bytes4", "name": "interfaceID", "type": "bytes4" }
     ],
     "name": "supportsInterface",
modifiedtests/src/eth/createCollection.test.tsdiffbeforeafterboth
--- a/tests/src/eth/createCollection.test.ts
+++ b/tests/src/eth/createCollection.test.ts
@@ -57,10 +57,47 @@
     const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);
     const sponsor = await createEthAccountWithBalance(api, web3);
     result = await helper.methods.setSponsor(collectionIdAddress, sponsor).send();
-    const collection = (await getDetailedCollectionInfo(api, collectionId))!;
+    let collection = (await getDetailedCollectionInfo(api, collectionId))!;
     expect(collection.sponsorship.isUnconfirmed).to.be.true;
     expect(collection.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));
+    await expect(helper.methods.confirmSponsorship(collectionIdAddress).call()).to.be.rejectedWith('Caller is not set as sponsor');
+    const sponsorHelper = collectionHelper(web3, sponsor);
+    await sponsorHelper.methods.confirmSponsorship(collectionIdAddress).send();
+    collection = (await getDetailedCollectionInfo(api, collectionId))!;
+    expect(collection.sponsorship.isConfirmed).to.be.true;
+    expect(collection.sponsorship.asConfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));
+  });
+  
+  itWeb3('Set offchain shema', async ({api, web3}) => {
+    const owner = await createEthAccountWithBalance(api, web3);
+    const helper = collectionHelper(web3, owner);
+    let result = await helper.methods.create721Collection('Shema collection', '2', '2').send();
+    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);
+    const shema = 'Some shema';
+    result = await helper.methods.setOffchainShema(collectionIdAddress, shema).send();
+    const collection = (await getDetailedCollectionInfo(api, collectionId))!;
+    expect(collection.offchainSchema.toHuman()).to.be.eq(shema);
   });
-
-
+  
+  itWeb3('Set variable on chain schema', async ({api, web3}) => {
+    const owner = await createEthAccountWithBalance(api, web3);
+    const helper = collectionHelper(web3, owner);
+    let result = await helper.methods.create721Collection('Variable collection', '3', '3').send();
+    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);
+    const variable = 'Some variable';
+    result = await helper.methods.setVariableOnChainSchema(collectionIdAddress, variable).send();
+    const collection = (await getDetailedCollectionInfo(api, collectionId))!;
+    expect(collection.variableOnChainSchema.toHuman()).to.be.eq(variable);
+  });
+  
+  itWeb3('Set const on chain schema', async ({api, web3}) => {
+    const owner = await createEthAccountWithBalance(api, web3);
+    const helper = collectionHelper(web3, owner);
+    let result = await helper.methods.create721Collection('Const collection', '4', '4').send();
+    const {collectionIdAddress, collectionId} = await getCollectionAddressFromResult(api, result);
+    const constShema = 'Some const';
+    result = await helper.methods.setConstOnChainSchema(collectionIdAddress, constShema).send();
+    const collection = (await getDetailedCollectionInfo(api, collectionId))!;
+    expect(collection.constOnChainSchema.toHuman()).to.be.eq(constShema);
+  });
 });
\ No newline at end of file