From 8e8a175523283f9208dae6da134efd4343b8a16b Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Wed, 02 Jun 2021 19:20:42 +0000 Subject: [PATCH] refactor: switch to macro-based ERC logs --- --- 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)>>); +pub struct LogRecorder(RefCell>); impl LogRecorder { pub fn is_empty(&self) -> bool { self.0.borrow().is_empty() } - pub fn log(&self, topics: Vec, data: super::abi::AbiWriter) { - self.0.borrow_mut().push((topics, data.finish())); - } - fn retrieve_logs(self) -> Vec<(Vec, Vec)> { - 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 { - // 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 { + self.0.into_inner() } } --- a/pallets/nft/src/lib.rs +++ b/pallets/nft/src/lib.rs @@ -57,6 +57,7 @@ pub use eth::NftErcSupport; pub use eth::account::*; +use eth::erc::{ERC20Events, ERC721Events}; pub const MAX_DECIMAL_POINTS: DecimalPoints = 30; pub const MAX_REFUNGIBLE_PIECES: u128 = 1_000_000_000_000_000_000_000; @@ -197,8 +198,8 @@ logs: eth::log::LogRecorder::default(), }) } - pub fn log(&self, topics: Vec, data: eth::abi::AbiWriter) { - self.logs.log(topics, data) + pub fn log(&self, log: impl evm_coder::ToLog) { + self.logs.log(log.to_log(self.evm_address)) } pub fn into_inner(self) -> Collection { self.collection.clone() @@ -1758,27 +1759,20 @@ if matches!(collection.mode, CollectionMode::NFT) { // TODO: NFT: only one owner may exist for token in ERC721 - collection.log( - Vec::from([ - eth::APPROVAL_NFT_TOPIC, - eth::address_to_topic(sender.as_eth()), - eth::address_to_topic(spender.as_eth()), - eth::u32_to_topic(item_id), - ]), - abi_encode!(), - ); + collection.log(ERC721Events::Approval { + owner: *sender.as_eth(), + approved: *spender.as_eth(), + token_id: item_id.into(), + }); } if matches!(collection.mode, CollectionMode::Fungible(_)) { // TODO: NFT: only one owner may exist for token in ERC20 - collection.log( - Vec::from([ - eth::APPROVAL_FUNGIBLE_TOPIC, - eth::address_to_topic(sender.as_eth()), - eth::address_to_topic(spender.as_eth()), - ]), - abi_encode!(uint256(allowance.into())), - ); + collection.log(ERC20Events::Approval { + owner: *sender.as_eth(), + spender: *spender.as_eth(), + value: allowance.into() + }); } Self::deposit_event(RawEvent::Approved(collection.id, item_id, sender.clone(), spender.clone(), allowance)); @@ -1836,14 +1830,11 @@ }; if matches!(collection.mode, CollectionMode::Fungible(_)) { - collection.log( - Vec::from([ - eth::APPROVAL_FUNGIBLE_TOPIC, - eth::address_to_topic(from.as_eth()), - eth::address_to_topic(sender.as_eth()), - ]), - abi_encode!(uint256(allowance.into())), - ); + collection.log(ERC20Events::Approval { + owner: *from.as_eth(), + spender: *sender.as_eth(), + value: allowance.into() + }); } Ok(()) @@ -2115,15 +2106,11 @@ .ok_or(Error::::NumOverflow)?; >::insert(collection_id, item_owner.as_sub(), new_balance); - collection.log( - Vec::from([ - eth::TRANSFER_NFT_TOPIC, - eth::address_to_topic(&H160::default()), - eth::address_to_topic(item_owner.as_eth()), - eth::u32_to_topic(current_index), - ]), - abi_encode!(), - ); + collection.log(ERC721Events::Transfer { + from: H160::default(), + to: *item_owner.as_eth(), + token_id: current_index.into(), + }); Self::deposit_event(RawEvent::ItemCreated(collection_id, current_index, item_owner)); Ok(()) } @@ -2210,14 +2197,11 @@ >::remove(collection_id, owner.as_sub()); } - collection.log( - Vec::from([ - eth::TRANSFER_FUNGIBLE_TOPIC, - eth::address_to_topic(owner.as_eth()), - eth::address_to_topic(&H160::default()), - ]), - abi_encode!(uint256(value.into())), - ); + collection.log(ERC20Events::Transfer { + from: *owner.as_eth(), + to: H160::default(), + value: value.into(), + }); Ok(()) } @@ -2236,7 +2220,7 @@ } T::EthereumTransactionSender::submit_logs_transaction( eth::generate_transaction(collection.id, T::EthereumChainId::get()), - collection.logs.retrieve_logs_for_contract(eth::collection_id_to_address(collection.id)), + collection.logs.retrieve_logs(), ) } @@ -2344,14 +2328,11 @@ >::insert(collection_id, owner.as_sub(), balance); } - collection.log( - Vec::from([ - eth::TRANSFER_FUNGIBLE_TOPIC, - eth::address_to_topic(owner.as_eth()), - eth::address_to_topic(recipient.as_eth()), - ]), - abi_encode!(uint256(value.into())), - ); + collection.log(ERC20Events::Transfer { + from: *owner.as_eth(), + to: *recipient.as_eth(), + value: value.into(), + }); Self::deposit_event(RawEvent::Transfer(collection.id, 1, owner.clone(), recipient.clone(), value)); Ok(()) @@ -2476,15 +2457,11 @@ // update index collection Self::move_token_index(collection_id, item_id, &old_owner, &new_owner)?; - collection.log( - Vec::from([ - eth::TRANSFER_NFT_TOPIC, - eth::address_to_topic(sender.as_eth()), - eth::address_to_topic(new_owner.as_eth()), - eth::u32_to_topic(item_id), - ]), - abi_encode!(), - ); + collection.log(ERC721Events::Transfer { + from: *sender.as_eth(), + to: *new_owner.as_eth(), + token_id: item_id.into(), + }); Self::deposit_event(RawEvent::Transfer(collection.id, item_id, sender, new_owner, 1)); Ok(()) -- gitstuff