difftreelog
refactor switch to macro-based ERC logs
in: master
2 files changed
pallets/nft/src/eth/log.rsdiffbeforeafterboth--- a/pallets/nft/src/eth/log.rs
+++ b/pallets/nft/src/eth/log.rs
@@ -1,41 +1,19 @@
-use pallet_evm::PrecompileLog;
use sp_std::cell::RefCell;
use sp_std::vec::Vec;
use sp_core::{H160, H256};
+use ethereum::Log;
#[derive(Default)]
-pub struct LogRecorder(RefCell<Vec<(Vec<H256>, Vec<u8>)>>);
+pub struct LogRecorder(RefCell<Vec<Log>>);
impl LogRecorder {
pub fn is_empty(&self) -> bool {
self.0.borrow().is_empty()
}
- pub fn log(&self, topics: Vec<H256>, data: super::abi::AbiWriter) {
- self.0.borrow_mut().push((topics, data.finish()));
- }
- fn retrieve_logs(self) -> Vec<(Vec<H256>, Vec<u8>)> {
- self.0.replace(Vec::new())
+ pub fn log(&self, log: Log) {
+ self.0.borrow_mut().push(log);
}
- pub fn retrieve_logs_for_contract(self, contract: H160) -> Vec<PrecompileLog> {
- // TODO: Remove reallocation
- self.retrieve_logs().into_iter()
- .map(|(topics, data)| PrecompileLog(contract.clone(), topics, data))
- .collect()
- }
-}
-impl Drop for LogRecorder {
- fn drop(&mut self) {
- #[cfg(feature = "std")]
- {
- // In debug mode, log recorder panics if dropped with logs left
- let logs = self.0.borrow();
- if !logs.is_empty() {
- eprintln!("Logs lost:");
- for line in logs.iter() {
- eprintln!("{:?} {:?}", line.0, line.1);
- }
- panic!();
- }
- }
+ pub fn retrieve_logs(self) -> Vec<Log> {
+ self.0.into_inner()
}
}
pallets/nft/src/lib.rsdiffbeforeafterboth575758pub use eth::NftErcSupport;58pub use eth::NftErcSupport;59pub use eth::account::*;59pub use eth::account::*;60use eth::erc::{ERC20Events, ERC721Events};606161pub const MAX_DECIMAL_POINTS: DecimalPoints = 30;62pub const MAX_DECIMAL_POINTS: DecimalPoints = 30;62pub const MAX_REFUNGIBLE_PIECES: u128 = 1_000_000_000_000_000_000_000;63pub const MAX_REFUNGIBLE_PIECES: u128 = 1_000_000_000_000_000_000_000;197 logs: eth::log::LogRecorder::default(),198 logs: eth::log::LogRecorder::default(),198 })199 })199 }200 }200 pub fn log(&self, topics: Vec<H256>, data: eth::abi::AbiWriter) {201 pub fn log(&self, log: impl evm_coder::ToLog) {201 self.logs.log(topics, data)202 self.logs.log(log.to_log(self.evm_address))202 }203 }203 pub fn into_inner(self) -> Collection<T> {204 pub fn into_inner(self) -> Collection<T> {204 self.collection.clone()205 self.collection.clone()175817591759 if matches!(collection.mode, CollectionMode::NFT) {1760 if matches!(collection.mode, CollectionMode::NFT) {1760 // TODO: NFT: only one owner may exist for token in ERC7211761 // TODO: NFT: only one owner may exist for token in ERC7211761 collection.log(1762 collection.log(ERC721Events::Approval {1762 Vec::from([1763 eth::APPROVAL_NFT_TOPIC,1764 eth::address_to_topic(sender.as_eth()),1763 owner: *sender.as_eth(),1765 eth::address_to_topic(spender.as_eth()),1764 approved: *spender.as_eth(),1766 eth::u32_to_topic(item_id),1765 token_id: item_id.into(),1767 ]),1768 abi_encode!(),1769 );1766 });1770 }1767 }177117681772 if matches!(collection.mode, CollectionMode::Fungible(_)) {1769 if matches!(collection.mode, CollectionMode::Fungible(_)) {1773 // TODO: NFT: only one owner may exist for token in ERC201770 // TODO: NFT: only one owner may exist for token in ERC201774 collection.log(1771 collection.log(ERC20Events::Approval {1775 Vec::from([1776 eth::APPROVAL_FUNGIBLE_TOPIC,1777 eth::address_to_topic(sender.as_eth()),1772 owner: *sender.as_eth(),1778 eth::address_to_topic(spender.as_eth()),1773 spender: *spender.as_eth(),1779 ]),1774 value: allowance.into()1780 abi_encode!(uint256(allowance.into())),1781 );1775 });1782 }1776 }178317771784 Self::deposit_event(RawEvent::Approved(collection.id, item_id, sender.clone(), spender.clone(), allowance));1778 Self::deposit_event(RawEvent::Approved(collection.id, item_id, sender.clone(), spender.clone(), allowance));1836 };1830 };183718311838 if matches!(collection.mode, CollectionMode::Fungible(_)) {1832 if matches!(collection.mode, CollectionMode::Fungible(_)) {1839 collection.log(1833 collection.log(ERC20Events::Approval {1840 Vec::from([1841 eth::APPROVAL_FUNGIBLE_TOPIC,1842 eth::address_to_topic(from.as_eth()),1834 owner: *from.as_eth(),1843 eth::address_to_topic(sender.as_eth()),1835 spender: *sender.as_eth(),1844 ]),1836 value: allowance.into()1845 abi_encode!(uint256(allowance.into())),1846 );1837 });1847 }1838 }184818391849 Ok(())1840 Ok(())2115 .ok_or(Error::<T>::NumOverflow)?;2106 .ok_or(Error::<T>::NumOverflow)?;2116 <Balance<T>>::insert(collection_id, item_owner.as_sub(), new_balance);2107 <Balance<T>>::insert(collection_id, item_owner.as_sub(), new_balance);211721082118 collection.log(2109 collection.log(ERC721Events::Transfer {2119 Vec::from([2120 eth::TRANSFER_NFT_TOPIC,2121 eth::address_to_topic(&H160::default()),2110 from: H160::default(),2122 eth::address_to_topic(item_owner.as_eth()),2111 to: *item_owner.as_eth(),2123 eth::u32_to_topic(current_index),2112 token_id: current_index.into(),2124 ]),2125 abi_encode!(),2126 );2113 });2127 Self::deposit_event(RawEvent::ItemCreated(collection_id, current_index, item_owner));2114 Self::deposit_event(RawEvent::ItemCreated(collection_id, current_index, item_owner));2128 Ok(())2115 Ok(())2129 }2116 }2210 <FungibleItemList<T>>::remove(collection_id, owner.as_sub());2197 <FungibleItemList<T>>::remove(collection_id, owner.as_sub());2211 }2198 }221221992213 collection.log(2200 collection.log(ERC20Events::Transfer {2214 Vec::from([2201 from: *owner.as_eth(),2215 eth::TRANSFER_FUNGIBLE_TOPIC,2216 eth::address_to_topic(owner.as_eth()),2217 eth::address_to_topic(&H160::default()),2202 to: H160::default(),2218 ]),2203 value: value.into(),2219 abi_encode!(uint256(value.into())),2220 );2204 });2221 Ok(())2205 Ok(())2222 }2206 }222322072236 }2220 }2237 T::EthereumTransactionSender::submit_logs_transaction(2221 T::EthereumTransactionSender::submit_logs_transaction(2238 eth::generate_transaction(collection.id, T::EthereumChainId::get()),2222 eth::generate_transaction(collection.id, T::EthereumChainId::get()),2239 collection.logs.retrieve_logs_for_contract(eth::collection_id_to_address(collection.id)),2223 collection.logs.retrieve_logs(),2240 )2224 )2241 }2225 }224222262344 <FungibleItemList<T>>::insert(collection_id, owner.as_sub(), balance);2328 <FungibleItemList<T>>::insert(collection_id, owner.as_sub(), balance);2345 }2329 }234623302347 collection.log(2331 collection.log(ERC20Events::Transfer {2348 Vec::from([2332 from: *owner.as_eth(),2349 eth::TRANSFER_FUNGIBLE_TOPIC,2350 eth::address_to_topic(owner.as_eth()),2351 eth::address_to_topic(recipient.as_eth()),2333 to: *recipient.as_eth(),2352 ]),2334 value: value.into(),2353 abi_encode!(uint256(value.into())),2354 );2335 });2355 Self::deposit_event(RawEvent::Transfer(collection.id, 1, owner.clone(), recipient.clone(), value));2336 Self::deposit_event(RawEvent::Transfer(collection.id, 1, owner.clone(), recipient.clone(), value));235623372357 Ok(())2338 Ok(())2476 // update index collection2457 // update index collection2477 Self::move_token_index(collection_id, item_id, &old_owner, &new_owner)?;2458 Self::move_token_index(collection_id, item_id, &old_owner, &new_owner)?;247824592479 collection.log(2460 collection.log(ERC721Events::Transfer {2480 Vec::from([2461 from: *sender.as_eth(),2481 eth::TRANSFER_NFT_TOPIC,2482 eth::address_to_topic(sender.as_eth()),2483 eth::address_to_topic(new_owner.as_eth()),2462 to: *new_owner.as_eth(),2484 eth::u32_to_topic(item_id),2463 token_id: item_id.into(),2485 ]),2486 abi_encode!(),2487 );2464 });2488 Self::deposit_event(RawEvent::Transfer(collection.id, item_id, sender, new_owner, 1));2465 Self::deposit_event(RawEvent::Transfer(collection.id, item_id, sender, new_owner, 1));248924662490 Ok(())2467 Ok(())