difftreelog
feat log recording
in: master
2 files changed
pallets/nft/src/lib.rsdiffbeforeafterboth43};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;484849#[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}189206190impl<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>;491492 type EthereumChainId: Get<u64>;493 type EthereumTransactionSender: pallet_ethereum::EthereumTransactionSender;474}494}475495476#[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)?;112711471148 Self::submit_logs(target_collection)?;1128 Ok(())1149 Ok(())1129 }1150 }11301151115811791159 Self::create_multiple_items_internal(sender, &collection, owner, items_data)?;1180 Self::create_multiple_items_internal(sender, &collection, owner, items_data)?;116011811182 Self::submit_logs(collection)?;1161 Ok(())1183 Ok(())1162 }1184 }11631185118312051184 Self::burn_item_internal(&sender, &target_collection, item_id, value)?;1206 Self::burn_item_internal(&sender, &target_collection, item_id, value)?;118512071208 Self::submit_logs(target_collection)?;1186 Ok(())1209 Ok(())1187 }1210 }11881211121712401218 Self::transfer_internal(sender, recipient, &collection, item_id, value)?;1241 Self::transfer_internal(sender, recipient, &collection, item_id, value)?;121912421243 Self::submit_logs(collection)?;1220 Ok(())1244 Ok(())1221 }1245 }12221246124412681245 Self::approve_internal(sender, spender, &collection, item_id, amount)?;1269 Self::approve_internal(sender, spender, &collection, item_id, amount)?;124612701271 Self::submit_logs(collection)?;1247 Ok(())1272 Ok(())1248 }1273 }1249 1274 127513001276 Self::transfer_from_internal(sender, from, recipient, &collection, item_id, value)?;1301 Self::transfer_from_internal(sender, from, recipient, &collection, item_id, value)?;127713021303 Self::submit_logs(collection)?;1278 Ok(())1304 Ok(())1279 }1305 }128013061736 }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);173817641765 if matches!(collection.mode, CollectionMode::NFT) {1766 // TODO: NFT: only one owner may exist for token in ERC7211767 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 }17761777 if matches!(collection.mode, CollectionMode::Fungible(_)) {1778 // TODO: NFT: only one owner may exist for token in ERC201779 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 }173917881740 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 };18421843 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 }179318531794 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);201920792080 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 }210521732174 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 }210821842109 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 collection2114 })2115 .ok_or(Error::<T>::CollectionNotFound)?)2187 .ok_or(Error::<T>::CollectionNotFound)?)2116 }2188 }211721892118 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 }212121932194 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 }212222002123 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 }222623042305 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));222823142229 Ok(())2315 Ok(())2348 // update index collection2434 // update index collection2349 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)?;235024362437 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));235224462353 Ok(())2447 Ok(())runtime/src/lib.rsdiffbeforeafterboth381 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}385386386impl 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;592593 type EthereumChainId = ChainId;594 type EthereumTransactionSender = pallet_ethereum::Module<Runtime>;591}595}592596593construct_runtime!(597construct_runtime!(