git.delta.rocks / unique-network / refs/commits / d7a043d9818f

difftreelog

CORE-302 Implement setSponsor method.

Trubnikov Sergey2022-04-20parent: #281abcb.patch.diff
in: master

10 files changed

modifiedMakefilediffbeforeafterboth
--- a/Makefile
+++ b/Makefile
@@ -21,7 +21,7 @@
 TESTS_API=./tests/src/eth/api/
 
 .PHONY: regenerate_solidity
-regenerate_solidity: UniqueFungible.sol UniqueNFT.sol ContractHelpers.sol
+regenerate_solidity: UniqueFungible.sol UniqueNFT.sol ContractHelpers.sol Collection.sol
 
 UniqueFungible.sol:
 	PACKAGE=pallet-fungible NAME=erc::gen_iface OUTPUT=$(TESTS_API)/$@ ./.maintain/scripts/generate_sol.sh
@@ -36,8 +36,8 @@
 	PACKAGE=pallet-evm-contract-helpers NAME=eth::contract_helpers_impl OUTPUT=$(CONTRACT_HELPERS_STUBS)/$@ ./.maintain/scripts/generate_sol.sh
 
 Collection.sol:
-	PACKAGE=pallet-evm-collection NAME=eth::contract_helpers_iface OUTPUT=$(TESTS_API)/$@ ./.maintain/scripts/generate_sol.sh
-	PACKAGE=pallet-evm-collection NAME=eth::contract_helpers_impl OUTPUT=$(COLLECTION_STUBS)/$@ ./.maintain/scripts/generate_sol.sh
+	PACKAGE=pallet-evm-collection NAME=eth::collection_iface OUTPUT=$(TESTS_API)/$@ ./.maintain/scripts/generate_sol.sh
+	PACKAGE=pallet-evm-collection NAME=eth::collection_impl OUTPUT=$(COLLECTION_STUBS)/$@ ./.maintain/scripts/generate_sol.sh
 
 UniqueFungible: UniqueFungible.sol
 	INPUT=$(FUNGIBLE_EVM_STUBS)/$< OUTPUT=$(FUNGIBLE_EVM_STUBS)/UniqueFungible.raw ./.maintain/scripts/compile_stub.sh
modifiedpallets/common/src/lib.rsdiffbeforeafterboth
--- a/pallets/common/src/lib.rs
+++ b/pallets/common/src/lib.rs
@@ -114,6 +114,15 @@
 			recorder: SubstrateRecorder::new(gas_limit),
 		})
 	}
+
+	pub fn new_with_recorder(id: CollectionId, recorder: Rc<SubstrateRecorder<T>>) -> Option<Self> {
+		<CollectionById<T>>::get(id).map(|collection| Self {
+			id,
+			collection,
+			recorder,
+		})
+	}
+
 	pub fn new(id: CollectionId) -> Option<Self> {
 		Self::new_with_gas_limit(id, u64::MAX)
 	}
@@ -140,6 +149,10 @@
 		<CollectionById<T>>::insert(self.id, self.collection);
 		Ok(())
 	}
