git.delta.rocks / unique-network / refs/commits / 683924f8b65f

difftreelog

feat log recording

Yaroslav Bolyukin2021-04-30parent: #ab6ae76.patch.diff
in: master

2 files changed

modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
43};43};
44use sp_runtime::traits::StaticLookup;44use sp_runtime::traits::StaticLookup;
45use pallet_contracts::chain_extension::UncheckedFrom;45use pallet_contracts::chain_extension::UncheckedFrom;
46use pallet_evm::AddressMapping;46use pallet_ethereum::EthereumTransactionSender;
47use pallet_transaction_payment::OnChargeTransaction;47use pallet_transaction_payment::OnChargeTransaction;
4848
49#[cfg(test)]49#[cfg(test)]
185pub struct CollectionHandle<T: Config> {185pub struct CollectionHandle<T: Config> {
186 pub id: CollectionId,186 pub id: CollectionId,
187 collection: Collection<T>,187 collection: Collection<T>,
188 logs: eth::log::LogRecorder,
188}189}
190impl<T: Config> CollectionHandle<T> {
191 pub fn get(id: CollectionId) -> Option<Self> {
192 <CollectionById<T>>::get(id)
193 .map(|collection| Self {
194 id,
195 collection,
196 logs: eth::log::LogRecorder::default(),
197 })
198 }
199 pub fn log(&self, topics: Vec<H256>, data: eth::abi::AbiWriter) {
200 self.logs.log(topics, data)
201 }
202 pub fn into_inner(self) -> Collection<T> {
203 self.collection.clone()
204 }
205}
189206
190impl<T: Config> Deref for CollectionHandle<T> {207impl<T: Config> Deref for CollectionHandle<T> {
191 type Target = Collection<T>;208 type Target = Collection<T>;
472 type CollectionCreationPrice: Get<<<Self as Config>::Currency as Currency<Self::AccountId>>::Balance>;489 type CollectionCreationPrice: Get<<<Self as Config>::Currency as Currency<Self::AccountId>>::Balance>;
473 type TreasuryAccountId: Get<Self::AccountId>;490 type TreasuryAccountId: Get<Self::AccountId>;
491
492 type EthereumChainId: Get<u64>;
493 type EthereumTransactionSender: pallet_ethereum::EthereumTransactionSender;
474}494}
475495
476#[cfg(feature = "runtime-benchmarks")]496#[cfg(feature = "runtime-benchmarks")]
1125 Self::validate_create_item_args(&target_collection, &data)?;1145 Self::validate_create_item_args(&target_collection, &data)?;
1126 Self::create_item_no_validation(&target_collection, owner, data)?;1146 Self::create_item_no_validation(&target_collection, owner, data)?;
11271147
1148 Self::submit_logs(target_collection)?;
1128 Ok(())1149 Ok(())
1129 }1150 }
11301151
11581179
1159 Self::create_multiple_items_internal(sender, &collection, owner, items_data)?;1180 Self::create_multiple_items_internal(sender, &collection, owner, items_data)?;
11601181
1182 Self::submit_logs(collection)?;
1161 Ok(())1183 Ok(())
1162 }1184 }
11631185
11831205
1184 Self::burn_item_internal(&sender, &target_collection, item_id, value)?;1206 Self::burn_item_internal(&sender, &target_collection, item_id, value)?;
11851207
1208 Self::submit_logs(target_collection)?;
1186 Ok(())1209 Ok(())
1187 }1210 }
11881211
12171240
1218 Self::transfer_internal(sender, recipient, &collection, item_id, value)?;1241 Self::transfer_internal(sender, recipient, &collection, item_id, value)?;
12191242
1243 Self::submit_logs(collection)?;
1220 Ok(())1244 Ok(())
1221 }1245 }
12221246
12441268
1245 Self::approve_internal(sender, spender, &collection, item_id, amount)?;1269 Self::approve_internal(sender, spender, &collection, item_id, amount)?;
12461270
1271 Self::submit_logs(collection)?;
1247 Ok(())1272 Ok(())
1248 }1273 }
1249 1274
12751300
1276 Self::transfer_from_internal(sender, from, recipient, &collection, item_id, value)?;1301 Self::transfer_from_internal(sender, from, recipient, &collection, item_id, value)?;
12771302
1303 Self::submit_logs(collection)?;
1278 Ok(())1304 Ok(())
1279 }1305 }
12801306
1736 }1762 }
1737 <Allowances<T>>::insert(collection.id, (item_id, sender.as_sub(), spender.as_sub()), allowance);1763 <Allowances<T>>::insert(collection.id, (item_id, sender.as_sub(), spender.as_sub()), allowance);
17381764
1765 if matches!(collection.mode, CollectionMode::NFT) {
1766 // TODO: NFT: only one owner may exist for token in ERC721
1767 collection.log(
1768 Vec::from([
1769 eth::APPROVAL_NFT_TOPIC,
1770 eth::address_to_topic(sender.as_eth()),
1771 eth::address_to_topic(spender.as_eth()),
1772 ]),
1773 abi_encode!(uint256(item_id.into())),
1774 );
1775 }
1776
1777 if matches!(collection.mode, CollectionMode::Fungible(_)) {
1778 // TODO: NFT: only one owner may exist for token in ERC20
1779 collection.log(
1780 Vec::from([
1781 eth::APPROVAL_FUNGIBLE_TOPIC,
1782 eth::address_to_topic(sender.as_eth()),
1783 eth::address_to_topic(spender.as_eth()),
1784 ]),
1785 abi_encode!(uint256(allowance.into())),
1786 );
1787 }
17391788
1740 Self::deposit_event(RawEvent::Approved(collection.id, item_id, sender, spender, allowance));1789 Self::deposit_event(RawEvent::Approved(collection.id, item_id, sender, spender, allowance));
1741 Ok(())1790 Ok(())
1791 _ => ()1840 _ => ()
1792 };1841 };
1842
1843 if matches!(collection.mode, CollectionMode::Fungible(_)) {
1844 collection.log(
1845 Vec::from([
1846 eth::APPROVAL_FUNGIBLE_TOPIC,
1847 eth::address_to_topic(from.as_eth()),
1848 eth::address_to_topic(sender.as_eth()),
1849 ]),
1850 abi_encode!(uint256(allowance.into())),
1851 );
1852 }
17931853
1794 Ok(())1854 Ok(())
1795 }1855 }
2017 .ok_or(Error::<T>::NumOverflow)?;2077 .ok_or(Error::<T>::NumOverflow)?;
2018 <Balance<T>>::insert(collection_id, item_owner.as_sub(), new_balance);2078 <Balance<T>>::insert(collection_id, item_owner.as_sub(), new_balance);
20192079
2080 collection.log(
2081 Vec::from([
2082 eth::TRANSFER_NFT_TOPIC,
2083 eth::address_to_topic(&H160::default()),
2084 eth::address_to_topic(item_owner.as_eth()),
2085 ]),
2086 abi_encode!(uint256(current_index.into())),
2087 );
2020 Self::deposit_event(RawEvent::ItemCreated(collection_id, current_index, item_owner));2088 Self::deposit_event(RawEvent::ItemCreated(collection_id, current_index, item_owner));
2021 Ok(())2089 Ok(())
2022 }2090 }
2103 <FungibleItemList<T>>::remove(collection_id, owner.as_sub());2171 <FungibleItemList<T>>::remove(collection_id, owner.as_sub());
2104 }2172 }
21052173
2174 collection.log(
2175 Vec::from([
2176 eth::TRANSFER_FUNGIBLE_TOPIC,
2177 eth::address_to_topic(owner.as_eth()),
2178 eth::address_to_topic(&H160::default()),
2179 ]),
2180 abi_encode!(uint256(value.into())),
2181 );
2106 Ok(())2182 Ok(())
2107 }2183 }
21082184
2109 pub fn get_collection(collection_id: CollectionId) -> Result<CollectionHandle<T>, sp_runtime::DispatchError> {2185 pub fn get_collection(collection_id: CollectionId) -> Result<CollectionHandle<T>, sp_runtime::DispatchError> {
2110 Ok(<CollectionById<T>>::get(collection_id)2186 Ok(<CollectionHandle<T>>::get(collection_id)
2111 .map(|collection| CollectionHandle {
2112 id: collection_id,
2113 collection
2114 })
2115 .ok_or(Error::<T>::CollectionNotFound)?)2187 .ok_or(Error::<T>::CollectionNotFound)?)
2116 }2188 }
21172189
2118 fn save_collection(collection: CollectionHandle<T>) {2190 fn save_collection(collection: CollectionHandle<T>) {
2119 <CollectionById<T>>::insert(collection.id, collection.collection);2191 <CollectionById<T>>::insert(collection.id, collection.into_inner());
2120 }2192 }
21212193
2194 fn submit_logs(collection: CollectionHandle<T>) -> DispatchResult {
2195 T::EthereumTransactionSender::submit_logs_transaction(
2196 eth::generate_transaction(collection.id, T::EthereumChainId::get()),
2197 collection.logs.retrieve_logs_for_contract(eth::collection_id_to_address(collection.id)),
2198 )
2199 }
21222200
2123 fn check_owner_permissions(target_collection: &CollectionHandle<T>, subject: &T::CrossAccountId) -> DispatchResult {2201 fn check_owner_permissions(target_collection: &CollectionHandle<T>, subject: &T::CrossAccountId) -> DispatchResult {
2124 ensure!(2202 ensure!(
2224 <FungibleItemList<T>>::insert(collection_id, owner.as_sub(), balance);2302 <FungibleItemList<T>>::insert(collection_id, owner.as_sub(), balance);
2225 }2303 }
22262304
2305 collection.log(
2306 Vec::from([
2307 eth::TRANSFER_FUNGIBLE_TOPIC,
2308 eth::address_to_topic(owner.as_eth()),
2309 eth::address_to_topic(recipient.as_eth()),
2310 ]),
2311 abi_encode!(uint256(value.into())),
2312 );
2227 Self::deposit_event(RawEvent::Transfer(collection.id, 1, owner.clone(), recipient.clone(), value));2313 Self::deposit_event(RawEvent::Transfer(collection.id, 1, owner.clone(), recipient.clone(), value));
22282314
2229 Ok(())2315 Ok(())
2348 // update index collection2434 // update index collection
2349 Self::move_token_index(collection_id, item_id, &old_owner, &new_owner)?;2435 Self::move_token_index(collection_id, item_id, &old_owner, &new_owner)?;
23502436
2437 collection.log(
2438 Vec::from([
2439 eth::TRANSFER_NFT_TOPIC,
2440 eth::address_to_topic(sender.as_eth()),
2441 eth::address_to_topic(new_owner.as_eth()),
2442 ]),
2443 abi_encode!(uint256(item_id.into())),
2444 );
2351 Self::deposit_event(RawEvent::Transfer(collection.id, item_id, sender, new_owner, 1));2445 Self::deposit_event(RawEvent::Transfer(collection.id, item_id, sender, new_owner, 1));
23522446
2353 Ok(())2447 Ok(())
modifiedruntime/src/lib.rsdiffbeforeafterboth
381 type Event = Event;381 type Event = Event;
382 type FindAuthor = EthereumFindAuthor<Aura>;382 type FindAuthor = EthereumFindAuthor<Aura>;
383 type StateRoot = pallet_ethereum::IntermediateStateRoot;383 type StateRoot = pallet_ethereum::IntermediateStateRoot;
384 type EvmSubmitLog = pallet_evm::Module<Runtime>;
384}385}
385386
386impl pallet_grandpa::Config for Runtime {387impl pallet_grandpa::Config for Runtime {
589 type CollectionCreationPrice = CollectionCreationPrice;590 type CollectionCreationPrice = CollectionCreationPrice;
590 type TreasuryAccountId = TreasuryAccountId;591 type TreasuryAccountId = TreasuryAccountId;
592
593 type EthereumChainId = ChainId;
594 type EthereumTransactionSender = pallet_ethereum::Module<Runtime>;
591}595}
592596
593construct_runtime!(597construct_runtime!(