+
+	pub fn set_sponsor(&mut self, sponsor: T::AccountId) {
+		self.collection.sponsorship = SponsorshipState::Unconfirmed(sponsor);
+	}
 }
 impl<T: Config> Deref for CollectionHandle<T> {
 	type Target = Collection<T::AccountId>;
modifiedpallets/evm-collection/src/eth.rsdiffbeforeafterboth
--- a/pallets/evm-collection/src/eth.rs
+++ b/pallets/evm-collection/src/eth.rs
@@ -17,7 +17,7 @@
 use core::marker::PhantomData;
 use evm_coder::{abi::AbiWriter, execution::*, generate_stubgen, solidity_interface, types::*, ToLog};
 use ethereum as _;
-use pallet_common::CollectionById;
+use pallet_common::{CollectionById, CollectionHandle};
 use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};
 use pallet_evm::{
 	ExitRevert, OnCreate, OnMethodCall, PrecompileResult, PrecompileFailure,
@@ -26,7 +26,7 @@
 use sp_core::H160;
 use up_data_structs::{
 	CreateCollectionData, MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH,
-	MAX_COLLECTION_NAME_LENGTH,
+	MAX_COLLECTION_NAME_LENGTH, SponsorshipState,
 };
 use crate::{Config, Pallet};
 use frame_support::traits::Get;
@@ -57,6 +57,7 @@
 
 #[solidity_interface(name = "Collection")]
 impl<T: Config> EvmCollection<T> {
+
 	fn create_721_collection(
 		&self,
 		caller: caller,
@@ -102,15 +103,27 @@
 		Ok(address)
 	}
 
-	// fn set_sponsor(collection_id: address, sponsor: address) -> Result<void> {
-	// 	let collection_id =
-	// 		pallet_common::eth::map_eth_to_id(&collection_id).ok_or(Error::Revert("".into()))?;
-	// 	let mut collection = <CollectionById<T>>::get(collection_id).ok_or(Error::Revert("".into()))?;
-	// 	let sponsor = T::CrossAccountId::from_eth(sponsor);
-	// 	collection.sponsorship = SponsorshipState::Unconfirmed(sponsor.as_sub().clone());
-	// 	<CollectionById<T>>::insert(collection_id, collection);
-	// 	Ok(())
-	// }
+	fn set_sponsor(
+		&self,
+		caller: caller,
+		contract_address: address,
+		sponsor: address,
+	) -> Result<void> {
+		let collection_id =
+			pallet_common::eth::map_eth_to_id(&contract_address).ok_or(Error::Revert("".into()))?;
+		let mut collection =
+			pallet_common::CollectionHandle::new_with_recorder(collection_id, self.0.clone())
+				.ok_or(Error::Revert("".into()))?;
+		
+		let caller = T::CrossAccountId::from_eth(caller);
+		collection.check_is_owner(&caller).map_err(|e| Error::Revert(format!("{:?}", e)))?;
+
+		let sponsor = T::CrossAccountId::from_eth(sponsor);
+		collection.set_sponsor(sponsor.as_sub().clone());
+		collection
+			.save()
+			.map_err(|e| Error::Revert(format!("{:?}", e)))
+	}
 
 	// fn set_offchain_shema(shema: string) -> Result<void> {
 	// 	Ok(())
modifiedpallets/evm-collection/src/stubs/Collection.rawdiffbeforeafterboth

binary blob — no preview

modifiedpallets/evm-collection/src/stubs/Collection.soldiffbeforeafterboth
21 }21 }
22}22}
2323
24// Selector: ee5467a824// Selector: 6503bbc2
25contract Collection is Dummy, ERC165 {25contract Collection is Dummy, ERC165 {
26 // Selector: contractOwner(address) 5152b14c26 // Selector: create721Collection(string,string,string) 951c0151
27 function contractOwner(address contractAddress)
28 public
29 view
30 returns (address)
31 {
32 require(false, stub_error);
33 contractAddress;
34 dummy;
35 return 0x0000000000000000000000000000000000000000;
36 }
37
38 // Selector: sponsoringEnabled(address) 6027dc61
39 function sponsoringEnabled(address contractAddress)27 function create721Collection(
40 public
41 view
42 returns (bool)
43 {
44 require(false, stub_error);28 string memory name,
45 contractAddress;
46 dummy;
47 return false;
48 }
49
50 // Deprecated
51 //
52 // Selector: toggleSponsoring(address,bool) fcac6d86
53 function toggleSponsoring(address contractAddress, bool enabled) public {29 string memory description,
54 require(false, stub_error);30 string memory tokenPrefix
55 contractAddress;
56 enabled;
57 dummy = 0;
58 }
59
60 // Selector: setSponsoringMode(address,uint8) fde8a560
61 function setSponsoringMode(address contractAddress, uint8 mode) public {
62 require(false, stub_error);
63 contractAddress;
64 mode;
65 dummy = 0;
66 }
67
68 // Selector: sponsoringMode(address) b70c7267
69 function sponsoringMode(address contractAddress)
70 public
71 view31 ) public view returns (address) {
72 returns (uint8)
73 {
74 require(false, stub_error);
75 contractAddress;
76 dummy;
77 return 0;
78 }
79
80 // Selector: setSponsoringRateLimit(address,uint32) 77b6c908
81 function setSponsoringRateLimit(address contractAddress, uint32 rateLimit)
82 public
83 {
84 require(false, stub_error);32 require(false, stub_error);
85 contractAddress;33 name;
86 rateLimit;34 description;
87 dummy = 0;
88 }
89
90 // Selector: getSponsoringRateLimit(address) 610cfabd
91 function getSponsoringRateLimit(address contractAddress)
92 public
93 view
94 returns (uint32)
95 {
96 require(false, stub_error);35 tokenPrefix;
97 contractAddress;
98 dummy;36 dummy;
99 return 0;37 return 0x0000000000000000000000000000000000000000;
100 }38 }
10139
102 // Selector: allowed(address,address) 5c65816540 // Selector: setSponsor(address,address) f01fba93
103 function allowed(address contractAddress, address user)41 function setSponsor(address contractAddress, address sponsor) public view {
104 public
105 view
106 returns (bool)
107 {
108 require(false, stub_error);42 require(false, stub_error);
109 contractAddress;43 contractAddress;
110 user;44 sponsor;
111 dummy;45 dummy;
112 return false;
113 }46 }
114
115 // Selector: allowlistEnabled(address) c772ef6c
116 function allowlistEnabled(address contractAddress)
117 public
118 view
119 returns (bool)
120 {
121 require(false, stub_error);
122 contractAddress;
123 dummy;
124 return false;
125 }
126
127 // Selector: toggleAllowlist(address,bool) 36de20f5
128 function toggleAllowlist(address contractAddress, bool enabled) public {
129 require(false, stub_error);
130 contractAddress;
131 enabled;
132 dummy = 0;
133 }
134
135 // Selector: toggleAllowed(address,address,bool) 4706cc1c
136 function toggleAllowed(
137 address contractAddress,
138 address user,
139 bool allowed
140 ) public {
141 require(false, stub_error);
142 contractAddress;
143 user;
144 allowed;
145 dummy = 0;
146 }
147
148 // Selector: create721Collection(string,string,string) 951c0151
149 function create721Collection(
150 string memory name,
151 string memory description,
152 string memory tokenPrefix
153 ) public view returns (address) {
154 require(false, stub_error);
155 name;
156 description;
157 tokenPrefix;
158 dummy;
159 return 0x0000000000000000000000000000000000000000;
160 }
161}47}
16248
modifiedpallets/unique/src/lib.rsdiffbeforeafterboth
--- a/pallets/unique/src/lib.rs
+++ b/pallets/unique/src/lib.rs
@@ -520,7 +520,7 @@
 			let mut target_collection = <CollectionHandle<T>>::try_get(collection_id)?;
 			target_collection.check_is_owner(&sender)?;
 
-			target_collection.sponsorship = SponsorshipState::Unconfirmed(new_sponsor.clone());
+			target_collection.set_sponsor(new_sponsor.clone());
 
 			<Pallet<T>>::deposit_event(Event::<T>::CollectionSponsorSet(
 				collection_id,
modifiedtests/src/eth/api/Collection.soldiffbeforeafterboth
--- a/tests/src/eth/api/Collection.sol
+++ b/tests/src/eth/api/Collection.sol
@@ -12,70 +12,15 @@
 	function supportsInterface(bytes4 interfaceID) external view returns (bool);
 }
 
-// Selector: ee5467a8
+// Selector: 6503bbc2
 interface Collection is Dummy, ERC165 {
-	// Selector: contractOwner(address) 5152b14c
-	function contractOwner(address contractAddress)
-		external
-		view
-		returns (address);
-
-	// Selector: sponsoringEnabled(address) 6027dc61
-	function sponsoringEnabled(address contractAddress)
-		external
-		view
-		returns (bool);
-
-	// Deprecated
-	//
-	// Selector: toggleSponsoring(address,bool) fcac6d86
-	function toggleSponsoring(address contractAddress, bool enabled) external;
-
-	// Selector: setSponsoringMode(address,uint8) fde8a560
-	function setSponsoringMode(address contractAddress, uint8 mode) external;
-
-	// Selector: sponsoringMode(address) b70c7267
-	function sponsoringMode(address contractAddress)
-		external
-		view
-		returns (uint8);
-
-	// Selector: setSponsoringRateLimit(address,uint32) 77b6c908
-	function setSponsoringRateLimit(address contractAddress, uint32 rateLimit)
-		external;
-
-	// Selector: getSponsoringRateLimit(address) 610cfabd
-	function getSponsoringRateLimit(address contractAddress)
-		external
-		view
-		returns (uint32);
-
-	// Selector: allowed(address,address) 5c658165
-	function allowed(address contractAddress, address user)
-		external
-		view
-		returns (bool);
-
-	// Selector: allowlistEnabled(address) c772ef6c
-	function allowlistEnabled(address contractAddress)
-		external
-		view
-		returns (bool);
-
-	// Selector: toggleAllowlist(address,bool) 36de20f5
-	function toggleAllowlist(address contractAddress, bool enabled) external;
-
-	// Selector: toggleAllowed(address,address,bool) 4706cc1c
-	function toggleAllowed(
-		address contractAddress,
-		address user,
-		bool allowed
-	) external;
-
 	// Selector: create721Collection(string,string,string) 951c0151
 	function create721Collection(
 		string memory name,
 		string memory description,
 		string memory tokenPrefix
 	) external view returns (address);
+
+	// Selector: setSponsor(address,address) f01fba93
+	function setSponsor(address contractAddress, address sponsor) external view;
 }
modifiedtests/src/eth/collectionAbi.jsondiffbeforeafterboth
--- a/tests/src/eth/collectionAbi.json
+++ b/tests/src/eth/collectionAbi.json
@@ -1,65 +1,12 @@
 [
   {
     "inputs": [
-      {
-        "internalType": "address",
-        "name": "contractAddress",
-        "type": "address"
-      },
-      { "internalType": "address", "name": "user", "type": "address" }
-    ],
-    "name": "allowed",
-    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
-    "stateMutability": "view",
-    "type": "function"
-  },
-  {
-    "inputs": [
-      {
-        "internalType": "address",
-        "name": "contractAddress",
-        "type": "address"
-      }
-    ],
-    "name": "allowlistEnabled",
-    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
-    "stateMutability": "view",
-    "type": "function"
-  },
-  {
-    "inputs": [
-      {
-        "internalType": "address",
-        "name": "contractAddress",
-        "type": "address"
-      }
-    ],
-    "name": "contractOwner",
-    "outputs": [{ "internalType": "address", "name": "", "type": "address" }],
-    "stateMutability": "view",
-    "type": "function"
-  },
-  {
-    "inputs": [
       { "internalType": "string", "name": "name", "type": "string" },
       { "internalType": "string", "name": "description", "type": "string" },
       { "internalType": "string", "name": "tokenPrefix", "type": "string" }
     ],
     "name": "create721Collection",
     "outputs": [{ "internalType": "address", "name": "", "type": "address" }],
-    "stateMutability": "view",
-    "type": "function"
-  },
-  {
-    "inputs": [
-      {
-        "internalType": "address",
-        "name": "contractAddress",
-        "type": "address"
-      }
-    ],
-    "name": "getSponsoringRateLimit",
-    "outputs": [{ "internalType": "uint32", "name": "", "type": "uint32" }],
     "stateMutability": "view",
     "type": "function"
   },
@@ -70,103 +17,20 @@
         "name": "contractAddress",
         "type": "address"
       },
-      { "internalType": "uint8", "name": "mode", "type": "uint8" }
-    ],
-    "name": "setSponsoringMode",
-    "outputs": [],
-    "stateMutability": "nonpayable",
-    "type": "function"
-  },
-  {
-    "inputs": [
-      {
-        "internalType": "address",
-        "name": "contractAddress",
-        "type": "address"
-      },
-      { "internalType": "uint32", "name": "rateLimit", "type": "uint32" }
+      { "internalType": "address", "name": "sponsor", "type": "address" }
     ],
-    "name": "setSponsoringRateLimit",
+    "name": "setSponsor",
     "outputs": [],
-    "stateMutability": "nonpayable",
-    "type": "function"
-  },
-  {
-    "inputs": [
-      {
-        "internalType": "address",
-        "name": "contractAddress",
-        "type": "address"
-      }
-    ],
-    "name": "sponsoringEnabled",
-    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
     "stateMutability": "view",
     "type": "function"
   },
   {
     "inputs": [
-      {
-        "internalType": "address",
-        "name": "contractAddress",
-        "type": "address"
-      }
-    ],
-    "name": "sponsoringMode",
-    "outputs": [{ "internalType": "uint8", "name": "", "type": "uint8" }],
-    "stateMutability": "view",
-    "type": "function"
-  },
-  {
-    "inputs": [
       { "internalType": "bytes4", "name": "interfaceID", "type": "bytes4" }
     ],
     "name": "supportsInterface",
     "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
     "stateMutability": "view",
-    "type": "function"
-  },
-  {
-    "inputs": [
-      {
-        "internalType": "address",
-        "name": "contractAddress",
-        "type": "address"
-      },
-      { "internalType": "address", "name": "user", "type": "address" },
-      { "internalType": "bool", "name": "allowed", "type": "bool" }
-    ],
-    "name": "toggleAllowed",
-    "outputs": [],
-    "stateMutability": "nonpayable",
-    "type": "function"
-  },
-  {
-    "inputs": [
-      {
-        "internalType": "address",
-        "name": "contractAddress",
-        "type": "address"
-      },
-      { "internalType": "bool", "name": "enabled", "type": "bool" }
-    ],
-    "name": "toggleAllowlist",
-    "outputs": [],
-    "stateMutability": "nonpayable",
-    "type": "function"
-  },
-  {
-    "inputs": [
-      {
-        "internalType": "address",
-        "name": "contractAddress",
-        "type": "address"
-      },
-      { "internalType": "bool", "name": "enabled", "type": "bool" }
-    ],
-    "name": "toggleSponsoring",
-    "outputs": [],
-    "stateMutability": "nonpayable",
     "type": "function"
   }
 ]
modifiedtests/src/eth/createCollection.test.tsdiffbeforeafterboth
--- a/tests/src/eth/createCollection.test.ts
+++ b/tests/src/eth/createCollection.test.ts
@@ -14,32 +14,53 @@
 // You should have received a copy of the GNU General Public License
 // along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
 
+import {ApiPromise} from '@polkadot/api';
+import {evmToAddress} from '@polkadot/util-crypto';
 import {expect} from 'chai';
 import {getCreatedCollectionCount, getDetailedCollectionInfo} from '../util/helpers';
-import {collectionHelper, collectionIdFromAddress, contractHelpers, createEthAccountWithBalance, itWeb3} from './util/helpers';
+import {collectionHelper, collectionIdFromAddress, createEthAccountWithBalance, itWeb3, normalizeAddress} from './util/helpers';
 
+async function getCollectionAddressFromResult(api: ApiPromise, result: any) {
+  const collectionIdAddress = normalizeAddress(result.events[0].raw.topics[2]);
+  const collectionId = collectionIdFromAddress(collectionIdAddress);  
+  const collection = (await getDetailedCollectionInfo(api, collectionId))!;
+  return {collectionIdAddress, collectionId, collection};
+}
+
 describe('Create collection from EVM', () => {
   itWeb3('Create collection', async ({api, web3}) => {
     const owner = await createEthAccountWithBalance(api, web3);
-    const helpers = collectionHelper(web3, owner);
+    const helper = collectionHelper(web3, owner);
     const collectionName = 'CollectionEVM';
     const description = 'Some description';
     const tokenPrefix = 'token prefix';
   
     const collectionCountBefore = await getCreatedCollectionCount(api);
-    const result = await helpers.methods
+    const result = await helper.methods
       .create721Collection(collectionName, description, tokenPrefix)
       .send();
     const collectionCountAfter = await getCreatedCollectionCount(api);
   
-    const collectionId = collectionIdFromAddress(result.events[0].raw.topics[2]);
+    const {collectionId, collection} = await getCollectionAddressFromResult(api, result);
     expect(collectionCountAfter - collectionCountBefore).to.be.eq(1);
     expect(collectionId).to.be.eq(collectionCountAfter);
-      
-    const collection = (await getDetailedCollectionInfo(api, collectionId))!;
     expect(collection.name.map(v => String.fromCharCode(v.toNumber())).join('')).to.be.eq(collectionName);
     expect(collection.description.map(v => String.fromCharCode(v.toNumber())).join('')).to.be.eq(description);
     expect(collection.tokenPrefix.toHuman()).to.be.eq(tokenPrefix);
     expect(collection.schemaVersion.type).to.be.eq('ImageURL');
   });
+  
+  itWeb3('Set sponsorship', async ({api, web3}) => {
+    const owner = await createEthAccountWithBalance(api, web3);
+    const helper = collectionHelper(web3, owner);
+    let result = await helper.methods.create721Collection('Sponsor collection', '1', '1').send();
+    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))!;
+    expect(collection.sponsorship.isUnconfirmed).to.be.true;
+    expect(collection.sponsorship.asUnconfirmed.toHuman()).to.be.eq(evmToAddress(sponsor));
+  });
+
+
 });
\ No newline at end of file
modifiedtests/src/eth/util/helpers.tsdiffbeforeafterboth
--- a/tests/src/eth/util/helpers.ts
+++ b/tests/src/eth/util/helpers.ts
@@ -74,7 +74,15 @@
   return Web3.utils.toChecksumAddress('0x' + buf.toString('hex'));
 }
 export function collectionIdFromAddress(address: string): number {
-  return Number('0x' + address.substring(address.length - 8));
+  if (!address.startsWith('0x'))
+    throw 'address not starts with "0x"';
+  if (address.length > 42)
+    throw 'address length is more than 20 bytes';
+    return Number('0x' + address.substring(address.length - 8));
+}
+  
+export function normalizeAddress(address: string): string {
+  return '0x' + address.substring(address.length - 40);
 }
 
 export function tokenIdToAddress(collection: number, token: number): string {